indentation
[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 #include <gnunet_applications.h>
37
38 #include "gnunet-daemon-vpn-dns.h"
39 #include "gnunet-daemon-vpn.h"
40 #include "gnunet-daemon-vpn-helper.h"
41 #include "gnunet-service-dns-p.h"
42 #include "gnunet-vpn-packet.h"
43 #include "gnunet-vpn-checksum.h"
44 #include "gnunet-helper-vpn-api.h"
45
46 struct GNUNET_VPN_HELPER_Handle *helper_handle;
47
48 /**
49  * The tunnels that will be used to send tcp- and udp-packets
50  */
51 static struct GNUNET_MESH_Tunnel *tcp_tunnel;
52 static struct GNUNET_MESH_Tunnel *udp_tunnel;
53
54 /**
55  * Start the helper-process
56  *
57  * If cls != NULL it is assumed that this function is called as a result of a dying
58  * helper. cls is then taken as handle to the old helper and is cleaned up.
59  * {{{
60  */
61 void
62 start_helper_and_schedule (void *cls,
63                            const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   shs_task = GNUNET_SCHEDULER_NO_TASK;
66   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
67     return;
68
69   if (cls != NULL)
70     cleanup_helper (cls);
71   cls = NULL;
72
73   char *ifname;
74   char *ipv6addr;
75   char *ipv6prefix;
76   char *ipv4addr;
77   char *ipv4mask;
78
79   if (GNUNET_SYSERR ==
80       GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
81   {
82     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83                 "No entry 'IFNAME' in configuration!\n");
84     exit (1);
85   }
86
87   if (GNUNET_SYSERR ==
88       GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", &ipv6addr))
89   {
90     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
91                 "No entry 'IPV6ADDR' in configuration!\n");
92     exit (1);
93   }
94
95   if (GNUNET_SYSERR ==
96       GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
97                                              &ipv6prefix))
98   {
99     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
100                 "No entry 'IPV6PREFIX' in configuration!\n");
101     exit (1);
102   }
103
104   if (GNUNET_SYSERR ==
105       GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR", &ipv4addr))
106   {
107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108                 "No entry 'IPV4ADDR' in configuration!\n");
109     exit (1);
110   }
111
112   if (GNUNET_SYSERR ==
113       GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK", &ipv4mask))
114   {
115     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116                 "No entry 'IPV4MASK' in configuration!\n");
117     exit (1);
118   }
119
120   /* Start the helper
121    * Messages get passed to the function message_token
122    * When the helper dies, this function will be called again with the
123    * helper_handle as cls.
124    */
125   helper_handle =
126       start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
127                     "vpn-gnunet", start_helper_and_schedule, message_token,
128                     NULL);
129
130   GNUNET_free (ipv6addr);
131   GNUNET_free (ipv6prefix);
132   GNUNET_free (ipv4addr);
133   GNUNET_free (ipv4mask);
134   GNUNET_free (ifname);
135
136   /* Tell the dns-service to rehijack the dns-port
137    * The routing-table gets flushed if an interface disappears.
138    */
139   restart_hijack = 1;
140   if (NULL != dns_connection)
141     GNUNET_CLIENT_notify_transmit_ready (dns_connection,
142                                          sizeof (struct GNUNET_MessageHeader),
143                                          GNUNET_TIME_UNIT_FOREVER_REL,
144                                          GNUNET_YES, &send_query, NULL);
145
146   GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
147                                    helper_handle->fh_to_helper, &helper_write,
148                                    NULL);
149 }
150
151 /*}}}*/
152
153 /**
154  * Send an dns-answer-packet to the helper
155  */
156 void
157 helper_write (void *cls
158               __attribute__ ((unused)),
159               const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
160 {
161   if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
162     return;
163
164   struct answer_packet_list *ans = answer_proc_head;
165
166   if (NULL == ans)
167     return;
168
169   size_t len = ntohs (ans->pkt.hdr.size);
170
171   GNUNET_assert (ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
172
173   GNUNET_assert (20 == sizeof (struct ip_hdr));
174   GNUNET_assert (8 == sizeof (struct udp_pkt));
175   size_t data_len = len - sizeof (struct answer_packet) + 1;
176   size_t net_len = sizeof (struct ip_hdr) + sizeof (struct udp_dns) + data_len;
177   size_t pkt_len =
178       sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + net_len;
179
180   struct ip_udp_dns *pkt = alloca (pkt_len);
181
182   GNUNET_assert (pkt != NULL);
183   memset (pkt, 0, pkt_len);
184
185   /* set the gnunet-header */
186   pkt->shdr.size = htons (pkt_len);
187   pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
188
189   /* set the tun-header (no flags and ethertype of IPv4) */
190   pkt->tun.flags = 0;
191   pkt->tun.type = htons (0x0800);
192
193   /* set the ip-header */
194   pkt->ip_hdr.version = 4;
195   pkt->ip_hdr.hdr_lngth = 5;
196   pkt->ip_hdr.diff_serv = 0;
197   pkt->ip_hdr.tot_lngth = htons (net_len);
198   pkt->ip_hdr.ident = 0;
199   pkt->ip_hdr.flags = 0;
200   pkt->ip_hdr.frag_off = 0;
201   pkt->ip_hdr.ttl = 255;
202   pkt->ip_hdr.proto = 0x11;     /* UDP */
203   pkt->ip_hdr.chks = 0;         /* Will be calculated later */
204   pkt->ip_hdr.sadr = ans->pkt.from;
205   pkt->ip_hdr.dadr = ans->pkt.to;
206
207   pkt->ip_hdr.chks = calculate_ip_checksum ((uint16_t *) & pkt->ip_hdr, 5 * 4);
208
209   /* set the udp-header */
210   pkt->udp_dns.udp_hdr.spt = htons (53);
211   pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
212   pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip_hdr));
213   pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
214
215   memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len);
216
217   GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
218   GNUNET_free (ans);
219
220   if (GNUNET_DISK_file_write (helper_handle->fh_to_helper, pkt, pkt_len) < 0)
221   {
222     cleanup_helper (helper_handle);
223     GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
224     return;
225   }
226
227   /* if more packets are available, reschedule */
228   if (answer_proc_head != NULL)
229     GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
230                                      helper_handle->fh_to_helper, &helper_write,
231                                      NULL);
232 }
233
234 /**
235  * Receive packets from the helper-process
236  */
237 void
238 message_token (void *cls __attribute__ ((unused)), void *client
239                __attribute__ ((unused)),
240                const struct GNUNET_MessageHeader *message)
241 {
242   GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
243
244   struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
245   GNUNET_HashCode *key;
246
247   /* ethertype is ipv6 */
248   if (ntohs (pkt_tun->tun.type) == 0x86dd)
249   {
250     struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
251
252     GNUNET_assert (pkt6->ip6_hdr.version == 6);
253     struct ip6_tcp *pkt6_tcp;
254     struct ip6_udp *pkt6_udp;
255     struct ip6_icmp *pkt6_icmp;
256
257     switch (pkt6->ip6_hdr.nxthdr)
258     {
259     case IPPROTO_TCP:
260     case IPPROTO_UDP:
261       pkt6_tcp = (struct ip6_tcp *) pkt6;
262       pkt6_udp = (struct ip6_udp *) pkt6;
263
264       if ((key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
265       {
266         struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
267
268         GNUNET_assert (me != NULL);
269         GNUNET_free (key);
270
271         size_t size =
272             sizeof (struct GNUNET_MESH_Tunnel *) +
273             sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
274             ntohs (pkt6->ip6_hdr.paylgth);
275
276         struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
277         struct GNUNET_MessageHeader *hdr =
278             (struct GNUNET_MessageHeader *) (cls + 1);
279         GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
280
281         hdr->size =
282             htons (sizeof (struct GNUNET_MessageHeader) +
283                    sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth));
284
285         GNUNET_MESH_ApplicationType app_type;
286
287         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
288                     me->addrlen);
289         if (me->addrlen == 0)
290         {
291           /* This is a mapping to a gnunet-service */
292           memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
293
294           if (0x11 == pkt6->ip6_hdr.nxthdr &&
295               (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
296               (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt) ||
297                testBit (me->additional_ports, ntohs (pkt6_udp->udp_hdr.dpt))))
298           {
299             hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
300
301             memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
302
303           }
304           else if (0x06 == pkt6->ip6_hdr.nxthdr &&
305                    (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
306                    && (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
307           {
308             hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
309
310             memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
311
312           }
313           if (me->tunnel == NULL && NULL != cls)
314           {
315             *cls =
316                 GNUNET_MESH_peer_request_connect_all (mesh_handle,
317                                                       GNUNET_TIME_UNIT_FOREVER_REL,
318                                                       1,
319                                                       (struct
320                                                        GNUNET_PeerIdentity *)
321                                                       &me->desc.peer,
322                                                       send_pkt_to_peer, NULL,
323                                                       cls);
324             me->tunnel = *cls;
325           }
326           else if (NULL != cls)
327           {
328             *cls = me->tunnel;
329             send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
330             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331                         "Queued to send to peer %x, type %d\n",
332                         *((unsigned int *) &me->desc.peer), ntohs (hdr->type));
333           }
334         }
335         else
336         {
337           /* This is a mapping to a "real" address */
338           struct remote_addr *s = (struct remote_addr *) hc;
339
340           s->addrlen = me->addrlen;
341           memcpy (s->addr, me->addr, me->addrlen);
342           s->proto = pkt6->ip6_hdr.nxthdr;
343           if (s->proto == IPPROTO_UDP)
344           {
345             hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
346             memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
347             app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
348             if (NULL != udp_tunnel)
349               me->tunnel = udp_tunnel;
350           }
351           else if (s->proto == IPPROTO_TCP)
352           {
353             hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
354             memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
355             app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
356             if (NULL != tcp_tunnel)
357               me->tunnel = tcp_tunnel;
358           }
359           else
360           {
361             GNUNET_assert (0);
362           }
363           if (me->tunnel == NULL && NULL != cls)
364           {
365             *cls =
366                 GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
367                                                           GNUNET_TIME_UNIT_FOREVER_REL,
368                                                           app_type,
369                                                           send_pkt_to_peer,
370                                                           NULL, cls);
371             me->tunnel = *cls;
372             if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
373               udp_tunnel = *cls;
374             else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type)
375               tcp_tunnel = *cls;
376           }
377           else if (NULL != cls)
378           {
379             *cls = me->tunnel;
380             send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
381           }
382         }
383       }
384       else
385       {
386         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387                     "Packet to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, which has no mapping\n",
388                     pkt6->ip6_hdr.dadr[0], pkt6->ip6_hdr.dadr[1],
389                     pkt6->ip6_hdr.dadr[2], pkt6->ip6_hdr.dadr[3],
390                     pkt6->ip6_hdr.dadr[4], pkt6->ip6_hdr.dadr[5],
391                     pkt6->ip6_hdr.dadr[6], pkt6->ip6_hdr.dadr[7],
392                     pkt6->ip6_hdr.dadr[8], pkt6->ip6_hdr.dadr[9],
393                     pkt6->ip6_hdr.dadr[10], pkt6->ip6_hdr.dadr[11],
394                     pkt6->ip6_hdr.dadr[12], pkt6->ip6_hdr.dadr[13],
395                     pkt6->ip6_hdr.dadr[14], pkt6->ip6_hdr.dadr[15]);
396       }
397       break;
398     case 0x3a:
399       /* ICMPv6 */
400       pkt6_icmp = (struct ip6_icmp *) pkt6;
401       /* If this packet is an icmp-echo-request and a mapping exists, answer */
402       if (pkt6_icmp->icmp_hdr.type == 0x80 &&
403           (key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
404       {
405         GNUNET_free (key);
406         pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
407         memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
408         GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp);
409       }
410       break;
411     }
412   }
413   /* ethertype is ipv4 */
414   else if (ntohs (pkt_tun->tun.type) == 0x0800)
415   {
416     struct ip_pkt *pkt = (struct ip_pkt *) message;
417     struct ip_udp *udp = (struct ip_udp *) message;
418     struct ip_tcp *pkt_tcp;
419     struct ip_udp *pkt_udp;
420     struct ip_icmp *pkt_icmp;
421
422     GNUNET_assert (pkt->ip_hdr.version == 4);
423
424     /* Send dns-packets to the service-dns */
425     if (pkt->ip_hdr.proto == 0x11 && ntohs (udp->udp_hdr.dpt) == 53)
426     {
427       /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
428       size_t len = sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
429
430       struct query_packet_list *query =
431           GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
432       query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS);
433       query->pkt.hdr.size = htons (len);
434       query->pkt.orig_to = pkt->ip_hdr.dadr;
435       query->pkt.orig_from = pkt->ip_hdr.sadr;
436       query->pkt.src_port = udp->udp_hdr.spt;
437       memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
438
439       GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
440
441       GNUNET_assert (head != NULL);
442
443       if (dns_connection != NULL)
444         GNUNET_CLIENT_notify_transmit_ready (dns_connection, len,
445                                              GNUNET_TIME_UNIT_FOREVER_REL,
446                                              GNUNET_YES, &send_query, NULL);
447     }
448     else
449     {
450       uint32_t dadr = pkt->ip_hdr.dadr;
451       unsigned char *c = (unsigned char *) &dadr;
452
453       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n",
454                   c[0], c[1], c[2], c[3], pkt->ip_hdr.proto);
455       switch (pkt->ip_hdr.proto)
456       {
457       case IPPROTO_TCP:
458       case IPPROTO_UDP:
459         pkt_tcp = (struct ip_tcp *) pkt;
460         pkt_udp = (struct ip_udp *) pkt;
461
462         if ((key = address4_mapping_exists (dadr)) != NULL)
463         {
464           struct map_entry *me =
465               GNUNET_CONTAINER_multihashmap_get (hashmap, key);
466           GNUNET_assert (me != NULL);
467           GNUNET_free (key);
468
469           size_t size =
470               sizeof (struct GNUNET_MESH_Tunnel *) +
471               sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
472               ntohs (pkt->ip_hdr.tot_lngth) - 4 * pkt->ip_hdr.hdr_lngth;
473
474           struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
475           struct GNUNET_MessageHeader *hdr =
476               (struct GNUNET_MessageHeader *) (cls + 1);
477           GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
478
479           hdr->size =
480               htons (sizeof (struct GNUNET_MessageHeader) +
481                      sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) -
482                      4 * pkt->ip_hdr.hdr_lngth);
483
484           GNUNET_MESH_ApplicationType app_type;
485
486           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
487                       me->addrlen);
488           if (me->addrlen == 0)
489           {
490             /* This is a mapping to a gnunet-service */
491             memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
492
493             if ((IPPROTO_UDP == pkt->ip_hdr.proto) &&
494                 (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
495                 (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt) ||
496                  testBit (me->additional_ports, ntohs (pkt_udp->udp_hdr.dpt))))
497             {
498               hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
499
500               memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
501
502             }
503             else if ((IPPROTO_TCP == pkt->ip_hdr.proto) &&
504                      (me->
505                       desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
506                      && (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt)))
507             {
508               hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
509
510               memcpy (hc + 1, &pkt_tcp->tcp_hdr,
511                       ntohs (pkt->ip_hdr.tot_lngth) -
512                       4 * pkt->ip_hdr.hdr_lngth);
513
514             }
515             if (me->tunnel == NULL && NULL != cls)
516             {
517               *cls =
518                   GNUNET_MESH_peer_request_connect_all (mesh_handle,
519                                                         GNUNET_TIME_UNIT_FOREVER_REL,
520                                                         1,
521                                                         (struct
522                                                          GNUNET_PeerIdentity *)
523                                                         &me->desc.peer,
524                                                         send_pkt_to_peer, NULL,
525                                                         cls);
526               me->tunnel = *cls;
527             }
528             else if (NULL != cls)
529             {
530               *cls = me->tunnel;
531               send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
532               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533                           "Queued to send to peer %x, type %d\n",
534                           *((unsigned int *) &me->desc.peer),
535                           ntohs (hdr->type));
536             }
537           }
538           else
539           {
540             /* This is a mapping to a "real" address */
541             struct remote_addr *s = (struct remote_addr *) hc;
542
543             s->addrlen = me->addrlen;
544             memcpy (s->addr, me->addr, me->addrlen);
545             s->proto = pkt->ip_hdr.proto;
546             if (s->proto == 0x11)
547             {
548               hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
549               memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
550               app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
551             }
552             else if (s->proto == 0x06)
553             {
554               hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
555               memcpy (hc + 1, &pkt_tcp->tcp_hdr,
556                       ntohs (pkt->ip_hdr.tot_lngth) -
557                       4 * pkt->ip_hdr.hdr_lngth);
558               app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
559             }
560             if (me->tunnel == NULL && NULL != cls)
561             {
562               *cls =
563                   GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
564                                                             GNUNET_TIME_UNIT_FOREVER_REL,
565                                                             app_type,
566                                                             send_pkt_to_peer,
567                                                             NULL, cls);
568               me->tunnel = *cls;
569             }
570             else if (NULL != cls)
571             {
572               *cls = me->tunnel;
573               send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
574             }
575           }
576         }
577         else
578         {
579           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580                       "Packet to %x which has no mapping\n", dadr);
581         }
582         break;
583       case 0x01:
584         /* ICMP */
585         pkt_icmp = (struct ip_icmp *) pkt;
586         if (pkt_icmp->icmp_hdr.type == 0x8 &&
587             (key = address4_mapping_exists (dadr)) != NULL)
588         {
589           GNUNET_free (key);
590           pkt_icmp = GNUNET_malloc (ntohs (pkt->shdr.size));
591           memcpy (pkt_icmp, pkt, ntohs (pkt->shdr.size));
592           GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp);
593         }
594         break;
595       }
596     }
597   }
598 }
599
600 void
601 write_to_helper (void *buf, size_t len)
602 {
603   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
604 }
605
606 void
607 schedule_helper_write (struct GNUNET_TIME_Relative time, void *cls)
608 {
609   if (GNUNET_SCHEDULER_NO_TASK != shs_task)
610     return;
611   GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper,
612                                    &helper_write, cls);
613 }