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