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