fix bugs found by compiling with -Wextra
[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   /* peer == NULL means that all peers in this request are connected */
194   if (peer == NULL) return;
195   struct GNUNET_MESH_Tunnel **tunnel = cls;
196   struct GNUNET_MessageHeader *hdr =
197     (struct GNUNET_MessageHeader *) (tunnel + 1);
198
199   GNUNET_assert(NULL != tunnel);
200   GNUNET_assert(NULL != *tunnel);
201
202   GNUNET_MESH_notify_transmit_ready (*tunnel,
203                                      GNUNET_NO,
204                                      42,
205                                      GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
206                                      (const struct GNUNET_PeerIdentity *)NULL,
207                                      ntohs(hdr->size),
208                                      send_pkt_to_peer_notify_callback,
209                                      cls);
210 }
211
212 /**
213  * Create a new Address from an answer-packet
214  */
215 void
216 new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
217     char* ipv6addr;
218     unsigned long long ipv6prefix;
219     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
220     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
221     GNUNET_assert(ipv6prefix < 127);
222     ipv6prefix = (ipv6prefix + 7)/8;
223
224     inet_pton (AF_INET6, ipv6addr, buf);
225     GNUNET_free(ipv6addr);
226
227     int peer_length = 16 - ipv6prefix - 6;
228     if (peer_length <= 0)
229       peer_length = 0;
230
231     int service_length = 16 - ipv6prefix - peer_length;
232     if (service_length <= 0)
233       service_length = 0;
234
235     memcpy(buf+ipv6prefix, service_desc, service_length);
236     memcpy(buf+ipv6prefix+service_length, peer, peer_length);
237 }
238 /*}}}*/
239
240
241 /**
242  * Create a new Address from an answer-packet
243  */
244 void
245 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
246 {                               /* {{{ */
247   char *ipv6addr;
248   unsigned long long ipv6prefix;
249   GNUNET_assert (GNUNET_OK ==
250                  GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
251                                                         "IPV6ADDR",
252                                                         &ipv6addr));
253   GNUNET_assert (GNUNET_OK ==
254                  GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
255                                                         "IPV6PREFIX",
256                                                         &ipv6prefix));
257   GNUNET_assert (ipv6prefix < 127);
258   ipv6prefix = (ipv6prefix + 7) / 8;
259
260   inet_pton (AF_INET6, ipv6addr, buf);
261   GNUNET_free (ipv6addr);
262
263   int local_length = 16 - ipv6prefix;
264
265   memcpy (buf + ipv6prefix, addr, GNUNET_MAX (addrlen, local_length));
266 }
267 /*}}}*/
268
269 /**
270  * This gets scheduled with cls pointing to an answer_packet and does everything
271  * needed in order to send it to the helper.
272  *
273  * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
274  * doing nothing for "real" services.
275  */
276 void
277 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
278     if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
279       return;
280
281     struct answer_packet* pkt = cls;
282     struct answer_packet_list* list;
283
284     /* This answer is about a .gnunet-service
285      *
286      * It contains an almost complete DNS-Response, we have to fill in the ip
287      * at the offset pkt->addroffset
288      */
289     if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
290       {
291         pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
292
293         GNUNET_HashCode key;
294         memset(&key, 0, sizeof(GNUNET_HashCode));
295
296         unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
297         unsigned char* k = (unsigned char*)&key;
298         new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
299         /*
300          * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
301          */
302         unsigned int i;
303         for (i = 0; i < 16; i++)
304             k[15-i] = c[i];
305
306         uint16_t namelen = strlen((char*)pkt->data+12)+1;
307
308         struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
309         char* name = (char*)(value +1);
310
311         value->namelen = namelen;
312         memcpy(name, pkt->data+12, namelen);
313
314         memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
315
316         memset(value->additional_ports, 0, 8192);
317
318         memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
319
320         if (GNUNET_NO ==
321             GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
322           {
323             GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
324                                                GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
325
326             value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
327                                                              GNUNET_TIME_absolute_get ().abs_value);
328             if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
329               GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
330           }
331         else
332           GNUNET_free(value);
333
334
335         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
336
337         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
338
339       }
340     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
341       {
342         GNUNET_HashCode key;
343         memset(&key, 0, sizeof key);
344         unsigned char* k = (unsigned char*)&key;
345         unsigned char* s = pkt->data+12;
346         int i = 0;
347         /* Whoever designed the reverse IPv6-lookup is batshit insane */
348         for (i = 0; i < 16; i++)
349           {
350             unsigned char c1 = s[(4*i)+1];
351             unsigned char c2 = s[(4*i)+3];
352             if (c1 <= '9')
353               k[i] = c1 - '0';
354             else
355               k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
356             if (c2 <= '9')
357               k[i] += 16*(c2 - '0');
358             else
359               k[i] += 16*(c2 - 87);
360           }
361
362         struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
363         uint16_t offset = ntohs(pkt->addroffset);
364
365         if (map_entry == NULL)
366           {
367             GNUNET_free(pkt);
368             return;
369           }
370
371         GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
372                                            GNUNET_TIME_absolute_get ().abs_value);
373
374
375         unsigned short namelen = htons(map_entry->namelen);
376         char* name = (char*)(map_entry + 1);
377
378         list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
379
380         struct answer_packet* rpkt = &list->pkt;
381
382         /* The offset points to the first byte belonging to the address */
383         memcpy(rpkt, pkt, offset - 1);
384
385         rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
386         rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
387
388         memcpy(((char*)rpkt)+offset, &namelen, 2);
389         memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
390
391       }
392     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
393       {
394         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
395         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
396       }
397     else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE)
398       {
399         pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
400
401         GNUNET_HashCode key;
402         memset(&key, 0, sizeof(GNUNET_HashCode));
403
404         unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
405         new_ip6addr_remote(c, pkt->addr, pkt->addrsize);
406         unsigned char* k = (unsigned char*)&key;
407         /*
408          * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
409          */
410         unsigned int i;
411         for (i = 0; i < 16; i++)
412             k[15-i] = c[i];
413
414         uint16_t namelen = strlen((char*)pkt->data+12)+1;
415
416         struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
417         char* name = (char*)(value +1);
418
419         value->namelen = namelen;
420         memcpy(name, pkt->data+12, namelen);
421
422         value->addrlen = pkt->addrsize;
423         memcpy(&value->addr, &pkt->addr, pkt->addrsize);
424         memset(value->additional_ports, 0, 8192);
425
426         memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
427
428         if (GNUNET_NO ==
429             GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
430           {
431             GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
432                                                GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
433             value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
434                                                              GNUNET_TIME_absolute_get ().abs_value);
435             if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
436               GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
437           }
438         else
439           GNUNET_free(value);
440
441         list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
442
443         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
444       }
445     else
446       {
447         GNUNET_break(0);
448         GNUNET_free(pkt);
449         return;
450       }
451
452     GNUNET_free(pkt);
453
454     GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
455
456     schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
457
458     return;
459 }
460
461 /**
462  * Sets a bit active in a bitArray.
463  *
464  * @param bitArray memory area to set the bit in
465  * @param bitIdx which bit to set
466  */
467 void
468 setBit (char *bitArray, unsigned int bitIdx)
469 {
470   size_t arraySlot;
471   unsigned int targetBit;
472
473   arraySlot = bitIdx / 8;
474   targetBit = (1L << (bitIdx % 8));
475   bitArray[arraySlot] |= targetBit;
476 }
477
478 /**
479  * Clears a bit from bitArray.
480  *
481  * @param bitArray memory area to set the bit in
482  * @param bitIdx which bit to unset
483  */
484 void
485 clearBit (char *bitArray, unsigned int bitIdx)
486 {
487   size_t slot;
488   unsigned int targetBit;
489
490   slot = bitIdx / 8;
491   targetBit = (1L << (bitIdx % 8));
492   bitArray[slot] = bitArray[slot] & (~targetBit);
493 }
494
495 /**
496  * Checks if a bit is active in the bitArray
497  *
498  * @param bitArray memory area to set the bit in
499  * @param bitIdx which bit to test
500  * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
501  */
502 int
503 testBit (char *bitArray, unsigned int bitIdx)
504 {
505   size_t slot;
506   unsigned int targetBit;
507
508   slot = bitIdx / 8;
509   targetBit = (1L << (bitIdx % 8));
510   if (bitArray[slot] & targetBit)
511     return GNUNET_YES;
512   else
513     return GNUNET_NO;
514 }
515
516 /**
517  * @brief Add the port to the list of additional ports in the map_entry
518  *
519  * @param me the map_entry
520  * @param port the port in host-byte-order
521  */
522 static void
523 add_additional_port (struct map_entry *me, uint16_t port)
524 {
525   setBit(me->additional_ports, port);
526 }
527
528 static int
529 receive_udp_back (void *cls, struct GNUNET_MESH_Tunnel* tunnel,
530                   void **tunnel_ctx,
531                   const struct GNUNET_PeerIdentity *sender,
532                   const struct GNUNET_MessageHeader *message,
533                   const struct GNUNET_TRANSPORT_ATS_Information *atsi)
534 {
535   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
536   struct remote_addr* s = (struct remote_addr*)desc;
537   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
538   const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
539
540   size_t size = sizeof(struct ip6_udp) + ntohs(pkt->len) - 1 - sizeof(struct udp_pkt);
541
542   struct ip6_udp* pkt6 = alloca(size);
543
544   GNUNET_assert(pkt6 != NULL);
545
546   if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
547     new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
548   else
549     new_ip6addr_remote(pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
550
551   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, ntohs(message->size), ntohs(pkt->len));
552
553   pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
554   pkt6->shdr.size = htons(size);
555
556   pkt6->tun.flags = 0;
557   pkt6->tun.type = htons(0x86dd);
558
559   pkt6->ip6_hdr.version = 6;
560   pkt6->ip6_hdr.tclass_h = 0;
561   pkt6->ip6_hdr.tclass_l = 0;
562   pkt6->ip6_hdr.flowlbl = 0;
563   pkt6->ip6_hdr.paylgth = pkt->len;
564   pkt6->ip6_hdr.nxthdr = 0x11;
565   pkt6->ip6_hdr.hoplmt = 0xff;
566
567   {
568     char* ipv6addr;
569     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
570     inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
571     GNUNET_free(ipv6addr);
572   }
573   memcpy(&pkt6->udp_hdr, pkt, ntohs(pkt->len));
574
575   GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
576   GNUNET_assert (key != NULL);
577
578   struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
579   GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
580                                      GNUNET_TIME_absolute_get ().abs_value);
581
582   GNUNET_free(key);
583
584   GNUNET_assert (me != NULL);
585   if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
586     {
587       GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP));
588       if (!port_in_ports(me->desc.ports, pkt6->udp_hdr.spt) &&
589           !testBit(me->additional_ports, ntohs(pkt6->udp_hdr.spt))) {
590           add_additional_port(me, ntohs(pkt6->udp_hdr.spt));
591       }
592     }
593
594   pkt6->udp_hdr.crc = 0;
595   uint32_t sum = 0;
596   sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.sadr, 16);
597   sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.dadr, 16);
598   uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
599   sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
600   tmp = htons(((pkt6->ip6_hdr.nxthdr & 0x00ff)));
601   sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
602
603   sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->udp_hdr, ntohs(pkt->len));
604   pkt6->udp_hdr.crc = calculate_checksum_end(sum);
605
606   write_to_helper(pkt6, size);
607
608   return GNUNET_OK;
609 }
610
611 static int
612 receive_tcp_back (void *cls, struct GNUNET_MESH_Tunnel* tunnel,
613                   void **tunnel_ctx,
614                   const struct GNUNET_PeerIdentity *sender,
615                   const struct GNUNET_MessageHeader *message,
616                   const struct GNUNET_TRANSPORT_ATS_Information *atsi)
617 {
618   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
619   struct remote_addr* s = (struct remote_addr*)desc;
620   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
621   const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
622
623   size_t pktlen = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader) - sizeof(GNUNET_HashCode);
624   size_t size = pktlen + sizeof(struct ip6_tcp) - 1;
625
626   struct ip6_tcp* pkt6 = alloca(size);
627   memset(pkt6, 0, size);
628
629   GNUNET_assert(pkt6 != NULL);
630
631   if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK)
632     new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
633   else
634     new_ip6addr_remote(pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
635
636   pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
637   pkt6->shdr.size = htons(size);
638
639   pkt6->tun.flags = 0;
640   pkt6->tun.type = htons(0x86dd);
641
642   pkt6->ip6_hdr.version = 6;
643   pkt6->ip6_hdr.tclass_h = 0;
644   pkt6->ip6_hdr.tclass_l = 0;
645   pkt6->ip6_hdr.flowlbl = 0;
646   pkt6->ip6_hdr.paylgth = htons(pktlen);
647   pkt6->ip6_hdr.nxthdr = 0x06;
648   pkt6->ip6_hdr.hoplmt = 0xff;
649
650   {
651     char* ipv6addr;
652     GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
653     inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
654     GNUNET_free(ipv6addr);
655   }
656   memcpy(&pkt6->tcp_hdr, pkt, pktlen);
657
658   GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
659   GNUNET_assert (key != NULL);
660
661   struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
662   GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
663                                      GNUNET_TIME_absolute_get ().abs_value);
664
665   GNUNET_free(key);
666
667   GNUNET_assert (me != NULL);
668   if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
669     GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_TCP));
670
671   pkt6->tcp_hdr.crc = 0;
672   uint32_t sum = 0;
673   uint32_t tmp;
674   sum =
675     calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
676   sum =
677     calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
678   tmp = htonl(pktlen);
679   sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
680   tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
681   sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
682
683   sum =
684     calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
685                                ntohs (pkt6->ip6_hdr.paylgth));
686   pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
687
688   write_to_helper(pkt6, size);
689
690   return GNUNET_OK;
691 }
692
693 /**
694  * Main function that will be run by the scheduler.
695  *
696  * @param cls closure
697  * @param args remaining command-line arguments
698  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
699  * @param cfg_ configuration
700  */
701 static void
702 run (void *cls,
703      char *const *args,
704      const char *cfgfile,
705      const struct GNUNET_CONFIGURATION_Handle *cfg_)
706 {
707     static const struct GNUNET_MESH_MessageHandler handlers[] = {
708           {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
709           {receive_tcp_back, GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK, 0},
710           {receive_udp_back, GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK, 0},
711           {receive_tcp_back, GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK, 0},
712           {NULL, 0, 0}
713     };
714     mesh_handle = GNUNET_MESH_connect(cfg_,
715                                       NULL,
716                                       NULL,
717                                       handlers,
718                                       NULL);
719     cfg = cfg_;
720     restart_hijack = 0;
721     hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
722     heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
723     GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
724                                            &max_mappings);
725     udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
726     GNUNET_SCHEDULER_TaskIdentifier conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
727     GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
728     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
729 }
730
731 /**
732  * The main function to obtain template from gnunetd.
733  *
734  * @param argc number of arguments from the command line
735  * @param argv command line arguments
736  * @return 0 ok, 1 on error
737  */
738 int
739 main (int argc, char *const *argv) {
740     static const struct GNUNET_GETOPT_CommandLineOption options[] = {
741         GNUNET_GETOPT_OPTION_END
742     };
743
744     return (GNUNET_OK ==
745             GNUNET_PROGRAM_run (argc,
746                                 argv,
747                                 "gnunet-daemon-vpn",
748                                 gettext_noop ("help text"),
749                                 options, &run, NULL)) ? ret : 1;
750 }
751
752 /* end of gnunet-daemon-vpn.c */
753