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