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