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