2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * Where did the given local address originate from?
61 * To be used for debugging as well as in the future
62 * to remove all addresses from a certain source when
63 * we reevaluate the source.
65 enum LocalAddressSource
68 * Address was obtained by DNS resolution of the external hostname
69 * given in the configuration (i.e. hole-punched DynDNS setup).
74 * Address was obtained by DNS resolution of the external hostname
75 * given in the configuration (i.e. hole-punched DynDNS setup)
76 * during the previous iteration (see #3213).
81 * Address was obtained by looking up our own hostname in DNS.
86 * Address was obtained by scanning our hosts's network interfaces
87 * and taking their address (no DNS involved).
89 LAL_INTERFACE_ADDRESS,
92 * Addresses we were explicitly bound to.
97 * Addresses from UPnP or PMP
109 * List of local addresses that we currently deem valid. Actual
110 * struct is followed by the 'struct sockaddr'. Note that the code
111 * intentionally makes no attempt to ensure that a particular address
112 * is only listed once (especially since it may come from different
113 * sources, and the source is an "internal" construct).
115 struct LocalAddressList
118 * This is a linked list.
120 struct LocalAddressList *next;
125 struct LocalAddressList *prev;
128 * Number of bytes of address that follow.
133 * Origin of the local address.
135 enum LocalAddressSource source;
140 * Handle for miniupnp-based NAT traversal actions.
146 * Doubly-linked list.
148 struct MiniList *next;
151 * Doubly-linked list.
153 struct MiniList *prev;
156 * Handle to mini-action.
158 struct GNUNET_NAT_MiniHandle *mini;
161 * Local port number that was mapped.
169 * Handle for active NAT registrations.
171 struct GNUNET_NAT_Handle
175 * Configuration to use.
177 const struct GNUNET_CONFIGURATION_Handle *cfg;
180 * Function to call when we learn about a new address.
182 GNUNET_NAT_AddressCallback address_callback;
185 * Function to call when we notice another peer asking for
186 * connection reversal.
188 GNUNET_NAT_ReversalCallback reversal_callback;
191 * Closure for callbacks (@e address_callback and @e reversal_callback)
196 * Handle for (DYN)DNS lookup of our external IP.
198 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
201 * Handle for request of hostname resolution, non-NULL if pending.
203 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
206 * stdout pipe handle for the gnunet-helper-nat-server process
208 struct GNUNET_DISK_PipeHandle *server_stdout;
211 * stdout file handle (for reading) for the gnunet-helper-nat-server process
213 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
216 * Linked list of currently valid addresses (head).
218 struct LocalAddressList *lal_head;
221 * Linked list of currently valid addresses (tail).
223 struct LocalAddressList *lal_tail;
226 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
228 struct GNUNET_TIME_Relative server_retry_delay;
231 * ID of select gnunet-helper-nat-server stdout read task
233 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
236 * ID of interface IP-scan task
238 GNUNET_SCHEDULER_TaskIdentifier ifc_task;
241 * ID of hostname DNS lookup task
243 GNUNET_SCHEDULER_TaskIdentifier hostname_task;
246 * ID of DynDNS lookup task
248 GNUNET_SCHEDULER_TaskIdentifier dns_task;
251 * ID of task to add addresses from bind.
253 GNUNET_SCHEDULER_TaskIdentifier bind_task;
256 * How often do we scan for changes in our IP address from our local
259 struct GNUNET_TIME_Relative ifc_scan_frequency;
262 * How often do we scan for changes in how our hostname resolves?
264 struct GNUNET_TIME_Relative hostname_dns_frequency;
267 * How often do we scan for changes in how our external (dyndns) hostname resolves?
269 struct GNUNET_TIME_Relative dyndns_frequency;
272 * The process id of the server process (if behind NAT)
274 struct GNUNET_OS_Process *server_proc;
277 * LAN address as passed by the caller (array).
279 struct sockaddr **local_addrs;
282 * Length of the @e local_addrs.
284 socklen_t *local_addrlens;
287 * List of handles for UPnP-traversal, one per local port (if
290 struct MiniList *mini_head;
293 * List of handles for UPnP-traversal, one per local port (if
296 struct MiniList *mini_tail;
299 * Number of entries in 'local_addrs' array.
301 unsigned int num_local_addrs;
304 * Our external address (according to config, UPnP may disagree...),
305 * in dotted decimal notation, IPv4-only. Or NULL if not known.
307 char *external_address;
310 * Presumably our internal address (according to config)
312 char *internal_address;
315 * Is this transport configured to be behind a NAT?
320 * Has the NAT been punched? (according to config)
325 * Is this transport configured to allow connections to NAT'd peers?
327 int enable_nat_client;
330 * Should we run the gnunet-helper-nat-server?
332 int enable_nat_server;
335 * Are we allowed to try UPnP/PMP for NAT traversal?
340 * Should we use local addresses (loopback)? (according to config)
342 int use_localaddresses;
345 * Should we return local addresses to clients
347 int return_localaddress;
350 * Should we do a DNS lookup of our hostname to find out our own IP?
355 * Is using IPv6 disabled?
360 * Is this TCP or UDP?
365 * Port we advertise to the outside.
373 * Try to start the gnunet-helper-nat-server (if it is not
376 * @param h handle to NAT
379 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
383 * Remove all addresses from the list of 'local' addresses
384 * that originated from the given source.
386 * @param h handle to NAT
387 * @param src source that identifies addresses to remove
390 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
391 enum LocalAddressSource src)
393 struct LocalAddressList *pos;
394 struct LocalAddressList *next;
397 while (NULL != (pos = next))
400 if (pos->source != src)
402 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
403 if (NULL != h->address_callback)
404 h->address_callback (h->callback_cls, GNUNET_NO,
405 (const struct sockaddr *) &pos[1], pos->addrlen);
412 * Add the given address to the list of 'local' addresses, thereby
413 * making it a 'legal' address for this peer to have.
415 * @param h handle to NAT
416 * @param src where did the local address originate from?
417 * @param arg the address, some `struct sockaddr`
418 * @param arg_size number of bytes in @a arg
421 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
422 enum LocalAddressSource src,
423 const struct sockaddr *arg, socklen_t arg_size)
425 struct LocalAddressList *lal;
427 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
428 memcpy (&lal[1], arg, arg_size);
429 lal->addrlen = arg_size;
431 GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal);
432 LOG (GNUNET_ERROR_TYPE_DEBUG,
433 "Adding address `%s' from source %d\n",
434 GNUNET_a2s (arg, arg_size),
436 if (NULL != h->address_callback)
437 h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size);
442 * Add the given address to the list of 'local' addresses, thereby
443 * making it a 'legal' address for this peer to have. Set the
444 * port number in the process to the advertised port and possibly
445 * also to zero (if we have the gnunet-helper-nat-server).
447 * @param h handle to NAT
448 * @param src where did the local address originate from?
449 * @param arg the address, some `struct sockaddr`
450 * @param arg_size number of bytes in @a arg
453 add_to_address_list (struct GNUNET_NAT_Handle *h,
454 enum LocalAddressSource src,
455 const struct sockaddr *arg,
458 struct sockaddr_in s4;
459 const struct sockaddr_in *in4;
460 struct sockaddr_in6 s6;
461 const struct sockaddr_in6 *in6;
463 if (arg_size == sizeof (struct sockaddr_in))
465 in4 = (const struct sockaddr_in *) arg;
467 s4.sin_port = htons (h->adv_port);
468 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
469 sizeof (struct sockaddr_in));
470 if (GNUNET_YES == h->enable_nat_server)
472 /* also add with PORT = 0 to indicate NAT server is enabled */
473 s4.sin_port = htons (0);
474 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
475 sizeof (struct sockaddr_in));
478 else if (arg_size == sizeof (struct sockaddr_in6))
480 if (GNUNET_YES != h->disable_ipv6)
482 in6 = (const struct sockaddr_in6 *) arg;
484 s6.sin6_port = htons (h->adv_port);
485 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s6,
486 sizeof (struct sockaddr_in6));
497 * Add the given IP address to the list of 'local' addresses, thereby
498 * making it a 'legal' address for this peer to have.
500 * @param h handle to NAT
501 * @param src where did the local address originate from?
502 * @param addr the address, some `struct in_addr` or `struct in6_addr`
503 * @param addrlen number of bytes in addr
506 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
507 enum LocalAddressSource src, const void *addr,
510 struct sockaddr_in s4;
511 const struct in_addr *in4;
512 struct sockaddr_in6 s6;
513 const struct in6_addr *in6;
515 if (addrlen == sizeof (struct in_addr))
517 in4 = (const struct in_addr *) addr;
518 memset (&s4, 0, sizeof (s4));
519 s4.sin_family = AF_INET;
521 #if HAVE_SOCKADDR_IN_SIN_LEN
522 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
525 add_to_address_list (h, src, (const struct sockaddr *) &s4,
526 sizeof (struct sockaddr_in));
527 if (GNUNET_YES == h->enable_nat_server)
529 /* also add with PORT = 0 to indicate NAT server is enabled */
530 s4.sin_port = htons (0);
531 add_to_address_list (h, src, (const struct sockaddr *) &s4,
532 sizeof (struct sockaddr_in));
536 else if (addrlen == sizeof (struct in6_addr))
538 if (GNUNET_YES != h->disable_ipv6)
540 in6 = (const struct in6_addr *) addr;
541 memset (&s6, 0, sizeof (s6));
542 s6.sin6_family = AF_INET6;
543 s6.sin6_port = htons (h->adv_port);
544 #if HAVE_SOCKADDR_IN_SIN_LEN
545 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
548 add_to_address_list (h, src, (const struct sockaddr *) &s6,
549 sizeof (struct sockaddr_in6));
560 * Task to do DNS lookup on our external hostname to
561 * get DynDNS-IP addresses.
563 * @param cls the NAT handle
564 * @param tc scheduler context
567 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
571 * Our (external) hostname was resolved and the configuration says that
572 * the NAT was hole-punched.
574 * @param cls the `struct GNUNET_NAT_Handle`
575 * @param addr NULL on error, otherwise result of DNS lookup
576 * @param addrlen number of bytes in @a addr
579 process_external_ip (void *cls,
580 const struct sockaddr *addr,
583 struct GNUNET_NAT_Handle *h = cls;
584 struct in_addr dummy;
589 /* Current iteration is over, remove 'old' IPs now */
590 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
591 if (1 == inet_pton (AF_INET,
594 return; /* repated lookup pointless: was numeric! */
596 GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
600 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
605 * Task to do a lookup on our hostname for IP addresses.
607 * @param cls the NAT handle
608 * @param tc scheduler context
611 resolve_hostname (void *cls,
612 const struct GNUNET_SCHEDULER_TaskContext *tc);
616 * Function called by the resolver for each address obtained from DNS
617 * for our own hostname. Add the addresses to the list of our IP
621 * @param addr one of the addresses of the host, NULL for the last address
622 * @param addrlen length of the @a addr
625 process_hostname_ip (void *cls,
626 const struct sockaddr *addr,
629 struct GNUNET_NAT_Handle *h = cls;
633 h->hostname_dns = NULL;
635 GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
636 &resolve_hostname, h);
639 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
644 * Add the IP of our network interface to the list of
647 * @param cls the `struct GNUNET_NAT_Handle`
648 * @param name name of the interface
649 * @param isDefault do we think this may be our default interface
650 * @param addr address of the interface
651 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
652 * @param netmask the network mask (can be NULL for unknown or unassigned))
653 * @param addrlen number of bytes in @a addr and @a broadcast_addr
654 * @return #GNUNET_OK to continue iterating
657 process_interfaces (void *cls, const char *name, int isDefault,
658 const struct sockaddr *addr,
659 const struct sockaddr *broadcast_addr,
660 const struct sockaddr *netmask, socklen_t addrlen)
662 const static struct in6_addr any6 = IN6ADDR_ANY_INIT;
663 struct GNUNET_NAT_Handle *h = cls;
664 const struct sockaddr_in *s4;
665 const struct sockaddr_in6 *s6;
667 char buf[INET6_ADDRSTRLEN];
671 switch (addr->sa_family)
674 /* check if we're bound to the "ANY" IP address */
675 have_any = GNUNET_NO;
676 for (i=0;i<h->num_local_addrs;i++)
678 if (h->local_addrs[i]->sa_family != AF_INET)
683 if (INADDR_ANY == ((struct sockaddr_in*) h->local_addrs[i])->sin_addr.s_addr)
685 have_any = GNUNET_YES;
689 if (GNUNET_NO == have_any)
690 return GNUNET_OK; /* not bound to IP 0.0.0.0 but to specific IP addresses,
691 do not use those from interfaces */
692 s4 = (struct sockaddr_in *) addr;
695 /* Check if address is in 127.0.0.0/8 */
696 uint32_t address = ntohl ((uint32_t) (s4->sin_addr.s_addr));
697 uint32_t value = (address & 0xFF000000) ^ 0x7F000000;
699 if ((h->return_localaddress == GNUNET_NO) && (value == 0))
703 if (GNUNET_YES == h->use_localaddresses)
705 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s4->sin_addr,
706 sizeof (struct in_addr));
710 /* check if we're bound to the "ANY" IP address */
711 have_any = GNUNET_NO;
712 for (i=0;i<h->num_local_addrs;i++)
714 if (h->local_addrs[i]->sa_family != AF_INET6)
716 if (0 == memcmp (&any6,
717 &((struct sockaddr_in6*) h->local_addrs[i])->sin6_addr,
718 sizeof (struct in6_addr)))
720 have_any = GNUNET_YES;
724 if (GNUNET_NO == have_any)
725 return GNUNET_OK; /* not bound to "ANY" IP (::0) but to specific IP addresses,
726 do not use those from interfaces */
728 s6 = (struct sockaddr_in6 *) addr;
729 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
731 /* skip link local addresses */
734 if ((h->return_localaddress == GNUNET_NO) &&
735 (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr)))
740 if (GNUNET_YES == h->use_localaddresses)
742 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s6->sin6_addr,
743 sizeof (struct in6_addr));
750 if ((h->internal_address == NULL) && (h->server_proc == NULL) &&
751 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
752 (GNUNET_YES == isDefault) && ((addr->sa_family == AF_INET) ||
753 (addr->sa_family == AF_INET6)))
755 /* no internal address configured, but we found a "default"
756 * interface, try using that as our 'internal' address */
757 h->internal_address =
758 GNUNET_strdup (inet_ntop (addr->sa_family, ip, buf, sizeof (buf)));
759 start_gnunet_nat_server (h);
766 * Task that restarts the gnunet-helper-nat-server process after a crash
767 * after a certain delay.
769 * @param cls the `struct GNUNET_NAT_Handle`
770 * @param tc scheduler context
773 restart_nat_server (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
775 struct GNUNET_NAT_Handle *h = cls;
777 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
778 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
780 start_gnunet_nat_server (h);
785 * We have been notified that gnunet-helper-nat-server has written
786 * something to stdout. Handle the output, then reschedule this
787 * function to be called again once more is available.
789 * @param cls the NAT handle
790 * @param tc the scheduling context
793 nat_server_read (void *cls,
794 const struct GNUNET_SCHEDULER_TaskContext *tc)
796 struct GNUNET_NAT_Handle *h = cls;
801 const char *port_start;
802 struct sockaddr_in sin_addr;
804 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
805 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
807 memset (mybuf, 0, sizeof (mybuf));
809 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
812 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 "Finished reading from server stdout with code: %d\n",
815 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
816 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
817 GNUNET_OS_process_wait (h->server_proc);
818 GNUNET_OS_process_destroy (h->server_proc);
819 h->server_proc = NULL;
820 GNUNET_DISK_pipe_close (h->server_stdout);
821 h->server_stdout = NULL;
822 h->server_stdout_handle = NULL;
823 /* now try to restart it */
824 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
825 h->server_read_task =
826 GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
827 &restart_nat_server, h);
832 for (i = 0; i < sizeof (mybuf); i++)
834 if (mybuf[i] == '\n')
839 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
842 port_start = &mybuf[i + 1];
846 /* construct socket address of sender */
847 memset (&sin_addr, 0, sizeof (sin_addr));
848 sin_addr.sin_family = AF_INET;
849 #if HAVE_SOCKADDR_IN_SIN_LEN
850 sin_addr.sin_len = sizeof (sin_addr);
852 if ((NULL == port_start) || (1 != SSCANF (port_start, "%d", &port)) ||
853 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
855 /* should we restart gnunet-helper-nat-server? */
856 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
857 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
859 h->server_read_task =
860 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
861 h->server_stdout_handle,
862 &nat_server_read, h);
865 sin_addr.sin_port = htons ((uint16_t) port);
866 LOG (GNUNET_ERROR_TYPE_DEBUG, "gnunet-helper-nat-server read: %s:%d\n", mybuf,
868 h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr,
870 h->server_read_task =
871 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
872 h->server_stdout_handle, &nat_server_read,
878 * Try to start the gnunet-helper-nat-server (if it is not
881 * @param h handle to NAT
884 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
888 if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) &&
889 (h->internal_address != NULL) &&
892 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES))))
894 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting `%s' at `%s'\n",
895 "gnunet-helper-nat-server", h->internal_address);
896 /* Start the server process */
897 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
899 GNUNET_OS_start_process (GNUNET_NO, 0, NULL, h->server_stdout,
901 "gnunet-helper-nat-server",
902 h->internal_address, NULL);
903 GNUNET_free (binary);
904 if (h->server_proc == NULL)
906 LOG (GNUNET_ERROR_TYPE_WARNING, "nat", _("Failed to start %s\n"),
907 "gnunet-helper-nat-server");
908 GNUNET_DISK_pipe_close (h->server_stdout);
909 h->server_stdout = NULL;
913 /* Close the write end of the read pipe */
914 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
915 h->server_stdout_handle =
916 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
917 h->server_read_task =
918 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
919 h->server_stdout_handle,
920 &nat_server_read, h);
927 * Task to scan the local network interfaces for IP addresses.
929 * @param cls the NAT handle
930 * @param tc scheduler context
933 list_interfaces (void *cls,
934 const struct GNUNET_SCHEDULER_TaskContext *tc)
936 struct GNUNET_NAT_Handle *h = cls;
938 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
939 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
940 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
942 GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
943 &list_interfaces, h);
948 * Task to do a lookup on our hostname for IP addresses.
950 * @param cls the NAT handle
951 * @param tc scheduler context
954 resolve_hostname (void *cls,
955 const struct GNUNET_SCHEDULER_TaskContext *tc)
957 struct GNUNET_NAT_Handle *h = cls;
959 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
960 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
962 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT,
963 &process_hostname_ip, h);
968 * Task to do DNS lookup on our external hostname to
969 * get DynDNS-IP addresses.
971 * @param cls the NAT handle
972 * @param tc scheduler context
975 resolve_dns (void *cls,
976 const struct GNUNET_SCHEDULER_TaskContext *tc)
978 struct GNUNET_NAT_Handle *h = cls;
979 struct LocalAddressList *pos;
981 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
982 for (pos = h->lal_head; NULL != pos; pos = pos->next)
983 if (pos->source == LAL_EXTERNAL_IP)
984 pos->source = LAL_EXTERNAL_IP_OLD;
986 GNUNET_RESOLVER_ip_get (h->external_address, AF_INET,
987 GNUNET_TIME_UNIT_MINUTES,
988 &process_external_ip, h);
993 * Add or remove UPnP-mapped addresses.
995 * @param cls the `struct GNUNET_NAT_Handle`
996 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
997 * the previous (now invalid) one
998 * @param addr either the previous or the new public IP address
999 * @param addrlen actual lenght of @a addr
1002 upnp_add (void *cls,
1004 const struct sockaddr *addr,
1007 struct GNUNET_NAT_Handle *h = cls;
1008 struct LocalAddressList *pos;
1009 struct LocalAddressList *next;
1011 if (GNUNET_YES == add_remove)
1013 add_to_address_list (h, LAL_UPNP, addr, addrlen);
1016 /* remove address */
1018 while (NULL != (pos = next))
1021 if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) ||
1022 (0 != memcmp (&pos[1], addr, addrlen)))
1024 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
1025 if (NULL != h->address_callback)
1026 h->address_callback (h->callback_cls, GNUNET_NO,
1027 (const struct sockaddr *) &pos[1], pos->addrlen);
1029 return; /* only remove once */
1031 /* asked to remove address that does not exist */
1037 * Try to add a port mapping using UPnP.
1039 * @param h overall NAT handle
1040 * @param port port to map with UPnP
1043 add_minis (struct GNUNET_NAT_Handle *h,
1046 struct MiniList *ml;
1051 if (port == ml->port)
1052 return; /* already got this port */
1055 ml = GNUNET_new (struct MiniList);
1057 ml->mini = GNUNET_NAT_mini_map_start (port, h->is_tcp, &upnp_add, h);
1058 GNUNET_CONTAINER_DLL_insert (h->mini_head, h->mini_tail, ml);
1063 * Task to add addresses from original bind to set of valid addrs.
1065 * @param cls the NAT handle
1066 * @param tc scheduler context
1069 add_from_bind (void *cls,
1070 const struct GNUNET_SCHEDULER_TaskContext *tc)
1072 static struct in6_addr any = IN6ADDR_ANY_INIT;
1073 struct GNUNET_NAT_Handle *h = cls;
1075 struct sockaddr *sa;
1076 const struct sockaddr_in *v4;
1078 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1079 for (i = 0; i < h->num_local_addrs; i++)
1081 sa = h->local_addrs[i];
1082 switch (sa->sa_family)
1085 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1090 v4 = (const struct sockaddr_in *) sa;
1091 if (0 != v4->sin_addr.s_addr)
1092 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1093 sizeof (struct sockaddr_in));
1095 add_minis (h, ntohs (v4->sin_port));
1098 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1104 memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, &any,
1105 sizeof (struct in6_addr)))
1106 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1107 sizeof (struct sockaddr_in6));
1118 * Attempt to enable port redirection and detect public IP address contacting
1119 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1120 * of the local host's addresses should the external port be mapped. The port
1121 * is taken from the corresponding sockaddr_in[6] field.
1123 * @param cfg configuration to use
1124 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
1125 * @param adv_port advertised port (port we are either bound to or that our OS
1126 * locally performs redirection from to our bound port).
1127 * @param num_addrs number of addresses in 'addrs'
1128 * @param addrs the local addresses packets should be redirected to
1129 * @param addrlens actual lengths of the addresses
1130 * @param address_callback function to call everytime the public IP address changes
1131 * @param reversal_callback function to call if someone wants connection reversal from us
1132 * @param callback_cls closure for callbacks
1133 * @return NULL on error, otherwise handle that can be used to unregister
1135 struct GNUNET_NAT_Handle *
1136 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1139 unsigned int num_addrs,
1140 const struct sockaddr **addrs,
1141 const socklen_t * addrlens,
1142 GNUNET_NAT_AddressCallback address_callback,
1143 GNUNET_NAT_ReversalCallback reversal_callback,
1146 struct GNUNET_NAT_Handle *h;
1147 struct in_addr in_addr;
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1153 (unsigned int) adv_port, num_addrs);
1154 h = GNUNET_new (struct GNUNET_NAT_Handle);
1155 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1158 h->address_callback = address_callback;
1159 h->reversal_callback = reversal_callback;
1160 h->callback_cls = callback_cls;
1161 h->num_local_addrs = num_addrs;
1162 h->adv_port = adv_port;
1165 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr *));
1166 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1167 for (i = 0; i < num_addrs; i++)
1169 GNUNET_assert (addrlens[i] > 0);
1170 GNUNET_assert (addrs[i] != NULL);
1171 h->local_addrlens[i] = addrlens[i];
1172 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1173 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1176 h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1178 GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1180 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1182 &h->internal_address);
1184 if ((h->internal_address != NULL) &&
1185 (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1187 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1188 "nat", "INTERNAL_ADDRESS",
1190 GNUNET_free (h->internal_address);
1191 h->internal_address = NULL;
1195 GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1197 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1199 &h->external_address);
1202 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT");
1204 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT");
1205 h->enable_nat_client =
1206 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_CLIENT");
1207 h->enable_nat_server =
1208 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_SERVER");
1210 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP");
1211 h->use_localaddresses =
1212 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR");
1213 h->return_localaddress =
1214 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat",
1215 "RETURN_LOCAL_ADDRESSES");
1218 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME");
1220 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6");
1222 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY",
1223 &h->dyndns_frequency))
1224 h->dyndns_frequency = DYNDNS_FREQUENCY;
1226 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY",
1227 &h->ifc_scan_frequency))
1228 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1230 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY",
1231 &h->hostname_dns_frequency))
1232 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1234 if (NULL == reversal_callback)
1235 h->enable_nat_server = GNUNET_NO;
1237 /* Check if NAT was hole-punched */
1238 if ((NULL != h->address_callback) && (h->external_address != NULL) &&
1239 (h->nat_punched == GNUNET_YES))
1241 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1242 h->enable_nat_server = GNUNET_NO;
1243 h->enable_upnp = GNUNET_NO;
1246 /* Test for SUID binaries */
1247 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
1248 if ((h->behind_nat == GNUNET_YES) && (GNUNET_YES == h->enable_nat_server) &&
1250 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
1252 h->enable_nat_server = GNUNET_NO;
1253 LOG (GNUNET_ERROR_TYPE_WARNING,
1255 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1256 "gnunet-helper-nat-server");
1258 GNUNET_free (binary);
1259 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
1260 if ((GNUNET_YES == h->enable_nat_client) &&
1262 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
1264 h->enable_nat_client = GNUNET_NO;
1265 LOG (GNUNET_ERROR_TYPE_WARNING,
1267 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1268 "gnunet-helper-nat-client");
1270 GNUNET_free (binary);
1271 start_gnunet_nat_server (h);
1273 /* FIXME: add support for UPnP, etc */
1275 if (NULL != h->address_callback)
1277 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1278 if (GNUNET_YES == h->use_hostname)
1279 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1287 * Stop port redirection and public IP address detection for the given handle.
1288 * This frees the handle, after having sent the needed commands to close open ports.
1290 * @param h the handle to stop
1293 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1296 struct LocalAddressList *lal;
1297 struct MiniList *ml;
1299 while (NULL != (ml = h->mini_head))
1301 GNUNET_CONTAINER_DLL_remove (h->mini_head, h->mini_tail, ml);
1302 if (NULL != ml->mini)
1303 GNUNET_NAT_mini_map_stop (ml->mini);
1306 if (NULL != h->ext_dns)
1308 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1311 if (NULL != h->hostname_dns)
1313 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1314 h->hostname_dns = NULL;
1316 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1318 GNUNET_SCHEDULER_cancel (h->server_read_task);
1319 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1321 if (GNUNET_SCHEDULER_NO_TASK != h->bind_task)
1323 GNUNET_SCHEDULER_cancel (h->bind_task);
1324 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1326 if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1328 GNUNET_SCHEDULER_cancel (h->ifc_task);
1329 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1331 if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1333 GNUNET_SCHEDULER_cancel (h->hostname_task);
1334 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1336 if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1338 GNUNET_SCHEDULER_cancel (h->dns_task);
1339 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1341 if (NULL != h->server_proc)
1343 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
1344 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
1345 GNUNET_OS_process_wait (h->server_proc);
1346 GNUNET_OS_process_destroy (h->server_proc);
1347 h->server_proc = NULL;
1348 GNUNET_DISK_pipe_close (h->server_stdout);
1349 h->server_stdout = NULL;
1350 h->server_stdout_handle = NULL;
1352 if (NULL != h->server_stdout)
1354 GNUNET_DISK_pipe_close (h->server_stdout);
1355 h->server_stdout = NULL;
1356 h->server_stdout_handle = NULL;
1358 while (NULL != (lal = h->lal_head))
1360 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal);
1361 if (NULL != h->address_callback)
1362 h->address_callback (h->callback_cls, GNUNET_NO,
1363 (const struct sockaddr *) &lal[1], lal->addrlen);
1366 for (i = 0; i < h->num_local_addrs; i++)
1367 GNUNET_free (h->local_addrs[i]);
1368 GNUNET_free_non_null (h->local_addrs);
1369 GNUNET_free_non_null (h->local_addrlens);
1370 GNUNET_free_non_null (h->external_address);
1371 GNUNET_free_non_null (h->internal_address);
1377 * We learned about a peer (possibly behind NAT) so run the
1378 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1379 * that peer to connect to us (connection reversal).
1381 * @param h handle (used for configuration)
1382 * @param sa the address of the peer (IPv4-only)
1383 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
1384 * #GNUNET_OK otherwise
1387 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1388 const struct sockaddr_in *sa)
1392 char inet4[INET_ADDRSTRLEN];
1393 char port_as_string[6];
1394 struct GNUNET_OS_Process *proc;
1397 if (GNUNET_YES != h->enable_nat_client)
1398 return GNUNET_NO; /* not permitted / possible */
1400 if (h->internal_address == NULL)
1402 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
1403 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1404 return GNUNET_SYSERR;
1406 GNUNET_assert (sa->sin_family == AF_INET);
1407 if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN))
1409 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
1412 return GNUNET_SYSERR;
1414 GNUNET_snprintf (port_as_string,
1415 sizeof (port_as_string),
1418 LOG (GNUNET_ERROR_TYPE_DEBUG,
1419 _("Running gnunet-helper-nat-client %s %s %u\n"),
1420 h->internal_address,
1422 (unsigned int) h->adv_port);
1423 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
1425 GNUNET_OS_start_process (GNUNET_NO, 0, NULL, NULL,
1427 "gnunet-helper-nat-client",
1428 h->internal_address,
1429 inet4, port_as_string, NULL);
1430 GNUNET_free (binary);
1432 return GNUNET_SYSERR;
1433 /* we know that the gnunet-helper-nat-client will terminate virtually
1435 GNUNET_OS_process_wait (proc);
1436 GNUNET_OS_process_destroy (proc);
1442 * Test if the given address is (currently) a plausible IP address for this peer.
1444 * @param h the handle returned by register
1445 * @param addr IP address to test (IPv4 or IPv6)
1446 * @param addrlen number of bytes in @a addr
1447 * @return #GNUNET_YES if the address is plausible,
1448 * #GNUNET_NO if the address is not plausible,
1449 * #GNUNET_SYSERR if the address is malformed
1452 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1456 struct LocalAddressList *pos;
1457 const struct sockaddr_in *in4;
1458 const struct sockaddr_in6 *in6;
1460 if ((addrlen != sizeof (struct in_addr)) &&
1461 (addrlen != sizeof (struct in6_addr)))
1464 return GNUNET_SYSERR;
1466 for (pos = h->lal_head; NULL != pos; pos = pos->next)
1468 if (pos->addrlen == sizeof (struct sockaddr_in))
1470 in4 = (struct sockaddr_in *) &pos[1];
1471 if ((addrlen == sizeof (struct in_addr)) &&
1472 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))))
1475 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1477 in6 = (struct sockaddr_in6 *) &pos[1];
1478 if ((addrlen == sizeof (struct in6_addr)) &&
1479 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))))
1487 LOG (GNUNET_ERROR_TYPE_WARNING,
1488 "Asked to validate one of my addresses and validation failed!\n");