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_mesh_service.h>
31 #include <gnunet_constants.h>
34 #include "gnunet-vpn-packet.h"
35 #include "gnunet-helper-vpn-api.h"
36 #include "gnunet-vpn-checksum.h"
39 * The handle to the configuration used throughout the process
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * The handle to the service-configuration
46 static struct GNUNET_CONFIGURATION_Handle *servicecfg;
49 * The handle to the helper
51 struct GNUNET_VPN_HELPER_Handle *helper_handle;
61 static struct GNUNET_MESH_Handle *mesh_handle;
64 * This hashmaps contains the mapping from peer, service-descriptor,
65 * source-port and destination-port to a struct redirect_state
67 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
68 static struct GNUNET_CONTAINER_Heap *udp_connections_heap;
69 static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
70 static struct GNUNET_CONTAINER_Heap *tcp_connections_heap;
73 * If there are at least this many udp-Connections, old ones will be removed
75 static long long unsigned int max_udp_connections = 200;
78 * If there are at least this many tcp-Connections, old ones will be removed
80 static long long unsigned int max_tcp_connections = 200;
83 * This struct is saved into the services-hashmap
85 struct redirect_service
110 * The source-address of this connection. When a packet to this address is
111 * received, this tunnel is used to forward it. ipv4-addresses will be put
112 * here left-aligned */
115 * The source-port of this connection
121 * This struct is saved into {tcp,udp}_connections;
123 struct redirect_state
125 struct GNUNET_MESH_Tunnel *tunnel;
126 GNUNET_HashCode desc;
127 struct redirect_service *serv;
129 struct GNUNET_CONTAINER_HeapNode* heap_node;
130 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
131 GNUNET_HashCode hash;
134 * The source-address and -port of this connection
136 struct redirect_info redirect_info;
140 * This hashmaps saves interesting things about the configured services
142 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
143 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
146 * Function that frees everything from a hashmap
149 free_iterate(void* cls, const GNUNET_HashCode* hash, void* value)
156 * Function scheduled as very last function, cleans up after us
159 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
160 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
162 GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
166 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
170 if (mesh_handle != NULL)
172 GNUNET_MESH_disconnect(mesh_handle);
178 collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
179 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
183 struct GNUNET_CONTAINER_Heap *heap = cls;
185 struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap);
187 /* This is free()ed memory! */
188 state->heap_node = NULL;
190 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
192 GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state);
198 hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
201 /* the gnunet hashmap only uses the first 32bit of the hash
203 * build the hash out of the last two bytes of the address and the 2 bytes of
206 memcpy(&hash, &u_i->pt, sizeof(u_i->pt));
207 memcpy(((unsigned char*)&hash)+2, u_i->addr+(addrlen-2), 2);
211 * cls is the pointer to a GNUNET_MessageHeader that is
212 * followed by the service-descriptor and the udp-packet that should be sent;
215 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
217 struct GNUNET_MessageHeader *hdr = cls;
218 GNUNET_assert (size >= ntohs (hdr->size));
219 memcpy (buf, hdr, ntohs (hdr->size));
220 size = ntohs(hdr->size);
226 * @brief Handles an UDP-Packet received from the helper.
228 * @param udp A pointer to the Packet
229 * @param dadr The IP-Destination-address
230 * @param addrlen The length of the address
231 * @param version 4 or 6
234 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen,
235 unsigned int version)
237 struct redirect_info u_i;
238 struct GNUNET_MESH_Tunnel *tunnel;
240 struct GNUNET_MessageHeader *msg;
242 memset (&u_i, 0, sizeof (struct redirect_info));
244 memcpy (&u_i.addr, dadr, addrlen);
248 /* get tunnel and service-descriptor from this */
249 GNUNET_HashCode hash;
250 hash_redirect_info(&hash, &u_i, addrlen);
252 struct redirect_state *state =
253 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
255 /* Mark this connection as freshly used */
256 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
257 GNUNET_TIME_absolute_get ().abs_value);
259 tunnel = state->tunnel;
261 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
262 if (ntohs (udp->spt) == state->serv->remote_port)
264 udp->spt = htons (state->serv->my_port);
268 struct redirect_service *serv =
269 GNUNET_malloc (sizeof (struct redirect_service));
270 memcpy (serv, state->serv, sizeof (struct redirect_service));
271 serv->my_port = ntohs (udp->spt);
272 serv->remote_port = ntohs (udp->spt);
273 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
274 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
275 sizeof (GNUNET_HashCode));
276 *desc = ntohs (udp->spt);
277 GNUNET_assert (GNUNET_OK ==
278 GNUNET_CONTAINER_multihashmap_put (udp_services,
279 (GNUNET_HashCode*)desc, serv,
280 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
284 /* send udp-packet back */
286 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
288 msg = GNUNET_malloc (len);
289 msg->size = htons (len);
290 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK);
291 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
292 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
293 void *_udp = desc + 1;
294 memcpy (_udp, udp, ntohs (udp->len));
296 GNUNET_MESH_notify_transmit_ready (tunnel,
299 GNUNET_TIME_relative_divide
300 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
301 (const struct GNUNET_PeerIdentity *)
303 send_udp_to_peer_notify_callback, msg);
307 * @brief Handles a TCP-Packet received from the helper.
309 * @param tcp A pointer to the Packet
310 * @param dadr The IP-Destination-address
311 * @param addrlen The length of the address
312 * @param version 4 or 6
313 * @param pktlen the length of the packet, including its header
316 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
317 unsigned int version, size_t pktlen)
319 struct redirect_info u_i;
320 struct GNUNET_MESH_Tunnel *tunnel;
322 struct GNUNET_MessageHeader *msg;
324 memset (&u_i, 0, sizeof (struct redirect_info));
326 memcpy (&u_i.addr, dadr, addrlen);
329 /* get tunnel and service-descriptor from this */
330 GNUNET_HashCode hash;
331 hash_redirect_info(&hash, &u_i, addrlen);
333 struct redirect_state *state =
334 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
336 /* Mark this connection as freshly used */
337 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
338 GNUNET_TIME_absolute_get ().abs_value);
340 tunnel = state->tunnel;
342 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
343 if (ntohs (tcp->spt) == state->serv->remote_port)
345 tcp->spt = htons (state->serv->my_port);
349 // This is an illegal packet.
352 /* send tcp-packet back */
354 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
355 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
356 msg = GNUNET_malloc (len);
357 msg->size = htons (len);
358 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK);
359 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
360 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
361 void *_tcp = desc + 1;
362 memcpy (_tcp, tcp, pktlen);
364 GNUNET_MESH_notify_transmit_ready (tunnel,
367 GNUNET_TIME_relative_divide
368 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
369 (const struct GNUNET_PeerIdentity *)NULL,
370 len, send_udp_to_peer_notify_callback,
376 * Receive packets from the helper-process
379 message_token (void *cls,
380 void *client, const struct GNUNET_MessageHeader *message)
382 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
384 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
386 /* ethertype is ipv6 */
387 if (ntohs (pkt_tun->tun.type) == 0x86dd)
389 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
390 if (0x11 == pkt6->ip6_hdr.nxthdr)
391 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
392 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6);
393 else if (0x06 == pkt6->ip6_hdr.nxthdr)
394 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
395 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6,
396 ntohs (pkt6->ip6_hdr.paylgth));
398 else if (ntohs (pkt_tun->tun.type) == 0x0800)
400 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
401 uint32_t tmp = pkt4->ip_hdr.dadr;
402 if (0x11 == pkt4->ip_hdr.proto)
403 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
404 (unsigned char *) &tmp, 4, 4);
405 else if (0x06 == pkt4->ip_hdr.proto)
407 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
409 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
411 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
412 (unsigned char *) &tmp, 4, 4, pktlen);
422 * Reads the configuration servicecfg and populates udp_services
425 * @param section name of section in config, equal to hostname
426 * @param option type of redirect
427 * @param value specification of services, format is
428 * "OFFERED-PORT:HOSTNAME:HOST-PORT" (SPACE <more of those>)*
431 read_service_conf (void *cls, const char *section, const char *option,
438 GNUNET_HashCode hash;
439 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
440 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
441 (GNUNET_HashCode *) (desc + 1));
447 if (0 == strcmp ("UDP_REDIRECTS", option))
449 else if (0 == strcmp ("TCP_REDIRECTS", option))
456 cpy = GNUNET_strdup (value);
457 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
460 if (NULL == (hostname = strstr (redirect, ":")))
462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
468 if (NULL == (hostport = strstr (hostname, ":")))
470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
477 int local_port = atoi (redirect);
478 if (!((local_port > 0) && (local_port < 65536)))
479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
484 struct redirect_service *serv =
485 GNUNET_malloc (sizeof (struct redirect_service));
486 memset (serv, 0, sizeof (struct redirect_service));
487 serv->my_port = local_port;
489 if (0 == strcmp ("localhost4", hostname))
494 GNUNET_assert (GNUNET_OK ==
495 GNUNET_CONFIGURATION_get_value_string (cfg,
500 inet_pton (AF_INET, ip4addr,
501 serv->v4.ip4address));
502 GNUNET_free (ip4addr);
504 else if (0 == strcmp ("localhost6", hostname))
509 GNUNET_assert (GNUNET_OK ==
510 GNUNET_CONFIGURATION_get_value_string (cfg,
515 inet_pton (AF_INET6, ip6addr,
516 serv->v6.ip6address));
517 GNUNET_free (ip6addr);
521 // TODO Lookup, yadayadayada
524 serv->remote_port = atoi (hostport);
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
526 *((unsigned long long *) (desc + 1)));
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key2 %x\n",
528 *((unsigned long long *) &hash));
530 GNUNET_assert (GNUNET_OK ==
531 GNUNET_CONTAINER_multihashmap_put (udp_services,
532 (GNUNET_HashCode*)desc, serv,
533 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
535 GNUNET_assert (GNUNET_OK ==
536 GNUNET_CONTAINER_multihashmap_put (tcp_services,
537 (GNUNET_HashCode*)desc, serv,
538 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
546 * Start the helper-process
548 * If cls != NULL it is assumed that this function is called as a result of a dying
549 * helper. cls is then taken as handle to the old helper and is cleaned up.
552 start_helper_and_schedule(void *cls,
553 const struct GNUNET_SCHEDULER_TaskContext *tc) {
554 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
567 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
569 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
573 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
575 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
579 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
581 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
585 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
587 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
591 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
593 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
598 * Messages get passed to the function message_token
599 * When the helper dies, this function will be called again with the
600 * helper_handle as cls.
602 helper_handle = start_helper(ifname,
608 start_helper_and_schedule,
613 GNUNET_free(ipv6addr);
614 GNUNET_free(ipv6prefix);
615 GNUNET_free(ipv4addr);
616 GNUNET_free(ipv4mask);
621 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
622 uint16_t protocol, void *ipaddress, void *tunnel,
623 struct redirect_state *state, struct ip_pkt *pkt4)
627 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
628 pkt4->shdr.size = htons (len);
630 pkt4->tun.type = htons (0x0800);
632 memcpy (&pkt4->data, payload, pktlen);
634 pkt4->ip_hdr.version = 4;
635 pkt4->ip_hdr.hdr_lngth = 5;
636 pkt4->ip_hdr.diff_serv = 0;
637 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
638 pkt4->ip_hdr.ident = 0;
639 pkt4->ip_hdr.flags = 0;
640 pkt4->ip_hdr.frag_off = 0;
641 pkt4->ip_hdr.ttl = 255;
642 pkt4->ip_hdr.proto = protocol;
643 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
645 memcpy (&tmp, ipaddress, 4);
646 pkt4->ip_hdr.dadr = tmp;
648 /* Generate a new src-address */
651 GNUNET_assert (GNUNET_OK ==
652 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
655 GNUNET_assert (GNUNET_OK ==
656 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
659 inet_pton (AF_INET, ipv4addr, &tmp);
660 inet_pton (AF_INET, ipv4mask, &tmp2);
661 GNUNET_free (ipv4addr);
662 GNUNET_free (ipv4mask);
664 /* This should be a noop */
667 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
669 pkt4->ip_hdr.sadr = tmp;
671 memcpy (&state->redirect_info.addr, &tmp, 4);
672 if (0x11 == protocol)
674 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
675 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
677 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
679 else if (0x06 == protocol)
681 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
682 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
684 pkt4_tcp->tcp_hdr.crc = 0;
686 tmp = pkt4->ip_hdr.sadr;
688 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
689 tmp = pkt4->ip_hdr.dadr;
691 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
693 tmp = (protocol << 16) | (0xffff & pktlen);
695 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
699 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
702 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
703 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
707 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
711 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
712 uint16_t protocol, void *ipaddress, void *tunnel,
713 struct redirect_state *state, struct ip6_pkt *pkt6)
717 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
718 pkt6->shdr.size = htons (len);
721 pkt6->tun.type = htons (0x86dd);
723 memcpy (&pkt6->data, payload, pktlen);
725 pkt6->ip6_hdr.version = 6;
726 pkt6->ip6_hdr.nxthdr = protocol;
727 pkt6->ip6_hdr.paylgth = htons (pktlen);
728 pkt6->ip6_hdr.hoplmt = 64;
730 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
732 /* Generate a new src-address
733 * This takes as much from the address of the tunnel as fits into
736 unsigned long long ipv6prefix;
737 GNUNET_assert (GNUNET_OK ==
738 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
741 GNUNET_assert (GNUNET_OK ==
742 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
745 GNUNET_assert (ipv6prefix < 127);
746 ipv6prefix = (ipv6prefix + 7) / 8;
748 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
749 GNUNET_free (ipv6addr);
751 if (ipv6prefix < (16 - sizeof (void *)))
752 ipv6prefix = 16 - sizeof (void *);
754 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
755 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
756 ((char *) &tunnel) + offset, 16 - ipv6prefix);
758 /* copy the needed information into the state */
759 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
761 if (0x11 == protocol)
763 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
764 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
766 pkt6_udp->udp_hdr.crc = 0;
769 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
771 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
772 tmp = (htons (pktlen) & 0xffff);
773 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
774 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
775 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
778 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
779 ntohs (pkt6_udp->udp_hdr.len));
780 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
782 else if (0x06 == protocol)
784 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
785 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
787 pkt6_tcp->tcp_hdr.crc = 0;
790 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
792 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
794 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
795 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
796 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
799 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
800 ntohs (pkt6->ip6_hdr.paylgth));
801 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
806 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
809 receive_tcp_service (void *cls,
810 struct GNUNET_MESH_Tunnel *tunnel,
812 const struct GNUNET_PeerIdentity *sender,
813 const struct GNUNET_MessageHeader *message,
814 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
817 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
818 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
819 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
820 GNUNET_MessageHeader)
821 - sizeof (GNUNET_HashCode);
823 /** Get the configuration from the services-hashmap.
825 * Which service is needed only depends on the service-descriptor and the
828 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
830 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
831 *tcp_desc = ntohs (pkt->dpt);
832 struct redirect_service *serv =
833 GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
836 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
837 "No service found for TCP dpt %d!\n", *tcp_desc);
841 pkt->dpt = htons (serv->remote_port);
844 * At this point it would be possible to check against some kind of ACL.
850 /* Prepare the state.
851 * This will be saved in the hashmap, so that the receiving procedure knows
852 * through which tunnel this connection has to be routed.
854 struct redirect_state *state =
855 GNUNET_malloc (sizeof (struct redirect_state));
856 memset (state, 0, sizeof (struct redirect_state));
857 state->tunnel = tunnel;
859 state->hashmap = tcp_connections;
860 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
862 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
863 sizeof (struct ip6_hdr) + pkt_len;
866 memset (buf, 0, len);
868 switch (serv->version)
871 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
872 &serv->v4.ip4address,
873 tunnel, state, (struct ip_pkt *) buf);
876 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
877 &serv->v6.ip6address,
878 tunnel, state, (struct ip6_pkt *) buf);
886 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
889 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
891 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
892 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
895 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
896 GNUNET_TIME_absolute_get ().abs_value);
898 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
899 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
904 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
909 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
912 receive_udp_service (void *cls,
913 struct GNUNET_MESH_Tunnel *tunnel,
915 const struct GNUNET_PeerIdentity *sender,
916 const struct GNUNET_MessageHeader *message,
917 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
919 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
920 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
922 GNUNET_assert (ntohs (pkt->len) ==
923 ntohs (message->size) -
924 sizeof (struct GNUNET_MessageHeader) -
925 sizeof (GNUNET_HashCode));
927 /* Get the configuration from the hashmap */
928 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
929 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
930 *udp_desc = ntohs (pkt->dpt);
931 struct redirect_service *serv =
932 GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
935 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
936 "No service found for UDP dpt %d!\n", *udp_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->hashmap = udp_connections;
959 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
961 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
962 sizeof (struct ip6_hdr) + ntohs (pkt->len);
965 memset (buf, 0, len);
967 switch (serv->version)
970 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
971 &serv->v4.ip4address,
972 tunnel, state, (struct ip_pkt *) buf);
975 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
976 &serv->v6.ip6address,
977 tunnel, state, (struct ip6_pkt *) buf);
985 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
988 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
990 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
991 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
994 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
995 GNUNET_TIME_absolute_get ().abs_value);
997 if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections)
998 GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap);
1001 GNUNET_free (state);
1003 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1008 * @brief Main function that will be run by the scheduler.
1010 * @param cls closure
1011 * @param args remaining command-line arguments
1012 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1013 * @param cfg_ configuration
1018 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg_)
1020 const static struct GNUNET_MESH_MessageHandler handlers[] = {
1021 {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
1022 {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
1025 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, NULL);
1028 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1029 udp_connections_heap =
1030 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1031 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1032 tcp_connections_heap =
1033 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1034 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1035 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1037 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1038 &max_udp_connections);
1039 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1040 &max_tcp_connections);
1043 GNUNET_CONFIGURATION_get_value_filename (cfg, "dns", "SERVICES", &services);
1044 servicecfg = GNUNET_CONFIGURATION_create ();
1045 if (GNUNET_OK == GNUNET_CONFIGURATION_parse (servicecfg, services))
1047 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
1048 GNUNET_CONFIGURATION_iterate (servicecfg, read_service_conf, NULL);
1050 if (NULL != services)
1051 GNUNET_free (services);
1053 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1054 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1058 * The main function to obtain template from gnunetd.
1060 * @param argc number of arguments from the command line
1061 * @param argv command line arguments
1062 * @return 0 ok, 1 on error
1065 main (int argc, char *const *argv) {
1066 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1067 GNUNET_GETOPT_OPTION_END
1070 return (GNUNET_OK ==
1071 GNUNET_PROGRAM_run (argc,
1074 gettext_noop ("help text"),
1075 options, &run, NULL)) ? ret : 1;
1078 /* end of gnunet-daemon-exit.c */