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