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