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;
154 * Function that frees everything from a hashmap
157 free_iterate(void* cls __attribute__((unused)), const GNUNET_HashCode* hash __attribute__((unused)), void* value)
164 * Function scheduled as very last function, cleans up after us
167 cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
168 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
170 GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
174 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
178 if (mesh_handle != NULL)
180 GNUNET_MESH_disconnect(mesh_handle);
186 collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
187 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
191 struct GNUNET_CONTAINER_Heap *heap = cls;
193 struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap);
195 /* This is free()ed memory! */
196 state->heap_node = NULL;
198 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
200 GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state);
206 hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
209 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
211 * build the hash out of the last bytes of the address and the 2 bytes of
214 memcpy(hash, &u_i->pt, sizeof(u_i->pt));
215 memcpy(((unsigned char*)hash)+2, u_i->addr+(addrlen-(sizeof(unsigned int) - 2)), (sizeof(unsigned int) - 2));
216 memset(((unsigned char*)hash)+sizeof(unsigned int), 0, sizeof(GNUNET_HashCode) - sizeof(unsigned int));
220 * cls is the pointer to a GNUNET_MessageHeader that is
221 * followed by the service-descriptor and the udp-packet that should be sent;
224 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
226 struct GNUNET_MessageHeader *hdr = cls;
227 GNUNET_assert (size >= ntohs (hdr->size));
228 memcpy (buf, hdr, ntohs (hdr->size));
229 size = ntohs(hdr->size);
235 * @brief Handles an UDP-Packet received from the helper.
237 * @param udp A pointer to the Packet
238 * @param dadr The IP-Destination-address
239 * @param addrlen The length of the address
240 * @param version 4 or 6
243 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
245 struct redirect_info u_i;
246 struct GNUNET_MESH_Tunnel *tunnel;
248 struct GNUNET_MessageHeader *msg;
250 memset (&u_i, 0, sizeof (struct redirect_info));
252 memcpy (&u_i.addr, dadr, addrlen);
256 /* get tunnel and service-descriptor from this */
257 GNUNET_HashCode hash;
258 hash_redirect_info(&hash, &u_i, addrlen);
260 struct redirect_state *state =
261 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
263 /* Mark this connection as freshly used */
264 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
265 GNUNET_TIME_absolute_get ().abs_value);
267 tunnel = state->tunnel;
269 if (state->type == SERVICE)
271 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
272 if (ntohs (udp->spt) == state->serv->remote_port)
274 udp->spt = htons (state->serv->my_port);
278 /* otherwise the answer came from a different port (tftp does this)
279 * add this new port to the list of all services, so that the packets
280 * coming back from the client to this new port will be routed correctly
282 struct redirect_service *serv =
283 GNUNET_malloc (sizeof (struct redirect_service));
284 memcpy (serv, state->serv, sizeof (struct redirect_service));
285 serv->my_port = ntohs (udp->spt);
286 serv->remote_port = ntohs (udp->spt);
287 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
288 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
289 sizeof (GNUNET_HashCode));
290 *desc = ntohs (udp->spt);
291 GNUNET_assert (GNUNET_OK ==
292 GNUNET_CONTAINER_multihashmap_put (udp_services,
293 (GNUNET_HashCode*)desc, serv,
294 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
300 /* send udp-packet back */
302 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
304 msg = GNUNET_malloc (len);
305 msg->size = htons (len);
306 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK);
307 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
308 if (state->type == SERVICE)
309 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
311 memcpy (desc, &state->remote, sizeof (struct remote_addr));
312 void *_udp = desc + 1;
313 memcpy (_udp, udp, ntohs (udp->len));
315 GNUNET_MESH_notify_transmit_ready (tunnel,
318 GNUNET_TIME_relative_divide
319 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
320 (const struct GNUNET_PeerIdentity *)
322 send_udp_to_peer_notify_callback, msg);
326 * @brief Handles a TCP-Packet received from the helper.
328 * @param tcp A pointer to the Packet
329 * @param dadr The IP-Destination-address
330 * @param addrlen The length of the address
331 * @param version 4 or 6
332 * @param pktlen the length of the packet, including its header
335 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
338 struct redirect_info u_i;
339 struct GNUNET_MESH_Tunnel *tunnel;
341 struct GNUNET_MessageHeader *msg;
343 memset (&u_i, 0, sizeof (struct redirect_info));
345 memcpy (&u_i.addr, dadr, addrlen);
348 /* get tunnel and service-descriptor from this */
349 GNUNET_HashCode hash;
350 hash_redirect_info(&hash, &u_i, addrlen);
352 struct redirect_state *state =
353 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
355 if (state == NULL) return;
357 /* Mark this connection as freshly used */
358 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
359 GNUNET_TIME_absolute_get ().abs_value);
361 tunnel = state->tunnel;
363 if (state->type == SERVICE)
365 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
366 if (ntohs (tcp->spt) == state->serv->remote_port)
368 tcp->spt = htons (state->serv->my_port);
372 // This is an illegal packet.
377 /* send tcp-packet back */
379 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
380 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
381 msg = GNUNET_malloc (len);
382 msg->size = htons (len);
383 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK);
384 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
385 if (state->type == SERVICE)
386 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
388 memcpy (desc, &state->remote, sizeof (struct remote_addr));
389 void *_tcp = desc + 1;
390 memcpy (_tcp, tcp, pktlen);
392 GNUNET_MESH_notify_transmit_ready (tunnel,
395 GNUNET_TIME_relative_divide
396 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
397 (const struct GNUNET_PeerIdentity *)NULL,
398 len, send_udp_to_peer_notify_callback,
404 * Receive packets from the helper-process
407 message_token (void *cls __attribute__((unused)),
408 void *client __attribute__((unused)), const struct GNUNET_MessageHeader *message)
410 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
412 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
414 /* ethertype is ipv6 */
415 if (ntohs (pkt_tun->tun.type) == 0x86dd)
417 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
418 if (0x11 == pkt6->ip6_hdr.nxthdr)
419 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
420 (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
421 else if (0x06 == pkt6->ip6_hdr.nxthdr)
422 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
423 (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
424 ntohs (pkt6->ip6_hdr.paylgth));
426 else if (ntohs (pkt_tun->tun.type) == 0x0800)
428 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
429 uint32_t tmp = pkt4->ip_hdr.dadr;
430 if (0x11 == pkt4->ip_hdr.proto)
431 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
432 (unsigned char *) &tmp, 4);
433 else if (0x06 == pkt4->ip_hdr.proto)
435 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
437 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
439 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
440 (unsigned char *) &tmp, 4, pktlen);
450 * Reads the configuration servicecfg and populates udp_services
453 * @param section name of section in config, equal to hostname
456 read_service_conf (void *cls __attribute__((unused)), const char *section)
458 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n", strlen(section), section, section + (strlen(section) - 8));
467 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
468 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
469 (GNUNET_HashCode *) (desc + 1));
478 if (proto == UDP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "UDP_REDIRECTS", &cpy)))
480 else if (proto == TCP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "TCP_REDIRECTS", &cpy)))
483 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
486 if (NULL == (hostname = strstr (redirect, ":")))
488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
494 if (NULL == (hostport = strstr (hostname, ":")))
496 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
503 int local_port = atoi (redirect);
504 if (!((local_port > 0) && (local_port < 65536)))
505 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
510 struct redirect_service *serv =
511 GNUNET_malloc (sizeof (struct redirect_service));
512 memset (serv, 0, sizeof (struct redirect_service));
513 serv->my_port = local_port;
515 if (0 == strcmp ("localhost4", hostname))
520 GNUNET_assert (GNUNET_OK ==
521 GNUNET_CONFIGURATION_get_value_string (cfg,
526 inet_pton (AF_INET, ip4addr,
527 serv->v4.ip4address));
528 GNUNET_free (ip4addr);
530 else if (0 == strcmp ("localhost6", hostname))
535 GNUNET_assert (GNUNET_OK ==
536 GNUNET_CONFIGURATION_get_value_string (cfg,
541 inet_pton (AF_INET6, ip6addr,
542 serv->v6.ip6address));
543 GNUNET_free (ip6addr);
547 // TODO Lookup, yadayadayada
550 serv->remote_port = atoi (hostport);
552 GNUNET_assert (GNUNET_OK ==
553 GNUNET_CONTAINER_multihashmap_put (udp_services,
554 (GNUNET_HashCode*)desc, serv,
555 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
557 GNUNET_assert (GNUNET_OK ==
558 GNUNET_CONTAINER_multihashmap_put (tcp_services,
559 (GNUNET_HashCode*)desc, serv,
560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
565 proto = (proto == UDP) ? TCP : UDP;
567 while (proto != UDP);
571 * Start the helper-process
573 * If cls != NULL it is assumed that this function is called as a result of a dying
574 * helper. cls is then taken as handle to the old helper and is cleaned up.
577 start_helper_and_schedule(void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc) {
579 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
592 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
594 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
598 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
600 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
604 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
606 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
610 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
612 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
616 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
618 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
623 * Messages get passed to the function message_token
624 * When the helper dies, this function will be called again with the
625 * helper_handle as cls.
627 helper_handle = start_helper(ifname,
633 start_helper_and_schedule,
637 GNUNET_free(ipv6addr);
638 GNUNET_free(ipv6prefix);
639 GNUNET_free(ipv4addr);
640 GNUNET_free(ipv4mask);
645 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
646 uint16_t protocol, void *ipaddress, void *tunnel,
647 struct redirect_state *state, struct ip_pkt *pkt4)
651 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
652 pkt4->shdr.size = htons (len);
654 pkt4->tun.type = htons (0x0800);
656 memcpy (&pkt4->data, payload, pktlen);
658 pkt4->ip_hdr.version = 4;
659 pkt4->ip_hdr.hdr_lngth = 5;
660 pkt4->ip_hdr.diff_serv = 0;
661 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
662 pkt4->ip_hdr.ident = 0;
663 pkt4->ip_hdr.flags = 0;
664 pkt4->ip_hdr.frag_off = 0;
665 pkt4->ip_hdr.ttl = 255;
666 pkt4->ip_hdr.proto = protocol;
667 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
669 memcpy (&tmp, ipaddress, 4);
670 pkt4->ip_hdr.dadr = tmp;
672 /* Generate a new src-address */
675 GNUNET_assert (GNUNET_OK ==
676 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
679 GNUNET_assert (GNUNET_OK ==
680 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
683 inet_pton (AF_INET, ipv4addr, &tmp);
684 inet_pton (AF_INET, ipv4mask, &tmp2);
685 GNUNET_free (ipv4addr);
686 GNUNET_free (ipv4mask);
688 /* This should be a noop */
691 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
693 pkt4->ip_hdr.sadr = tmp;
695 memcpy (&state->redirect_info.addr, &tmp, 4);
696 if (0x11 == protocol)
698 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
699 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
701 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
703 else if (0x06 == protocol)
705 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
706 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
708 pkt4_tcp->tcp_hdr.crc = 0;
710 tmp = pkt4->ip_hdr.sadr;
712 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
713 tmp = pkt4->ip_hdr.dadr;
715 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
717 tmp = (protocol << 16) | (0xffff & pktlen);
719 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
723 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
726 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
727 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
731 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
735 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
736 uint16_t protocol, void *ipaddress, void *tunnel,
737 struct redirect_state *state, struct ip6_pkt *pkt6)
741 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
742 pkt6->shdr.size = htons (len);
745 pkt6->tun.type = htons (0x86dd);
747 memcpy (&pkt6->data, payload, pktlen);
749 pkt6->ip6_hdr.version = 6;
750 pkt6->ip6_hdr.nxthdr = protocol;
751 pkt6->ip6_hdr.paylgth = htons (pktlen);
752 pkt6->ip6_hdr.hoplmt = 64;
754 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
756 /* Generate a new src-address
757 * This takes as much from the address of the tunnel as fits into
760 unsigned long long ipv6prefix;
761 GNUNET_assert (GNUNET_OK ==
762 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
765 GNUNET_assert (GNUNET_OK ==
766 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
769 GNUNET_assert (ipv6prefix < 127);
770 ipv6prefix = (ipv6prefix + 7) / 8;
772 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
773 GNUNET_free (ipv6addr);
775 if (ipv6prefix < (16 - sizeof (void *)))
776 ipv6prefix = 16 - sizeof (void *);
778 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
779 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
780 ((char *) &tunnel) + offset, 16 - ipv6prefix);
782 /* copy the needed information into the state */
783 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
785 if (0x11 == protocol)
787 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
788 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
790 pkt6_udp->udp_hdr.crc = 0;
793 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
795 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
796 tmp = (htons (pktlen) & 0xffff);
797 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
798 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
799 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
802 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
803 ntohs (pkt6_udp->udp_hdr.len));
804 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
806 else if (0x06 == protocol)
808 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
809 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
811 pkt6_tcp->tcp_hdr.crc = 0;
814 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
816 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
818 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
819 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
820 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
823 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
824 ntohs (pkt6->ip6_hdr.paylgth));
825 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
830 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
833 receive_tcp_service (void *cls __attribute__((unused)),
834 struct GNUNET_MESH_Tunnel *tunnel,
835 void **tunnel_ctx __attribute__((unused)),
836 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
837 const struct GNUNET_MessageHeader *message,
838 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
841 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
842 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
843 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
844 GNUNET_MessageHeader)
845 - sizeof (GNUNET_HashCode);
847 /** Get the configuration from the services-hashmap.
849 * Which service is needed only depends on the service-descriptor and the
852 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
854 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
855 *tcp_desc = ntohs (pkt->dpt);
856 struct redirect_service *serv =
857 GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
860 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
861 "No service found for TCP dpt %d!\n", *tcp_desc);
865 pkt->dpt = htons (serv->remote_port);
868 * At this point it would be possible to check against some kind of ACL.
874 /* Prepare the state.
875 * This will be saved in the hashmap, so that the receiving procedure knows
876 * through which tunnel this connection has to be routed.
878 struct redirect_state *state =
879 GNUNET_malloc (sizeof (struct redirect_state));
880 memset (state, 0, sizeof (struct redirect_state));
881 state->tunnel = tunnel;
883 state->type = SERVICE;
884 state->hashmap = tcp_connections;
885 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
887 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
888 sizeof (struct ip6_hdr) + pkt_len;
891 memset (buf, 0, len);
893 switch (serv->version)
896 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
897 &serv->v4.ip4address,
898 tunnel, state, (struct ip_pkt *) buf);
901 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
902 &serv->v6.ip6address,
903 tunnel, state, (struct ip6_pkt *) buf);
911 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
914 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
916 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
917 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
920 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
921 GNUNET_TIME_absolute_get ().abs_value);
923 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
924 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
929 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
934 receive_tcp_remote (void *cls __attribute__((unused)),
935 struct GNUNET_MESH_Tunnel *tunnel,
936 void **tunnel_ctx __attribute__((unused)),
937 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
938 const struct GNUNET_MessageHeader *message,
939 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
941 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
942 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
943 struct remote_addr *s = (struct remote_addr *) desc;
946 unsigned int pkt_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - sizeof (GNUNET_HashCode);
948 struct redirect_state *state =
949 GNUNET_malloc (sizeof (struct redirect_state));
950 memset (state, 0, sizeof (struct redirect_state));
951 state->tunnel = tunnel;
952 state->type = REMOTE;
953 state->hashmap = tcp_connections;
954 memcpy (&state->remote, s, sizeof (struct remote_addr));
956 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
957 sizeof (struct ip6_hdr) + pkt_len;
960 memset (buf, 0, len);
965 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
966 &s->addr, tunnel, state, (struct ip_pkt *) buf);
969 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
970 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
977 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
980 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
982 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
983 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
986 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
987 GNUNET_TIME_absolute_get ().abs_value);
989 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
991 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
996 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1002 receive_udp_remote (void *cls __attribute__((unused)),
1003 struct GNUNET_MESH_Tunnel *tunnel,
1004 void **tunnel_ctx __attribute__((unused)),
1005 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
1006 const struct GNUNET_MessageHeader *message,
1007 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
1009 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1010 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1011 struct remote_addr *s = (struct remote_addr *) desc;
1015 GNUNET_assert (ntohs (pkt->len) ==
1016 ntohs (message->size) -
1017 sizeof (struct GNUNET_MessageHeader) -
1018 sizeof (GNUNET_HashCode));
1020 /* Prepare the state.
1021 * This will be saved in the hashmap, so that the receiving procedure knows
1022 * through which tunnel this connection has to be routed.
1024 struct redirect_state *state =
1025 GNUNET_malloc (sizeof (struct redirect_state));
1026 memset (state, 0, sizeof (struct redirect_state));
1027 state->tunnel = tunnel;
1028 state->hashmap = udp_connections;
1029 state->type = REMOTE;
1030 memcpy (&state->remote, s, sizeof (struct remote_addr));
1032 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1033 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1036 memset (buf, 0, len);
1041 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1042 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1045 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1046 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1053 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1056 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1058 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1059 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1062 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1063 GNUNET_TIME_absolute_get ().abs_value);
1065 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1066 max_udp_connections)
1067 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1070 GNUNET_free (state);
1072 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1077 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1080 receive_udp_service (void *cls __attribute__((unused)),
1081 struct GNUNET_MESH_Tunnel *tunnel,
1082 void **tunnel_ctx __attribute__((unused)),
1083 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
1084 const struct GNUNET_MessageHeader *message,
1085 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
1087 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1088 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1090 GNUNET_assert (ntohs (pkt->len) ==
1091 ntohs (message->size) -
1092 sizeof (struct GNUNET_MessageHeader) -
1093 sizeof (GNUNET_HashCode));
1095 /* Get the configuration from the hashmap */
1096 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1097 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1098 *udp_desc = ntohs (pkt->dpt);
1099 struct redirect_service *serv =
1100 GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
1103 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1104 "No service found for UDP dpt %d!\n", *udp_desc);
1108 pkt->dpt = htons (serv->remote_port);
1111 * At this point it would be possible to check against some kind of ACL.
1117 /* Prepare the state.
1118 * This will be saved in the hashmap, so that the receiving procedure knows
1119 * through which tunnel this connection has to be routed.
1121 struct redirect_state *state =
1122 GNUNET_malloc (sizeof (struct redirect_state));
1123 memset (state, 0, sizeof (struct redirect_state));
1124 state->tunnel = tunnel;
1126 state->type = SERVICE;
1127 state->hashmap = udp_connections;
1128 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1130 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1131 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1134 memset (buf, 0, len);
1136 switch (serv->version)
1139 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1140 &serv->v4.ip4address,
1141 tunnel, state, (struct ip_pkt *) buf);
1144 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1145 &serv->v6.ip6address,
1146 tunnel, state, (struct ip6_pkt *) buf);
1154 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
1157 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1159 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1160 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1163 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1164 GNUNET_TIME_absolute_get ().abs_value);
1166 if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections)
1167 GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap);
1170 GNUNET_free (state);
1172 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1180 int handler_idx, app_idx;
1182 udp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_UDP");
1183 tcp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_TCP");
1185 static struct GNUNET_MESH_MessageHandler handlers[] = {
1186 {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
1187 {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
1193 static GNUNET_MESH_ApplicationType apptypes[] =
1195 GNUNET_APPLICATION_TYPE_END,
1196 GNUNET_APPLICATION_TYPE_END,
1197 GNUNET_APPLICATION_TYPE_END
1203 if (GNUNET_YES == udp)
1205 handlers[handler_idx].callback = receive_udp_remote;
1206 handlers[handler_idx].expected_size = 0;
1207 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_UDP;
1208 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1213 if (GNUNET_YES == tcp)
1215 handlers[handler_idx].callback = receive_tcp_remote;
1216 handlers[handler_idx].expected_size = 0;
1217 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_TCP;
1218 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1223 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1227 * @brief Main function that will be run by the scheduler.
1229 * @param cls closure
1230 * @param args remaining command-line arguments
1231 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1232 * @param cfg_ configuration
1236 char *const *args __attribute__((unused)),
1237 const char *cfgfile __attribute__((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1243 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1244 udp_connections_heap =
1245 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1246 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1247 tcp_connections_heap =
1248 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1249 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1250 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1252 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1253 &max_udp_connections);
1254 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1255 &max_tcp_connections);
1257 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1259 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1260 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1264 * The main function to obtain template from gnunetd.
1266 * @param argc number of arguments from the command line
1267 * @param argv command line arguments
1268 * @return 0 ok, 1 on error
1271 main (int argc, char *const *argv) {
1272 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1273 GNUNET_GETOPT_OPTION_END
1276 return (GNUNET_OK ==
1277 GNUNET_PROGRAM_run (argc,
1280 gettext_noop ("help text"),
1281 options, &run, NULL)) ? ret : 1;
1284 /* end of gnunet-daemon-exit.c */