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;
85 unsigned char addr[16];
90 * This struct is saved into the services-hashmap
92 struct redirect_service
117 * The source-address of this connection. When a packet to this address is
118 * received, this tunnel is used to forward it. ipv4-addresses will be put
119 * here left-aligned */
122 * The source-port of this connection
128 * This struct is saved into {tcp,udp}_connections;
130 struct redirect_state
132 struct GNUNET_MESH_Tunnel *tunnel;
133 GNUNET_HashCode desc;
134 struct redirect_service *serv;
135 struct remote_addr remote;
137 struct GNUNET_CONTAINER_HeapNode* heap_node;
138 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
139 GNUNET_HashCode hash;
142 * The source-address and -port of this connection
144 struct redirect_info redirect_info;
148 * This hashmaps saves interesting things about the configured services
150 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
151 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
154 * Function that frees everything from a hashmap
157 free_iterate(void* cls, const GNUNET_HashCode* hash, void* value)
164 * Function scheduled as very last function, cleans up after us
167 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
168 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
170 GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
174 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
178 if (mesh_handle != NULL)
180 GNUNET_MESH_disconnect(mesh_handle);
186 collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
187 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
191 struct GNUNET_CONTAINER_Heap *heap = cls;
193 struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap);
195 /* This is free()ed memory! */
196 state->heap_node = NULL;
198 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
200 GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state);
206 hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
209 /* the gnunet hashmap only uses the first 32bit of the hash
211 * build the hash out of the last two bytes of the address and the 2 bytes of
214 memcpy(&hash, &u_i->pt, sizeof(u_i->pt));
215 memcpy(((unsigned char*)&hash)+2, u_i->addr+(addrlen-2), 2);
219 * cls is the pointer to a GNUNET_MessageHeader that is
220 * followed by the service-descriptor and the udp-packet that should be sent;
223 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
225 struct GNUNET_MessageHeader *hdr = cls;
226 GNUNET_assert (size >= ntohs (hdr->size));
227 memcpy (buf, hdr, ntohs (hdr->size));
228 size = ntohs(hdr->size);
234 * @brief Handles an UDP-Packet received from the helper.
236 * @param udp A pointer to the Packet
237 * @param dadr The IP-Destination-address
238 * @param addrlen The length of the address
239 * @param version 4 or 6
242 udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen,
243 unsigned int version)
245 struct redirect_info u_i;
246 struct GNUNET_MESH_Tunnel *tunnel;
248 struct GNUNET_MessageHeader *msg;
250 memset (&u_i, 0, sizeof (struct redirect_info));
252 memcpy (&u_i.addr, dadr, addrlen);
256 /* get tunnel and service-descriptor from this */
257 GNUNET_HashCode hash;
258 hash_redirect_info(&hash, &u_i, addrlen);
260 struct redirect_state *state =
261 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
263 /* Mark this connection as freshly used */
264 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
265 GNUNET_TIME_absolute_get ().abs_value);
267 tunnel = state->tunnel;
269 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
270 if (ntohs (udp->spt) == state->serv->remote_port)
272 udp->spt = htons (state->serv->my_port);
276 /* otherwise the answer came from a different port (tftp does this)
277 * add this new port to the list of all services, so that the packets
278 * coming back from the client to this new port will be routed correctly
280 struct redirect_service *serv =
281 GNUNET_malloc (sizeof (struct redirect_service));
282 memcpy (serv, state->serv, sizeof (struct redirect_service));
283 serv->my_port = ntohs (udp->spt);
284 serv->remote_port = ntohs (udp->spt);
285 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
286 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
287 sizeof (GNUNET_HashCode));
288 *desc = ntohs (udp->spt);
289 GNUNET_assert (GNUNET_OK ==
290 GNUNET_CONTAINER_multihashmap_put (udp_services,
291 (GNUNET_HashCode*)desc, serv,
292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
296 /* send udp-packet back */
298 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
300 msg = GNUNET_malloc (len);
301 msg->size = htons (len);
302 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK);
303 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
304 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
305 void *_udp = desc + 1;
306 memcpy (_udp, udp, ntohs (udp->len));
308 GNUNET_MESH_notify_transmit_ready (tunnel,
311 GNUNET_TIME_relative_divide
312 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
313 (const struct GNUNET_PeerIdentity *)
315 send_udp_to_peer_notify_callback, msg);
319 * @brief Handles a TCP-Packet received from the helper.
321 * @param tcp A pointer to the Packet
322 * @param dadr The IP-Destination-address
323 * @param addrlen The length of the address
324 * @param version 4 or 6
325 * @param pktlen the length of the packet, including its header
328 tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
329 unsigned int version, size_t pktlen)
331 struct redirect_info u_i;
332 struct GNUNET_MESH_Tunnel *tunnel;
334 struct GNUNET_MessageHeader *msg;
336 memset (&u_i, 0, sizeof (struct redirect_info));
338 memcpy (&u_i.addr, dadr, addrlen);
341 /* get tunnel and service-descriptor from this */
342 GNUNET_HashCode hash;
343 hash_redirect_info(&hash, &u_i, addrlen);
345 struct redirect_state *state =
346 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
348 /* Mark this connection as freshly used */
349 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
350 GNUNET_TIME_absolute_get ().abs_value);
352 tunnel = state->tunnel;
354 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
355 if (ntohs (tcp->spt) == state->serv->remote_port)
357 tcp->spt = htons (state->serv->my_port);
361 // This is an illegal packet.
364 /* send tcp-packet back */
366 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
368 msg = GNUNET_malloc (len);
369 msg->size = htons (len);
370 msg->type = htons (GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK);
371 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
372 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
373 void *_tcp = desc + 1;
374 memcpy (_tcp, tcp, pktlen);
376 GNUNET_MESH_notify_transmit_ready (tunnel,
379 GNUNET_TIME_relative_divide
380 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
381 (const struct GNUNET_PeerIdentity *)NULL,
382 len, send_udp_to_peer_notify_callback,
388 * Receive packets from the helper-process
391 message_token (void *cls,
392 void *client, const struct GNUNET_MessageHeader *message)
394 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
396 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
398 /* ethertype is ipv6 */
399 if (ntohs (pkt_tun->tun.type) == 0x86dd)
401 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
402 if (0x11 == pkt6->ip6_hdr.nxthdr)
403 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
404 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6);
405 else if (0x06 == pkt6->ip6_hdr.nxthdr)
406 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
407 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 6,
408 ntohs (pkt6->ip6_hdr.paylgth));
410 else if (ntohs (pkt_tun->tun.type) == 0x0800)
412 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
413 uint32_t tmp = pkt4->ip_hdr.dadr;
414 if (0x11 == pkt4->ip_hdr.proto)
415 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
416 (unsigned char *) &tmp, 4, 4);
417 else if (0x06 == pkt4->ip_hdr.proto)
419 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
421 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
423 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
424 (unsigned char *) &tmp, 4, 4, pktlen);
434 * Reads the configuration servicecfg and populates udp_services
437 * @param section name of section in config, equal to hostname
438 * @param option type of redirect
439 * @param value specification of services, format is
440 * "OFFERED-PORT:HOSTNAME:HOST-PORT" (SPACE <more of those>)*
443 read_service_conf (void *cls, const char *section, const char *option,
450 GNUNET_HashCode hash;
451 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
452 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
453 (GNUNET_HashCode *) (desc + 1));
459 if (0 == strcmp ("UDP_REDIRECTS", option))
461 else if (0 == strcmp ("TCP_REDIRECTS", option))
468 cpy = GNUNET_strdup (value);
469 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
472 if (NULL == (hostname = strstr (redirect, ":")))
474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
480 if (NULL == (hostport = strstr (hostname, ":")))
482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
489 int local_port = atoi (redirect);
490 if (!((local_port > 0) && (local_port < 65536)))
491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
496 struct redirect_service *serv =
497 GNUNET_malloc (sizeof (struct redirect_service));
498 memset (serv, 0, sizeof (struct redirect_service));
499 serv->my_port = local_port;
501 if (0 == strcmp ("localhost4", hostname))
506 GNUNET_assert (GNUNET_OK ==
507 GNUNET_CONFIGURATION_get_value_string (cfg,
512 inet_pton (AF_INET, ip4addr,
513 serv->v4.ip4address));
514 GNUNET_free (ip4addr);
516 else if (0 == strcmp ("localhost6", hostname))
521 GNUNET_assert (GNUNET_OK ==
522 GNUNET_CONFIGURATION_get_value_string (cfg,
527 inet_pton (AF_INET6, ip6addr,
528 serv->v6.ip6address));
529 GNUNET_free (ip6addr);
533 // TODO Lookup, yadayadayada
536 serv->remote_port = atoi (hostport);
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
538 *((unsigned long long *) (desc + 1)));
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key2 %x\n",
540 *((unsigned long long *) &hash));
542 GNUNET_assert (GNUNET_OK ==
543 GNUNET_CONTAINER_multihashmap_put (udp_services,
544 (GNUNET_HashCode*)desc, serv,
545 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
547 GNUNET_assert (GNUNET_OK ==
548 GNUNET_CONTAINER_multihashmap_put (tcp_services,
549 (GNUNET_HashCode*)desc, serv,
550 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
558 * Start the helper-process
560 * If cls != NULL it is assumed that this function is called as a result of a dying
561 * helper. cls is then taken as handle to the old helper and is cleaned up.
564 start_helper_and_schedule(void *cls,
565 const struct GNUNET_SCHEDULER_TaskContext *tc) {
566 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
579 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
581 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
585 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
587 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
591 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
593 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
597 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
599 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
603 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
605 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
610 * Messages get passed to the function message_token
611 * When the helper dies, this function will be called again with the
612 * helper_handle as cls.
614 helper_handle = start_helper(ifname,
620 start_helper_and_schedule,
625 GNUNET_free(ipv6addr);
626 GNUNET_free(ipv6prefix);
627 GNUNET_free(ipv4addr);
628 GNUNET_free(ipv4mask);
633 prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
634 uint16_t protocol, void *ipaddress, void *tunnel,
635 struct redirect_state *state, struct ip_pkt *pkt4)
639 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
640 pkt4->shdr.size = htons (len);
642 pkt4->tun.type = htons (0x0800);
644 memcpy (&pkt4->data, payload, pktlen);
646 pkt4->ip_hdr.version = 4;
647 pkt4->ip_hdr.hdr_lngth = 5;
648 pkt4->ip_hdr.diff_serv = 0;
649 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
650 pkt4->ip_hdr.ident = 0;
651 pkt4->ip_hdr.flags = 0;
652 pkt4->ip_hdr.frag_off = 0;
653 pkt4->ip_hdr.ttl = 255;
654 pkt4->ip_hdr.proto = protocol;
655 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
657 memcpy (&tmp, ipaddress, 4);
658 pkt4->ip_hdr.dadr = tmp;
660 /* Generate a new src-address */
663 GNUNET_assert (GNUNET_OK ==
664 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
667 GNUNET_assert (GNUNET_OK ==
668 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
671 inet_pton (AF_INET, ipv4addr, &tmp);
672 inet_pton (AF_INET, ipv4mask, &tmp2);
673 GNUNET_free (ipv4addr);
674 GNUNET_free (ipv4mask);
676 /* This should be a noop */
679 tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
681 pkt4->ip_hdr.sadr = tmp;
683 memcpy (&state->redirect_info.addr, &tmp, 4);
684 if (0x11 == protocol)
686 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
687 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
689 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
691 else if (0x06 == protocol)
693 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
694 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
696 pkt4_tcp->tcp_hdr.crc = 0;
698 tmp = pkt4->ip_hdr.sadr;
700 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
701 tmp = pkt4->ip_hdr.dadr;
703 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
705 tmp = (protocol << 16) | (0xffff & pktlen);
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
711 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
714 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
715 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
719 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
723 prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
724 uint16_t protocol, void *ipaddress, void *tunnel,
725 struct redirect_state *state, struct ip6_pkt *pkt6)
729 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
730 pkt6->shdr.size = htons (len);
733 pkt6->tun.type = htons (0x86dd);
735 memcpy (&pkt6->data, payload, pktlen);
737 pkt6->ip6_hdr.version = 6;
738 pkt6->ip6_hdr.nxthdr = protocol;
739 pkt6->ip6_hdr.paylgth = htons (pktlen);
740 pkt6->ip6_hdr.hoplmt = 64;
742 memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
744 /* Generate a new src-address
745 * This takes as much from the address of the tunnel as fits into
748 unsigned long long ipv6prefix;
749 GNUNET_assert (GNUNET_OK ==
750 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
753 GNUNET_assert (GNUNET_OK ==
754 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
757 GNUNET_assert (ipv6prefix < 127);
758 ipv6prefix = (ipv6prefix + 7) / 8;
760 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
761 GNUNET_free (ipv6addr);
763 if (ipv6prefix < (16 - sizeof (void *)))
764 ipv6prefix = 16 - sizeof (void *);
766 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
767 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
768 ((char *) &tunnel) + offset, 16 - ipv6prefix);
770 /* copy the needed information into the state */
771 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
773 if (0x11 == protocol)
775 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
776 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
778 pkt6_udp->udp_hdr.crc = 0;
781 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
783 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
784 tmp = (htons (pktlen) & 0xffff);
785 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
786 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
787 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
790 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
791 ntohs (pkt6_udp->udp_hdr.len));
792 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
794 else if (0x06 == protocol)
796 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
797 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
799 pkt6_tcp->tcp_hdr.crc = 0;
802 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
804 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
806 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
807 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
808 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
811 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
812 ntohs (pkt6->ip6_hdr.paylgth));
813 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
818 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
821 receive_tcp_service (void *cls,
822 struct GNUNET_MESH_Tunnel *tunnel,
824 const struct GNUNET_PeerIdentity *sender,
825 const struct GNUNET_MessageHeader *message,
826 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
829 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
830 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
831 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
832 GNUNET_MessageHeader)
833 - sizeof (GNUNET_HashCode);
835 /** Get the configuration from the services-hashmap.
837 * Which service is needed only depends on the service-descriptor and the
840 uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
842 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
843 *tcp_desc = ntohs (pkt->dpt);
844 struct redirect_service *serv =
845 GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
848 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
849 "No service found for TCP dpt %d!\n", *tcp_desc);
853 pkt->dpt = htons (serv->remote_port);
856 * At this point it would be possible to check against some kind of ACL.
862 /* Prepare the state.
863 * This will be saved in the hashmap, so that the receiving procedure knows
864 * through which tunnel this connection has to be routed.
866 struct redirect_state *state =
867 GNUNET_malloc (sizeof (struct redirect_state));
868 memset (state, 0, sizeof (struct redirect_state));
869 state->tunnel = tunnel;
871 state->hashmap = tcp_connections;
872 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
874 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
875 sizeof (struct ip6_hdr) + pkt_len;
878 memset (buf, 0, len);
880 switch (serv->version)
883 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
884 &serv->v4.ip4address,
885 tunnel, state, (struct ip_pkt *) buf);
888 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
889 &serv->v6.ip6address,
890 tunnel, state, (struct ip6_pkt *) buf);
898 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
901 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
903 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
904 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
907 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
908 GNUNET_TIME_absolute_get ().abs_value);
910 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
911 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
916 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
921 receive_tcp_remote (void *cls,
922 struct GNUNET_MESH_Tunnel *tunnel,
924 const struct GNUNET_PeerIdentity *sender,
925 const struct GNUNET_MessageHeader *message,
926 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
928 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
929 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
930 struct remote_addr *s = (struct remote_addr *) desc;
933 unsigned int pkt_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - sizeof (GNUNET_HashCode);
935 struct redirect_state *state =
936 GNUNET_malloc (sizeof (struct redirect_state));
937 memset (state, 0, sizeof (struct redirect_state));
938 state->tunnel = tunnel;
939 state->hashmap = tcp_connections;
940 memcpy (&state->remote, s, sizeof (struct remote_addr));
942 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
943 sizeof (struct ip6_hdr) + pkt_len;
946 memset (buf, 0, len);
951 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
952 &s->addr, tunnel, state, (struct ip_pkt *) buf);
955 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x06, /* TCP */
956 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
963 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
966 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
968 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
969 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
972 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
973 GNUNET_TIME_absolute_get ().abs_value);
975 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
977 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
982 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
988 receive_udp_remote (void *cls,
989 struct GNUNET_MESH_Tunnel *tunnel,
991 const struct GNUNET_PeerIdentity *sender,
992 const struct GNUNET_MessageHeader *message,
993 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
995 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
996 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
997 struct remote_addr *s = (struct remote_addr *) desc;
1001 GNUNET_assert (ntohs (pkt->len) ==
1002 ntohs (message->size) -
1003 sizeof (struct GNUNET_MessageHeader) -
1004 sizeof (GNUNET_HashCode));
1006 /* Prepare the state.
1007 * This will be saved in the hashmap, so that the receiving procedure knows
1008 * through which tunnel this connection has to be routed.
1010 struct redirect_state *state =
1011 GNUNET_malloc (sizeof (struct redirect_state));
1012 memset (state, 0, sizeof (struct redirect_state));
1013 state->tunnel = tunnel;
1014 state->hashmap = udp_connections;
1015 memcpy (&state->remote, s, sizeof (struct remote_addr));
1017 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1018 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1021 memset (buf, 0, len);
1026 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1027 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1030 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1031 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1038 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1041 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1043 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1044 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1047 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1048 GNUNET_TIME_absolute_get ().abs_value);
1050 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1051 max_udp_connections)
1052 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1055 GNUNET_free (state);
1057 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1062 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1065 receive_udp_service (void *cls,
1066 struct GNUNET_MESH_Tunnel *tunnel,
1068 const struct GNUNET_PeerIdentity *sender,
1069 const struct GNUNET_MessageHeader *message,
1070 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1072 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1073 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
1075 GNUNET_assert (ntohs (pkt->len) ==
1076 ntohs (message->size) -
1077 sizeof (struct GNUNET_MessageHeader) -
1078 sizeof (GNUNET_HashCode));
1080 /* Get the configuration from the hashmap */
1081 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1082 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1083 *udp_desc = ntohs (pkt->dpt);
1084 struct redirect_service *serv =
1085 GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
1088 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1089 "No service found for UDP dpt %d!\n", *udp_desc);
1093 pkt->dpt = htons (serv->remote_port);
1096 * At this point it would be possible to check against some kind of ACL.
1102 /* Prepare the state.
1103 * This will be saved in the hashmap, so that the receiving procedure knows
1104 * through which tunnel this connection has to be routed.
1106 struct redirect_state *state =
1107 GNUNET_malloc (sizeof (struct redirect_state));
1108 memset (state, 0, sizeof (struct redirect_state));
1109 state->tunnel = tunnel;
1111 state->hashmap = udp_connections;
1112 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1114 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1115 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1118 memset (buf, 0, len);
1120 switch (serv->version)
1123 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1124 &serv->v4.ip4address,
1125 tunnel, state, (struct ip_pkt *) buf);
1128 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1129 &serv->v6.ip6address,
1130 tunnel, state, (struct ip6_pkt *) buf);
1138 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
1141 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1143 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1144 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1147 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1148 GNUNET_TIME_absolute_get ().abs_value);
1150 if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections)
1151 GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap);
1154 GNUNET_free (state);
1156 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
1161 * @brief Main function that will be run by the scheduler.
1163 * @param cls closure
1164 * @param args remaining command-line arguments
1165 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1166 * @param cfg_ configuration
1171 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg_)
1173 const static struct GNUNET_MESH_MessageHandler handlers[] = {
1174 {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
1175 {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
1176 {receive_udp_remote, GNUNET_MESSAGE_TYPE_REMOTE_UDP, 0},
1177 {receive_tcp_remote, GNUNET_MESSAGE_TYPE_REMOTE_TCP, 0},
1180 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, NULL);
1183 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1184 udp_connections_heap =
1185 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1186 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1187 tcp_connections_heap =
1188 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1189 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1190 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1192 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1193 &max_udp_connections);
1194 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1195 &max_tcp_connections);
1198 GNUNET_CONFIGURATION_get_value_filename (cfg, "dns", "SERVICES", &services);
1199 servicecfg = GNUNET_CONFIGURATION_create ();
1200 if (GNUNET_OK == GNUNET_CONFIGURATION_parse (servicecfg, services))
1202 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
1203 GNUNET_CONFIGURATION_iterate (servicecfg, read_service_conf, NULL);
1205 if (NULL != services)
1206 GNUNET_free (services);
1208 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
1209 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1213 * The main function to obtain template from gnunetd.
1215 * @param argc number of arguments from the command line
1216 * @param argv command line arguments
1217 * @return 0 ok, 1 on error
1220 main (int argc, char *const *argv) {
1221 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1222 GNUNET_GETOPT_OPTION_END
1225 return (GNUNET_OK ==
1226 GNUNET_PROGRAM_run (argc,
1229 gettext_noop ("help text"),
1230 options, &run, NULL)) ? ret : 1;
1233 /* end of gnunet-daemon-exit.c */