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