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