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