2 This file is part of GNUnet.
3 (C) 2010, 2012 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 exit/gnunet-daemon-exit.c
23 * @brief tool to allow IP traffic exit from the GNUnet mesh to the Internet
24 * @author Philipp Toelke
25 * @author Christian Grothoff
28 * - need proper message headers for mesh P2P messages
29 * - factor out crc computations from DNS/EXIT into shared library?
30 * - which code should advertise services? the service model is right
31 * now a bit odd, especially as this code DOES the exit and knows
32 * the DNS "name", but OTOH this is clearly NOT the place to advertise
33 * the service's existence; maybe the daemon should turn into a
34 * service with an API to add local-exit services dynamically?
37 #include "gnunet_util_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_applications.h"
40 #include "gnunet_mesh_service.h"
41 #include "gnunet_constants.h"
42 #include "tcpip_tun.h"
45 * Information about an address.
50 * AF_INET or AF_INET6.
55 * Remote address information.
60 * Address, if af is AF_INET.
65 * Address, if af is AF_INET6.
71 * IPPROTO_TCP or IPPROTO_UDP;
76 * Remote port, in host byte order!
83 * This struct is saved into the services-hashmap to represent
84 * a service this peer is specifically offering an exit for
85 * (for a specific domain name).
91 * Remote address to use for the service.
93 struct SocketAddress address;
96 * DNS name of the service.
101 * Port I am listening on within GNUnet for this service, in host
102 * byte order. (as we may redirect ports).
109 * Information we use to track a connection (the classical 6-tuple of
110 * IP-version, protocol, source-IP, destination-IP, source-port and
113 struct RedirectInformation
117 * Address information for the other party (equivalent of the
118 * arguments one would give to "connect").
120 struct SocketAddress remote_address;
123 * Address information we used locally (AF and proto must match
124 * "remote_address"). Equivalent of the arguments one would give to
127 struct SocketAddress local_address;
130 Note 1: additional information might be added here in the
131 future to support protocols that require special handling,
134 Note 2: we might also sometimes not match on all components
135 of the tuple, to support protocols where things do not always
142 * Queue of messages to a tunnel.
144 struct TunnelMessageQueue
147 * This is a doubly-linked list.
149 struct TunnelMessageQueue *next;
152 * This is a doubly-linked list.
154 struct TunnelMessageQueue *prev;
157 * Payload to send via the tunnel.
162 * Number of bytes in 'payload'.
169 * This struct is saved into connections_map to allow finding the
170 * right tunnel given an IP packet from TUN. It is also associated
171 * with the tunnel's closure so we can find it again for the next
172 * message from the tunnel.
177 * Mesh tunnel that is used for this connection.
179 struct GNUNET_MESH_Tunnel *tunnel;
182 * Heap node for this state in the connections_heap.
184 struct GNUNET_CONTAINER_HeapNode *heap_node;
187 * Key this state has in the connections_map.
189 GNUNET_HashCode state_key;
192 * Associated service record, or NULL for no service.
194 struct LocalService *serv;
197 * Head of DLL of messages for this tunnel.
199 struct TunnelMessageQueue *head;
202 * Tail of DLL of messages for this tunnel.
204 struct TunnelMessageQueue *tail;
207 * Active tunnel transmission request (or NULL).
209 struct GNUNET_MESH_TransmitHandle *th;
212 * Primary redirection information for this connection.
214 struct RedirectInformation ri;
220 * The handle to the configuration used throughout the process
222 static const struct GNUNET_CONFIGURATION_Handle *cfg;
225 * The handle to the helper
227 static struct GNUNET_HELPER_Handle *helper_handle;
230 * Arguments to the exit helper.
232 static char *exit_argv[7];
235 * IPv6 prefix (0..127) from configuration file.
237 static unsigned long long ipv6prefix;
242 static struct GNUNET_MESH_Handle *mesh_handle;
245 * This hashmaps contains the mapping from peer, service-descriptor,
246 * source-port and destination-port to a struct TunnelState
248 static struct GNUNET_CONTAINER_MultiHashMap *connections_map;
251 * Heap so we can quickly find "old" connections.
253 static struct GNUNET_CONTAINER_Heap *connections_heap;
256 * If there are at least this many connections, old ones will be removed
258 static long long unsigned int max_connections = 200;
261 * This hashmaps saves interesting things about the configured UDP services
263 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
266 * This hashmaps saves interesting things about the configured TCP services
268 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
272 * Given IP information about a connection, calculate the respective
273 * hash we would use for the 'connections_map'.
275 * @param hash resulting hash
276 * @param ri information about the connection
279 hash_redirect_info (GNUNET_HashCode *hash,
280 const struct RedirectInformation *ri)
284 memset (hash, 0, sizeof (GNUNET_HashCode));
285 /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
286 so we put the IP address in there (and hope for few collisions) */
288 switch (ri->remote_address.af)
291 memcpy (off, &ri->remote_address.address.ipv4, sizeof (struct in_addr));
292 off += sizeof (struct in_addr);
295 memcpy (off, &ri->remote_address.address.ipv6, sizeof (struct in6_addr));
296 off += sizeof (struct in_addr);
301 memcpy (off, &ri->remote_address.port, sizeof (uint16_t));
302 off += sizeof (uint16_t);
303 switch (ri->local_address.af)
306 memcpy (off, &ri->local_address.address.ipv4, sizeof (struct in_addr));
307 off += sizeof (struct in_addr);
310 memcpy (off, &ri->local_address.address.ipv6, sizeof (struct in6_addr));
311 off += sizeof (struct in_addr);
316 memcpy (off, &ri->local_address.port, sizeof (uint16_t));
317 off += sizeof (uint16_t);
318 memcpy (off, &ri->remote_address.proto, sizeof (uint8_t));
319 off += sizeof (uint8_t);
324 * Get our connection tracking state. Warns if it does not exists,
325 * refreshes the timestamp if it does exist.
327 * @param af address family
328 * @param protocol IPPROTO_UDP or IPPROTO_TCP
329 * @param destination_ip target IP
330 * @param destination_port target port
331 * @param local_ip local IP
332 * @param local_port local port
333 * @param state_key set to hash's state if non-NULL
334 * @return NULL if we have no tracking information for this tuple
336 static struct TunnelState *
337 get_redirect_state (int af,
339 const void *destination_ip,
340 uint16_t destination_port,
341 const void *local_ip,
343 GNUNET_HashCode *state_key)
345 struct RedirectInformation ri;
347 struct TunnelState *state;
349 ri.remote_address.af = af;
351 ri.remote_address.address.ipv4 = *((struct in_addr*) destination_ip);
353 ri.remote_address.address.ipv6 = * ((struct in6_addr*) destination_ip);
354 ri.remote_address.port = destination_port;
355 ri.remote_address.proto = protocol;
356 ri.local_address.af = af;
358 ri.local_address.address.ipv4 = *((struct in_addr*) local_ip);
360 ri.local_address.address.ipv6 = * ((struct in6_addr*) local_ip);
361 ri.local_address.port = local_port;
362 ri.local_address.proto = protocol;
363 hash_redirect_info (&key, &ri);
364 if (NULL != state_key)
366 state = GNUNET_CONTAINER_multihashmap_get (connections_map, &key);
369 /* Mark this connection as freshly used */
370 if (NULL == state_key)
371 GNUNET_CONTAINER_heap_update_cost (connections_heap,
373 GNUNET_TIME_absolute_get ().abs_value);
379 * Given a service descriptor and a destination port, find the
380 * respective service entry.
382 * @param service_map map of services (TCP or UDP)
383 * @param desc service descriptor
384 * @param dpt destination port
385 * @return NULL if we are not aware of such a service
387 static struct LocalService *
388 find_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
389 const GNUNET_HashCode *desc,
392 char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
394 memcpy (&key[0], &dpt, sizeof (uint16_t));
395 memcpy (&key[sizeof(uint16_t)], desc, sizeof (GNUNET_HashCode));
396 return GNUNET_CONTAINER_multihashmap_get (service_map,
397 (GNUNET_HashCode *) key);
402 * Free memory associated with a service record.
405 * @param key service descriptor
406 * @param value service record to free
410 free_service_record (void *cls,
411 const GNUNET_HashCode *key,
414 struct LocalService *service = value;
416 GNUNET_free_non_null (service->name);
417 GNUNET_free (service);
423 * Given a service descriptor and a destination port, find the
424 * respective service entry.
426 * @param service_map map of services (TCP or UDP)
427 * @param name name of the service
428 * @param dpt destination port
429 * @param service service information record to store (service->name will be set).
432 store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
435 struct LocalService *service)
437 char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
438 GNUNET_HashCode desc;
440 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &desc);
441 service->name = GNUNET_strdup (name);
442 memcpy (&key[0], &dpt, sizeof (uint16_t));
443 memcpy (&key[sizeof(uint16_t)], &desc, sizeof (GNUNET_HashCode));
445 GNUNET_CONTAINER_multihashmap_put (service_map,
446 (GNUNET_HashCode *) key,
448 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
450 free_service_record (NULL, (GNUNET_HashCode *) key, service);
451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
452 _("Got duplicate service records for `%s:%u'\n"),
460 * MESH is ready to receive a message for the tunnel. Transmit it.
462 * @param cls the 'struct TunnelState'.
463 * @param size number of bytes available in buf
464 * @param buf where to copy the message
465 * @return number of bytes copied to buf
468 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
470 struct TunnelState *s = cls;
471 struct GNUNET_MESH_Tunnel *tunnel = s->tunnel;
472 struct TunnelMessageQueue *tnq;
476 GNUNET_assert (size >= tnq->len);
477 memcpy (buf, tnq->payload, tnq->len);
479 GNUNET_CONTAINER_DLL_remove (s->head,
483 if (NULL != (tnq = s->head))
484 s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
485 GNUNET_NO /* corking */,
487 GNUNET_TIME_UNIT_FOREVER_REL,
490 &send_to_peer_notify_callback,
497 * Send the given packet via the mesh tunnel.
499 * @param mesh_tunnel destination
500 * @param payload message to transmit
501 * @param payload_length number of bytes in payload
502 * @param desc descriptor to add before payload (optional)
503 * @param mtype message type to use
506 send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
508 size_t payload_length,
509 const GNUNET_HashCode *desc,
512 struct TunnelState *s;
513 struct TunnelMessageQueue *tnq;
514 struct GNUNET_MessageHeader *msg;
518 len = sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + payload_length;
519 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
524 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + len);
525 tnq->payload = &tnq[1];
527 msg = (struct GNUNET_MessageHeader *) &tnq[1];
528 msg->size = htons ((uint16_t) len);
529 msg->type = htons (mtype);
532 dp = (GNUNET_HashCode *) &msg[1];
534 memcpy (&dp[1], payload, payload_length);
538 memcpy (&msg[1], payload, payload_length);
540 s = GNUNET_MESH_tunnel_get_data (mesh_tunnel);
541 GNUNET_assert (NULL != s);
542 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, tnq);
544 s->th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel, GNUNET_NO /* cork */, 0 /* priority */,
545 GNUNET_TIME_UNIT_FOREVER_REL,
547 &send_to_peer_notify_callback,
553 * @brief Handles an UDP packet received from the helper.
555 * @param udp A pointer to the Packet
556 * @param pktlen number of bytes in 'udp'
557 * @param af address family (AFINET or AF_INET6)
558 * @param destination_ip destination IP-address of the IP packet (should
559 * be our local address)
560 * @param source_ip original source IP-address of the IP packet (should
561 * be the original destination address)
564 udp_from_helper (const struct udp_packet *udp,
567 const void *destination_ip,
568 const void *source_ip)
570 struct TunnelState *state;
572 if (pktlen < sizeof (struct udp_packet))
578 if (pktlen != ntohs (udp->len))
584 state = get_redirect_state (af, IPPROTO_UDP,
592 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
593 _("Packet dropped, have no matching connection information\n"));
596 send_packet_to_mesh_tunnel (state->tunnel,
597 &udp[1], pktlen - sizeof (struct udp_packet),
600 ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK
601 : GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
606 * @brief Handles a TCP packet received from the helper.
608 * @param tcp A pointer to the Packet
609 * @param pktlen the length of the packet, including its header
610 * @param af address family (AFINET or AF_INET6)
611 * @param destination_ip destination IP-address of the IP packet (should
612 * be our local address)
613 * @param source_ip original source IP-address of the IP packet (should
614 * be the original destination address)
617 tcp_from_helper (const struct tcp_packet *tcp,
620 const void *destination_ip,
621 const void *source_ip)
623 struct TunnelState *state;
625 struct tcp_packet *mtcp;
627 if (pktlen < sizeof (struct tcp_packet))
633 state = get_redirect_state (af, IPPROTO_TCP,
641 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
642 _("Packet dropped, have no matching connection information\n"));
646 /* mug port numbers and crc to avoid information leakage;
647 sender will need to lookup the correct values anyway */
648 memcpy (buf, tcp, pktlen);
649 mtcp = (struct tcp_packet *) buf;
653 send_packet_to_mesh_tunnel (state->tunnel,
657 ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK
658 : GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
663 * Receive packets from the helper-process
666 * @param client unsued
667 * @param message message received from helper
670 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
671 const struct GNUNET_MessageHeader *message)
673 const struct tun_header *pkt_tun;
676 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
681 size = ntohs (message->size);
682 if (size < sizeof (struct tun_header) + sizeof (struct GNUNET_MessageHeader))
687 pkt_tun = (const struct tun_header *) &message[1];
688 size -= sizeof (struct tun_header) + sizeof (struct GNUNET_MessageHeader);
689 switch (ntohs (pkt_tun->proto))
693 const struct ip6_header *pkt6;
695 if (size < sizeof (struct ip6_header))
697 /* Kernel to blame? */
701 pkt6 = (struct ip6_header *) &pkt_tun[1];
702 if (size != ntohs (pkt6->payload_length))
704 /* Kernel to blame? */
708 size -= sizeof (struct ip6_header);
709 switch (pkt6->next_header)
712 udp_from_helper ((const struct udp_packet *) &pkt6[1], size,
714 &pkt6->destination_address,
715 &pkt6->source_address);
718 tcp_from_helper ((const struct tcp_packet *) &pkt6[1], size,
720 &pkt6->destination_address,
721 &pkt6->source_address);
724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
725 _("IPv6 packet with unsupported next header received. Ignored.\n"));
732 const struct ip4_header *pkt4;
734 if (size < sizeof (struct ip4_header))
736 /* Kernel to blame? */
740 pkt4 = (const struct ip4_header *) &pkt_tun[1];
741 if (size != ntohs (pkt4->total_length))
743 /* Kernel to blame? */
747 if (pkt4->header_length * 4 != sizeof (struct ip4_header))
749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
750 _("IPv4 packet options received. Ignored.\n"));
753 size -= sizeof (struct ip4_header);
754 switch (pkt4->protocol)
757 udp_from_helper ((const struct udp_packet *) &pkt4[1], size,
759 &pkt4->destination_address,
760 &pkt4->source_address);
762 tcp_from_helper ((const struct tcp_packet *) &pkt4[1], size,
764 &pkt4->destination_address,
765 &pkt4->source_address);
768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
769 _("IPv4 packet with unsupported next header received. Ignored.\n"));
775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
776 _("Packet from unknown protocol %u received. Ignored.\n"),
777 ntohs (pkt_tun->proto));
784 * We need to create a (unique) fresh local address (IP+port).
787 * @param af desired address family
788 * @param proto desired protocol (IPPROTO_UDP or IPPROTO_TCP)
789 * @param local_address address to initialize
792 setup_fresh_address (int af,
794 struct SocketAddress *local_address)
796 local_address->af = af;
797 local_address->proto = (uint8_t) proto;
798 /* default "local" port range is often 32768--61000,
799 so we pick a random value in that range */
801 = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
807 const char *ipv4addr = exit_argv[4];
808 const char *ipv4mask = exit_argv[5];
813 GNUNET_assert (1 == inet_pton (AF_INET, ipv4addr, &addr));
814 GNUNET_assert (1 == inet_pton (AF_INET, ipv4mask, &mask));
815 if (0 == ~mask.s_addr)
817 /* only one valid IP anyway */
818 local_address->address.ipv4 = addr;
821 /* Given 192.168.0.1/255.255.0.0, we want a mask
822 of '192.168.255.255', thus: */
823 mask.s_addr = addr.s_addr | ~mask.s_addr;
824 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
827 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
829 local_address->address.ipv4.s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
831 while ( (local_address->address.ipv4.s_addr == addr.s_addr) ||
832 (local_address->address.ipv4.s_addr == mask.s_addr) );
837 const char *ipv6addr = exit_argv[2];
838 struct in6_addr addr;
839 struct in6_addr mask;
843 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6addr, &addr));
844 GNUNET_assert (ipv6prefix < 128);
845 if (ipv6prefix == 127)
847 /* only one valid IP anyway */
848 local_address->address.ipv6 = addr;
851 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
854 for (i=127;i>=128-ipv6prefix;i--)
855 mask.s6_addr[i / 8] |= (1 << (i % 8));
857 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
862 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
864 local_address->address.ipv6.s6_addr[i]
865 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
868 while ( (0 == memcmp (&local_address->address.ipv6,
870 sizeof (struct in6_addr))) ||
871 (0 == memcmp (&local_address->address.ipv6,
873 sizeof (struct in6_addr))) );
883 * We are starting a fresh connection (TCP or UDP) and need
884 * to pick a source port and IP address (within the correct
885 * range and address family) to associate replies with the
886 * connection / correct mesh tunnel. This function generates
887 * a "fresh" source IP and source port number for a connection
888 * After picking a good source address, this function sets up
889 * the state in the 'connections_map' and 'connections_heap'
890 * to allow finding the state when needed later. The function
891 * also makes sure that we remain within memory limits by
892 * cleaning up 'old' states.
894 * @param state skeleton state to setup a record for; should
895 * 'state->ri.remote_address' filled in so that
896 * this code can determine which AF/protocol is
897 * going to be used (the 'tunnel' should also
898 * already be set); after calling this function,
899 * heap_node and the local_address will be
900 * also initialized (heap_node != NULL can be
901 * used to test if a state has been fully setup).
904 setup_state_record (struct TunnelState *state)
907 struct TunnelState *s;
909 /* generate fresh, unique address */
912 setup_fresh_address (state->serv->address.af,
913 state->serv->address.proto,
914 &state->ri.local_address);
915 } while (NULL != get_redirect_state (state->ri.remote_address.af,
917 &state->ri.remote_address.address,
918 state->ri.remote_address.port,
919 &state->ri.local_address.address,
920 state->ri.local_address.port,
922 GNUNET_assert (GNUNET_OK ==
923 GNUNET_CONTAINER_multihashmap_put (connections_map,
925 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
926 state->heap_node = GNUNET_CONTAINER_heap_insert (connections_heap,
928 GNUNET_TIME_absolute_get ().abs_value);
929 while (GNUNET_CONTAINER_heap_get_size (connections_heap) > max_connections)
931 s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
932 GNUNET_assert (state != s);
934 GNUNET_MESH_tunnel_destroy (s->tunnel);
935 GNUNET_assert (GNUNET_OK ==
936 GNUNET_CONTAINER_multihashmap_remove (connections_map,
945 * Prepare an IPv4 packet for transmission via the TUN interface.
946 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
947 * For UDP, the UDP header will be fully created, whereas for TCP
948 * only the ports and checksum will be filled in. So for TCP,
949 * a skeleton TCP header must be part of the provided payload.
951 * @param payload payload of the packet (starting with UDP payload or
952 * TCP header, depending on protocol)
953 * @param payload_length number of bytes in 'payload'
954 * @param protocol IPPROTO_UDP or IPPROTO_TCP
955 * @param src_address source address to use (IP and port)
956 * @param dst_address destination address to use (IP and port)
957 * @param pkt6 where to write the assembled packet; must
958 * contain enough space for the IP header, UDP/TCP header
962 prepare_ipv4_packet (const void *payload, size_t payload_length,
964 const struct SocketAddress *src_address,
965 const struct SocketAddress *dst_address,
966 struct ip4_header *pkt4)
970 len = payload_length;
974 len += sizeof (struct udp_packet);
977 /* tcp_header (with port/crc not set) must be part of payload! */
978 if (len < sizeof (struct tcp_packet))
988 if (len + sizeof (struct ip4_header) > UINT16_MAX)
995 pkt4->header_length = sizeof (struct ip4_header) / 4;
997 pkt4->total_length = htons ((uint16_t) (sizeof (struct ip4_header) + len));
998 pkt4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1001 pkt4->fragmentation_offset = 0;
1003 pkt4->protocol = protocol;
1005 pkt4->destination_address = dst_address->address.ipv4;
1006 pkt4->source_address = src_address->address.ipv4;
1007 pkt4->checksum = GNUNET_CRYPTO_crc16_n (pkt4, sizeof (struct ip4_header));
1013 struct udp_packet *pkt4_udp = (struct udp_packet *) &pkt4[1];
1015 pkt4_udp->spt = htons (src_address->port);
1016 pkt4_udp->dpt = htons (dst_address->port);
1017 pkt4_udp->crc = 0; /* Optional for IPv4 */
1018 pkt4_udp->len = htons ((uint16_t) payload_length);
1019 memcpy (&pkt4_udp[1], payload, payload_length);
1024 struct tcp_packet *pkt4_tcp = (struct tcp_packet *) &pkt4[1];
1026 memcpy (pkt4_tcp, payload, payload_length);
1027 pkt4_tcp->spt = htons (src_address->port);
1028 pkt4_tcp->dpt = htons (dst_address->port);
1031 sum = GNUNET_CRYPTO_crc16_step (sum,
1032 &pkt4->source_address,
1033 sizeof (struct in_addr) * 2);
1034 uint32_t tmp = htonl ((protocol << 16) | (0xffff & len));
1035 sum = GNUNET_CRYPTO_crc16_step (sum, & tmp, sizeof (uint32_t));
1036 sum = GNUNET_CRYPTO_crc16_step (sum, & pkt4_tcp, len);
1037 pkt4_tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1047 * Prepare an IPv6 packet for transmission via the TUN interface.
1048 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
1049 * For UDP, the UDP header will be fully created, whereas for TCP
1050 * only the ports and checksum will be filled in. So for TCP,
1051 * a skeleton TCP header must be part of the provided payload.
1053 * @param payload payload of the packet (starting with UDP payload or
1054 * TCP header, depending on protocol)
1055 * @param payload_length number of bytes in 'payload'
1056 * @param protocol IPPROTO_UDP or IPPROTO_TCP
1057 * @param src_address source address to use (IP and port)
1058 * @param dst_address destination address to use (IP and port)
1059 * @param pkt6 where to write the assembled packet; must
1060 * contain enough space for the IP header, UDP/TCP header
1064 prepare_ipv6_packet (const void *payload, size_t payload_length,
1066 const struct SocketAddress *src_address,
1067 const struct SocketAddress *dst_address,
1068 struct ip6_header *pkt6)
1072 len = payload_length;
1076 len += sizeof (struct udp_packet);
1079 /* tcp_header (with port/crc not set) must be part of payload! */
1080 if (len < sizeof (struct tcp_packet))
1090 if (len > UINT16_MAX)
1097 pkt6->next_header = protocol;
1098 pkt6->payload_length = htons ((uint16_t) (len + sizeof (struct ip6_header)));
1099 pkt6->hop_limit = 255;
1100 pkt6->destination_address = dst_address->address.ipv6;
1101 pkt6->source_address = src_address->address.ipv6;
1107 struct udp_packet *pkt6_udp = (struct udp_packet *) &pkt6[1];
1109 memcpy (&pkt6[1], payload, payload_length);
1111 pkt6_udp->spt = htons (src_address->port);
1112 pkt6_udp->dpt = htons (dst_address->port);
1113 pkt6_udp->len = htons ((uint16_t) payload_length);
1116 sum = GNUNET_CRYPTO_crc16_step (sum,
1117 &pkt6->source_address,
1118 sizeof (struct in6_addr) * 2);
1119 uint32_t tmp = htons (len);
1120 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1121 tmp = htonl (pkt6->next_header);
1122 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1123 sum = GNUNET_CRYPTO_crc16_step (sum, pkt6_udp, len);
1124 pkt6_udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1129 struct tcp_packet *pkt6_tcp = (struct tcp_packet *) pkt6;
1131 memcpy (pkt6_tcp, payload, payload_length);
1133 pkt6_tcp->spt = htons (src_address->port);
1134 pkt6_tcp->dpt = htons (dst_address->port);
1137 sum = GNUNET_CRYPTO_crc16_step (sum, &pkt6->source_address,
1138 sizeof (struct in6_addr) * 2);
1139 uint32_t tmp = htonl (len);
1140 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1141 tmp = htonl (pkt6->next_header);
1142 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1143 sum = GNUNET_CRYPTO_crc16_step (sum, pkt6_tcp, len);
1144 pkt6_tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1155 * Send a TCP packet via the TUN interface.
1157 * @param destination_address IP and port to use for the TCP packet's destination
1158 * @param source_address IP and port to use for the TCP packet's source
1159 * @param payload payload of the IP header (includes TCP header)
1160 * @param payload_length number of bytes in 'payload'
1163 send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
1164 const struct SocketAddress *source_address,
1165 const void *payload, size_t payload_length)
1169 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header);
1170 switch (source_address->af)
1173 len += sizeof (struct ip4_header);
1176 len += sizeof (struct ip6_header);
1182 len += payload_length;
1183 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1190 struct GNUNET_MessageHeader *hdr;
1191 struct tun_header *tun;
1193 hdr= (struct GNUNET_MessageHeader *) buf;
1194 hdr->type = htons (42);
1195 hdr->size = htons (len);
1196 tun = (struct tun_header*) &hdr[1];
1197 tun->flags = htons (0);
1198 switch (source_address->af)
1202 struct ip4_header * ipv4 = (struct ip4_header*) &tun[1];
1204 tun->proto = htons (ETH_P_IPV4);
1205 prepare_ipv4_packet (payload, payload_length, IPPROTO_TCP,
1207 destination_address,
1213 struct ip6_header * ipv6 = (struct ip6_header*) &tun[1];
1215 tun->proto = htons (ETH_P_IPV6);
1216 prepare_ipv6_packet (payload, payload_length, IPPROTO_TCP,
1218 destination_address,
1226 (void) GNUNET_HELPER_send (helper_handle,
1227 (const struct GNUNET_MessageHeader*) buf,
1235 * Process a request via mesh to send a request to a UDP service
1236 * offered by this system.
1238 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_packet
1239 * (FIXME: this is not great).
1241 * @param cls closure, NULL
1242 * @param tunnel connection to the other end
1243 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1244 * @param sender who sent the message
1245 * @param message the actual message
1246 * @param atsi performance data for the connection
1247 * @return GNUNET_OK to keep the connection open,
1248 * GNUNET_SYSERR to close it (signal serious error)
1251 receive_tcp_service (void *unused GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1252 void **tunnel_ctx GNUNET_UNUSED,
1253 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1254 const struct GNUNET_MessageHeader *message,
1255 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1257 struct TunnelState *state = *tunnel_ctx;
1258 // FIXME: write proper request struct (we don't need the descriptor EACH time here!)
1259 const GNUNET_HashCode *desc = (const GNUNET_HashCode *) &message[1];
1260 const struct tcp_packet *pkt = (const struct tcp_packet *) &desc[1];
1261 uint16_t pkt_len = ntohs (message->size);
1264 /* check that we got at least a valid header */
1265 if (pkt_len < sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + sizeof (struct tcp_packet))
1267 GNUNET_break_op (0);
1270 pkt_len -= (sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode));
1272 if (NULL == state->serv)
1274 /* setup fresh connection */
1275 GNUNET_assert (NULL == state->heap_node);
1276 if (NULL == (state->serv = find_service (tcp_services, desc, ntohs (pkt->dpt))))
1278 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1279 _("No service found for %s on port %d!\n"),
1282 GNUNET_MESH_tunnel_destroy (state->tunnel);
1285 state->ri.remote_address = state->serv->address;
1286 setup_state_record (state);
1288 send_tcp_packet_via_tun (&state->ri.remote_address,
1289 &state->ri.local_address,
1296 * Process a request to forward TCP data to the Internet via this peer.
1298 * @param cls closure, NULL
1299 * @param tunnel connection to the other end
1300 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1301 * @param sender who sent the message
1302 * @param message the actual message
1303 * @param atsi performance data for the connection
1304 * @return GNUNET_OK to keep the connection open,
1305 * GNUNET_SYSERR to close it (signal serious error)
1308 receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1309 void **tunnel_ctx GNUNET_UNUSED,
1310 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1311 const struct GNUNET_MessageHeader *message,
1312 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1314 struct TunnelState *state = *tunnel_ctx;
1315 // FIXME: write proper request struct (!)
1316 const GNUNET_HashCode *desc = (const GNUNET_HashCode *) &message[1];
1317 const struct tcp_packet *pkt = (const struct tcp_packet *) &desc[1];
1318 const struct SocketAddress *s = (const struct SocketAddress *) desc;
1319 uint16_t pkt_len = ntohs (message->size);
1321 if (pkt_len < sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + sizeof (struct tcp_packet))
1323 GNUNET_break_op (0);
1326 pkt_len -= (sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode));
1328 if (NULL == state->heap_node)
1330 /* first packet, setup record */
1331 state->ri.remote_address = *s;
1332 setup_state_record (state);
1335 send_tcp_packet_via_tun (&state->ri.remote_address,
1336 &state->ri.local_address,
1343 * Send a UDP packet via the TUN interface.
1345 * @param destination_address IP and port to use for the UDP packet's destination
1346 * @param source_address IP and port to use for the UDP packet's source
1347 * @param payload payload of the UDP packet (does NOT include UDP header)
1348 * @param payload_length number of bytes of data in payload
1351 send_udp_packet_via_tun (const struct SocketAddress *destination_address,
1352 const struct SocketAddress *source_address,
1353 const void *payload, size_t payload_length)
1357 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header);
1358 switch (source_address->af)
1361 len += sizeof (struct ip4_header);
1364 len += sizeof (struct ip6_header);
1370 len += sizeof (struct udp_packet);
1371 len += payload_length;
1372 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1379 struct GNUNET_MessageHeader *hdr;
1380 struct tun_header *tun;
1382 hdr= (struct GNUNET_MessageHeader *) buf;
1383 hdr->type = htons (42);
1384 hdr->size = htons (len);
1385 tun = (struct tun_header*) &hdr[1];
1386 tun->flags = htons (0);
1387 switch (source_address->af)
1391 struct ip4_header * ipv4 = (struct ip4_header*) &tun[1];
1393 tun->proto = htons (ETH_P_IPV4);
1394 prepare_ipv4_packet (payload, payload_length, IPPROTO_UDP,
1396 destination_address,
1402 struct ip6_header * ipv6 = (struct ip6_header*) &tun[1];
1404 tun->proto = htons (ETH_P_IPV6);
1405 prepare_ipv6_packet (payload, payload_length, IPPROTO_UDP,
1407 destination_address,
1415 (void) GNUNET_HELPER_send (helper_handle,
1416 (const struct GNUNET_MessageHeader*) buf,
1424 * Process a request to forward UDP data to the Internet via this peer.
1426 * @param cls closure, NULL
1427 * @param tunnel connection to the other end
1428 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1429 * @param sender who sent the message
1430 * @param message the actual message
1431 * @param atsi performance data for the connection
1432 * @return GNUNET_OK to keep the connection open,
1433 * GNUNET_SYSERR to close it (signal serious error)
1436 receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1437 void **tunnel_ctx GNUNET_UNUSED,
1438 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1439 const struct GNUNET_MessageHeader *message,
1440 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1442 struct TunnelState *state = *tunnel_ctx;
1443 // FIXME: write proper request struct (!)
1444 const GNUNET_HashCode *desc = (const GNUNET_HashCode *) &message[1];
1445 const struct udp_packet *pkt = (const struct udp_packet *) &desc[1];
1446 const struct SocketAddress *s = (const struct SocketAddress *) desc;
1447 uint16_t pkt_len = ntohs (message->size);
1449 if (pkt_len < sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + sizeof (struct udp_packet))
1451 GNUNET_break_op (0);
1454 pkt_len -= (sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode));
1456 if (NULL == state->heap_node)
1458 /* first packet, setup record */
1459 state->ri.remote_address = *s;
1460 setup_state_record (state);
1463 send_udp_packet_via_tun (&state->ri.remote_address,
1464 &state->ri.local_address,
1465 &pkt[1], pkt_len - sizeof (struct udp_packet));
1471 * Process a request via mesh to send a request to a UDP service
1472 * offered by this system.
1474 * @param cls closure, NULL
1475 * @param tunnel connection to the other end
1476 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1477 * @param sender who sent the message
1478 * @param message the actual message
1479 * @param atsi performance data for the connection
1480 * @return GNUNET_OK to keep the connection open,
1481 * GNUNET_SYSERR to close it (signal serious error)
1484 receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1486 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1487 const struct GNUNET_MessageHeader *message,
1488 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1490 struct TunnelState *state = *tunnel_ctx;
1491 // FIXME: write proper request struct (we don't need UDP except dpt either!)
1492 const GNUNET_HashCode *desc = (const GNUNET_HashCode *) &message[1];
1493 const struct udp_packet *pkt = (const struct udp_packet *) &desc[1];
1494 uint16_t pkt_len = ntohs (message->size);
1497 /* check that we got at least a valid header */
1498 if (pkt_len < sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + sizeof (struct udp_packet))
1500 GNUNET_break_op (0);
1503 pkt_len -= (sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode));
1505 GNUNET_assert (ntohs (pkt->len) ==
1506 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1507 sizeof (GNUNET_HashCode));
1509 if (NULL == state->serv)
1511 /* setup fresh connection */
1512 GNUNET_assert (NULL == state->heap_node);
1513 if (NULL == (state->serv = find_service (udp_services, desc, ntohs (pkt->dpt))))
1515 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1516 _("No service found for %s on port %d!\n"),
1519 GNUNET_MESH_tunnel_destroy (state->tunnel);
1522 state->ri.remote_address = state->serv->address;
1523 setup_state_record (state);
1525 send_udp_packet_via_tun (&state->ri.remote_address,
1526 &state->ri.local_address,
1527 &pkt[1], pkt_len - sizeof (struct udp_packet));
1533 * Callback from GNUNET_MESH for new tunnels.
1535 * @param cls closure
1536 * @param tunnel new handle to the tunnel
1537 * @param initiator peer that started the tunnel
1538 * @param atsi performance information for the tunnel
1539 * @return initial tunnel context for the tunnel
1542 new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1543 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
1544 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
1546 struct TunnelState *s = GNUNET_malloc (sizeof (struct TunnelState));
1554 * Function called by mesh whenever an inbound tunnel is destroyed.
1555 * Should clean up any associated state.
1557 * @param cls closure (set from GNUNET_MESH_connect)
1558 * @param tunnel connection to the other end (henceforth invalid)
1559 * @param tunnel_ctx place where local state associated
1560 * with the tunnel is stored
1563 clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
1566 struct TunnelState *s = tunnel_ctx;
1567 struct TunnelMessageQueue *tnq;
1569 while (NULL != (tnq = s->head))
1571 GNUNET_CONTAINER_DLL_remove (s->head,
1576 if (s->heap_node != NULL)
1578 GNUNET_assert (GNUNET_YES ==
1579 GNUNET_CONTAINER_multihashmap_remove (connections_map,
1582 GNUNET_CONTAINER_heap_remove_node (s->heap_node);
1583 s->heap_node = NULL;
1587 GNUNET_MESH_notify_transmit_ready_cancel (s->th);
1595 * Function that frees everything from a hashmap
1599 * @param value value to free
1602 free_iterate (void *cls GNUNET_UNUSED,
1603 const GNUNET_HashCode * hash GNUNET_UNUSED, void *value)
1605 GNUNET_free (value);
1611 * Function scheduled as very last function, cleans up after us
1614 cleanup (void *cls GNUNET_UNUSED,
1615 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
1619 if (helper_handle != NULL)
1621 GNUNET_HELPER_stop (helper_handle);
1622 helper_handle = NULL;
1624 if (mesh_handle != NULL)
1626 GNUNET_MESH_disconnect (mesh_handle);
1629 if (NULL != connections_map)
1631 GNUNET_CONTAINER_multihashmap_iterate (connections_map, &free_iterate, NULL);
1632 GNUNET_CONTAINER_multihashmap_destroy (connections_map);
1633 connections_map = NULL;
1635 if (NULL != connections_heap)
1637 GNUNET_CONTAINER_heap_destroy (connections_heap);
1638 connections_heap = NULL;
1640 if (NULL != tcp_services)
1642 GNUNET_CONTAINER_multihashmap_iterate (tcp_services, &free_service_record, NULL);
1643 GNUNET_CONTAINER_multihashmap_destroy (tcp_services);
1644 tcp_services = NULL;
1646 if (NULL != udp_services)
1648 GNUNET_CONTAINER_multihashmap_iterate (udp_services, &free_service_record, NULL);
1649 GNUNET_CONTAINER_multihashmap_destroy (udp_services);
1650 udp_services = NULL;
1653 GNUNET_free_non_null (exit_argv[i]);
1658 * Add services to the service map.
1660 * @param proto IPPROTO_TCP or IPPROTO_UDP
1661 * @param cpy copy of the service descriptor (can be mutilated)
1662 * @param name DNS name of the service
1665 add_services (int proto,
1672 struct LocalService *serv;
1674 for (redirect = strtok (cpy, " "); redirect != NULL;
1675 redirect = strtok (NULL, " "))
1677 if (NULL == (hostname = strstr (redirect, ":")))
1679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1680 "option `%s' for domain `%s' is not formatted correctly!\n",
1687 if (NULL == (hostport = strstr (hostname, ":")))
1689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1690 "option `%s' for domain `%s' is not formatted correctly!\n",
1698 int local_port = atoi (redirect);
1699 int remote_port = atoi (hostport);
1701 if (!((local_port > 0) && (local_port < 65536)))
1703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1704 "`%s' is not a valid port number (for domain `%s')!", redirect,
1708 if (!((remote_port > 0) && (remote_port < 65536)))
1710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1711 "`%s' is not a valid port number (for domain `%s')!", hostport,
1716 serv = GNUNET_malloc (sizeof (struct LocalService));
1717 serv->my_port = (uint16_t) local_port;
1718 serv->address.port = remote_port;
1719 if (0 == strcmp ("localhost4", hostname))
1721 const char *ip4addr = exit_argv[4];
1723 serv->address.af = AF_INET;
1724 GNUNET_assert (1 != inet_pton (AF_INET, ip4addr, &serv->address.address.ipv4));
1726 else if (0 == strcmp ("localhost6", hostname))
1728 const char *ip6addr = exit_argv[2];
1730 serv->address.af = AF_INET6;
1731 GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, &serv->address.address.ipv6));
1735 struct addrinfo *res;
1738 ret = getaddrinfo (hostname, NULL, NULL, &res);
1739 if ( (ret != 0) || (res == NULL) )
1741 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1742 _("No addresses found for hostname `%s' of service `%s'!\n"),
1749 serv->address.af = res->ai_family;
1750 switch (res->ai_family)
1753 serv->address.address.ipv4 = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
1756 serv->address.address.ipv6 = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1760 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1761 _("No IP addresses found for hostname `%s' of service `%s'!\n"),
1769 store_service ((IPPROTO_UDP == proto) ? udp_services : tcp_services,
1778 * Reads the configuration servicecfg and populates udp_services
1781 * @param section name of section in config, equal to hostname
1784 read_service_conf (void *cls GNUNET_UNUSED, const char *section)
1788 if ((strlen (section) < 8) ||
1789 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
1792 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
1795 add_services (IPPROTO_UDP, cpy, section);
1799 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1802 add_services (IPPROTO_TCP, cpy, section);
1809 * @brief Main function that will be run by the scheduler.
1811 * @param cls closure
1812 * @param args remaining command-line arguments
1813 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1814 * @param cfg_ configuration
1817 run (void *cls, char *const *args GNUNET_UNUSED,
1818 const char *cfgfile GNUNET_UNUSED,
1819 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1821 static struct GNUNET_MESH_MessageHandler handlers[] = {
1822 {&receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
1823 {&receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP, 0},
1829 static GNUNET_MESH_ApplicationType apptypes[] = {
1830 GNUNET_APPLICATION_TYPE_END,
1831 GNUNET_APPLICATION_TYPE_END,
1832 GNUNET_APPLICATION_TYPE_END
1834 unsigned int handler_idx;
1835 unsigned int app_idx;
1847 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1849 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_CONNECTIONS",
1851 max_connections = 1024;
1852 exit_argv[0] = GNUNET_strdup ("exit-gnunet");
1853 if (GNUNET_SYSERR ==
1854 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname))
1856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1857 "No entry 'IFNAME' in configuration!\n");
1858 GNUNET_SCHEDULER_shutdown ();
1861 exit_argv[1] = ifname;
1862 if ( (GNUNET_SYSERR ==
1863 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
1865 (1 != inet_pton (AF_INET6, ipv6addr, &v6))) )
1867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1868 "No valid entry 'IPV6ADDR' in configuration!\n");
1869 GNUNET_SCHEDULER_shutdown ();
1872 exit_argv[2] = ipv6addr;
1873 if (GNUNET_SYSERR ==
1874 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
1877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1878 "No entry 'IPV6PREFIX' in configuration!\n");
1879 GNUNET_SCHEDULER_shutdown ();
1882 exit_argv[3] = ipv6prefix_s;
1884 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
1887 (ipv6prefix >= 127) )
1889 GNUNET_SCHEDULER_shutdown ();
1893 if ( (GNUNET_SYSERR ==
1894 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
1896 (1 != inet_pton (AF_INET, ipv4addr, &v4))) )
1898 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1899 "No valid entry for 'IPV4ADDR' in configuration!\n");
1900 GNUNET_SCHEDULER_shutdown ();
1903 exit_argv[4] = ipv4addr;
1904 if ( (GNUNET_SYSERR ==
1905 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
1907 (1 != inet_pton (AF_INET, ipv4mask, &v4))) )
1909 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1910 "No valid entry 'IPV4MASK' in configuration!\n");
1911 GNUNET_SCHEDULER_shutdown ();
1914 exit_argv[5] = ipv4mask;
1915 exit_argv[6] = NULL;
1919 udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP");
1920 tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP");
1921 if (GNUNET_YES == udp)
1923 handlers[handler_idx].callback = &receive_udp_remote;
1924 handlers[handler_idx].expected_size = 0;
1925 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP;
1926 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1931 if (GNUNET_YES == tcp)
1933 handlers[handler_idx].callback = &receive_tcp_remote;
1934 handlers[handler_idx].expected_size = 0;
1935 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP;
1936 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1940 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1941 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1942 GNUNET_CONFIGURATION_iterate_sections (cfg, &read_service_conf, NULL);
1944 connections_map = GNUNET_CONTAINER_multihashmap_create (65536);
1945 connections_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1947 = GNUNET_MESH_connect (cfg, 42 /* queue size */, NULL,
1949 &clean_tunnel, handlers,
1951 if (NULL == mesh_handle)
1953 GNUNET_SCHEDULER_shutdown ();
1956 helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn",
1958 &message_token, NULL);
1965 * @param argc number of arguments from the command line
1966 * @param argv command line arguments
1967 * @return 0 ok, 1 on error
1970 main (int argc, char *const *argv)
1972 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1973 GNUNET_GETOPT_OPTION_END
1976 return (GNUNET_OK ==
1977 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
1979 ("Daemon to run to provide an IP exit node for the VPN"),
1980 options, &run, NULL)) ? 0 : 1;
1984 /* end of gnunet-daemon-exit.c */