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