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