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