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 * Active STUN request, if any.
293 struct GNUNET_NAT_STUN_Handle *stun_request;
296 * How often do we scan for changes in our IP address from our local
299 struct GNUNET_TIME_Relative ifc_scan_frequency;
302 * How often do we scan for changes in how our hostname resolves?
304 struct GNUNET_TIME_Relative hostname_dns_frequency;
307 * How often do we scan for changes in how our external (dyndns) hostname resolves?
309 struct GNUNET_TIME_Relative dyndns_frequency;
312 * The process id of the server process (if behind NAT)
314 struct GNUNET_OS_Process *server_proc;
317 * LAN address as passed by the caller (array).
319 struct sockaddr **local_addrs;
322 * Length of the @e local_addrs.
324 socklen_t *local_addrlens;
327 * List of handles for UPnP-traversal, one per local port (if
330 struct MiniList *mini_head;
333 * List of handles for UPnP-traversal, one per local port (if
336 struct MiniList *mini_tail;
339 * Number of entries in 'local_addrs' array.
341 unsigned int num_local_addrs;
344 * Our external address (according to config, UPnP may disagree...),
345 * in dotted decimal notation, IPv4-only. Or NULL if not known.
347 char *external_address;
350 * Presumably our internal address (according to config)
352 char *internal_address;
355 * Is this transport configured to be behind a NAT?
360 * Has the NAT been punched? (according to config)
365 * Is this transport configured to allow connections to NAT'd peers?
367 int enable_nat_client;
370 * Should we run the gnunet-helper-nat-server?
372 int enable_nat_server;
375 * Are we allowed to try UPnP/PMP for NAT traversal?
380 * Should we use local addresses (loopback)? (according to config)
382 int use_localaddresses;
385 * Should we return local addresses to clients
387 int return_localaddress;
390 * Should we do a DNS lookup of our hostname to find out our own IP?
395 * Is using IPv6 disabled?
400 * Is this TCP or UDP?
405 * Port we advertise to the outside.
410 * Should we use STUN ?
415 * How often should we check STUN ?
417 struct GNUNET_TIME_Relative stun_frequency;
422 struct GNUNET_NETWORK_Handle* socket;
425 * Am I waiting for a STUN response ?
432 struct GNUNET_SCHEDULER_Task * stun_task;
435 * Head of List of STUN servers
437 struct StunServerList *stun_servers_head;
440 * Tail of List of STUN servers
442 struct StunServerList *stun_servers_tail;
447 struct StunServerList *actual_stun_server;
453 * Try to start the gnunet-helper-nat-server (if it is not
456 * @param h handle to NAT
459 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
463 * Call task to process STUN
465 * @param cls handle to NAT
466 * @param tc TaskContext
469 process_stun (void *cls,
470 const struct GNUNET_SCHEDULER_TaskContext *tc);
474 * Remove all addresses from the list of 'local' addresses
475 * that originated from the given source.
477 * @param h handle to NAT
478 * @param src source that identifies addresses to remove
481 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
482 enum LocalAddressSource src)
484 struct LocalAddressList *pos;
485 struct LocalAddressList *next;
488 while (NULL != (pos = next))
491 if (pos->source != src)
493 GNUNET_CONTAINER_DLL_remove (h->lal_head,
496 if (NULL != h->address_callback)
497 h->address_callback (h->callback_cls,
499 (const struct sockaddr *) &pos[1],
507 * Add the given address to the list of 'local' addresses, thereby
508 * making it a 'legal' address for this peer to have.
510 * @param h handle to NAT
511 * @param src where did the local address originate from?
512 * @param arg the address, some `struct sockaddr`
513 * @param arg_size number of bytes in @a arg
516 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
517 enum LocalAddressSource src,
518 const struct sockaddr *arg,
521 struct LocalAddressList *lal;
523 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
524 memcpy (&lal[1], arg, arg_size);
525 lal->addrlen = arg_size;
527 GNUNET_CONTAINER_DLL_insert (h->lal_head,
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Adding address `%s' from source %d\n",
532 GNUNET_a2s (arg, arg_size),
534 if (NULL != h->address_callback)
535 h->address_callback (h->callback_cls,
543 * Add the given address to the list of 'local' addresses, thereby
544 * making it a 'legal' address for this peer to have. Set the
545 * port number in the process to the advertised port and possibly
546 * also to zero (if we have the gnunet-helper-nat-server).
548 * @param h handle to NAT
549 * @param src where did the local address originate from?
550 * @param arg the address, some `struct sockaddr`
551 * @param arg_size number of bytes in @a arg
554 add_to_address_list (struct GNUNET_NAT_Handle *h,
555 enum LocalAddressSource src,
556 const struct sockaddr *arg,
559 struct sockaddr_in s4;
560 const struct sockaddr_in *in4;
561 struct sockaddr_in6 s6;
562 const struct sockaddr_in6 *in6;
564 if (arg_size == sizeof (struct sockaddr_in))
566 in4 = (const struct sockaddr_in *) arg;
568 s4.sin_port = htons (h->adv_port);
569 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
570 sizeof (struct sockaddr_in));
571 if (GNUNET_YES == h->enable_nat_server)
573 /* also add with PORT = 0 to indicate NAT server is enabled */
574 s4.sin_port = htons (0);
575 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
576 sizeof (struct sockaddr_in));
579 else if (arg_size == sizeof (struct sockaddr_in6))
581 if (GNUNET_YES != h->disable_ipv6)
583 in6 = (const struct sockaddr_in6 *) arg;
585 s6.sin6_port = htons (h->adv_port);
586 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s6,
587 sizeof (struct sockaddr_in6));
598 * Add the given IP address to the list of 'local' addresses, thereby
599 * making it a 'legal' address for this peer to have.
601 * @param h handle to NAT
602 * @param src where did the local address originate from?
603 * @param addr the address, some `struct in_addr` or `struct in6_addr`
604 * @param addrlen number of bytes in addr
607 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
608 enum LocalAddressSource src, const void *addr,
611 struct sockaddr_in s4;
612 const struct in_addr *in4;
613 struct sockaddr_in6 s6;
614 const struct in6_addr *in6;
616 if (addrlen == sizeof (struct in_addr))
618 in4 = (const struct in_addr *) addr;
619 memset (&s4, 0, sizeof (s4));
620 s4.sin_family = AF_INET;
622 #if HAVE_SOCKADDR_IN_SIN_LEN
623 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
626 add_to_address_list (h, src, (const struct sockaddr *) &s4,
627 sizeof (struct sockaddr_in));
628 if (GNUNET_YES == h->enable_nat_server)
630 /* also add with PORT = 0 to indicate NAT server is enabled */
631 s4.sin_port = htons (0);
632 add_to_address_list (h, src, (const struct sockaddr *) &s4,
633 sizeof (struct sockaddr_in));
637 else if (addrlen == sizeof (struct in6_addr))
639 if (GNUNET_YES != h->disable_ipv6)
641 in6 = (const struct in6_addr *) addr;
642 memset (&s6, 0, sizeof (s6));
643 s6.sin6_family = AF_INET6;
644 s6.sin6_port = htons (h->adv_port);
645 #if HAVE_SOCKADDR_IN_SIN_LEN
646 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
649 add_to_address_list (h, src, (const struct sockaddr *) &s6,
650 sizeof (struct sockaddr_in6));
661 * Task to do DNS lookup on our external hostname to
662 * get DynDNS-IP addresses.
664 * @param cls the NAT handle
665 * @param tc scheduler context
668 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
672 * Our (external) hostname was resolved and the configuration says that
673 * the NAT was hole-punched.
675 * @param cls the `struct GNUNET_NAT_Handle`
676 * @param addr NULL on error, otherwise result of DNS lookup
677 * @param addrlen number of bytes in @a addr
680 process_external_ip (void *cls,
681 const struct sockaddr *addr,
684 struct GNUNET_NAT_Handle *h = cls;
685 struct in_addr dummy;
690 /* Current iteration is over, remove 'old' IPs now */
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Purging old IPs for external address\n");
693 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP_OLD);
694 if (1 == inet_pton (AF_INET,
698 LOG (GNUNET_ERROR_TYPE_DEBUG,
699 "Got numeric IP for external address, not repeating lookup\n");
700 return; /* repated lookup pointless: was numeric! */
703 GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
707 LOG (GNUNET_ERROR_TYPE_DEBUG,
708 "Got IP `%s' for external address `%s'\n",
709 GNUNET_a2s (addr, addrlen),
710 h->external_address);
711 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
716 * Task to do a lookup on our hostname for IP addresses.
718 * @param cls the NAT handle
719 * @param tc scheduler context
722 resolve_hostname (void *cls,
723 const struct GNUNET_SCHEDULER_TaskContext *tc);
727 * Function called by the resolver for each address obtained from DNS
728 * for our own hostname. Add the addresses to the list of our IP
732 * @param addr one of the addresses of the host, NULL for the last address
733 * @param addrlen length of the @a addr
736 process_hostname_ip (void *cls,
737 const struct sockaddr *addr,
740 struct GNUNET_NAT_Handle *h = cls;
744 h->hostname_dns = NULL;
746 GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
747 &resolve_hostname, h);
750 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
755 * Length of the interface names returned from os_network.c.
756 * (in that file, hardcoded at 11).
758 #define IF_NAME_LEN 11
762 * Add the IP of our network interface to the list of
765 * @param cls the `struct GNUNET_NAT_Handle`
766 * @param name name of the interface
767 * @param isDefault do we think this may be our default interface
768 * @param addr address of the interface
769 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
770 * @param netmask the network mask (can be NULL for unknown or unassigned))
771 * @param addrlen number of bytes in @a addr and @a broadcast_addr
772 * @return #GNUNET_OK to continue iterating
775 process_interfaces (void *cls,
778 const struct sockaddr *addr,
779 const struct sockaddr *broadcast_addr,
780 const struct sockaddr *netmask,
783 const static struct in6_addr any6 = IN6ADDR_ANY_INIT;
784 struct GNUNET_NAT_Handle *h = cls;
785 const struct sockaddr_in *s4;
786 const struct sockaddr_in6 *s6;
788 char buf[INET6_ADDRSTRLEN];
793 /* skip virtual interfaces created by GNUnet-vpn */
795 GNUNET_CONFIGURATION_get_value_string (h->cfg,
800 if (0 == strncasecmp (name,
804 GNUNET_free (tun_if);
808 /* skip virtual interfaces created by GNUnet-dns */
810 GNUNET_CONFIGURATION_get_value_string (h->cfg,
815 if (0 == strncasecmp (name,
819 GNUNET_free (tun_if);
823 /* skip virtual interfaces created by GNUnet-exit */
825 GNUNET_CONFIGURATION_get_value_string (h->cfg,
830 if (0 == strncasecmp (name,
834 GNUNET_free (tun_if);
840 switch (addr->sa_family)
843 /* check if we're bound to the "ANY" IP address */
844 have_any = GNUNET_NO;
845 for (i=0;i<h->num_local_addrs;i++)
847 if (h->local_addrs[i]->sa_family != AF_INET)
852 if (INADDR_ANY == ((struct sockaddr_in*) h->local_addrs[i])->sin_addr.s_addr)
854 have_any = GNUNET_YES;
858 if (GNUNET_NO == have_any)
859 return GNUNET_OK; /* not bound to IP 0.0.0.0 but to specific IP addresses,
860 do not use those from interfaces */
861 s4 = (struct sockaddr_in *) addr;
864 /* Check if address is in 127.0.0.0/8 */
865 uint32_t address = ntohl ((uint32_t) (s4->sin_addr.s_addr));
866 uint32_t value = (address & 0xFF000000) ^ 0x7F000000;
868 if ((h->return_localaddress == GNUNET_NO) && (value == 0))
872 if ((GNUNET_YES == h->use_localaddresses) || (value != 0))
874 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s4->sin_addr,
875 sizeof (struct in_addr));
879 /* check if we're bound to the "ANY" IP address */
880 have_any = GNUNET_NO;
881 for (i=0;i<h->num_local_addrs;i++)
883 if (h->local_addrs[i]->sa_family != AF_INET6)
885 if (0 == memcmp (&any6,
886 &((struct sockaddr_in6*) h->local_addrs[i])->sin6_addr,
887 sizeof (struct in6_addr)))
889 have_any = GNUNET_YES;
893 if (GNUNET_NO == have_any)
894 return GNUNET_OK; /* not bound to "ANY" IP (::0) but to specific IP addresses,
895 do not use those from interfaces */
897 s6 = (struct sockaddr_in6 *) addr;
898 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
900 /* skip link local addresses */
903 if ((h->return_localaddress == GNUNET_NO) &&
904 (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr)))
909 if (GNUNET_YES == h->use_localaddresses)
911 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s6->sin6_addr,
912 sizeof (struct in6_addr));
919 if ((h->internal_address == NULL) && (h->server_proc == NULL) &&
920 (h->server_read_task == NULL) &&
921 (GNUNET_YES == isDefault) && ((addr->sa_family == AF_INET) ||
922 (addr->sa_family == AF_INET6)))
924 /* no internal address configured, but we found a "default"
925 * interface, try using that as our 'internal' address */
926 h->internal_address =
927 GNUNET_strdup (inet_ntop (addr->sa_family, ip, buf, sizeof (buf)));
928 start_gnunet_nat_server (h);
935 * Task that restarts the gnunet-helper-nat-server process after a crash
936 * after a certain delay.
938 * @param cls the `struct GNUNET_NAT_Handle`
939 * @param tc scheduler context
942 restart_nat_server (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
944 struct GNUNET_NAT_Handle *h = cls;
946 h->server_read_task = NULL;
947 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
949 start_gnunet_nat_server (h);
954 * We have been notified that gnunet-helper-nat-server has written
955 * something to stdout. Handle the output, then reschedule this
956 * function to be called again once more is available.
958 * @param cls the NAT handle
959 * @param tc the scheduling context
962 nat_server_read (void *cls,
963 const struct GNUNET_SCHEDULER_TaskContext *tc)
965 struct GNUNET_NAT_Handle *h = cls;
970 const char *port_start;
971 struct sockaddr_in sin_addr;
973 h->server_read_task = NULL;
974 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
976 memset (mybuf, 0, sizeof (mybuf));
978 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
981 LOG (GNUNET_ERROR_TYPE_DEBUG,
982 "Finished reading from server stdout with code: %d\n",
984 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
985 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
986 GNUNET_OS_process_wait (h->server_proc);
987 GNUNET_OS_process_destroy (h->server_proc);
988 h->server_proc = NULL;
989 GNUNET_DISK_pipe_close (h->server_stdout);
990 h->server_stdout = NULL;
991 h->server_stdout_handle = NULL;
992 /* now try to restart it */
993 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
994 h->server_read_task =
995 GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
996 &restart_nat_server, h);
1001 for (i = 0; i < sizeof (mybuf); i++)
1003 if (mybuf[i] == '\n')
1008 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
1011 port_start = &mybuf[i + 1];
1015 /* construct socket address of sender */
1016 memset (&sin_addr, 0, sizeof (sin_addr));
1017 sin_addr.sin_family = AF_INET;
1018 #if HAVE_SOCKADDR_IN_SIN_LEN
1019 sin_addr.sin_len = sizeof (sin_addr);
1021 if ((NULL == port_start) || (1 != SSCANF (port_start, "%d", &port)) ||
1022 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
1024 /* should we restart gnunet-helper-nat-server? */
1025 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
1026 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
1028 h->server_read_task =
1029 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1030 h->server_stdout_handle,
1031 &nat_server_read, h);
1034 sin_addr.sin_port = htons ((uint16_t) port);
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, "gnunet-helper-nat-server read: %s:%d\n", mybuf,
1037 h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr,
1039 h->server_read_task =
1040 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1041 h->server_stdout_handle, &nat_server_read,
1047 * Try to start the gnunet-helper-nat-server (if it is not
1050 * @param h handle to NAT
1053 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
1057 if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) &&
1058 (h->internal_address != NULL) &&
1061 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES))))
1063 LOG (GNUNET_ERROR_TYPE_DEBUG,
1064 "Starting `%s' at `%s'\n",
1065 "gnunet-helper-nat-server", h->internal_address);
1066 /* Start the server process */
1067 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
1069 GNUNET_OS_start_process (GNUNET_NO, 0, NULL, h->server_stdout, NULL,
1071 "gnunet-helper-nat-server",
1072 h->internal_address, NULL);
1073 GNUNET_free (binary);
1074 if (h->server_proc == NULL)
1076 LOG (GNUNET_ERROR_TYPE_WARNING, "nat", _("Failed to start %s\n"),
1077 "gnunet-helper-nat-server");
1078 GNUNET_DISK_pipe_close (h->server_stdout);
1079 h->server_stdout = NULL;
1083 /* Close the write end of the read pipe */
1084 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1085 h->server_stdout_handle =
1086 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
1087 h->server_read_task =
1088 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1089 h->server_stdout_handle,
1090 &nat_server_read, h);
1097 * Task to scan the local network interfaces for IP addresses.
1099 * @param cls the NAT handle
1100 * @param tc scheduler context
1103 list_interfaces (void *cls,
1104 const struct GNUNET_SCHEDULER_TaskContext *tc)
1106 struct GNUNET_NAT_Handle *h = cls;
1109 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
1110 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
1112 GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
1113 &list_interfaces, h);
1118 * Callback with the result from the STUN request.
1120 * @param cls the NAT handle
1121 * @param result the status
1124 stun_request_callback (void *cls,
1125 enum GNUNET_NAT_StatusCode result)
1127 struct GNUNET_NAT_Handle *h = cls;
1129 h->stun_request = NULL;
1130 if (GNUNET_NAT_ERROR_SUCCESS != result)
1132 LOG (GNUNET_ERROR_TYPE_WARNING,
1133 "Error processing a STUN request");
1137 h->waiting_stun = GNUNET_YES;
1143 * CHECK if is a valid STUN packet sending to GNUNET_NAT_stun_handle_packet().
1144 * It also check if it can handle the packet based on the NAT handler.
1145 * You don't need to call anything else to check if the packet is valid,
1147 * @param cls the NAT handle
1148 * @param data packet
1149 * @param len packet length
1150 * @return #GNUNET_NO if it can't decode, #GNUNET_YES if is a packet
1153 GNUNET_NAT_is_valid_stun_packet (void *cls,
1157 struct GNUNET_NAT_Handle *h = cls;
1158 struct sockaddr_in answer;
1160 /* We are not expecting a STUN message */
1161 if (GNUNET_YES != h->waiting_stun)
1164 /* We dont have STUN installed */
1168 /* Empty the answer structure */
1171 sizeof(struct sockaddr_in));
1173 /*Lets handle the packet*/
1175 GNUNET_NAT_stun_handle_packet (data,
1180 LOG (GNUNET_ERROR_TYPE_INFO,
1181 "STUN server returned %s:%d\n",
1182 inet_ntoa (answer.sin_addr),
1183 ntohs (answer.sin_port));
1184 /* Remove old IPs from previous STUN calls */
1185 remove_from_address_list_by_source (h,
1186 LAL_EXTERNAL_STUN_IP);
1187 /* Add new IP from STUN packet */
1188 add_to_address_list (h,
1189 LAL_EXTERNAL_STUN_IP,
1190 (const struct sockaddr *) &answer,
1191 sizeof (struct sockaddr_in));
1192 h->waiting_stun = GNUNET_NO;
1198 * Task to do a STUN request
1200 * @param cls the NAT handle
1201 * @param tc scheduler context
1204 process_stun (void *cls,
1205 const struct GNUNET_SCHEDULER_TaskContext *tc)
1207 struct GNUNET_NAT_Handle *h = cls;
1208 struct StunServerList* elem = h->actual_stun_server;
1210 h->stun_task = NULL;
1211 /* Make the request */
1212 LOG (GNUNET_ERROR_TYPE_INFO,
1213 "I will request the stun server %s:%i\n",
1216 if (NULL != h->stun_request)
1218 GNUNET_NAT_stun_make_request_cancel (h->stun_request);
1219 h->stun_request = NULL;
1221 h->waiting_stun = GNUNET_NO;
1223 = GNUNET_NAT_stun_make_request (elem->address,
1226 &stun_request_callback,
1228 if (NULL == h->stun_request)
1230 LOG (GNUNET_ERROR_TYPE_ERROR,
1231 "STUN request to %s:%i failed\n",
1236 GNUNET_SCHEDULER_add_delayed (h->stun_frequency,
1240 /* Set actual Server*/
1241 if (NULL != elem->next)
1243 h->actual_stun_server = elem->next;
1247 h->actual_stun_server = h->stun_servers_head;
1253 * Task to do a lookup on our hostname for IP addresses.
1255 * @param cls the NAT handle
1256 * @param tc scheduler context
1259 resolve_hostname (void *cls,
1260 const struct GNUNET_SCHEDULER_TaskContext *tc)
1262 struct GNUNET_NAT_Handle *h = cls;
1264 h->hostname_task = NULL;
1265 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
1267 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT,
1268 &process_hostname_ip, h);
1273 * Task to do DNS lookup on our external hostname to
1274 * get DynDNS-IP addresses.
1276 * @param cls the NAT handle
1277 * @param tc scheduler context
1280 resolve_dns (void *cls,
1281 const struct GNUNET_SCHEDULER_TaskContext *tc)
1283 struct GNUNET_NAT_Handle *h = cls;
1284 struct LocalAddressList *pos;
1287 for (pos = h->lal_head; NULL != pos; pos = pos->next)
1288 if (pos->source == LAL_EXTERNAL_IP)
1289 pos->source = LAL_EXTERNAL_IP_OLD;
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "Resolving external address `%s'\n",
1292 h->external_address);
1294 GNUNET_RESOLVER_ip_get (h->external_address, AF_INET,
1295 GNUNET_TIME_UNIT_MINUTES,
1296 &process_external_ip, h);
1301 * Add or remove UPnP-mapped addresses.
1303 * @param cls the `struct GNUNET_NAT_Handle`
1304 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
1305 * the previous (now invalid) one
1306 * @param addr either the previous or the new public IP address
1307 * @param addrlen actual lenght of @a addr
1308 * @param ret GNUNET_NAT_ERROR_SUCCESS on success, otherwise an error code
1311 upnp_add (void *cls,
1313 const struct sockaddr *addr,
1315 enum GNUNET_NAT_StatusCode ret)
1317 struct GNUNET_NAT_Handle *h = cls;
1318 struct LocalAddressList *pos;
1319 struct LocalAddressList *next;
1322 if (GNUNET_NAT_ERROR_SUCCESS != ret)
1324 /* Error while running upnp client */
1325 LOG (GNUNET_ERROR_TYPE_ERROR,
1326 _("Error while running upnp client:\n"));
1327 //FIXME: convert error code to string
1331 if (GNUNET_YES == add_remove)
1333 add_to_address_list (h,
1339 else if (GNUNET_NO == add_remove)
1341 /* remove address */
1343 while (NULL != (pos = next))
1346 if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) ||
1347 (0 != memcmp (&pos[1], addr, addrlen)))
1349 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1352 if (NULL != h->address_callback)
1353 h->address_callback (h->callback_cls,
1355 (const struct sockaddr *) &pos[1],
1358 return; /* only remove once */
1360 /* asked to remove address that does not exist */
1361 LOG (GNUNET_ERROR_TYPE_ERROR,
1362 "Asked to remove unkown address `%s'\n",
1363 GNUNET_a2s(addr, addrlen));
1375 * Try to add a port mapping using UPnP.
1377 * @param h overall NAT handle
1378 * @param port port to map with UPnP
1381 add_minis (struct GNUNET_NAT_Handle *h,
1384 struct MiniList *ml;
1389 if (port == ml->port)
1390 return; /* already got this port */
1394 ml = GNUNET_new (struct MiniList);
1396 ml->mini = GNUNET_NAT_mini_map_start (port, h->is_tcp, &upnp_add, h);
1398 if (NULL == ml->mini)
1400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1401 _("Failed to run upnp client for port %u\n"), ml->port);
1406 GNUNET_CONTAINER_DLL_insert (h->mini_head,
1413 * Task to add addresses from original bind to set of valid addrs.
1415 * @param h the NAT handle
1418 add_from_bind (struct GNUNET_NAT_Handle *h)
1420 static struct in6_addr any = IN6ADDR_ANY_INIT;
1423 struct sockaddr *sa;
1424 const struct sockaddr_in *v4;
1426 for (i = 0; i < h->num_local_addrs; i++)
1428 sa = h->local_addrs[i];
1429 switch (sa->sa_family)
1432 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1437 v4 = (const struct sockaddr_in *) sa;
1438 if (0 != v4->sin_addr.s_addr)
1439 add_to_address_list (h,
1440 LAL_BINDTO_ADDRESS, sa,
1441 sizeof (struct sockaddr_in));
1444 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1445 "Running upnp client for address `%s'\n",
1446 GNUNET_a2s (sa,sizeof (struct sockaddr_in)));
1447 add_minis (h, ntohs (v4->sin_port));
1451 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1457 memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr,
1459 sizeof (struct in6_addr)))
1460 add_to_address_list (h,
1463 sizeof (struct sockaddr_in6));
1473 * Attempt to enable port redirection and detect public IP address contacting
1474 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1475 * of the local host's addresses should the external port be mapped. The port
1476 * is taken from the corresponding sockaddr_in[6] field.
1478 * @param cfg configuration to use
1479 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
1480 * @param adv_port advertised port (port we are either bound to or that our OS
1481 * locally performs redirection from to our bound port).
1482 * @param num_addrs number of addresses in @a addrs
1483 * @param addrs the local addresses packets should be redirected to
1484 * @param addrlens actual lengths of the addresses
1485 * @param address_callback function to call everytime the public IP address changes
1486 * @param reversal_callback function to call if someone wants connection reversal from us
1487 * @param callback_cls closure for callbacks
1488 * @param sock used socket
1489 * @return NULL on error, otherwise handle that can be used to unregister
1491 struct GNUNET_NAT_Handle *
1492 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1495 unsigned int num_addrs,
1496 const struct sockaddr **addrs,
1497 const socklen_t *addrlens,
1498 GNUNET_NAT_AddressCallback address_callback,
1499 GNUNET_NAT_ReversalCallback reversal_callback,
1501 struct GNUNET_NETWORK_Handle *sock)
1503 struct GNUNET_NAT_Handle *h;
1504 struct in_addr in_addr;
1508 LOG (GNUNET_ERROR_TYPE_DEBUG,
1509 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1510 (unsigned int) adv_port, num_addrs);
1511 h = GNUNET_new (struct GNUNET_NAT_Handle);
1512 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1515 h->address_callback = address_callback;
1516 h->reversal_callback = reversal_callback;
1517 h->callback_cls = callback_cls;
1518 h->num_local_addrs = num_addrs;
1519 h->adv_port = adv_port;
1522 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr *));
1523 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1524 for (i = 0; i < num_addrs; i++)
1526 GNUNET_assert (addrlens[i] > 0);
1527 GNUNET_assert (addrs[i] != NULL);
1528 h->local_addrlens[i] = addrlens[i];
1529 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1530 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1534 GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1536 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1538 &h->internal_address);
1540 if ((h->internal_address != NULL) &&
1541 (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1543 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1544 "nat", "INTERNAL_ADDRESS",
1546 GNUNET_free (h->internal_address);
1547 h->internal_address = NULL;
1551 GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1553 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1555 &h->external_address);
1558 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT");
1560 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT");
1561 h->enable_nat_client =
1562 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_CLIENT");
1563 h->enable_nat_server =
1564 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_SERVER");
1566 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP");
1567 h->use_localaddresses =
1568 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR");
1569 h->return_localaddress =
1570 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat",
1571 "RETURN_LOCAL_ADDRESSES");
1574 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME");
1576 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6");
1578 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY",
1579 &h->dyndns_frequency))
1580 h->dyndns_frequency = DYNDNS_FREQUENCY;
1582 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY",
1583 &h->ifc_scan_frequency))
1584 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1586 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY",
1587 &h->hostname_dns_frequency))
1588 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1590 if (NULL == reversal_callback)
1591 h->enable_nat_server = GNUNET_NO;
1593 /* Check for UPnP client, disable immediately if not available */
1594 if ( (GNUNET_YES == h->enable_upnp) &&
1596 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)) )
1598 LOG (GNUNET_ERROR_TYPE_ERROR,
1599 _("UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP \n"));
1600 h->enable_upnp = GNUNET_NO;
1605 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1610 GNUNET_CONFIGURATION_get_value_time (cfg,
1613 &h->stun_frequency))
1614 h->stun_frequency = STUN_FREQUENCY;
1617 /* Check if NAT was hole-punched */
1618 if ((NULL != h->address_callback) &&
1619 (NULL != h->external_address) &&
1620 (GNUNET_YES == h->nat_punched))
1622 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1623 h->enable_nat_server = GNUNET_NO;
1624 h->enable_upnp = GNUNET_NO;
1625 h->use_stun = GNUNET_NO;
1629 LOG (GNUNET_ERROR_TYPE_DEBUG,
1630 "No external IP address given to add to our list of addresses\n");
1633 /* ENABLE STUN ONLY ON UDP */
1644 h->actual_stun_server = NULL;
1645 /* Lets process the servers*/
1647 GNUNET_CONFIGURATION_get_value_string (cfg,
1652 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
1658 h->stun_servers_head = NULL;
1659 h->stun_servers_tail = NULL;
1660 h->actual_stun_server = NULL;
1661 if (strlen (stun_servers) > 0)
1663 pos = strlen (stun_servers) - 1;
1667 if (stun_servers[pos] == ':')
1671 if ((stun_servers[pos] == ' ') || (0 == pos))
1674 /*Check if we do have a port*/
1675 if((0 == pos_port) || (pos_port <= pos))
1677 LOG (GNUNET_ERROR_TYPE_WARNING,
1678 "STUN server format mistake\n");
1684 struct StunServerList* ml = GNUNET_new (struct StunServerList);
1686 ml->port = atoi (&stun_servers[pos_port]);
1687 stun_servers[pos_port-1] = '\0';
1689 /* Remove trailing space */
1690 if(stun_servers[pos] == ' ')
1691 ml->address = GNUNET_strdup (&stun_servers[pos + 1]);
1693 ml->address = GNUNET_strdup (&stun_servers[pos]);
1695 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 "Found STUN server %s:%i\n",
1700 GNUNET_CONTAINER_DLL_insert (h->stun_servers_head,
1701 h->stun_servers_tail,
1703 /* Make sure that we STOP if is the last one*/
1713 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
1714 "nat", "STUN_SERVERS");
1718 /* Set the actual STUN server*/
1719 h->actual_stun_server = h->stun_servers_head;
1721 h->stun_task = GNUNET_SCHEDULER_add_now (&process_stun,
1726 /* Test for SUID binaries */
1727 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
1728 if ( (GNUNET_YES == h->behind_nat) &&
1729 (GNUNET_YES == h->enable_nat_server) &&
1731 GNUNET_OS_check_helper_binary (binary,
1735 // use localhost as source for that one udp-port, ok for testing
1736 h->enable_nat_server = GNUNET_NO;
1737 LOG (GNUNET_ERROR_TYPE_WARNING,
1738 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1739 "gnunet-helper-nat-server");
1741 GNUNET_free (binary);
1742 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
1743 if ((GNUNET_YES == h->enable_nat_client) &&
1745 GNUNET_OS_check_helper_binary (binary,
1747 "-d 127.0.0.1 127.0.0.2 42"))) /* none of these parameters are actually used in privilege testing mode */
1749 h->enable_nat_client = GNUNET_NO;
1750 LOG (GNUNET_ERROR_TYPE_WARNING,
1751 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1752 "gnunet-helper-nat-client");
1754 GNUNET_free (binary);
1755 start_gnunet_nat_server (h);
1757 /* FIXME: add support for UPnP, etc */
1759 if (NULL != h->address_callback)
1761 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces,
1763 if (GNUNET_YES == h->use_hostname)
1764 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname,
1774 * Stop port redirection and public IP address detection for the given handle.
1775 * This frees the handle, after having sent the needed commands to close open ports.
1777 * @param h the handle to stop
1780 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1783 struct LocalAddressList *lal;
1784 struct MiniList *ml;
1786 LOG (GNUNET_ERROR_TYPE_DEBUG,
1787 "NAT unregister called\n");
1788 while (NULL != (ml = h->mini_head))
1790 GNUNET_CONTAINER_DLL_remove (h->mini_head,
1793 if (NULL != ml->mini)
1794 GNUNET_NAT_mini_map_stop (ml->mini);
1797 if (NULL != h->ext_dns)
1799 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1802 if (NULL != h->hostname_dns)
1804 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1805 h->hostname_dns = NULL;
1807 if (NULL != h->server_read_task)
1809 GNUNET_SCHEDULER_cancel (h->server_read_task);
1810 h->server_read_task = NULL;
1812 if (NULL != h->ifc_task)
1814 GNUNET_SCHEDULER_cancel (h->ifc_task);
1817 if (NULL != h->hostname_task)
1819 GNUNET_SCHEDULER_cancel (h->hostname_task);
1820 h->hostname_task = NULL;
1822 if (NULL != h->dns_task)
1824 GNUNET_SCHEDULER_cancel (h->dns_task);
1827 if (NULL != h->stun_task)
1829 GNUNET_SCHEDULER_cancel (h->stun_task);
1830 h->stun_task = NULL;
1832 if (NULL != h->stun_request)
1834 GNUNET_NAT_stun_make_request_cancel (h->stun_request);
1835 h->stun_request = NULL;
1837 if (NULL != h->server_proc)
1839 if (0 != GNUNET_OS_process_kill (h->server_proc,
1841 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
1844 GNUNET_OS_process_wait (h->server_proc);
1845 GNUNET_OS_process_destroy (h->server_proc);
1846 h->server_proc = NULL;
1847 GNUNET_DISK_pipe_close (h->server_stdout);
1848 h->server_stdout = NULL;
1849 h->server_stdout_handle = NULL;
1851 if (NULL != h->server_stdout)
1853 GNUNET_DISK_pipe_close (h->server_stdout);
1854 h->server_stdout = NULL;
1855 h->server_stdout_handle = NULL;
1857 while (NULL != (lal = h->lal_head))
1859 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1862 if (NULL != h->address_callback)
1863 h->address_callback (h->callback_cls,
1865 (const struct sockaddr *) &lal[1],
1869 for (i = 0; i < h->num_local_addrs; i++)
1870 GNUNET_free (h->local_addrs[i]);
1871 GNUNET_free_non_null (h->local_addrs);
1872 GNUNET_free_non_null (h->local_addrlens);
1873 GNUNET_free_non_null (h->external_address);
1874 GNUNET_free_non_null (h->internal_address);
1880 * We learned about a peer (possibly behind NAT) so run the
1881 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1882 * that peer to connect to us (connection reversal).
1884 * @param h handle (used for configuration)
1885 * @param sa the address of the peer (IPv4-only)
1886 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
1887 * #GNUNET_OK otherwise
1890 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1891 const struct sockaddr_in *sa)
1895 char inet4[INET_ADDRSTRLEN];
1896 char port_as_string[6];
1897 struct GNUNET_OS_Process *proc;
1900 if (GNUNET_YES != h->enable_nat_client)
1901 return GNUNET_NO; /* not permitted / possible */
1903 if (h->internal_address == NULL)
1905 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
1906 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1907 return GNUNET_SYSERR;
1909 GNUNET_assert (sa->sin_family == AF_INET);
1910 if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN))
1912 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
1915 return GNUNET_SYSERR;
1917 GNUNET_snprintf (port_as_string,
1918 sizeof (port_as_string),
1921 LOG (GNUNET_ERROR_TYPE_DEBUG,
1922 _("Running gnunet-helper-nat-client %s %s %u\n"),
1923 h->internal_address,
1925 (unsigned int) h->adv_port);
1926 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
1928 GNUNET_OS_start_process (GNUNET_NO, 0, NULL, NULL, NULL,
1930 "gnunet-helper-nat-client",
1931 h->internal_address,
1932 inet4, port_as_string, NULL);
1933 GNUNET_free (binary);
1935 return GNUNET_SYSERR;
1936 /* we know that the gnunet-helper-nat-client will terminate virtually
1938 GNUNET_OS_process_wait (proc);
1939 GNUNET_OS_process_destroy (proc);
1945 * Test if the given address is (currently) a plausible IP address for this peer.
1947 * @param h the handle returned by register
1948 * @param addr IP address to test (IPv4 or IPv6)
1949 * @param addrlen number of bytes in @a addr
1950 * @return #GNUNET_YES if the address is plausible,
1951 * #GNUNET_NO if the address is not plausible,
1952 * #GNUNET_SYSERR if the address is malformed
1955 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1959 struct LocalAddressList *pos;
1960 const struct sockaddr_in *in4;
1961 const struct sockaddr_in6 *in6;
1963 if ((addrlen != sizeof (struct in_addr)) &&
1964 (addrlen != sizeof (struct in6_addr)))
1967 return GNUNET_SYSERR;
1969 for (pos = h->lal_head; NULL != pos; pos = pos->next)
1971 if (pos->addrlen == sizeof (struct sockaddr_in))
1973 in4 = (struct sockaddr_in *) &pos[1];
1974 if ((addrlen == sizeof (struct in_addr)) &&
1975 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))))
1978 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1980 in6 = (struct sockaddr_in6 *) &pos[1];
1981 if ((addrlen == sizeof (struct in6_addr)) &&
1982 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))))
1990 LOG (GNUNET_ERROR_TYPE_WARNING,
1991 "Asked to validate one of my addresses (%s) and validation failed!\n",
1998 * Converts enum GNUNET_NAT_StatusCode to a string
2000 * @param err error code to resolve to a string
2001 * @return pointer to a static string containing the error code
2004 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err)
2008 case GNUNET_NAT_ERROR_SUCCESS:
2009 return _ ("Operation Successful");
2010 case GNUNET_NAT_ERROR_IPC_FAILURE:
2011 return _ ("Internal Failure (IPC, ...)");
2012 case GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR:
2013 return _ ("Failure in network subsystem, check permissions.");
2014 case GNUNET_NAT_ERROR_TIMEOUT:
2015 return _ ("Encountered timeout while performing operation");
2016 case GNUNET_NAT_ERROR_NOT_ONLINE:
2017 return _ ("detected that we are offline");
2018 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
2019 return _ ("`upnpc` command not found");
2020 case GNUNET_NAT_ERROR_UPNPC_FAILED:
2021 return _ ("Failed to run `upnpc` command");
2022 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
2023 return _ ("`upnpc' command took too long, process killed");
2024 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
2025 return _ ("`upnpc' command failed to establish port mapping");
2026 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
2027 return _ ("`external-ip' command not found");
2028 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
2029 return _ ("Failed to run `external-ip` command");
2030 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
2031 return _ ("`external-ip' command output invalid");
2032 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
2033 return _ ("no valid address was returned by `external-ip'");
2034 case GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO:
2035 return _ ("Could not determine interface with internal/local network address");
2036 case GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND:
2037 return _ ("No functioning gnunet-helper-nat-server installation found");
2038 case GNUNET_NAT_ERROR_NAT_TEST_START_FAILED:
2039 return _ ("NAT test could not be initialized");
2040 case GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT:
2041 return _ ("NAT test timeout reached");
2042 case GNUNET_NAT_ERROR_NAT_REGISTER_FAILED:
2043 return _ ("could not register NAT");
2044 case GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND:
2045 return _ ("No working gnunet-helper-nat-client installation found");
2049 return "unknown status code";