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