hostkeys timing
[oweals/gnunet.git] / src / vpn / gnunet-daemon-exit.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-exit.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include <platform.h>
27 #include <gnunet_common.h>
28 #include <gnunet_program_lib.h>
29 #include <gnunet_protocols.h>
30 #include <gnunet_mesh_service.h>
31 #include <gnunet_constants.h>
32 #include <string.h>
33
34 #include "gnunet-vpn-packet.h"
35 #include "gnunet-helper-vpn-api.h"
36 #include "gnunet-vpn-checksum.h"
37
38 /**
39  * The handle to the configuration used throughout the process
40  */
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 /**
44  * The handle to the service-configuration
45  */
46 static struct GNUNET_CONFIGURATION_Handle *servicecfg;
47
48 /**
49  * The handle to the helper
50  */
51 struct GNUNET_VPN_HELPER_Handle *helper_handle;
52
53 /**
54  * Final status code.
55  */
56 static int ret;
57
58 /**
59  * The handle to mesh
60  */
61 static struct GNUNET_MESH_Handle *mesh_handle;
62
63 /**
64  * This hashmap contains the mapping from peer, service-descriptor,
65  * source-port and destination-port to a socket
66  */
67 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
68 static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
69
70 /**
71  * This struct is saved into the services-hashmap
72  */
73 struct redirect_service
74 {
75   /**
76    * One of 4 or 6
77    */
78   unsigned int version;
79   uint16_t my_port;
80   uint16_t remote_port;
81
82   union
83   {
84     struct
85     {
86       char ip4address[4];
87     } v4;
88     struct
89     {
90       char ip6address[16];
91     } v6;
92   };
93 };
94
95 struct redirect_info
96 {
97     /**
98      * The source-address of this connection. When a packet to this address is
99      * received, this tunnel is used to forward it.  ipv4-addresses will be put
100      * here left-aligned */
101   char addr[16];
102     /**
103      * The source-port of this connection
104      */
105   uint16_t pt;
106 };
107
108 /**
109  * This struct is saved into {tcp,udp}_connections;
110  */
111 struct redirect_state
112 {
113   struct GNUNET_MESH_Tunnel *tunnel;
114   GNUNET_HashCode desc;
115   struct redirect_service *serv;
116
117   /**
118    * The source-address and -port of this connection
119    */
120   struct redirect_info redirect_info;
121 };
122
123 /**
124  * This hashmaps saves interesting things about the configured services
125  */
126 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
127 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
128
129 /**
130  * Function that frees everything from a hashmap
131  */
132 static int
133 free_iterate(void* cls, const GNUNET_HashCode* hash, void* value)
134 {
135   GNUNET_free(value);
136   return GNUNET_YES;
137 }
138
139 /**
140  * Function scheduled as very last function, cleans up after us
141  */
142 static void
143 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
144     GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
145
146     GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
147                                           free_iterate,
148                                           NULL);
149
150     GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
151                                           free_iterate,
152                                           NULL);
153
154     if (mesh_handle != NULL)
155       {
156         GNUNET_MESH_disconnect(mesh_handle);
157         mesh_handle = NULL;
158       }
159 }
160
161 /**
162  * cls is the pointer to a GNUNET_MessageHeader that is
163  * followed by the service-descriptor and the udp-packet that should be sent;
164  */
165 static size_t
166 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
167 {
168   struct GNUNET_MessageHeader *hdr = cls;
169   GNUNET_assert (size >= ntohs (hdr->size));
170   memcpy (buf, hdr, ntohs (hdr->size));
171   size = ntohs(hdr->size);
172   GNUNET_free (cls);
173   return size;
174 }
175
176 /**
177  * @brief Handles an UDP-Packet received from the helper.
178  *
179  * @param udp A pointer to the Packet
180  * @param dadr The IP-Destination-address
181  * @param addrlen The length of the address
182  * @param version 4 or 6
183  */
184 static void
185 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen,
186                  unsigned int version)
187 {
188   struct redirect_info u_i;
189   struct GNUNET_MESH_Tunnel *tunnel;
190   uint32_t len;
191   struct GNUNET_MessageHeader *msg;
192
193   memset (&u_i, 0, sizeof (struct redirect_info));
194
195   memcpy (&u_i.addr, dadr, addrlen);
196
197   u_i.pt = udp->dpt;
198
199   /* get tunnel and service-descriptor from this */
200   GNUNET_HashCode hash;
201   GNUNET_CRYPTO_hash (&u_i, sizeof (struct redirect_info), &hash);
202   struct redirect_state *state =
203     GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
204
205   tunnel = state->tunnel;
206
207   /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
208   if (ntohs (udp->spt) == state->serv->remote_port)
209     {
210       udp->spt = htons (state->serv->my_port);
211     }
212   else
213     {
214       struct redirect_service *serv =
215         GNUNET_malloc (sizeof (struct redirect_service));
216       memcpy (serv, state->serv, sizeof (struct redirect_service));
217       serv->my_port = ntohs (udp->spt);
218       serv->remote_port = ntohs (udp->spt);
219       uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
220       memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
221               sizeof (GNUNET_HashCode));
222       *desc = ntohs (udp->spt);
223       GNUNET_HashCode hash;
224       GNUNET_CRYPTO_hash (desc, sizeof (GNUNET_HashCode) + 2, &hash);
225       GNUNET_assert (GNUNET_OK ==
226                      GNUNET_CONTAINER_multihashmap_put (udp_services,
227                                                         &hash, serv,
228                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
229       state->serv = serv;
230     }
231   /* send udp-packet back */
232   len =
233     sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
234     ntohs (udp->len);
235   msg = GNUNET_malloc (len);
236   msg->size = htons (len);
237   msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK);
238   GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
239   memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
240   void *_udp = desc + 1;
241   memcpy (_udp, udp, ntohs (udp->len));
242
243   GNUNET_MESH_notify_transmit_ready (tunnel,
244                                      GNUNET_NO,
245                                      42,
246                                      GNUNET_TIME_relative_divide
247                                      (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
248                                      (const struct GNUNET_PeerIdentity *)NULL,
249                                      len, send_udp_to_peer_notify_callback,
250                                      msg);
251 }
252
253 /**
254  * @brief Handles a TCP-Packet received from the helper.
255  *
256  * @param tcp A pointer to the Packet
257  * @param dadr The IP-Destination-address
258  * @param addrlen The length of the address
259  * @param version 4 or 6
260  * @param pktlen the length of the packet, including its header
261  */
262 static void
263 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
264                  unsigned int version, size_t pktlen)
265 {
266   struct redirect_info u_i;
267   struct GNUNET_MESH_Tunnel *tunnel;
268   uint32_t len;
269   struct GNUNET_MessageHeader *msg;
270
271   memset (&u_i, 0, sizeof (struct redirect_info));
272
273   memcpy (&u_i.addr, dadr, addrlen);
274   u_i.pt = tcp->dpt;
275
276   /* get tunnel and service-descriptor from this */
277   GNUNET_HashCode hash;
278   GNUNET_CRYPTO_hash (&u_i, sizeof (struct redirect_info), &hash);
279   struct redirect_state *state =
280     GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
281
282   tunnel = state->tunnel;
283
284   /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
285   if (ntohs (tcp->spt) == state->serv->remote_port)
286     {
287       tcp->spt = htons (state->serv->my_port);
288     }
289   else
290     {
291       // This is an illegal packet.
292       GNUNET_assert (0);
293     }
294   /* send udp-packet back */
295   len =
296     sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
297   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
298   msg = GNUNET_malloc (len);
299   msg->size = htons (len);
300   msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK);
301   GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
302   memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
303   void *_tcp = desc + 1;
304   memcpy (_tcp, tcp, pktlen);
305
306   GNUNET_MESH_notify_transmit_ready (tunnel,
307                                      GNUNET_NO,
308                                      42,
309                                      GNUNET_TIME_relative_divide
310                                      (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
311                                      (const struct GNUNET_PeerIdentity *)NULL,
312                                      len, send_udp_to_peer_notify_callback,
313                                      msg);
314 }
315
316
317 /**
318  * Receive packets from the helper-process
319  */
320 static void
321 message_token (void *cls,
322                void *client, const struct GNUNET_MessageHeader *message)
323 {
324   GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
325
326   struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
327
328   /* ethertype is ipv6 */
329   if (ntohs (pkt_tun->tun.type) == 0x86dd)
330     {
331       struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
332       if (0x11 == pkt6->ip6_hdr.nxthdr)
333         udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
334                          (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6);
335       else if (0x06 == pkt6->ip6_hdr.nxthdr)
336         tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
337                          (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6,
338                          ntohs (pkt6->ip6_hdr.paylgth));
339     }
340   else if (ntohs (pkt_tun->tun.type) == 0x0800)
341     {
342       struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
343       uint32_t tmp = pkt4->ip_hdr.dadr;
344       if (0x11 == pkt4->ip_hdr.proto)
345         udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
346                          (unsigned char *) &tmp, 4, 4);
347       else if (0x06 == pkt4->ip_hdr.proto)
348         {
349           size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
350           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
351           pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
352           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
353           tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
354                            (unsigned char *) &tmp, 4, 4, pktlen);
355         }
356     }
357   else
358     {
359       return;
360     }
361 }
362
363 /**
364  * Reads the configuration servicecfg and populates udp_services
365  *
366  * @param cls unused
367  * @param section name of section in config, equal to hostname
368  * @param option type of redirect
369  * @param value specification of services, format is
370  *         "OFFERED-PORT:HOSTNAME:HOST-PORT" (SPACE &lt;more of those&gt;)*
371  */
372 static void
373 read_service_conf (void *cls, const char *section, const char *option,
374                    const char *value)
375 {
376   char *cpy;
377   char *redirect;
378   char *hostname;
379   char *hostport;
380   GNUNET_HashCode hash;
381   uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
382   GNUNET_CRYPTO_hash (section, strlen (section) + 1,
383                       (GNUNET_HashCode *) (desc + 1));
384
385 #define TCP 2
386 #define UDP 1
387
388   unsigned int proto;
389   if (0 == strcmp ("UDP_REDIRECTS", option))
390     proto = UDP;
391   else if (0 == strcmp ("TCP_REDIRECTS", option))
392     proto = TCP;
393   else
394     proto = 0;
395
396   if (0 != proto)
397     {
398       cpy = GNUNET_strdup (value);
399       for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
400            (NULL, " "))
401         {
402           if (NULL == (hostname = strstr (redirect, ":")))
403             {
404               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
405                           redirect);
406               continue;
407             }
408           hostname[0] = '\0';
409           hostname++;
410           if (NULL == (hostport = strstr (hostname, ":")))
411             {
412               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
413                           redirect);
414               continue;
415             }
416           hostport[0] = '\0';
417           hostport++;
418
419           int local_port = atoi (redirect);
420           if (!((local_port > 0) && (local_port < 65536)))
421             GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
422                         redirect);
423
424           *desc = local_port;
425
426           GNUNET_CRYPTO_hash (desc, sizeof (GNUNET_HashCode) + 2, &hash);
427
428           struct redirect_service *serv =
429             GNUNET_malloc (sizeof (struct redirect_service));
430           memset (serv, 0, sizeof (struct redirect_service));
431           serv->my_port = local_port;
432
433           if (0 == strcmp ("localhost4", hostname))
434             {
435               serv->version = 4;
436
437               char *ip4addr;
438               GNUNET_assert (GNUNET_OK ==
439                              GNUNET_CONFIGURATION_get_value_string (cfg,
440                                                                     "exit",
441                                                                     "IPV4ADDR",
442                                                                     &ip4addr));
443               GNUNET_assert (1 ==
444                              inet_pton (AF_INET, ip4addr,
445                                         serv->v4.ip4address));
446               GNUNET_free (ip4addr);
447             }
448           else if (0 == strcmp ("localhost6", hostname))
449             {
450               serv->version = 6;
451
452               char *ip6addr;
453               GNUNET_assert (GNUNET_OK ==
454                              GNUNET_CONFIGURATION_get_value_string (cfg,
455                                                                     "exit",
456                                                                     "IPV6ADDR",
457                                                                     &ip6addr));
458               GNUNET_assert (1 ==
459                              inet_pton (AF_INET6, ip6addr,
460                                         serv->v6.ip6address));
461               GNUNET_free (ip6addr);
462             }
463           else
464             {
465               // Lookup, yadayadayada
466               GNUNET_assert (0);
467             }
468           serv->remote_port = atoi (hostport);
469           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
470                       *((unsigned long long *) (desc + 1)));
471           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key2 %x\n",
472                       *((unsigned long long *) &hash));
473           if (UDP == proto)
474             GNUNET_assert (GNUNET_OK ==
475                            GNUNET_CONTAINER_multihashmap_put (udp_services,
476                                                               &hash, serv,
477                                                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
478           else
479             GNUNET_assert (GNUNET_OK ==
480                            GNUNET_CONTAINER_multihashmap_put (tcp_services,
481                                                               &hash, serv,
482                                                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
483
484         }
485       GNUNET_free (cpy);
486     }
487 }
488
489 /**
490  * Start the helper-process
491  *
492  * If cls != NULL it is assumed that this function is called as a result of a dying
493  * helper. cls is then taken as handle to the old helper and is cleaned up.
494  */
495 static void
496 start_helper_and_schedule(void *cls,
497                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
498     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
499       return;
500
501     if (cls != NULL)
502       cleanup_helper(cls);
503     cls = NULL;
504
505     char* ifname;
506     char* ipv6addr;
507     char* ipv6prefix;
508     char* ipv4addr;
509     char* ipv4mask;
510
511     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
512       {
513         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
514         exit(1);
515       }
516
517     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
518       {
519         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
520         exit(1);
521       }
522
523     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
524       {
525         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
526         exit(1);
527       }
528
529     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
530       {
531         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
532         exit(1);
533       }
534
535     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
536       {
537         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
538         exit(1);
539       }
540
541     /* Start the helper
542      * Messages get passed to the function message_token
543      * When the helper dies, this function will be called again with the
544      * helper_handle as cls.
545      */
546     helper_handle = start_helper(ifname,
547                                  ipv6addr,
548                                  ipv6prefix,
549                                  ipv4addr,
550                                  ipv4mask,
551                                  "exit-gnunet",
552                                  start_helper_and_schedule,
553                                  message_token,
554                                  NULL,
555                                  NULL);
556
557     GNUNET_free(ipv6addr);
558     GNUNET_free(ipv6prefix);
559     GNUNET_free(ipv4addr);
560     GNUNET_free(ipv4mask);
561     GNUNET_free(ifname);
562 }
563
564 static void
565 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
566                      uint16_t protocol, void *ipaddress, void *tunnel,
567                      struct redirect_state *state, struct ip_pkt *pkt4)
568 {
569   uint32_t tmp, tmp2;
570
571   pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
572   pkt4->shdr.size = htons (len);
573   pkt4->tun.flags = 0;
574   pkt4->tun.type = htons (0x0800);
575
576   memcpy (&pkt4->data, payload, pktlen);
577
578   pkt4->ip_hdr.version = 4;
579   pkt4->ip_hdr.hdr_lngth = 5;
580   pkt4->ip_hdr.diff_serv = 0;
581   pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
582   pkt4->ip_hdr.ident = 0;
583   pkt4->ip_hdr.flags = 0;
584   pkt4->ip_hdr.frag_off = 0;
585   pkt4->ip_hdr.ttl = 255;
586   pkt4->ip_hdr.proto = protocol;
587   pkt4->ip_hdr.chks = 0;        /* Will be calculated later */
588
589   memcpy (&tmp, ipaddress, 4);
590   pkt4->ip_hdr.dadr = tmp;
591
592   /* Generate a new src-address */
593   char *ipv4addr;
594   char *ipv4mask;
595   GNUNET_assert (GNUNET_OK ==
596                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
597                                                         "IPV4ADDR",
598                                                         &ipv4addr));
599   GNUNET_assert (GNUNET_OK ==
600                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
601                                                         "IPV4MASK",
602                                                         &ipv4mask));
603   inet_pton (AF_INET, ipv4addr, &tmp);
604   inet_pton (AF_INET, ipv4mask, &tmp2);
605   GNUNET_free (ipv4addr);
606   GNUNET_free (ipv4mask);
607
608   /* This should be a noop */
609   tmp = tmp & tmp2;
610
611   tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
612
613   pkt4->ip_hdr.sadr = tmp;
614
615   memcpy (&state->redirect_info.addr, &tmp, 4);
616   if (0x11 == protocol)
617     {
618       struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
619       state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
620
621       pkt4_udp->udp_hdr.crc = 0;        /* Optional for IPv4 */
622     }
623   else if (0x06 == protocol)
624     {
625       struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
626       state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
627
628       pkt4_tcp->tcp_hdr.crc = 0;
629       uint32_t sum = 0;
630       tmp = pkt4->ip_hdr.sadr;
631       sum =
632         calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
633       tmp = pkt4->ip_hdr.dadr;
634       sum =
635         calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
636
637       tmp = (protocol << 16) | (0xffff & pktlen);
638
639       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
640
641       tmp = htonl(tmp);
642
643       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
644
645       sum =
646         calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
647       pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
648     }
649
650   pkt4->ip_hdr.chks =
651     calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
652 }
653
654 static void
655 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
656                      uint16_t protocol, void *ipaddress, void *tunnel,
657                      struct redirect_state *state, struct ip6_pkt *pkt6)
658 {
659   uint32_t tmp;
660
661   pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
662   pkt6->shdr.size = htons (len);
663   pkt6->tun.flags = 0;
664
665   pkt6->tun.type = htons (0x86dd);
666
667   memcpy (&pkt6->data, payload, pktlen);
668
669   pkt6->ip6_hdr.version = 6;
670   pkt6->ip6_hdr.nxthdr = protocol;
671   pkt6->ip6_hdr.paylgth = htons (pktlen);
672   pkt6->ip6_hdr.hoplmt = 64;
673
674   memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
675
676   /* Generate a new src-address
677    * This takes as much from the address of the tunnel as fits into
678    * the host-mask*/
679   char *ipv6addr;
680   unsigned long long ipv6prefix;
681   GNUNET_assert (GNUNET_OK ==
682                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
683                                                         "IPV6ADDR",
684                                                         &ipv6addr));
685   GNUNET_assert (GNUNET_OK ==
686                  GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
687                                                         "IPV6PREFIX",
688                                                         &ipv6prefix));
689   GNUNET_assert (ipv6prefix < 127);
690   ipv6prefix = (ipv6prefix + 7) / 8;
691
692   inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
693   GNUNET_free (ipv6addr);
694
695   if (ipv6prefix < (16 - sizeof (void *)))
696     ipv6prefix = 16 - sizeof (void *);
697
698   unsigned int offset = ipv6prefix - (16 - sizeof (void *));
699   memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
700           ((char *) &tunnel) + offset, 16 - ipv6prefix);
701
702   /* copy the needed information into the state */
703   memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
704
705   if (0x11 == protocol)
706     {
707       struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
708       state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
709
710       pkt6_udp->udp_hdr.crc = 0;
711       uint32_t sum = 0;
712       sum =
713         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
714       sum =
715         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
716       tmp = (htons (pktlen) & 0xffff);
717       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
718       tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
719       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
720
721       sum =
722         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
723                                    ntohs (pkt6_udp->udp_hdr.len));
724       pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
725     }
726   else if (0x06 == protocol)
727     {
728       struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
729       state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
730
731       pkt6_tcp->tcp_hdr.crc = 0;
732       uint32_t sum = 0;
733       sum =
734         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
735       sum =
736         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
737       tmp = htonl(pktlen);
738       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
739       tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
740       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
741
742       sum =
743         calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
744                                    ntohs (pkt6->ip6_hdr.paylgth));
745       pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
746     }
747 }
748
749 /**
750  * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
751  */
752 static int
753 receive_tcp_service (void *cls,
754                      struct GNUNET_MESH_Tunnel *tunnel,
755                      void **tunnel_ctx,
756                      const struct GNUNET_PeerIdentity *sender,
757                      const struct GNUNET_MessageHeader *message,
758                      const struct GNUNET_TRANSPORT_ATS_Information *atsi)
759 {
760   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
761   GNUNET_HashCode hash;
762   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
763   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
764   unsigned int pkt_len = ntohs(message->size) - sizeof (struct
765                                                         GNUNET_MessageHeader) -
766     sizeof(GNUNET_HashCode);
767
768   /** Get the configuration from the services-hashmap.
769    *
770    * Which service is needed only depends on the service-descriptor and the
771    * destination-port
772    */
773   uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
774
775   memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
776   *tcp_desc = ntohs (pkt->dpt);
777   GNUNET_CRYPTO_hash (tcp_desc, sizeof (GNUNET_HashCode) + 2, &hash);
778   struct redirect_service *serv =
779     GNUNET_CONTAINER_multihashmap_get (tcp_services, &hash);
780   if (NULL == serv)
781     {
782       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n",
783                   *tcp_desc);
784       return GNUNET_YES;
785     }
786
787   pkt->dpt = htons (serv->remote_port);
788   /* FIXME -> check acl etc */
789
790   char *buf;
791   size_t len;
792
793   /* Prepare the state.
794    * This will be saved in the hashmap, so that the receiving procedure knows
795    * through which tunnel this connection has to be routed.
796    */
797   struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
798   memset (state, 0, sizeof (struct redirect_state));
799   state->tunnel = tunnel;
800   state->serv = serv;
801   memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
802
803   len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
804     sizeof (struct ip6_hdr) + pkt_len;
805   buf = alloca (len);
806
807   memset (buf, 0, len);
808
809   switch (serv->version)
810     {
811     case 4:
812       prepare_ipv4_packet (len, pkt_len, pkt, 0x06,    /* TCP */
813                            &serv->v4.ip4address,
814                            tunnel, state, (struct ip_pkt *) buf);
815       break;
816     case 6:
817       prepare_ipv6_packet (len, pkt_len, pkt, 0x06,    /* TCP */
818                            &serv->v6.ip6address,
819                            tunnel, state, (struct ip6_pkt *) buf);
820
821       break;
822     default:
823       GNUNET_assert (0);
824       break;
825     }
826
827   GNUNET_CRYPTO_hash (&state->redirect_info, sizeof (struct redirect_info), &hash);
828
829   if (GNUNET_NO ==
830       GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &hash))
831     GNUNET_CONTAINER_multihashmap_put (tcp_connections, &hash, state,
832                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
833   else
834     GNUNET_free (state);
835
836   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
837   return GNUNET_YES;
838 }
839
840 /**
841  * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
842  */
843 static int
844 receive_udp_service (void *cls,
845                      struct GNUNET_MESH_Tunnel *tunnel,
846                      void **tunnel_ctx,
847                      const struct GNUNET_PeerIdentity *sender,
848                      const struct GNUNET_MessageHeader *message,
849                      const struct GNUNET_TRANSPORT_ATS_Information *atsi)
850 {
851   GNUNET_HashCode hash;
852   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
853   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
854
855   GNUNET_assert (ntohs (pkt->len) ==
856                  ntohs (message->size) -
857                  sizeof (struct GNUNET_MessageHeader) -
858                  sizeof (GNUNET_HashCode));
859
860   /* Get the configuration from the hashmap */
861   uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
862   memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
863   *udp_desc = ntohs (pkt->dpt);
864   GNUNET_CRYPTO_hash (udp_desc, sizeof (GNUNET_HashCode) + 2, &hash);
865   struct redirect_service *serv =
866     GNUNET_CONTAINER_multihashmap_get (udp_services, &hash);
867   if (NULL == serv)
868     {
869       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n",
870                   *udp_desc);
871       return GNUNET_YES;
872     }
873
874   pkt->dpt = htons (serv->remote_port);
875   /* FIXME -> check acl etc */
876
877   char *buf;
878   size_t len;
879
880   /* Prepare the state.
881    * This will be saved in the hashmap, so that the receiving procedure knows
882    * through which tunnel this connection has to be routed.
883    */
884   struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
885   memset (state, 0, sizeof (struct redirect_state));
886   state->tunnel = tunnel;
887   state->serv = serv;
888   memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
889
890   len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
891     sizeof (struct ip6_hdr) + ntohs (pkt->len);
892   buf = alloca (len);
893
894   memset (buf, 0, len);
895
896   switch (serv->version)
897     {
898     case 4:
899       prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11,    /* UDP */
900                            &serv->v4.ip4address,
901                            tunnel, state, (struct ip_pkt *) buf);
902       break;
903     case 6:
904       prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11,    /* UDP */
905                            &serv->v6.ip6address,
906                            tunnel, state, (struct ip6_pkt *) buf);
907
908       break;
909     default:
910       GNUNET_assert (0);
911       break;
912     }
913
914   GNUNET_CRYPTO_hash (&state->redirect_info, sizeof (struct redirect_info), &hash);
915
916   if (GNUNET_NO ==
917       GNUNET_CONTAINER_multihashmap_contains (udp_connections, &hash))
918     GNUNET_CONTAINER_multihashmap_put (udp_connections, &hash, state,
919                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
920   else
921     GNUNET_free (state);
922
923   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
924   return GNUNET_YES;
925 }
926
927 /**
928  * @brief Main function that will be run by the scheduler.
929  *
930  * @param cls closure
931  * @param args remaining command-line arguments
932  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
933  * @param cfg_ configuration
934  */
935 static void
936 run (void *cls,
937      char *const *args,
938      const char *cfgfile,
939      const struct GNUNET_CONFIGURATION_Handle *cfg_)
940 {
941   const static struct GNUNET_MESH_MessageHandler handlers[] = {
942         {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
943         {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
944         {NULL, 0, 0}
945   };
946   mesh_handle = GNUNET_MESH_connect(cfg_,
947                                     NULL,
948                                     NULL,
949                                     handlers,
950                                     NULL);
951
952   cfg = cfg_;
953   udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
954   tcp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
955   udp_services = GNUNET_CONTAINER_multihashmap_create(65536);
956   tcp_services = GNUNET_CONTAINER_multihashmap_create(65536);
957
958   char *services;
959   GNUNET_CONFIGURATION_get_value_filename(cfg, "dns", "SERVICES", &services);
960   servicecfg = GNUNET_CONFIGURATION_create();
961   if (GNUNET_OK == GNUNET_CONFIGURATION_parse(servicecfg, services))
962     {
963       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
964       GNUNET_CONFIGURATION_iterate(servicecfg, read_service_conf, NULL);
965     }
966   if (NULL != services)
967     GNUNET_free(services);
968
969   GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
970   GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
971 }
972
973 /**
974  * The main function to obtain template from gnunetd.
975  *
976  * @param argc number of arguments from the command line
977  * @param argv command line arguments
978  * @return 0 ok, 1 on error
979  */
980 int
981 main (int argc, char *const *argv) {
982     static const struct GNUNET_GETOPT_CommandLineOption options[] = {
983         GNUNET_GETOPT_OPTION_END
984     };
985
986     return (GNUNET_OK ==
987             GNUNET_PROGRAM_run (argc,
988                                 argv,
989                                 "exit",
990                                 gettext_noop ("help text"),
991                                 options, &run, NULL)) ? ret : 1;
992 }
993
994 /* end of gnunet-daemon-exit.c */
995