2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief Library handling UPnP and NAT-PMP port forwarding and
24 * external IP address retrieval
25 * @author Milan Bouchet-Valat
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_nat_lib.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
37 * How often do we scan for changes in our IP address from our local
40 #define IFC_SCAN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
43 * How often do we scan for changes in how our hostname resolves?
45 #define HOSTNAME_DNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 20)
49 * How often do we scan for changes in how our external (dyndns) hostname resolves?
51 #define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 7)
54 * How long until we give up trying to resolve our own hostname?
56 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
60 * How often do we check a STUN server ?
62 #define STUN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
66 * Where did the given local address originate from?
67 * To be used for debugging as well as in the future
68 * to remove all addresses from a certain source when
69 * we reevaluate the source.
71 enum LocalAddressSource
74 * Address was obtained by DNS resolution of the external hostname
75 * given in the configuration (i.e. hole-punched DynDNS setup).
80 * Address was obtained by an external STUN server
85 * Address was obtained by DNS resolution of the external hostname
86 * given in the configuration (i.e. hole-punched DynDNS setup)
87 * during the previous iteration (see #3213).
92 * Address was obtained by looking up our own hostname in DNS.
97 * Address was obtained by scanning our hosts's network interfaces
98 * and taking their address (no DNS involved).
100 LAL_INTERFACE_ADDRESS,
103 * Addresses we were explicitly bound to.
108 * Addresses from UPnP or PMP
120 * List of local addresses that we currently deem valid. Actual
121 * struct is followed by the 'struct sockaddr'. Note that the code
122 * intentionally makes no attempt to ensure that a particular address
123 * is only listed once (especially since it may come from different
124 * sources, and the source is an "internal" construct).
126 struct LocalAddressList
129 * This is a linked list.
131 struct LocalAddressList *next;
136 struct LocalAddressList *prev;
139 * Number of bytes of address that follow.
144 * Origin of the local address.
146 enum LocalAddressSource source;
151 * Handle for miniupnp-based NAT traversal actions.
157 * Doubly-linked list.
159 struct MiniList *next;
162 * Doubly-linked list.
164 struct MiniList *prev;
167 * Handle to mini-action.
169 struct GNUNET_NAT_MiniHandle *mini;
172 * Local port number that was mapped.
180 * List of STUN servers
182 struct StunServerList
186 * Doubly-linked list.
188 struct StunServerList *next;
191 * Doubly-linked list.
193 struct StunServerList *prev;
209 * Handle for active NAT registrations.
211 struct GNUNET_NAT_Handle
215 * Configuration to use.
217 const struct GNUNET_CONFIGURATION_Handle *cfg;
220 * Function to call when we learn about a new address.
222 GNUNET_NAT_AddressCallback address_callback;
225 * Function to call when we notice another peer asking for
226 * connection reversal.
228 GNUNET_NAT_ReversalCallback reversal_callback;
231 * Closure for callbacks (@e address_callback and @e reversal_callback)
236 * Handle for (DYN)DNS lookup of our external IP.
238 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
241 * Handle for request of hostname resolution, non-NULL if pending.
243 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
246 * stdout pipe handle for the gnunet-helper-nat-server process
248 struct GNUNET_DISK_PipeHandle *server_stdout;
251 * stdout file handle (for reading) for the gnunet-helper-nat-server process
253 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
256 * Linked list of currently valid addresses (head).
258 struct LocalAddressList *lal_head;
261 * Linked list of currently valid addresses (tail).
263 struct LocalAddressList *lal_tail;
266 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
268 struct GNUNET_TIME_Relative server_retry_delay;
271 * ID of select gnunet-helper-nat-server stdout read task
273 struct GNUNET_SCHEDULER_Task * server_read_task;
276 * ID of interface IP-scan task
278 struct GNUNET_SCHEDULER_Task * ifc_task;
281 * ID of hostname DNS lookup task
283 struct GNUNET_SCHEDULER_Task * hostname_task;
286 * ID of DynDNS lookup task
288 struct GNUNET_SCHEDULER_Task *dns_task;
291 * How often do we scan for changes in our IP address from our local
294 struct GNUNET_TIME_Relative ifc_scan_frequency;
297 * How often do we scan for changes in how our hostname resolves?
299 struct GNUNET_TIME_Relative hostname_dns_frequency;
302 * How often do we scan for changes in how our external (dyndns) hostname resolves?
304 struct GNUNET_TIME_Relative dyndns_frequency;
307 * The process id of the server process (if behind NAT)
309 struct GNUNET_OS_Process *server_proc;
312 * LAN address as passed by the caller (array).
314 struct sockaddr **local_addrs;
317 * Length of the @e local_addrs.
319 socklen_t *local_addrlens;
322 * List of handles for UPnP-traversal, one per local port (if
325 struct MiniList *mini_head;
328 * List of handles for UPnP-traversal, one per local port (if
331 struct MiniList *mini_tail;
334 * Number of entries in 'local_addrs' array.
336 unsigned int num_local_addrs;
339 * Our external address (according to config, UPnP may disagree...),
340 * in dotted decimal notation, IPv4-only. Or NULL if not known.
342 char *external_address;
345 * Presumably our internal address (according to config)
347 char *internal_address;
350 * Is this transport configured to be behind a NAT?
355 * Has the NAT been punched? (according to config)
360 * Is this transport configured to allow connections to NAT'd peers?
362 int enable_nat_client;
365 * Should we run the gnunet-helper-nat-server?
367 int enable_nat_server;
370 * Are we allowed to try UPnP/PMP for NAT traversal?
375 * Should we use local addresses (loopback)? (according to config)
377 int use_localaddresses;
380 * Should we return local addresses to clients
382 int return_localaddress;
385 * Should we do a DNS lookup of our hostname to find out our own IP?
390 * Is using IPv6 disabled?
395 * Is this TCP or UDP?
400 * Port we advertise to the outside.
405 * Should we use STUN ?
410 * How often should we check STUN ?
412 struct GNUNET_TIME_Relative stun_frequency;
417 struct GNUNET_NETWORK_Handle* socket;
420 * Am I waiting for a STUN response ?
427 struct GNUNET_SCHEDULER_Task * stun_task;
430 * Head of List of STUN servers
432 struct StunServerList *stun_servers_head;
435 * Tail of List of STUN servers
437 struct StunServerList *stun_servers_tail;
442 struct StunServerList *actual_stun_server;
448 * Try to start the gnunet-helper-nat-server (if it is not
451 * @param h handle to NAT
454 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
460 * Call task to process STUN
462 * @param cls handle to NAT
463 * @param tc TaskContext
467 process_stun (void *cls,
468 const struct GNUNET_SCHEDULER_TaskContext *tc);
472 * Remove all addresses from the list of 'local' addresses
473 * that originated from the given source.
475 * @param h handle to NAT
476 * @param src source that identifies addresses to remove
479 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
480 enum LocalAddressSource src)
482 struct LocalAddressList *pos;
483 struct LocalAddressList *next;
486 while (NULL != (pos = next))
489 if (pos->source != src)
491 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
492 if (NULL != h->address_callback)
493 h->address_callback (h->callback_cls, GNUNET_NO,
494 (const struct sockaddr *) &pos[1], pos->addrlen);
501 * Add the given address to the list of 'local' addresses, thereby
502 * making it a 'legal' address for this peer to have.
504 * @param h handle to NAT
505 * @param src where did the local address originate from?
506 * @param arg the address, some `struct sockaddr`
507 * @param arg_size number of bytes in @a arg
510 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
511 enum LocalAddressSource src,
512 const struct sockaddr *arg, socklen_t arg_size)
514 struct LocalAddressList *lal;
516 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
517 memcpy (&lal[1], arg, arg_size);
518 lal->addrlen = arg_size;
520 GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal);
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Adding address `%s' from source %d\n",
523 GNUNET_a2s (arg, arg_size),
525 if (NULL != h->address_callback)
526 h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size);
531 * Add the given address to the list of 'local' addresses, thereby
532 * making it a 'legal' address for this peer to have. Set the
533 * port number in the process to the advertised port and possibly
534 * also to zero (if we have the gnunet-helper-nat-server).
536 * @param h handle to NAT
537 * @param src where did the local address originate from?
538 * @param arg the address, some `struct sockaddr`
539 * @param arg_size number of bytes in @a arg
542 add_to_address_list (struct GNUNET_NAT_Handle *h,
543 enum LocalAddressSource src,
544 const struct sockaddr *arg,
547 struct sockaddr_in s4;
548 const struct sockaddr_in *in4;
549 struct sockaddr_in6 s6;
550 const struct sockaddr_in6 *in6;
552 if (arg_size == sizeof (struct sockaddr_in))
554 in4 = (const struct sockaddr_in *) arg;
556 s4.sin_port = htons (h->adv_port);
557 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
558 sizeof (struct sockaddr_in));
559 if (GNUNET_YES == h->enable_nat_server)
561 /* also add with PORT = 0 to indicate NAT server is enabled */
562 s4.sin_port = htons (0);
563 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
564 sizeof (struct sockaddr_in));
567 else if (arg_size == sizeof (struct sockaddr_in6))
569 if (GNUNET_YES != h->disable_ipv6)
571 in6 = (const struct sockaddr_in6 *) arg;
573 s6.sin6_port = htons (h->adv_port);
574 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s6,
575 sizeof (struct sockaddr_in6));
586 * Add the given IP address to the list of 'local' addresses, thereby
587 * making it a 'legal' address for this peer to have.
589 * @param h handle to NAT
590 * @param src where did the local address originate from?
591 * @param addr the address, some `struct in_addr` or `struct in6_addr`
592 * @param addrlen number of bytes in addr
595 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
596 enum LocalAddressSource src, const void *addr,
599 struct sockaddr_in s4;
600 const struct in_addr *in4;
601 struct sockaddr_in6 s6;
602 const struct in6_addr *in6;
604 if (addrlen == sizeof (struct in_addr))
606 in4 = (const struct in_addr *) addr;
607 memset (&s4, 0, sizeof (s4));
608 s4.sin_family = AF_INET;
610 #if HAVE_SOCKADDR_IN_SIN_LEN
611 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
614 add_to_address_list (h, src, (const struct sockaddr *) &s4,
615 sizeof (struct sockaddr_in));
616 if (GNUNET_YES == h->enable_nat_server)
618 /* also add with PORT = 0 to indicate NAT server is enabled */
619 s4.sin_port = htons (0);
620 add_to_address_list (h, src, (const struct sockaddr *) &s4,
621 sizeof (struct sockaddr_in));
625 else if (addrlen == sizeof (struct in6_addr))
627 if (GNUNET_YES != h->disable_ipv6)
629 in6 = (const struct in6_addr *) addr;
630 memset (&s6, 0, sizeof (s6));
631 s6.sin6_family = AF_INET6;
632 s6.sin6_port = htons (h->adv_port);
633 #if HAVE_SOCKADDR_IN_SIN_LEN
634 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
637 add_to_address_list (h, src, (const struct sockaddr *) &s6,
638 sizeof (struct sockaddr_in6));
649 * Task to do DNS lookup on our external hostname to
650 * get DynDNS-IP addresses.
652 * @param cls the NAT handle
653 * @param tc scheduler context
656 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
660 * Our (external) hostname was resolved and the configuration says that
661 * the NAT was hole-punched.
663 * @param cls the `struct GNUNET_NAT_Handle`
664 * @param addr NULL on error, otherwise result of DNS lookup
665 * @param addrlen number of bytes in @a addr
668 process_external_ip (void *cls,
669 const struct sockaddr *addr,
672 struct GNUNET_NAT_Handle *h = cls;
673 struct in_addr dummy;
678 /* Current iteration is over, remove 'old' IPs now */
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "Purging old IPs for external address\n");
681 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP_OLD);
682 if (1 == inet_pton (AF_INET,
686 LOG (GNUNET_ERROR_TYPE_DEBUG,
687 "Got numeric IP for external address, not repeating lookup\n");
688 return; /* repated lookup pointless: was numeric! */
691 GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "Got IP `%s' for external address `%s'\n",
697 GNUNET_a2s (addr, addrlen),
698 h->external_address);
699 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
704 * Task to do a lookup on our hostname for IP addresses.
706 * @param cls the NAT handle
707 * @param tc scheduler context
710 resolve_hostname (void *cls,
711 const struct GNUNET_SCHEDULER_TaskContext *tc);
715 * Function called by the resolver for each address obtained from DNS
716 * for our own hostname. Add the addresses to the list of our IP
720 * @param addr one of the addresses of the host, NULL for the last address
721 * @param addrlen length of the @a addr
724 process_hostname_ip (void *cls,
725 const struct sockaddr *addr,
728 struct GNUNET_NAT_Handle *h = cls;
732 h->hostname_dns = NULL;
734 GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
735 &resolve_hostname, h);
738 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
743 * Length of the interface names returned from os_network.c.
744 * (in that file, hardcoded at 11).
746 #define IF_NAME_LEN 11
750 * Add the IP of our network interface to the list of
753 * @param cls the `struct GNUNET_NAT_Handle`
754 * @param name name of the interface
755 * @param isDefault do we think this may be our default interface
756 * @param addr address of the interface
757 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
758 * @param netmask the network mask (can be NULL for unknown or unassigned))
759 * @param addrlen number of bytes in @a addr and @a broadcast_addr
760 * @return #GNUNET_OK to continue iterating
763 process_interfaces (void *cls,
766 const struct sockaddr *addr,
767 const struct sockaddr *broadcast_addr,
768 const struct sockaddr *netmask,
771 const static struct in6_addr any6 = IN6ADDR_ANY_INIT;
772 struct GNUNET_NAT_Handle *h = cls;
773 const struct sockaddr_in *s4;
774 const struct sockaddr_in6 *s6;
776 char buf[INET6_ADDRSTRLEN];
781 /* skip virtual interfaces created by GNUnet-vpn */
783 GNUNET_CONFIGURATION_get_value_string (h->cfg,
788 if (0 == strncasecmp (name,
792 GNUNET_free (tun_if);
796 /* skip virtual interfaces created by GNUnet-dns */
798 GNUNET_CONFIGURATION_get_value_string (h->cfg,
803 if (0 == strncasecmp (name,
807 GNUNET_free (tun_if);
811 /* skip virtual interfaces created by GNUnet-exit */
813 GNUNET_CONFIGURATION_get_value_string (h->cfg,
818 if (0 == strncasecmp (name,
822 GNUNET_free (tun_if);
828 switch (addr->sa_family)
831 /* check if we're bound to the "ANY" IP address */
832 have_any = GNUNET_NO;
833 for (i=0;i<h->num_local_addrs;i++)
835 if (h->local_addrs[i]->sa_family != AF_INET)
840 if (INADDR_ANY == ((struct sockaddr_in*) h->local_addrs[i])->sin_addr.s_addr)
842 have_any = GNUNET_YES;
846 if (GNUNET_NO == have_any)
847 return GNUNET_OK; /* not bound to IP 0.0.0.0 but to specific IP addresses,
848 do not use those from interfaces */
849 s4 = (struct sockaddr_in *) addr;
852 /* Check if address is in 127.0.0.0/8 */
853 uint32_t address = ntohl ((uint32_t) (s4->sin_addr.s_addr));
854 uint32_t value = (address & 0xFF000000) ^ 0x7F000000;
856 if ((h->return_localaddress == GNUNET_NO) && (value == 0))
860 if ((GNUNET_YES == h->use_localaddresses) || (value != 0))
862 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s4->sin_addr,
863 sizeof (struct in_addr));
867 /* check if we're bound to the "ANY" IP address */
868 have_any = GNUNET_NO;
869 for (i=0;i<h->num_local_addrs;i++)
871 if (h->local_addrs[i]->sa_family != AF_INET6)
873 if (0 == memcmp (&any6,
874 &((struct sockaddr_in6*) h->local_addrs[i])->sin6_addr,
875 sizeof (struct in6_addr)))
877 have_any = GNUNET_YES;
881 if (GNUNET_NO == have_any)
882 return GNUNET_OK; /* not bound to "ANY" IP (::0) but to specific IP addresses,
883 do not use those from interfaces */
885 s6 = (struct sockaddr_in6 *) addr;
886 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
888 /* skip link local addresses */
891 if ((h->return_localaddress == GNUNET_NO) &&
892 (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr)))
897 if (GNUNET_YES == h->use_localaddresses)
899 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s6->sin6_addr,
900 sizeof (struct in6_addr));
907 if ((h->internal_address == NULL) && (h->server_proc == NULL) &&
908 (h->server_read_task == NULL) &&
909 (GNUNET_YES == isDefault) && ((addr->sa_family == AF_INET) ||
910 (addr->sa_family == AF_INET6)))
912 /* no internal address configured, but we found a "default"
913 * interface, try using that as our 'internal' address */
914 h->internal_address =
915 GNUNET_strdup (inet_ntop (addr->sa_family, ip, buf, sizeof (buf)));
916 start_gnunet_nat_server (h);
923 * Task that restarts the gnunet-helper-nat-server process after a crash
924 * after a certain delay.
926 * @param cls the `struct GNUNET_NAT_Handle`
927 * @param tc scheduler context
930 restart_nat_server (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
932 struct GNUNET_NAT_Handle *h = cls;
934 h->server_read_task = NULL;
935 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
937 start_gnunet_nat_server (h);
942 * We have been notified that gnunet-helper-nat-server has written
943 * something to stdout. Handle the output, then reschedule this
944 * function to be called again once more is available.
946 * @param cls the NAT handle
947 * @param tc the scheduling context
950 nat_server_read (void *cls,
951 const struct GNUNET_SCHEDULER_TaskContext *tc)
953 struct GNUNET_NAT_Handle *h = cls;
958 const char *port_start;
959 struct sockaddr_in sin_addr;
961 h->server_read_task = NULL;
962 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
964 memset (mybuf, 0, sizeof (mybuf));
966 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
969 LOG (GNUNET_ERROR_TYPE_DEBUG,
970 "Finished reading from server stdout with code: %d\n",
972 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
973 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
974 GNUNET_OS_process_wait (h->server_proc);
975 GNUNET_OS_process_destroy (h->server_proc);
976 h->server_proc = NULL;
977 GNUNET_DISK_pipe_close (h->server_stdout);
978 h->server_stdout = NULL;
979 h->server_stdout_handle = NULL;
980 /* now try to restart it */
981 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
982 h->server_read_task =
983 GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
984 &restart_nat_server, h);
989 for (i = 0; i < sizeof (mybuf); i++)
991 if (mybuf[i] == '\n')
996 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
999 port_start = &mybuf[i + 1];
1003 /* construct socket address of sender */
1004 memset (&sin_addr, 0, sizeof (sin_addr));
1005 sin_addr.sin_family = AF_INET;
1006 #if HAVE_SOCKADDR_IN_SIN_LEN
1007 sin_addr.sin_len = sizeof (sin_addr);
1009 if ((NULL == port_start) || (1 != SSCANF (port_start, "%d", &port)) ||
1010 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
1012 /* should we restart gnunet-helper-nat-server? */
1013 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
1014 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
1016 h->server_read_task =
1017 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1018 h->server_stdout_handle,
1019 &nat_server_read, h);
1022 sin_addr.sin_port = htons ((uint16_t) port);
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, "gnunet-helper-nat-server read: %s:%d\n", mybuf,
1025 h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr,
1027 h->server_read_task =
1028 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1029 h->server_stdout_handle, &nat_server_read,
1035 * Try to start the gnunet-helper-nat-server (if it is not
1038 * @param h handle to NAT
1041 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
1045 if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) &&
1046 (h->internal_address != NULL) &&
1049 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES))))
1051 LOG (GNUNET_ERROR_TYPE_DEBUG,
1052 "Starting `%s' at `%s'\n",
1053 "gnunet-helper-nat-server", h->internal_address);
1054 /* Start the server process */
1055 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
1057 GNUNET_OS_start_process (GNUNET_NO, 0, NULL, h->server_stdout, NULL,
1059 "gnunet-helper-nat-server",
1060 h->internal_address, NULL);
1061 GNUNET_free (binary);
1062 if (h->server_proc == NULL)
1064 LOG (GNUNET_ERROR_TYPE_WARNING, "nat", _("Failed to start %s\n"),
1065 "gnunet-helper-nat-server");
1066 GNUNET_DISK_pipe_close (h->server_stdout);
1067 h->server_stdout = NULL;
1071 /* Close the write end of the read pipe */
1072 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1073 h->server_stdout_handle =
1074 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
1075 h->server_read_task =
1076 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1077 h->server_stdout_handle,
1078 &nat_server_read, h);
1085 * Task to scan the local network interfaces for IP addresses.
1087 * @param cls the NAT handle
1088 * @param tc scheduler context
1091 list_interfaces (void *cls,
1092 const struct GNUNET_SCHEDULER_TaskContext *tc)
1094 struct GNUNET_NAT_Handle *h = cls;
1097 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
1098 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
1100 GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
1101 &list_interfaces, h);
1107 * Callback if the STun request have a error
1109 * @param cls the NAT handle
1110 * @param result , the status
1113 stun_request_callback(void *cls,
1114 enum GNUNET_NAT_StatusCode result)
1117 struct GNUNET_NAT_Handle *h = cls;
1122 h->waiting_stun = GNUNET_NO;
1124 if(result != GNUNET_OK)
1126 LOG (GNUNET_ERROR_TYPE_WARNING,
1127 "Error processing a STUN request");
1133 * CHECK if is a valid STUN packet sending to GNUNET_NAT_stun_handle_packet.
1134 * It also check if it can handle the packet based on the NAT handler.
1135 * You don't need to call anything else to check if the packet is valid,
1137 * @param cls the NAT handle
1138 * @param data, packet
1139 * @param len, packet length
1141 * @return #GNUNET_NO if it can't decode, #GNUNET_YES if is a packet
1144 GNUNET_NAT_is_valid_stun_packet(void *cls, const void *data, size_t len)
1146 struct GNUNET_NAT_Handle *h = cls;
1147 struct sockaddr_in answer;
1149 /* We are not expecting a STUN message*/
1150 if(!h->waiting_stun)
1153 /*We dont have STUN installed*/
1157 /* Empty the answer structure */
1158 memset(&answer, 0, sizeof(struct sockaddr_in));
1160 /*Lets handle the packet*/
1161 int valid = GNUNET_NAT_stun_handle_packet(data,len, &answer);
1164 LOG (GNUNET_ERROR_TYPE_INFO,
1165 "Stun server returned IP %s , with port %d \n", inet_ntoa(answer.sin_addr), ntohs(answer.sin_port));
1166 /* ADD IP AS VALID*/
1167 add_to_address_list (h, LAL_EXTERNAL_IP, (const struct sockaddr *) &answer,
1168 sizeof (struct sockaddr_in));
1169 h->waiting_stun = GNUNET_NO;
1182 * Task to do a STUN request
1184 * @param cls the NAT handle
1185 * @param tc scheduler context
1188 process_stun (void *cls,
1189 const struct GNUNET_SCHEDULER_TaskContext *tc)
1191 struct GNUNET_NAT_Handle *h = cls;
1193 h->stun_task = NULL;
1197 struct StunServerList* elem = h->actual_stun_server;
1199 /* Make the request */
1200 LOG (GNUNET_ERROR_TYPE_INFO,
1201 "I will request the stun server %s:%i !\n", elem->address, elem->port);
1203 if(GNUNET_OK == GNUNET_NAT_stun_make_request(elem->address, elem->port, h->socket, &stun_request_callback, NULL))
1205 h->waiting_stun = GNUNET_YES;
1209 LOG (GNUNET_ERROR_TYPE_ERROR,
1210 "STUN request failed %s:%i !\n", elem->address, elem->port);
1214 GNUNET_SCHEDULER_add_delayed (h->stun_frequency,
1217 /* Set actual Server*/
1220 h->actual_stun_server = elem->next;
1224 h->actual_stun_server = h->stun_servers_head;
1232 * Task to do a lookup on our hostname for IP addresses.
1234 * @param cls the NAT handle
1235 * @param tc scheduler context
1238 resolve_hostname (void *cls,
1239 const struct GNUNET_SCHEDULER_TaskContext *tc)
1241 struct GNUNET_NAT_Handle *h = cls;
1243 h->hostname_task = NULL;
1244 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
1246 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT,
1247 &process_hostname_ip, h);
1252 * Task to do DNS lookup on our external hostname to
1253 * get DynDNS-IP addresses.
1255 * @param cls the NAT handle
1256 * @param tc scheduler context
1259 resolve_dns (void *cls,
1260 const struct GNUNET_SCHEDULER_TaskContext *tc)
1262 struct GNUNET_NAT_Handle *h = cls;
1263 struct LocalAddressList *pos;
1266 for (pos = h->lal_head; NULL != pos; pos = pos->next)
1267 if (pos->source == LAL_EXTERNAL_IP)
1268 pos->source = LAL_EXTERNAL_IP_OLD;
1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1270 "Resolving external address `%s'\n",
1271 h->external_address);
1273 GNUNET_RESOLVER_ip_get (h->external_address, AF_INET,
1274 GNUNET_TIME_UNIT_MINUTES,
1275 &process_external_ip, h);
1280 * Add or remove UPnP-mapped addresses.
1282 * @param cls the `struct GNUNET_NAT_Handle`
1283 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
1284 * the previous (now invalid) one
1285 * @param addr either the previous or the new public IP address
1286 * @param addrlen actual lenght of @a addr
1287 * @param ret GNUNET_NAT_ERROR_SUCCESS on success, otherwise an error code
1290 upnp_add (void *cls,
1292 const struct sockaddr *addr,
1294 enum GNUNET_NAT_StatusCode ret)
1296 struct GNUNET_NAT_Handle *h = cls;
1297 struct LocalAddressList *pos;
1298 struct LocalAddressList *next;
1301 if (GNUNET_NAT_ERROR_SUCCESS != ret)
1303 /* Error while running upnp client */
1304 LOG (GNUNET_ERROR_TYPE_ERROR,
1305 _("Error while running upnp client:\n"));
1307 //FIXME: convert error code to string
1312 if (GNUNET_YES == add_remove)
1314 add_to_address_list (h, LAL_UPNP, addr, addrlen);
1317 else if (GNUNET_NO == add_remove)
1319 /* remove address */
1321 while (NULL != (pos = next))
1324 if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) ||
1325 (0 != memcmp (&pos[1], addr, addrlen)))
1327 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
1328 if (NULL != h->address_callback)
1329 h->address_callback (h->callback_cls, GNUNET_NO,
1330 (const struct sockaddr *) &pos[1], pos->addrlen);
1332 return; /* only remove once */
1334 /* asked to remove address that does not exist */
1335 LOG (GNUNET_ERROR_TYPE_ERROR,
1336 "Asked to remove unkown address `%s'\n",
1337 GNUNET_a2s(addr, addrlen));
1349 * Try to add a port mapping using UPnP.
1351 * @param h overall NAT handle
1352 * @param port port to map with UPnP
1355 add_minis (struct GNUNET_NAT_Handle *h,
1358 struct MiniList *ml;
1363 if (port == ml->port)
1364 return; /* already got this port */
1368 ml = GNUNET_new (struct MiniList);
1370 ml->mini = GNUNET_NAT_mini_map_start (port, h->is_tcp, &upnp_add, h);
1372 if (NULL == ml->mini)
1374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1375 _("Failed to run upnp client for port %u\n"), ml->port);
1380 GNUNET_CONTAINER_DLL_insert (h->mini_head, h->mini_tail, ml);
1385 * Task to add addresses from original bind to set of valid addrs.
1387 * @param h the NAT handle
1390 add_from_bind (struct GNUNET_NAT_Handle *h)
1392 static struct in6_addr any = IN6ADDR_ANY_INIT;
1395 struct sockaddr *sa;
1396 const struct sockaddr_in *v4;
1398 for (i = 0; i < h->num_local_addrs; i++)
1400 sa = h->local_addrs[i];
1401 switch (sa->sa_family)
1404 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1409 v4 = (const struct sockaddr_in *) sa;
1410 if (0 != v4->sin_addr.s_addr)
1411 add_to_address_list (h,
1412 LAL_BINDTO_ADDRESS, sa,
1413 sizeof (struct sockaddr_in));
1416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1417 "Running upnp client for address `%s'\n",
1418 GNUNET_a2s (sa,sizeof (struct sockaddr_in)));
1419 add_minis (h, ntohs (v4->sin_port));
1423 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1429 memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr,
1431 sizeof (struct in6_addr)))
1432 add_to_address_list (h,
1435 sizeof (struct sockaddr_in6));
1445 * Attempt to enable port redirection and detect public IP address contacting
1446 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1447 * of the local host's addresses should the external port be mapped. The port
1448 * is taken from the corresponding sockaddr_in[6] field.
1450 * @param cfg configuration to use
1451 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
1452 * @param adv_port advertised port (port we are either bound to or that our OS
1453 * locally performs redirection from to our bound port).
1454 * @param num_addrs number of addresses in @a addrs
1455 * @param addrs the local addresses packets should be redirected to
1456 * @param addrlens actual lengths of the addresses
1457 * @param address_callback function to call everytime the public IP address changes
1458 * @param reversal_callback function to call if someone wants connection reversal from us
1459 * @param callback_cls closure for callbacks
1460 * @param sock used socket
1461 * @return NULL on error, otherwise handle that can be used to unregister
1463 struct GNUNET_NAT_Handle *
1464 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1467 unsigned int num_addrs,
1468 const struct sockaddr **addrs,
1469 const socklen_t *addrlens,
1470 GNUNET_NAT_AddressCallback address_callback,
1471 GNUNET_NAT_ReversalCallback reversal_callback,
1473 struct GNUNET_NETWORK_Handle* sock )
1475 struct GNUNET_NAT_Handle *h;
1476 struct in_addr in_addr;
1480 LOG (GNUNET_ERROR_TYPE_DEBUG,
1481 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1482 (unsigned int) adv_port, num_addrs);
1483 h = GNUNET_new (struct GNUNET_NAT_Handle);
1484 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1487 h->address_callback = address_callback;
1488 h->reversal_callback = reversal_callback;
1489 h->callback_cls = callback_cls;
1490 h->num_local_addrs = num_addrs;
1491 h->adv_port = adv_port;
1494 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr *));
1495 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1496 for (i = 0; i < num_addrs; i++)
1498 GNUNET_assert (addrlens[i] > 0);
1499 GNUNET_assert (addrs[i] != NULL);
1500 h->local_addrlens[i] = addrlens[i];
1501 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1502 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1506 GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1508 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1510 &h->internal_address);
1512 if ((h->internal_address != NULL) &&
1513 (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1515 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1516 "nat", "INTERNAL_ADDRESS",
1518 GNUNET_free (h->internal_address);
1519 h->internal_address = NULL;
1523 GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1525 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1527 &h->external_address);
1530 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT");
1532 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT");
1533 h->enable_nat_client =
1534 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_CLIENT");
1535 h->enable_nat_server =
1536 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_SERVER");
1538 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP");
1539 h->use_localaddresses =
1540 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR");
1541 h->return_localaddress =
1542 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat",
1543 "RETURN_LOCAL_ADDRESSES");
1546 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME");
1548 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6");
1550 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY",
1551 &h->dyndns_frequency))
1552 h->dyndns_frequency = DYNDNS_FREQUENCY;
1554 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY",
1555 &h->ifc_scan_frequency))
1556 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1558 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY",
1559 &h->hostname_dns_frequency))
1560 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1562 if (NULL == reversal_callback)
1563 h->enable_nat_server = GNUNET_NO;
1565 /* Check for UPnP client, disable immediately if not available */
1566 if ( (GNUNET_YES == h->enable_upnp) &&
1568 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)) )
1570 LOG (GNUNET_ERROR_TYPE_ERROR,
1571 _("UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP \n"));
1572 h->enable_upnp = GNUNET_NO;
1577 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat",
1581 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "STUN_FREQUENCY",
1582 &h->stun_frequency))
1583 h->stun_frequency = STUN_FREQUENCY;
1586 /* Check if NAT was hole-punched */
1587 if ((NULL != h->address_callback) &&
1588 (NULL != h->external_address) &&
1589 (GNUNET_YES == h->nat_punched))
1591 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1592 h->enable_nat_server = GNUNET_NO;
1593 h->enable_upnp = GNUNET_NO;
1594 h->use_stun = GNUNET_NO;
1598 LOG (GNUNET_ERROR_TYPE_DEBUG,
1599 "No external IP address given to add to our list of addresses\n");
1602 /* ENABLE STUN ONLY ON UDP*/
1603 if(!is_tcp && (NULL != sock) && h->use_stun )
1606 h->actual_stun_server = NULL;
1608 /* Lets process the servers*/
1616 GNUNET_CONFIGURATION_get_value_string (cfg, "nat", "STUN_SERVERS",
1619 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
1620 "nat", "STUN_SERVERS");
1624 h->stun_servers_head = NULL;
1625 h->stun_servers_tail = NULL;
1626 h->actual_stun_server = NULL;
1627 if (strlen (stun_servers) > 0)
1629 pos = strlen (stun_servers) - 1;
1633 if (stun_servers[pos] == ':')
1637 if ((stun_servers[pos] == ' ') || (0 == pos))
1640 /*Check if we do have a port*/
1641 if((0 == pos_port) || (pos_port <= pos))
1643 LOG (GNUNET_ERROR_TYPE_WARNING,
1644 "STUN server format mistake\n");
1650 struct StunServerList* ml = GNUNET_new (struct StunServerList);
1655 ml->port = atoi(&stun_servers[pos_port]);
1656 stun_servers[pos_port-1] = '\0';
1658 /* Remove trailing space */
1659 if(stun_servers[pos] == ' ')
1660 ml->address = GNUNET_strdup (&stun_servers[pos + 1]);
1662 ml->address = GNUNET_strdup (&stun_servers[pos]);
1664 LOG (GNUNET_ERROR_TYPE_DEBUG,
1665 "Found STUN server %s port %i !!!\n", ml->address, ml->port);
1667 GNUNET_CONTAINER_DLL_insert (h->stun_servers_head, h->stun_servers_tail, ml);
1668 /* Make sure that we STOP if is the last one*/
1678 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
1679 "nat", "STUN_SERVERS");
1683 /* Set the actual STUN server*/
1684 h->actual_stun_server = h->stun_servers_head;
1687 h->stun_task = GNUNET_SCHEDULER_add_now(&process_stun,
1692 /* Test for SUID binaries */
1693 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
1694 if ((h->behind_nat == GNUNET_YES) && (GNUNET_YES == h->enable_nat_server) &&
1696 GNUNET_OS_check_helper_binary (binary, GNUNET_YES, "-d 127.0.0.1" ))) // use localhost as source for that one udp-port, ok for testing
1698 h->enable_nat_server = GNUNET_NO;
1699 LOG (GNUNET_ERROR_TYPE_WARNING,
1700 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1701 "gnunet-helper-nat-server");
1703 GNUNET_free (binary);
1704 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
1705 if ((GNUNET_YES == h->enable_nat_client) &&
1707 GNUNET_OS_check_helper_binary (binary, GNUNET_YES, "-d 127.0.0.1 127.0.0.2 42"))) // none of these parameters are actually used in privilege testing mode
1709 h->enable_nat_client = GNUNET_NO;
1710 LOG (GNUNET_ERROR_TYPE_WARNING,
1712 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1713 "gnunet-helper-nat-client");
1715 GNUNET_free (binary);
1716 start_gnunet_nat_server (h);
1718 /* FIXME: add support for UPnP, etc */
1720 if (NULL != h->address_callback)
1722 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces,
1724 if (GNUNET_YES == h->use_hostname)
1725 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname,
1735 * Stop port redirection and public IP address detection for the given handle.
1736 * This frees the handle, after having sent the needed commands to close open ports.
1738 * @param h the handle to stop
1741 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1744 struct LocalAddressList *lal;
1745 struct MiniList *ml;
1747 LOG (GNUNET_ERROR_TYPE_DEBUG,
1748 "NAT unregister called\n");
1749 while (NULL != (ml = h->mini_head))
1751 GNUNET_CONTAINER_DLL_remove (h->mini_head,
1754 if (NULL != ml->mini)
1755 GNUNET_NAT_mini_map_stop (ml->mini);
1758 if (NULL != h->ext_dns)
1760 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1763 if (NULL != h->hostname_dns)
1765 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1766 h->hostname_dns = NULL;
1768 if (NULL != h->server_read_task)
1770 GNUNET_SCHEDULER_cancel (h->server_read_task);
1771 h->server_read_task = NULL;
1773 if (NULL != h->ifc_task)
1775 GNUNET_SCHEDULER_cancel (h->ifc_task);
1778 if (NULL != h->hostname_task)
1780 GNUNET_SCHEDULER_cancel (h->hostname_task);
1781 h->hostname_task = NULL;
1783 if (NULL != h->dns_task)
1785 GNUNET_SCHEDULER_cancel (h->dns_task);
1788 if (NULL != h->stun_task)
1790 GNUNET_SCHEDULER_cancel (h->stun_task);
1791 h->stun_task = NULL;
1793 if (NULL != h->server_proc)
1795 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
1796 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
1797 GNUNET_OS_process_wait (h->server_proc);
1798 GNUNET_OS_process_destroy (h->server_proc);
1799 h->server_proc = NULL;
1800 GNUNET_DISK_pipe_close (h->server_stdout);
1801 h->server_stdout = NULL;
1802 h->server_stdout_handle = NULL;
1804 if (NULL != h->server_stdout)
1806 GNUNET_DISK_pipe_close (h->server_stdout);
1807 h->server_stdout = NULL;
1808 h->server_stdout_handle = NULL;
1810 while (NULL != (lal = h->lal_head))
1812 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal);
1813 if (NULL != h->address_callback)
1814 h->address_callback (h->callback_cls, GNUNET_NO,
1815 (const struct sockaddr *) &lal[1], lal->addrlen);
1818 for (i = 0; i < h->num_local_addrs; i++)
1819 GNUNET_free (h->local_addrs[i]);
1820 GNUNET_free_non_null (h->local_addrs);
1821 GNUNET_free_non_null (h->local_addrlens);
1822 GNUNET_free_non_null (h->external_address);
1823 GNUNET_free_non_null (h->internal_address);
1829 * We learned about a peer (possibly behind NAT) so run the
1830 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1831 * that peer to connect to us (connection reversal).
1833 * @param h handle (used for configuration)
1834 * @param sa the address of the peer (IPv4-only)
1835 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
1836 * #GNUNET_OK otherwise
1839 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1840 const struct sockaddr_in *sa)
1844 char inet4[INET_ADDRSTRLEN];
1845 char port_as_string[6];
1846 struct GNUNET_OS_Process *proc;
1849 if (GNUNET_YES != h->enable_nat_client)
1850 return GNUNET_NO; /* not permitted / possible */
1852 if (h->internal_address == NULL)
1854 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
1855 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1856 return GNUNET_SYSERR;
1858 GNUNET_assert (sa->sin_family == AF_INET);
1859 if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN))
1861 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
1864 return GNUNET_SYSERR;
1866 GNUNET_snprintf (port_as_string,
1867 sizeof (port_as_string),
1870 LOG (GNUNET_ERROR_TYPE_DEBUG,
1871 _("Running gnunet-helper-nat-client %s %s %u\n"),
1872 h->internal_address,
1874 (unsigned int) h->adv_port);
1875 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
1877 GNUNET_OS_start_process (GNUNET_NO, 0, NULL, NULL, NULL,
1879 "gnunet-helper-nat-client",
1880 h->internal_address,
1881 inet4, port_as_string, NULL);
1882 GNUNET_free (binary);
1884 return GNUNET_SYSERR;
1885 /* we know that the gnunet-helper-nat-client will terminate virtually
1887 GNUNET_OS_process_wait (proc);
1888 GNUNET_OS_process_destroy (proc);
1894 * Test if the given address is (currently) a plausible IP address for this peer.
1896 * @param h the handle returned by register
1897 * @param addr IP address to test (IPv4 or IPv6)
1898 * @param addrlen number of bytes in @a addr
1899 * @return #GNUNET_YES if the address is plausible,
1900 * #GNUNET_NO if the address is not plausible,
1901 * #GNUNET_SYSERR if the address is malformed
1904 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1908 struct LocalAddressList *pos;
1909 const struct sockaddr_in *in4;
1910 const struct sockaddr_in6 *in6;
1912 if ((addrlen != sizeof (struct in_addr)) &&
1913 (addrlen != sizeof (struct in6_addr)))
1916 return GNUNET_SYSERR;
1918 for (pos = h->lal_head; NULL != pos; pos = pos->next)
1920 if (pos->addrlen == sizeof (struct sockaddr_in))
1922 in4 = (struct sockaddr_in *) &pos[1];
1923 if ((addrlen == sizeof (struct in_addr)) &&
1924 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))))
1927 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1929 in6 = (struct sockaddr_in6 *) &pos[1];
1930 if ((addrlen == sizeof (struct in6_addr)) &&
1931 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))))
1939 LOG (GNUNET_ERROR_TYPE_WARNING,
1940 "Asked to validate one of my addresses (%s) and validation failed!\n",
1947 * Converts enum GNUNET_NAT_StatusCode to a string
1949 * @param err error code to resolve to a string
1950 * @return pointer to a static string containing the error code
1953 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err)
1957 case GNUNET_NAT_ERROR_SUCCESS:
1958 return _ ("Operation Successful");
1959 case GNUNET_NAT_ERROR_IPC_FAILURE:
1960 return _ ("Internal Failure (IPC, ...)");
1961 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR:
1962 return _ ("Failure in network subsystem, check permissions.");
1963 case GNUNET_NAT_ERROR_TIMEOUT:
1964 return _ ("Encountered timeout while performing operation");
1965 case GNUNET_NAT_ERROR_NOT_ONLINE:
1966 return _ ("detected that we are offline");
1967 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
1968 return _ ("`upnpc` command not found");
1969 case GNUNET_NAT_ERROR_UPNPC_FAILED:
1970 return _ ("Failed to run `upnpc` command");
1971 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
1972 return _ ("`upnpc' command took too long, process killed");
1973 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
1974 return _ ("`upnpc' command failed to establish port mapping");
1975 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
1976 return _ ("`external-ip' command not found");
1977 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
1978 return _ ("Failed to run `external-ip` command");
1979 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
1980 return _ ("`external-ip' command output invalid");
1981 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
1982 return _ ("no valid address was returned by `external-ip'");
1983 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO:
1984 return _ ("Could not determine interface with internal/local network address");
1985 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND:
1986 return _ ("No functioning gnunet-helper-nat-server installation found");
1987 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED:
1988 return _ ("NAT test could not be initialized");
1989 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT:
1990 return _ ("NAT test timeout reached");
1991 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED:
1992 return _ ("could not register NAT");
1993 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND:
1994 return _ ("No working gnunet-helper-nat-client installation found");
1998 return "unknown status code";