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 looking up our own hostname in DNS.
79 * Address was obtained by scanning our hosts's network interfaces
80 * and taking their address (no DNS involved).
82 LAL_INTERFACE_ADDRESS,
85 * Addresses we were explicitly bound to.
90 * Addresses from UPnP or PMP
102 * List of local addresses that we currently deem valid. Actual
103 * struct is followed by the 'struct sockaddr'. Note that the code
104 * intentionally makes no attempt to ensure that a particular address
105 * is only listed once (especially since it may come from different
106 * sources, and the source is an "internal" construct).
108 struct LocalAddressList
111 * This is a linked list.
113 struct LocalAddressList *next;
118 struct LocalAddressList *prev;
121 * Number of bytes of address that follow.
126 * Origin of the local address.
128 enum LocalAddressSource source;
133 * Handle for miniupnp-based NAT traversal actions.
139 * Doubly-linked list.
141 struct MiniList *next;
144 * Doubly-linked list.
146 struct MiniList *prev;
149 * Handle to mini-action.
151 struct GNUNET_NAT_MiniHandle *mini;
154 * Local port number that was mapped.
162 * Handle for active NAT registrations.
164 struct GNUNET_NAT_Handle
168 * Configuration to use.
170 const struct GNUNET_CONFIGURATION_Handle *cfg;
173 * Function to call when we learn about a new address.
175 GNUNET_NAT_AddressCallback address_callback;
178 * Function to call when we notice another peer asking for
179 * connection reversal.
181 GNUNET_NAT_ReversalCallback reversal_callback;
184 * Closure for 'callback'.
189 * Handle for (DYN)DNS lookup of our external IP.
191 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
194 * Handle for request of hostname resolution, non-NULL if pending.
196 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
199 * stdout pipe handle for the gnunet-helper-nat-server process
201 struct GNUNET_DISK_PipeHandle *server_stdout;
204 * stdout file handle (for reading) for the gnunet-helper-nat-server process
206 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
209 * Linked list of currently valid addresses (head).
211 struct LocalAddressList *lal_head;
214 * Linked list of currently valid addresses (tail).
216 struct LocalAddressList *lal_tail;
219 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
221 struct GNUNET_TIME_Relative server_retry_delay;
224 * ID of select gnunet-helper-nat-server stdout read task
226 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
229 * ID of interface IP-scan task
231 GNUNET_SCHEDULER_TaskIdentifier ifc_task;
234 * ID of hostname DNS lookup task
236 GNUNET_SCHEDULER_TaskIdentifier hostname_task;
239 * ID of DynDNS lookup task
241 GNUNET_SCHEDULER_TaskIdentifier dns_task;
244 * ID of task to add addresses from bind.
246 GNUNET_SCHEDULER_TaskIdentifier bind_task;
249 * How often do we scan for changes in our IP address from our local
252 struct GNUNET_TIME_Relative ifc_scan_frequency;
255 * How often do we scan for changes in how our hostname resolves?
257 struct GNUNET_TIME_Relative hostname_dns_frequency;
260 * How often do we scan for changes in how our external (dyndns) hostname resolves?
262 struct GNUNET_TIME_Relative dyndns_frequency;
265 * The process id of the server process (if behind NAT)
267 struct GNUNET_OS_Process *server_proc;
270 * LAN address as passed by the caller (array).
272 struct sockaddr **local_addrs;
275 * Length of the 'local_addrs'.
277 socklen_t *local_addrlens;
280 * List of handles for UPnP-traversal, one per local port (if
283 struct MiniList *mini_head;
286 * List of handles for UPnP-traversal, one per local port (if
289 struct MiniList *mini_tail;
292 * Number of entries in 'local_addrs' array.
294 unsigned int num_local_addrs;
297 * Our external address (according to config, UPnP may disagree...),
298 * in dotted decimal notation, IPv4-only. Or NULL if not known.
300 char *external_address;
303 * Presumably our internal address (according to config)
305 char *internal_address;
308 * Is this transport configured to be behind a NAT?
313 * Has the NAT been punched? (according to config)
318 * Is this transport configured to allow connections to NAT'd peers?
320 int enable_nat_client;
323 * Should we run the gnunet-helper-nat-server?
325 int enable_nat_server;
328 * Are we allowed to try UPnP/PMP for NAT traversal?
333 * Should we use local addresses (loopback)? (according to config)
335 int use_localaddresses;
338 * Should we return local addresses to clients
340 int return_localaddress;
343 * Should we do a DNS lookup of our hostname to find out our own IP?
348 * Is using IPv6 disabled?
353 * Is this TCP or UDP?
358 * Port we advertise to the outside.
366 * Try to start the gnunet-helper-nat-server (if it is not
369 * @param h handle to NAT
372 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
376 * Remove all addresses from the list of 'local' addresses
377 * that originated from the given source.
379 * @param h handle to NAT
380 * @param src source that identifies addresses to remove
383 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
384 enum LocalAddressSource src)
386 struct LocalAddressList *pos;
387 struct LocalAddressList *next;
390 while (NULL != (pos = next))
393 if (pos->source != src)
395 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
396 if (NULL != h->address_callback)
397 h->address_callback (h->callback_cls, GNUNET_NO,
398 (const struct sockaddr *) &pos[1], pos->addrlen);
405 * Add the given address to the list of 'local' addresses, thereby
406 * making it a 'legal' address for this peer to have.
408 * @param h handle to NAT
409 * @param src where did the local address originate from?
410 * @param arg the address, some 'struct sockaddr'
411 * @param arg_size number of bytes in arg
414 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
415 enum LocalAddressSource src,
416 const struct sockaddr *arg, socklen_t arg_size)
418 struct LocalAddressList *lal;
420 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
421 memcpy (&lal[1], arg, arg_size);
422 lal->addrlen = arg_size;
424 GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal);
425 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding address `%s' from source %d\n",
426 GNUNET_a2s (arg, arg_size), src);
427 if (NULL != h->address_callback)
428 h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size);
433 * Add the given address to the list of 'local' addresses, thereby
434 * making it a 'legal' address for this peer to have. Set the
435 * port number in the process to the advertised port and possibly
436 * also to zero (if we have the gnunet-helper-nat-server).
438 * @param h handle to NAT
439 * @param src where did the local address originate from?
440 * @param arg the address, some 'struct sockaddr'
441 * @param arg_size number of bytes in arg
444 add_to_address_list (struct GNUNET_NAT_Handle *h, enum LocalAddressSource src,
445 const struct sockaddr *arg, socklen_t arg_size)
447 struct sockaddr_in s4;
448 const struct sockaddr_in *in4;
449 struct sockaddr_in6 s6;
450 const struct sockaddr_in6 *in6;
452 if (arg_size == sizeof (struct sockaddr_in))
454 in4 = (const struct sockaddr_in *) arg;
456 s4.sin_port = htons (h->adv_port);
457 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
458 sizeof (struct sockaddr_in));
459 if (GNUNET_YES == h->enable_nat_server)
461 /* also add with PORT = 0 to indicate NAT server is enabled */
462 s4.sin_port = htons (0);
463 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
464 sizeof (struct sockaddr_in));
467 else if (arg_size == sizeof (struct sockaddr_in6))
469 if (GNUNET_YES != h->disable_ipv6)
471 in6 = (const struct sockaddr_in6 *) arg;
473 s6.sin6_port = htons (h->adv_port);
474 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s6,
475 sizeof (struct sockaddr_in6));
486 * Add the given IP address to the list of 'local' addresses, thereby
487 * making it a 'legal' address for this peer to have.
489 * @param h handle to NAT
490 * @param src where did the local address originate from?
491 * @param addr the address, some 'struct in_addr' or 'struct in6_addr'
492 * @param addrlen number of bytes in addr
495 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
496 enum LocalAddressSource src, const void *addr,
499 struct sockaddr_in s4;
500 const struct in_addr *in4;
501 struct sockaddr_in6 s6;
502 const struct in6_addr *in6;
504 if (addrlen == sizeof (struct in_addr))
506 in4 = (const struct in_addr *) addr;
507 memset (&s4, 0, sizeof (s4));
508 s4.sin_family = AF_INET;
510 #if HAVE_SOCKADDR_IN_SIN_LEN
511 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
514 add_to_address_list (h, src, (const struct sockaddr *) &s4,
515 sizeof (struct sockaddr_in));
516 if (GNUNET_YES == h->enable_nat_server)
518 /* also add with PORT = 0 to indicate NAT server is enabled */
519 s4.sin_port = htons (0);
520 add_to_address_list (h, src, (const struct sockaddr *) &s4,
521 sizeof (struct sockaddr_in));
525 else if (addrlen == sizeof (struct in6_addr))
527 if (GNUNET_YES != h->disable_ipv6)
529 in6 = (const struct in6_addr *) addr;
530 memset (&s6, 0, sizeof (s6));
531 s6.sin6_family = AF_INET6;
532 s6.sin6_port = htons (h->adv_port);
533 #if HAVE_SOCKADDR_IN_SIN_LEN
534 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
537 add_to_address_list (h, src, (const struct sockaddr *) &s6,
538 sizeof (struct sockaddr_in6));
549 * Task to do DNS lookup on our external hostname to
550 * get DynDNS-IP addresses.
552 * @param cls the NAT handle
553 * @param tc scheduler context
556 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
560 * Our (external) hostname was resolved and the configuration says that
561 * the NAT was hole-punched.
563 * @param cls the 'struct Plugin'
564 * @param addr NULL on error, otherwise result of DNS lookup
565 * @param addrlen number of bytes in addr
568 process_external_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
570 struct GNUNET_NAT_Handle *h = cls;
571 struct in_addr dummy;
576 if (1 == inet_pton (AF_INET, h->external_address, &dummy))
577 return; /* repated lookup pointless: was numeric! */
579 GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency, &resolve_dns, h);
582 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
587 * Task to do a lookup on our hostname for IP addresses.
589 * @param cls the NAT handle
590 * @param tc scheduler context
593 resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
597 * Function called by the resolver for each address obtained from DNS
598 * for our own hostname. Add the addresses to the list of our IP
602 * @param addr one of the addresses of the host, NULL for the last address
603 * @param addrlen length of the address
606 process_hostname_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
608 struct GNUNET_NAT_Handle *h = cls;
612 h->hostname_dns = NULL;
614 GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
615 &resolve_hostname, h);
618 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
623 * Add the IP of our network interface to the list of
626 * @param cls the 'struct GNUNET_NAT_Handle'
627 * @param name name of the interface
628 * @param isDefault do we think this may be our default interface
629 * @param addr address of the interface
630 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
631 * @param netmask the network mask (can be NULL for unknown or unassigned))
632 * @param addrlen number of bytes in addr
633 * @return GNUNET_OK to continue iterating
636 process_interfaces (void *cls, const char *name, int isDefault,
637 const struct sockaddr *addr,
638 const struct sockaddr *broadcast_addr,
639 const struct sockaddr *netmask, socklen_t addrlen)
641 struct GNUNET_NAT_Handle *h = cls;
642 const struct sockaddr_in *s4;
643 const struct sockaddr_in6 *s6;
645 char buf[INET6_ADDRSTRLEN];
647 switch (addr->sa_family)
650 s4 = (struct sockaddr_in *) addr;
653 /* Check if address is in 127.0.0.0/8 */
654 uint32_t address = ntohl ((uint32_t) (s4->sin_addr.s_addr));
655 uint32_t value = (address & 0xFF000000) ^ 0x7F000000;
657 if ((h->return_localaddress == GNUNET_NO) && (value == 0))
661 if (GNUNET_YES == h->use_localaddresses)
663 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s4->sin_addr,
664 sizeof (struct in_addr));
668 s6 = (struct sockaddr_in6 *) addr;
669 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
671 /* skip link local addresses */
674 if ((h->return_localaddress == GNUNET_NO) &&
675 (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr)))
680 if (GNUNET_YES == h->use_localaddresses)
682 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s6->sin6_addr,
683 sizeof (struct in6_addr));
690 if ((h->internal_address == NULL) && (h->server_proc == NULL) &&
691 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
692 (GNUNET_YES == isDefault) && ((addr->sa_family == AF_INET) ||
693 (addr->sa_family == AF_INET6)))
695 /* no internal address configured, but we found a "default"
696 * interface, try using that as our 'internal' address */
697 h->internal_address =
698 GNUNET_strdup (inet_ntop (addr->sa_family, ip, buf, sizeof (buf)));
699 start_gnunet_nat_server (h);
706 * Task that restarts the gnunet-helper-nat-server process after a crash
707 * after a certain delay.
709 * @param cls the 'struct GNUNET_NAT_Handle'
710 * @param tc scheduler context
713 restart_nat_server (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
715 struct GNUNET_NAT_Handle *h = cls;
717 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
718 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
720 start_gnunet_nat_server (h);
725 * We have been notified that gnunet-helper-nat-server has written something to stdout.
726 * Handle the output, then reschedule this function to be called again once
729 * @param cls the NAT handle
730 * @param tc the scheduling context
733 nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
735 struct GNUNET_NAT_Handle *h = cls;
740 const char *port_start;
741 struct sockaddr_in sin_addr;
743 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
744 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
746 memset (mybuf, 0, sizeof (mybuf));
748 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
751 LOG (GNUNET_ERROR_TYPE_DEBUG,
752 "Finished reading from server stdout with code: %d\n", bytes);
753 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
754 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
755 GNUNET_OS_process_wait (h->server_proc);
756 GNUNET_OS_process_destroy (h->server_proc);
757 h->server_proc = NULL;
758 GNUNET_DISK_pipe_close (h->server_stdout);
759 h->server_stdout = NULL;
760 h->server_stdout_handle = NULL;
761 /* now try to restart it */
762 h->server_retry_delay =
763 GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
764 h->server_retry_delay =
765 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
766 h->server_retry_delay);
767 h->server_read_task =
768 GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
769 &restart_nat_server, h);
774 for (i = 0; i < sizeof (mybuf); i++)
776 if (mybuf[i] == '\n')
781 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
784 port_start = &mybuf[i + 1];
788 /* construct socket address of sender */
789 memset (&sin_addr, 0, sizeof (sin_addr));
790 sin_addr.sin_family = AF_INET;
791 #if HAVE_SOCKADDR_IN_SIN_LEN
792 sin_addr.sin_len = sizeof (sin_addr);
794 if ((NULL == port_start) || (1 != SSCANF (port_start, "%d", &port)) ||
795 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
797 /* should we restart gnunet-helper-nat-server? */
798 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
799 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
801 h->server_read_task =
802 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
803 h->server_stdout_handle,
804 &nat_server_read, h);
807 sin_addr.sin_port = htons ((uint16_t) port);
808 LOG (GNUNET_ERROR_TYPE_DEBUG, "gnunet-helper-nat-server read: %s:%d\n", mybuf,
810 h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr,
812 h->server_read_task =
813 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
814 h->server_stdout_handle, &nat_server_read,
820 * Try to start the gnunet-helper-nat-server (if it is not
823 * @param h handle to NAT
826 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
828 if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) &&
829 (h->internal_address != NULL) &&
832 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES))))
834 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting `%s' at `%s'\n",
835 "gnunet-helper-nat-server", h->internal_address);
836 /* Start the server process */
838 GNUNET_OS_start_process (GNUNET_NO, NULL, h->server_stdout,
839 "gnunet-helper-nat-server",
840 "gnunet-helper-nat-server",
841 h->internal_address, NULL);
842 if (h->server_proc == NULL)
844 LOG (GNUNET_ERROR_TYPE_WARNING, "nat", _("Failed to start %s\n"),
845 "gnunet-helper-nat-server");
846 GNUNET_DISK_pipe_close (h->server_stdout);
847 h->server_stdout = NULL;
851 /* Close the write end of the read pipe */
852 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
853 h->server_stdout_handle =
854 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
855 h->server_read_task =
856 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
857 h->server_stdout_handle,
858 &nat_server_read, h);
865 * Task to scan the local network interfaces for IP addresses.
867 * @param cls the NAT handle
868 * @param tc scheduler context
871 list_interfaces (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
873 struct GNUNET_NAT_Handle *h = cls;
875 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
876 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
877 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
879 GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency, &list_interfaces, h);
884 * Task to do a lookup on our hostname for IP addresses.
886 * @param cls the NAT handle
887 * @param tc scheduler context
890 resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
892 struct GNUNET_NAT_Handle *h = cls;
894 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
895 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
897 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT,
898 &process_hostname_ip, h);
903 * Task to do DNS lookup on our external hostname to
904 * get DynDNS-IP addresses.
906 * @param cls the NAT handle
907 * @param tc scheduler context
910 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
912 struct GNUNET_NAT_Handle *h = cls;
914 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
915 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
917 GNUNET_RESOLVER_ip_get (h->external_address, AF_INET,
918 GNUNET_TIME_UNIT_MINUTES, &process_external_ip,
924 * Add or remove UPnP-mapped addresses.
926 * @param cls the GNUNET_NAT_Handle
927 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
928 * the previous (now invalid) one
929 * @param addr either the previous or the new public IP address
930 * @param addrlen actual lenght of the address
933 upnp_add (void *cls, int add_remove, const struct sockaddr *addr,
936 struct GNUNET_NAT_Handle *h = cls;
937 struct LocalAddressList *pos;
938 struct LocalAddressList *next;
940 if (GNUNET_YES == add_remove)
942 add_to_address_list (h, LAL_UPNP, addr, addrlen);
947 while (NULL != (pos = next))
950 if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) ||
951 (0 != memcmp (&pos[1], addr, addrlen)))
953 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
954 if (NULL != h->address_callback)
955 h->address_callback (h->callback_cls, GNUNET_NO,
956 (const struct sockaddr *) &pos[1], pos->addrlen);
958 return; /* only remove once */
960 /* asked to remove address that does not exist */
966 * Try to add a port mapping using UPnP.
968 * @param h overall NAT handle
969 * @param port port to map with UPnP
972 add_minis (struct GNUNET_NAT_Handle *h, uint16_t port)
979 if (port == ml->port)
980 return; /* already got this port */
983 ml = GNUNET_malloc (sizeof (struct MiniList));
985 ml->mini = GNUNET_NAT_mini_map_start (port, h->is_tcp, &upnp_add, h);
986 GNUNET_CONTAINER_DLL_insert (h->mini_head, h->mini_tail, ml);
991 * Task to add addresses from original bind to set of valid addrs.
993 * @param cls the NAT handle
994 * @param tc scheduler context
997 add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
999 static struct in6_addr any = IN6ADDR_ANY_INIT;
1000 struct GNUNET_NAT_Handle *h = cls;
1002 struct sockaddr *sa;
1003 const struct sockaddr_in *v4;
1005 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1006 for (i = 0; i < h->num_local_addrs; i++)
1008 sa = h->local_addrs[i];
1009 switch (sa->sa_family)
1012 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1017 v4 = (const struct sockaddr_in *) sa;
1018 if (0 != v4->sin_addr.s_addr)
1019 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1020 sizeof (struct sockaddr_in));
1022 add_minis (h, ntohs (v4->sin_port));
1025 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1031 memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, &any,
1032 sizeof (struct in6_addr)))
1033 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1034 sizeof (struct sockaddr_in6));
1045 * Attempt to enable port redirection and detect public IP address contacting
1046 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1047 * of the local host's addresses should the external port be mapped. The port
1048 * is taken from the corresponding sockaddr_in[6] field.
1050 * @param cfg configuration to use
1051 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1052 * @param adv_port advertised port (port we are either bound to or that our OS
1053 * locally performs redirection from to our bound port).
1054 * @param num_addrs number of addresses in 'addrs'
1055 * @param addrs the local addresses packets should be redirected to
1056 * @param addrlens actual lengths of the addresses
1057 * @param address_callback function to call everytime the public IP address changes
1058 * @param reversal_callback function to call if someone wants connection reversal from us
1059 * @param callback_cls closure for callbacks
1060 * @return NULL on error, otherwise handle that can be used to unregister
1062 struct GNUNET_NAT_Handle *
1063 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, int is_tcp,
1064 uint16_t adv_port, unsigned int num_addrs,
1065 const struct sockaddr **addrs, const socklen_t * addrlens,
1066 GNUNET_NAT_AddressCallback address_callback,
1067 GNUNET_NAT_ReversalCallback reversal_callback,
1070 struct GNUNET_NAT_Handle *h;
1071 struct in_addr in_addr;
1074 LOG (GNUNET_ERROR_TYPE_DEBUG,
1075 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1076 (unsigned int) adv_port, num_addrs);
1077 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1078 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1081 h->address_callback = address_callback;
1082 h->reversal_callback = reversal_callback;
1083 h->callback_cls = callback_cls;
1084 h->num_local_addrs = num_addrs;
1085 h->adv_port = adv_port;
1088 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr *));
1089 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1090 for (i = 0; i < num_addrs; i++)
1092 GNUNET_assert (addrlens[i] > 0);
1093 GNUNET_assert (addrs[i] != NULL);
1094 h->local_addrlens[i] = addrlens[i];
1095 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1096 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1099 h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1101 GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1103 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1105 &h->internal_address);
1107 if ((h->internal_address != NULL) &&
1108 (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1110 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
1111 _("Malformed %s `%s' given in configuration!\n"), "INTERNAL_ADDRESS",
1112 h->internal_address);
1113 GNUNET_free (h->internal_address);
1114 h->internal_address = NULL;
1118 GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1120 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1122 &h->external_address);
1125 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT");
1127 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT");
1128 h->enable_nat_client =
1129 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_CLIENT");
1130 h->enable_nat_server =
1131 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_ICMP_SERVER");
1133 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP");
1134 h->use_localaddresses =
1135 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR");
1136 h->return_localaddress =
1137 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat",
1138 "RETURN_LOCAL_ADDRESSES");
1141 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME");
1143 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6");
1145 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY",
1146 &h->dyndns_frequency))
1147 h->dyndns_frequency = DYNDNS_FREQUENCY;
1149 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY",
1150 &h->ifc_scan_frequency))
1151 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1153 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY",
1154 &h->hostname_dns_frequency))
1155 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1157 if (NULL == reversal_callback)
1158 h->enable_nat_server = GNUNET_NO;
1160 /* Check if NAT was hole-punched */
1161 if ((NULL != h->address_callback) && (h->external_address != NULL) &&
1162 (h->nat_punched == GNUNET_YES))
1164 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1165 h->enable_nat_server = GNUNET_NO;
1166 h->enable_upnp = GNUNET_NO;
1169 /* Test for SUID binaries */
1170 if ((h->behind_nat == GNUNET_YES) && (GNUNET_YES == h->enable_nat_server) &&
1172 GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server")))
1174 h->enable_nat_server = GNUNET_NO;
1175 LOG (GNUNET_ERROR_TYPE_WARNING,
1177 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1178 "gnunet-helper-nat-server");
1180 if ((GNUNET_YES == h->enable_nat_client) &&
1182 GNUNET_OS_check_helper_binary ("gnunet-helper-nat-client")))
1184 h->enable_nat_client = GNUNET_NO;
1185 LOG (GNUNET_ERROR_TYPE_WARNING,
1187 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1188 "gnunet-helper-nat-client");
1191 start_gnunet_nat_server (h);
1193 /* FIXME: add support for UPnP, etc */
1195 if (NULL != h->address_callback)
1197 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1198 if (GNUNET_YES == h->use_hostname)
1199 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1207 * Stop port redirection and public IP address detection for the given handle.
1208 * This frees the handle, after having sent the needed commands to close open ports.
1210 * @param h the handle to stop
1213 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1216 struct LocalAddressList *lal;
1217 struct MiniList *ml;
1219 while (NULL != (ml = h->mini_head))
1221 GNUNET_CONTAINER_DLL_remove (h->mini_head, h->mini_tail, ml);
1222 if (NULL != ml->mini)
1223 GNUNET_NAT_mini_map_stop (ml->mini);
1226 if (h->ext_dns != NULL)
1228 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1231 if (NULL != h->hostname_dns)
1233 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1234 h->hostname_dns = NULL;
1236 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1238 GNUNET_SCHEDULER_cancel (h->server_read_task);
1239 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1241 if (GNUNET_SCHEDULER_NO_TASK != h->bind_task)
1243 GNUNET_SCHEDULER_cancel (h->bind_task);
1244 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1246 if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1248 GNUNET_SCHEDULER_cancel (h->ifc_task);
1249 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1251 if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1253 GNUNET_SCHEDULER_cancel (h->hostname_task);
1254 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1256 if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1258 GNUNET_SCHEDULER_cancel (h->dns_task);
1259 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1261 if (NULL != h->server_proc)
1263 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1264 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
1265 GNUNET_OS_process_wait (h->server_proc);
1266 GNUNET_OS_process_destroy (h->server_proc);
1267 h->server_proc = NULL;
1268 GNUNET_DISK_pipe_close (h->server_stdout);
1269 h->server_stdout = NULL;
1270 h->server_stdout_handle = NULL;
1272 if (NULL != h->server_stdout)
1274 GNUNET_DISK_pipe_close (h->server_stdout);
1275 h->server_stdout = NULL;
1276 h->server_stdout_handle = NULL;
1278 while (NULL != (lal = h->lal_head))
1280 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal);
1281 if (NULL != h->address_callback)
1282 h->address_callback (h->callback_cls, GNUNET_NO,
1283 (const struct sockaddr *) &lal[1], lal->addrlen);
1286 for (i = 0; i < h->num_local_addrs; i++)
1287 GNUNET_free (h->local_addrs[i]);
1288 GNUNET_free_non_null (h->local_addrs);
1289 GNUNET_free_non_null (h->local_addrlens);
1290 GNUNET_free_non_null (h->external_address);
1291 GNUNET_free_non_null (h->internal_address);
1297 * We learned about a peer (possibly behind NAT) so run the
1298 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1299 * that peer to connect to us (connection reversal).
1301 * @return GNUNET_SYSERR on error, GNUNET_NO if nat client is disabled,
1302 * GNUNET_OK otherwise
1305 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1306 const struct sockaddr_in *sa)
1310 char inet4[INET_ADDRSTRLEN];
1311 char port_as_string[6];
1312 struct GNUNET_OS_Process *proc;
1314 if (GNUNET_YES != h->enable_nat_client)
1315 return GNUNET_NO; /* not permitted / possible */
1317 if (h->internal_address == NULL)
1319 LOG (GNUNET_ERROR_TYPE_WARNING, "nat",
1321 ("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1322 return GNUNET_SYSERR;
1324 GNUNET_assert (sa->sin_family == AF_INET);
1325 if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN))
1327 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "inet_ntop");
1328 return GNUNET_SYSERR;
1330 GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port);
1331 LOG (GNUNET_ERROR_TYPE_DEBUG,
1332 _("Running gnunet-helper-nat-client %s %s %u\n"), h->internal_address,
1333 inet4, (unsigned int) h->adv_port);
1335 GNUNET_OS_start_process (GNUNET_NO,
1336 NULL, NULL, "gnunet-helper-nat-client",
1337 "gnunet-helper-nat-client", h->internal_address,
1338 inet4, port_as_string, NULL);
1340 return GNUNET_SYSERR;
1341 /* we know that the gnunet-helper-nat-client will terminate virtually
1343 GNUNET_OS_process_wait (proc);
1344 GNUNET_OS_process_destroy (proc);
1350 * Test if the given address is (currently) a plausible IP address for this peer.
1352 * @param h the handle returned by register
1353 * @param addr IP address to test (IPv4 or IPv6)
1354 * @param addrlen number of bytes in addr
1355 * @return GNUNET_YES if the address is plausible,
1356 * GNUNET_NO if the address is not plausible,
1357 * GNUNET_SYSERR if the address is malformed
1360 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, const void *addr,
1363 struct LocalAddressList *pos;
1364 const struct sockaddr_in *in4;
1365 const struct sockaddr_in6 *in6;
1367 if ((addrlen != sizeof (struct in_addr)) &&
1368 (addrlen != sizeof (struct in6_addr)))
1371 return GNUNET_SYSERR;
1376 if (pos->addrlen == sizeof (struct sockaddr_in))
1378 in4 = (struct sockaddr_in *) &pos[1];
1379 if ((addrlen == sizeof (struct in_addr)) &&
1380 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))))
1383 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1385 in6 = (struct sockaddr_in6 *) &pos[1];
1386 if ((addrlen == sizeof (struct in6_addr)) &&
1387 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))))
1396 LOG (GNUNET_ERROR_TYPE_WARNING,
1397 "Asked to validate one of my addresses and validation failed!\n");