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