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