2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
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.
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.
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.
22 * @file vpn/gnunet-daemon-exit.c
24 * @author Philipp Toelke
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>
35 #include "gnunet-vpn-packet.h"
36 #include "gnunet-helper-vpn-api.h"
37 #include "gnunet-vpn-checksum.h"
39 GNUNET_SCHEDULER_TaskIdentifier shs_task;
42 * The handle to the configuration used throughout the process
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
47 * The handle to the helper
49 struct GNUNET_VPN_HELPER_Handle *helper_handle;
59 static struct GNUNET_MESH_Handle *mesh_handle;
62 * This hashmaps contains the mapping from peer, service-descriptor,
63 * source-port and destination-port to a struct redirect_state
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;
71 * If there are at least this many udp-Connections, old ones will be removed
73 static long long unsigned int max_udp_connections = 200;
76 * If there are at least this many tcp-Connections, old ones will be removed
78 static long long unsigned int max_tcp_connections = 200;
83 unsigned char addr[16];
88 * This struct is saved into the services-hashmap
90 struct redirect_service
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 */
120 * The source-port of this connection
126 * This struct is saved into {tcp,udp}_connections;
128 struct redirect_state
130 struct GNUNET_MESH_Tunnel *tunnel;
131 GNUNET_HashCode desc;
132 struct redirect_service *serv;
133 struct remote_addr remote;
135 struct GNUNET_CONTAINER_HeapNode *heap_node;
136 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
137 GNUNET_HashCode hash;
140 { SERVICE, REMOTE } type;
143 * The source-address and -port of this connection
145 struct redirect_info redirect_info;
149 * This hashmaps saves interesting things about the configured services
151 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
152 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
154 struct tunnel_notify_queue
156 struct tunnel_notify_queue *next;
157 struct tunnel_notify_queue *prev;
163 * Function that frees everything from a hashmap
166 free_iterate (void *cls __attribute__ ((unused)), const GNUNET_HashCode * hash
167 __attribute__ ((unused)), void *value)
174 * Function scheduled as very last function, cleans up after us
178 __attribute__ ((unused)),
179 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
181 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
183 GNUNET_CONTAINER_multihashmap_iterate (udp_connections, free_iterate, NULL);
185 GNUNET_CONTAINER_multihashmap_iterate (tcp_connections, free_iterate, NULL);
187 if (mesh_handle != NULL)
189 GNUNET_MESH_disconnect (mesh_handle);
195 collect_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
197 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
201 struct GNUNET_CONTAINER_Heap *heap = cls;
203 struct redirect_state *state = GNUNET_CONTAINER_heap_remove_root (heap);
205 /* This is free()ed memory! */
206 state->heap_node = NULL;
208 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
210 GNUNET_assert (GNUNET_OK ==
211 GNUNET_CONTAINER_multihashmap_remove (state->hashmap, &state->hash, state));
217 hash_redirect_info (GNUNET_HashCode * hash, struct redirect_info *u_i,
221 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
223 * build the hash out of the last bytes of the address and the 2 bytes of
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));
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;
239 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
241 struct GNUNET_MESH_Tunnel **tunnel = cls;
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);
250 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
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);
256 GNUNET_CONTAINER_DLL_remove (head, tail, element);
258 GNUNET_MESH_tunnel_set_head (*tunnel, head);
259 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
261 struct GNUNET_MESH_TransmitHandle *th =
262 GNUNET_MESH_notify_transmit_ready (*tunnel,
265 GNUNET_TIME_relative_divide
266 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
267 (const struct GNUNET_PeerIdentity *)
269 send_udp_to_peer_notify_callback,
272 /* save the handle */
273 GNUNET_MESH_tunnel_set_data (*tunnel, th);
274 GNUNET_free (element);
283 * @brief Handles an UDP-Packet received from the helper.
285 * @param udp A pointer to the Packet
286 * @param dadr The IP-Destination-address
287 * @param addrlen The length of the address
290 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
292 struct redirect_info u_i;
293 struct GNUNET_MESH_Tunnel *tunnel;
295 struct GNUNET_MessageHeader *msg;
297 memset (&u_i, 0, sizeof (struct redirect_info));
299 memcpy (&u_i.addr, dadr, addrlen);
303 /* get tunnel and service-descriptor from this */
304 GNUNET_HashCode hash;
306 hash_redirect_info (&hash, &u_i, addrlen);
308 struct redirect_state *state =
309 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
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);
315 tunnel = state->tunnel;
317 if (state->type == SERVICE)
319 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
320 if (ntohs (udp->spt) == state->serv->remote_port)
322 udp->spt = htons (state->serv->my_port);
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
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);
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,
344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
350 /* send udp-packet back */
352 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
354 struct GNUNET_MESH_Tunnel **ctunnel =
355 GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
357 msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
358 msg->size = htons (len);
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);
365 if (state->type == SERVICE)
366 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
368 memcpy (desc, &state->remote, sizeof (struct remote_addr));
369 void *_udp = desc + 1;
371 memcpy (_udp, udp, ntohs (udp->len));
373 if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
375 /* No notify is pending */
376 struct GNUNET_MESH_TransmitHandle *th =
377 GNUNET_MESH_notify_transmit_ready (tunnel,
380 GNUNET_TIME_relative_divide
381 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
382 (const struct GNUNET_PeerIdentity *)
384 send_udp_to_peer_notify_callback,
387 /* save the handle */
388 GNUNET_MESH_tunnel_set_data (tunnel, th);
392 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
393 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
395 struct tunnel_notify_queue *element =
396 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
397 element->cls = ctunnel;
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);
407 * @brief Handles a TCP-Packet received from the helper.
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
415 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
418 struct redirect_info u_i;
419 struct GNUNET_MESH_Tunnel *tunnel;
421 struct GNUNET_MessageHeader *msg;
423 memset (&u_i, 0, sizeof (struct redirect_info));
425 memcpy (&u_i.addr, dadr, addrlen);
428 /* get tunnel and service-descriptor from this */
429 GNUNET_HashCode hash;
431 hash_redirect_info (&hash, &u_i, addrlen);
433 struct redirect_state *state =
434 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "No mapping for this connection; hash is %x\n",
440 *((uint32_t *) & hash));
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);
448 tunnel = state->tunnel;
450 if (state->type == SERVICE)
452 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
453 if (ntohs (tcp->spt) == state->serv->remote_port)
455 tcp->spt = htons (state->serv->my_port);
459 // This is an illegal packet.
464 /* send tcp-packet back */
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);
471 msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
472 msg->size = htons (len);
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);
479 if (state->type == SERVICE)
480 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
482 memcpy (desc, &state->remote, sizeof (struct remote_addr));
483 void *_tcp = desc + 1;
485 memcpy (_tcp, tcp, pktlen);
487 if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
489 /* No notify is pending */
490 struct GNUNET_MESH_TransmitHandle *th =
491 GNUNET_MESH_notify_transmit_ready (tunnel,
494 GNUNET_TIME_relative_divide
495 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
496 (const struct GNUNET_PeerIdentity *)
499 send_udp_to_peer_notify_callback,
502 /* save the handle */
503 GNUNET_MESH_tunnel_set_data (tunnel, th);
507 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
508 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
510 struct tunnel_notify_queue *element =
511 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
512 element->cls = ctunnel;
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);
523 * Receive packets from the helper-process
526 message_token (void *cls __attribute__ ((unused)), void *client
527 __attribute__ ((unused)),
528 const struct GNUNET_MessageHeader *message)
530 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
532 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
534 /* ethertype is ipv6 */
535 if (ntohs (pkt_tun->tun.type) == 0x86dd)
537 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
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));
547 else if (ntohs (pkt_tun->tun.type) == 0x0800)
549 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
550 uint32_t tmp = pkt4->ip_hdr.dadr;
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)
557 size_t pktlen = ntohs (pkt4->ip_hdr.tot_lngth);
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);
573 * Reads the configuration servicecfg and populates udp_services
576 * @param section name of section in config, equal to hostname
579 read_service_conf (void *cls __attribute__ ((unused)), const char *section)
581 if ((strlen (section) < 8) ||
582 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n",
586 strlen (section), section, section + (strlen (section) - 8));
592 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
594 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
595 (GNUNET_HashCode *) (desc + 1));
606 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
609 else if (proto == TCP &&
611 GNUNET_CONFIGURATION_get_value_string (cfg, section,
612 "TCP_REDIRECTS", &cpy)))
615 for (redirect = strtok (cpy, " "); redirect != NULL;
616 redirect = strtok (NULL, " "))
618 if (NULL == (hostname = strstr (redirect, ":")))
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 "Warning: option %s is not formatted correctly!\n",
627 if (NULL == (hostport = strstr (hostname, ":")))
629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
630 "Warning: option %s is not formatted correctly!\n",
637 int local_port = atoi (redirect);
639 if (!((local_port > 0) && (local_port < 65536)))
640 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
641 "Warning: %s is not a correct port.", redirect);
645 struct redirect_service *serv =
646 GNUNET_malloc (sizeof (struct redirect_service));
647 serv->my_port = local_port;
649 if (0 == strcmp ("localhost4", hostname))
655 GNUNET_assert (GNUNET_OK ==
656 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
659 GNUNET_assert (1 == inet_pton (AF_INET, ip4addr, serv->v4.ip4address));
660 GNUNET_free (ip4addr);
662 else if (0 == strcmp ("localhost6", hostname))
668 GNUNET_assert (GNUNET_OK ==
669 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
672 GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, serv->v6.ip6address));
673 GNUNET_free (ip6addr);
677 struct addrinfo* res;
679 int ret = getaddrinfo(hostname, NULL, NULL, &res);
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No addresses found for %s!\n", hostname);
690 struct addrinfo* c = res;
694 if (c->ai_family == AF_INET)
697 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);
698 memcpy(serv->v4.ip4address, &((struct sockaddr_in *)(c->ai_addr))->sin_addr, 4);
700 else if (c->ai_family == AF_INET6)
703 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);
704 memcpy(serv->v6.ip6address, &((struct sockaddr_in6 *)(c->ai_addr))->sin6_addr, 16);
716 serv->remote_port = atoi (hostport);
718 GNUNET_assert (GNUNET_OK ==
719 GNUNET_CONTAINER_multihashmap_put (udp_services,
722 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
724 GNUNET_assert (GNUNET_OK ==
725 GNUNET_CONTAINER_multihashmap_put (tcp_services,
728 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
733 proto = (proto == UDP) ? TCP : UDP;
735 while (proto != UDP);
739 * Start the helper-process
741 * If cls != NULL it is assumed that this function is called as a result of a dying
742 * helper. cls is then taken as handle to the old helper and is cleaned up.
745 start_helper_and_schedule (void *cls,
746 const struct GNUNET_SCHEDULER_TaskContext *tc)
748 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
752 cleanup_helper (cls);
762 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname))
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 "No entry 'IFNAME' in configuration!\n");
770 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
774 "No entry 'IPV6ADDR' in configuration!\n");
779 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
782 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
783 "No entry 'IPV6PREFIX' in configuration!\n");
788 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
792 "No entry 'IPV4ADDR' in configuration!\n");
797 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
801 "No entry 'IPV4MASK' in configuration!\n");
806 * Messages get passed to the function message_token
807 * When the helper dies, this function will be called again with the
808 * helper_handle as cls.
811 start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
812 "exit-gnunet", start_helper_and_schedule, message_token,
815 GNUNET_free (ipv6addr);
816 GNUNET_free (ipv6prefix);
817 GNUNET_free (ipv4addr);
818 GNUNET_free (ipv4mask);
819 GNUNET_free (ifname);
823 prepare_ipv4_packet (size_t len, uint16_t pktlen, void *payload,
824 uint16_t protocol, void *ipaddress, void *tunnel,
825 struct redirect_state *state, struct ip_pkt *pkt4)
829 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
830 pkt4->shdr.size = htons (len);
832 pkt4->tun.type = htons (0x0800);
834 memcpy (&pkt4->data, payload, pktlen);
836 pkt4->ip_hdr.version = 4;
837 pkt4->ip_hdr.hdr_lngth = 5;
838 pkt4->ip_hdr.diff_serv = 0;
839 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
840 pkt4->ip_hdr.ident = 0;
841 pkt4->ip_hdr.flags = 0;
842 pkt4->ip_hdr.frag_off = 0;
843 pkt4->ip_hdr.ttl = 255;
844 pkt4->ip_hdr.proto = protocol;
845 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
847 memcpy (&tmp, ipaddress, 4);
848 pkt4->ip_hdr.dadr = tmp;
850 /* Generate a new src-address */
854 GNUNET_assert (GNUNET_OK ==
855 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
857 GNUNET_assert (GNUNET_OK ==
858 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
860 inet_pton (AF_INET, ipv4addr, &tmp);
861 inet_pton (AF_INET, ipv4mask, &tmp2);
862 GNUNET_free (ipv4addr);
863 GNUNET_free (ipv4mask);
865 /* This should be a noop */
868 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
870 pkt4->ip_hdr.sadr = tmp;
872 memcpy (&state->redirect_info.addr, &tmp, 4);
873 if (IPPROTO_UDP == protocol)
875 struct ip_udp *pkt4_udp = (struct ip_udp *) pkt4;
877 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
879 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
881 else if (IPPROTO_TCP == protocol)
883 struct ip_tcp *pkt4_tcp = (struct ip_tcp *) pkt4;
885 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
887 pkt4_tcp->tcp_hdr.crc = 0;
890 tmp = pkt4->ip_hdr.sadr;
891 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
892 tmp = pkt4->ip_hdr.dadr;
893 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
895 tmp = (protocol << 16) | (0xffff & pktlen);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp,
902 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
905 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr,
907 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
911 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
915 prepare_ipv6_packet (size_t len, uint16_t pktlen, void *payload,
916 uint16_t protocol, void *ipaddress, void *tunnel,
917 struct redirect_state *state, struct ip6_pkt *pkt6)
921 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
922 pkt6->shdr.size = htons (len);
925 pkt6->tun.type = htons (0x86dd);
927 memcpy (&pkt6->data, payload, pktlen);
929 pkt6->ip6_hdr.version = 6;
930 pkt6->ip6_hdr.nxthdr = protocol;
931 pkt6->ip6_hdr.paylgth = htons (pktlen);
932 pkt6->ip6_hdr.hoplmt = 64;
934 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
936 /* Generate a new src-address
937 * This takes as much from the address of the tunnel as fits into
940 unsigned long long ipv6prefix;
942 GNUNET_assert (GNUNET_OK ==
943 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
945 GNUNET_assert (GNUNET_OK ==
946 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
949 GNUNET_assert (ipv6prefix < 127);
950 ipv6prefix = (ipv6prefix + 7) / 8;
952 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
953 GNUNET_free (ipv6addr);
955 if (ipv6prefix < (16 - sizeof (void *)))
956 ipv6prefix = 16 - sizeof (void *);
958 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
960 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
961 ((char *) &tunnel) + offset, 16 - ipv6prefix);
963 /* copy the needed information into the state */
964 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
966 if (IPPROTO_UDP == protocol)
968 struct ip6_udp *pkt6_udp = (struct ip6_udp *) pkt6;
970 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
972 pkt6_udp->udp_hdr.crc = 0;
976 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr,
979 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr,
981 tmp = (htons (pktlen) & 0xffff);
982 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
983 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
984 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
987 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
988 ntohs (pkt6_udp->udp_hdr.len));
989 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
991 else if (IPPROTO_TCP == protocol)
993 struct ip6_tcp *pkt6_tcp = (struct ip6_tcp *) pkt6;
995 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
997 pkt6_tcp->tcp_hdr.crc = 0;
1001 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1003 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1004 tmp = htonl (pktlen);
1005 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1006 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1007 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1010 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
1011 ntohs (pkt6->ip6_hdr.paylgth));
1012 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
1017 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
1020 receive_tcp_service (void *cls
1021 __attribute__ ((unused)),
1022 struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
1023 __attribute__ ((unused)),
1024 const struct GNUNET_PeerIdentity *sender
1025 __attribute__ ((unused)),
1026 const struct GNUNET_MessageHeader *message,
1027 const struct GNUNET_ATS_Information *atsi
1028 __attribute__ ((unused)))
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
1031 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1032 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1034 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1035 sizeof (GNUNET_HashCode);
1037 /** Get the configuration from the services-hashmap.
1039 * Which service is needed only depends on the service-descriptor and the
1042 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1044 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
1045 *tcp_desc = ntohs (pkt->dpt);
1046 struct redirect_service *serv =
1047 GNUNET_CONTAINER_multihashmap_get (tcp_services,
1048 (GNUNET_HashCode *) tcp_desc);
1052 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n",
1057 pkt->dpt = htons (serv->remote_port);
1060 * At this point it would be possible to check against some kind of ACL.
1066 /* Prepare the state.
1067 * This will be saved in the hashmap, so that the receiving procedure knows
1068 * through which tunnel this connection has to be routed.
1070 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1072 state->tunnel = tunnel;
1074 state->type = SERVICE;
1075 state->hashmap = tcp_connections;
1076 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1079 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1080 sizeof (struct ip6_hdr) + pkt_len;
1083 memset (buf, 0, len);
1085 switch (serv->version)
1088 prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP,
1089 &serv->v4.ip4address, tunnel, state,
1090 (struct ip_pkt *) buf);
1093 prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP,
1094 &serv->v6.ip6address, tunnel, state,
1095 (struct ip6_pkt *) buf);
1103 hash_redirect_info (&state->hash, &state->redirect_info,
1104 serv->version == 4 ? 4 : 16);
1107 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1109 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1110 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1113 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1114 GNUNET_TIME_absolute_get ().abs_value);
1116 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1117 max_tcp_connections)
1118 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1121 GNUNET_free (state);
1123 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1128 receive_tcp_remote (void *cls
1129 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1131 __attribute__ ((unused)),
1132 const struct GNUNET_PeerIdentity *sender
1133 __attribute__ ((unused)),
1134 const struct GNUNET_MessageHeader *message,
1135 const struct GNUNET_ATS_Information *atsi
1136 __attribute__ ((unused)))
1138 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1139 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1140 struct remote_addr *s = (struct remote_addr *) desc;
1144 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1145 sizeof (GNUNET_HashCode);
1147 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1149 state->tunnel = tunnel;
1150 state->type = REMOTE;
1151 state->hashmap = tcp_connections;
1152 memcpy (&state->remote, s, sizeof (struct remote_addr));
1155 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1156 sizeof (struct ip6_hdr) + pkt_len;
1159 memset (buf, 0, len);
1164 prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP,
1165 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1168 prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP,
1169 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1172 GNUNET_free (state);
1173 return GNUNET_SYSERR;
1176 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n",
1179 *((uint32_t *) & state->hash));
1182 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1184 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1185 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1188 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1189 GNUNET_TIME_absolute_get ().abs_value);
1191 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1192 max_tcp_connections)
1193 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1196 GNUNET_free (state);
1198 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1204 receive_udp_remote (void *cls
1205 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1207 __attribute__ ((unused)),
1208 const struct GNUNET_PeerIdentity *sender
1209 __attribute__ ((unused)),
1210 const struct GNUNET_MessageHeader *message,
1211 const struct GNUNET_ATS_Information *atsi
1212 __attribute__ ((unused)))
1214 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1215 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1216 struct remote_addr *s = (struct remote_addr *) desc;
1220 GNUNET_assert (ntohs (pkt->len) ==
1221 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1222 sizeof (GNUNET_HashCode));
1224 /* Prepare the state.
1225 * This will be saved in the hashmap, so that the receiving procedure knows
1226 * through which tunnel this connection has to be routed.
1228 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1230 state->tunnel = tunnel;
1231 state->hashmap = udp_connections;
1232 state->type = REMOTE;
1233 memcpy (&state->remote, s, sizeof (struct remote_addr));
1236 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1237 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1240 memset (buf, 0, len);
1245 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
1246 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1249 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
1250 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1257 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1260 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1262 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1263 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1266 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1267 GNUNET_TIME_absolute_get ().abs_value);
1269 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1270 max_udp_connections)
1271 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1274 GNUNET_free (state);
1276 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1281 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1284 receive_udp_service (void *cls
1285 __attribute__ ((unused)),
1286 struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
1287 __attribute__ ((unused)),
1288 const struct GNUNET_PeerIdentity *sender
1289 __attribute__ ((unused)),
1290 const struct GNUNET_MessageHeader *message,
1291 const struct GNUNET_ATS_Information *atsi
1292 __attribute__ ((unused)))
1294 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1295 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1297 GNUNET_assert (ntohs (pkt->len) ==
1298 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1299 sizeof (GNUNET_HashCode));
1301 /* Get the configuration from the hashmap */
1302 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1304 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1305 *udp_desc = ntohs (pkt->dpt);
1306 struct redirect_service *serv =
1307 GNUNET_CONTAINER_multihashmap_get (udp_services,
1308 (GNUNET_HashCode *) udp_desc);
1312 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n",
1317 pkt->dpt = htons (serv->remote_port);
1320 * At this point it would be possible to check against some kind of ACL.
1326 /* Prepare the state.
1327 * This will be saved in the hashmap, so that the receiving procedure knows
1328 * through which tunnel this connection has to be routed.
1330 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1332 state->tunnel = tunnel;
1334 state->type = SERVICE;
1335 state->hashmap = udp_connections;
1336 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1339 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1340 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1343 memset (buf, 0, len);
1345 switch (serv->version)
1348 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
1349 &serv->v4.ip4address, tunnel, state,
1350 (struct ip_pkt *) buf);
1353 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
1354 &serv->v6.ip6address, tunnel, state,
1355 (struct ip6_pkt *) buf);
1363 hash_redirect_info (&state->hash, &state->redirect_info,
1364 serv->version == 4 ? 4 : 16);
1367 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1369 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1373 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1374 GNUNET_TIME_absolute_get ().abs_value);
1376 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1377 max_udp_connections)
1378 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1381 GNUNET_free (state);
1383 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1391 int handler_idx, app_idx;
1393 udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP");
1394 tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP");
1396 static struct GNUNET_MESH_MessageHandler handlers[] = {
1397 {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
1398 {receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP, 0},
1404 static GNUNET_MESH_ApplicationType apptypes[] = {
1405 GNUNET_APPLICATION_TYPE_END,
1406 GNUNET_APPLICATION_TYPE_END,
1407 GNUNET_APPLICATION_TYPE_END
1413 if (GNUNET_YES == udp)
1415 handlers[handler_idx].callback = receive_udp_remote;
1416 handlers[handler_idx].expected_size = 0;
1417 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP;
1418 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1423 if (GNUNET_YES == tcp)
1425 handlers[handler_idx].callback = receive_tcp_remote;
1426 handlers[handler_idx].expected_size = 0;
1427 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP;
1428 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1433 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1438 * @brief Main function that will be run by the scheduler.
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
1446 run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfile
1447 __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1453 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1454 udp_connections_heap =
1455 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1456 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1457 tcp_connections_heap =
1458 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1459 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1460 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1463 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1464 &max_udp_connections))
1465 max_udp_connections = 1024;
1467 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1468 &max_tcp_connections))
1469 max_tcp_connections = 256;
1470 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1471 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1472 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1479 * @param argc number of arguments from the command line
1480 * @param argv command line arguments
1481 * @return 0 ok, 1 on error
1484 main (int argc, char *const *argv)
1486 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1487 GNUNET_GETOPT_OPTION_END
1490 return (GNUNET_OK ==
1491 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
1492 gettext_noop ("Daemon to run to provide an IP exit node for the VPN"),
1493 options, &run, NULL)) ? ret : 1;
1497 /* end of gnunet-daemon-exit.c */