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