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
288 * @param version 4 or 6
291 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
293 struct redirect_info u_i;
294 struct GNUNET_MESH_Tunnel *tunnel;
296 struct GNUNET_MessageHeader *msg;
298 memset (&u_i, 0, sizeof (struct redirect_info));
300 memcpy (&u_i.addr, dadr, addrlen);
304 /* get tunnel and service-descriptor from this */
305 GNUNET_HashCode hash;
307 hash_redirect_info (&hash, &u_i, addrlen);
309 struct redirect_state *state =
310 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
312 /* Mark this connection as freshly used */
313 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
314 GNUNET_TIME_absolute_get ().abs_value);
316 tunnel = state->tunnel;
318 if (state->type == SERVICE)
320 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
321 if (ntohs (udp->spt) == state->serv->remote_port)
323 udp->spt = htons (state->serv->my_port);
327 /* otherwise the answer came from a different port (tftp does this)
328 * add this new port to the list of all services, so that the packets
329 * coming back from the client to this new port will be routed correctly
331 struct redirect_service *serv =
332 GNUNET_malloc (sizeof (struct redirect_service));
333 memcpy (serv, state->serv, sizeof (struct redirect_service));
334 serv->my_port = ntohs (udp->spt);
335 serv->remote_port = ntohs (udp->spt);
336 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
338 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
339 sizeof (GNUNET_HashCode));
340 *desc = ntohs (udp->spt);
341 GNUNET_assert (GNUNET_OK ==
342 GNUNET_CONTAINER_multihashmap_put (udp_services,
345 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
351 /* send udp-packet back */
353 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
355 struct GNUNET_MESH_Tunnel **ctunnel =
356 GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
358 msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
359 msg->size = htons (len);
361 htons (state->type ==
362 SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK :
363 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
364 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
366 if (state->type == SERVICE)
367 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
369 memcpy (desc, &state->remote, sizeof (struct remote_addr));
370 void *_udp = desc + 1;
372 memcpy (_udp, udp, ntohs (udp->len));
374 if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
376 /* No notify is pending */
377 struct GNUNET_MESH_TransmitHandle *th =
378 GNUNET_MESH_notify_transmit_ready (tunnel,
381 GNUNET_TIME_relative_divide
382 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
383 (const struct GNUNET_PeerIdentity *)
385 send_udp_to_peer_notify_callback,
388 /* save the handle */
389 GNUNET_MESH_tunnel_set_data (tunnel, th);
393 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
394 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
396 struct tunnel_notify_queue *element =
397 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
398 element->cls = ctunnel;
401 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
402 GNUNET_MESH_tunnel_set_head (tunnel, head);
403 GNUNET_MESH_tunnel_set_tail (tunnel, tail);
408 * @brief Handles a TCP-Packet received from the helper.
410 * @param tcp A pointer to the Packet
411 * @param dadr The IP-Destination-address
412 * @param addrlen The length of the address
413 * @param version 4 or 6
414 * @param pktlen the length of the packet, including its header
417 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
420 struct redirect_info u_i;
421 struct GNUNET_MESH_Tunnel *tunnel;
423 struct GNUNET_MessageHeader *msg;
425 memset (&u_i, 0, sizeof (struct redirect_info));
427 memcpy (&u_i.addr, dadr, addrlen);
430 /* get tunnel and service-descriptor from this */
431 GNUNET_HashCode hash;
433 hash_redirect_info (&hash, &u_i, addrlen);
435 struct redirect_state *state =
436 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "No mapping for this connection; hash is %x\n",
442 *((uint32_t *) & hash));
446 /* Mark this connection as freshly used */
447 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
448 GNUNET_TIME_absolute_get ().abs_value);
450 tunnel = state->tunnel;
452 if (state->type == SERVICE)
454 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
455 if (ntohs (tcp->spt) == state->serv->remote_port)
457 tcp->spt = htons (state->serv->my_port);
461 // This is an illegal packet.
466 /* send tcp-packet back */
468 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
470 struct GNUNET_MESH_Tunnel **ctunnel =
471 GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
473 msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
474 msg->size = htons (len);
476 htons (state->type ==
477 SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK :
478 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
479 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
481 if (state->type == SERVICE)
482 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
484 memcpy (desc, &state->remote, sizeof (struct remote_addr));
485 void *_tcp = desc + 1;
487 memcpy (_tcp, tcp, pktlen);
489 if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
491 /* No notify is pending */
492 struct GNUNET_MESH_TransmitHandle *th =
493 GNUNET_MESH_notify_transmit_ready (tunnel,
496 GNUNET_TIME_relative_divide
497 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
498 (const struct GNUNET_PeerIdentity *)
501 send_udp_to_peer_notify_callback,
504 /* save the handle */
505 GNUNET_MESH_tunnel_set_data (tunnel, th);
509 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
510 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
512 struct tunnel_notify_queue *element =
513 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
514 element->cls = ctunnel;
517 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
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 (0x11 == 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 (0x06 == 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 (0x11 == pkt4->ip_hdr.proto)
553 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
554 (unsigned char *) &tmp, 4);
555 else if (0x06 == 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 memset (serv, 0, sizeof (struct redirect_service));
648 serv->my_port = local_port;
650 if (0 == strcmp ("localhost4", hostname))
656 GNUNET_assert (GNUNET_OK ==
657 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
660 GNUNET_assert (1 == inet_pton (AF_INET, ip4addr, serv->v4.ip4address));
661 GNUNET_free (ip4addr);
663 else if (0 == strcmp ("localhost6", hostname))
669 GNUNET_assert (GNUNET_OK ==
670 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
673 GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, serv->v6.ip6address));
674 GNUNET_free (ip6addr);
678 // TODO Lookup, yadayadayada
681 serv->remote_port = atoi (hostport);
683 GNUNET_assert (GNUNET_OK ==
684 GNUNET_CONTAINER_multihashmap_put (udp_services,
687 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
689 GNUNET_assert (GNUNET_OK ==
690 GNUNET_CONTAINER_multihashmap_put (tcp_services,
693 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
698 proto = (proto == UDP) ? TCP : UDP;
700 while (proto != UDP);
704 * Start the helper-process
706 * If cls != NULL it is assumed that this function is called as a result of a dying
707 * helper. cls is then taken as handle to the old helper and is cleaned up.
710 start_helper_and_schedule (void *cls,
711 const struct GNUNET_SCHEDULER_TaskContext *tc)
713 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
717 cleanup_helper (cls);
727 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname))
729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
730 "No entry 'IFNAME' in configuration!\n");
735 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
739 "No entry 'IPV6ADDR' in configuration!\n");
744 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
747 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
748 "No entry 'IPV6PREFIX' in configuration!\n");
753 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 "No entry 'IPV4ADDR' in configuration!\n");
762 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
766 "No entry 'IPV4MASK' in configuration!\n");
771 * Messages get passed to the function message_token
772 * When the helper dies, this function will be called again with the
773 * helper_handle as cls.
776 start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
777 "exit-gnunet", start_helper_and_schedule, message_token,
780 GNUNET_free (ipv6addr);
781 GNUNET_free (ipv6prefix);
782 GNUNET_free (ipv4addr);
783 GNUNET_free (ipv4mask);
784 GNUNET_free (ifname);
788 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
789 uint16_t protocol, void *ipaddress, void *tunnel,
790 struct redirect_state *state, struct ip_pkt *pkt4)
794 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
795 pkt4->shdr.size = htons (len);
797 pkt4->tun.type = htons (0x0800);
799 memcpy (&pkt4->data, payload, pktlen);
801 pkt4->ip_hdr.version = 4;
802 pkt4->ip_hdr.hdr_lngth = 5;
803 pkt4->ip_hdr.diff_serv = 0;
804 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
805 pkt4->ip_hdr.ident = 0;
806 pkt4->ip_hdr.flags = 0;
807 pkt4->ip_hdr.frag_off = 0;
808 pkt4->ip_hdr.ttl = 255;
809 pkt4->ip_hdr.proto = protocol;
810 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
812 memcpy (&tmp, ipaddress, 4);
813 pkt4->ip_hdr.dadr = tmp;
815 /* Generate a new src-address */
819 GNUNET_assert (GNUNET_OK ==
820 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
822 GNUNET_assert (GNUNET_OK ==
823 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
825 inet_pton (AF_INET, ipv4addr, &tmp);
826 inet_pton (AF_INET, ipv4mask, &tmp2);
827 GNUNET_free (ipv4addr);
828 GNUNET_free (ipv4mask);
830 /* This should be a noop */
833 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
835 pkt4->ip_hdr.sadr = tmp;
837 memcpy (&state->redirect_info.addr, &tmp, 4);
838 if (0x11 == protocol)
840 struct ip_udp *pkt4_udp = (struct ip_udp *) pkt4;
842 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
844 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
846 else if (0x06 == protocol)
848 struct ip_tcp *pkt4_tcp = (struct ip_tcp *) pkt4;
850 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
852 pkt4_tcp->tcp_hdr.crc = 0;
855 tmp = pkt4->ip_hdr.sadr;
856 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
857 tmp = pkt4->ip_hdr.dadr;
858 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
860 tmp = (protocol << 16) | (0xffff & pktlen);
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp,
867 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
870 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr,
872 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
876 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
880 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
881 uint16_t protocol, void *ipaddress, void *tunnel,
882 struct redirect_state *state, struct ip6_pkt *pkt6)
886 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
887 pkt6->shdr.size = htons (len);
890 pkt6->tun.type = htons (0x86dd);
892 memcpy (&pkt6->data, payload, pktlen);
894 pkt6->ip6_hdr.version = 6;
895 pkt6->ip6_hdr.nxthdr = protocol;
896 pkt6->ip6_hdr.paylgth = htons (pktlen);
897 pkt6->ip6_hdr.hoplmt = 64;
899 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
901 /* Generate a new src-address
902 * This takes as much from the address of the tunnel as fits into
905 unsigned long long ipv6prefix;
907 GNUNET_assert (GNUNET_OK ==
908 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
910 GNUNET_assert (GNUNET_OK ==
911 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
914 GNUNET_assert (ipv6prefix < 127);
915 ipv6prefix = (ipv6prefix + 7) / 8;
917 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
918 GNUNET_free (ipv6addr);
920 if (ipv6prefix < (16 - sizeof (void *)))
921 ipv6prefix = 16 - sizeof (void *);
923 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
925 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
926 ((char *) &tunnel) + offset, 16 - ipv6prefix);
928 /* copy the needed information into the state */
929 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
931 if (0x11 == protocol)
933 struct ip6_udp *pkt6_udp = (struct ip6_udp *) pkt6;
935 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
937 pkt6_udp->udp_hdr.crc = 0;
941 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr,
944 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr,
946 tmp = (htons (pktlen) & 0xffff);
947 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
948 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
949 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
952 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
953 ntohs (pkt6_udp->udp_hdr.len));
954 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
956 else if (0x06 == protocol)
958 struct ip6_tcp *pkt6_tcp = (struct ip6_tcp *) pkt6;
960 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
962 pkt6_tcp->tcp_hdr.crc = 0;
966 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
968 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
969 tmp = htonl (pktlen);
970 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
971 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
972 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
975 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
976 ntohs (pkt6->ip6_hdr.paylgth));
977 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
982 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
985 receive_tcp_service (void *cls
986 __attribute__ ((unused)),
987 struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
988 __attribute__ ((unused)),
989 const struct GNUNET_PeerIdentity *sender
990 __attribute__ ((unused)),
991 const struct GNUNET_MessageHeader *message,
992 const struct GNUNET_TRANSPORT_ATS_Information *atsi
993 __attribute__ ((unused)))
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
996 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
997 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
998 unsigned int pkt_len =
999 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1000 sizeof (GNUNET_HashCode);
1002 /** Get the configuration from the services-hashmap.
1004 * Which service is needed only depends on the service-descriptor and the
1007 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1009 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
1010 *tcp_desc = ntohs (pkt->dpt);
1011 struct redirect_service *serv =
1012 GNUNET_CONTAINER_multihashmap_get (tcp_services,
1013 (GNUNET_HashCode *) tcp_desc);
1017 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n",
1022 pkt->dpt = htons (serv->remote_port);
1025 * At this point it would be possible to check against some kind of ACL.
1031 /* Prepare the state.
1032 * This will be saved in the hashmap, so that the receiving procedure knows
1033 * through which tunnel this connection has to be routed.
1035 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1037 memset (state, 0, sizeof (struct redirect_state));
1038 state->tunnel = tunnel;
1040 state->type = SERVICE;
1041 state->hashmap = tcp_connections;
1042 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1045 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1046 sizeof (struct ip6_hdr) + pkt_len;
1049 memset (buf, 0, len);
1051 switch (serv->version)
1054 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
1055 &serv->v4.ip4address, tunnel, state,
1056 (struct ip_pkt *) buf);
1059 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
1060 &serv->v6.ip6address, tunnel, state,
1061 (struct ip6_pkt *) buf);
1069 hash_redirect_info (&state->hash, &state->redirect_info,
1070 serv->version == 4 ? 4 : 16);
1073 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1075 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1076 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1079 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1080 GNUNET_TIME_absolute_get ().abs_value);
1082 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1083 max_tcp_connections)
1084 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1087 GNUNET_free (state);
1089 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1094 receive_tcp_remote (void *cls
1095 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1097 __attribute__ ((unused)),
1098 const struct GNUNET_PeerIdentity *sender
1099 __attribute__ ((unused)),
1100 const struct GNUNET_MessageHeader *message,
1101 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1102 __attribute__ ((unused)))
1104 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1105 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1106 struct remote_addr *s = (struct remote_addr *) desc;
1109 unsigned int pkt_len =
1110 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1111 sizeof (GNUNET_HashCode);
1113 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1115 memset (state, 0, sizeof (struct redirect_state));
1116 state->tunnel = tunnel;
1117 state->type = REMOTE;
1118 state->hashmap = tcp_connections;
1119 memcpy (&state->remote, s, sizeof (struct remote_addr));
1122 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1123 sizeof (struct ip6_hdr) + pkt_len;
1126 memset (buf, 0, len);
1131 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
1132 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1135 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
1136 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1139 return GNUNET_SYSERR;
1143 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n",
1146 *((uint32_t *) & state->hash));
1149 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1151 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1155 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1156 GNUNET_TIME_absolute_get ().abs_value);
1158 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1159 max_tcp_connections)
1160 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1163 GNUNET_free (state);
1165 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1171 receive_udp_remote (void *cls
1172 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1174 __attribute__ ((unused)),
1175 const struct GNUNET_PeerIdentity *sender
1176 __attribute__ ((unused)),
1177 const struct GNUNET_MessageHeader *message,
1178 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1179 __attribute__ ((unused)))
1181 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1182 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1183 struct remote_addr *s = (struct remote_addr *) desc;
1187 GNUNET_assert (ntohs (pkt->len) ==
1188 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1189 sizeof (GNUNET_HashCode));
1191 /* Prepare the state.
1192 * This will be saved in the hashmap, so that the receiving procedure knows
1193 * through which tunnel this connection has to be routed.
1195 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1197 memset (state, 0, sizeof (struct redirect_state));
1198 state->tunnel = tunnel;
1199 state->hashmap = udp_connections;
1200 state->type = REMOTE;
1201 memcpy (&state->remote, s, sizeof (struct remote_addr));
1204 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1205 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1208 memset (buf, 0, len);
1213 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1214 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1217 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1218 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1225 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1228 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1230 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1231 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1234 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1235 GNUNET_TIME_absolute_get ().abs_value);
1237 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1238 max_udp_connections)
1239 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1242 GNUNET_free (state);
1244 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1249 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1252 receive_udp_service (void *cls
1253 __attribute__ ((unused)),
1254 struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
1255 __attribute__ ((unused)),
1256 const struct GNUNET_PeerIdentity *sender
1257 __attribute__ ((unused)),
1258 const struct GNUNET_MessageHeader *message,
1259 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1260 __attribute__ ((unused)))
1262 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1263 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1265 GNUNET_assert (ntohs (pkt->len) ==
1266 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1267 sizeof (GNUNET_HashCode));
1269 /* Get the configuration from the hashmap */
1270 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1272 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1273 *udp_desc = ntohs (pkt->dpt);
1274 struct redirect_service *serv =
1275 GNUNET_CONTAINER_multihashmap_get (udp_services,
1276 (GNUNET_HashCode *) udp_desc);
1280 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n",
1285 pkt->dpt = htons (serv->remote_port);
1288 * At this point it would be possible to check against some kind of ACL.
1294 /* Prepare the state.
1295 * This will be saved in the hashmap, so that the receiving procedure knows
1296 * through which tunnel this connection has to be routed.
1298 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1300 memset (state, 0, sizeof (struct redirect_state));
1301 state->tunnel = tunnel;
1303 state->type = SERVICE;
1304 state->hashmap = udp_connections;
1305 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1308 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1309 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1312 memset (buf, 0, len);
1314 switch (serv->version)
1317 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1318 &serv->v4.ip4address, tunnel, state,
1319 (struct ip_pkt *) buf);
1322 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1323 &serv->v6.ip6address, tunnel, state,
1324 (struct ip6_pkt *) buf);
1332 hash_redirect_info (&state->hash, &state->redirect_info,
1333 serv->version == 4 ? 4 : 16);
1336 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1338 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1342 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1343 GNUNET_TIME_absolute_get ().abs_value);
1345 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1346 max_udp_connections)
1347 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1350 GNUNET_free (state);
1352 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1360 int handler_idx, app_idx;
1362 udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP");
1363 tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP");
1365 static struct GNUNET_MESH_MessageHandler handlers[] = {
1366 {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
1367 {receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP, 0},
1373 static GNUNET_MESH_ApplicationType apptypes[] = {
1374 GNUNET_APPLICATION_TYPE_END,
1375 GNUNET_APPLICATION_TYPE_END,
1376 GNUNET_APPLICATION_TYPE_END
1382 if (GNUNET_YES == udp)
1384 handlers[handler_idx].callback = receive_udp_remote;
1385 handlers[handler_idx].expected_size = 0;
1386 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP;
1387 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1392 if (GNUNET_YES == tcp)
1394 handlers[handler_idx].callback = receive_tcp_remote;
1395 handlers[handler_idx].expected_size = 0;
1396 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP;
1397 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1402 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1406 * @brief Main function that will be run by the scheduler.
1408 * @param cls closure
1409 * @param args remaining command-line arguments
1410 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1411 * @param cfg_ configuration
1414 run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfile
1415 __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1421 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1422 udp_connections_heap =
1423 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1424 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1425 tcp_connections_heap =
1426 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1427 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1428 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1430 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1431 &max_udp_connections);
1432 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1433 &max_tcp_connections);
1435 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1437 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1438 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1442 * The main function to obtain template from gnunetd.
1444 * @param argc number of arguments from the command line
1445 * @param argv command line arguments
1446 * @return 0 ok, 1 on error
1449 main (int argc, char *const *argv)
1451 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1452 GNUNET_GETOPT_OPTION_END
1455 return (GNUNET_OK ==
1456 GNUNET_PROGRAM_run (argc, argv, "exit", gettext_noop ("help text"),
1457 options, &run, NULL)) ? ret : 1;
1460 /* end of gnunet-daemon-exit.c */