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