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;
139 enum { SERVICE, REMOTE } type;
142 * The source-address and -port of this connection
144 struct redirect_info redirect_info;
148 * This hashmaps saves interesting things about the configured services
150 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
151 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
153 struct tunnel_notify_queue
155 struct tunnel_notify_queue* next;
156 struct tunnel_notify_queue* prev;
162 * Function that frees everything from a hashmap
165 free_iterate(void* cls __attribute__((unused)), const GNUNET_HashCode* hash __attribute__((unused)), void* value)
172 * Function scheduled as very last function, cleans up after us
175 cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
176 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
178 GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
182 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
186 if (mesh_handle != NULL)
188 GNUNET_MESH_disconnect(mesh_handle);
194 collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
195 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
199 struct GNUNET_CONTAINER_Heap *heap = cls;
201 struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap);
203 /* This is free()ed memory! */
204 state->heap_node = NULL;
206 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
208 GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state);
214 hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
217 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
219 * build the hash out of the last bytes of the address and the 2 bytes of
222 memcpy(hash, &u_i->pt, sizeof(u_i->pt));
223 memcpy(((unsigned char*)hash)+2, u_i->addr+(addrlen-(sizeof(unsigned int) - 2)), (sizeof(unsigned int) - 2));
224 memset(((unsigned char*)hash)+sizeof(unsigned int), 0, sizeof(GNUNET_HashCode) - sizeof(unsigned int));
228 * cls is the pointer to a GNUNET_MessageHeader that is
229 * followed by the service-descriptor and the udp-packet that should be sent;
232 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
234 struct GNUNET_MESH_Tunnel** tunnel = cls;
235 GNUNET_MESH_tunnel_set_data(*tunnel, NULL);
236 struct GNUNET_MessageHeader *hdr = (struct GNUNET_MessageHeader*)(tunnel + 1);
237 GNUNET_assert (size >= ntohs (hdr->size));
238 memcpy (buf, hdr, ntohs (hdr->size));
239 size = ntohs(hdr->size);
241 if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel))
243 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel);
244 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
245 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
247 GNUNET_CONTAINER_DLL_remove(head, tail, element);
249 GNUNET_MESH_tunnel_set_head(*tunnel, head);
250 GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
252 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
255 GNUNET_TIME_relative_divide
256 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
257 (const struct GNUNET_PeerIdentity *)
259 send_udp_to_peer_notify_callback, element->cls);
260 /* save the handle */
261 GNUNET_MESH_tunnel_set_data(*tunnel, th);
270 * @brief Handles an UDP-Packet received from the helper.
272 * @param udp A pointer to the Packet
273 * @param dadr The IP-Destination-address
274 * @param addrlen The length of the address
275 * @param version 4 or 6
278 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
280 struct redirect_info u_i;
281 struct GNUNET_MESH_Tunnel *tunnel;
283 struct GNUNET_MessageHeader *msg;
285 memset (&u_i, 0, sizeof (struct redirect_info));
287 memcpy (&u_i.addr, dadr, addrlen);
291 /* get tunnel and service-descriptor from this */
292 GNUNET_HashCode hash;
293 hash_redirect_info(&hash, &u_i, addrlen);
295 struct redirect_state *state =
296 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
298 /* Mark this connection as freshly used */
299 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
300 GNUNET_TIME_absolute_get ().abs_value);
302 tunnel = state->tunnel;
304 if (state->type == SERVICE)
306 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
307 if (ntohs (udp->spt) == state->serv->remote_port)
309 udp->spt = htons (state->serv->my_port);
313 /* otherwise the answer came from a different port (tftp does this)
314 * add this new port to the list of all services, so that the packets
315 * coming back from the client to this new port will be routed correctly
317 struct redirect_service *serv =
318 GNUNET_malloc (sizeof (struct redirect_service));
319 memcpy (serv, state->serv, sizeof (struct redirect_service));
320 serv->my_port = ntohs (udp->spt);
321 serv->remote_port = ntohs (udp->spt);
322 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
323 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
324 sizeof (GNUNET_HashCode));
325 *desc = ntohs (udp->spt);
326 GNUNET_assert (GNUNET_OK ==
327 GNUNET_CONTAINER_multihashmap_put (udp_services,
328 (GNUNET_HashCode*)desc, serv,
329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
335 /* send udp-packet back */
337 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
339 struct GNUNET_MESH_Tunnel** ctunnel = GNUNET_malloc (sizeof(struct GNUNET_MESH_TUNNEL*) + len);
341 msg = (struct GNUNET_MessageHeader*)(ctunnel + 1);
342 msg->size = htons (len);
343 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK);
344 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
345 if (state->type == SERVICE)
346 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
348 memcpy (desc, &state->remote, sizeof (struct remote_addr));
349 void *_udp = desc + 1;
350 memcpy (_udp, udp, ntohs (udp->len));
352 if (NULL == GNUNET_MESH_tunnel_get_data(tunnel))
354 /* No notify is pending */
355 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (tunnel,
358 GNUNET_TIME_relative_divide
359 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
360 (const struct GNUNET_PeerIdentity *)
362 send_udp_to_peer_notify_callback, ctunnel);
363 /* save the handle */
364 GNUNET_MESH_tunnel_set_data(tunnel, th);
368 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(tunnel);
369 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(tunnel);
371 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue));
372 element->cls = ctunnel;
375 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
376 GNUNET_MESH_tunnel_set_head(tunnel, head);
377 GNUNET_MESH_tunnel_set_tail(tunnel, tail);
382 * @brief Handles a TCP-Packet received from the helper.
384 * @param tcp A pointer to the Packet
385 * @param dadr The IP-Destination-address
386 * @param addrlen The length of the address
387 * @param version 4 or 6
388 * @param pktlen the length of the packet, including its header
391 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
394 struct redirect_info u_i;
395 struct GNUNET_MESH_Tunnel *tunnel;
397 struct GNUNET_MessageHeader *msg;
399 memset (&u_i, 0, sizeof (struct redirect_info));
401 memcpy (&u_i.addr, dadr, addrlen);
404 /* get tunnel and service-descriptor from this */
405 GNUNET_HashCode hash;
406 hash_redirect_info(&hash, &u_i, addrlen);
408 struct redirect_state *state =
409 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
411 if (state == NULL) return;
413 /* Mark this connection as freshly used */
414 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
415 GNUNET_TIME_absolute_get ().abs_value);
417 tunnel = state->tunnel;
419 if (state->type == SERVICE)
421 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
422 if (ntohs (tcp->spt) == state->serv->remote_port)
424 tcp->spt = htons (state->serv->my_port);
428 // This is an illegal packet.
433 /* send tcp-packet back */
435 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
437 struct GNUNET_MESH_Tunnel** ctunnel = GNUNET_malloc (sizeof(struct GNUNET_MESH_TUNNEL*) + len);
439 msg = (struct GNUNET_MessageHeader*)(ctunnel + 1);
440 msg->size = htons (len);
441 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK);
442 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
443 if (state->type == SERVICE)
444 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
446 memcpy (desc, &state->remote, sizeof (struct remote_addr));
447 void *_tcp = desc + 1;
448 memcpy (_tcp, tcp, pktlen);
450 if (NULL == GNUNET_MESH_tunnel_get_data(tunnel))
452 /* No notify is pending */
453 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (tunnel,
456 GNUNET_TIME_relative_divide
457 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
458 (const struct GNUNET_PeerIdentity *)NULL,
459 len, send_udp_to_peer_notify_callback,
461 /* save the handle */
462 GNUNET_MESH_tunnel_set_data(tunnel, th);
466 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(tunnel);
467 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(tunnel);
469 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue));
470 element->cls = ctunnel;
473 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
479 * Receive packets from the helper-process
482 message_token (void *cls __attribute__((unused)),
483 void *client __attribute__((unused)), const struct GNUNET_MessageHeader *message)
485 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
487 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
489 /* ethertype is ipv6 */
490 if (ntohs (pkt_tun->tun.type) == 0x86dd)
492 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
493 if (0x11 == pkt6->ip6_hdr.nxthdr)
494 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
495 (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
496 else if (0x06 == pkt6->ip6_hdr.nxthdr)
497 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
498 (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
499 ntohs (pkt6->ip6_hdr.paylgth));
501 else if (ntohs (pkt_tun->tun.type) == 0x0800)
503 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
504 uint32_t tmp = pkt4->ip_hdr.dadr;
505 if (0x11 == pkt4->ip_hdr.proto)
506 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
507 (unsigned char *) &tmp, 4);
508 else if (0x06 == pkt4->ip_hdr.proto)
510 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
511 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
512 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
513 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
514 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
515 (unsigned char *) &tmp, 4, pktlen);
525 * Reads the configuration servicecfg and populates udp_services
528 * @param section name of section in config, equal to hostname
531 read_service_conf (void *cls __attribute__((unused)), const char *section)
533 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
536 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n", strlen(section), section, section + (strlen(section) - 8));
542 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
543 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
544 (GNUNET_HashCode *) (desc + 1));
553 if (proto == UDP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "UDP_REDIRECTS", &cpy)))
555 else if (proto == TCP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "TCP_REDIRECTS", &cpy)))
558 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
561 if (NULL == (hostname = strstr (redirect, ":")))
563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
569 if (NULL == (hostport = strstr (hostname, ":")))
571 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
578 int local_port = atoi (redirect);
579 if (!((local_port > 0) && (local_port < 65536)))
580 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
585 struct redirect_service *serv =
586 GNUNET_malloc (sizeof (struct redirect_service));
587 memset (serv, 0, sizeof (struct redirect_service));
588 serv->my_port = local_port;
590 if (0 == strcmp ("localhost4", hostname))
595 GNUNET_assert (GNUNET_OK ==
596 GNUNET_CONFIGURATION_get_value_string (cfg,
601 inet_pton (AF_INET, ip4addr,
602 serv->v4.ip4address));
603 GNUNET_free (ip4addr);
605 else if (0 == strcmp ("localhost6", hostname))
610 GNUNET_assert (GNUNET_OK ==
611 GNUNET_CONFIGURATION_get_value_string (cfg,
616 inet_pton (AF_INET6, ip6addr,
617 serv->v6.ip6address));
618 GNUNET_free (ip6addr);
622 // TODO Lookup, yadayadayada
625 serv->remote_port = atoi (hostport);
627 GNUNET_assert (GNUNET_OK ==
628 GNUNET_CONTAINER_multihashmap_put (udp_services,
629 (GNUNET_HashCode*)desc, serv,
630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
632 GNUNET_assert (GNUNET_OK ==
633 GNUNET_CONTAINER_multihashmap_put (tcp_services,
634 (GNUNET_HashCode*)desc, serv,
635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
640 proto = (proto == UDP) ? TCP : UDP;
642 while (proto != UDP);
646 * Start the helper-process
648 * If cls != NULL it is assumed that this function is called as a result of a dying
649 * helper. cls is then taken as handle to the old helper and is cleaned up.
652 start_helper_and_schedule(void *cls,
653 const struct GNUNET_SCHEDULER_TaskContext *tc) {
654 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
667 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
669 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
673 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
675 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
679 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
681 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
685 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
687 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
691 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
693 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
698 * Messages get passed to the function message_token
699 * When the helper dies, this function will be called again with the
700 * helper_handle as cls.
702 helper_handle = start_helper(ifname,
708 start_helper_and_schedule,
712 GNUNET_free(ipv6addr);
713 GNUNET_free(ipv6prefix);
714 GNUNET_free(ipv4addr);
715 GNUNET_free(ipv4mask);
720 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
721 uint16_t protocol, void *ipaddress, void *tunnel,
722 struct redirect_state *state, struct ip_pkt *pkt4)
726 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
727 pkt4->shdr.size = htons (len);
729 pkt4->tun.type = htons (0x0800);
731 memcpy (&pkt4->data, payload, pktlen);
733 pkt4->ip_hdr.version = 4;
734 pkt4->ip_hdr.hdr_lngth = 5;
735 pkt4->ip_hdr.diff_serv = 0;
736 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
737 pkt4->ip_hdr.ident = 0;
738 pkt4->ip_hdr.flags = 0;
739 pkt4->ip_hdr.frag_off = 0;
740 pkt4->ip_hdr.ttl = 255;
741 pkt4->ip_hdr.proto = protocol;
742 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
744 memcpy (&tmp, ipaddress, 4);
745 pkt4->ip_hdr.dadr = tmp;
747 /* Generate a new src-address */
750 GNUNET_assert (GNUNET_OK ==
751 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
754 GNUNET_assert (GNUNET_OK ==
755 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
758 inet_pton (AF_INET, ipv4addr, &tmp);
759 inet_pton (AF_INET, ipv4mask, &tmp2);
760 GNUNET_free (ipv4addr);
761 GNUNET_free (ipv4mask);
763 /* This should be a noop */
766 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
768 pkt4->ip_hdr.sadr = tmp;
770 memcpy (&state->redirect_info.addr, &tmp, 4);
771 if (0x11 == protocol)
773 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
774 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
776 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
778 else if (0x06 == protocol)
780 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
781 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
783 pkt4_tcp->tcp_hdr.crc = 0;
785 tmp = pkt4->ip_hdr.sadr;
787 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
788 tmp = pkt4->ip_hdr.dadr;
790 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
792 tmp = (protocol << 16) | (0xffff & pktlen);
794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
798 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
801 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
802 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
806 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
810 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
811 uint16_t protocol, void *ipaddress, void *tunnel,
812 struct redirect_state *state, struct ip6_pkt *pkt6)
816 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
817 pkt6->shdr.size = htons (len);
820 pkt6->tun.type = htons (0x86dd);
822 memcpy (&pkt6->data, payload, pktlen);
824 pkt6->ip6_hdr.version = 6;
825 pkt6->ip6_hdr.nxthdr = protocol;
826 pkt6->ip6_hdr.paylgth = htons (pktlen);
827 pkt6->ip6_hdr.hoplmt = 64;
829 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
831 /* Generate a new src-address
832 * This takes as much from the address of the tunnel as fits into
835 unsigned long long ipv6prefix;
836 GNUNET_assert (GNUNET_OK ==
837 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
840 GNUNET_assert (GNUNET_OK ==
841 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
844 GNUNET_assert (ipv6prefix < 127);
845 ipv6prefix = (ipv6prefix + 7) / 8;
847 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
848 GNUNET_free (ipv6addr);
850 if (ipv6prefix < (16 - sizeof (void *)))
851 ipv6prefix = 16 - sizeof (void *);
853 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
854 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
855 ((char *) &tunnel) + offset, 16 - ipv6prefix);
857 /* copy the needed information into the state */
858 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
860 if (0x11 == protocol)
862 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
863 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
865 pkt6_udp->udp_hdr.crc = 0;
868 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
870 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
871 tmp = (htons (pktlen) & 0xffff);
872 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
873 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
874 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
877 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
878 ntohs (pkt6_udp->udp_hdr.len));
879 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
881 else if (0x06 == protocol)
883 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
884 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
886 pkt6_tcp->tcp_hdr.crc = 0;
889 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
891 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
893 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
894 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
895 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
898 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
899 ntohs (pkt6->ip6_hdr.paylgth));
900 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
905 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
908 receive_tcp_service (void *cls __attribute__((unused)),
909 struct GNUNET_MESH_Tunnel *tunnel,
910 void **tunnel_ctx __attribute__((unused)),
911 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
912 const struct GNUNET_MessageHeader *message,
913 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
916 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
917 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
918 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
919 GNUNET_MessageHeader)
920 - sizeof (GNUNET_HashCode);
922 /** Get the configuration from the services-hashmap.
924 * Which service is needed only depends on the service-descriptor and the
927 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
929 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
930 *tcp_desc = ntohs (pkt->dpt);
931 struct redirect_service *serv =
932 GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
935 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
936 "No service found for TCP dpt %d!\n", *tcp_desc);
940 pkt->dpt = htons (serv->remote_port);
943 * At this point it would be possible to check against some kind of ACL.
949 /* Prepare the state.
950 * This will be saved in the hashmap, so that the receiving procedure knows
951 * through which tunnel this connection has to be routed.
953 struct redirect_state *state =
954 GNUNET_malloc (sizeof (struct redirect_state));
955 memset (state, 0, sizeof (struct redirect_state));
956 state->tunnel = tunnel;
958 state->type = SERVICE;
959 state->hashmap = tcp_connections;
960 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
962 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
963 sizeof (struct ip6_hdr) + pkt_len;
966 memset (buf, 0, len);
968 switch (serv->version)
971 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
972 &serv->v4.ip4address,
973 tunnel, state, (struct ip_pkt *) buf);
976 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
977 &serv->v6.ip6address,
978 tunnel, state, (struct ip6_pkt *) buf);
986 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
989 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
991 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
995 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
996 GNUNET_TIME_absolute_get ().abs_value);
998 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
999 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
1002 GNUNET_free (state);
1004 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1009 receive_tcp_remote (void *cls __attribute__((unused)),
1010 struct GNUNET_MESH_Tunnel *tunnel,
1011 void **tunnel_ctx __attribute__((unused)),
1012 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
1013 const struct GNUNET_MessageHeader *message,
1014 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
1016 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1017 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1018 struct remote_addr *s = (struct remote_addr *) desc;
1021 unsigned int pkt_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - sizeof (GNUNET_HashCode);
1023 struct redirect_state *state =
1024 GNUNET_malloc (sizeof (struct redirect_state));
1025 memset (state, 0, sizeof (struct redirect_state));
1026 state->tunnel = tunnel;
1027 state->type = REMOTE;
1028 state->hashmap = tcp_connections;
1029 memcpy (&state->remote, s, sizeof (struct remote_addr));
1031 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1032 sizeof (struct ip6_hdr) + pkt_len;
1035 memset (buf, 0, len);
1040 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
1041 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1044 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
1045 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1052 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1055 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1057 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1058 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1061 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1062 GNUNET_TIME_absolute_get ().abs_value);
1064 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1065 max_udp_connections)
1066 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1069 GNUNET_free (state);
1071 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1077 receive_udp_remote (void *cls __attribute__((unused)),
1078 struct GNUNET_MESH_Tunnel *tunnel,
1079 void **tunnel_ctx __attribute__((unused)),
1080 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
1081 const struct GNUNET_MessageHeader *message,
1082 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
1084 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1085 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1086 struct remote_addr *s = (struct remote_addr *) desc;
1090 GNUNET_assert (ntohs (pkt->len) ==
1091 ntohs (message->size) -
1092 sizeof (struct GNUNET_MessageHeader) -
1093 sizeof (GNUNET_HashCode));
1095 /* Prepare the state.
1096 * This will be saved in the hashmap, so that the receiving procedure knows
1097 * through which tunnel this connection has to be routed.
1099 struct redirect_state *state =
1100 GNUNET_malloc (sizeof (struct redirect_state));
1101 memset (state, 0, sizeof (struct redirect_state));
1102 state->tunnel = tunnel;
1103 state->hashmap = udp_connections;
1104 state->type = REMOTE;
1105 memcpy (&state->remote, s, sizeof (struct remote_addr));
1107 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1108 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1111 memset (buf, 0, len);
1116 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1117 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1120 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1121 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1128 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1131 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1133 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1134 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1137 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1138 GNUNET_TIME_absolute_get ().abs_value);
1140 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1141 max_udp_connections)
1142 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1145 GNUNET_free (state);
1147 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1152 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1155 receive_udp_service (void *cls __attribute__((unused)),
1156 struct GNUNET_MESH_Tunnel *tunnel,
1157 void **tunnel_ctx __attribute__((unused)),
1158 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
1159 const struct GNUNET_MessageHeader *message,
1160 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
1162 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1163 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1165 GNUNET_assert (ntohs (pkt->len) ==
1166 ntohs (message->size) -
1167 sizeof (struct GNUNET_MessageHeader) -
1168 sizeof (GNUNET_HashCode));
1170 /* Get the configuration from the hashmap */
1171 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1172 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1173 *udp_desc = ntohs (pkt->dpt);
1174 struct redirect_service *serv =
1175 GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
1178 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1179 "No service found for UDP dpt %d!\n", *udp_desc);
1183 pkt->dpt = htons (serv->remote_port);
1186 * At this point it would be possible to check against some kind of ACL.
1192 /* Prepare the state.
1193 * This will be saved in the hashmap, so that the receiving procedure knows
1194 * through which tunnel this connection has to be routed.
1196 struct redirect_state *state =
1197 GNUNET_malloc (sizeof (struct redirect_state));
1198 memset (state, 0, sizeof (struct redirect_state));
1199 state->tunnel = tunnel;
1201 state->type = SERVICE;
1202 state->hashmap = udp_connections;
1203 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1205 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1206 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1209 memset (buf, 0, len);
1211 switch (serv->version)
1214 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1215 &serv->v4.ip4address,
1216 tunnel, state, (struct ip_pkt *) buf);
1219 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1220 &serv->v6.ip6address,
1221 tunnel, state, (struct ip6_pkt *) buf);
1229 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
1232 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1234 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1235 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1238 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1239 GNUNET_TIME_absolute_get ().abs_value);
1241 if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections)
1242 GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap);
1245 GNUNET_free (state);
1247 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1255 int handler_idx, app_idx;
1257 udp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_UDP");
1258 tcp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_TCP");
1260 static struct GNUNET_MESH_MessageHandler handlers[] = {
1261 {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
1262 {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
1268 static GNUNET_MESH_ApplicationType apptypes[] =
1270 GNUNET_APPLICATION_TYPE_END,
1271 GNUNET_APPLICATION_TYPE_END,
1272 GNUNET_APPLICATION_TYPE_END
1278 if (GNUNET_YES == udp)
1280 handlers[handler_idx].callback = receive_udp_remote;
1281 handlers[handler_idx].expected_size = 0;
1282 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_UDP;
1283 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1288 if (GNUNET_YES == tcp)
1290 handlers[handler_idx].callback = receive_tcp_remote;
1291 handlers[handler_idx].expected_size = 0;
1292 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_TCP;
1293 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1298 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1302 * @brief Main function that will be run by the scheduler.
1304 * @param cls closure
1305 * @param args remaining command-line arguments
1306 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1307 * @param cfg_ configuration
1311 char *const *args __attribute__((unused)),
1312 const char *cfgfile __attribute__((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1318 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1319 udp_connections_heap =
1320 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1321 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1322 tcp_connections_heap =
1323 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1324 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1325 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1327 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1328 &max_udp_connections);
1329 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1330 &max_tcp_connections);
1332 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1334 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1335 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1339 * The main function to obtain template from gnunetd.
1341 * @param argc number of arguments from the command line
1342 * @param argv command line arguments
1343 * @return 0 ok, 1 on error
1346 main (int argc, char *const *argv) {
1347 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1348 GNUNET_GETOPT_OPTION_END
1351 return (GNUNET_OK ==
1352 GNUNET_PROGRAM_run (argc,
1355 gettext_noop ("help text"),
1356 options, &run, NULL)) ? ret : 1;
1359 /* end of gnunet-daemon-exit.c */