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