3673b72c6ea463d64724d29b3108956cc5028f50
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.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.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet-vpn-packet.h"
30 #include "gnunet_common.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_applications.h"
33 #include <gnunet_mesh_service.h>
34 #include "gnunet_client_lib.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_constants.h"
37 #include <block_dns.h>
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-daemon-vpn-dns.h"
40 #include "gnunet-daemon-vpn.h"
41 #include "gnunet-vpn-checksum.h"
42
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 struct GNUNET_MESH_Handle *mesh_handle;
45 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
46 static struct GNUNET_CONTAINER_Heap *heap;
47
48 struct tunnel_notify_queue
49 {
50   struct tunnel_notify_queue *next;
51   struct tunnel_notify_queue *prev;
52   size_t len;
53   void *cls;
54 };
55
56 /**
57  * If there are at least this many address-mappings, old ones will be removed
58  */
59 static long long unsigned int max_mappings = 200;
60
61 /**
62  * Final status code.
63  */
64 static int ret;
65
66 /**
67  * This hashmap contains the mapping from peer, service-descriptor,
68  * source-port and destination-port to a socket
69  */
70 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
71
72 GNUNET_SCHEDULER_TaskIdentifier conn_task;
73
74 GNUNET_SCHEDULER_TaskIdentifier shs_task;
75
76 /**
77  * Function scheduled as very last function, cleans up after us
78  *{{{
79  */
80 static void
81 cleanup (void *cls
82          __attribute__ ((unused)),
83          const struct GNUNET_SCHEDULER_TaskContext *tskctx)
84 {
85   GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
86
87   /* stop the helper */
88   cleanup_helper (helper_handle);
89
90   /* close the connection to the service-dns */
91   if (dns_connection != NULL)
92   {
93     GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
94     dns_connection = NULL;
95   }
96
97   if (mesh_handle != NULL)
98   {
99     GNUNET_MESH_disconnect (mesh_handle);
100     mesh_handle = NULL;
101   }
102   if (GNUNET_SCHEDULER_NO_TASK != shs_task)
103   {
104     GNUNET_SCHEDULER_cancel (shs_task);
105     shs_task = GNUNET_SCHEDULER_NO_TASK;
106   }
107   if (GNUNET_SCHEDULER_NO_TASK != conn_task)
108   {
109     GNUNET_SCHEDULER_cancel (conn_task);
110     conn_task = GNUNET_SCHEDULER_NO_TASK;
111   }
112 }
113
114 /*}}}*/
115
116 /**
117  * @return the hash of the IP-Address if a mapping exists, NULL otherwise
118  */
119 GNUNET_HashCode *
120 address6_mapping_exists (unsigned char addr[])
121 {
122   GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
123   unsigned char *k = (unsigned char *) key;
124
125   memset (key, 0, sizeof (GNUNET_HashCode));
126   unsigned int i;
127
128   for (i = 0; i < 16; i++)
129     k[15 - i] = addr[i];
130
131   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
132     return key;
133   else
134   {
135     GNUNET_free (key);
136     return NULL;
137   }
138 }
139
140 /**
141  * @return the hash of the IP-Address if a mapping exists, NULL otherwise
142  */
143 GNUNET_HashCode *
144 address4_mapping_exists (uint32_t addr)
145 {
146   GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
147
148   memset (key, 0, sizeof (GNUNET_HashCode));
149   unsigned char *c = (unsigned char *) &addr;
150   unsigned char *k = (unsigned char *) key;
151   unsigned int i;
152
153   for (i = 0; i < 4; i++)
154     k[3 - i] = c[i];
155
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157               "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
158               *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
159
160   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
161     return key;
162   else
163   {
164     GNUNET_free (key);
165     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
166     return NULL;
167   }
168 }
169
170 static void
171 collect_mappings (void *cls
172                   __attribute__ ((unused)),
173                   const struct GNUNET_SCHEDULER_TaskContext *tc)
174 {
175   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
176     return;
177
178   struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
179
180   /* This is free()ed memory! */
181   me->heap_node = NULL;
182
183   /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
184
185   GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me);
186
187   GNUNET_free (me);
188 }
189
190 void
191 send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 {
193   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
194     return;
195
196   struct ip_icmp *request = cls;
197
198   struct ip_icmp *response = alloca (ntohs (request->shdr.size));
199
200   GNUNET_assert (response != NULL);
201   memset (response, 0, ntohs (request->shdr.size));
202
203   response->shdr.size = request->shdr.size;
204   response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
205
206   response->tun.flags = 0;
207   response->tun.type = htons (0x0800);
208
209   response->ip_hdr.hdr_lngth = 5;
210   response->ip_hdr.version = 4;
211   response->ip_hdr.proto = 0x01;
212   response->ip_hdr.dadr = request->ip_hdr.sadr;
213   response->ip_hdr.sadr = request->ip_hdr.dadr;
214   response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
215
216   response->ip_hdr.chks =
217       calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
218
219   response->icmp_hdr.code = 0;
220   response->icmp_hdr.type = 0x0;
221
222   /* Magic, more Magic! */
223   response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
224
225   /* Copy the rest of the packet */
226   memcpy (response + 1, request + 1,
227           ntohs (request->shdr.size) - sizeof (struct ip_icmp));
228
229   write_to_helper (response, ntohs (response->shdr.size));
230
231   GNUNET_free (request);
232 }
233
234 void
235 send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
236 {
237   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
238     return;
239
240   struct ip6_icmp *request = cls;
241
242   struct ip6_icmp *response = alloca (ntohs (request->shdr.size));
243
244   GNUNET_assert (response != NULL);
245   memset (response, 0, ntohs (request->shdr.size));
246
247   response->shdr.size = request->shdr.size;
248   response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
249
250   response->tun.flags = 0;
251   response->tun.type = htons (0x86dd);
252
253   response->ip6_hdr.hoplmt = 255;
254   response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
255   response->ip6_hdr.nxthdr = 0x3a;
256   response->ip6_hdr.version = 6;
257   memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
258   memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
259
260   response->icmp_hdr.code = 0;
261   response->icmp_hdr.type = 0x81;
262
263   /* Magic, more Magic! */
264   response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
265
266   /* Copy the rest of the packet */
267   memcpy (response + 1, request + 1,
268           ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
269
270   write_to_helper (response, ntohs (response->shdr.size));
271
272   GNUNET_free (request);
273 }
274
275 /**
276  * cls is the pointer to a GNUNET_MessageHeader that is
277  * followed by the service-descriptor and the packet that should be sent;
278  */
279 static size_t
280 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
281 {
282   struct GNUNET_MESH_Tunnel **tunnel = cls;
283
284   GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
285   struct GNUNET_MessageHeader *hdr =
286       (struct GNUNET_MessageHeader *) (tunnel + 1);
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288               "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
289               size);
290   GNUNET_assert (size >= ntohs (hdr->size));
291   memcpy (buf, hdr, ntohs (hdr->size));
292   size = ntohs (hdr->size);
293   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
294
295   if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
296   {
297     struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
298     struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
299     struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
300
301     GNUNET_CONTAINER_DLL_remove (head, tail, element);
302
303     GNUNET_MESH_tunnel_set_head (*tunnel, head);
304     GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
305
306     struct GNUNET_MESH_TransmitHandle *th =
307         GNUNET_MESH_notify_transmit_ready (*tunnel,
308                                            GNUNET_NO,
309                                            42,
310                                            GNUNET_TIME_relative_divide
311                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
312                                            (const struct GNUNET_PeerIdentity *)
313                                            NULL, element->len,
314                                            send_pkt_to_peer_notify_callback,
315                                            element->cls);
316
317     /* save the handle */
318     GNUNET_MESH_tunnel_set_data (*tunnel, th);
319     GNUNET_free (element);
320   }
321   GNUNET_free (cls);
322
323   return size;
324 }
325
326 unsigned int
327 port_in_ports (uint64_t ports, uint16_t port)
328 {
329   uint16_t *ps = (uint16_t *) & ports;
330
331   return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
332 }
333
334 void
335 send_pkt_to_peer (void *cls,
336                   const struct GNUNET_PeerIdentity *peer,
337                   const struct GNUNET_TRANSPORT_ATS_Information *atsi
338                   __attribute__ ((unused)))
339 {
340   /* peer == NULL means that all peers in this request are connected */
341   if (peer == NULL)
342     return;
343   struct GNUNET_MESH_Tunnel **tunnel = cls;
344   struct GNUNET_MessageHeader *hdr =
345       (struct GNUNET_MessageHeader *) (tunnel + 1);
346
347   GNUNET_assert (NULL != tunnel);
348   GNUNET_assert (NULL != *tunnel);
349
350   if (NULL == GNUNET_MESH_tunnel_get_data (*tunnel))
351   {
352     struct GNUNET_MESH_TransmitHandle *th =
353         GNUNET_MESH_notify_transmit_ready (*tunnel,
354                                            GNUNET_NO,
355                                            42,
356                                            GNUNET_TIME_relative_divide
357                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
358                                            (const struct GNUNET_PeerIdentity *)
359                                            NULL,
360                                            ntohs (hdr->size),
361                                            send_pkt_to_peer_notify_callback,
362                                            cls);
363
364     GNUNET_MESH_tunnel_set_data (*tunnel, th);
365   }
366   else
367   {
368     struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
369     struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
370     struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
371
372     element->cls = cls;
373     element->len = ntohs (hdr->size);
374
375     GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
376
377     GNUNET_MESH_tunnel_set_head (*tunnel, head);
378     GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
379   }
380 }
381
382 /**
383  * Create a new Address from an answer-packet
384  */
385 void
386 new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
387              const GNUNET_HashCode * service_desc)
388 {                               /* {{{ */
389   char *ipv6addr;
390   unsigned long long ipv6prefix;
391
392   GNUNET_assert (GNUNET_OK ==
393                  GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
394                                                         &ipv6addr));
395   GNUNET_assert (GNUNET_OK ==
396                  GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
397                                                         "IPV6PREFIX",
398                                                         &ipv6prefix));
399   GNUNET_assert (ipv6prefix < 127);
400   ipv6prefix = (ipv6prefix + 7) / 8;
401
402   inet_pton (AF_INET6, ipv6addr, buf);
403   GNUNET_free (ipv6addr);
404
405   int peer_length = 16 - ipv6prefix - 6;
406
407   if (peer_length <= 0)
408     peer_length = 0;
409
410   int service_length = 16 - ipv6prefix - peer_length;
411
412   if (service_length <= 0)
413     service_length = 0;
414
415   memcpy (buf + ipv6prefix, service_desc, service_length);
416   memcpy (buf + ipv6prefix + service_length, peer, peer_length);
417 }
418
419 /*}}}*/
420
421
422 /**
423  * Create a new Address from an answer-packet
424  */
425 void
426 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
427 {                               /* {{{ */
428   char *ipv6addr;
429   unsigned long long ipv6prefix;
430
431   GNUNET_assert (GNUNET_OK ==
432                  GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
433                                                         "IPV6ADDR", &ipv6addr));
434   GNUNET_assert (GNUNET_OK ==
435                  GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
436                                                         "IPV6PREFIX",
437                                                         &ipv6prefix));
438   GNUNET_assert (ipv6prefix < 127);
439   ipv6prefix = (ipv6prefix + 7) / 8;
440
441   inet_pton (AF_INET6, ipv6addr, buf);
442   GNUNET_free (ipv6addr);
443
444   int local_length = 16 - ipv6prefix;
445
446   memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
447 }
448
449 /*}}}*/
450
451 /**
452  * Create a new Address from an answer-packet
453  */
454 void
455 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
456 {                               /* {{{ */
457   char *ipv4addr;
458   char *ipv4mask;
459
460   GNUNET_assert (GNUNET_OK ==
461                  GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
462                                                         "IPV4ADDR", &ipv4addr));
463   GNUNET_assert (GNUNET_OK ==
464                  GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
465                                                         "IPV4MASK", &ipv4mask));
466   uint32_t mask;
467
468   inet_pton (AF_INET, ipv4addr, buf);
469   int r = inet_pton (AF_INET, ipv4mask, &mask);
470
471   mask = htonl (mask);
472   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
473               mask);
474
475   GNUNET_free (ipv4addr);
476
477   int c;
478
479   if (mask)
480   {
481     mask = (mask ^ (mask - 1)) >> 1;
482     for (c = 0; mask; c++)
483     {
484       mask >>= 1;
485     }
486   }
487   else
488   {
489     c = CHAR_BIT * sizeof (mask);
490   }
491
492   c = 32 - c;
493   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
494               ipv4mask, c);
495
496   GNUNET_free (ipv4mask);
497
498   if (c % 8 == 0)
499     c = c / 8;
500   else
501     GNUNET_assert (0);
502
503   memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
504 }
505
506 /*}}}*/
507
508 /**
509  * This gets scheduled with cls pointing to an answer_packet and does everything
510  * needed in order to send it to the helper.
511  *
512  * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
513  * doing nothing for "real" services.
514  */
515 void
516 process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
517 {
518   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
519     return;
520
521   struct answer_packet *pkt = cls;
522   struct answer_packet_list *list;
523
524   /* This answer is about a .gnunet-service
525    *
526    * It contains an almost complete DNS-Response, we have to fill in the ip
527    * at the offset pkt->addroffset
528    */
529   if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
530   {
531     pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
532
533     GNUNET_HashCode key;
534
535     memset (&key, 0, sizeof (GNUNET_HashCode));
536
537     unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
538     unsigned char *k = (unsigned char *) &key;
539
540     new_ip6addr (c, &pkt->service_descr.peer,
541                  &pkt->service_descr.service_descriptor);
542     /*
543      * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
544      */
545     unsigned int i;
546
547     for (i = 0; i < 16; i++)
548       k[15 - i] = c[i];
549
550     uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
551
552     struct map_entry *value =
553         GNUNET_malloc (sizeof (struct map_entry) + namelen);
554     char *name = (char *) (value + 1);
555
556     value->namelen = namelen;
557     memcpy (name, pkt->data + 12, namelen);
558
559     memcpy (&value->desc, &pkt->service_descr,
560             sizeof (struct GNUNET_vpn_service_descriptor));
561
562     memset (value->additional_ports, 0, 8192);
563
564     memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
565
566     if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
567     {
568       GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
569                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
570
571       value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
572                                                        GNUNET_TIME_absolute_get
573                                                        ().abs_value);
574       if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
575         GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
576     }
577     else
578       GNUNET_free (value);
579
580
581     list =
582         GNUNET_malloc (htons (pkt->hdr.size) +
583                        2 * sizeof (struct answer_packet_list *));
584
585     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
586
587   }
588   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
589   {
590     GNUNET_HashCode key;
591
592     memset (&key, 0, sizeof key);
593     unsigned char *k = (unsigned char *) &key;
594     unsigned char *s = pkt->data + 12;
595     int i = 0;
596
597     /* Whoever designed the reverse IPv6-lookup is batshit insane */
598     for (i = 0; i < 16; i++)
599     {
600       unsigned char c1 = s[(4 * i) + 1];
601       unsigned char c2 = s[(4 * i) + 3];
602
603       if (c1 <= '9')
604         k[i] = c1 - '0';
605       else
606         k[i] = c1 - 87;         /* 87 is the difference between 'a' and 10 */
607       if (c2 <= '9')
608         k[i] += 16 * (c2 - '0');
609       else
610         k[i] += 16 * (c2 - 87);
611     }
612
613     struct map_entry *map_entry =
614         GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
615     uint16_t offset = ntohs (pkt->addroffset);
616
617     if (map_entry == NULL)
618     {
619       GNUNET_free (pkt);
620       return;
621     }
622
623     GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
624                                        GNUNET_TIME_absolute_get ().abs_value);
625
626
627     unsigned short namelen = htons (map_entry->namelen);
628     char *name = (char *) (map_entry + 1);
629
630     list =
631         GNUNET_malloc (2 * sizeof (struct answer_packet_list *) + offset + 2 +
632                        ntohs (namelen));
633
634     struct answer_packet *rpkt = &list->pkt;
635
636     /* The offset points to the first byte belonging to the address */
637     memcpy (rpkt, pkt, offset - 1);
638
639     rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
640     rpkt->hdr.size = ntohs (offset + 2 + ntohs (namelen));
641
642     memcpy (((char *) rpkt) + offset, &namelen, 2);
643     memcpy (((char *) rpkt) + offset + 2, name, ntohs (namelen));
644
645   }
646   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
647   {
648     list =
649         GNUNET_malloc (htons (pkt->hdr.size) +
650                        2 * sizeof (struct answer_packet_list *));
651     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
652   }
653   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
654   {
655     pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
656
657     GNUNET_HashCode key;
658
659     memset (&key, 0, sizeof (GNUNET_HashCode));
660
661     unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
662
663     new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
664     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665                 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
666                 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
667                 c[10], c[11], c[12], c[13], c[14], c[15]);
668     unsigned char *k = (unsigned char *) &key;
669
670     /*
671      * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
672      */
673     unsigned int i;
674
675     for (i = 0; i < 16; i++)
676       k[15 - i] = c[i];
677
678     uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
679
680     struct map_entry *value =
681         GNUNET_malloc (sizeof (struct map_entry) + namelen);
682     char *name = (char *) (value + 1);
683
684     value->namelen = namelen;
685     memcpy (name, pkt->data + 12, namelen);
686
687     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
688                 pkt->addrsize);
689     value->addrlen = pkt->addrsize;
690     memcpy (&value->addr, &pkt->addr, pkt->addrsize);
691     memset (value->additional_ports, 0, 8192);
692
693     memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
694
695     if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
696     {
697       GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
698                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
699       value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
700                                                        GNUNET_TIME_absolute_get
701                                                        ().abs_value);
702       if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
703         GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
704     }
705     else
706       GNUNET_free (value);
707
708     list =
709         GNUNET_malloc (htons (pkt->hdr.size) +
710                        2 * sizeof (struct answer_packet_list *));
711
712     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
713   }
714   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
715   {
716     pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
717
718     GNUNET_HashCode key;
719
720     memset (&key, 0, sizeof (GNUNET_HashCode));
721
722     unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
723
724     new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
725     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n",
726                 c[0], c[1], c[2], c[3]);
727     unsigned char *k = (unsigned char *) &key;
728
729     /*
730      * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
731      */
732     unsigned int i;
733
734     for (i = 0; i < 4; i++)
735       k[3 - i] = c[i];
736
737     uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
738
739     struct map_entry *value =
740         GNUNET_malloc (sizeof (struct map_entry) + namelen);
741     char *name = (char *) (value + 1);
742
743     value->namelen = namelen;
744     memcpy (name, pkt->data + 12, namelen);
745
746     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
747                 pkt->addrsize);
748     value->addrlen = pkt->addrsize;
749     memcpy (&value->addr, &pkt->addr, pkt->addrsize);
750     memset (value->additional_ports, 0, 8192);
751
752     memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
753
754     if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
755     {
756       GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
757                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
758       value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
759                                                        GNUNET_TIME_absolute_get
760                                                        ().abs_value);
761       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762                   "Mapping is saved in the hashmap with key %08x.\n",
763                   *((uint32_t *) (&key)));
764       if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
765         GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
766     }
767     else
768       GNUNET_free (value);
769
770     list =
771         GNUNET_malloc (htons (pkt->hdr.size) +
772                        2 * sizeof (struct answer_packet_list *));
773
774     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
775   }
776   else
777   {
778     GNUNET_break (0);
779     GNUNET_free (pkt);
780     return;
781   }
782
783   GNUNET_free (pkt);
784
785   GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
786                                      answer_proc_tail, list);
787
788   schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
789
790   return;
791 }
792
793 /**
794  * Sets a bit active in a bitArray.
795  *
796  * @param bitArray memory area to set the bit in
797  * @param bitIdx which bit to set
798  */
799 void
800 setBit (char *bitArray, unsigned int bitIdx)
801 {
802   size_t arraySlot;
803   unsigned int targetBit;
804
805   arraySlot = bitIdx / 8;
806   targetBit = (1L << (bitIdx % 8));
807   bitArray[arraySlot] |= targetBit;
808 }
809
810 /**
811  * Clears a bit from bitArray.
812  *
813  * @param bitArray memory area to set the bit in
814  * @param bitIdx which bit to unset
815  */
816 void
817 clearBit (char *bitArray, unsigned int bitIdx)
818 {
819   size_t slot;
820   unsigned int targetBit;
821
822   slot = bitIdx / 8;
823   targetBit = (1L << (bitIdx % 8));
824   bitArray[slot] = bitArray[slot] & (~targetBit);
825 }
826
827 /**
828  * Checks if a bit is active in the bitArray
829  *
830  * @param bitArray memory area to set the bit in
831  * @param bitIdx which bit to test
832  * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
833  */
834 int
835 testBit (char *bitArray, unsigned int bitIdx)
836 {
837   size_t slot;
838   unsigned int targetBit;
839
840   slot = bitIdx / 8;
841   targetBit = (1L << (bitIdx % 8));
842   if (bitArray[slot] & targetBit)
843     return GNUNET_YES;
844   else
845     return GNUNET_NO;
846 }
847
848 /**
849  * @brief Add the port to the list of additional ports in the map_entry
850  *
851  * @param me the map_entry
852  * @param port the port in host-byte-order
853  */
854 static void
855 add_additional_port (struct map_entry *me, uint16_t port)
856 {
857   setBit (me->additional_ports, port);
858 }
859
860 static int
861 receive_udp_back (void *cls
862                   __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
863                   void **tunnel_ctx __attribute__ ((unused)),
864                   const struct GNUNET_PeerIdentity *sender
865                   __attribute__ ((unused)),
866                   const struct GNUNET_MessageHeader *message,
867                   const struct GNUNET_TRANSPORT_ATS_Information *atsi
868                   __attribute__ ((unused)))
869 {
870   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
871   struct remote_addr *s = (struct remote_addr *) desc;
872   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
873   const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
874
875   if (16 == s->addrlen)
876   {
877     size_t size =
878         sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
879         sizeof (struct udp_pkt);
880
881     struct ip6_udp *pkt6 = alloca (size);
882
883     GNUNET_assert (pkt6 != NULL);
884
885     if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
886       new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
887     else
888       new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
889
890     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891                 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
892                 ntohs (message->size), ntohs (pkt->len));
893
894     pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
895     pkt6->shdr.size = htons (size);
896
897     pkt6->tun.flags = 0;
898     pkt6->tun.type = htons (0x86dd);
899
900     pkt6->ip6_hdr.version = 6;
901     pkt6->ip6_hdr.tclass_h = 0;
902     pkt6->ip6_hdr.tclass_l = 0;
903     pkt6->ip6_hdr.flowlbl = 0;
904     pkt6->ip6_hdr.paylgth = pkt->len;
905     pkt6->ip6_hdr.nxthdr = 0x11;
906     pkt6->ip6_hdr.hoplmt = 0xff;
907
908     {
909       char *ipv6addr;
910
911       GNUNET_assert (GNUNET_OK ==
912                      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
913                                                             "IPV6ADDR",
914                                                             &ipv6addr));
915       inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
916       GNUNET_free (ipv6addr);
917     }
918     memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
919
920     GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
921
922     GNUNET_assert (key != NULL);
923
924     struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
925
926     GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
927                                        GNUNET_TIME_absolute_get ().abs_value);
928
929     GNUNET_free (key);
930
931     GNUNET_assert (me != NULL);
932     if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
933     {
934       GNUNET_assert (me->desc.
935                      service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
936       if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) &&
937           !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
938       {
939         add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
940       }
941     }
942
943     pkt6->udp_hdr.crc = 0;
944     uint32_t sum = 0;
945
946     sum =
947         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
948     sum =
949         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
950     uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
951
952     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
953     tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
954     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
955
956     sum =
957         calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
958                                    ntohs (pkt->len));
959     pkt6->udp_hdr.crc = calculate_checksum_end (sum);
960
961     write_to_helper (pkt6, size);
962   }
963   else
964   {
965     size_t size =
966         sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
967
968     struct ip_udp *pkt4 = alloca (size);
969
970     GNUNET_assert (pkt4 != NULL);
971
972     GNUNET_assert (ntohs (message->type) ==
973                    GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
974     uint32_t sadr;
975
976     new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
977     pkt4->ip_hdr.sadr = sadr;
978
979     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980                 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
981                 ntohs (message->size), ntohs (pkt->len));
982
983     pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
984     pkt4->shdr.size = htons (size);
985
986     pkt4->tun.flags = 0;
987     pkt4->tun.type = htons (0x0800);
988
989     pkt4->ip_hdr.version = 4;
990     pkt4->ip_hdr.hdr_lngth = 5;
991     pkt4->ip_hdr.diff_serv = 0;
992     pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len));
993     pkt4->ip_hdr.ident = 0;
994     pkt4->ip_hdr.flags = 0;
995     pkt4->ip_hdr.frag_off = 0;
996     pkt4->ip_hdr.ttl = 255;
997     pkt4->ip_hdr.proto = 0x11;
998     pkt4->ip_hdr.chks = 0;      /* Will be calculated later */
999
1000     {
1001       char *ipv4addr;
1002       uint32_t dadr;
1003
1004       GNUNET_assert (GNUNET_OK ==
1005                      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1006                                                             "IPV4ADDR",
1007                                                             &ipv4addr));
1008       inet_pton (AF_INET, ipv4addr, &dadr);
1009       GNUNET_free (ipv4addr);
1010       pkt4->ip_hdr.dadr = dadr;
1011     }
1012     memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1013
1014     GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1015
1016     GNUNET_assert (key != NULL);
1017
1018     struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1019
1020     GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1021                                        GNUNET_TIME_absolute_get ().abs_value);
1022
1023     GNUNET_free (key);
1024
1025     GNUNET_assert (me != NULL);
1026
1027     pkt4->udp_hdr.crc = 0;      /* Optional for IPv4 */
1028
1029     pkt4->ip_hdr.chks =
1030         calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1031
1032     write_to_helper (pkt4, size);
1033   }
1034
1035   return GNUNET_OK;
1036 }
1037
1038 static int
1039 receive_tcp_back (void *cls
1040                   __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1041                   void **tunnel_ctx
1042                   __attribute__ ((unused)),
1043                   const struct GNUNET_PeerIdentity *sender
1044                   __attribute__ ((unused)),
1045                   const struct GNUNET_MessageHeader *message,
1046                   const struct GNUNET_TRANSPORT_ATS_Information *atsi
1047                   __attribute__ ((unused)))
1048 {
1049   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1050   struct remote_addr *s = (struct remote_addr *) desc;
1051   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1052   const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
1053
1054   size_t pktlen =
1055       ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1056       sizeof (GNUNET_HashCode);
1057
1058   if (s->addrlen == 16)
1059   {
1060     size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1061
1062     struct ip6_tcp *pkt6 = alloca (size);
1063
1064     memset (pkt6, 0, size);
1065
1066     GNUNET_assert (pkt6 != NULL);
1067
1068     if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1069       new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1070     else
1071       new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1072
1073     pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1074     pkt6->shdr.size = htons (size);
1075
1076     pkt6->tun.flags = 0;
1077     pkt6->tun.type = htons (0x86dd);
1078
1079     pkt6->ip6_hdr.version = 6;
1080     pkt6->ip6_hdr.tclass_h = 0;
1081     pkt6->ip6_hdr.tclass_l = 0;
1082     pkt6->ip6_hdr.flowlbl = 0;
1083     pkt6->ip6_hdr.paylgth = htons (pktlen);
1084     pkt6->ip6_hdr.nxthdr = 0x06;
1085     pkt6->ip6_hdr.hoplmt = 0xff;
1086
1087     {
1088       char *ipv6addr;
1089
1090       GNUNET_assert (GNUNET_OK ==
1091                      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1092                                                             "IPV6ADDR",
1093                                                             &ipv6addr));
1094       inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1095       GNUNET_free (ipv6addr);
1096     }
1097     memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1098
1099     GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
1100
1101     GNUNET_assert (key != NULL);
1102
1103     struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1104
1105     GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1106                                        GNUNET_TIME_absolute_get ().abs_value);
1107
1108     GNUNET_free (key);
1109
1110     GNUNET_assert (me != NULL);
1111     if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1112       GNUNET_assert (me->desc.
1113                      service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1114
1115     pkt6->tcp_hdr.crc = 0;
1116     uint32_t sum = 0;
1117     uint32_t tmp;
1118
1119     sum =
1120         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1121     sum =
1122         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1123     tmp = htonl (pktlen);
1124     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1125     tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1126     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1127
1128     sum =
1129         calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1130                                    ntohs (pkt6->ip6_hdr.paylgth));
1131     pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1132
1133     write_to_helper (pkt6, size);
1134   }
1135   else
1136   {
1137     size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1138
1139     struct ip_tcp *pkt4 = alloca (size);
1140
1141     GNUNET_assert (pkt4 != NULL);
1142     memset (pkt4, 0, size);
1143
1144     GNUNET_assert (ntohs (message->type) ==
1145                    GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1146     uint32_t sadr;
1147
1148     new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1149     pkt4->ip_hdr.sadr = sadr;
1150
1151     pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1152     pkt4->shdr.size = htons (size);
1153
1154     pkt4->tun.flags = 0;
1155     pkt4->tun.type = htons (0x0800);
1156
1157     pkt4->ip_hdr.version = 4;
1158     pkt4->ip_hdr.hdr_lngth = 5;
1159     pkt4->ip_hdr.diff_serv = 0;
1160     pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1161     pkt4->ip_hdr.ident = 0;
1162     pkt4->ip_hdr.flags = 0;
1163     pkt4->ip_hdr.frag_off = 0;
1164     pkt4->ip_hdr.ttl = 255;
1165     pkt4->ip_hdr.proto = 0x06;
1166     pkt4->ip_hdr.chks = 0;      /* Will be calculated later */
1167
1168     {
1169       char *ipv4addr;
1170       uint32_t dadr;
1171
1172       GNUNET_assert (GNUNET_OK ==
1173                      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1174                                                             "IPV4ADDR",
1175                                                             &ipv4addr));
1176       inet_pton (AF_INET, ipv4addr, &dadr);
1177       GNUNET_free (ipv4addr);
1178       pkt4->ip_hdr.dadr = dadr;
1179     }
1180
1181     memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1182
1183     GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1184
1185     GNUNET_assert (key != NULL);
1186
1187     struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1188
1189     GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1190                                        GNUNET_TIME_absolute_get ().abs_value);
1191
1192     GNUNET_free (key);
1193
1194     GNUNET_assert (me != NULL);
1195     pkt4->tcp_hdr.crc = 0;
1196     uint32_t sum = 0;
1197     uint32_t tmp;
1198
1199     tmp = pkt4->ip_hdr.sadr;
1200     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1201     tmp = pkt4->ip_hdr.dadr;
1202     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1203
1204     tmp = (0x06 << 16) | (0xffff & pktlen);
1205
1206     tmp = htonl (tmp);
1207
1208     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1209
1210     sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1211     pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1212
1213     pkt4->ip_hdr.chks =
1214         calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1215
1216     write_to_helper (pkt4, size);
1217   }
1218
1219   return GNUNET_OK;
1220 }
1221
1222 /**
1223  * Main function that will be run by the scheduler.
1224  *
1225  * @param cls closure
1226  * @param args remaining command-line arguments
1227  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1228  * @param cfg_ configuration
1229  */
1230 static void
1231 run (void *cls,
1232      char *const *args __attribute__ ((unused)),
1233      const char *cfgfilep __attribute__ ((unused)),
1234      const struct GNUNET_CONFIGURATION_Handle *cfg_)
1235 {
1236   static const struct GNUNET_MESH_MessageHandler handlers[] = {
1237     {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1238     {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1239     {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1240     {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1241     {NULL, 0, 0}
1242   };
1243
1244   static const GNUNET_MESH_ApplicationType types[] = {
1245     GNUNET_APPLICATION_TYPE_END
1246   };
1247
1248   mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, types);
1249   cfg = cfg_;
1250   restart_hijack = 0;
1251   hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1252   heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1253   GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1254                                          &max_mappings);
1255   udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1256   conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1257   shs_task =
1258       GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1259   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1260 }
1261
1262 /**
1263  * The main function to obtain template from gnunetd.
1264  *
1265  * @param argc number of arguments from the command line
1266  * @param argv command line arguments
1267  * @return 0 ok, 1 on error
1268  */
1269 int
1270 main (int argc, char *const *argv)
1271 {
1272   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1273     GNUNET_GETOPT_OPTION_END
1274   };
1275
1276   return (GNUNET_OK ==
1277           GNUNET_PROGRAM_run (argc,
1278                               argv,
1279                               "vpn",
1280                               gettext_noop ("help text"),
1281                               options, &run, NULL)) ? ret : 1;
1282 }
1283
1284 /* end of gnunet-daemon-vpn.c */