check
[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* tc) {
179           if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
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 static void
198 hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
199 {
200
201   /* the gnunet hashmap only uses the first 32bit of the hash
202    *
203    * build the hash out of the last two bytes of the address and the 2 bytes of
204    * the port
205    */
206   memcpy(&hash, &u_i->pt, sizeof(u_i->pt));
207   memcpy(((unsigned char*)&hash)+2, u_i->addr+(addrlen-2), 2);
208 }
209
210 /**
211  * cls is the pointer to a GNUNET_MessageHeader that is
212  * followed by the service-descriptor and the udp-packet that should be sent;
213  */
214 static size_t
215 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
216 {
217   struct GNUNET_MessageHeader *hdr = cls;
218   GNUNET_assert (size >= ntohs (hdr->size));
219   memcpy (buf, hdr, ntohs (hdr->size));
220   size = ntohs(hdr->size);
221   GNUNET_free (cls);
222   return size;
223 }
224
225 /**
226  * @brief Handles an UDP-Packet received from the helper.
227  *
228  * @param udp A pointer to the Packet
229  * @param dadr The IP-Destination-address
230  * @param addrlen The length of the address
231  * @param version 4 or 6
232  */
233 static void
234 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen,
235                  unsigned int version)
236 {
237   struct redirect_info u_i;
238   struct GNUNET_MESH_Tunnel *tunnel;
239   uint32_t len;
240   struct GNUNET_MessageHeader *msg;
241
242   memset (&u_i, 0, sizeof (struct redirect_info));
243
244   memcpy (&u_i.addr, dadr, addrlen);
245
246   u_i.pt = udp->dpt;
247
248   /* get tunnel and service-descriptor from this */
249   GNUNET_HashCode hash;
250   hash_redirect_info(&hash, &u_i, addrlen);
251
252   struct redirect_state *state =
253     GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
254
255   /* Mark this connection as freshly used */
256   GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
257                                      GNUNET_TIME_absolute_get ().abs_value);
258
259   tunnel = state->tunnel;
260
261   /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
262   if (ntohs (udp->spt) == state->serv->remote_port)
263     {
264       udp->spt = htons (state->serv->my_port);
265     }
266   else
267     {
268       struct redirect_service *serv =
269         GNUNET_malloc (sizeof (struct redirect_service));
270       memcpy (serv, state->serv, sizeof (struct redirect_service));
271       serv->my_port = ntohs (udp->spt);
272       serv->remote_port = ntohs (udp->spt);
273       uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
274       memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
275               sizeof (GNUNET_HashCode));
276       *desc = ntohs (udp->spt);
277       GNUNET_assert (GNUNET_OK ==
278                      GNUNET_CONTAINER_multihashmap_put (udp_services,
279                                                         (GNUNET_HashCode*)desc, serv,
280                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
281
282       state->serv = serv;
283     }
284   /* send udp-packet back */
285   len =
286     sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
287     ntohs (udp->len);
288   msg = GNUNET_malloc (len);
289   msg->size = htons (len);
290   msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK);
291   GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
292   memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
293   void *_udp = desc + 1;
294   memcpy (_udp, udp, ntohs (udp->len));
295
296   GNUNET_MESH_notify_transmit_ready (tunnel,
297                                      GNUNET_NO,
298                                      42,
299                                      GNUNET_TIME_relative_divide
300                                      (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
301                                      (const struct GNUNET_PeerIdentity *)
302                                      NULL, len,
303                                      send_udp_to_peer_notify_callback, msg);
304 }
305
306 /**
307  * @brief Handles a TCP-Packet received from the helper.
308  *
309  * @param tcp A pointer to the Packet
310  * @param dadr The IP-Destination-address
311  * @param addrlen The length of the address
312  * @param version 4 or 6
313  * @param pktlen the length of the packet, including its header
314  */
315 static void
316 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
317                  unsigned int version, size_t pktlen)
318 {
319   struct redirect_info u_i;
320   struct GNUNET_MESH_Tunnel *tunnel;
321   uint32_t len;
322   struct GNUNET_MessageHeader *msg;
323
324   memset (&u_i, 0, sizeof (struct redirect_info));
325
326   memcpy (&u_i.addr, dadr, addrlen);
327   u_i.pt = tcp->dpt;
328
329   /* get tunnel and service-descriptor from this */
330   GNUNET_HashCode hash;
331   hash_redirect_info(&hash, &u_i, addrlen);
332
333   struct redirect_state *state =
334     GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
335
336   /* Mark this connection as freshly used */
337   GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
338                                      GNUNET_TIME_absolute_get ().abs_value);
339
340   tunnel = state->tunnel;
341
342   /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
343   if (ntohs (tcp->spt) == state->serv->remote_port)
344     {
345       tcp->spt = htons (state->serv->my_port);
346     }
347   else
348     {
349       // This is an illegal packet.
350       return;
351     }
352   /* send tcp-packet back */
353   len =
354     sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
355   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
356   msg = GNUNET_malloc (len);
357   msg->size = htons (len);
358   msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK);
359   GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
360   memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
361   void *_tcp = desc + 1;
362   memcpy (_tcp, tcp, pktlen);
363
364   GNUNET_MESH_notify_transmit_ready (tunnel,
365                                      GNUNET_NO,
366                                      42,
367                                      GNUNET_TIME_relative_divide
368                                      (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
369                                      (const struct GNUNET_PeerIdentity *)NULL,
370                                      len, send_udp_to_peer_notify_callback,
371                                      msg);
372 }
373
374
375 /**
376  * Receive packets from the helper-process
377  */
378 static void
379 message_token (void *cls,
380                void *client, const struct GNUNET_MessageHeader *message)
381 {
382   GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
383
384   struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
385
386   /* ethertype is ipv6 */
387   if (ntohs (pkt_tun->tun.type) == 0x86dd)
388     {
389       struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
390       if (0x11 == pkt6->ip6_hdr.nxthdr)
391         udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
392                          (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6);
393       else if (0x06 == pkt6->ip6_hdr.nxthdr)
394         tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
395                          (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6,
396                          ntohs (pkt6->ip6_hdr.paylgth));
397     }
398   else if (ntohs (pkt_tun->tun.type) == 0x0800)
399     {
400       struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
401       uint32_t tmp = pkt4->ip_hdr.dadr;
402       if (0x11 == pkt4->ip_hdr.proto)
403         udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
404                          (unsigned char *) &tmp, 4, 4);
405       else if (0x06 == pkt4->ip_hdr.proto)
406         {
407           size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
408           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
409           pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
410           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
411           tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
412                            (unsigned char *) &tmp, 4, 4, pktlen);
413         }
414     }
415   else
416     {
417       return;
418     }
419 }
420
421 /**
422  * Reads the configuration servicecfg and populates udp_services
423  *
424  * @param cls unused
425  * @param section name of section in config, equal to hostname
426  * @param option type of redirect
427  * @param value specification of services, format is
428  *         "OFFERED-PORT:HOSTNAME:HOST-PORT" (SPACE &lt;more of those&gt;)*
429  */
430 static void
431 read_service_conf (void *cls, const char *section, const char *option,
432                    const char *value)
433 {
434   char *cpy;
435   char *redirect;
436   char *hostname;
437   char *hostport;
438   GNUNET_HashCode hash;
439   uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
440   GNUNET_CRYPTO_hash (section, strlen (section) + 1,
441                       (GNUNET_HashCode *) (desc + 1));
442
443 #define TCP 2
444 #define UDP 1
445
446   unsigned int proto;
447   if (0 == strcmp ("UDP_REDIRECTS", option))
448     proto = UDP;
449   else if (0 == strcmp ("TCP_REDIRECTS", option))
450     proto = TCP;
451   else
452     proto = 0;
453
454   if (0 != proto)
455     {
456       cpy = GNUNET_strdup (value);
457       for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
458            (NULL, " "))
459         {
460           if (NULL == (hostname = strstr (redirect, ":")))
461             {
462               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
463                           redirect);
464               continue;
465             }
466           hostname[0] = '\0';
467           hostname++;
468           if (NULL == (hostport = strstr (hostname, ":")))
469             {
470               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
471                           redirect);
472               continue;
473             }
474           hostport[0] = '\0';
475           hostport++;
476
477           int local_port = atoi (redirect);
478           if (!((local_port > 0) && (local_port < 65536)))
479             GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
480                         redirect);
481
482           *desc = local_port;
483
484           struct redirect_service *serv =
485             GNUNET_malloc (sizeof (struct redirect_service));
486           memset (serv, 0, sizeof (struct redirect_service));
487           serv->my_port = local_port;
488
489           if (0 == strcmp ("localhost4", hostname))
490             {
491               serv->version = 4;
492
493               char *ip4addr;
494               GNUNET_assert (GNUNET_OK ==
495                              GNUNET_CONFIGURATION_get_value_string (cfg,
496                                                                     "exit",
497                                                                     "IPV4ADDR",
498                                                                     &ip4addr));
499               GNUNET_assert (1 ==
500                              inet_pton (AF_INET, ip4addr,
501                                         serv->v4.ip4address));
502               GNUNET_free (ip4addr);
503             }
504           else if (0 == strcmp ("localhost6", hostname))
505             {
506               serv->version = 6;
507
508               char *ip6addr;
509               GNUNET_assert (GNUNET_OK ==
510                              GNUNET_CONFIGURATION_get_value_string (cfg,
511                                                                     "exit",
512                                                                     "IPV6ADDR",
513                                                                     &ip6addr));
514               GNUNET_assert (1 ==
515                              inet_pton (AF_INET6, ip6addr,
516                                         serv->v6.ip6address));
517               GNUNET_free (ip6addr);
518             }
519           else
520             {
521               // TODO Lookup, yadayadayada
522               GNUNET_assert (0);
523             }
524           serv->remote_port = atoi (hostport);
525           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
526                       *((unsigned long long *) (desc + 1)));
527           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key2 %x\n",
528                       *((unsigned long long *) &hash));
529           if (UDP == proto)
530             GNUNET_assert (GNUNET_OK ==
531                            GNUNET_CONTAINER_multihashmap_put (udp_services,
532                                                               (GNUNET_HashCode*)desc, serv,
533                                                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
534           else
535             GNUNET_assert (GNUNET_OK ==
536                            GNUNET_CONTAINER_multihashmap_put (tcp_services,
537                                                               (GNUNET_HashCode*)desc, serv,
538                                                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
539
540         }
541       GNUNET_free (cpy);
542     }
543 }
544
545 /**
546  * Start the helper-process
547  *
548  * If cls != NULL it is assumed that this function is called as a result of a dying
549  * helper. cls is then taken as handle to the old helper and is cleaned up.
550  */
551 static void
552 start_helper_and_schedule(void *cls,
553                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
554     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
555       return;
556
557     if (cls != NULL)
558       cleanup_helper(cls);
559     cls = NULL;
560
561     char* ifname;
562     char* ipv6addr;
563     char* ipv6prefix;
564     char* ipv4addr;
565     char* ipv4mask;
566
567     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
568       {
569         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
570         exit(1);
571       }
572
573     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
574       {
575         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
576         exit(1);
577       }
578
579     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
580       {
581         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
582         exit(1);
583       }
584
585     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
586       {
587         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
588         exit(1);
589       }
590
591     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
592       {
593         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
594         exit(1);
595       }
596
597     /* Start the helper
598      * Messages get passed to the function message_token
599      * When the helper dies, this function will be called again with the
600      * helper_handle as cls.
601      */
602     helper_handle = start_helper(ifname,
603                                  ipv6addr,
604                                  ipv6prefix,
605                                  ipv4addr,
606                                  ipv4mask,
607                                  "exit-gnunet",
608                                  start_helper_and_schedule,
609                                  message_token,
610                                  NULL,
611                                  NULL);
612
613     GNUNET_free(ipv6addr);
614     GNUNET_free(ipv6prefix);
615     GNUNET_free(ipv4addr);
616     GNUNET_free(ipv4mask);
617     GNUNET_free(ifname);
618 }
619
620 static void
621 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
622                      uint16_t protocol, void *ipaddress, void *tunnel,
623                      struct redirect_state *state, struct ip_pkt *pkt4)
624 {
625   uint32_t tmp, tmp2;
626
627   pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
628   pkt4->shdr.size = htons (len);
629   pkt4->tun.flags = 0;
630   pkt4->tun.type = htons (0x0800);
631
632   memcpy (&pkt4->data, payload, pktlen);
633
634   pkt4->ip_hdr.version = 4;
635   pkt4->ip_hdr.hdr_lngth = 5;
636   pkt4->ip_hdr.diff_serv = 0;
637   pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
638   pkt4->ip_hdr.ident = 0;
639   pkt4->ip_hdr.flags = 0;
640   pkt4->ip_hdr.frag_off = 0;
641   pkt4->ip_hdr.ttl = 255;
642   pkt4->ip_hdr.proto = protocol;
643   pkt4->ip_hdr.chks = 0;        /* Will be calculated later */
644
645   memcpy (&tmp, ipaddress, 4);
646   pkt4->ip_hdr.dadr = tmp;
647
648   /* Generate a new src-address */
649   char *ipv4addr;
650   char *ipv4mask;
651   GNUNET_assert (GNUNET_OK ==
652                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
653                                                         "IPV4ADDR",
654                                                         &ipv4addr));
655   GNUNET_assert (GNUNET_OK ==
656                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
657                                                         "IPV4MASK",
658                                                         &ipv4mask));
659   inet_pton (AF_INET, ipv4addr, &tmp);
660   inet_pton (AF_INET, ipv4mask, &tmp2);
661   GNUNET_free (ipv4addr);
662   GNUNET_free (ipv4mask);
663
664   /* This should be a noop */
665   tmp = tmp & tmp2;
666
667   tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
668
669   pkt4->ip_hdr.sadr = tmp;
670
671   memcpy (&state->redirect_info.addr, &tmp, 4);
672   if (0x11 == protocol)
673     {
674       struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
675       state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
676
677       pkt4_udp->udp_hdr.crc = 0;        /* Optional for IPv4 */
678     }
679   else if (0x06 == protocol)
680     {
681       struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
682       state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
683
684       pkt4_tcp->tcp_hdr.crc = 0;
685       uint32_t sum = 0;
686       tmp = pkt4->ip_hdr.sadr;
687       sum =
688         calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
689       tmp = pkt4->ip_hdr.dadr;
690       sum =
691         calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
692
693       tmp = (protocol << 16) | (0xffff & pktlen);
694
695       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
696
697       tmp = htonl(tmp);
698
699       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
700
701       sum =
702         calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
703       pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
704     }
705
706   pkt4->ip_hdr.chks =
707     calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
708 }
709
710 static void
711 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
712                      uint16_t protocol, void *ipaddress, void *tunnel,
713                      struct redirect_state *state, struct ip6_pkt *pkt6)
714 {
715   uint32_t tmp;
716
717   pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
718   pkt6->shdr.size = htons (len);
719   pkt6->tun.flags = 0;
720
721   pkt6->tun.type = htons (0x86dd);
722
723   memcpy (&pkt6->data, payload, pktlen);
724
725   pkt6->ip6_hdr.version = 6;
726   pkt6->ip6_hdr.nxthdr = protocol;
727   pkt6->ip6_hdr.paylgth = htons (pktlen);
728   pkt6->ip6_hdr.hoplmt = 64;
729
730   memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
731
732   /* Generate a new src-address
733    * This takes as much from the address of the tunnel as fits into
734    * the host-mask*/
735   char *ipv6addr;
736   unsigned long long ipv6prefix;
737   GNUNET_assert (GNUNET_OK ==
738                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
739                                                         "IPV6ADDR",
740                                                         &ipv6addr));
741   GNUNET_assert (GNUNET_OK ==
742                  GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
743                                                         "IPV6PREFIX",
744                                                         &ipv6prefix));
745   GNUNET_assert (ipv6prefix < 127);
746   ipv6prefix = (ipv6prefix + 7) / 8;
747
748   inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
749   GNUNET_free (ipv6addr);
750
751   if (ipv6prefix < (16 - sizeof (void *)))
752     ipv6prefix = 16 - sizeof (void *);
753
754   unsigned int offset = ipv6prefix - (16 - sizeof (void *));
755   memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
756           ((char *) &tunnel) + offset, 16 - ipv6prefix);
757
758   /* copy the needed information into the state */
759   memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
760
761   if (0x11 == protocol)
762     {
763       struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
764       state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
765
766       pkt6_udp->udp_hdr.crc = 0;
767       uint32_t sum = 0;
768       sum =
769         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
770       sum =
771         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
772       tmp = (htons (pktlen) & 0xffff);
773       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
774       tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
775       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
776
777       sum =
778         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
779                                    ntohs (pkt6_udp->udp_hdr.len));
780       pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
781     }
782   else if (0x06 == protocol)
783     {
784       struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
785       state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
786
787       pkt6_tcp->tcp_hdr.crc = 0;
788       uint32_t sum = 0;
789       sum =
790         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
791       sum =
792         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
793       tmp = htonl(pktlen);
794       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
795       tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
796       sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
797
798       sum =
799         calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
800                                    ntohs (pkt6->ip6_hdr.paylgth));
801       pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
802     }
803 }
804
805 /**
806  * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
807  */
808 static int
809 receive_tcp_service (void *cls,
810                      struct GNUNET_MESH_Tunnel *tunnel,
811                      void **tunnel_ctx,
812                      const struct GNUNET_PeerIdentity *sender,
813                      const struct GNUNET_MessageHeader *message,
814                      const struct GNUNET_TRANSPORT_ATS_Information *atsi)
815 {
816   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
817   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
818   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
819   unsigned int pkt_len = ntohs (message->size) - sizeof (struct
820                                                          GNUNET_MessageHeader)
821     - sizeof (GNUNET_HashCode);
822
823   /** Get the configuration from the services-hashmap.
824    *
825    * Which service is needed only depends on the service-descriptor and the
826    * destination-port
827    */
828   uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
829
830   memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
831   *tcp_desc = ntohs (pkt->dpt);
832   struct redirect_service *serv =
833     GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
834   if (NULL == serv)
835     {
836       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
837                   "No service found for TCP dpt %d!\n", *tcp_desc);
838       return GNUNET_YES;
839     }
840
841   pkt->dpt = htons (serv->remote_port);
842
843   /*
844    * At this point it would be possible to check against some kind of ACL.
845    */
846
847   char *buf;
848   size_t len;
849
850   /* Prepare the state.
851    * This will be saved in the hashmap, so that the receiving procedure knows
852    * through which tunnel this connection has to be routed.
853    */
854   struct redirect_state *state =
855     GNUNET_malloc (sizeof (struct redirect_state));
856   memset (state, 0, sizeof (struct redirect_state));
857   state->tunnel = tunnel;
858   state->serv = serv;
859   state->hashmap = tcp_connections;
860   memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
861
862   len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
863     sizeof (struct ip6_hdr) + pkt_len;
864   buf = alloca (len);
865
866   memset (buf, 0, len);
867
868   switch (serv->version)
869     {
870     case 4:
871       prepare_ipv4_packet (len, pkt_len, pkt, 0x06,     /* TCP */
872                            &serv->v4.ip4address,
873                            tunnel, state, (struct ip_pkt *) buf);
874       break;
875     case 6:
876       prepare_ipv6_packet (len, pkt_len, pkt, 0x06,     /* TCP */
877                            &serv->v6.ip6address,
878                            tunnel, state, (struct ip6_pkt *) buf);
879
880       break;
881     default:
882       GNUNET_assert (0);
883       break;
884     }
885
886   hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
887
888   if (GNUNET_NO ==
889       GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
890     {
891       GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
892                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
893
894       state->heap_node =
895         GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
896                                       GNUNET_TIME_absolute_get ().abs_value);
897
898       if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
899         GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
900     }
901   else
902     GNUNET_free (state);
903
904   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
905   return GNUNET_YES;
906 }
907
908 /**
909  * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
910  */
911 static int
912 receive_udp_service (void *cls,
913                      struct GNUNET_MESH_Tunnel *tunnel,
914                      void **tunnel_ctx,
915                      const struct GNUNET_PeerIdentity *sender,
916                      const struct GNUNET_MessageHeader *message,
917                      const struct GNUNET_TRANSPORT_ATS_Information *atsi)
918 {
919   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
920   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
921
922   GNUNET_assert (ntohs (pkt->len) ==
923                  ntohs (message->size) -
924                  sizeof (struct GNUNET_MessageHeader) -
925                  sizeof (GNUNET_HashCode));
926
927   /* Get the configuration from the hashmap */
928   uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
929   memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
930   *udp_desc = ntohs (pkt->dpt);
931   struct redirect_service *serv =
932     GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
933   if (NULL == serv)
934     {
935       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
936                   "No service found for UDP dpt %d!\n", *udp_desc);
937       return GNUNET_YES;
938     }
939
940   pkt->dpt = htons (serv->remote_port);
941
942   /*
943    * At this point it would be possible to check against some kind of ACL.
944    */
945
946   char *buf;
947   size_t len;
948
949   /* Prepare the state.
950    * This will be saved in the hashmap, so that the receiving procedure knows
951    * through which tunnel this connection has to be routed.
952    */
953   struct redirect_state *state =
954     GNUNET_malloc (sizeof (struct redirect_state));
955   memset (state, 0, sizeof (struct redirect_state));
956   state->tunnel = tunnel;
957   state->serv = serv;
958   state->hashmap = udp_connections;
959   memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
960
961   len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
962     sizeof (struct ip6_hdr) + ntohs (pkt->len);
963   buf = alloca (len);
964
965   memset (buf, 0, len);
966
967   switch (serv->version)
968     {
969     case 4:
970       prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11,    /* UDP */
971                            &serv->v4.ip4address,
972                            tunnel, state, (struct ip_pkt *) buf);
973       break;
974     case 6:
975       prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11,    /* UDP */
976                            &serv->v6.ip6address,
977                            tunnel, state, (struct ip6_pkt *) buf);
978
979       break;
980     default:
981       GNUNET_assert (0);
982       break;
983     }
984
985   hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
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