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