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