fix
[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_mesh_service.h>
33 #include "gnunet_client_lib.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet_constants.h"
36 #include <block_dns.h>
37 #include "gnunet-daemon-vpn-helper.h"
38 #include "gnunet-daemon-vpn-dns.h"
39 #include "gnunet-daemon-vpn.h"
40 #include "gnunet-vpn-checksum.h"
41
42 const struct GNUNET_CONFIGURATION_Handle *cfg;
43 struct GNUNET_MESH_Handle *mesh_handle;
44 struct GNUNET_CONTAINER_MultiHashMap* hashmap;
45 static struct GNUNET_CONTAINER_Heap *heap;
46
47 /**
48  * If there are at least this many address-mappings, old ones will be removed
49  */
50 static long long unsigned int max_mappings = 200;
51
52 /**
53  * Final status code.
54  */
55 static int ret;
56
57 /**
58  * This hashmap contains the mapping from peer, service-descriptor,
59  * source-port and destination-port to a socket
60  */
61 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
62
63 /**
64  * Function scheduled as very last function, cleans up after us
65  *{{{
66  */
67 static void
68 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
69     GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
70
71     /* stop the helper */
72     cleanup_helper(helper_handle);
73
74     /* close the connection to the service-dns */
75     if (dns_connection != NULL)
76       {
77         GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
78         dns_connection = NULL;
79       }
80
81     if (mesh_handle != NULL)
82       {
83         GNUNET_MESH_disconnect(mesh_handle);
84         mesh_handle = NULL;
85       }
86 }
87 /*}}}*/
88
89 /**
90  * @return the hash of the IP-Address if a mapping exists, NULL otherwise
91  */
92 GNUNET_HashCode*
93 address_mapping_exists(unsigned char addr[]) {
94     GNUNET_HashCode* key = GNUNET_malloc(sizeof(GNUNET_HashCode));
95     unsigned char* k = (unsigned char*)key;
96     memset(key, 0, sizeof(GNUNET_HashCode));
97     unsigned int i;
98     for (i = 0; i < 16; i++)
99         k[15-i] = addr[i];
100
101     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(hashmap, key))
102       return key;
103     else
104       {
105         GNUNET_free(key);
106         return NULL;
107       }
108 }
109
110 static void
111 collect_mappings(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
112     if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
113       return;
114
115     struct map_entry* me = GNUNET_CONTAINER_heap_remove_root(heap);
116
117     /* This is free()ed memory! */
118     me->heap_node = NULL;
119
120     /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
121
122     GNUNET_CONTAINER_multihashmap_remove(hashmap, &me->hash, me);
123
124     GNUNET_free(me);
125 }
126
127 void
128 send_icmp_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
129     struct ip6_icmp* request = cls;
130
131     struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
132     GNUNET_assert(response != NULL);
133     memset(response, 0, ntohs(request->shdr.size));
134
135     response->shdr.size = request->shdr.size;
136     response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
137
138     response->tun.flags = 0;
139     response->tun.type = htons(0x86dd);
140
141     response->ip6_hdr.hoplmt = 255;
142     response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
143     response->ip6_hdr.nxthdr = 0x3a;
144     response->ip6_hdr.version = 6;
145     memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
146     memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
147
148     response->icmp_hdr.code = 0;
149     response->icmp_hdr.type = 0x81;
150
151     /* Magic, more Magic! */
152     response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
153
154     /* Copy the rest of the packet */
155     memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
156
157     write_to_helper(response, ntohs(response->shdr.size));
158
159     GNUNET_free(request);
160 }
161
162 /**
163  * cls is the pointer to a GNUNET_MessageHeader that is
164  * followed by the service-descriptor and the packet that should be sent;
165  */
166 static size_t
167 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
168 {
169   struct GNUNET_MESH_Tunnel **tunnel = cls;
170   struct GNUNET_MessageHeader *hdr =
171     (struct GNUNET_MessageHeader *) (tunnel + 1);
172   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
173   GNUNET_assert (size >= ntohs (hdr->size));
174   memcpy (buf, hdr, ntohs (hdr->size));
175   size = ntohs(hdr->size);
176   GNUNET_free (cls);
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
178   return size;
179 }
180
181 unsigned int
182 port_in_ports (uint64_t ports, uint16_t port)
183 {
184   uint16_t *ps = (uint16_t *) & ports;
185   return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
186 }
187
188 void
189 send_pkt_to_peer (void *cls, 
190                   const struct GNUNET_PeerIdentity *peer,
191                   const struct GNUNET_TRANSPORT_ATS_Information *atsi)
192 {
193   if (peer == NULL) return;
194   struct GNUNET_MESH_Tunnel **tunnel = cls;
195   struct GNUNET_MessageHeader *hdr =
196     (struct GNUNET_MessageHeader *) (tunnel + 1);
197
198   GNUNET_assert(NULL != tunnel);
199   GNUNET_assert(NULL != *tunnel);
200
201   GNUNET_MESH_notify_transmit_ready (*tunnel,
202                                      GNUNET_NO,
203                                      42,
204                                      GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
205                                      (const struct GNUNET_PeerIdentity *)NULL,
206                                      ntohs(hdr->size),
207                                      send_pkt_to_peer_notify_callback,
208                                      cls);
209 }
210
211 /**
212  * Create a new Address from an answer-packet
213  */
214 void
215 new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
216     char* ipv6addr;
217     unsigned long long ipv6prefix;
218     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
219     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
220     GNUNET_assert(ipv6prefix < 127);
221     ipv6prefix = (ipv6prefix + 7)/8;
222
223     inet_pton (AF_INET6, ipv6addr, buf);
224     GNUNET_free(ipv6addr);
225
226     int peer_length = 16 - ipv6prefix - 6;
227     if (peer_length <= 0)
228       peer_length = 0;
229
230     int service_length = 16 - ipv6prefix - peer_length;
231     if (service_length <= 0)
232       service_length = 0;
233
234     memcpy(buf+ipv6prefix, service_desc, service_length);
235     memcpy(buf+ipv6prefix+service_length, peer, peer_length);
236 }
237 /*}}}*/
238
239
240 /**
241  * Create a new Address from an answer-packet
242  */
243 void
244 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
245 {                               /* {{{ */
246   char *ipv6addr;
247   unsigned long long ipv6prefix;
248   GNUNET_assert (GNUNET_OK ==
249                  GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
250                                                         "IPV6ADDR",
251                                                         &ipv6addr));
252   GNUNET_assert (GNUNET_OK ==
253                  GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
254                                                         "IPV6PREFIX",
255                                                         &ipv6prefix));
256   GNUNET_assert (ipv6prefix < 127);
257   ipv6prefix = (ipv6prefix + 7) / 8;
258
259   inet_pton (AF_INET6, ipv6addr, buf);
260   GNUNET_free (ipv6addr);
261
262   int local_length = 16 - ipv6prefix;
263
264   memcpy (buf + ipv6prefix, addr, GNUNET_MAX (addrlen, local_length));
265 }
266 /*}}}*/
267
268 /**
269  * This gets scheduled with cls pointing to an answer_packet and does everything
270  * needed in order to send it to the helper.
271  *
272  * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
273  * doing nothing for "real" services.
274  */
275 void
276 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
277     struct answer_packet* pkt = cls;
278     struct answer_packet_list* list;
279
280     /* This answer is about a .gnunet-service
281      *
282      * It contains an almost complete DNS-Response, we have to fill in the ip
283      * at the offset pkt->addroffset
284      */
285     if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
286       {
287         pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
288
289         GNUNET_HashCode key;
290         memset(&key, 0, sizeof(GNUNET_HashCode));
291
292         unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
293         unsigned char* k = (unsigned char*)&key;
294         new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
295         /*
296          * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
297          */
298         unsigned int i;
299         for (i = 0; i < 16; i++)
300             k[15-i] = c[i];
301
302         uint16_t namelen = strlen((char*)pkt->data+12)+1;
303
304         struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
305         char* name = (char*)(value +1);
306
307         value->namelen = namelen;
308         memcpy(name, pkt->data+12, namelen);
309
310         memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
311
312         memset(value->additional_ports, 0, 8192);
313
314         memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
315
316         if (GNUNET_NO ==
317             GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
318           {
319             GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
320                                                GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
321
322             value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
323                                                              GNUNET_TIME_absolute_get ().abs_value);
324             if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
325               GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
326           }
327         else
328           GNUNET_free(value);
329
330
331         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
332
333         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
334
335       }
336     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
337       {
338         GNUNET_HashCode key;
339         memset(&key, 0, sizeof key);
340         unsigned char* k = (unsigned char*)&key;
341         unsigned char* s = pkt->data+12;
342         int i = 0;
343         /* Whoever designed the reverse IPv6-lookup is batshit insane */
344         for (i = 0; i < 16; i++)
345           {
346             unsigned char c1 = s[(4*i)+1];
347             unsigned char c2 = s[(4*i)+3];
348             if (c1 <= '9')
349               k[i] = c1 - '0';
350             else
351               k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
352             if (c2 <= '9')
353               k[i] += 16*(c2 - '0');
354             else
355               k[i] += 16*(c2 - 87);
356           }
357
358         struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
359         GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
360                                            GNUNET_TIME_absolute_get ().abs_value);
361
362         uint16_t offset = ntohs(pkt->addroffset);
363
364         if (map_entry == NULL)
365           {
366             GNUNET_free(pkt);
367             return;
368           }
369
370         unsigned short namelen = htons(map_entry->namelen);
371         char* name = (char*)(map_entry + 1);
372
373         list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
374
375         struct answer_packet* rpkt = &list->pkt;
376
377         /* The offset points to the first byte belonging to the address */
378         memcpy(rpkt, pkt, offset - 1);
379
380         rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
381         rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
382
383         memcpy(((char*)rpkt)+offset, &namelen, 2);
384         memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
385
386       }
387     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
388       {
389         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
390         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
391       }
392     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE)
393       {
394         pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
395
396         GNUNET_HashCode key;
397         memset(&key, 0, sizeof(GNUNET_HashCode));
398
399         unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
400         new_ip6addr_remote(c, pkt->addr, pkt->addrsize);
401         unsigned char* k = (unsigned char*)&key;
402         /*
403          * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
404          */
405         unsigned int i;
406         for (i = 0; i < 16; i++)
407             k[15-i] = c[i];
408
409         uint16_t namelen = strlen((char*)pkt->data+12)+1;
410
411         struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
412         char* name = (char*)(value +1);
413
414         value->namelen = namelen;
415         memcpy(name, pkt->data+12, namelen);
416
417         value->addrlen = pkt->addrsize;
418         memcpy(&value->addr, &pkt->addr, pkt->addrsize);
419         memset(value->additional_ports, 0, 8192);
420
421         memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
422
423         if (GNUNET_NO ==
424             GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
425           {
426             GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
427                                                GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
428             value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
429                                                              GNUNET_TIME_absolute_get ().abs_value);
430             if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
431               GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
432           }
433         else
434           GNUNET_free(value);
435
436         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
437
438         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
439       }
440     else
441       {
442         GNUNET_break(0);
443         GNUNET_free(pkt);
444         return;
445       }
446
447     GNUNET_free(pkt);
448
449     GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
450
451     schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
452
453     return;
454 }
455
456 /**
457  * Sets a bit active in a bitArray.
458  *
459  * @param bitArray memory area to set the bit in
460  * @param bitIdx which bit to set
461  */
462 void
463 setBit (char *bitArray, unsigned int bitIdx)
464 {
465   size_t arraySlot;
466   unsigned int targetBit;
467
468   arraySlot = bitIdx / 8;
469   targetBit = (1L << (bitIdx % 8));
470   bitArray[arraySlot] |= targetBit;
471 }
472
473 /**
474  * Clears a bit from bitArray.
475  *
476  * @param bitArray memory area to set the bit in
477  * @param bitIdx which bit to unset
478  */
479 void
480 clearBit (char *bitArray, unsigned int bitIdx)
481 {
482   size_t slot;
483   unsigned int targetBit;
484
485   slot = bitIdx / 8;
486   targetBit = (1L << (bitIdx % 8));
487   bitArray[slot] = bitArray[slot] & (~targetBit);
488 }
489
490 /**
491  * Checks if a bit is active in the bitArray
492  *
493  * @param bitArray memory area to set the bit in
494  * @param bitIdx which bit to test
495  * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
496  */
497 int
498 testBit (char *bitArray, unsigned int bitIdx)
499 {
500   size_t slot;
501   unsigned int targetBit;
502
503   slot = bitIdx / 8;
504   targetBit = (1L << (bitIdx % 8));
505   if (bitArray[slot] & targetBit)
506     return GNUNET_YES;
507   else
508     return GNUNET_NO;
509 }
510
511 /**
512  * @brief Add the port to the list of additional ports in the map_entry
513  *
514  * @param me the map_entry
515  * @param port the port in host-byte-order
516  */
517 static void
518 add_additional_port (struct map_entry *me, uint16_t port)
519 {
520   setBit(me->additional_ports, port);
521 }
522
523 static int
524 receive_udp_back (void *cls, struct GNUNET_MESH_Tunnel* tunnel,
525                   void **tunnel_ctx,
526                   const struct GNUNET_PeerIdentity *sender,
527                   const struct GNUNET_MessageHeader *message,
528                   const struct GNUNET_TRANSPORT_ATS_Information *atsi)
529 {
530   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
531   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
532   const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
533
534   size_t size = sizeof(struct ip6_udp) + ntohs(pkt->len) - 1 - sizeof(struct udp_pkt);
535
536   struct ip6_udp* pkt6 = alloca(size);
537
538   GNUNET_assert(pkt6 != NULL);
539
540   new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
541
542   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, ntohs(message->size), ntohs(pkt->len));
543
544   pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
545   pkt6->shdr.size = htons(size);
546
547   pkt6->tun.flags = 0;
548   pkt6->tun.type = htons(0x86dd);
549
550   pkt6->ip6_hdr.version = 6;
551   pkt6->ip6_hdr.tclass_h = 0;
552   pkt6->ip6_hdr.tclass_l = 0;
553   pkt6->ip6_hdr.flowlbl = 0;
554   pkt6->ip6_hdr.paylgth = pkt->len;
555   pkt6->ip6_hdr.nxthdr = 0x11;
556   pkt6->ip6_hdr.hoplmt = 0xff;
557
558   {
559     char* ipv6addr;
560     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
561     inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
562     GNUNET_free(ipv6addr);
563   }
564   memcpy(&pkt6->udp_hdr, pkt, ntohs(pkt->len));
565
566   GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
567   GNUNET_assert (key != NULL);
568
569   struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
570   GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
571                                      GNUNET_TIME_absolute_get ().abs_value);
572
573   GNUNET_free(key);
574
575   GNUNET_assert (me != NULL);
576   GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP));
577   if (!port_in_ports(me->desc.ports, pkt6->udp_hdr.spt) &&
578       !testBit(me->additional_ports, ntohs(pkt6->udp_hdr.spt))) {
579       add_additional_port(me, ntohs(pkt6->udp_hdr.spt));
580   }
581
582   pkt6->udp_hdr.crc = 0;
583   uint32_t sum = 0;
584   sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.sadr, 16);
585   sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.dadr, 16);
586   uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
587   sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
588   tmp = htons(((pkt6->ip6_hdr.nxthdr & 0x00ff)));
589   sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
590
591   sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->udp_hdr, ntohs(pkt->len));
592   pkt6->udp_hdr.crc = calculate_checksum_end(sum);
593
594   write_to_helper(pkt6, size);
595
596   return GNUNET_OK;
597 }
598
599 static int
600 receive_tcp_back (void *cls, struct GNUNET_MESH_Tunnel* tunnel,
601                   void **tunnel_ctx,
602                   const struct GNUNET_PeerIdentity *sender,
603                   const struct GNUNET_MessageHeader *message,
604                   const struct GNUNET_TRANSPORT_ATS_Information *atsi)
605 {
606   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
607   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
608   const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
609
610   size_t pktlen = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader) - sizeof(GNUNET_HashCode);
611   size_t size = pktlen + sizeof(struct ip6_tcp) - 1;
612
613   struct ip6_tcp* pkt6 = alloca(size);
614
615   GNUNET_assert(pkt6 != NULL);
616
617   new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
618
619   pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
620   pkt6->shdr.size = htons(size);
621
622   pkt6->tun.flags = 0;
623   pkt6->tun.type = htons(0x86dd);
624
625   pkt6->ip6_hdr.version = 6;
626   pkt6->ip6_hdr.tclass_h = 0;
627   pkt6->ip6_hdr.tclass_l = 0;
628   pkt6->ip6_hdr.flowlbl = 0;
629   pkt6->ip6_hdr.paylgth = htons(pktlen);
630   pkt6->ip6_hdr.nxthdr = 0x06;
631   pkt6->ip6_hdr.hoplmt = 0xff;
632
633   {
634     char* ipv6addr;
635     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
636     inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
637     GNUNET_free(ipv6addr);
638   }
639   memcpy(&pkt6->tcp_hdr, pkt, pktlen);
640
641   GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
642   GNUNET_assert (key != NULL);
643
644   struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
645   GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
646                                      GNUNET_TIME_absolute_get ().abs_value);
647
648   GNUNET_free(key);
649
650   GNUNET_assert (me != NULL);
651   GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_TCP));
652
653   pkt6->tcp_hdr.crc = 0;
654   uint32_t sum = 0;
655   uint32_t tmp;
656   sum =
657     calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
658   sum =
659     calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
660   tmp = htonl(pktlen);
661   sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
662   tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
663   sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
664
665   sum =
666     calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
667                                ntohs (pkt6->ip6_hdr.paylgth));
668   pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
669
670   write_to_helper(pkt6, size);
671
672   return GNUNET_OK;
673 }
674
675 /**
676  * Main function that will be run by the scheduler.
677  *
678  * @param cls closure
679  * @param args remaining command-line arguments
680  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
681  * @param cfg_ configuration
682  */
683 static void
684 run (void *cls,
685      char *const *args,
686      const char *cfgfile,
687      const struct GNUNET_CONFIGURATION_Handle *cfg_)
688 {
689     const static struct GNUNET_MESH_MessageHandler handlers[] = {
690           {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
691           {receive_tcp_back, GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK, 0},
692           {NULL, 0, 0}
693     };
694     mesh_handle = GNUNET_MESH_connect(cfg_,
695                                       NULL,
696                                       NULL,
697                                       handlers,
698                                       NULL);
699     cfg = cfg_;
700     restart_hijack = 0;
701     hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
702     heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
703     GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
704                                            &max_mappings);
705     udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
706     GNUNET_SCHEDULER_TaskIdentifier conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
707     GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
708     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
709 }
710
711 /**
712  * The main function to obtain template from gnunetd.
713  *
714  * @param argc number of arguments from the command line
715  * @param argv command line arguments
716  * @return 0 ok, 1 on error
717  */
718 int
719 main (int argc, char *const *argv) {
720     static const struct GNUNET_GETOPT_CommandLineOption options[] = {
721         GNUNET_GETOPT_OPTION_END
722     };
723
724     return (GNUNET_OK ==
725             GNUNET_PROGRAM_run (argc,
726                                 argv,
727                                 "gnunet-daemon-vpn",
728                                 gettext_noop ("help text"),
729                                 options, &run, NULL)) ? ret : 1;
730 }
731
732 /* end of gnunet-daemon-vpn.c */
733