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