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"
33 #define DEBUG_NAT GNUNET_NO
36 * How often do we scan for changes in our IP address from our local
39 #define IFC_SCAN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
42 * How often do we scan for changes in how our hostname resolves?
44 #define HOSTNAME_DNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 20)
48 * How often do we scan for changes in how our external (dyndns) hostname resolves?
50 #define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 7)
53 * How long until we give up trying to resolve our own hostname?
55 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
59 * Where did the given local address originate from?
60 * To be used for debugging as well as in the future
61 * to remove all addresses from a certain source when
62 * we reevaluate the source.
64 enum LocalAddressSource
67 * Address was obtained by DNS resolution of the external hostname
68 * given in the configuration (i.e. hole-punched DynDNS setup).
73 * Address was obtained by looking up our own hostname in DNS.
78 * Address was obtained by scanning our hosts's network interfaces
79 * and taking their address (no DNS involved).
81 LAL_INTERFACE_ADDRESS,
84 * Addresses we were explicitly bound to.
89 * Addresses from UPnP or PMP
101 * List of local addresses that we currently deem valid. Actual
102 * struct is followed by the 'struct sockaddr'. Note that the code
103 * intentionally makes no attempt to ensure that a particular address
104 * is only listed once (especially since it may come from different
105 * sources, and the source is an "internal" construct).
107 struct LocalAddressList
110 * This is a linked list.
112 struct LocalAddressList *next;
117 struct LocalAddressList *prev;
120 * Number of bytes of address that follow.
125 * Origin of the local address.
127 enum LocalAddressSource source;
132 * Handle for miniupnp-based NAT traversal actions.
138 * Doubly-linked list.
140 struct MiniList *next;
143 * Doubly-linked list.
145 struct MiniList *prev;
148 * Handle to mini-action.
150 struct GNUNET_NAT_MiniHandle *mini;
153 * Local port number that was mapped.
161 * Handle for active NAT registrations.
163 struct GNUNET_NAT_Handle
167 * Configuration to use.
169 const struct GNUNET_CONFIGURATION_Handle *cfg;
172 * Function to call when we learn about a new address.
174 GNUNET_NAT_AddressCallback address_callback;
177 * Function to call when we notice another peer asking for
178 * connection reversal.
180 GNUNET_NAT_ReversalCallback reversal_callback;
183 * Closure for 'callback'.
188 * Handle for (DYN)DNS lookup of our external IP.
190 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
193 * Handle for request of hostname resolution, non-NULL if pending.
195 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
198 * stdout pipe handle for the gnunet-helper-nat-server process
200 struct GNUNET_DISK_PipeHandle *server_stdout;
203 * stdout file handle (for reading) for the gnunet-helper-nat-server process
205 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
208 * Linked list of currently valid addresses (head).
210 struct LocalAddressList *lal_head;
213 * Linked list of currently valid addresses (tail).
215 struct LocalAddressList *lal_tail;
218 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
220 struct GNUNET_TIME_Relative server_retry_delay;
223 * ID of select gnunet-helper-nat-server stdout read task
225 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
228 * ID of interface IP-scan task
230 GNUNET_SCHEDULER_TaskIdentifier ifc_task;
233 * ID of hostname DNS lookup task
235 GNUNET_SCHEDULER_TaskIdentifier hostname_task;
238 * ID of DynDNS lookup task
240 GNUNET_SCHEDULER_TaskIdentifier dns_task;
243 * ID of task to add addresses from bind.
245 GNUNET_SCHEDULER_TaskIdentifier bind_task;
248 * How often do we scan for changes in our IP address from our local
251 struct GNUNET_TIME_Relative ifc_scan_frequency;
254 * How often do we scan for changes in how our hostname resolves?
256 struct GNUNET_TIME_Relative hostname_dns_frequency;
259 * How often do we scan for changes in how our external (dyndns) hostname resolves?
261 struct GNUNET_TIME_Relative dyndns_frequency;
264 * The process id of the server process (if behind NAT)
266 struct GNUNET_OS_Process *server_proc;
269 * LAN address as passed by the caller (array).
271 struct sockaddr **local_addrs;
274 * Length of the 'local_addrs'.
276 socklen_t *local_addrlens;
279 * List of handles for UPnP-traversal, one per local port (if
282 struct MiniList *mini_head;
285 * List of handles for UPnP-traversal, one per local port (if
288 struct MiniList *mini_tail;
291 * Number of entries in 'local_addrs' array.
293 unsigned int num_local_addrs;
296 * The our external address (according to config, UPnP may disagree...)
298 char *external_address;
301 * Presumably our internal address (according to config)
303 char *internal_address;
306 * Is this transport configured to be behind a NAT?
311 * Has the NAT been punched? (according to config)
316 * Is this transport configured to allow connections to NAT'd peers?
318 int enable_nat_client;
321 * Should we run the gnunet-helper-nat-server?
323 int enable_nat_server;
326 * Are we allowed to try UPnP/PMP for NAT traversal?
331 * Should we use local addresses (loopback)? (according to config)
333 int use_localaddresses;
336 * Should we do a DNS lookup of our hostname to find out our own IP?
341 * Is using IPv6 disabled?
346 * Is this TCP or UDP?
351 * Port we advertise to the outside.
359 * Try to start the gnunet-helper-nat-server (if it is not
362 * @param h handle to NAT
364 static void start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
368 * Remove all addresses from the list of 'local' addresses
369 * that originated from the given source.
371 * @param plugin the plugin
372 * @param src source that identifies addresses to remove
375 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
376 enum LocalAddressSource src)
378 struct LocalAddressList *pos;
379 struct LocalAddressList *next;
382 while (NULL != (pos = next))
385 if (pos->source != src)
387 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
388 if (NULL != h->address_callback)
389 h->address_callback (h->callback_cls, GNUNET_NO,
390 (const struct sockaddr *) &pos[1], pos->addrlen);
397 * Add the given address to the list of 'local' addresses, thereby
398 * making it a 'legal' address for this peer to have.
400 * @param plugin the plugin
401 * @param src where did the local address originate from?
402 * @param arg the address, some 'struct sockaddr'
403 * @param arg_size number of bytes in arg
406 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
407 enum LocalAddressSource src,
408 const struct sockaddr *arg, socklen_t arg_size)
410 struct LocalAddressList *lal;
412 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
413 memcpy (&lal[1], arg, arg_size);
414 lal->addrlen = arg_size;
416 GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal);
418 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
419 "Adding address `%s' from source %d\n", GNUNET_a2s (arg,
423 if (NULL != h->address_callback)
424 h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size);
429 * Add the given address to the list of 'local' addresses, thereby
430 * making it a 'legal' address for this peer to have. Set the
431 * port number in the process to the advertised port and possibly
432 * also to zero (if we have the gnunet-helper-nat-server).
434 * @param plugin the plugin
435 * @param src where did the local address originate from?
436 * @param arg the address, some 'struct sockaddr'
437 * @param arg_size number of bytes in arg
440 add_to_address_list (struct GNUNET_NAT_Handle *h, enum LocalAddressSource src,
441 const struct sockaddr *arg, socklen_t arg_size)
443 struct sockaddr_in s4;
444 const struct sockaddr_in *in4;
445 struct sockaddr_in6 s6;
446 const struct sockaddr_in6 *in6;
448 if (arg_size == sizeof (struct sockaddr_in))
450 in4 = (const struct sockaddr_in *) arg;
452 s4.sin_port = htons (h->adv_port);
453 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
454 sizeof (struct sockaddr_in));
455 if (GNUNET_YES == h->enable_nat_server)
457 /* also add with PORT = 0 to indicate NAT server is enabled */
458 s4.sin_port = htons (0);
459 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4,
460 sizeof (struct sockaddr_in));
463 else if (arg_size == sizeof (struct sockaddr_in6))
465 if (GNUNET_YES != h->disable_ipv6)
467 in6 = (const struct sockaddr_in6 *) arg;
469 s6.sin6_port = htons (h->adv_port);
470 add_to_address_list_as_is (h, src, (const struct sockaddr *) &s6,
471 sizeof (struct sockaddr_in6));
482 * Add the given IP address to the list of 'local' addresses, thereby
483 * making it a 'legal' address for this peer to have.
485 * @param plugin the plugin
486 * @param src where did the local address originate from?
487 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
488 * @param arg_size number of bytes in arg
491 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
492 enum LocalAddressSource src, const void *addr,
495 struct sockaddr_in s4;
496 const struct in_addr *in4;
497 struct sockaddr_in6 s6;
498 const struct in6_addr *in6;
500 if (addrlen == sizeof (struct in_addr))
502 in4 = (const struct in_addr *) addr;
503 memset (&s4, 0, sizeof (s4));
504 s4.sin_family = AF_INET;
506 #if HAVE_SOCKADDR_IN_SIN_LEN
507 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
510 add_to_address_list (h, src, (const struct sockaddr *) &s4,
511 sizeof (struct sockaddr_in));
512 if (GNUNET_YES == h->enable_nat_server)
514 /* also add with PORT = 0 to indicate NAT server is enabled */
515 s4.sin_port = htons (0);
516 add_to_address_list (h, src, (const struct sockaddr *) &s4,
517 sizeof (struct sockaddr_in));
521 else if (addrlen == sizeof (struct in6_addr))
523 if (GNUNET_YES != h->disable_ipv6)
525 in6 = (const struct in6_addr *) addr;
526 memset (&s6, 0, sizeof (s6));
527 s6.sin6_family = AF_INET6;
528 s6.sin6_port = htons (h->adv_port);
529 #if HAVE_SOCKADDR_IN_SIN_LEN
530 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
533 add_to_address_list (h, src, (const struct sockaddr *) &s6,
534 sizeof (struct sockaddr_in6));
545 * Task to do DNS lookup on our external hostname to
546 * get DynDNS-IP addresses.
548 * @param cls the NAT handle
549 * @param tc scheduler context
551 static void resolve_dns (void *cls,
552 const struct GNUNET_SCHEDULER_TaskContext *tc);
556 * Our (external) hostname was resolved and the configuration says that
557 * the NAT was hole-punched.
559 * @param cls the 'struct Plugin'
560 * @param addr NULL on error, otherwise result of DNS lookup
561 * @param addrlen number of bytes in addr
564 process_external_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
566 struct GNUNET_NAT_Handle *h = cls;
567 struct in_addr dummy;
572 if (1 == inet_pton (AF_INET, h->external_address, &dummy))
573 return; /* repated lookup pointless: was numeric! */
575 GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency, &resolve_dns, h);
578 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
583 * Task to do a lookup on our hostname for IP addresses.
585 * @param cls the NAT handle
586 * @param tc scheduler context
588 static void resolve_hostname (void *cls,
589 const struct GNUNET_SCHEDULER_TaskContext *tc);
593 * Function called by the resolver for each address obtained from DNS
594 * for our own hostname. Add the addresses to the list of our IP
598 * @param addr one of the addresses of the host, NULL for the last address
599 * @param addrlen length of the address
602 process_hostname_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
604 struct GNUNET_NAT_Handle *h = cls;
608 h->hostname_dns = NULL;
610 GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
611 &resolve_hostname, h);
614 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
619 * Add the IP of our network interface to the list of
622 * @param cls the 'struct GNUNET_NAT_Handle'
623 * @param name name of the interface
624 * @param isDefault do we think this may be our default interface
625 * @param addr address of the interface
626 * @param addrlen number of bytes in addr
627 * @return GNUNET_OK to continue iterating
630 process_interfaces (void *cls, const char *name, int isDefault,
631 const struct sockaddr *addr, socklen_t addrlen)
633 struct GNUNET_NAT_Handle *h = cls;
634 const struct sockaddr_in *s4;
635 const struct sockaddr_in6 *s6;
637 char buf[INET6_ADDRSTRLEN];
639 switch (addr->sa_family)
642 s4 = (struct sockaddr_in *) addr;
644 if (GNUNET_YES == h->use_localaddresses)
645 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s4->sin_addr,
646 sizeof (struct in_addr));
649 s6 = (struct sockaddr_in6 *) addr;
650 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
652 /* skip link local addresses */
656 if (GNUNET_YES == h->use_localaddresses)
657 add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s6->sin6_addr,
658 sizeof (struct in6_addr));
664 if ((h->internal_address == NULL) && (h->server_proc == NULL) &&
665 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
666 (GNUNET_YES == isDefault) && ((addr->sa_family == AF_INET) ||
667 (addr->sa_family == AF_INET6)))
669 /* no internal address configured, but we found a "default"
670 * interface, try using that as our 'internal' address */
671 h->internal_address =
672 GNUNET_strdup (inet_ntop (addr->sa_family, ip, buf, sizeof (buf)));
673 start_gnunet_nat_server (h);
680 * Task that restarts the gnunet-helper-nat-server process after a crash
681 * after a certain delay.
683 * @param cls the 'struct GNUNET_NAT_Handle'
684 * @param tc scheduler context
687 restart_nat_server (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
689 struct GNUNET_NAT_Handle *h = cls;
691 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
692 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
694 start_gnunet_nat_server (h);
699 * We have been notified that gnunet-helper-nat-server has written something to stdout.
700 * Handle the output, then reschedule this function to be called again once
703 * @param cls the NAT handle
704 * @param tc the scheduling context
707 nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
709 struct GNUNET_NAT_Handle *h = cls;
714 const char *port_start;
715 struct sockaddr_in sin_addr;
717 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
718 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
720 memset (mybuf, 0, sizeof (mybuf));
722 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
726 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
727 "Finished reading from server stdout with code: %d\n",
730 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
731 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
732 GNUNET_OS_process_wait (h->server_proc);
733 GNUNET_OS_process_close (h->server_proc);
734 h->server_proc = NULL;
735 GNUNET_DISK_pipe_close (h->server_stdout);
736 h->server_stdout = NULL;
737 h->server_stdout_handle = NULL;
738 /* now try to restart it */
739 h->server_retry_delay =
740 GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
741 h->server_retry_delay =
742 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
743 h->server_retry_delay);
744 h->server_read_task =
745 GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
746 &restart_nat_server, h);
751 for (i = 0; i < sizeof (mybuf); i++)
753 if (mybuf[i] == '\n')
758 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
761 port_start = &mybuf[i + 1];
765 /* construct socket address of sender */
766 memset (&sin_addr, 0, sizeof (sin_addr));
767 sin_addr.sin_family = AF_INET;
768 #if HAVE_SOCKADDR_IN_SIN_LEN
769 sin_addr.sin_len = sizeof (sin_addr);
771 if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) ||
772 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
774 /* should we restart gnunet-helper-nat-server? */
775 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
777 ("gnunet-helper-nat-server generated malformed address `%s'\n"),
779 h->server_read_task =
780 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
781 h->server_stdout_handle,
782 &nat_server_read, h);
785 sin_addr.sin_port = htons ((uint16_t) port);
787 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
788 "gnunet-helper-nat-server read: %s:%d\n", mybuf, port);
790 h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr,
792 h->server_read_task =
793 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
794 h->server_stdout_handle, &nat_server_read,
800 * Try to start the gnunet-helper-nat-server (if it is not
803 * @param h handle to NAT
806 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
808 if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) &&
809 (h->internal_address != NULL) &&
812 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES))))
815 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat" "Starting %s at `%s'\n",
816 "gnunet-helper-nat-server", h->internal_address);
818 /* Start the server process */
820 GNUNET_OS_start_process (NULL, h->server_stdout,
821 "gnunet-helper-nat-server",
822 "gnunet-helper-nat-server",
823 h->internal_address, NULL);
824 if (h->server_proc == NULL)
826 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
827 _("Failed to start %s\n"), "gnunet-helper-nat-server");
828 GNUNET_DISK_pipe_close (h->server_stdout);
829 h->server_stdout = NULL;
833 /* Close the write end of the read pipe */
834 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
835 h->server_stdout_handle =
836 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
837 h->server_read_task =
838 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
839 h->server_stdout_handle,
840 &nat_server_read, h);
847 * Task to scan the local network interfaces for IP addresses.
849 * @param cls the NAT handle
850 * @param tc scheduler context
853 list_interfaces (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 struct GNUNET_NAT_Handle *h = cls;
857 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
858 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
859 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
861 GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency, &list_interfaces, h);
866 * Task to do a lookup on our hostname for IP addresses.
868 * @param cls the NAT handle
869 * @param tc scheduler context
872 resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct GNUNET_NAT_Handle *h = cls;
876 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
877 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
879 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT,
880 &process_hostname_ip, h);
885 * Task to do DNS lookup on our external hostname to
886 * get DynDNS-IP addresses.
888 * @param cls the NAT handle
889 * @param tc scheduler context
892 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
894 struct GNUNET_NAT_Handle *h = cls;
896 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
897 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
899 GNUNET_RESOLVER_ip_get (h->external_address, AF_INET,
900 GNUNET_TIME_UNIT_MINUTES, &process_external_ip,
906 * Add or remove UPnP-mapped addresses.
908 * @param cls the GNUNET_NAT_Handle
909 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
910 * the previous (now invalid) one
911 * @param addr either the previous or the new public IP address
912 * @param addrlen actual lenght of the address
915 upnp_add (void *cls, int add_remove, const struct sockaddr *addr,
918 struct GNUNET_NAT_Handle *h = cls;
919 struct LocalAddressList *pos;
920 struct LocalAddressList *next;
922 if (GNUNET_YES == add_remove)
924 add_to_address_list (h, LAL_UPNP, addr, addrlen);
929 while (NULL != (pos = next))
932 if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) ||
933 (0 != memcmp (&pos[1], addr, addrlen)))
935 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
936 if (NULL != h->address_callback)
937 h->address_callback (h->callback_cls, GNUNET_NO,
938 (const struct sockaddr *) &pos[1], pos->addrlen);
940 return; /* only remove once */
942 /* asked to remove address that does not exist */
948 * Try to add a port mapping using UPnP.
950 * @param h overall NAT handle
951 * @param port port to map with UPnP
954 add_minis (struct GNUNET_NAT_Handle *h, uint16_t port)
961 if (port == ml->port)
962 return; /* already got this port */
965 ml = GNUNET_malloc (sizeof (struct MiniList));
967 ml->mini = GNUNET_NAT_mini_map_start (port, h->is_tcp, &upnp_add, h);
968 GNUNET_CONTAINER_DLL_insert (h->mini_head, h->mini_tail, ml);
973 * Task to add addresses from original bind to set of valid addrs.
975 * @param cls the NAT handle
976 * @param tc scheduler context
979 add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
981 static struct in6_addr any = IN6ADDR_ANY_INIT;
982 struct GNUNET_NAT_Handle *h = cls;
985 const struct sockaddr_in *v4;
987 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
988 for (i = 0; i < h->num_local_addrs; i++)
990 sa = h->local_addrs[i];
991 switch (sa->sa_family)
994 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
999 v4 = (const struct sockaddr_in *) sa;
1000 if (0 != v4->sin_addr.s_addr)
1001 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1002 sizeof (struct sockaddr_in));
1004 add_minis (h, ntohs (v4->sin_port));
1007 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1013 memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, &any,
1014 sizeof (struct in6_addr)))
1015 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1016 sizeof (struct sockaddr_in6));
1027 * Attempt to enable port redirection and detect public IP address contacting
1028 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1029 * of the local host's addresses should the external port be mapped. The port
1030 * is taken from the corresponding sockaddr_in[6] field.
1032 * @param cfg configuration to use
1033 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1034 * @param adv_port advertised port (port we are either bound to or that our OS
1035 * locally performs redirection from to our bound port).
1036 * @param num_addrs number of addresses in 'addrs'
1037 * @param addr the local address packets should be redirected to
1038 * @param addrlen actual lenght of the address
1039 * @param address_callback function to call everytime the public IP address changes
1040 * @param reversal_callback function to call if someone wants connection reversal from us
1041 * @param callback_cls closure for callbacks
1042 * @return NULL on error, otherwise handle that can be used to unregister
1044 struct GNUNET_NAT_Handle *
1045 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, int is_tcp,
1046 uint16_t adv_port, unsigned int num_addrs,
1047 const struct sockaddr **addrs, const socklen_t * addrlens,
1048 GNUNET_NAT_AddressCallback address_callback,
1049 GNUNET_NAT_ReversalCallback reversal_callback,
1052 struct GNUNET_NAT_Handle *h;
1053 struct in_addr in_addr;
1057 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
1058 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1059 (unsigned int) adv_port, num_addrs);
1061 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1062 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1065 h->address_callback = address_callback;
1066 h->reversal_callback = reversal_callback;
1067 h->callback_cls = callback_cls;
1068 h->num_local_addrs = num_addrs;
1069 h->adv_port = adv_port;
1072 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr *));
1073 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1074 for (i = 0; i < num_addrs; i++)
1076 GNUNET_assert (addrlens[i] > 0);
1077 GNUNET_assert (addrs[i] != NULL);
1078 h->local_addrlens[i] = addrlens[i];
1079 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1080 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1083 h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1085 GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1087 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1089 &h->internal_address);
1091 if ((h->internal_address != NULL) &&
1092 (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1094 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1095 _("Malformed %s `%s' given in configuration!\n"),
1096 "INTERNAL_ADDRESS", h->internal_address);
1097 GNUNET_free (h->internal_address);
1098 h->internal_address = NULL;
1102 GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1104 (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1106 &h->external_address);
1108 if ((h->external_address != NULL) &&
1109 (inet_pton (AF_INET, h->external_address, &in_addr) != 1))
1111 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1112 _("Malformed %s `%s' given in configuration!\n"),
1113 "EXTERNAL_ADDRESS", h->external_address);
1114 GNUNET_free (h->external_address);
1115 h->external_address = NULL;
1118 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT");
1120 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT");
1121 h->enable_nat_client =
1122 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_CLIENT");
1123 h->enable_nat_server =
1124 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_SERVER");
1126 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP");
1127 h->use_localaddresses =
1128 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR");
1130 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME");
1132 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6");
1134 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY",
1135 &h->dyndns_frequency))
1136 h->dyndns_frequency = DYNDNS_FREQUENCY;
1138 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY",
1139 &h->ifc_scan_frequency))
1140 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1142 GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY",
1143 &h->hostname_dns_frequency))
1144 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1146 if (NULL == reversal_callback)
1147 h->enable_nat_server = GNUNET_NO;
1149 /* Check if NAT was hole-punched */
1150 if ((NULL != h->address_callback) && (h->external_address != NULL) &&
1151 (h->nat_punched == GNUNET_YES))
1153 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1154 h->enable_nat_server = GNUNET_NO;
1155 h->enable_upnp = GNUNET_NO;
1158 /* Test for SUID binaries */
1159 if ((h->behind_nat == GNUNET_YES) && (GNUNET_YES == h->enable_nat_server) &&
1161 GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server")))
1163 h->enable_nat_server = GNUNET_NO;
1164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1166 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1167 "gnunet-helper-nat-server");
1169 if ((GNUNET_YES == h->enable_nat_client) &&
1171 GNUNET_OS_check_helper_binary ("gnunet-helper-nat-client")))
1173 h->enable_nat_client = GNUNET_NO;
1174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1176 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1177 "gnunet-helper-nat-client");
1180 start_gnunet_nat_server (h);
1182 /* FIXME: add support for UPnP, etc */
1184 if (NULL != h->address_callback)
1186 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1187 if (GNUNET_YES == h->use_hostname)
1188 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1196 * Stop port redirection and public IP address detection for the given handle.
1197 * This frees the handle, after having sent the needed commands to close open ports.
1199 * @param h the handle to stop
1202 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1205 struct LocalAddressList *lal;
1206 struct MiniList *ml;
1208 while (NULL != (ml = h->mini_head))
1210 GNUNET_CONTAINER_DLL_remove (h->mini_head, h->mini_tail, ml);
1211 if (NULL != ml->mini)
1212 GNUNET_NAT_mini_map_stop (ml->mini);
1215 if (h->ext_dns != NULL)
1217 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1220 if (NULL != h->hostname_dns)
1222 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1223 h->hostname_dns = NULL;
1225 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1227 GNUNET_SCHEDULER_cancel (h->server_read_task);
1228 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1230 if (GNUNET_SCHEDULER_NO_TASK != h->bind_task)
1232 GNUNET_SCHEDULER_cancel (h->bind_task);
1233 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1235 if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1237 GNUNET_SCHEDULER_cancel (h->ifc_task);
1238 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1240 if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1242 GNUNET_SCHEDULER_cancel (h->hostname_task);
1243 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1245 if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1247 GNUNET_SCHEDULER_cancel (h->dns_task);
1248 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1250 if (NULL != h->server_proc)
1252 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1254 GNUNET_OS_process_wait (h->server_proc);
1255 GNUNET_OS_process_close (h->server_proc);
1256 h->server_proc = NULL;
1257 GNUNET_DISK_pipe_close (h->server_stdout);
1258 h->server_stdout = NULL;
1259 h->server_stdout_handle = NULL;
1261 if (NULL != h->server_stdout)
1263 GNUNET_DISK_pipe_close (h->server_stdout);
1264 h->server_stdout = NULL;
1265 h->server_stdout_handle = NULL;
1267 while (NULL != (lal = h->lal_head))
1269 GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal);
1270 if (NULL != h->address_callback)
1271 h->address_callback (h->callback_cls, GNUNET_NO,
1272 (const struct sockaddr *) &lal[1], lal->addrlen);
1275 for (i = 0; i < h->num_local_addrs; i++)
1276 GNUNET_free (h->local_addrs[i]);
1277 GNUNET_free_non_null (h->local_addrs);
1278 GNUNET_free_non_null (h->local_addrlens);
1279 GNUNET_free_non_null (h->external_address);
1280 GNUNET_free_non_null (h->internal_address);
1286 * We learned about a peer (possibly behind NAT) so run the
1287 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1288 * that peer to connect to us (connection reversal).
1290 * @param h NAT handle for us (largely used for configuration)
1291 * @param sa the address of the peer (IPv4-only)
1294 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1295 const struct sockaddr_in *sa)
1297 char inet4[INET_ADDRSTRLEN];
1298 char port_as_string[6];
1299 struct GNUNET_OS_Process *proc;
1301 if (GNUNET_YES != h->enable_nat_client)
1302 return; /* not permitted / possible */
1304 if (h->internal_address == NULL)
1306 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1308 ("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1311 GNUNET_assert (sa->sin_family == AF_INET);
1312 if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN))
1314 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1317 GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port);
1319 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
1320 _("Running gnunet-helper-nat-client %s %s %u\n"),
1321 h->internal_address, inet4, (unsigned int) h->adv_port);
1324 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-nat-client",
1325 "gnunet-helper-nat-client", h->internal_address,
1326 inet4, port_as_string, NULL);
1329 /* we know that the gnunet-helper-nat-client will terminate virtually
1331 GNUNET_OS_process_wait (proc);
1332 GNUNET_OS_process_close (proc);
1337 * Test if the given address is (currently) a plausible IP address for this peer.
1339 * @param h the handle returned by register
1340 * @param addr IP address to test (IPv4 or IPv6)
1341 * @param addrlen number of bytes in addr
1342 * @return GNUNET_YES if the address is plausible,
1343 * GNUNET_NO if the address is not plausible,
1344 * GNUNET_SYSERR if the address is malformed
1347 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, const void *addr,
1350 struct LocalAddressList *pos;
1351 const struct sockaddr_in *in4;
1352 const struct sockaddr_in6 *in6;
1354 if ((addrlen != sizeof (struct in_addr)) &&
1355 (addrlen != sizeof (struct in6_addr)))
1358 return GNUNET_SYSERR;
1363 if (pos->addrlen == sizeof (struct sockaddr_in))
1365 in4 = (struct sockaddr_in *) &pos[1];
1366 if ((addrlen == sizeof (struct in_addr)) &&
1367 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))))
1370 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1372 in6 = (struct sockaddr_in6 *) &pos[1];
1373 if ((addrlen == sizeof (struct in6_addr)) &&
1374 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))))
1383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1384 "Asked to validate one of my addresses and validation failed!\n");