compile before commiting
[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_applications.h>
31 #include <gnunet_mesh_service.h>
32 #include <gnunet_constants.h>
33 #include <string.h>
34
35 #include "gnunet-vpn-packet.h"
36 #include "gnunet-helper-vpn-api.h"
37 #include "gnunet-vpn-checksum.h"
38
39 GNUNET_SCHEDULER_TaskIdentifier shs_task;
40
41 /**
42  * The handle to the configuration used throughout the process
43  */
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 /**
47  * The handle to the helper
48  */
49 struct GNUNET_VPN_HELPER_Handle *helper_handle;
50
51 /**
52  * Final status code.
53  */
54 static int ret;
55
56 /**
57  * The handle to mesh
58  */
59 static struct GNUNET_MESH_Handle *mesh_handle;
60
61 /**
62  * This hashmaps contains the mapping from peer, service-descriptor,
63  * source-port and destination-port to a struct redirect_state
64  */
65 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
66 static struct GNUNET_CONTAINER_Heap *udp_connections_heap;
67 static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
68 static struct GNUNET_CONTAINER_Heap *tcp_connections_heap;
69
70 /**
71  * If there are at least this many udp-Connections, old ones will be removed
72  */
73 static long long unsigned int max_udp_connections = 200;
74
75 /**
76  * If there are at least this many tcp-Connections, old ones will be removed
77  */
78 static long long unsigned int max_tcp_connections = 200;
79
80 struct remote_addr
81 {
82   char addrlen;
83   unsigned char addr[16];
84   char proto;
85 };
86
87 /**
88  * This struct is saved into the services-hashmap
89  */
90 struct redirect_service
91 {
92   /**
93    * One of 4 or 6
94    */
95   unsigned int version;
96   uint16_t my_port;
97   uint16_t remote_port;
98
99   union
100   {
101     struct
102     {
103       char ip4address[4];
104     } v4;
105     struct
106     {
107       char ip6address[16];
108     } v6;
109   };
110 };
111
112 struct redirect_info
113 {
114     /**
115      * The source-address of this connection. When a packet to this address is
116      * received, this tunnel is used to forward it.  ipv4-addresses will be put
117      * here left-aligned */
118   char addr[16];
119     /**
120      * The source-port of this connection
121      */
122   uint16_t pt;
123 };
124
125 /**
126  * This struct is saved into {tcp,udp}_connections;
127  */
128 struct redirect_state
129 {
130   struct GNUNET_MESH_Tunnel *tunnel;
131   GNUNET_HashCode desc;
132   struct redirect_service *serv;
133   struct remote_addr remote;
134
135   struct GNUNET_CONTAINER_HeapNode *heap_node;
136   struct GNUNET_CONTAINER_MultiHashMap *hashmap;
137   GNUNET_HashCode hash;
138
139   enum
140   { SERVICE, REMOTE } type;
141
142   /**
143    * The source-address and -port of this connection
144    */
145   struct redirect_info redirect_info;
146 };
147
148 /**
149  * This hashmaps saves interesting things about the configured services
150  */
151 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
152 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
153
154 struct tunnel_notify_queue
155 {
156   struct tunnel_notify_queue *next;
157   struct tunnel_notify_queue *prev;
158   void *cls;
159   size_t len;
160 };
161
162 /**
163  * Function that frees everything from a hashmap
164  */
165 static int
166 free_iterate (void *cls __attribute__ ((unused)), const GNUNET_HashCode * hash
167               __attribute__ ((unused)), void *value)
168 {
169   GNUNET_free (value);
170   return GNUNET_YES;
171 }
172
173 /**
174  * Function scheduled as very last function, cleans up after us
175  */
176 static void
177 cleanup (void *cls
178          __attribute__ ((unused)),
179          const struct GNUNET_SCHEDULER_TaskContext *tskctx)
180 {
181   GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
182
183   GNUNET_CONTAINER_multihashmap_iterate (udp_connections, free_iterate, NULL);
184
185   GNUNET_CONTAINER_multihashmap_iterate (tcp_connections, free_iterate, NULL);
186
187   if (mesh_handle != NULL)
188   {
189     GNUNET_MESH_disconnect (mesh_handle);
190     mesh_handle = NULL;
191   }
192 }
193
194 static void
195 collect_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 {
197   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
198     return;
199
200
201   struct GNUNET_CONTAINER_Heap *heap = cls;
202
203   struct redirect_state *state = GNUNET_CONTAINER_heap_remove_root (heap);
204
205   /* This is free()ed memory! */
206   state->heap_node = NULL;
207
208   /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
209
210   GNUNET_assert (GNUNET_OK ==
211                  GNUNET_CONTAINER_multihashmap_remove (state->hashmap, &state->hash, state));
212
213   GNUNET_free (state);
214 }
215
216 static void
217 hash_redirect_info (GNUNET_HashCode * hash, struct redirect_info *u_i,
218                     size_t addrlen)
219 {
220
221   /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
222    *
223    * build the hash out of the last bytes of the address and the 2 bytes of
224    * the port
225    */
226   memcpy (hash, &u_i->pt, sizeof (u_i->pt));
227   memcpy (((unsigned char *) hash) + 2,
228           u_i->addr + (addrlen - (sizeof (unsigned int) - 2)),
229           (sizeof (unsigned int) - 2));
230   memset (((unsigned char *) hash) + sizeof (unsigned int), 0,
231           sizeof (GNUNET_HashCode) - sizeof (unsigned int));
232 }
233
234 /**
235  * cls is the pointer to a GNUNET_MessageHeader that is
236  * followed by the service-descriptor and the udp-packet that should be sent;
237  */
238 static size_t
239 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
240 {
241   struct GNUNET_MESH_Tunnel **tunnel = cls;
242
243   GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
244   struct GNUNET_MessageHeader *hdr =
245       (struct GNUNET_MessageHeader *) (tunnel + 1);
246   GNUNET_assert (size >= ntohs (hdr->size));
247   memcpy (buf, hdr, ntohs (hdr->size));
248   size = ntohs (hdr->size);
249
250   if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
251   {
252     struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
253     struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
254     struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
255
256     GNUNET_CONTAINER_DLL_remove (head, tail, element);
257
258     GNUNET_MESH_tunnel_set_head (*tunnel, head);
259     GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
260
261     struct GNUNET_MESH_TransmitHandle *th =
262         GNUNET_MESH_notify_transmit_ready (*tunnel,
263                                            GNUNET_NO,
264                                            42,
265                                            GNUNET_TIME_relative_divide
266                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
267                                            (const struct GNUNET_PeerIdentity *)
268                                            NULL, element->len,
269                                            send_udp_to_peer_notify_callback,
270                                            element->cls);
271
272     /* save the handle */
273     GNUNET_MESH_tunnel_set_data (*tunnel, th);
274     GNUNET_free (element);
275   }
276
277   GNUNET_free (cls);
278
279   return size;
280 }
281
282 /**
283  * @brief Handles an UDP-Packet received from the helper.
284  *
285  * @param udp A pointer to the Packet
286  * @param dadr The IP-Destination-address
287  * @param addrlen The length of the address
288  * @param version 4 or 6
289  */
290 static void
291 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
292 {
293   struct redirect_info u_i;
294   struct GNUNET_MESH_Tunnel *tunnel;
295   uint32_t len;
296   struct GNUNET_MessageHeader *msg;
297
298   memset (&u_i, 0, sizeof (struct redirect_info));
299
300   memcpy (&u_i.addr, dadr, addrlen);
301
302   u_i.pt = udp->dpt;
303
304   /* get tunnel and service-descriptor from this */
305   GNUNET_HashCode hash;
306
307   hash_redirect_info (&hash, &u_i, addrlen);
308
309   struct redirect_state *state =
310       GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
311
312   /* Mark this connection as freshly used */
313   GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
314                                      GNUNET_TIME_absolute_get ().abs_value);
315
316   tunnel = state->tunnel;
317
318   if (state->type == SERVICE)
319   {
320     /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
321     if (ntohs (udp->spt) == state->serv->remote_port)
322     {
323       udp->spt = htons (state->serv->my_port);
324     }
325     else
326     {
327       /* otherwise the answer came from a different port (tftp does this)
328        * add this new port to the list of all services, so that the packets
329        * coming back from the client to this new port will be routed correctly
330        */
331       struct redirect_service *serv =
332           GNUNET_malloc (sizeof (struct redirect_service));
333       memcpy (serv, state->serv, sizeof (struct redirect_service));
334       serv->my_port = ntohs (udp->spt);
335       serv->remote_port = ntohs (udp->spt);
336       uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
337
338       memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
339               sizeof (GNUNET_HashCode));
340       *desc = ntohs (udp->spt);
341       GNUNET_assert (GNUNET_OK ==
342                      GNUNET_CONTAINER_multihashmap_put (udp_services,
343                                                         (GNUNET_HashCode *)
344                                                         desc, serv,
345                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
346
347       state->serv = serv;
348     }
349   }
350
351   /* send udp-packet back */
352   len =
353       sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
354       ntohs (udp->len);
355   struct GNUNET_MESH_Tunnel **ctunnel =
356       GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
357   *ctunnel = tunnel;
358   msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
359   msg->size = htons (len);
360   msg->type =
361       htons (state->type ==
362              SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK :
363              GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
364   GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
365
366   if (state->type == SERVICE)
367     memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
368   else
369     memcpy (desc, &state->remote, sizeof (struct remote_addr));
370   void *_udp = desc + 1;
371
372   memcpy (_udp, udp, ntohs (udp->len));
373
374   if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
375   {
376     /* No notify is pending */
377     struct GNUNET_MESH_TransmitHandle *th =
378         GNUNET_MESH_notify_transmit_ready (tunnel,
379                                            GNUNET_NO,
380                                            42,
381                                            GNUNET_TIME_relative_divide
382                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
383                                            (const struct GNUNET_PeerIdentity *)
384                                            NULL, len,
385                                            send_udp_to_peer_notify_callback,
386                                            ctunnel);
387
388     /* save the handle */
389     GNUNET_MESH_tunnel_set_data (tunnel, th);
390   }
391   else
392   {
393     struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
394     struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
395
396     struct tunnel_notify_queue *element =
397         GNUNET_malloc (sizeof (struct tunnel_notify_queue));
398     element->cls = ctunnel;
399     element->len = len;
400
401     GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
402     GNUNET_MESH_tunnel_set_head (tunnel, head);
403     GNUNET_MESH_tunnel_set_tail (tunnel, tail);
404   }
405 }
406
407 /**
408  * @brief Handles a TCP-Packet received from the helper.
409  *
410  * @param tcp A pointer to the Packet
411  * @param dadr The IP-Destination-address
412  * @param addrlen The length of the address
413  * @param version 4 or 6
414  * @param pktlen the length of the packet, including its header
415  */
416 static void
417 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
418                  size_t pktlen)
419 {
420   struct redirect_info u_i;
421   struct GNUNET_MESH_Tunnel *tunnel;
422   uint32_t len;
423   struct GNUNET_MessageHeader *msg;
424
425   memset (&u_i, 0, sizeof (struct redirect_info));
426
427   memcpy (&u_i.addr, dadr, addrlen);
428   u_i.pt = tcp->dpt;
429
430   /* get tunnel and service-descriptor from this */
431   GNUNET_HashCode hash;
432
433   hash_redirect_info (&hash, &u_i, addrlen);
434
435   struct redirect_state *state =
436       GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
437
438   if (state == NULL)
439   {
440     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441                 "No mapping for this connection; hash is %x\n",
442                 *((uint32_t *) & hash));
443     return;
444   }
445
446   /* Mark this connection as freshly used */
447   GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
448                                      GNUNET_TIME_absolute_get ().abs_value);
449
450   tunnel = state->tunnel;
451
452   if (state->type == SERVICE)
453   {
454     /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
455     if (ntohs (tcp->spt) == state->serv->remote_port)
456     {
457       tcp->spt = htons (state->serv->my_port);
458     }
459     else
460     {
461       // This is an illegal packet.
462       return;
463     }
464   }
465
466   /* send tcp-packet back */
467   len =
468       sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
470   struct GNUNET_MESH_Tunnel **ctunnel =
471       GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
472   *ctunnel = tunnel;
473   msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
474   msg->size = htons (len);
475   msg->type =
476       htons (state->type ==
477              SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK :
478              GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
479   GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
480
481   if (state->type == SERVICE)
482     memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
483   else
484     memcpy (desc, &state->remote, sizeof (struct remote_addr));
485   void *_tcp = desc + 1;
486
487   memcpy (_tcp, tcp, pktlen);
488
489   if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
490   {
491     /* No notify is pending */
492     struct GNUNET_MESH_TransmitHandle *th =
493         GNUNET_MESH_notify_transmit_ready (tunnel,
494                                            GNUNET_NO,
495                                            42,
496                                            GNUNET_TIME_relative_divide
497                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
498                                            (const struct GNUNET_PeerIdentity *)
499                                            NULL,
500                                            len,
501                                            send_udp_to_peer_notify_callback,
502                                            ctunnel);
503
504     /* save the handle */
505     GNUNET_MESH_tunnel_set_data (tunnel, th);
506   }
507   else
508   {
509     struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
510     struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
511
512     struct tunnel_notify_queue *element =
513         GNUNET_malloc (sizeof (struct tunnel_notify_queue));
514     element->cls = ctunnel;
515     element->len = len;
516
517     GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
518     GNUNET_MESH_tunnel_set_head (tunnel, head);
519     GNUNET_MESH_tunnel_set_tail (tunnel, tail);
520   }
521 }
522
523
524 /**
525  * Receive packets from the helper-process
526  */
527 static void
528 message_token (void *cls __attribute__ ((unused)), void *client
529                __attribute__ ((unused)),
530                const struct GNUNET_MessageHeader *message)
531 {
532   GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
533
534   struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
535
536   /* ethertype is ipv6 */
537   if (ntohs (pkt_tun->tun.type) == 0x86dd)
538   {
539     struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
540
541     if (IPPROTO_UDP == pkt6->ip6_hdr.nxthdr)
542       udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
543                        (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
544     else if (IPPROTO_TCP == pkt6->ip6_hdr.nxthdr)
545       tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
546                        (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
547                        ntohs (pkt6->ip6_hdr.paylgth));
548   }
549   else if (ntohs (pkt_tun->tun.type) == 0x0800)
550   {
551     struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
552     uint32_t tmp = pkt4->ip_hdr.dadr;
553
554     if (IPPROTO_UDP == pkt4->ip_hdr.proto)
555       udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
556                        (unsigned char *) &tmp, 4);
557     else if (IPPROTO_TCP == pkt4->ip_hdr.proto)
558     {
559       size_t pktlen = ntohs (pkt4->ip_hdr.tot_lngth);
560
561       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
562       pktlen -= 4 * pkt4->ip_hdr.hdr_lngth;
563       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
564       tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
565                        (unsigned char *) &tmp, 4, pktlen);
566     }
567   }
568   else
569   {
570     return;
571   }
572 }
573
574 /**
575  * Reads the configuration servicecfg and populates udp_services
576  *
577  * @param cls unused
578  * @param section name of section in config, equal to hostname
579  */
580 static void
581 read_service_conf (void *cls __attribute__ ((unused)), const char *section)
582 {
583   if ((strlen (section) < 8) ||
584       (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
585     return;
586
587   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n",
588               strlen (section), section, section + (strlen (section) - 8));
589
590   char *cpy;
591   char *redirect;
592   char *hostname;
593   char *hostport;
594   uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
595
596   GNUNET_CRYPTO_hash (section, strlen (section) + 1,
597                       (GNUNET_HashCode *) (desc + 1));
598
599 #define TCP 2
600 #define UDP 1
601
602   int proto = UDP;
603
604   do
605   {
606     if (proto == UDP &&
607         (GNUNET_OK !=
608          GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
609                                                 &cpy)))
610       goto next;
611     else if (proto == TCP &&
612              (GNUNET_OK !=
613               GNUNET_CONFIGURATION_get_value_string (cfg, section,
614                                                      "TCP_REDIRECTS", &cpy)))
615       goto next;
616
617     for (redirect = strtok (cpy, " "); redirect != NULL;
618          redirect = strtok (NULL, " "))
619     {
620       if (NULL == (hostname = strstr (redirect, ":")))
621       {
622         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623                     "Warning: option %s is not formatted correctly!\n",
624                     redirect);
625         continue;
626       }
627       hostname[0] = '\0';
628       hostname++;
629       if (NULL == (hostport = strstr (hostname, ":")))
630       {
631         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
632                     "Warning: option %s is not formatted correctly!\n",
633                     redirect);
634         continue;
635       }
636       hostport[0] = '\0';
637       hostport++;
638
639       int local_port = atoi (redirect);
640
641       if (!((local_port > 0) && (local_port < 65536)))
642         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
643                     "Warning: %s is not a correct port.", redirect);
644
645       *desc = local_port;
646
647       struct redirect_service *serv =
648           GNUNET_malloc (sizeof (struct redirect_service));
649       serv->my_port = local_port;
650
651       if (0 == strcmp ("localhost4", hostname))
652       {
653         serv->version = 4;
654
655         char *ip4addr;
656
657         GNUNET_assert (GNUNET_OK ==
658                        GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
659                                                               "IPV4ADDR",
660                                                               &ip4addr));
661         GNUNET_assert (1 == inet_pton (AF_INET, ip4addr, serv->v4.ip4address));
662         GNUNET_free (ip4addr);
663       }
664       else if (0 == strcmp ("localhost6", hostname))
665       {
666         serv->version = 6;
667
668         char *ip6addr;
669
670         GNUNET_assert (GNUNET_OK ==
671                        GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
672                                                               "IPV6ADDR",
673                                                               &ip6addr));
674         GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, serv->v6.ip6address));
675         GNUNET_free (ip6addr);
676       }
677       else
678       {
679         struct addrinfo* res;
680         struct addrinfo hints;
681
682         hints.ai_flags |= AI_NUMERICHOST;
683
684         int ret = getaddrinfo(hostname, NULL, NULL, &res);
685
686         if (ret != 0)
687           {
688             GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No addresses found for %s!\n", hostname);
689             continue;
690           }
691         else
692           {
693             char buf[256];
694             struct addrinfo* c = res;
695
696             if(c)
697               {
698                 if (c->ai_family == AF_INET)
699                   {
700                     serv->version = 4;
701                     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %s as address for %s\n", inet_ntop(c->ai_family, &((struct sockaddr_in *)(c->ai_addr))->sin_addr, (char*)&buf, 256), hostname);
702                     memcpy(serv->v4.ip4address, &((struct sockaddr_in *)(c->ai_addr))->sin_addr, 4);
703                   }
704                 else if (c->ai_family == AF_INET6)
705                   {
706                     serv->version = 6;
707                     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %s as address for %s\n", inet_ntop(c->ai_family, &((struct sockaddr_in6*)(c->ai_addr))->sin6_addr, (char*)&buf, 256), hostname);
708                     memcpy(serv->v6.ip6address, &((struct sockaddr_in6 *)(c->ai_addr))->sin6_addr, 16);
709                   }
710               }
711             else
712               {
713                 freeaddrinfo(res);
714                 continue;
715               }
716             freeaddrinfo(res);
717           }
718       }
719       serv->remote_port = atoi (hostport);
720       if (UDP == proto)
721         GNUNET_assert (GNUNET_OK ==
722                        GNUNET_CONTAINER_multihashmap_put (udp_services,
723                                                           (GNUNET_HashCode *)
724                                                           desc, serv,
725                                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
726       else
727         GNUNET_assert (GNUNET_OK ==
728                        GNUNET_CONTAINER_multihashmap_put (tcp_services,
729                                                           (GNUNET_HashCode *)
730                                                           desc, serv,
731                                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
732
733     }
734     GNUNET_free (cpy);
735 next:
736     proto = (proto == UDP) ? TCP : UDP;
737   }
738   while (proto != UDP);
739 }
740
741 /**
742  * Start the helper-process
743  *
744  * If cls != NULL it is assumed that this function is called as a result of a dying
745  * helper. cls is then taken as handle to the old helper and is cleaned up.
746  */
747 static void
748 start_helper_and_schedule (void *cls,
749                            const struct GNUNET_SCHEDULER_TaskContext *tc)
750 {
751   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
752     return;
753
754   if (cls != NULL)
755     cleanup_helper (cls);
756   cls = NULL;
757
758   char *ifname;
759   char *ipv6addr;
760   char *ipv6prefix;
761   char *ipv4addr;
762   char *ipv4mask;
763
764   if (GNUNET_SYSERR ==
765       GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname))
766   {
767     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
768                 "No entry 'IFNAME' in configuration!\n");
769     exit (1);
770   }
771
772   if (GNUNET_SYSERR ==
773       GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
774                                              &ipv6addr))
775   {
776     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
777                 "No entry 'IPV6ADDR' in configuration!\n");
778     exit (1);
779   }
780
781   if (GNUNET_SYSERR ==
782       GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
783                                              &ipv6prefix))
784   {
785     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
786                 "No entry 'IPV6PREFIX' in configuration!\n");
787     exit (1);
788   }
789
790   if (GNUNET_SYSERR ==
791       GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
792                                              &ipv4addr))
793   {
794     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
795                 "No entry 'IPV4ADDR' in configuration!\n");
796     exit (1);
797   }
798
799   if (GNUNET_SYSERR ==
800       GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
801                                              &ipv4mask))
802   {
803     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
804                 "No entry 'IPV4MASK' in configuration!\n");
805     exit (1);
806   }
807
808   /* Start the helper
809    * Messages get passed to the function message_token
810    * When the helper dies, this function will be called again with the
811    * helper_handle as cls.
812    */
813   helper_handle =
814       start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
815                     "exit-gnunet", start_helper_and_schedule, message_token,
816                     NULL);
817
818   GNUNET_free (ipv6addr);
819   GNUNET_free (ipv6prefix);
820   GNUNET_free (ipv4addr);
821   GNUNET_free (ipv4mask);
822   GNUNET_free (ifname);
823 }
824
825 static void
826 prepare_ipv4_packet (size_t len, uint16_t pktlen, void *payload,
827                      uint16_t protocol, void *ipaddress, void *tunnel,
828                      struct redirect_state *state, struct ip_pkt *pkt4)
829 {
830   uint32_t tmp, tmp2;
831
832   pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
833   pkt4->shdr.size = htons (len);
834   pkt4->tun.flags = 0;
835   pkt4->tun.type = htons (0x0800);
836
837   memcpy (&pkt4->data, payload, pktlen);
838
839   pkt4->ip_hdr.version = 4;
840   pkt4->ip_hdr.hdr_lngth = 5;
841   pkt4->ip_hdr.diff_serv = 0;
842   pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
843   pkt4->ip_hdr.ident = 0;
844   pkt4->ip_hdr.flags = 0;
845   pkt4->ip_hdr.frag_off = 0;
846   pkt4->ip_hdr.ttl = 255;
847   pkt4->ip_hdr.proto = protocol;
848   pkt4->ip_hdr.chks = 0;        /* Will be calculated later */
849
850   memcpy (&tmp, ipaddress, 4);
851   pkt4->ip_hdr.dadr = tmp;
852
853   /* Generate a new src-address */
854   char *ipv4addr;
855   char *ipv4mask;
856
857   GNUNET_assert (GNUNET_OK ==
858                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
859                                                         &ipv4addr));
860   GNUNET_assert (GNUNET_OK ==
861                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
862                                                         &ipv4mask));
863   inet_pton (AF_INET, ipv4addr, &tmp);
864   inet_pton (AF_INET, ipv4mask, &tmp2);
865   GNUNET_free (ipv4addr);
866   GNUNET_free (ipv4mask);
867
868   /* This should be a noop */
869   tmp = tmp & tmp2;
870
871   tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
872
873   pkt4->ip_hdr.sadr = tmp;
874
875   memcpy (&state->redirect_info.addr, &tmp, 4);
876   if (IPPROTO_UDP == protocol)
877   {
878     struct ip_udp *pkt4_udp = (struct ip_udp *) pkt4;
879
880     state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
881
882     pkt4_udp->udp_hdr.crc = 0;  /* Optional for IPv4 */
883   }
884   else if (IPPROTO_TCP == protocol)
885   {
886     struct ip_tcp *pkt4_tcp = (struct ip_tcp *) pkt4;
887
888     state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
889
890     pkt4_tcp->tcp_hdr.crc = 0;
891     uint32_t sum = 0;
892
893     tmp = pkt4->ip_hdr.sadr;
894     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
895     tmp = pkt4->ip_hdr.dadr;
896     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
897
898     tmp = (protocol << 16) | (0xffff & pktlen);
899
900     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp,
901                 (0xffff & pktlen));
902
903     tmp = htonl (tmp);
904
905     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
906
907     sum =
908         calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr,
909                                    pktlen);
910     pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
911   }
912
913   pkt4->ip_hdr.chks =
914       calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
915 }
916
917 static void
918 prepare_ipv6_packet (size_t len, uint16_t pktlen, void *payload,
919                      uint16_t protocol, void *ipaddress, void *tunnel,
920                      struct redirect_state *state, struct ip6_pkt *pkt6)
921 {
922   uint32_t tmp;
923
924   pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
925   pkt6->shdr.size = htons (len);
926   pkt6->tun.flags = 0;
927
928   pkt6->tun.type = htons (0x86dd);
929
930   memcpy (&pkt6->data, payload, pktlen);
931
932   pkt6->ip6_hdr.version = 6;
933   pkt6->ip6_hdr.nxthdr = protocol;
934   pkt6->ip6_hdr.paylgth = htons (pktlen);
935   pkt6->ip6_hdr.hoplmt = 64;
936
937   memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
938
939   /* Generate a new src-address
940    * This takes as much from the address of the tunnel as fits into
941    * the host-mask*/
942   char *ipv6addr;
943   unsigned long long ipv6prefix;
944
945   GNUNET_assert (GNUNET_OK ==
946                  GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
947                                                         &ipv6addr));
948   GNUNET_assert (GNUNET_OK ==
949                  GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
950                                                         "IPV6PREFIX",
951                                                         &ipv6prefix));
952   GNUNET_assert (ipv6prefix < 127);
953   ipv6prefix = (ipv6prefix + 7) / 8;
954
955   inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
956   GNUNET_free (ipv6addr);
957
958   if (ipv6prefix < (16 - sizeof (void *)))
959     ipv6prefix = 16 - sizeof (void *);
960
961   unsigned int offset = ipv6prefix - (16 - sizeof (void *));
962
963   memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
964           ((char *) &tunnel) + offset, 16 - ipv6prefix);
965
966   /* copy the needed information into the state */
967   memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
968
969   if (IPPROTO_UDP == protocol)
970   {
971     struct ip6_udp *pkt6_udp = (struct ip6_udp *) pkt6;
972
973     state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
974
975     pkt6_udp->udp_hdr.crc = 0;
976     uint32_t sum = 0;
977
978     sum =
979         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr,
980                                    16);
981     sum =
982         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr,
983                                    16);
984     tmp = (htons (pktlen) & 0xffff);
985     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
986     tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
987     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
988
989     sum =
990         calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
991                                    ntohs (pkt6_udp->udp_hdr.len));
992     pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
993   }
994   else if (IPPROTO_TCP == protocol)
995   {
996     struct ip6_tcp *pkt6_tcp = (struct ip6_tcp *) pkt6;
997
998     state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
999
1000     pkt6_tcp->tcp_hdr.crc = 0;
1001     uint32_t sum = 0;
1002
1003     sum =
1004         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1005     sum =
1006         calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1007     tmp = htonl (pktlen);
1008     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1009     tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1010     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1011
1012     sum =
1013         calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
1014                                    ntohs (pkt6->ip6_hdr.paylgth));
1015     pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
1016   }
1017 }
1018
1019 /**
1020  * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
1021  */
1022 static int
1023 receive_tcp_service (void *cls
1024                      __attribute__ ((unused)),
1025                      struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
1026                      __attribute__ ((unused)),
1027                      const struct GNUNET_PeerIdentity *sender
1028                      __attribute__ ((unused)),
1029                      const struct GNUNET_MessageHeader *message,
1030                      const struct GNUNET_TRANSPORT_ATS_Information *atsi
1031                      __attribute__ ((unused)))
1032 {
1033   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
1034   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1035   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1036   uint16_t pkt_len =
1037       ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1038       sizeof (GNUNET_HashCode);
1039
1040   /** Get the configuration from the services-hashmap.
1041    *
1042    * Which service is needed only depends on the service-descriptor and the
1043    * destination-port
1044    */
1045   uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1046
1047   memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
1048   *tcp_desc = ntohs (pkt->dpt);
1049   struct redirect_service *serv =
1050       GNUNET_CONTAINER_multihashmap_get (tcp_services,
1051                                          (GNUNET_HashCode *) tcp_desc);
1052
1053   if (NULL == serv)
1054   {
1055     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n",
1056                 *tcp_desc);
1057     return GNUNET_YES;
1058   }
1059
1060   pkt->dpt = htons (serv->remote_port);
1061
1062   /*
1063    * At this point it would be possible to check against some kind of ACL.
1064    */
1065
1066   char *buf;
1067   size_t len;
1068
1069   /* Prepare the state.
1070    * This will be saved in the hashmap, so that the receiving procedure knows
1071    * through which tunnel this connection has to be routed.
1072    */
1073   struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1074
1075   state->tunnel = tunnel;
1076   state->serv = serv;
1077   state->type = SERVICE;
1078   state->hashmap = tcp_connections;
1079   memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1080
1081   len =
1082       sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1083       sizeof (struct ip6_hdr) + pkt_len;
1084   buf = alloca (len);
1085
1086   memset (buf, 0, len);
1087
1088   switch (serv->version)
1089   {
1090   case 4:
1091     prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP,
1092                          &serv->v4.ip4address, tunnel, state,
1093                          (struct ip_pkt *) buf);
1094     break;
1095   case 6:
1096     prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP,
1097                          &serv->v6.ip6address, tunnel, state,
1098                          (struct ip6_pkt *) buf);
1099
1100     break;
1101   default:
1102     GNUNET_assert (0);
1103     break;
1104   }
1105
1106   hash_redirect_info (&state->hash, &state->redirect_info,
1107                       serv->version == 4 ? 4 : 16);
1108
1109   if (GNUNET_NO ==
1110       GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1111   {
1112     GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1113                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1114
1115     state->heap_node =
1116         GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1117                                       GNUNET_TIME_absolute_get ().abs_value);
1118
1119     if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1120         max_tcp_connections)
1121       GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1122   }
1123   else
1124     GNUNET_free (state);
1125
1126   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1127   return GNUNET_YES;
1128 }
1129
1130 static int
1131 receive_tcp_remote (void *cls
1132                     __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1133                     void **tunnel_ctx
1134                     __attribute__ ((unused)),
1135                     const struct GNUNET_PeerIdentity *sender
1136                     __attribute__ ((unused)),
1137                     const struct GNUNET_MessageHeader *message,
1138                     const struct GNUNET_TRANSPORT_ATS_Information *atsi
1139                     __attribute__ ((unused)))
1140 {
1141   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1142   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1143   struct remote_addr *s = (struct remote_addr *) desc;
1144   char *buf;
1145   size_t len;
1146   uint16_t pkt_len =
1147       ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1148       sizeof (GNUNET_HashCode);
1149
1150   struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1151
1152   state->tunnel = tunnel;
1153   state->type = REMOTE;
1154   state->hashmap = tcp_connections;
1155   memcpy (&state->remote, s, sizeof (struct remote_addr));
1156
1157   len =
1158       sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1159       sizeof (struct ip6_hdr) + pkt_len;
1160   buf = alloca (len);
1161
1162   memset (buf, 0, len);
1163
1164   switch (s->addrlen)
1165   {
1166   case 4:
1167     prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP,
1168                          &s->addr, tunnel, state, (struct ip_pkt *) buf);
1169     break;
1170   case 16:
1171     prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP,
1172                          &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1173     break;
1174   default:
1175     GNUNET_free (state);
1176     return GNUNET_SYSERR;
1177   }
1178
1179   hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1180
1181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n",
1182               *((uint32_t *) & state->hash));
1183
1184   if (GNUNET_NO ==
1185       GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1186   {
1187     GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1188                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1189
1190     state->heap_node =
1191         GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1192                                       GNUNET_TIME_absolute_get ().abs_value);
1193
1194     if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1195         max_tcp_connections)
1196       GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1197   }
1198   else
1199     GNUNET_free (state);
1200
1201   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1202   return GNUNET_YES;
1203
1204 }
1205
1206 static int
1207 receive_udp_remote (void *cls
1208                     __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1209                     void **tunnel_ctx
1210                     __attribute__ ((unused)),
1211                     const struct GNUNET_PeerIdentity *sender
1212                     __attribute__ ((unused)),
1213                     const struct GNUNET_MessageHeader *message,
1214                     const struct GNUNET_TRANSPORT_ATS_Information *atsi
1215                     __attribute__ ((unused)))
1216 {
1217   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1218   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1219   struct remote_addr *s = (struct remote_addr *) desc;
1220   char *buf;
1221   size_t len;
1222
1223   GNUNET_assert (ntohs (pkt->len) ==
1224                  ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1225                  sizeof (GNUNET_HashCode));
1226
1227   /* Prepare the state.
1228    * This will be saved in the hashmap, so that the receiving procedure knows
1229    * through which tunnel this connection has to be routed.
1230    */
1231   struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1232
1233   state->tunnel = tunnel;
1234   state->hashmap = udp_connections;
1235   state->type = REMOTE;
1236   memcpy (&state->remote, s, sizeof (struct remote_addr));
1237
1238   len =
1239       sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1240       sizeof (struct ip6_hdr) + ntohs (pkt->len);
1241   buf = alloca (len);
1242
1243   memset (buf, 0, len);
1244
1245   switch (s->addrlen)
1246   {
1247   case 4:
1248     prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, 
1249                          &s->addr, tunnel, state, (struct ip_pkt *) buf);
1250     break;
1251   case 16:
1252     prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, 
1253                          &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1254     break;
1255   default:
1256     GNUNET_assert (0);
1257     break;
1258   }
1259
1260   hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1261
1262   if (GNUNET_NO ==
1263       GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1264   {
1265     GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1266                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1267
1268     state->heap_node =
1269         GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1270                                       GNUNET_TIME_absolute_get ().abs_value);
1271
1272     if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1273         max_udp_connections)
1274       GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1275   }
1276   else
1277     GNUNET_free (state);
1278
1279   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1280   return GNUNET_YES;
1281 }
1282
1283 /**
1284  * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1285  */
1286 static int
1287 receive_udp_service (void *cls
1288                      __attribute__ ((unused)),
1289                      struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
1290                      __attribute__ ((unused)),
1291                      const struct GNUNET_PeerIdentity *sender
1292                      __attribute__ ((unused)),
1293                      const struct GNUNET_MessageHeader *message,
1294                      const struct GNUNET_TRANSPORT_ATS_Information *atsi
1295                      __attribute__ ((unused)))
1296 {
1297   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1298   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1299
1300   GNUNET_assert (ntohs (pkt->len) ==
1301                  ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1302                  sizeof (GNUNET_HashCode));
1303
1304   /* Get the configuration from the hashmap */
1305   uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1306
1307   memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1308   *udp_desc = ntohs (pkt->dpt);
1309   struct redirect_service *serv =
1310       GNUNET_CONTAINER_multihashmap_get (udp_services,
1311                                          (GNUNET_HashCode *) udp_desc);
1312
1313   if (NULL == serv)
1314   {
1315     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n",
1316                 *udp_desc);
1317     return GNUNET_YES;
1318   }
1319
1320   pkt->dpt = htons (serv->remote_port);
1321
1322   /*
1323    * At this point it would be possible to check against some kind of ACL.
1324    */
1325
1326   char *buf;
1327   size_t len;
1328
1329   /* Prepare the state.
1330    * This will be saved in the hashmap, so that the receiving procedure knows
1331    * through which tunnel this connection has to be routed.
1332    */
1333   struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1334
1335   state->tunnel = tunnel;
1336   state->serv = serv;
1337   state->type = SERVICE;
1338   state->hashmap = udp_connections;
1339   memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1340
1341   len =
1342       sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1343       sizeof (struct ip6_hdr) + ntohs (pkt->len);
1344   buf = alloca (len);
1345
1346   memset (buf, 0, len);
1347
1348   switch (serv->version)
1349   {
1350   case 4:
1351     prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
1352                          &serv->v4.ip4address, tunnel, state,
1353                          (struct ip_pkt *) buf);
1354     break;
1355   case 6:
1356     prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
1357                          &serv->v6.ip6address, tunnel, state,
1358                          (struct ip6_pkt *) buf);
1359
1360     break;
1361   default:
1362     GNUNET_assert (0);
1363     break;
1364   }
1365
1366   hash_redirect_info (&state->hash, &state->redirect_info,
1367                       serv->version == 4 ? 4 : 16);
1368
1369   if (GNUNET_NO ==
1370       GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1371   {
1372     GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1373                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1374
1375     state->heap_node =
1376         GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1377                                       GNUNET_TIME_absolute_get ().abs_value);
1378
1379     if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1380         max_udp_connections)
1381       GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1382   }
1383   else
1384     GNUNET_free (state);
1385
1386   (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1387   return GNUNET_YES;
1388 }
1389
1390 static void
1391 connect_to_mesh ()
1392 {
1393   int udp, tcp;
1394   int handler_idx, app_idx;
1395
1396   udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP");
1397   tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP");
1398
1399   static struct GNUNET_MESH_MessageHandler handlers[] = {
1400     {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
1401     {receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP, 0},
1402     {NULL, 0, 0},
1403     {NULL, 0, 0},
1404     {NULL, 0, 0}
1405   };
1406
1407   static GNUNET_MESH_ApplicationType apptypes[] = {
1408     GNUNET_APPLICATION_TYPE_END,
1409     GNUNET_APPLICATION_TYPE_END,
1410     GNUNET_APPLICATION_TYPE_END
1411   };
1412
1413   app_idx = 0;
1414   handler_idx = 2;
1415
1416   if (GNUNET_YES == udp)
1417   {
1418     handlers[handler_idx].callback = receive_udp_remote;
1419     handlers[handler_idx].expected_size = 0;
1420     handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP;
1421     apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1422     handler_idx++;
1423     app_idx++;
1424   }
1425
1426   if (GNUNET_YES == tcp)
1427   {
1428     handlers[handler_idx].callback = receive_tcp_remote;
1429     handlers[handler_idx].expected_size = 0;
1430     handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP;
1431     apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1432     handler_idx++;
1433     app_idx++;
1434   }
1435
1436   mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1437 }
1438
1439
1440 /**
1441  * @brief Main function that will be run by the scheduler.
1442  *
1443  * @param cls closure
1444  * @param args remaining command-line arguments
1445  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1446  * @param cfg_ configuration
1447  */
1448 static void
1449 run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfile
1450      __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1451 {
1452   cfg = cfg_;
1453
1454   connect_to_mesh ();
1455
1456   udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1457   udp_connections_heap =
1458       GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1459   tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1460   tcp_connections_heap =
1461       GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1462   udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1463   tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1464
1465   if (GNUNET_OK !=
1466       GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1467                                              &max_udp_connections))
1468     max_udp_connections = 1024;
1469   if (GNUNET_OK !=
1470       GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1471                                              &max_tcp_connections))
1472     max_tcp_connections = 256;
1473   GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1474   GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1475   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1476 }
1477
1478
1479 /**
1480  * The main function 
1481  *
1482  * @param argc number of arguments from the command line
1483  * @param argv command line arguments
1484  * @return 0 ok, 1 on error
1485  */
1486 int
1487 main (int argc, char *const *argv)
1488 {
1489   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1490     GNUNET_GETOPT_OPTION_END
1491   };
1492
1493   return (GNUNET_OK ==
1494           GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit", 
1495                               gettext_noop ("Daemon to run to provide an IP exit node for the VPN"),
1496                               options, &run, NULL)) ? ret : 1;
1497 }
1498
1499
1500 /* end of gnunet-daemon-exit.c */