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* t) {
179 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == t->reason)
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 * cls is the pointer to a GNUNET_MessageHeader that is
199 * followed by the service-descriptor and the udp-packet that should be sent;
202 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
204 struct GNUNET_MessageHeader *hdr = cls;
205 GNUNET_assert (size >= ntohs (hdr->size));
206 memcpy (buf, hdr, ntohs (hdr->size));
207 size = ntohs(hdr->size);
213 * @brief Handles an UDP-Packet received from the helper.
215 * @param udp A pointer to the Packet
216 * @param dadr The IP-Destination-address
217 * @param addrlen The length of the address
218 * @param version 4 or 6
221 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen,
222 unsigned int version)
224 struct redirect_info u_i;
225 struct GNUNET_MESH_Tunnel *tunnel;
227 struct GNUNET_MessageHeader *msg;
229 memset (&u_i, 0, sizeof (struct redirect_info));
231 memcpy (&u_i.addr, dadr, addrlen);
235 /* get tunnel and service-descriptor from this */
236 /* FIXME better hashing */
237 GNUNET_HashCode hash;
238 GNUNET_CRYPTO_hash (&u_i, sizeof (struct redirect_info), &hash);
240 struct redirect_state *state =
241 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
243 /* Mark this connection as freshly used */
244 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
245 GNUNET_TIME_absolute_get ().abs_value);
247 tunnel = state->tunnel;
249 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
250 if (ntohs (udp->spt) == state->serv->remote_port)
252 udp->spt = htons (state->serv->my_port);
256 struct redirect_service *serv =
257 GNUNET_malloc (sizeof (struct redirect_service));
258 memcpy (serv, state->serv, sizeof (struct redirect_service));
259 serv->my_port = ntohs (udp->spt);
260 serv->remote_port = ntohs (udp->spt);
261 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
262 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
263 sizeof (GNUNET_HashCode));
264 *desc = ntohs (udp->spt);
265 GNUNET_HashCode hash;
266 GNUNET_CRYPTO_hash (desc, sizeof (GNUNET_HashCode) + 2, &hash);
267 GNUNET_assert (GNUNET_OK ==
268 GNUNET_CONTAINER_multihashmap_put (udp_services,
270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
274 /* send udp-packet back */
276 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
278 msg = GNUNET_malloc (len);
279 msg->size = htons (len);
280 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK);
281 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
282 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
283 void *_udp = desc + 1;
284 memcpy (_udp, udp, ntohs (udp->len));
286 GNUNET_MESH_notify_transmit_ready (tunnel,
289 GNUNET_TIME_relative_divide
290 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
291 (const struct GNUNET_PeerIdentity *)
293 send_udp_to_peer_notify_callback, msg);
297 * @brief Handles a TCP-Packet received from the helper.
299 * @param tcp A pointer to the Packet
300 * @param dadr The IP-Destination-address
301 * @param addrlen The length of the address
302 * @param version 4 or 6
303 * @param pktlen the length of the packet, including its header
306 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
307 unsigned int version, size_t pktlen)
309 struct redirect_info u_i;
310 struct GNUNET_MESH_Tunnel *tunnel;
312 struct GNUNET_MessageHeader *msg;
314 memset (&u_i, 0, sizeof (struct redirect_info));
316 memcpy (&u_i.addr, dadr, addrlen);
319 /* FIXME better hashing */
320 /* get tunnel and service-descriptor from this */
321 GNUNET_HashCode hash;
322 GNUNET_CRYPTO_hash (&u_i, sizeof (struct redirect_info), &hash);
323 struct redirect_state *state =
324 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
326 /* Mark this connection as freshly used */
327 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
328 GNUNET_TIME_absolute_get ().abs_value);
330 tunnel = state->tunnel;
332 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
333 if (ntohs (tcp->spt) == state->serv->remote_port)
335 tcp->spt = htons (state->serv->my_port);
339 // This is an illegal packet.
342 /* send tcp-packet back */
344 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
346 msg = GNUNET_malloc (len);
347 msg->size = htons (len);
348 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK);
349 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
350 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
351 void *_tcp = desc + 1;
352 memcpy (_tcp, tcp, pktlen);
354 GNUNET_MESH_notify_transmit_ready (tunnel,
357 GNUNET_TIME_relative_divide
358 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
359 (const struct GNUNET_PeerIdentity *)NULL,
360 len, send_udp_to_peer_notify_callback,
366 * Receive packets from the helper-process
369 message_token (void *cls,
370 void *client, const struct GNUNET_MessageHeader *message)
372 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
374 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
376 /* ethertype is ipv6 */
377 if (ntohs (pkt_tun->tun.type) == 0x86dd)
379 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
380 if (0x11 == pkt6->ip6_hdr.nxthdr)
381 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
382 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6);
383 else if (0x06 == pkt6->ip6_hdr.nxthdr)
384 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
385 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6,
386 ntohs (pkt6->ip6_hdr.paylgth));
388 else if (ntohs (pkt_tun->tun.type) == 0x0800)
390 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
391 uint32_t tmp = pkt4->ip_hdr.dadr;
392 if (0x11 == pkt4->ip_hdr.proto)
393 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
394 (unsigned char *) &tmp, 4, 4);
395 else if (0x06 == pkt4->ip_hdr.proto)
397 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
399 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
400 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
401 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
402 (unsigned char *) &tmp, 4, 4, pktlen);
412 * Reads the configuration servicecfg and populates udp_services
415 * @param section name of section in config, equal to hostname
416 * @param option type of redirect
417 * @param value specification of services, format is
418 * "OFFERED-PORT:HOSTNAME:HOST-PORT" (SPACE <more of those>)*
421 read_service_conf (void *cls, const char *section, const char *option,
428 GNUNET_HashCode hash;
429 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
430 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
431 (GNUNET_HashCode *) (desc + 1));
437 if (0 == strcmp ("UDP_REDIRECTS", option))
439 else if (0 == strcmp ("TCP_REDIRECTS", option))
446 cpy = GNUNET_strdup (value);
447 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
450 if (NULL == (hostname = strstr (redirect, ":")))
452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
458 if (NULL == (hostport = strstr (hostname, ":")))
460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
467 int local_port = atoi (redirect);
468 if (!((local_port > 0) && (local_port < 65536)))
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
474 GNUNET_CRYPTO_hash (desc, sizeof (GNUNET_HashCode) + 2, &hash);
476 struct redirect_service *serv =
477 GNUNET_malloc (sizeof (struct redirect_service));
478 memset (serv, 0, sizeof (struct redirect_service));
479 serv->my_port = local_port;
481 if (0 == strcmp ("localhost4", hostname))
486 GNUNET_assert (GNUNET_OK ==
487 GNUNET_CONFIGURATION_get_value_string (cfg,
492 inet_pton (AF_INET, ip4addr,
493 serv->v4.ip4address));
494 GNUNET_free (ip4addr);
496 else if (0 == strcmp ("localhost6", hostname))
501 GNUNET_assert (GNUNET_OK ==
502 GNUNET_CONFIGURATION_get_value_string (cfg,
507 inet_pton (AF_INET6, ip6addr,
508 serv->v6.ip6address));
509 GNUNET_free (ip6addr);
513 // TODO Lookup, yadayadayada
516 serv->remote_port = atoi (hostport);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
518 *((unsigned long long *) (desc + 1)));
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key2 %x\n",
520 *((unsigned long long *) &hash));
522 GNUNET_assert (GNUNET_OK ==
523 GNUNET_CONTAINER_multihashmap_put (udp_services,
525 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
527 GNUNET_assert (GNUNET_OK ==
528 GNUNET_CONTAINER_multihashmap_put (tcp_services,
530 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
538 * Start the helper-process
540 * If cls != NULL it is assumed that this function is called as a result of a dying
541 * helper. cls is then taken as handle to the old helper and is cleaned up.
544 start_helper_and_schedule(void *cls,
545 const struct GNUNET_SCHEDULER_TaskContext *tc) {
546 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
559 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
561 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
565 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
567 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
571 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
573 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
577 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
579 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
583 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
585 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
590 * Messages get passed to the function message_token
591 * When the helper dies, this function will be called again with the
592 * helper_handle as cls.
594 helper_handle = start_helper(ifname,
600 start_helper_and_schedule,
605 GNUNET_free(ipv6addr);
606 GNUNET_free(ipv6prefix);
607 GNUNET_free(ipv4addr);
608 GNUNET_free(ipv4mask);
613 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
614 uint16_t protocol, void *ipaddress, void *tunnel,
615 struct redirect_state *state, struct ip_pkt *pkt4)
619 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
620 pkt4->shdr.size = htons (len);
622 pkt4->tun.type = htons (0x0800);
624 memcpy (&pkt4->data, payload, pktlen);
626 pkt4->ip_hdr.version = 4;
627 pkt4->ip_hdr.hdr_lngth = 5;
628 pkt4->ip_hdr.diff_serv = 0;
629 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
630 pkt4->ip_hdr.ident = 0;
631 pkt4->ip_hdr.flags = 0;
632 pkt4->ip_hdr.frag_off = 0;
633 pkt4->ip_hdr.ttl = 255;
634 pkt4->ip_hdr.proto = protocol;
635 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
637 memcpy (&tmp, ipaddress, 4);
638 pkt4->ip_hdr.dadr = tmp;
640 /* Generate a new src-address */
643 GNUNET_assert (GNUNET_OK ==
644 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
647 GNUNET_assert (GNUNET_OK ==
648 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
651 inet_pton (AF_INET, ipv4addr, &tmp);
652 inet_pton (AF_INET, ipv4mask, &tmp2);
653 GNUNET_free (ipv4addr);
654 GNUNET_free (ipv4mask);
656 /* This should be a noop */
659 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
661 pkt4->ip_hdr.sadr = tmp;
663 memcpy (&state->redirect_info.addr, &tmp, 4);
664 if (0x11 == protocol)
666 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
667 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
669 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
671 else if (0x06 == protocol)
673 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
674 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
676 pkt4_tcp->tcp_hdr.crc = 0;
678 tmp = pkt4->ip_hdr.sadr;
680 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
681 tmp = pkt4->ip_hdr.dadr;
683 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
685 tmp = (protocol << 16) | (0xffff & pktlen);
687 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
691 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
694 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
695 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
699 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
703 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
704 uint16_t protocol, void *ipaddress, void *tunnel,
705 struct redirect_state *state, struct ip6_pkt *pkt6)
709 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
710 pkt6->shdr.size = htons (len);
713 pkt6->tun.type = htons (0x86dd);
715 memcpy (&pkt6->data, payload, pktlen);
717 pkt6->ip6_hdr.version = 6;
718 pkt6->ip6_hdr.nxthdr = protocol;
719 pkt6->ip6_hdr.paylgth = htons (pktlen);
720 pkt6->ip6_hdr.hoplmt = 64;
722 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
724 /* Generate a new src-address
725 * This takes as much from the address of the tunnel as fits into
728 unsigned long long ipv6prefix;
729 GNUNET_assert (GNUNET_OK ==
730 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
733 GNUNET_assert (GNUNET_OK ==
734 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
737 GNUNET_assert (ipv6prefix < 127);
738 ipv6prefix = (ipv6prefix + 7) / 8;
740 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
741 GNUNET_free (ipv6addr);
743 if (ipv6prefix < (16 - sizeof (void *)))
744 ipv6prefix = 16 - sizeof (void *);
746 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
747 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
748 ((char *) &tunnel) + offset, 16 - ipv6prefix);
750 /* copy the needed information into the state */
751 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
753 if (0x11 == protocol)
755 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
756 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
758 pkt6_udp->udp_hdr.crc = 0;
761 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
763 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
764 tmp = (htons (pktlen) & 0xffff);
765 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
766 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
767 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
770 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
771 ntohs (pkt6_udp->udp_hdr.len));
772 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
774 else if (0x06 == protocol)
776 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
777 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
779 pkt6_tcp->tcp_hdr.crc = 0;
782 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
784 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
786 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
787 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
788 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
791 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
792 ntohs (pkt6->ip6_hdr.paylgth));
793 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
798 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
801 receive_tcp_service (void *cls,
802 struct GNUNET_MESH_Tunnel *tunnel,
804 const struct GNUNET_PeerIdentity *sender,
805 const struct GNUNET_MessageHeader *message,
806 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
809 GNUNET_HashCode hash;
810 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
811 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
812 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
813 GNUNET_MessageHeader)
814 - sizeof (GNUNET_HashCode);
816 /** Get the configuration from the services-hashmap.
818 * Which service is needed only depends on the service-descriptor and the
821 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
823 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
824 *tcp_desc = ntohs (pkt->dpt);
825 GNUNET_CRYPTO_hash (tcp_desc, sizeof (GNUNET_HashCode) + 2, &hash);
826 struct redirect_service *serv =
827 GNUNET_CONTAINER_multihashmap_get (tcp_services, &hash);
830 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
831 "No service found for TCP dpt %d!\n", *tcp_desc);
835 pkt->dpt = htons (serv->remote_port);
838 * At this point it would be possible to check against some kind of ACL.
844 /* Prepare the state.
845 * This will be saved in the hashmap, so that the receiving procedure knows
846 * through which tunnel this connection has to be routed.
848 struct redirect_state *state =
849 GNUNET_malloc (sizeof (struct redirect_state));
850 memset (state, 0, sizeof (struct redirect_state));
851 state->tunnel = tunnel;
853 state->hashmap = tcp_connections;
854 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
856 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
857 sizeof (struct ip6_hdr) + pkt_len;
860 memset (buf, 0, len);
862 switch (serv->version)
865 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
866 &serv->v4.ip4address,
867 tunnel, state, (struct ip_pkt *) buf);
870 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
871 &serv->v6.ip6address,
872 tunnel, state, (struct ip6_pkt *) buf);
880 /* FIXME better hashing */
881 GNUNET_CRYPTO_hash (&state->redirect_info, sizeof (struct redirect_info),
885 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
887 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
888 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
891 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
892 GNUNET_TIME_absolute_get ().abs_value);
894 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
895 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
900 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
905 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
908 receive_udp_service (void *cls,
909 struct GNUNET_MESH_Tunnel *tunnel,
911 const struct GNUNET_PeerIdentity *sender,
912 const struct GNUNET_MessageHeader *message,
913 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
915 GNUNET_HashCode hash;
916 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
917 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
919 GNUNET_assert (ntohs (pkt->len) ==
920 ntohs (message->size) -
921 sizeof (struct GNUNET_MessageHeader) -
922 sizeof (GNUNET_HashCode));
924 /* Get the configuration from the hashmap */
925 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
926 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
927 *udp_desc = ntohs (pkt->dpt);
928 GNUNET_CRYPTO_hash (udp_desc, sizeof (GNUNET_HashCode) + 2, &hash);
929 struct redirect_service *serv =
930 GNUNET_CONTAINER_multihashmap_get (udp_services, &hash);
933 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
934 "No service found for UDP dpt %d!\n", *udp_desc);
938 pkt->dpt = htons (serv->remote_port);
941 * At this point it would be possible to check against some kind of ACL.
947 /* Prepare the state.
948 * This will be saved in the hashmap, so that the receiving procedure knows
949 * through which tunnel this connection has to be routed.
951 struct redirect_state *state =
952 GNUNET_malloc (sizeof (struct redirect_state));
953 memset (state, 0, sizeof (struct redirect_state));
954 state->tunnel = tunnel;
956 state->hashmap = udp_connections;
957 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
959 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
960 sizeof (struct ip6_hdr) + ntohs (pkt->len);
963 memset (buf, 0, len);
965 switch (serv->version)
968 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
969 &serv->v4.ip4address,
970 tunnel, state, (struct ip_pkt *) buf);
973 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
974 &serv->v6.ip6address,
975 tunnel, state, (struct ip6_pkt *) buf);
983 /* FIXME better hashing */
984 GNUNET_CRYPTO_hash (&state->redirect_info, sizeof (struct redirect_info),
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 */