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