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"
40 * The handle to the configuration used throughout the process
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45 * The handle to the helper
47 struct GNUNET_VPN_HELPER_Handle *helper_handle;
57 static struct GNUNET_MESH_Handle *mesh_handle;
60 * This hashmaps contains the mapping from peer, service-descriptor,
61 * source-port and destination-port to a struct redirect_state
63 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
64 static struct GNUNET_CONTAINER_Heap *udp_connections_heap;
65 static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
66 static struct GNUNET_CONTAINER_Heap *tcp_connections_heap;
69 * If there are at least this many udp-Connections, old ones will be removed
71 static long long unsigned int max_udp_connections = 200;
74 * If there are at least this many tcp-Connections, old ones will be removed
76 static long long unsigned int max_tcp_connections = 200;
81 unsigned char addr[16];
86 * This struct is saved into the services-hashmap
88 struct redirect_service
113 * The source-address of this connection. When a packet to this address is
114 * received, this tunnel is used to forward it. ipv4-addresses will be put
115 * here left-aligned */
118 * The source-port of this connection
124 * This struct is saved into {tcp,udp}_connections;
126 struct redirect_state
128 struct GNUNET_MESH_Tunnel *tunnel;
129 GNUNET_HashCode desc;
130 struct redirect_service *serv;
131 struct remote_addr remote;
133 struct GNUNET_CONTAINER_HeapNode* heap_node;
134 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
135 GNUNET_HashCode hash;
137 enum { SERVICE, REMOTE } type;
140 * The source-address and -port of this connection
142 struct redirect_info redirect_info;
146 * This hashmaps saves interesting things about the configured services
148 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
149 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
152 * Function that frees everything from a hashmap
155 free_iterate(void* cls __attribute__((unused)), const GNUNET_HashCode* hash __attribute__((unused)), void* value)
162 * Function scheduled as very last function, cleans up after us
165 cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
166 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
168 GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
172 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
176 if (mesh_handle != NULL)
178 GNUNET_MESH_disconnect(mesh_handle);
184 collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
185 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
189 struct GNUNET_CONTAINER_Heap *heap = cls;
191 struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap);
193 /* This is free()ed memory! */
194 state->heap_node = NULL;
196 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
198 GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state);
204 hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
207 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
209 * build the hash out of the last bytes of the address and the 2 bytes of
212 memcpy(hash, &u_i->pt, sizeof(u_i->pt));
213 memcpy(((unsigned char*)hash)+2, u_i->addr+(addrlen-(sizeof(unsigned int) - 2)), (sizeof(unsigned int) - 2));
214 memset(((unsigned char*)hash)+sizeof(unsigned int), 0, sizeof(GNUNET_HashCode) - sizeof(unsigned int));
218 * cls is the pointer to a GNUNET_MessageHeader that is
219 * followed by the service-descriptor and the udp-packet that should be sent;
222 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
224 struct GNUNET_MessageHeader *hdr = cls;
225 GNUNET_assert (size >= ntohs (hdr->size));
226 memcpy (buf, hdr, ntohs (hdr->size));
227 size = ntohs(hdr->size);
233 * @brief Handles an UDP-Packet received from the helper.
235 * @param udp A pointer to the Packet
236 * @param dadr The IP-Destination-address
237 * @param addrlen The length of the address
238 * @param version 4 or 6
241 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
243 struct redirect_info u_i;
244 struct GNUNET_MESH_Tunnel *tunnel;
246 struct GNUNET_MessageHeader *msg;
248 memset (&u_i, 0, sizeof (struct redirect_info));
250 memcpy (&u_i.addr, dadr, addrlen);
254 /* get tunnel and service-descriptor from this */
255 GNUNET_HashCode hash;
256 hash_redirect_info(&hash, &u_i, addrlen);
258 struct redirect_state *state =
259 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
261 /* Mark this connection as freshly used */
262 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
263 GNUNET_TIME_absolute_get ().abs_value);
265 tunnel = state->tunnel;
267 if (state->type == SERVICE)
269 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
270 if (ntohs (udp->spt) == state->serv->remote_port)
272 udp->spt = htons (state->serv->my_port);
276 /* otherwise the answer came from a different port (tftp does this)
277 * add this new port to the list of all services, so that the packets
278 * coming back from the client to this new port will be routed correctly
280 struct redirect_service *serv =
281 GNUNET_malloc (sizeof (struct redirect_service));
282 memcpy (serv, state->serv, sizeof (struct redirect_service));
283 serv->my_port = ntohs (udp->spt);
284 serv->remote_port = ntohs (udp->spt);
285 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
286 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
287 sizeof (GNUNET_HashCode));
288 *desc = ntohs (udp->spt);
289 GNUNET_assert (GNUNET_OK ==
290 GNUNET_CONTAINER_multihashmap_put (udp_services,
291 (GNUNET_HashCode*)desc, serv,
292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
298 /* send udp-packet back */
300 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
302 msg = GNUNET_malloc (len);
303 msg->size = htons (len);
304 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK);
305 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
306 if (state->type == SERVICE)
307 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
309 memcpy (desc, &state->remote, sizeof (struct remote_addr));
310 void *_udp = desc + 1;
311 memcpy (_udp, udp, ntohs (udp->len));
313 GNUNET_MESH_notify_transmit_ready (tunnel,
316 GNUNET_TIME_relative_divide
317 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
318 (const struct GNUNET_PeerIdentity *)
320 send_udp_to_peer_notify_callback, msg);
324 * @brief Handles a TCP-Packet received from the helper.
326 * @param tcp A pointer to the Packet
327 * @param dadr The IP-Destination-address
328 * @param addrlen The length of the address
329 * @param version 4 or 6
330 * @param pktlen the length of the packet, including its header
333 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
336 struct redirect_info u_i;
337 struct GNUNET_MESH_Tunnel *tunnel;
339 struct GNUNET_MessageHeader *msg;
341 memset (&u_i, 0, sizeof (struct redirect_info));
343 memcpy (&u_i.addr, dadr, addrlen);
346 /* get tunnel and service-descriptor from this */
347 GNUNET_HashCode hash;
348 hash_redirect_info(&hash, &u_i, addrlen);
350 struct redirect_state *state =
351 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
353 if (state == NULL) return;
355 /* Mark this connection as freshly used */
356 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
357 GNUNET_TIME_absolute_get ().abs_value);
359 tunnel = state->tunnel;
361 if (state->type == SERVICE)
363 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
364 if (ntohs (tcp->spt) == state->serv->remote_port)
366 tcp->spt = htons (state->serv->my_port);
370 // This is an illegal packet.
375 /* send tcp-packet back */
377 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
378 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
379 msg = GNUNET_malloc (len);
380 msg->size = htons (len);
381 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK);
382 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
383 if (state->type == SERVICE)
384 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
386 memcpy (desc, &state->remote, sizeof (struct remote_addr));
387 void *_tcp = desc + 1;
388 memcpy (_tcp, tcp, pktlen);
390 GNUNET_MESH_notify_transmit_ready (tunnel,
393 GNUNET_TIME_relative_divide
394 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
395 (const struct GNUNET_PeerIdentity *)NULL,
396 len, send_udp_to_peer_notify_callback,
402 * Receive packets from the helper-process
405 message_token (void *cls __attribute__((unused)),
406 void *client __attribute__((unused)), const struct GNUNET_MessageHeader *message)
408 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
410 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
412 /* ethertype is ipv6 */
413 if (ntohs (pkt_tun->tun.type) == 0x86dd)
415 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
416 if (0x11 == pkt6->ip6_hdr.nxthdr)
417 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
418 (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
419 else if (0x06 == pkt6->ip6_hdr.nxthdr)
420 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
421 (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
422 ntohs (pkt6->ip6_hdr.paylgth));
424 else if (ntohs (pkt_tun->tun.type) == 0x0800)
426 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
427 uint32_t tmp = pkt4->ip_hdr.dadr;
428 if (0x11 == pkt4->ip_hdr.proto)
429 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
430 (unsigned char *) &tmp, 4);
431 else if (0x06 == pkt4->ip_hdr.proto)
433 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
435 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
437 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
438 (unsigned char *) &tmp, 4, pktlen);
448 * Reads the configuration servicecfg and populates udp_services
451 * @param section name of section in config, equal to hostname
454 read_service_conf (void *cls __attribute__((unused)), const char *section)
456 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
459 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n", strlen(section), section, section + (strlen(section) - 8));
465 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
466 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
467 (GNUNET_HashCode *) (desc + 1));
476 if (proto == UDP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "UDP_REDIRECTS", &cpy)))
478 else if (proto == TCP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "TCP_REDIRECTS", &cpy)))
481 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
484 if (NULL == (hostname = strstr (redirect, ":")))
486 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
492 if (NULL == (hostport = strstr (hostname, ":")))
494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
501 int local_port = atoi (redirect);
502 if (!((local_port > 0) && (local_port < 65536)))
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
508 struct redirect_service *serv =
509 GNUNET_malloc (sizeof (struct redirect_service));
510 memset (serv, 0, sizeof (struct redirect_service));
511 serv->my_port = local_port;
513 if (0 == strcmp ("localhost4", hostname))
518 GNUNET_assert (GNUNET_OK ==
519 GNUNET_CONFIGURATION_get_value_string (cfg,
524 inet_pton (AF_INET, ip4addr,
525 serv->v4.ip4address));
526 GNUNET_free (ip4addr);
528 else if (0 == strcmp ("localhost6", hostname))
533 GNUNET_assert (GNUNET_OK ==
534 GNUNET_CONFIGURATION_get_value_string (cfg,
539 inet_pton (AF_INET6, ip6addr,
540 serv->v6.ip6address));
541 GNUNET_free (ip6addr);
545 // TODO Lookup, yadayadayada
548 serv->remote_port = atoi (hostport);
550 GNUNET_assert (GNUNET_OK ==
551 GNUNET_CONTAINER_multihashmap_put (udp_services,
552 (GNUNET_HashCode*)desc, serv,
553 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
555 GNUNET_assert (GNUNET_OK ==
556 GNUNET_CONTAINER_multihashmap_put (tcp_services,
557 (GNUNET_HashCode*)desc, serv,
558 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
563 proto = (proto == UDP) ? TCP : UDP;
565 while (proto != UDP);
569 * Start the helper-process
571 * If cls != NULL it is assumed that this function is called as a result of a dying
572 * helper. cls is then taken as handle to the old helper and is cleaned up.
575 start_helper_and_schedule(void *cls,
576 const struct GNUNET_SCHEDULER_TaskContext *tc) {
577 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
590 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
592 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
596 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
598 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
602 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
604 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
608 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
610 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
614 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
616 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
621 * Messages get passed to the function message_token
622 * When the helper dies, this function will be called again with the
623 * helper_handle as cls.
625 helper_handle = start_helper(ifname,
631 start_helper_and_schedule,
635 GNUNET_free(ipv6addr);
636 GNUNET_free(ipv6prefix);
637 GNUNET_free(ipv4addr);
638 GNUNET_free(ipv4mask);
643 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
644 uint16_t protocol, void *ipaddress, void *tunnel,
645 struct redirect_state *state, struct ip_pkt *pkt4)
649 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
650 pkt4->shdr.size = htons (len);
652 pkt4->tun.type = htons (0x0800);
654 memcpy (&pkt4->data, payload, pktlen);
656 pkt4->ip_hdr.version = 4;
657 pkt4->ip_hdr.hdr_lngth = 5;
658 pkt4->ip_hdr.diff_serv = 0;
659 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
660 pkt4->ip_hdr.ident = 0;
661 pkt4->ip_hdr.flags = 0;
662 pkt4->ip_hdr.frag_off = 0;
663 pkt4->ip_hdr.ttl = 255;
664 pkt4->ip_hdr.proto = protocol;
665 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
667 memcpy (&tmp, ipaddress, 4);
668 pkt4->ip_hdr.dadr = tmp;
670 /* Generate a new src-address */
673 GNUNET_assert (GNUNET_OK ==
674 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
677 GNUNET_assert (GNUNET_OK ==
678 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
681 inet_pton (AF_INET, ipv4addr, &tmp);
682 inet_pton (AF_INET, ipv4mask, &tmp2);
683 GNUNET_free (ipv4addr);
684 GNUNET_free (ipv4mask);
686 /* This should be a noop */
689 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
691 pkt4->ip_hdr.sadr = tmp;
693 memcpy (&state->redirect_info.addr, &tmp, 4);
694 if (0x11 == protocol)
696 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
697 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
699 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
701 else if (0x06 == protocol)
703 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
704 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
706 pkt4_tcp->tcp_hdr.crc = 0;
708 tmp = pkt4->ip_hdr.sadr;
710 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
711 tmp = pkt4->ip_hdr.dadr;
713 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
715 tmp = (protocol << 16) | (0xffff & pktlen);
717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
721 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
724 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
725 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
729 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
733 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
734 uint16_t protocol, void *ipaddress, void *tunnel,
735 struct redirect_state *state, struct ip6_pkt *pkt6)
739 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
740 pkt6->shdr.size = htons (len);
743 pkt6->tun.type = htons (0x86dd);
745 memcpy (&pkt6->data, payload, pktlen);
747 pkt6->ip6_hdr.version = 6;
748 pkt6->ip6_hdr.nxthdr = protocol;
749 pkt6->ip6_hdr.paylgth = htons (pktlen);
750 pkt6->ip6_hdr.hoplmt = 64;
752 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
754 /* Generate a new src-address
755 * This takes as much from the address of the tunnel as fits into
758 unsigned long long ipv6prefix;
759 GNUNET_assert (GNUNET_OK ==
760 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
763 GNUNET_assert (GNUNET_OK ==
764 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
767 GNUNET_assert (ipv6prefix < 127);
768 ipv6prefix = (ipv6prefix + 7) / 8;
770 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
771 GNUNET_free (ipv6addr);
773 if (ipv6prefix < (16 - sizeof (void *)))
774 ipv6prefix = 16 - sizeof (void *);
776 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
777 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
778 ((char *) &tunnel) + offset, 16 - ipv6prefix);
780 /* copy the needed information into the state */
781 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
783 if (0x11 == protocol)
785 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
786 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
788 pkt6_udp->udp_hdr.crc = 0;
791 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
793 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
794 tmp = (htons (pktlen) & 0xffff);
795 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
796 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
797 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
800 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
801 ntohs (pkt6_udp->udp_hdr.len));
802 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
804 else if (0x06 == protocol)
806 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
807 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
809 pkt6_tcp->tcp_hdr.crc = 0;
812 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
814 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
816 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
817 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
818 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
821 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
822 ntohs (pkt6->ip6_hdr.paylgth));
823 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
828 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
831 receive_tcp_service (void *cls __attribute__((unused)),
832 struct GNUNET_MESH_Tunnel *tunnel,
833 void **tunnel_ctx __attribute__((unused)),
834 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
835 const struct GNUNET_MessageHeader *message,
836 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
839 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
840 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
841 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
842 GNUNET_MessageHeader)
843 - sizeof (GNUNET_HashCode);
845 /** Get the configuration from the services-hashmap.
847 * Which service is needed only depends on the service-descriptor and the
850 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
852 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
853 *tcp_desc = ntohs (pkt->dpt);
854 struct redirect_service *serv =
855 GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
858 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
859 "No service found for TCP dpt %d!\n", *tcp_desc);
863 pkt->dpt = htons (serv->remote_port);
866 * At this point it would be possible to check against some kind of ACL.
872 /* Prepare the state.
873 * This will be saved in the hashmap, so that the receiving procedure knows
874 * through which tunnel this connection has to be routed.
876 struct redirect_state *state =
877 GNUNET_malloc (sizeof (struct redirect_state));
878 memset (state, 0, sizeof (struct redirect_state));
879 state->tunnel = tunnel;
881 state->type = SERVICE;
882 state->hashmap = tcp_connections;
883 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
885 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
886 sizeof (struct ip6_hdr) + pkt_len;
889 memset (buf, 0, len);
891 switch (serv->version)
894 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
895 &serv->v4.ip4address,
896 tunnel, state, (struct ip_pkt *) buf);
899 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
900 &serv->v6.ip6address,
901 tunnel, state, (struct ip6_pkt *) buf);
909 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
912 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
914 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
915 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
918 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
919 GNUNET_TIME_absolute_get ().abs_value);
921 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
922 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
927 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
932 receive_tcp_remote (void *cls __attribute__((unused)),
933 struct GNUNET_MESH_Tunnel *tunnel,
934 void **tunnel_ctx __attribute__((unused)),
935 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
936 const struct GNUNET_MessageHeader *message,
937 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
939 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
940 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
941 struct remote_addr *s = (struct remote_addr *) desc;
944 unsigned int pkt_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - sizeof (GNUNET_HashCode);
946 struct redirect_state *state =
947 GNUNET_malloc (sizeof (struct redirect_state));
948 memset (state, 0, sizeof (struct redirect_state));
949 state->tunnel = tunnel;
950 state->type = REMOTE;
951 state->hashmap = tcp_connections;
952 memcpy (&state->remote, s, sizeof (struct remote_addr));
954 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
955 sizeof (struct ip6_hdr) + pkt_len;
958 memset (buf, 0, len);
963 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
964 &s->addr, tunnel, state, (struct ip_pkt *) buf);
967 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
968 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
975 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
978 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
980 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
981 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
984 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
985 GNUNET_TIME_absolute_get ().abs_value);
987 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
989 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
994 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1000 receive_udp_remote (void *cls __attribute__((unused)),
1001 struct GNUNET_MESH_Tunnel *tunnel,
1002 void **tunnel_ctx __attribute__((unused)),
1003 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
1004 const struct GNUNET_MessageHeader *message,
1005 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
1007 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1008 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1009 struct remote_addr *s = (struct remote_addr *) desc;
1013 GNUNET_assert (ntohs (pkt->len) ==
1014 ntohs (message->size) -
1015 sizeof (struct GNUNET_MessageHeader) -
1016 sizeof (GNUNET_HashCode));
1018 /* Prepare the state.
1019 * This will be saved in the hashmap, so that the receiving procedure knows
1020 * through which tunnel this connection has to be routed.
1022 struct redirect_state *state =
1023 GNUNET_malloc (sizeof (struct redirect_state));
1024 memset (state, 0, sizeof (struct redirect_state));
1025 state->tunnel = tunnel;
1026 state->hashmap = udp_connections;
1027 state->type = REMOTE;
1028 memcpy (&state->remote, s, sizeof (struct remote_addr));
1030 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1031 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1034 memset (buf, 0, len);
1039 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1040 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1043 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1044 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1051 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1054 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1056 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1057 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1060 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1061 GNUNET_TIME_absolute_get ().abs_value);
1063 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1064 max_udp_connections)
1065 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1068 GNUNET_free (state);
1070 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1075 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1078 receive_udp_service (void *cls __attribute__((unused)),
1079 struct GNUNET_MESH_Tunnel *tunnel,
1080 void **tunnel_ctx __attribute__((unused)),
1081 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
1082 const struct GNUNET_MessageHeader *message,
1083 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
1085 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1086 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1088 GNUNET_assert (ntohs (pkt->len) ==
1089 ntohs (message->size) -
1090 sizeof (struct GNUNET_MessageHeader) -
1091 sizeof (GNUNET_HashCode));
1093 /* Get the configuration from the hashmap */
1094 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1095 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1096 *udp_desc = ntohs (pkt->dpt);
1097 struct redirect_service *serv =
1098 GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
1101 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1102 "No service found for UDP dpt %d!\n", *udp_desc);
1106 pkt->dpt = htons (serv->remote_port);
1109 * At this point it would be possible to check against some kind of ACL.
1115 /* Prepare the state.
1116 * This will be saved in the hashmap, so that the receiving procedure knows
1117 * through which tunnel this connection has to be routed.
1119 struct redirect_state *state =
1120 GNUNET_malloc (sizeof (struct redirect_state));
1121 memset (state, 0, sizeof (struct redirect_state));
1122 state->tunnel = tunnel;
1124 state->type = SERVICE;
1125 state->hashmap = udp_connections;
1126 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1128 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1129 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1132 memset (buf, 0, len);
1134 switch (serv->version)
1137 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1138 &serv->v4.ip4address,
1139 tunnel, state, (struct ip_pkt *) buf);
1142 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1143 &serv->v6.ip6address,
1144 tunnel, state, (struct ip6_pkt *) buf);
1152 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
1155 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1157 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1158 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1161 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1162 GNUNET_TIME_absolute_get ().abs_value);
1164 if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections)
1165 GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap);
1168 GNUNET_free (state);
1170 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1178 int handler_idx, app_idx;
1180 udp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_UDP");
1181 tcp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_TCP");
1183 static struct GNUNET_MESH_MessageHandler handlers[] = {
1184 {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
1185 {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
1191 static GNUNET_MESH_ApplicationType apptypes[] =
1193 GNUNET_APPLICATION_TYPE_END,
1194 GNUNET_APPLICATION_TYPE_END,
1195 GNUNET_APPLICATION_TYPE_END
1201 if (GNUNET_YES == udp)
1203 handlers[handler_idx].callback = receive_udp_remote;
1204 handlers[handler_idx].expected_size = 0;
1205 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_UDP;
1206 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1211 if (GNUNET_YES == tcp)
1213 handlers[handler_idx].callback = receive_tcp_remote;
1214 handlers[handler_idx].expected_size = 0;
1215 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_TCP;
1216 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1221 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1225 * @brief Main function that will be run by the scheduler.
1227 * @param cls closure
1228 * @param args remaining command-line arguments
1229 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1230 * @param cfg_ configuration
1234 char *const *args __attribute__((unused)),
1235 const char *cfgfile __attribute__((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1241 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1242 udp_connections_heap =
1243 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1244 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1245 tcp_connections_heap =
1246 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1247 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1248 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1250 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1251 &max_udp_connections);
1252 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1253 &max_tcp_connections);
1255 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1257 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1258 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1262 * The main function to obtain template from gnunetd.
1264 * @param argc number of arguments from the command line
1265 * @param argv command line arguments
1266 * @return 0 ok, 1 on error
1269 main (int argc, char *const *argv) {
1270 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1271 GNUNET_GETOPT_OPTION_END
1274 return (GNUNET_OK ==
1275 GNUNET_PROGRAM_run (argc,
1278 gettext_noop ("help text"),
1279 options, &run, NULL)) ? ret : 1;
1282 /* end of gnunet-daemon-exit.c */