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 service-configuration
47 static struct GNUNET_CONFIGURATION_Handle *servicecfg;
50 * The handle to the helper
52 struct GNUNET_VPN_HELPER_Handle *helper_handle;
62 static struct GNUNET_MESH_Handle *mesh_handle;
65 * This hashmaps contains the mapping from peer, service-descriptor,
66 * source-port and destination-port to a struct redirect_state
68 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
69 static struct GNUNET_CONTAINER_Heap *udp_connections_heap;
70 static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
71 static struct GNUNET_CONTAINER_Heap *tcp_connections_heap;
74 * If there are at least this many udp-Connections, old ones will be removed
76 static long long unsigned int max_udp_connections = 200;
79 * If there are at least this many tcp-Connections, old ones will be removed
81 static long long unsigned int max_tcp_connections = 200;
86 unsigned char addr[16];
91 * This struct is saved into the services-hashmap
93 struct redirect_service
100 uint16_t remote_port;
118 * The source-address of this connection. When a packet to this address is
119 * received, this tunnel is used to forward it. ipv4-addresses will be put
120 * here left-aligned */
123 * The source-port of this connection
129 * This struct is saved into {tcp,udp}_connections;
131 struct redirect_state
133 struct GNUNET_MESH_Tunnel *tunnel;
134 GNUNET_HashCode desc;
135 struct redirect_service *serv;
136 struct remote_addr remote;
138 struct GNUNET_CONTAINER_HeapNode* heap_node;
139 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
140 GNUNET_HashCode hash;
142 enum { SERVICE, REMOTE } type;
145 * The source-address and -port of this connection
147 struct redirect_info redirect_info;
151 * This hashmaps saves interesting things about the configured services
153 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
154 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
157 * Function that frees everything from a hashmap
160 free_iterate(void* cls, const GNUNET_HashCode* hash, void* value)
167 * Function scheduled as very last function, cleans up after us
170 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
171 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
173 GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
177 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
181 if (mesh_handle != NULL)
183 GNUNET_MESH_disconnect(mesh_handle);
189 collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
190 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
194 struct GNUNET_CONTAINER_Heap *heap = cls;
196 struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap);
198 /* This is free()ed memory! */
199 state->heap_node = NULL;
201 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
203 GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state);
209 hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
212 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
214 * build the hash out of the last bytes of the address and the 2 bytes of
217 memcpy(hash, &u_i->pt, sizeof(u_i->pt));
218 memcpy(((unsigned char*)hash)+2, u_i->addr+(addrlen-(sizeof(unsigned int) - 2)), (sizeof(unsigned int) - 2));
219 memset(((unsigned char*)hash)+sizeof(unsigned int), 0, sizeof(GNUNET_HashCode) - sizeof(unsigned int));
223 * cls is the pointer to a GNUNET_MessageHeader that is
224 * followed by the service-descriptor and the udp-packet that should be sent;
227 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
229 struct GNUNET_MessageHeader *hdr = cls;
230 GNUNET_assert (size >= ntohs (hdr->size));
231 memcpy (buf, hdr, ntohs (hdr->size));
232 size = ntohs(hdr->size);
238 * @brief Handles an UDP-Packet received from the helper.
240 * @param udp A pointer to the Packet
241 * @param dadr The IP-Destination-address
242 * @param addrlen The length of the address
243 * @param version 4 or 6
246 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen,
247 unsigned int version)
249 struct redirect_info u_i;
250 struct GNUNET_MESH_Tunnel *tunnel;
252 struct GNUNET_MessageHeader *msg;
254 memset (&u_i, 0, sizeof (struct redirect_info));
256 memcpy (&u_i.addr, dadr, addrlen);
260 /* get tunnel and service-descriptor from this */
261 GNUNET_HashCode hash;
262 hash_redirect_info(&hash, &u_i, addrlen);
264 struct redirect_state *state =
265 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
267 /* Mark this connection as freshly used */
268 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
269 GNUNET_TIME_absolute_get ().abs_value);
271 tunnel = state->tunnel;
273 if (state->type == SERVICE)
275 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
276 if (ntohs (udp->spt) == state->serv->remote_port)
278 udp->spt = htons (state->serv->my_port);
282 /* otherwise the answer came from a different port (tftp does this)
283 * add this new port to the list of all services, so that the packets
284 * coming back from the client to this new port will be routed correctly
286 struct redirect_service *serv =
287 GNUNET_malloc (sizeof (struct redirect_service));
288 memcpy (serv, state->serv, sizeof (struct redirect_service));
289 serv->my_port = ntohs (udp->spt);
290 serv->remote_port = ntohs (udp->spt);
291 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
292 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
293 sizeof (GNUNET_HashCode));
294 *desc = ntohs (udp->spt);
295 GNUNET_assert (GNUNET_OK ==
296 GNUNET_CONTAINER_multihashmap_put (udp_services,
297 (GNUNET_HashCode*)desc, serv,
298 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
304 /* send udp-packet back */
306 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
308 msg = GNUNET_malloc (len);
309 msg->size = htons (len);
310 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK);
311 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
312 if (state->type == SERVICE)
313 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
315 memcpy (desc, &state->remote, sizeof (struct remote_addr));
316 void *_udp = desc + 1;
317 memcpy (_udp, udp, ntohs (udp->len));
319 GNUNET_MESH_notify_transmit_ready (tunnel,
322 GNUNET_TIME_relative_divide
323 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
324 (const struct GNUNET_PeerIdentity *)
326 send_udp_to_peer_notify_callback, msg);
330 * @brief Handles a TCP-Packet received from the helper.
332 * @param tcp A pointer to the Packet
333 * @param dadr The IP-Destination-address
334 * @param addrlen The length of the address
335 * @param version 4 or 6
336 * @param pktlen the length of the packet, including its header
339 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
340 unsigned int version, size_t pktlen)
342 struct redirect_info u_i;
343 struct GNUNET_MESH_Tunnel *tunnel;
345 struct GNUNET_MessageHeader *msg;
347 memset (&u_i, 0, sizeof (struct redirect_info));
349 memcpy (&u_i.addr, dadr, addrlen);
352 /* get tunnel and service-descriptor from this */
353 GNUNET_HashCode hash;
354 hash_redirect_info(&hash, &u_i, addrlen);
356 struct redirect_state *state =
357 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
359 if (state == NULL) return;
361 /* Mark this connection as freshly used */
362 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
363 GNUNET_TIME_absolute_get ().abs_value);
365 tunnel = state->tunnel;
367 if (state->type == SERVICE)
369 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
370 if (ntohs (tcp->spt) == state->serv->remote_port)
372 tcp->spt = htons (state->serv->my_port);
376 // This is an illegal packet.
381 /* send tcp-packet back */
383 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
385 msg = GNUNET_malloc (len);
386 msg->size = htons (len);
387 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK);
388 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
389 if (state->type == SERVICE)
390 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
392 memcpy (desc, &state->remote, sizeof (struct remote_addr));
393 void *_tcp = desc + 1;
394 memcpy (_tcp, tcp, pktlen);
396 GNUNET_MESH_notify_transmit_ready (tunnel,
399 GNUNET_TIME_relative_divide
400 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
401 (const struct GNUNET_PeerIdentity *)NULL,
402 len, send_udp_to_peer_notify_callback,
408 * Receive packets from the helper-process
411 message_token (void *cls,
412 void *client, const struct GNUNET_MessageHeader *message)
414 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
416 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
418 /* ethertype is ipv6 */
419 if (ntohs (pkt_tun->tun.type) == 0x86dd)
421 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
422 if (0x11 == pkt6->ip6_hdr.nxthdr)
423 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
424 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6);
425 else if (0x06 == pkt6->ip6_hdr.nxthdr)
426 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
427 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6,
428 ntohs (pkt6->ip6_hdr.paylgth));
430 else if (ntohs (pkt_tun->tun.type) == 0x0800)
432 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
433 uint32_t tmp = pkt4->ip_hdr.dadr;
434 if (0x11 == pkt4->ip_hdr.proto)
435 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
436 (unsigned char *) &tmp, 4, 4);
437 else if (0x06 == pkt4->ip_hdr.proto)
439 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
440 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
441 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
443 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
444 (unsigned char *) &tmp, 4, 4, pktlen);
454 * Reads the configuration servicecfg and populates udp_services
457 * @param section name of section in config, equal to hostname
458 * @param option type of redirect
459 * @param value specification of services, format is
460 * "OFFERED-PORT:HOSTNAME:HOST-PORT" (SPACE <more of those>)*
463 read_service_conf (void *cls, const char *section, const char *option,
470 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
471 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
472 (GNUNET_HashCode *) (desc + 1));
478 if (0 == strcmp ("UDP_REDIRECTS", option))
480 else if (0 == strcmp ("TCP_REDIRECTS", option))
487 cpy = GNUNET_strdup (value);
488 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
491 if (NULL == (hostname = strstr (redirect, ":")))
493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
499 if (NULL == (hostport = strstr (hostname, ":")))
501 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
508 int local_port = atoi (redirect);
509 if (!((local_port > 0) && (local_port < 65536)))
510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
515 struct redirect_service *serv =
516 GNUNET_malloc (sizeof (struct redirect_service));
517 memset (serv, 0, sizeof (struct redirect_service));
518 serv->my_port = local_port;
520 if (0 == strcmp ("localhost4", hostname))
525 GNUNET_assert (GNUNET_OK ==
526 GNUNET_CONFIGURATION_get_value_string (cfg,
531 inet_pton (AF_INET, ip4addr,
532 serv->v4.ip4address));
533 GNUNET_free (ip4addr);
535 else if (0 == strcmp ("localhost6", hostname))
540 GNUNET_assert (GNUNET_OK ==
541 GNUNET_CONFIGURATION_get_value_string (cfg,
546 inet_pton (AF_INET6, ip6addr,
547 serv->v6.ip6address));
548 GNUNET_free (ip6addr);
552 // TODO Lookup, yadayadayada
555 serv->remote_port = atoi (hostport);
557 GNUNET_assert (GNUNET_OK ==
558 GNUNET_CONTAINER_multihashmap_put (udp_services,
559 (GNUNET_HashCode*)desc, serv,
560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
562 GNUNET_assert (GNUNET_OK ==
563 GNUNET_CONTAINER_multihashmap_put (tcp_services,
564 (GNUNET_HashCode*)desc, serv,
565 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
573 * Start the helper-process
575 * If cls != NULL it is assumed that this function is called as a result of a dying
576 * helper. cls is then taken as handle to the old helper and is cleaned up.
579 start_helper_and_schedule(void *cls,
580 const struct GNUNET_SCHEDULER_TaskContext *tc) {
581 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
594 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
596 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
600 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
602 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
606 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
608 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
612 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
614 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
618 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
620 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
625 * Messages get passed to the function message_token
626 * When the helper dies, this function will be called again with the
627 * helper_handle as cls.
629 helper_handle = start_helper(ifname,
635 start_helper_and_schedule,
640 GNUNET_free(ipv6addr);
641 GNUNET_free(ipv6prefix);
642 GNUNET_free(ipv4addr);
643 GNUNET_free(ipv4mask);
648 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
649 uint16_t protocol, void *ipaddress, void *tunnel,
650 struct redirect_state *state, struct ip_pkt *pkt4)
654 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
655 pkt4->shdr.size = htons (len);
657 pkt4->tun.type = htons (0x0800);
659 memcpy (&pkt4->data, payload, pktlen);
661 pkt4->ip_hdr.version = 4;
662 pkt4->ip_hdr.hdr_lngth = 5;
663 pkt4->ip_hdr.diff_serv = 0;
664 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
665 pkt4->ip_hdr.ident = 0;
666 pkt4->ip_hdr.flags = 0;
667 pkt4->ip_hdr.frag_off = 0;
668 pkt4->ip_hdr.ttl = 255;
669 pkt4->ip_hdr.proto = protocol;
670 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
672 memcpy (&tmp, ipaddress, 4);
673 pkt4->ip_hdr.dadr = tmp;
675 /* Generate a new src-address */
678 GNUNET_assert (GNUNET_OK ==
679 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
682 GNUNET_assert (GNUNET_OK ==
683 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
686 inet_pton (AF_INET, ipv4addr, &tmp);
687 inet_pton (AF_INET, ipv4mask, &tmp2);
688 GNUNET_free (ipv4addr);
689 GNUNET_free (ipv4mask);
691 /* This should be a noop */
694 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
696 pkt4->ip_hdr.sadr = tmp;
698 memcpy (&state->redirect_info.addr, &tmp, 4);
699 if (0x11 == protocol)
701 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
702 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
704 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
706 else if (0x06 == protocol)
708 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
709 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
711 pkt4_tcp->tcp_hdr.crc = 0;
713 tmp = pkt4->ip_hdr.sadr;
715 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
716 tmp = pkt4->ip_hdr.dadr;
718 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
720 tmp = (protocol << 16) | (0xffff & pktlen);
722 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
726 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
729 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
730 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
734 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
738 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
739 uint16_t protocol, void *ipaddress, void *tunnel,
740 struct redirect_state *state, struct ip6_pkt *pkt6)
744 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
745 pkt6->shdr.size = htons (len);
748 pkt6->tun.type = htons (0x86dd);
750 memcpy (&pkt6->data, payload, pktlen);
752 pkt6->ip6_hdr.version = 6;
753 pkt6->ip6_hdr.nxthdr = protocol;
754 pkt6->ip6_hdr.paylgth = htons (pktlen);
755 pkt6->ip6_hdr.hoplmt = 64;
757 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
759 /* Generate a new src-address
760 * This takes as much from the address of the tunnel as fits into
763 unsigned long long ipv6prefix;
764 GNUNET_assert (GNUNET_OK ==
765 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
768 GNUNET_assert (GNUNET_OK ==
769 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
772 GNUNET_assert (ipv6prefix < 127);
773 ipv6prefix = (ipv6prefix + 7) / 8;
775 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
776 GNUNET_free (ipv6addr);
778 if (ipv6prefix < (16 - sizeof (void *)))
779 ipv6prefix = 16 - sizeof (void *);
781 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
782 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
783 ((char *) &tunnel) + offset, 16 - ipv6prefix);
785 /* copy the needed information into the state */
786 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
788 if (0x11 == protocol)
790 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
791 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
793 pkt6_udp->udp_hdr.crc = 0;
796 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
798 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
799 tmp = (htons (pktlen) & 0xffff);
800 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
801 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
802 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
805 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
806 ntohs (pkt6_udp->udp_hdr.len));
807 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
809 else if (0x06 == protocol)
811 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
812 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
814 pkt6_tcp->tcp_hdr.crc = 0;
817 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
819 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
821 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
822 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
823 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
826 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
827 ntohs (pkt6->ip6_hdr.paylgth));
828 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
833 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
836 receive_tcp_service (void *cls,
837 struct GNUNET_MESH_Tunnel *tunnel,
839 const struct GNUNET_PeerIdentity *sender,
840 const struct GNUNET_MessageHeader *message,
841 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
844 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
845 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
846 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
847 GNUNET_MessageHeader)
848 - sizeof (GNUNET_HashCode);
850 /** Get the configuration from the services-hashmap.
852 * Which service is needed only depends on the service-descriptor and the
855 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
857 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
858 *tcp_desc = ntohs (pkt->dpt);
859 struct redirect_service *serv =
860 GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
863 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
864 "No service found for TCP dpt %d!\n", *tcp_desc);
868 pkt->dpt = htons (serv->remote_port);
871 * At this point it would be possible to check against some kind of ACL.
877 /* Prepare the state.
878 * This will be saved in the hashmap, so that the receiving procedure knows
879 * through which tunnel this connection has to be routed.
881 struct redirect_state *state =
882 GNUNET_malloc (sizeof (struct redirect_state));
883 memset (state, 0, sizeof (struct redirect_state));
884 state->tunnel = tunnel;
886 state->type = SERVICE;
887 state->hashmap = tcp_connections;
888 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
890 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
891 sizeof (struct ip6_hdr) + pkt_len;
894 memset (buf, 0, len);
896 switch (serv->version)
899 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
900 &serv->v4.ip4address,
901 tunnel, state, (struct ip_pkt *) buf);
904 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
905 &serv->v6.ip6address,
906 tunnel, state, (struct ip6_pkt *) buf);
914 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
917 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
919 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
920 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
923 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
924 GNUNET_TIME_absolute_get ().abs_value);
926 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
927 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
932 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
937 receive_tcp_remote (void *cls,
938 struct GNUNET_MESH_Tunnel *tunnel,
940 const struct GNUNET_PeerIdentity *sender,
941 const struct GNUNET_MessageHeader *message,
942 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
944 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
945 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
946 struct remote_addr *s = (struct remote_addr *) desc;
949 unsigned int pkt_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - sizeof (GNUNET_HashCode);
951 struct redirect_state *state =
952 GNUNET_malloc (sizeof (struct redirect_state));
953 memset (state, 0, sizeof (struct redirect_state));
954 state->tunnel = tunnel;
955 state->type = REMOTE;
956 state->hashmap = tcp_connections;
957 memcpy (&state->remote, s, sizeof (struct remote_addr));
959 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
960 sizeof (struct ip6_hdr) + pkt_len;
963 memset (buf, 0, len);
968 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
969 &s->addr, tunnel, state, (struct ip_pkt *) buf);
972 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
973 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
980 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
983 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
985 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
986 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
989 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
990 GNUNET_TIME_absolute_get ().abs_value);
992 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
994 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
999 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1005 receive_udp_remote (void *cls,
1006 struct GNUNET_MESH_Tunnel *tunnel,
1008 const struct GNUNET_PeerIdentity *sender,
1009 const struct GNUNET_MessageHeader *message,
1010 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1012 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1013 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1014 struct remote_addr *s = (struct remote_addr *) desc;
1018 GNUNET_assert (ntohs (pkt->len) ==
1019 ntohs (message->size) -
1020 sizeof (struct GNUNET_MessageHeader) -
1021 sizeof (GNUNET_HashCode));
1023 /* Prepare the state.
1024 * This will be saved in the hashmap, so that the receiving procedure knows
1025 * through which tunnel this connection has to be routed.
1027 struct redirect_state *state =
1028 GNUNET_malloc (sizeof (struct redirect_state));
1029 memset (state, 0, sizeof (struct redirect_state));
1030 state->tunnel = tunnel;
1031 state->hashmap = udp_connections;
1032 state->type = REMOTE;
1033 memcpy (&state->remote, s, sizeof (struct remote_addr));
1035 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1036 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1039 memset (buf, 0, len);
1044 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1045 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1048 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1049 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1056 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1059 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1061 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1065 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1066 GNUNET_TIME_absolute_get ().abs_value);
1068 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1069 max_udp_connections)
1070 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1073 GNUNET_free (state);
1075 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1080 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1083 receive_udp_service (void *cls,
1084 struct GNUNET_MESH_Tunnel *tunnel,
1086 const struct GNUNET_PeerIdentity *sender,
1087 const struct GNUNET_MessageHeader *message,
1088 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1090 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1091 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1093 GNUNET_assert (ntohs (pkt->len) ==
1094 ntohs (message->size) -
1095 sizeof (struct GNUNET_MessageHeader) -
1096 sizeof (GNUNET_HashCode));
1098 /* Get the configuration from the hashmap */
1099 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1100 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1101 *udp_desc = ntohs (pkt->dpt);
1102 struct redirect_service *serv =
1103 GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
1106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1107 "No service found for UDP dpt %d!\n", *udp_desc);
1111 pkt->dpt = htons (serv->remote_port);
1114 * At this point it would be possible to check against some kind of ACL.
1120 /* Prepare the state.
1121 * This will be saved in the hashmap, so that the receiving procedure knows
1122 * through which tunnel this connection has to be routed.
1124 struct redirect_state *state =
1125 GNUNET_malloc (sizeof (struct redirect_state));
1126 memset (state, 0, sizeof (struct redirect_state));
1127 state->tunnel = tunnel;
1129 state->type = SERVICE;
1130 state->hashmap = udp_connections;
1131 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1133 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1134 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1137 memset (buf, 0, len);
1139 switch (serv->version)
1142 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1143 &serv->v4.ip4address,
1144 tunnel, state, (struct ip_pkt *) buf);
1147 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1148 &serv->v6.ip6address,
1149 tunnel, state, (struct ip6_pkt *) buf);
1157 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
1160 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1162 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1166 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1167 GNUNET_TIME_absolute_get ().abs_value);
1169 if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections)
1170 GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap);
1173 GNUNET_free (state);
1175 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1180 * @brief Main function that will be run by the scheduler.
1182 * @param cls closure
1183 * @param args remaining command-line arguments
1184 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1185 * @param cfg_ configuration
1190 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg_)
1192 const static struct GNUNET_MESH_MessageHandler handlers[] = {
1193 {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
1194 {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
1195 {receive_udp_remote, GNUNET_MESSAGE_TYPE_REMOTE_UDP, 0},
1196 {receive_tcp_remote, GNUNET_MESSAGE_TYPE_REMOTE_TCP, 0},
1200 const static GNUNET_MESH_ApplicationType apptypes[] =
1202 GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY,
1203 GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY,
1204 GNUNET_APPLICATION_TYPE_END
1207 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, apptypes);
1210 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1211 udp_connections_heap =
1212 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1213 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1214 tcp_connections_heap =
1215 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1216 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1217 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1219 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1220 &max_udp_connections);
1221 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1222 &max_tcp_connections);
1225 GNUNET_CONFIGURATION_get_value_filename (cfg, "dns", "SERVICES", &services);
1226 servicecfg = GNUNET_CONFIGURATION_create ();
1227 if (GNUNET_OK == GNUNET_CONFIGURATION_parse (servicecfg, services))
1229 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
1230 GNUNET_CONFIGURATION_iterate (servicecfg, read_service_conf, NULL);
1232 if (NULL != services)
1233 GNUNET_free (services);
1235 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1236 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1240 * The main function to obtain template from gnunetd.
1242 * @param argc number of arguments from the command line
1243 * @param argv command line arguments
1244 * @return 0 ok, 1 on error
1247 main (int argc, char *const *argv) {
1248 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1249 GNUNET_GETOPT_OPTION_END
1252 return (GNUNET_OK ==
1253 GNUNET_PROGRAM_run (argc,
1256 gettext_noop ("help text"),
1257 options, &run, NULL)) ? ret : 1;
1260 /* end of gnunet-daemon-exit.c */