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 * - implement UPnP/PMP support
32 #include "gnunet_util_lib.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_nat_lib.h"
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 on transmitting the welcome message?
56 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
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,
84 /* TODO: add UPnP, etc. */
95 * List of local addresses that we currently deem valid. Actual
96 * struct is followed by the 'struct sockaddr'. Note that the code
97 * intentionally makes no attempt to ensure that a particular address
98 * is only listed once (especially since it may come from different
99 * sources, and the source is an "internal" construct).
101 struct LocalAddressList
104 * This is a linked list.
106 struct LocalAddressList *next;
111 struct LocalAddressList *prev;
114 * Number of bytes of address that follow.
119 * Origin of the local address.
121 enum LocalAddressSource source;
126 * Handle for active NAT registrations.
128 struct GNUNET_NAT_Handle
132 * Configuration to use.
134 const struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Function to call when we learn about a new address.
139 GNUNET_NAT_AddressCallback address_callback;
142 * Function to call when we notice another peer asking for
143 * connection reversal.
145 GNUNET_NAT_ReversalCallback reversal_callback;
148 * Closure for 'callback'.
153 * Handle for (DYN)DNS lookup of our external IP.
155 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
158 * Handle for request of hostname resolution, non-NULL if pending.
160 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
163 * stdout pipe handle for the gnunet-helper-nat-server process
165 struct GNUNET_DISK_PipeHandle *server_stdout;
168 * stdout file handle (for reading) for the gnunet-helper-nat-server process
170 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
173 * Linked list of currently valid addresses (head).
175 struct LocalAddressList *lal_head;
178 * Linked list of currently valid addresses (tail).
180 struct LocalAddressList *lal_tail;
183 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
185 struct GNUNET_TIME_Relative server_retry_delay;
188 * ID of select gnunet-helper-nat-server stdout read task
190 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
193 * ID of interface IP-scan task
195 GNUNET_SCHEDULER_TaskIdentifier ifc_task;
198 * ID of hostname DNS lookup task
200 GNUNET_SCHEDULER_TaskIdentifier hostname_task;
203 * ID of DynDNS lookup task
205 GNUNET_SCHEDULER_TaskIdentifier dns_task;
208 * How often do we scan for changes in our IP address from our local
211 struct GNUNET_TIME_Relative ifc_scan_frequency;
214 * How often do we scan for changes in how our hostname resolves?
216 struct GNUNET_TIME_Relative hostname_dns_frequency;
219 * How often do we scan for changes in how our external (dyndns) hostname resolves?
221 struct GNUNET_TIME_Relative dyndns_frequency;
224 * The process id of the server process (if behind NAT)
226 struct GNUNET_OS_Process *server_proc;
229 * LAN address as passed by the caller (array).
231 struct sockaddr **local_addrs;
234 * Length of the 'local_addrs'.
236 socklen_t *local_addrlens;
239 * Number of entries in 'local_addrs' array.
241 unsigned int num_local_addrs;
244 * The our external address (according to config, UPnP may disagree...)
246 char *external_address;
249 * Presumably our internal address (according to config)
251 char *internal_address;
254 * Is this transport configured to be behind a NAT?
259 * Has the NAT been punched? (according to config)
264 * Is this transport configured to allow connections to NAT'd peers?
266 int enable_nat_client;
269 * Should we run the gnunet-helper-nat-server?
271 int enable_nat_server;
274 * Are we allowed to try UPnP/PMP for NAT traversal?
279 * Should we use local addresses (loopback)? (according to config)
281 int use_localaddresses;
284 * Is using IPv6 disabled?
289 * Is this TCP or UDP?
294 * Port we advertise to the outside.
302 * Try to start the gnunet-helper-nat-server (if it is not
305 * @param h handle to NAT
308 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
312 * Remove all addresses from the list of 'local' addresses
313 * that originated from the given source.
315 * @param plugin the plugin
316 * @param src source that identifies addresses to remove
319 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
320 enum LocalAddressSource src)
322 struct LocalAddressList *pos;
323 struct LocalAddressList *next;
326 while (NULL != (pos = next))
329 if (pos->source != src)
331 GNUNET_CONTAINER_DLL_remove (h->lal_head,
334 if (NULL != h->address_callback)
335 h->address_callback (h->callback_cls,
337 (const struct sockaddr* ) &pos[1],
345 * Add the given address to the list of 'local' addresses, thereby
346 * making it a 'legal' address for this peer to have.
348 * @param plugin the plugin
349 * @param src where did the local address originate from?
350 * @param arg the address, some 'struct sockaddr'
351 * @param arg_size number of bytes in arg
354 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
355 enum LocalAddressSource src,
356 const struct sockaddr *arg,
359 struct LocalAddressList *lal;
361 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
362 memcpy (&lal[1], arg, arg_size);
363 lal->addrlen = arg_size;
365 GNUNET_CONTAINER_DLL_insert (h->lal_head,
368 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
370 "Adding address `%s' from source %d\n",
371 GNUNET_a2s (arg, arg_size),
373 if (NULL != h->address_callback)
374 h->address_callback (h->callback_cls,
382 * Add the given address to the list of 'local' addresses, thereby
383 * making it a 'legal' address for this peer to have. Set the
384 * port number in the process to the advertised port and possibly
385 * also to zero (if we have the gnunet-helper-nat-server).
387 * @param plugin the plugin
388 * @param src where did the local address originate from?
389 * @param arg the address, some 'struct sockaddr'
390 * @param arg_size number of bytes in arg
393 add_to_address_list (struct GNUNET_NAT_Handle *h,
394 enum LocalAddressSource src,
395 const struct sockaddr *arg,
398 struct sockaddr_in s4;
399 const struct sockaddr_in *in4;
400 struct sockaddr_in6 s6;
401 const struct sockaddr_in6 *in6;
403 if (arg_size == sizeof (struct sockaddr_in))
405 in4 = (const struct sockaddr_in *) arg;
407 s4.sin_port = htons (h->adv_port);
408 add_to_address_list_as_is (h,
410 (const struct sockaddr*) &s4,
411 sizeof (struct sockaddr_in));
412 if (GNUNET_YES == h->enable_nat_server)
414 /* also add with PORT = 0 to indicate NAT server is enabled */
415 s4.sin_port = htons(0);
416 add_to_address_list_as_is (h,
418 (const struct sockaddr*) &s4,
419 sizeof (struct sockaddr_in));
422 else if (arg_size == sizeof (struct sockaddr_in6))
424 if (GNUNET_YES != h->disable_ipv6)
426 in6 = (const struct sockaddr_in6 *) arg;
428 s6.sin6_port = htons(h->adv_port);
429 add_to_address_list_as_is (h,
431 (const struct sockaddr*) &s6,
432 sizeof (struct sockaddr_in6));
443 * Add the given IP address to the list of 'local' addresses, thereby
444 * making it a 'legal' address for this peer to have.
446 * @param plugin the plugin
447 * @param src where did the local address originate from?
448 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
449 * @param arg_size number of bytes in arg
452 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
453 enum LocalAddressSource src,
457 struct sockaddr_in s4;
458 const struct in_addr *in4;
459 struct sockaddr_in6 s6;
460 const struct in6_addr *in6;
462 if (addrlen == sizeof (struct in_addr))
464 in4 = (const struct in_addr *) addr;
465 memset (&s4, 0, sizeof (s4));
466 s4.sin_family = AF_INET;
468 #if HAVE_SOCKADDR_IN_SIN_LEN
469 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
472 add_to_address_list (h,
474 (const struct sockaddr*) &s4,
475 sizeof (struct sockaddr_in));
476 if (GNUNET_YES == h->enable_nat_server)
478 /* also add with PORT = 0 to indicate NAT server is enabled */
479 s4.sin_port = htons(0);
480 add_to_address_list (h,
482 (const struct sockaddr*) &s4,
483 sizeof (struct sockaddr_in));
487 else if (addrlen == sizeof (struct in6_addr))
489 if (GNUNET_YES != h->disable_ipv6)
491 in6 = (const struct in6_addr *) addr;
492 memset (&s6, 0, sizeof (s6));
493 s6.sin6_family = AF_INET6;
494 s6.sin6_port = htons(h->adv_port);
495 #if HAVE_SOCKADDR_IN_SIN_LEN
496 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
499 add_to_address_list (h,
501 (const struct sockaddr*) &s6,
502 sizeof (struct sockaddr_in6));
513 * Task to do DNS lookup on our external hostname to
514 * get DynDNS-IP addresses.
516 * @param cls the NAT handle
517 * @param tc scheduler context
520 resolve_dns (void *cls,
521 const struct GNUNET_SCHEDULER_TaskContext *tc);
525 * Our (external) hostname was resolved and the configuration says that
526 * the NAT was hole-punched.
528 * @param cls the 'struct Plugin'
529 * @param addr NULL on error, otherwise result of DNS lookup
530 * @param addrlen number of bytes in addr
533 process_external_ip (void *cls,
534 const struct sockaddr *addr,
537 struct GNUNET_NAT_Handle *h = cls;
538 struct in_addr dummy;
543 if (1 == inet_pton (AF_INET,
546 return; /* repated lookup pointless: was numeric! */
547 h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
551 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
556 * Task to do a lookup on our hostname for IP addresses.
558 * @param cls the NAT handle
559 * @param tc scheduler context
562 resolve_hostname (void *cls,
563 const struct GNUNET_SCHEDULER_TaskContext *tc);
567 * Function called by the resolver for each address obtained from DNS
568 * for our own hostname. Add the addresses to the list of our IP
572 * @param addr one of the addresses of the host, NULL for the last address
573 * @param addrlen length of the address
576 process_hostname_ip (void *cls,
577 const struct sockaddr *addr, socklen_t addrlen)
579 struct GNUNET_NAT_Handle *h = cls;
583 h->hostname_dns = NULL;
584 h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
585 &resolve_hostname, h);
588 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
593 * Add the IP of our network interface to the list of
596 * @param cls the 'struct GNUNET_NAT_Handle'
597 * @param name name of the interface
598 * @param isDefault do we think this may be our default interface
599 * @param addr address of the interface
600 * @param addrlen number of bytes in addr
601 * @return GNUNET_OK to continue iterating
604 process_interfaces (void *cls,
607 const struct sockaddr *addr, socklen_t addrlen)
609 struct GNUNET_NAT_Handle *h = cls;
610 const struct sockaddr_in *s4;
611 const struct sockaddr_in6 *s6;
613 char buf[INET6_ADDRSTRLEN];
615 switch (addr->sa_family)
618 s4 = (struct sockaddr_in *) addr;
620 if (GNUNET_YES == h->use_localaddresses)
621 add_ip_to_address_list (h,
622 LAL_INTERFACE_ADDRESS,
624 sizeof (struct in_addr));
627 s6 = (struct sockaddr_in6 *) addr;
628 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
630 /* skip link local addresses */
634 if (GNUNET_YES == h->use_localaddresses)
635 add_ip_to_address_list (h,
636 LAL_INTERFACE_ADDRESS,
638 sizeof (struct in6_addr));
644 if ( (h->internal_address == NULL) &&
645 (h->server_proc == NULL) &&
646 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
647 (GNUNET_YES == isDefault) &&
648 ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) )
650 /* no internal address configured, but we found a "default"
651 interface, try using that as our 'internal' address */
652 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
656 start_gnunet_nat_server (h);
663 * Return the actual path to a file found in the current
664 * PATH environment variable.
666 * @param binary the name of the file to find
667 * @return path to binary, NULL if not found
670 get_path_from_PATH (const char *binary)
681 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
683 _("PATH environment variable is unset.\n"));
686 path = GNUNET_strdup (p); /* because we write on it */
687 buf = GNUNET_malloc (strlen (path) + 20);
690 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
693 sprintf (buf, "%s/%s", pos, binary);
694 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
701 sprintf (buf, "%s/%s", pos, binary);
702 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
714 * Check whether the suid bit is set on a file.
715 * Attempts to find the file using the current
716 * PATH environment variable as a search path.
718 * @param binary the name of the file to check
719 * @return GNUNET_YES if the file is SUID,
721 * GNUNET_SYSERR on error
724 check_gnunet_nat_binary (const char *binary)
732 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
733 p = get_path_from_PATH (binaryexe);
736 p = get_path_from_PATH (binary);
740 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
742 _("Could not find binary `%s' in PATH!\n"),
746 if (0 != STAT (p, &statbuf))
748 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
749 _("stat (%s) failed: %s\n"),
753 return GNUNET_SYSERR;
757 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
758 (statbuf.st_uid == 0) )
762 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
763 if (INVALID_SOCKET == rawsock)
765 DWORD err = GetLastError ();
766 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
768 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
769 return GNUNET_NO; /* not running as administrator */
771 closesocket (rawsock);
778 * Task that restarts the gnunet-helper-nat-server process after a crash
779 * after a certain delay.
781 * @param cls the 'struct GNUNET_NAT_Handle'
782 * @param tc scheduler context
785 restart_nat_server (void *cls,
786 const struct GNUNET_SCHEDULER_TaskContext *tc)
788 struct GNUNET_NAT_Handle *h = cls;
790 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
791 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
793 start_gnunet_nat_server (h);
798 * We have been notified that gnunet-helper-nat-server has written something to stdout.
799 * Handle the output, then reschedule this function to be called again once
802 * @param cls the NAT handle
803 * @param tc the scheduling context
806 nat_server_read (void *cls,
807 const struct GNUNET_SCHEDULER_TaskContext *tc)
809 struct GNUNET_NAT_Handle *h = cls;
814 const char *port_start;
815 struct sockaddr_in sin_addr;
817 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
818 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
820 memset (mybuf, 0, sizeof(mybuf));
821 bytes = GNUNET_DISK_file_read(h->server_stdout_handle,
827 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
829 "Finished reading from server stdout with code: %d\n",
832 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
833 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
834 GNUNET_OS_process_wait (h->server_proc);
835 GNUNET_OS_process_close (h->server_proc);
836 h->server_proc = NULL;
837 GNUNET_DISK_pipe_close (h->server_stdout);
838 h->server_stdout = NULL;
839 h->server_stdout_handle = NULL;
840 /* now try to restart it */
841 h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
842 h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
843 h->server_retry_delay);
844 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
851 for (i = 0; i < sizeof(mybuf); i++)
853 if (mybuf[i] == '\n')
858 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
861 port_start = &mybuf[i + 1];
865 /* construct socket address of sender */
866 memset (&sin_addr, 0, sizeof (sin_addr));
867 sin_addr.sin_family = AF_INET;
868 #if HAVE_SOCKADDR_IN_SIN_LEN
869 sin_addr.sin_len = sizeof (sin_addr);
871 if ( (NULL == port_start) ||
872 (1 != sscanf (port_start, "%d", &port)) ||
873 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
875 /* should we restart gnunet-helper-nat-server? */
876 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
878 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
881 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
882 h->server_stdout_handle,
887 sin_addr.sin_port = htons((uint16_t) port);
889 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
891 "gnunet-helper-nat-server read: %s:%d\n",
894 h->reversal_callback (h->callback_cls,
895 (const struct sockaddr*) &sin_addr,
897 h->server_read_task =
898 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
899 h->server_stdout_handle,
906 * Try to start the gnunet-helper-nat-server (if it is not
909 * @param h handle to NAT
912 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
914 if ( (h->behind_nat == GNUNET_YES) &&
915 (h->enable_nat_server == GNUNET_YES) &&
916 (h->internal_address != NULL) &&
917 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
922 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
924 "Starting %s at `%s'\n",
925 "gnunet-helper-nat-server",
926 h->internal_address);
928 /* Start the server process */
929 h->server_proc = GNUNET_OS_start_process (NULL,
931 "gnunet-helper-nat-server",
932 "gnunet-helper-nat-server",
935 if (h->server_proc == NULL)
937 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
939 _("Failed to start %s\n"),
940 "gnunet-helper-nat-server");
941 GNUNET_DISK_pipe_close (h->server_stdout);
942 h->server_stdout = NULL;
946 /* Close the write end of the read pipe */
947 GNUNET_DISK_pipe_close_end(h->server_stdout,
948 GNUNET_DISK_PIPE_END_WRITE);
949 h->server_stdout_handle
950 = GNUNET_DISK_pipe_handle (h->server_stdout,
951 GNUNET_DISK_PIPE_END_READ);
953 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
954 h->server_stdout_handle,
963 * Task to scan the local network interfaces for IP addresses.
965 * @param cls the NAT handle
966 * @param tc scheduler context
969 list_interfaces (void *cls,
970 const struct GNUNET_SCHEDULER_TaskContext *tc)
972 struct GNUNET_NAT_Handle *h = cls;
974 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
975 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
976 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
977 h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
978 &list_interfaces, h);
983 * Task to do a lookup on our hostname for IP addresses.
985 * @param cls the NAT handle
986 * @param tc scheduler context
989 resolve_hostname (void *cls,
990 const struct GNUNET_SCHEDULER_TaskContext *tc)
992 struct GNUNET_NAT_Handle *h = cls;
994 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
995 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
996 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
997 HOSTNAME_RESOLVE_TIMEOUT,
998 &process_hostname_ip,
1004 * Task to do DNS lookup on our external hostname to
1005 * get DynDNS-IP addresses.
1007 * @param cls the NAT handle
1008 * @param tc scheduler context
1011 resolve_dns (void *cls,
1012 const struct GNUNET_SCHEDULER_TaskContext *tc)
1014 struct GNUNET_NAT_Handle *h = cls;
1016 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1017 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
1018 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
1020 GNUNET_TIME_UNIT_MINUTES,
1021 &process_external_ip,
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,
1048 unsigned int num_addrs,
1049 const struct sockaddr **addrs,
1050 const socklen_t *addrlens,
1051 GNUNET_NAT_AddressCallback address_callback,
1052 GNUNET_NAT_ReversalCallback reversal_callback,
1055 struct GNUNET_NAT_Handle *h;
1056 struct in_addr in_addr;
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1061 (unsigned int) adv_port,
1063 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1064 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1067 h->address_callback = address_callback;
1068 h->reversal_callback = reversal_callback;
1069 h->callback_cls = callback_cls;
1070 h->num_local_addrs = num_addrs;
1071 h->adv_port = adv_port;
1074 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr*));
1075 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1076 for (i=0;i<num_addrs;i++)
1078 GNUNET_assert (addrlens[i] > 0);
1079 GNUNET_assert (addrs[i] != NULL);
1080 h->local_addrlens[i] = addrlens[i];
1081 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1082 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1087 GNUNET_CONFIGURATION_have_value (cfg,
1089 "INTERNAL_ADDRESS"))
1091 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1094 &h->internal_address);
1096 if ( (h->internal_address != NULL) &&
1097 (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) )
1099 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1101 _("Malformed %s `%s' given in configuration!\n"),
1103 h->internal_address);
1104 GNUNET_free (h->internal_address);
1105 h->internal_address = NULL;
1109 GNUNET_CONFIGURATION_have_value (cfg,
1111 "EXTERNAL_ADDRESS"))
1113 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1116 &h->external_address);
1118 if ( (h->external_address != NULL) &&
1119 (inet_pton(AF_INET, h->external_address, &in_addr) != 1) )
1121 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1123 _("Malformed %s `%s' given in configuration!\n"),
1125 h->external_address);
1126 GNUNET_free (h->external_address);
1127 h->external_address = NULL;
1129 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1132 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1135 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1137 "ENABLE_NAT_CLIENT");
1138 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1140 "ENABLE_NAT_SERVER");
1141 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1144 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1147 h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno(cfg,
1151 GNUNET_CONFIGURATION_get_value_time (cfg,
1154 &h->dyndns_frequency))
1155 h->dyndns_frequency = DYNDNS_FREQUENCY;
1157 GNUNET_CONFIGURATION_get_value_time (cfg,
1159 "IFC_SCAN_FREQUENCY",
1160 &h->ifc_scan_frequency))
1161 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1163 GNUNET_CONFIGURATION_get_value_time (cfg,
1165 "HOSTNAME_DNS_FREQUENCY",
1166 &h->hostname_dns_frequency))
1167 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1169 if (NULL == reversal_callback)
1170 h->enable_nat_server = GNUNET_NO;
1172 /* Check if NAT was hole-punched */
1173 if ( (NULL != h->address_callback) &&
1174 (h->external_address != NULL) &&
1175 (h->nat_punched == GNUNET_YES) )
1177 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1178 h->enable_nat_server = GNUNET_NO;
1179 h->enable_upnp = GNUNET_NO;
1182 /* Test for SUID binaries */
1183 if ( (h->behind_nat == GNUNET_YES) &&
1184 (GNUNET_YES == h->enable_nat_server) &&
1185 (GNUNET_YES != check_gnunet_nat_binary("gnunet-helper-nat-server")) )
1187 h->enable_nat_server = GNUNET_NO;
1188 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1189 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1190 "gnunet-helper-nat-server");
1192 if ( (GNUNET_YES == h->enable_nat_client) &&
1193 (GNUNET_YES != check_gnunet_nat_binary("gnunet-helper-nat-client")) )
1195 h->enable_nat_client = GNUNET_NO;
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1197 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1198 "gnunet-helper-nat-client");
1201 start_gnunet_nat_server (h);
1203 /* FIXME: add support for UPnP, etc */
1205 if (NULL != h->address_callback)
1207 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1208 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1215 * Stop port redirection and public IP address detection for the given handle.
1216 * This frees the handle, after having sent the needed commands to close open ports.
1218 * @param h the handle to stop
1221 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1224 struct LocalAddressList *lal;
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->ifc_task)
1243 GNUNET_SCHEDULER_cancel (h->ifc_task);
1244 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1246 if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1248 GNUNET_SCHEDULER_cancel (h->hostname_task);
1249 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1251 if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1253 GNUNET_SCHEDULER_cancel (h->dns_task);
1254 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1256 if (NULL != h->server_proc)
1258 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1259 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1260 GNUNET_OS_process_wait (h->server_proc);
1261 GNUNET_OS_process_close (h->server_proc);
1262 h->server_proc = NULL;
1263 GNUNET_DISK_pipe_close (h->server_stdout);
1264 h->server_stdout = NULL;
1265 h->server_stdout_handle = NULL;
1267 if (NULL != h->server_stdout)
1269 GNUNET_DISK_pipe_close (h->server_stdout);
1270 h->server_stdout = NULL;
1271 h->server_stdout_handle = NULL;
1273 while (NULL != (lal = h->lal_head))
1275 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1278 if (NULL != h->address_callback)
1279 h->address_callback (h->callback_cls,
1281 (const struct sockaddr*) &lal[1],
1285 for (i=0;i<h->num_local_addrs;i++)
1286 GNUNET_free (h->local_addrs[i]);
1287 GNUNET_free_non_null (h->local_addrs);
1288 GNUNET_free_non_null (h->local_addrlens);
1289 GNUNET_free_non_null (h->external_address);
1290 GNUNET_free_non_null (h->internal_address);
1296 * We learned about a peer (possibly behind NAT) so run the
1297 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1298 * that peer to connect to us (connection reversal).
1300 * @param h NAT handle for us (largely used for configuration)
1301 * @param sa the address of the peer (IPv4-only)
1304 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1305 const struct sockaddr_in *sa)
1307 char inet4[INET_ADDRSTRLEN];
1308 char port_as_string[6];
1309 struct GNUNET_OS_Process *proc;
1311 if (GNUNET_YES != h->enable_nat_client)
1312 return; /* not permitted / possible */
1314 if (h->internal_address == NULL)
1316 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1318 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1321 GNUNET_assert (sa->sin_family == AF_INET);
1322 if (NULL == inet_ntop (AF_INET,
1324 inet4, INET_ADDRSTRLEN))
1326 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1329 GNUNET_snprintf (port_as_string,
1330 sizeof (port_as_string),
1334 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1336 _("Running gnunet-helper-nat-client %s %s %u\n"),
1337 h->internal_address,
1339 (unsigned int) h->adv_port);
1341 proc = GNUNET_OS_start_process (NULL,
1343 "gnunet-helper-nat-client",
1344 "gnunet-helper-nat-client",
1345 h->internal_address,
1351 /* we know that the gnunet-helper-nat-client will terminate virtually
1353 GNUNET_OS_process_wait (proc);
1354 GNUNET_OS_process_close (proc);
1359 * Test if the given address is (currently) a plausible IP address for this peer.
1361 * @param h the handle returned by register
1362 * @param addr IP address to test (IPv4 or IPv6)
1363 * @param addrlen number of bytes in addr
1364 * @return GNUNET_YES if the address is plausible,
1365 * GNUNET_NO if the address is not plausible,
1366 * GNUNET_SYSERR if the address is malformed
1369 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1373 struct LocalAddressList *pos;
1374 const struct sockaddr_in *in4;
1375 const struct sockaddr_in6 *in6;
1377 if ( (addrlen != sizeof (struct in_addr)) &&
1378 (addrlen != sizeof (struct in6_addr)) )
1381 return GNUNET_SYSERR;
1386 if (pos->addrlen == sizeof (struct sockaddr_in))
1388 in4 = (struct sockaddr_in* ) &pos[1];
1389 if ( (addrlen == sizeof (struct in_addr)) &&
1390 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))) )
1393 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1395 in6 = (struct sockaddr_in6* ) &pos[1];
1396 if ( (addrlen == sizeof (struct in6_addr)) &&
1397 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))) )
1406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1407 "Asked to validate one of my addresses and validation failed!\n");