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_CONTAINER_multihashmap_remove (state->hashmap, &state->hash, state);
216 hash_redirect_info (GNUNET_HashCode * hash, struct redirect_info *u_i,
220 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
222 * build the hash out of the last bytes of the address and the 2 bytes of
225 memcpy (hash, &u_i->pt, sizeof (u_i->pt));
226 memcpy (((unsigned char *) hash) + 2,
227 u_i->addr + (addrlen - (sizeof (unsigned int) - 2)),
228 (sizeof (unsigned int) - 2));
229 memset (((unsigned char *) hash) + sizeof (unsigned int), 0,
230 sizeof (GNUNET_HashCode) - sizeof (unsigned int));
234 * cls is the pointer to a GNUNET_MessageHeader that is
235 * followed by the service-descriptor and the udp-packet that should be sent;
238 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
240 struct GNUNET_MESH_Tunnel **tunnel = cls;
242 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
243 struct GNUNET_MessageHeader *hdr =
244 (struct GNUNET_MessageHeader *) (tunnel + 1);
245 GNUNET_assert (size >= ntohs (hdr->size));
246 memcpy (buf, hdr, ntohs (hdr->size));
247 size = ntohs (hdr->size);
249 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
251 struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
252 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
253 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
255 GNUNET_CONTAINER_DLL_remove (head, tail, element);
257 GNUNET_MESH_tunnel_set_head (*tunnel, head);
258 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
260 struct GNUNET_MESH_TransmitHandle *th =
261 GNUNET_MESH_notify_transmit_ready (*tunnel,
264 GNUNET_TIME_relative_divide
265 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
266 (const struct GNUNET_PeerIdentity *)
268 send_udp_to_peer_notify_callback,
271 /* save the handle */
272 GNUNET_MESH_tunnel_set_data (*tunnel, th);
273 GNUNET_free (element);
282 * @brief Handles an UDP-Packet received from the helper.
284 * @param udp A pointer to the Packet
285 * @param dadr The IP-Destination-address
286 * @param addrlen The length of the address
287 * @param version 4 or 6
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 version 4 or 6
413 * @param pktlen the length of the packet, including its header
416 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
419 struct redirect_info u_i;
420 struct GNUNET_MESH_Tunnel *tunnel;
422 struct GNUNET_MessageHeader *msg;
424 memset (&u_i, 0, sizeof (struct redirect_info));
426 memcpy (&u_i.addr, dadr, addrlen);
429 /* get tunnel and service-descriptor from this */
430 GNUNET_HashCode hash;
432 hash_redirect_info (&hash, &u_i, addrlen);
434 struct redirect_state *state =
435 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "No mapping for this connection; hash is %x\n",
441 *((uint32_t *) & hash));
445 /* Mark this connection as freshly used */
446 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
447 GNUNET_TIME_absolute_get ().abs_value);
449 tunnel = state->tunnel;
451 if (state->type == SERVICE)
453 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
454 if (ntohs (tcp->spt) == state->serv->remote_port)
456 tcp->spt = htons (state->serv->my_port);
460 // This is an illegal packet.
465 /* send tcp-packet back */
467 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
469 struct GNUNET_MESH_Tunnel **ctunnel =
470 GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
472 msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
473 msg->size = htons (len);
475 htons (state->type ==
476 SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK :
477 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
478 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
480 if (state->type == SERVICE)
481 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
483 memcpy (desc, &state->remote, sizeof (struct remote_addr));
484 void *_tcp = desc + 1;
486 memcpy (_tcp, tcp, pktlen);
488 if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
490 /* No notify is pending */
491 struct GNUNET_MESH_TransmitHandle *th =
492 GNUNET_MESH_notify_transmit_ready (tunnel,
495 GNUNET_TIME_relative_divide
496 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
497 (const struct GNUNET_PeerIdentity *)
500 send_udp_to_peer_notify_callback,
503 /* save the handle */
504 GNUNET_MESH_tunnel_set_data (tunnel, th);
508 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
509 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
511 struct tunnel_notify_queue *element =
512 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
513 element->cls = ctunnel;
516 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
522 * Receive packets from the helper-process
525 message_token (void *cls __attribute__ ((unused)), void *client
526 __attribute__ ((unused)),
527 const struct GNUNET_MessageHeader *message)
529 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
531 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
533 /* ethertype is ipv6 */
534 if (ntohs (pkt_tun->tun.type) == 0x86dd)
536 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
538 if (0x11 == pkt6->ip6_hdr.nxthdr)
539 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
540 (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
541 else if (0x06 == pkt6->ip6_hdr.nxthdr)
542 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
543 (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
544 ntohs (pkt6->ip6_hdr.paylgth));
546 else if (ntohs (pkt_tun->tun.type) == 0x0800)
548 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
549 uint32_t tmp = pkt4->ip_hdr.dadr;
551 if (0x11 == pkt4->ip_hdr.proto)
552 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
553 (unsigned char *) &tmp, 4);
554 else if (0x06 == pkt4->ip_hdr.proto)
556 size_t pktlen = ntohs (pkt4->ip_hdr.tot_lngth);
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
559 pktlen -= 4 * pkt4->ip_hdr.hdr_lngth;
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
561 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
562 (unsigned char *) &tmp, 4, pktlen);
572 * Reads the configuration servicecfg and populates udp_services
575 * @param section name of section in config, equal to hostname
578 read_service_conf (void *cls __attribute__ ((unused)), const char *section)
580 if ((strlen (section) < 8) ||
581 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n",
585 strlen (section), section, section + (strlen (section) - 8));
591 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
593 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
594 (GNUNET_HashCode *) (desc + 1));
605 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
608 else if (proto == TCP &&
610 GNUNET_CONFIGURATION_get_value_string (cfg, section,
611 "TCP_REDIRECTS", &cpy)))
614 for (redirect = strtok (cpy, " "); redirect != NULL;
615 redirect = strtok (NULL, " "))
617 if (NULL == (hostname = strstr (redirect, ":")))
619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
620 "Warning: option %s is not formatted correctly!\n",
626 if (NULL == (hostport = strstr (hostname, ":")))
628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
629 "Warning: option %s is not formatted correctly!\n",
636 int local_port = atoi (redirect);
638 if (!((local_port > 0) && (local_port < 65536)))
639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
640 "Warning: %s is not a correct port.", redirect);
644 struct redirect_service *serv =
645 GNUNET_malloc (sizeof (struct redirect_service));
646 memset (serv, 0, 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 // TODO Lookup, yadayadayada
680 serv->remote_port = atoi (hostport);
682 GNUNET_assert (GNUNET_OK ==
683 GNUNET_CONTAINER_multihashmap_put (udp_services,
686 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
688 GNUNET_assert (GNUNET_OK ==
689 GNUNET_CONTAINER_multihashmap_put (tcp_services,
692 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
697 proto = (proto == UDP) ? TCP : UDP;
699 while (proto != UDP);
703 * Start the helper-process
705 * If cls != NULL it is assumed that this function is called as a result of a dying
706 * helper. cls is then taken as handle to the old helper and is cleaned up.
709 start_helper_and_schedule (void *cls,
710 const struct GNUNET_SCHEDULER_TaskContext *tc)
712 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
716 cleanup_helper (cls);
726 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname))
728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
729 "No entry 'IFNAME' in configuration!\n");
734 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
738 "No entry 'IPV6ADDR' in configuration!\n");
743 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
746 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
747 "No entry 'IPV6PREFIX' in configuration!\n");
752 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
756 "No entry 'IPV4ADDR' in configuration!\n");
761 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 "No entry 'IPV4MASK' in configuration!\n");
770 * Messages get passed to the function message_token
771 * When the helper dies, this function will be called again with the
772 * helper_handle as cls.
775 start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
776 "exit-gnunet", start_helper_and_schedule, message_token,
779 GNUNET_free (ipv6addr);
780 GNUNET_free (ipv6prefix);
781 GNUNET_free (ipv4addr);
782 GNUNET_free (ipv4mask);
783 GNUNET_free (ifname);
787 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
788 uint16_t protocol, void *ipaddress, void *tunnel,
789 struct redirect_state *state, struct ip_pkt *pkt4)
793 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
794 pkt4->shdr.size = htons (len);
796 pkt4->tun.type = htons (0x0800);
798 memcpy (&pkt4->data, payload, pktlen);
800 pkt4->ip_hdr.version = 4;
801 pkt4->ip_hdr.hdr_lngth = 5;
802 pkt4->ip_hdr.diff_serv = 0;
803 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
804 pkt4->ip_hdr.ident = 0;
805 pkt4->ip_hdr.flags = 0;
806 pkt4->ip_hdr.frag_off = 0;
807 pkt4->ip_hdr.ttl = 255;
808 pkt4->ip_hdr.proto = protocol;
809 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
811 memcpy (&tmp, ipaddress, 4);
812 pkt4->ip_hdr.dadr = tmp;
814 /* Generate a new src-address */
818 GNUNET_assert (GNUNET_OK ==
819 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
821 GNUNET_assert (GNUNET_OK ==
822 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
824 inet_pton (AF_INET, ipv4addr, &tmp);
825 inet_pton (AF_INET, ipv4mask, &tmp2);
826 GNUNET_free (ipv4addr);
827 GNUNET_free (ipv4mask);
829 /* This should be a noop */
832 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
834 pkt4->ip_hdr.sadr = tmp;
836 memcpy (&state->redirect_info.addr, &tmp, 4);
837 if (0x11 == protocol)
839 struct ip_udp *pkt4_udp = (struct ip_udp *) pkt4;
841 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
843 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
845 else if (0x06 == protocol)
847 struct ip_tcp *pkt4_tcp = (struct ip_tcp *) pkt4;
849 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
851 pkt4_tcp->tcp_hdr.crc = 0;
854 tmp = pkt4->ip_hdr.sadr;
855 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
856 tmp = pkt4->ip_hdr.dadr;
857 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
859 tmp = (protocol << 16) | (0xffff & pktlen);
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp,
866 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
869 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr,
871 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
875 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
879 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
880 uint16_t protocol, void *ipaddress, void *tunnel,
881 struct redirect_state *state, struct ip6_pkt *pkt6)
885 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
886 pkt6->shdr.size = htons (len);
889 pkt6->tun.type = htons (0x86dd);
891 memcpy (&pkt6->data, payload, pktlen);
893 pkt6->ip6_hdr.version = 6;
894 pkt6->ip6_hdr.nxthdr = protocol;
895 pkt6->ip6_hdr.paylgth = htons (pktlen);
896 pkt6->ip6_hdr.hoplmt = 64;
898 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
900 /* Generate a new src-address
901 * This takes as much from the address of the tunnel as fits into
904 unsigned long long ipv6prefix;
906 GNUNET_assert (GNUNET_OK ==
907 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
909 GNUNET_assert (GNUNET_OK ==
910 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
913 GNUNET_assert (ipv6prefix < 127);
914 ipv6prefix = (ipv6prefix + 7) / 8;
916 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
917 GNUNET_free (ipv6addr);
919 if (ipv6prefix < (16 - sizeof (void *)))
920 ipv6prefix = 16 - sizeof (void *);
922 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
924 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
925 ((char *) &tunnel) + offset, 16 - ipv6prefix);
927 /* copy the needed information into the state */
928 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
930 if (0x11 == protocol)
932 struct ip6_udp *pkt6_udp = (struct ip6_udp *) pkt6;
934 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
936 pkt6_udp->udp_hdr.crc = 0;
940 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr,
943 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr,
945 tmp = (htons (pktlen) & 0xffff);
946 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
947 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
948 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
951 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
952 ntohs (pkt6_udp->udp_hdr.len));
953 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
955 else if (0x06 == protocol)
957 struct ip6_tcp *pkt6_tcp = (struct ip6_tcp *) pkt6;
959 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
961 pkt6_tcp->tcp_hdr.crc = 0;
965 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
967 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
968 tmp = htonl (pktlen);
969 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
970 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
971 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
974 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
975 ntohs (pkt6->ip6_hdr.paylgth));
976 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
981 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
984 receive_tcp_service (void *cls
985 __attribute__ ((unused)),
986 struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
987 __attribute__ ((unused)),
988 const struct GNUNET_PeerIdentity *sender
989 __attribute__ ((unused)),
990 const struct GNUNET_MessageHeader *message,
991 const struct GNUNET_TRANSPORT_ATS_Information *atsi
992 __attribute__ ((unused)))
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
995 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
996 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
997 unsigned int pkt_len =
998 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
999 sizeof (GNUNET_HashCode);
1001 /** Get the configuration from the services-hashmap.
1003 * Which service is needed only depends on the service-descriptor and the
1006 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1008 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
1009 *tcp_desc = ntohs (pkt->dpt);
1010 struct redirect_service *serv =
1011 GNUNET_CONTAINER_multihashmap_get (tcp_services,
1012 (GNUNET_HashCode *) tcp_desc);
1016 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n",
1021 pkt->dpt = htons (serv->remote_port);
1024 * At this point it would be possible to check against some kind of ACL.
1030 /* Prepare the state.
1031 * This will be saved in the hashmap, so that the receiving procedure knows
1032 * through which tunnel this connection has to be routed.
1034 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1036 memset (state, 0, sizeof (struct redirect_state));
1037 state->tunnel = tunnel;
1039 state->type = SERVICE;
1040 state->hashmap = tcp_connections;
1041 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1044 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1045 sizeof (struct ip6_hdr) + pkt_len;
1048 memset (buf, 0, len);
1050 switch (serv->version)
1053 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
1054 &serv->v4.ip4address, tunnel, state,
1055 (struct ip_pkt *) buf);
1058 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
1059 &serv->v6.ip6address, tunnel, state,
1060 (struct ip6_pkt *) buf);
1068 hash_redirect_info (&state->hash, &state->redirect_info,
1069 serv->version == 4 ? 4 : 16);
1072 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1074 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1078 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1079 GNUNET_TIME_absolute_get ().abs_value);
1081 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1082 max_tcp_connections)
1083 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1086 GNUNET_free (state);
1088 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1093 receive_tcp_remote (void *cls
1094 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1096 __attribute__ ((unused)),
1097 const struct GNUNET_PeerIdentity *sender
1098 __attribute__ ((unused)),
1099 const struct GNUNET_MessageHeader *message,
1100 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1101 __attribute__ ((unused)))
1103 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1104 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1105 struct remote_addr *s = (struct remote_addr *) desc;
1108 unsigned int pkt_len =
1109 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1110 sizeof (GNUNET_HashCode);
1112 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1114 memset (state, 0, sizeof (struct redirect_state));
1115 state->tunnel = tunnel;
1116 state->type = REMOTE;
1117 state->hashmap = tcp_connections;
1118 memcpy (&state->remote, s, sizeof (struct remote_addr));
1121 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1122 sizeof (struct ip6_hdr) + pkt_len;
1125 memset (buf, 0, len);
1130 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
1131 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1134 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
1135 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1138 return GNUNET_SYSERR;
1142 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n",
1145 *((uint32_t *) & state->hash));
1148 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1150 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1154 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1155 GNUNET_TIME_absolute_get ().abs_value);
1157 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1158 max_tcp_connections)
1159 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1162 GNUNET_free (state);
1164 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1170 receive_udp_remote (void *cls
1171 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1173 __attribute__ ((unused)),
1174 const struct GNUNET_PeerIdentity *sender
1175 __attribute__ ((unused)),
1176 const struct GNUNET_MessageHeader *message,
1177 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1178 __attribute__ ((unused)))
1180 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1181 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1182 struct remote_addr *s = (struct remote_addr *) desc;
1186 GNUNET_assert (ntohs (pkt->len) ==
1187 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1188 sizeof (GNUNET_HashCode));
1190 /* Prepare the state.
1191 * This will be saved in the hashmap, so that the receiving procedure knows
1192 * through which tunnel this connection has to be routed.
1194 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1196 memset (state, 0, sizeof (struct redirect_state));
1197 state->tunnel = tunnel;
1198 state->hashmap = udp_connections;
1199 state->type = REMOTE;
1200 memcpy (&state->remote, s, sizeof (struct remote_addr));
1203 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1204 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1207 memset (buf, 0, len);
1212 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1213 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1216 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1217 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1224 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1227 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1229 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1233 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1234 GNUNET_TIME_absolute_get ().abs_value);
1236 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1237 max_udp_connections)
1238 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1241 GNUNET_free (state);
1243 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1248 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1251 receive_udp_service (void *cls
1252 __attribute__ ((unused)),
1253 struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
1254 __attribute__ ((unused)),
1255 const struct GNUNET_PeerIdentity *sender
1256 __attribute__ ((unused)),
1257 const struct GNUNET_MessageHeader *message,
1258 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1259 __attribute__ ((unused)))
1261 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1262 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1264 GNUNET_assert (ntohs (pkt->len) ==
1265 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1266 sizeof (GNUNET_HashCode));
1268 /* Get the configuration from the hashmap */
1269 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1271 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1272 *udp_desc = ntohs (pkt->dpt);
1273 struct redirect_service *serv =
1274 GNUNET_CONTAINER_multihashmap_get (udp_services,
1275 (GNUNET_HashCode *) udp_desc);
1279 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n",
1284 pkt->dpt = htons (serv->remote_port);
1287 * At this point it would be possible to check against some kind of ACL.
1293 /* Prepare the state.
1294 * This will be saved in the hashmap, so that the receiving procedure knows
1295 * through which tunnel this connection has to be routed.
1297 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1299 memset (state, 0, sizeof (struct redirect_state));
1300 state->tunnel = tunnel;
1302 state->type = SERVICE;
1303 state->hashmap = udp_connections;
1304 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1307 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1308 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1311 memset (buf, 0, len);
1313 switch (serv->version)
1316 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1317 &serv->v4.ip4address, tunnel, state,
1318 (struct ip_pkt *) buf);
1321 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1322 &serv->v6.ip6address, tunnel, state,
1323 (struct ip6_pkt *) buf);
1331 hash_redirect_info (&state->hash, &state->redirect_info,
1332 serv->version == 4 ? 4 : 16);
1335 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1337 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1341 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1342 GNUNET_TIME_absolute_get ().abs_value);
1344 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1345 max_udp_connections)
1346 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1349 GNUNET_free (state);
1351 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1359 int handler_idx, app_idx;
1361 udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP");
1362 tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP");
1364 static struct GNUNET_MESH_MessageHandler handlers[] = {
1365 {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
1366 {receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP, 0},
1372 static GNUNET_MESH_ApplicationType apptypes[] = {
1373 GNUNET_APPLICATION_TYPE_END,
1374 GNUNET_APPLICATION_TYPE_END,
1375 GNUNET_APPLICATION_TYPE_END
1381 if (GNUNET_YES == udp)
1383 handlers[handler_idx].callback = receive_udp_remote;
1384 handlers[handler_idx].expected_size = 0;
1385 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP;
1386 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1391 if (GNUNET_YES == tcp)
1393 handlers[handler_idx].callback = receive_tcp_remote;
1394 handlers[handler_idx].expected_size = 0;
1395 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP;
1396 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1401 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1405 * @brief Main function that will be run by the scheduler.
1407 * @param cls closure
1408 * @param args remaining command-line arguments
1409 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1410 * @param cfg_ configuration
1413 run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfile
1414 __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1420 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1421 udp_connections_heap =
1422 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1423 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1424 tcp_connections_heap =
1425 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1426 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1427 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1429 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1430 &max_udp_connections);
1431 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1432 &max_tcp_connections);
1434 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1436 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1437 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1441 * The main function to obtain template from gnunetd.
1443 * @param argc number of arguments from the command line
1444 * @param argv command line arguments
1445 * @return 0 ok, 1 on error
1448 main (int argc, char *const *argv)
1450 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1451 GNUNET_GETOPT_OPTION_END
1454 return (GNUNET_OK ==
1455 GNUNET_PROGRAM_run (argc, argv, "exit", gettext_noop ("help text"),
1456 options, &run, NULL)) ? ret : 1;
1459 /* end of gnunet-daemon-exit.c */