defining stuff in headers confuses the linker
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn-helper.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file vpn/gnunet-daemon-vpn-helper.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include <platform.h>
27 #include <gnunet_common.h>
28 #include <gnunet_client_lib.h>
29 #include <gnunet_os_lib.h>
30 #include <gnunet_mesh_service.h>
31 #include <gnunet_protocols.h>
32 #include <gnunet_server_lib.h>
33 #include <gnunet_container_lib.h>
34 #include <block_dns.h>
35 #include <gnunet_configuration_lib.h>
36
37 #include "gnunet-daemon-vpn-dns.h"
38 #include "gnunet-daemon-vpn.h"
39 #include "gnunet-daemon-vpn-helper.h"
40 #include "gnunet-service-dns-p.h"
41 #include "gnunet-vpn-packet.h"
42
43 struct GNUNET_VPN_HELPER_Handle *helper_handle;
44
45 /**
46  * Start the helper-process
47  *
48  * If cls != NULL it is assumed that this function is called as a result of a dying
49  * helper. cls is then taken as handle to the old helper and is cleaned up.
50  * {{{
51  */
52 void
53 start_helper_and_schedule(void *cls,
54                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
55     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
56       return;
57
58     if (cls != NULL)
59       cleanup_helper(cls);
60     cls = NULL;
61
62     char* ifname;
63     char* ipv6addr;
64     char* ipv6prefix;
65     char* ipv4addr;
66     char* ipv4mask;
67
68     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IFNAME", &ifname))
69       {
70         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
71         exit(1);
72       }
73
74     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr))
75       {
76         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
77         exit(1);
78       }
79
80     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6PREFIX", &ipv6prefix))
81       {
82         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
83         exit(1);
84       }
85
86     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4ADDR", &ipv4addr))
87       {
88         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
89         exit(1);
90       }
91
92     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4MASK", &ipv4mask))
93       {
94         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
95         exit(1);
96       }
97
98     /* Start the helper
99      * Messages get passed to the function message_token
100      * When the helper dies, this function will be called again with the
101      * helper_handle as cls.
102      */
103     helper_handle = start_helper(ifname,
104                                  ipv6addr,
105                                  ipv6prefix,
106                                  ipv4addr,
107                                  ipv4mask,
108                                  "vpn-gnunet",
109                                  start_helper_and_schedule,
110                                  message_token,
111                                  NULL,
112                                  NULL);
113
114     GNUNET_free(ipv6addr);
115     GNUNET_free(ipv6prefix);
116     GNUNET_free(ipv4addr);
117     GNUNET_free(ipv4mask);
118     GNUNET_free(ifname);
119
120     /* Tell the dns-service to rehijack the dns-port
121      * The routing-table gets flushed if an interface disappears.
122      */
123     restart_hijack = 1;
124     GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
125 }
126 /*}}}*/
127
128 /**
129  * Send an dns-answer-packet to the helper
130  */
131 void
132 helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
133     if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
134       return;
135
136     struct answer_packet_list* ans = answer_proc_head;
137     size_t len = ntohs(ans->pkt.hdr.size);
138
139     GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
140
141     GNUNET_assert (20 == sizeof (struct ip_hdr));
142     GNUNET_assert (8 == sizeof (struct udp_pkt));
143     size_t data_len = len - sizeof(struct answer_packet) + 1;
144     size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
145     size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
146
147     struct ip_udp_dns* pkt = alloca(pkt_len);
148     GNUNET_assert(pkt != NULL);
149     memset(pkt, 0, pkt_len);
150
151     /* set the gnunet-header */
152     pkt->shdr.size = htons(pkt_len);
153     pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
154
155     /* set the tun-header (no flags and ethertype of IPv4) */
156     pkt->tun.flags = 0;
157     pkt->tun.type = htons(0x0800);
158
159     /* set the ip-header */
160     pkt->ip_hdr.version = 4;
161     pkt->ip_hdr.hdr_lngth = 5;
162     pkt->ip_hdr.diff_serv = 0;
163     pkt->ip_hdr.tot_lngth = htons(net_len);
164     pkt->ip_hdr.ident = 0;
165     pkt->ip_hdr.flags = 0;
166     pkt->ip_hdr.frag_off = 0;
167     pkt->ip_hdr.ttl = 255;
168     pkt->ip_hdr.proto = 0x11; /* UDP */
169     pkt->ip_hdr.chks = 0; /* Will be calculated later*/
170     pkt->ip_hdr.sadr = ans->pkt.from;
171     pkt->ip_hdr.dadr = ans->pkt.to;
172
173     pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
174
175     /* set the udp-header */
176     pkt->udp_dns.udp_hdr.spt = htons(53);
177     pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
178     pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
179     pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
180
181     memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
182
183     GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
184     GNUNET_free(ans);
185
186     /* FIXME */ GNUNET_DISK_file_write(helper_handle->fh_to_helper, pkt, pkt_len);
187
188     /* if more packets are available, reschedule */
189     if (answer_proc_head != NULL)
190       GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
191                                        helper_handle->fh_to_helper,
192                                        &helper_write,
193                                        NULL);
194 }
195 /**
196  * Receive packets from the helper-process
197  */
198 void
199 message_token(void *cls,
200               void *client,
201               const struct GNUNET_MessageHeader *message) {
202     GNUNET_assert(ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
203
204     struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
205
206     /* ethertype is ipv6 */
207     if (ntohs(pkt_tun->tun.type) == 0x86dd)
208       {
209         struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
210         GNUNET_assert(pkt6->ip6_hdr.version == 6);
211         struct ip6_tcp *pkt6_tcp;
212         struct ip6_udp *pkt6_udp;
213         struct ip6_icmp *pkt6_icmp;
214         GNUNET_HashCode* key;
215
216         switch(pkt6->ip6_hdr.nxthdr)
217           {
218           case 0x06:
219             pkt6_tcp = (struct ip6_tcp*)pkt6;
220             break;
221           case 0x11:
222             pkt6_udp = (struct ip6_udp*)pkt6;
223             if ((key = address_mapping_exists(pkt6->ip6_hdr.dadr)) != NULL)
224               {
225                 struct map_entry* me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
226                 GNUNET_assert(me != NULL);
227                 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping exists; type: %d; UDP is %d; port: %x/%x!\n", me->desc.service_type, htonl(GNUNET_DNS_SERVICE_TYPE_UDP), pkt6_udp->udp_hdr.dpt, me->desc.ports);
228                 GNUNET_free(key);
229                 if (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP) &&
230                     (port_in_ports(me->desc.ports, pkt6_udp->udp_hdr.dpt) ||
231                      port_in_ports(me->additional_ports, pkt6_udp->udp_hdr.dpt)))
232                   {
233                     size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + sizeof(GNUNET_HashCode) + ntohs(pkt6_udp->udp_hdr.len);
234                     struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc(size);
235                     struct GNUNET_MessageHeader *hdr = (struct GNUNET_MessageHeader*)(cls+1);
236                     GNUNET_HashCode* hc = (GNUNET_HashCode*)(hdr + 1);
237
238                     memcpy(hc, &me->desc.service_descriptor, sizeof(GNUNET_HashCode));
239                     memcpy(hc+1, &pkt6_udp->udp_hdr, ntohs(pkt6_udp->udp_hdr.len));
240
241                     if (me->tunnel == NULL)
242                       {
243                         *cls = GNUNET_MESH_peer_request_connect_all(mesh_handle,
244                                                                     GNUNET_TIME_UNIT_FOREVER_REL,
245                                                                     1,
246                                                                     (struct GNUNET_PeerIdentity*)&me->desc.peer,
247                                                                     send_udp_to_peer,
248                                                                     NULL,
249                                                                     cls);
250                         me->tunnel = *cls;
251                       }
252                     else
253                       {
254                         *cls = me->tunnel;
255                         send_udp_to_peer(cls, (struct GNUNET_PeerIdentity*)1, NULL);
256                       }
257                     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Queued to send to peer %x\n", *((unsigned int*)&me->desc.peer));
258                   }
259               }
260             break;
261           case 0x3a:
262             /* ICMPv6 */
263             pkt6_icmp = (struct ip6_icmp*)pkt6;
264             /* If this packet is an icmp-echo-request and a mapping exists, answer */
265             if (pkt6_icmp->icmp_hdr.type == 0x80 && (key = address_mapping_exists(pkt6->ip6_hdr.dadr)) != NULL)
266               {
267                 GNUNET_free(key);
268                 pkt6_icmp = GNUNET_malloc(ntohs(pkt6->shdr.size));
269                 memcpy(pkt6_icmp, pkt6, ntohs(pkt6->shdr.size));
270                 GNUNET_SCHEDULER_add_now(&send_icmp_response, pkt6_icmp);
271               }
272             break;
273           }
274       }
275     /* ethertype is ipv4 */
276     else if (ntohs(pkt_tun->tun.type) == 0x0800)
277       {
278         struct ip_pkt *pkt = (struct ip_pkt*) message;
279         struct ip_udp *udp = (struct ip_udp*) message;
280         GNUNET_assert(pkt->ip_hdr.version == 4);
281
282         /* Send dns-packets to the service-dns */
283         if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 )
284           {
285             /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
286             size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9;
287
288             struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
289             query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
290             query->pkt.hdr.size = htons(len);
291             query->pkt.orig_to = pkt->ip_hdr.dadr;
292             query->pkt.orig_from = pkt->ip_hdr.sadr;
293             query->pkt.src_port = udp->udp_hdr.spt;
294             memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
295
296             GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, query);
297
298             GNUNET_assert(head != NULL);
299
300             if (dns_connection != NULL)
301               GNUNET_CLIENT_notify_transmit_ready(dns_connection,
302                                                   len,
303                                                   GNUNET_TIME_UNIT_FOREVER_REL,
304                                                   GNUNET_YES,
305                                                   &send_query,
306                                                   NULL);
307           }
308       }
309 }
310
311 void write_to_helper(void* buf, size_t len)
312 {
313   (void)GNUNET_DISK_file_write(helper_handle->fh_to_helper, buf, len);
314 }
315
316 void schedule_helper_write(struct GNUNET_TIME_Relative time, void* cls)
317 {
318   GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper, &helper_write, cls);
319 }