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 hashmap contains the mapping from peer, service-descriptor,
65 * source-port and destination-port to a socket
67 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
68 static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
71 * This struct is saved into the services-hashmap
73 struct redirect_service
98 * The source-address of this connection. When a packet to this address is
99 * received, this tunnel is used to forward it. ipv4-addresses will be put
100 * here left-aligned */
103 * The source-port of this connection
109 * This struct is saved into {tcp,udp}_connections;
111 struct redirect_state
113 struct GNUNET_MESH_Tunnel *tunnel;
114 GNUNET_HashCode desc;
115 struct redirect_service *serv;
118 * The source-address and -port of this connection
120 struct redirect_info redirect_info;
124 * This hashmaps saves interesting things about the configured services
126 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
127 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
130 * Function that frees everything from a hashmap
133 free_iterate(void* cls, const GNUNET_HashCode* hash, void* value)
140 * Function scheduled as very last function, cleans up after us
143 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
144 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
146 GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
150 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
154 if (mesh_handle != NULL)
156 GNUNET_MESH_disconnect(mesh_handle);
162 * cls is the pointer to a GNUNET_MessageHeader that is
163 * followed by the service-descriptor and the udp-packet that should be sent;
166 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
168 struct GNUNET_MessageHeader *hdr = cls;
169 GNUNET_assert (size >= ntohs (hdr->size));
170 memcpy (buf, hdr, ntohs (hdr->size));
171 size = ntohs(hdr->size);
177 * @brief Handles an UDP-Packet received from the helper.
179 * @param udp A pointer to the Packet
180 * @param dadr The IP-Destination-address
181 * @param addrlen The length of the address
182 * @param version 4 or 6
185 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen,
186 unsigned int version)
188 struct redirect_info u_i;
189 struct GNUNET_MESH_Tunnel *tunnel;
191 struct GNUNET_MessageHeader *msg;
193 memset (&u_i, 0, sizeof (struct redirect_info));
195 memcpy (&u_i.addr, dadr, addrlen);
199 /* get tunnel and service-descriptor from this */
200 GNUNET_HashCode hash;
201 GNUNET_CRYPTO_hash (&u_i, sizeof (struct redirect_info), &hash);
202 struct redirect_state *state =
203 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
205 tunnel = state->tunnel;
207 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
208 if (ntohs (udp->spt) == state->serv->remote_port)
210 udp->spt = htons (state->serv->my_port);
214 struct redirect_service *serv =
215 GNUNET_malloc (sizeof (struct redirect_service));
216 memcpy (serv, state->serv, sizeof (struct redirect_service));
217 serv->my_port = ntohs (udp->spt);
218 serv->remote_port = ntohs (udp->spt);
219 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
220 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
221 sizeof (GNUNET_HashCode));
222 *desc = ntohs (udp->spt);
223 GNUNET_HashCode hash;
224 GNUNET_CRYPTO_hash (desc, sizeof (GNUNET_HashCode) + 2, &hash);
225 GNUNET_assert (GNUNET_OK ==
226 GNUNET_CONTAINER_multihashmap_put (udp_services,
228 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
231 /* send udp-packet back */
233 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
235 msg = GNUNET_malloc (len);
236 msg->size = htons (len);
237 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK);
238 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
239 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
240 void *_udp = desc + 1;
241 memcpy (_udp, udp, ntohs (udp->len));
243 GNUNET_MESH_notify_transmit_ready (tunnel,
246 GNUNET_TIME_relative_divide
247 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
248 (const struct GNUNET_PeerIdentity *)NULL,
249 len, send_udp_to_peer_notify_callback,
254 * @brief Handles a TCP-Packet received from the helper.
256 * @param tcp A pointer to the Packet
257 * @param dadr The IP-Destination-address
258 * @param addrlen The length of the address
259 * @param version 4 or 6
260 * @param pktlen the length of the packet, including its header
263 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
264 unsigned int version, size_t pktlen)
266 struct redirect_info u_i;
267 struct GNUNET_MESH_Tunnel *tunnel;
269 struct GNUNET_MessageHeader *msg;
271 memset (&u_i, 0, sizeof (struct redirect_info));
273 memcpy (&u_i.addr, dadr, addrlen);
276 /* get tunnel and service-descriptor from this */
277 GNUNET_HashCode hash;
278 GNUNET_CRYPTO_hash (&u_i, sizeof (struct redirect_info), &hash);
279 struct redirect_state *state =
280 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
282 tunnel = state->tunnel;
284 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
285 if (ntohs (tcp->spt) == state->serv->remote_port)
287 tcp->spt = htons (state->serv->my_port);
291 // This is an illegal packet.
294 /* send udp-packet back */
296 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
298 msg = GNUNET_malloc (len);
299 msg->size = htons (len);
300 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK);
301 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
302 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
303 void *_tcp = desc + 1;
304 memcpy (_tcp, tcp, pktlen);
306 GNUNET_MESH_notify_transmit_ready (tunnel,
309 GNUNET_TIME_relative_divide
310 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
311 (const struct GNUNET_PeerIdentity *)NULL,
312 len, send_udp_to_peer_notify_callback,
318 * Receive packets from the helper-process
321 message_token (void *cls,
322 void *client, const struct GNUNET_MessageHeader *message)
324 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
326 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
328 /* ethertype is ipv6 */
329 if (ntohs (pkt_tun->tun.type) == 0x86dd)
331 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
332 if (0x11 == pkt6->ip6_hdr.nxthdr)
333 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
334 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6);
335 else if (0x06 == pkt6->ip6_hdr.nxthdr)
336 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
337 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6,
338 ntohs (pkt6->ip6_hdr.paylgth));
340 else if (ntohs (pkt_tun->tun.type) == 0x0800)
342 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
343 uint32_t tmp = pkt4->ip_hdr.dadr;
344 if (0x11 == pkt4->ip_hdr.proto)
345 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
346 (unsigned char *) &tmp, 4, 4);
347 else if (0x06 == pkt4->ip_hdr.proto)
349 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
351 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
352 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
353 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
354 (unsigned char *) &tmp, 4, 4, pktlen);
364 * Reads the configuration servicecfg and populates udp_services
367 * @param section name of section in config, equal to hostname
368 * @param option type of redirect
369 * @param value specification of services, format is
370 * "OFFERED-PORT:HOSTNAME:HOST-PORT" (SPACE <more of those>)*
373 read_service_conf (void *cls, const char *section, const char *option,
380 GNUNET_HashCode hash;
381 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
382 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
383 (GNUNET_HashCode *) (desc + 1));
389 if (0 == strcmp ("UDP_REDIRECTS", option))
391 else if (0 == strcmp ("TCP_REDIRECTS", option))
398 cpy = GNUNET_strdup (value);
399 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
402 if (NULL == (hostname = strstr (redirect, ":")))
404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
410 if (NULL == (hostport = strstr (hostname, ":")))
412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
419 int local_port = atoi (redirect);
420 if (!((local_port > 0) && (local_port < 65536)))
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
426 GNUNET_CRYPTO_hash (desc, sizeof (GNUNET_HashCode) + 2, &hash);
428 struct redirect_service *serv =
429 GNUNET_malloc (sizeof (struct redirect_service));
430 memset (serv, 0, sizeof (struct redirect_service));
431 serv->my_port = local_port;
433 if (0 == strcmp ("localhost4", hostname))
438 GNUNET_assert (GNUNET_OK ==
439 GNUNET_CONFIGURATION_get_value_string (cfg,
444 inet_pton (AF_INET, ip4addr,
445 serv->v4.ip4address));
446 GNUNET_free (ip4addr);
448 else if (0 == strcmp ("localhost6", hostname))
453 GNUNET_assert (GNUNET_OK ==
454 GNUNET_CONFIGURATION_get_value_string (cfg,
459 inet_pton (AF_INET6, ip6addr,
460 serv->v6.ip6address));
461 GNUNET_free (ip6addr);
465 // Lookup, yadayadayada
468 serv->remote_port = atoi (hostport);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
470 *((unsigned long long *) (desc + 1)));
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key2 %x\n",
472 *((unsigned long long *) &hash));
474 GNUNET_assert (GNUNET_OK ==
475 GNUNET_CONTAINER_multihashmap_put (udp_services,
477 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
479 GNUNET_assert (GNUNET_OK ==
480 GNUNET_CONTAINER_multihashmap_put (tcp_services,
482 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
490 * Start the helper-process
492 * If cls != NULL it is assumed that this function is called as a result of a dying
493 * helper. cls is then taken as handle to the old helper and is cleaned up.
496 start_helper_and_schedule(void *cls,
497 const struct GNUNET_SCHEDULER_TaskContext *tc) {
498 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
511 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
513 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
517 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
519 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
523 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
525 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
529 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
531 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
535 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
537 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
542 * Messages get passed to the function message_token
543 * When the helper dies, this function will be called again with the
544 * helper_handle as cls.
546 helper_handle = start_helper(ifname,
552 start_helper_and_schedule,
557 GNUNET_free(ipv6addr);
558 GNUNET_free(ipv6prefix);
559 GNUNET_free(ipv4addr);
560 GNUNET_free(ipv4mask);
565 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
566 uint16_t protocol, void *ipaddress, void *tunnel,
567 struct redirect_state *state, struct ip_pkt *pkt4)
571 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
572 pkt4->shdr.size = htons (len);
574 pkt4->tun.type = htons (0x0800);
576 memcpy (&pkt4->data, payload, pktlen);
578 pkt4->ip_hdr.version = 4;
579 pkt4->ip_hdr.hdr_lngth = 5;
580 pkt4->ip_hdr.diff_serv = 0;
581 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
582 pkt4->ip_hdr.ident = 0;
583 pkt4->ip_hdr.flags = 0;
584 pkt4->ip_hdr.frag_off = 0;
585 pkt4->ip_hdr.ttl = 255;
586 pkt4->ip_hdr.proto = protocol;
587 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
589 memcpy (&tmp, ipaddress, 4);
590 pkt4->ip_hdr.dadr = tmp;
592 /* Generate a new src-address */
595 GNUNET_assert (GNUNET_OK ==
596 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
599 GNUNET_assert (GNUNET_OK ==
600 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
603 inet_pton (AF_INET, ipv4addr, &tmp);
604 inet_pton (AF_INET, ipv4mask, &tmp2);
605 GNUNET_free (ipv4addr);
606 GNUNET_free (ipv4mask);
608 /* This should be a noop */
611 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
613 pkt4->ip_hdr.sadr = tmp;
615 memcpy (&state->redirect_info.addr, &tmp, 4);
616 if (0x11 == protocol)
618 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
619 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
621 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
623 else if (0x06 == protocol)
625 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
626 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
628 pkt4_tcp->tcp_hdr.crc = 0;
630 tmp = pkt4->ip_hdr.sadr;
632 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
633 tmp = pkt4->ip_hdr.dadr;
635 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
637 tmp = (protocol << 16) | (0xffff & pktlen);
639 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
643 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
646 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
647 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
651 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
655 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
656 uint16_t protocol, void *ipaddress, void *tunnel,
657 struct redirect_state *state, struct ip6_pkt *pkt6)
661 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
662 pkt6->shdr.size = htons (len);
665 pkt6->tun.type = htons (0x86dd);
667 memcpy (&pkt6->data, payload, pktlen);
669 pkt6->ip6_hdr.version = 6;
670 pkt6->ip6_hdr.nxthdr = protocol;
671 pkt6->ip6_hdr.paylgth = htons (pktlen);
672 pkt6->ip6_hdr.hoplmt = 64;
674 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
676 /* Generate a new src-address
677 * This takes as much from the address of the tunnel as fits into
680 unsigned long long ipv6prefix;
681 GNUNET_assert (GNUNET_OK ==
682 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
685 GNUNET_assert (GNUNET_OK ==
686 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
689 GNUNET_assert (ipv6prefix < 127);
690 ipv6prefix = (ipv6prefix + 7) / 8;
692 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
693 GNUNET_free (ipv6addr);
695 if (ipv6prefix < (16 - sizeof (void *)))
696 ipv6prefix = 16 - sizeof (void *);
698 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
699 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
700 ((char *) &tunnel) + offset, 16 - ipv6prefix);
702 /* copy the needed information into the state */
703 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
705 if (0x11 == protocol)
707 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
708 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
710 pkt6_udp->udp_hdr.crc = 0;
713 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
715 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
716 tmp = (htons (pktlen) & 0xffff);
717 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
718 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
719 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
722 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
723 ntohs (pkt6_udp->udp_hdr.len));
724 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
726 else if (0x06 == protocol)
728 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
729 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
731 pkt6_tcp->tcp_hdr.crc = 0;
734 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
736 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
738 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
739 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
740 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
743 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
744 ntohs (pkt6->ip6_hdr.paylgth));
745 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
750 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
753 receive_tcp_service (void *cls,
754 struct GNUNET_MESH_Tunnel *tunnel,
756 const struct GNUNET_PeerIdentity *sender,
757 const struct GNUNET_MessageHeader *message,
758 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
760 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
761 GNUNET_HashCode hash;
762 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
763 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
764 unsigned int pkt_len = ntohs(message->size) - sizeof (struct
765 GNUNET_MessageHeader) -
766 sizeof(GNUNET_HashCode);
768 /** Get the configuration from the services-hashmap.
770 * Which service is needed only depends on the service-descriptor and the
773 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
775 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
776 *tcp_desc = ntohs (pkt->dpt);
777 GNUNET_CRYPTO_hash (tcp_desc, sizeof (GNUNET_HashCode) + 2, &hash);
778 struct redirect_service *serv =
779 GNUNET_CONTAINER_multihashmap_get (tcp_services, &hash);
782 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n",
787 pkt->dpt = htons (serv->remote_port);
788 /* FIXME -> check acl etc */
793 /* Prepare the state.
794 * This will be saved in the hashmap, so that the receiving procedure knows
795 * through which tunnel this connection has to be routed.
797 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
798 memset (state, 0, sizeof (struct redirect_state));
799 state->tunnel = tunnel;
801 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
803 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
804 sizeof (struct ip6_hdr) + pkt_len;
807 memset (buf, 0, len);
809 switch (serv->version)
812 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
813 &serv->v4.ip4address,
814 tunnel, state, (struct ip_pkt *) buf);
817 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
818 &serv->v6.ip6address,
819 tunnel, state, (struct ip6_pkt *) buf);
827 GNUNET_CRYPTO_hash (&state->redirect_info, sizeof (struct redirect_info), &hash);
830 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &hash))
831 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &hash, state,
832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
836 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
841 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
844 receive_udp_service (void *cls,
845 struct GNUNET_MESH_Tunnel *tunnel,
847 const struct GNUNET_PeerIdentity *sender,
848 const struct GNUNET_MessageHeader *message,
849 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
851 GNUNET_HashCode hash;
852 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
853 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
855 GNUNET_assert (ntohs (pkt->len) ==
856 ntohs (message->size) -
857 sizeof (struct GNUNET_MessageHeader) -
858 sizeof (GNUNET_HashCode));
860 /* Get the configuration from the hashmap */
861 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
862 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
863 *udp_desc = ntohs (pkt->dpt);
864 GNUNET_CRYPTO_hash (udp_desc, sizeof (GNUNET_HashCode) + 2, &hash);
865 struct redirect_service *serv =
866 GNUNET_CONTAINER_multihashmap_get (udp_services, &hash);
869 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n",
874 pkt->dpt = htons (serv->remote_port);
875 /* FIXME -> check acl etc */
880 /* Prepare the state.
881 * This will be saved in the hashmap, so that the receiving procedure knows
882 * through which tunnel this connection has to be routed.
884 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
885 memset (state, 0, sizeof (struct redirect_state));
886 state->tunnel = tunnel;
888 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
890 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
891 sizeof (struct ip6_hdr) + ntohs (pkt->len);
894 memset (buf, 0, len);
896 switch (serv->version)
899 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
900 &serv->v4.ip4address,
901 tunnel, state, (struct ip_pkt *) buf);
904 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
905 &serv->v6.ip6address,
906 tunnel, state, (struct ip6_pkt *) buf);
914 GNUNET_CRYPTO_hash (&state->redirect_info, sizeof (struct redirect_info), &hash);
917 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &hash))
918 GNUNET_CONTAINER_multihashmap_put (udp_connections, &hash, state,
919 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
923 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
928 * @brief Main function that will be run by the scheduler.
931 * @param args remaining command-line arguments
932 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
933 * @param cfg_ configuration
939 const struct GNUNET_CONFIGURATION_Handle *cfg_)
941 const static struct GNUNET_MESH_MessageHandler handlers[] = {
942 {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
943 {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
946 mesh_handle = GNUNET_MESH_connect(cfg_,
953 udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
954 tcp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
955 udp_services = GNUNET_CONTAINER_multihashmap_create(65536);
956 tcp_services = GNUNET_CONTAINER_multihashmap_create(65536);
959 GNUNET_CONFIGURATION_get_value_filename(cfg, "dns", "SERVICES", &services);
960 servicecfg = GNUNET_CONFIGURATION_create();
961 if (GNUNET_OK == GNUNET_CONFIGURATION_parse(servicecfg, services))
963 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
964 GNUNET_CONFIGURATION_iterate(servicecfg, read_service_conf, NULL);
966 if (NULL != services)
967 GNUNET_free(services);
969 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
970 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
974 * The main function to obtain template from gnunetd.
976 * @param argc number of arguments from the command line
977 * @param argv command line arguments
978 * @return 0 ok, 1 on error
981 main (int argc, char *const *argv) {
982 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
983 GNUNET_GETOPT_OPTION_END
987 GNUNET_PROGRAM_run (argc,
990 gettext_noop ("help text"),
991 options, &run, NULL)) ? ret : 1;
994 /* end of gnunet-daemon-exit.c */