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 h->address_callback (h->callback_cls,
336 (const struct sockaddr* ) &pos[1],
344 * Add the given address to the list of 'local' addresses, thereby
345 * making it a 'legal' address for this peer to have.
347 * @param plugin the plugin
348 * @param src where did the local address originate from?
349 * @param arg the address, some 'struct sockaddr'
350 * @param arg_size number of bytes in arg
353 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
354 enum LocalAddressSource src,
355 const struct sockaddr *arg,
358 struct LocalAddressList *lal;
360 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
361 memcpy (&lal[1], arg, arg_size);
362 lal->addrlen = arg_size;
364 GNUNET_CONTAINER_DLL_insert (h->lal_head,
367 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
369 "Adding address `%s' from source %d\n",
370 GNUNET_a2s (arg, arg_size),
372 h->address_callback (h->callback_cls,
380 * Add the given address to the list of 'local' addresses, thereby
381 * making it a 'legal' address for this peer to have. Set the
382 * port number in the process to the advertised port and possibly
383 * also to zero (if we have the gnunet-helper-nat-server).
385 * @param plugin the plugin
386 * @param src where did the local address originate from?
387 * @param arg the address, some 'struct sockaddr'
388 * @param arg_size number of bytes in arg
391 add_to_address_list (struct GNUNET_NAT_Handle *h,
392 enum LocalAddressSource src,
393 const struct sockaddr *arg,
396 struct sockaddr_in s4;
397 const struct sockaddr_in *in4;
398 struct sockaddr_in6 s6;
399 const struct sockaddr_in6 *in6;
401 if (arg_size == sizeof (struct sockaddr_in))
403 in4 = (const struct sockaddr_in *) arg;
405 s4.sin_port = htons (h->adv_port);
406 add_to_address_list_as_is (h,
408 (const struct sockaddr*) &s4,
409 sizeof (struct sockaddr_in));
410 if (GNUNET_YES == h->enable_nat_server)
412 /* also add with PORT = 0 to indicate NAT server is enabled */
413 s4.sin_port = htons(0);
414 add_to_address_list_as_is (h,
416 (const struct sockaddr*) &s4,
417 sizeof (struct sockaddr_in));
420 else if (arg_size == sizeof (struct sockaddr_in6))
422 if (GNUNET_YES != h->disable_ipv6)
424 in6 = (const struct sockaddr_in6 *) arg;
426 s6.sin6_port = htons(h->adv_port);
427 add_to_address_list_as_is (h,
429 (const struct sockaddr*) &s6,
430 sizeof (struct sockaddr_in6));
441 * Add the given IP address to the list of 'local' addresses, thereby
442 * making it a 'legal' address for this peer to have.
444 * @param plugin the plugin
445 * @param src where did the local address originate from?
446 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
447 * @param arg_size number of bytes in arg
450 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
451 enum LocalAddressSource src,
455 struct sockaddr_in s4;
456 const struct in_addr *in4;
457 struct sockaddr_in6 s6;
458 const struct in6_addr *in6;
460 if (addrlen == sizeof (struct in_addr))
462 in4 = (const struct in_addr *) addr;
463 memset (&s4, 0, sizeof (s4));
464 s4.sin_family = AF_INET;
466 #if HAVE_SOCKADDR_IN_SIN_LEN
467 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
470 add_to_address_list (h,
472 (const struct sockaddr*) &s4,
473 sizeof (struct sockaddr_in));
474 if (GNUNET_YES == h->enable_nat_server)
476 /* also add with PORT = 0 to indicate NAT server is enabled */
477 s4.sin_port = htons(0);
478 add_to_address_list (h,
480 (const struct sockaddr*) &s4,
481 sizeof (struct sockaddr_in));
485 else if (addrlen == sizeof (struct in6_addr))
487 if (GNUNET_YES != h->disable_ipv6)
489 in6 = (const struct in6_addr *) addr;
490 memset (&s6, 0, sizeof (s6));
491 s6.sin6_family = AF_INET6;
492 s6.sin6_port = htons(h->adv_port);
493 #if HAVE_SOCKADDR_IN_SIN_LEN
494 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
497 add_to_address_list (h,
499 (const struct sockaddr*) &s6,
500 sizeof (struct sockaddr_in6));
511 * Task to do DNS lookup on our external hostname to
512 * get DynDNS-IP addresses.
514 * @param cls the NAT handle
515 * @param tc scheduler context
518 resolve_dns (void *cls,
519 const struct GNUNET_SCHEDULER_TaskContext *tc);
523 * Our (external) hostname was resolved and the configuration says that
524 * the NAT was hole-punched.
526 * @param cls the 'struct Plugin'
527 * @param addr NULL on error, otherwise result of DNS lookup
528 * @param addrlen number of bytes in addr
531 process_external_ip (void *cls,
532 const struct sockaddr *addr,
535 struct GNUNET_NAT_Handle *h = cls;
536 struct in_addr dummy;
541 if (1 == inet_pton (AF_INET,
544 return; /* repated lookup pointless: was numeric! */
545 h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
549 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
554 * Task to do a lookup on our hostname for IP addresses.
556 * @param cls the NAT handle
557 * @param tc scheduler context
560 resolve_hostname (void *cls,
561 const struct GNUNET_SCHEDULER_TaskContext *tc);
565 * Function called by the resolver for each address obtained from DNS
566 * for our own hostname. Add the addresses to the list of our IP
570 * @param addr one of the addresses of the host, NULL for the last address
571 * @param addrlen length of the address
574 process_hostname_ip (void *cls,
575 const struct sockaddr *addr, socklen_t addrlen)
577 struct GNUNET_NAT_Handle *h = cls;
581 h->hostname_dns = NULL;
582 h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
583 &resolve_hostname, h);
586 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
591 * Add the IP of our network interface to the list of
594 * @param cls the 'struct GNUNET_NAT_Handle'
595 * @param name name of the interface
596 * @param isDefault do we think this may be our default interface
597 * @param addr address of the interface
598 * @param addrlen number of bytes in addr
599 * @return GNUNET_OK to continue iterating
602 process_interfaces (void *cls,
605 const struct sockaddr *addr, socklen_t addrlen)
607 struct GNUNET_NAT_Handle *h = cls;
608 const struct sockaddr_in *s4;
609 const struct sockaddr_in6 *s6;
611 char buf[INET6_ADDRSTRLEN];
613 switch (addr->sa_family)
616 s4 = (struct sockaddr_in *) addr;
618 if (GNUNET_YES == h->use_localaddresses)
619 add_ip_to_address_list (h,
620 LAL_INTERFACE_ADDRESS,
622 sizeof (struct in_addr));
625 s6 = (struct sockaddr_in6 *) addr;
626 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
628 /* skip link local addresses */
632 if (GNUNET_YES == h->use_localaddresses)
633 add_ip_to_address_list (h,
634 LAL_INTERFACE_ADDRESS,
636 sizeof (struct in6_addr));
642 if ( (h->internal_address == NULL) &&
643 (h->server_proc == NULL) &&
644 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
645 (GNUNET_YES == isDefault) &&
646 ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) )
648 /* no internal address configured, but we found a "default"
649 interface, try using that as our 'internal' address */
650 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
654 start_gnunet_nat_server (h);
661 * Return the actual path to a file found in the current
662 * PATH environment variable.
664 * @param binary the name of the file to find
665 * @return path to binary, NULL if not found
668 get_path_from_PATH (const char *binary)
679 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
681 _("PATH environment variable is unset.\n"));
684 path = GNUNET_strdup (p); /* because we write on it */
685 buf = GNUNET_malloc (strlen (path) + 20);
688 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
691 sprintf (buf, "%s/%s", pos, binary);
692 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
699 sprintf (buf, "%s/%s", pos, binary);
700 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
712 * Check whether the suid bit is set on a file.
713 * Attempts to find the file using the current
714 * PATH environment variable as a search path.
716 * @param binary the name of the file to check
717 * @return GNUNET_YES if the file is SUID,
719 * GNUNET_SYSERR on error
722 check_gnunet_nat_binary (const char *binary)
730 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
731 p = get_path_from_PATH (binaryexe);
734 p = get_path_from_PATH (binary);
738 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
740 _("Could not find binary `%s' in PATH!\n"),
744 if (0 != STAT (p, &statbuf))
746 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
747 _("stat (%s) failed: %s\n"),
751 return GNUNET_SYSERR;
755 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
756 (statbuf.st_uid == 0) )
760 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
761 if (INVALID_SOCKET == rawsock)
763 DWORD err = GetLastError ();
764 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
766 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
767 return GNUNET_NO; /* not running as administrator */
769 closesocket (rawsock);
776 * Task that restarts the gnunet-helper-nat-server process after a crash
777 * after a certain delay.
779 * @param cls the 'struct GNUNET_NAT_Handle'
780 * @param tc scheduler context
783 restart_nat_server (void *cls,
784 const struct GNUNET_SCHEDULER_TaskContext *tc)
786 struct GNUNET_NAT_Handle *h = cls;
788 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
789 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
791 start_gnunet_nat_server (h);
796 * We have been notified that gnunet-helper-nat-server has written something to stdout.
797 * Handle the output, then reschedule this function to be called again once
800 * @param cls the NAT handle
801 * @param tc the scheduling context
804 nat_server_read (void *cls,
805 const struct GNUNET_SCHEDULER_TaskContext *tc)
807 struct GNUNET_NAT_Handle *h = cls;
812 const char *port_start;
813 struct sockaddr_in sin_addr;
815 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
816 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
818 memset (mybuf, 0, sizeof(mybuf));
819 bytes = GNUNET_DISK_file_read(h->server_stdout_handle,
825 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
827 "Finished reading from server stdout with code: %d\n",
830 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
831 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
832 GNUNET_OS_process_wait (h->server_proc);
833 GNUNET_OS_process_close (h->server_proc);
834 h->server_proc = NULL;
835 GNUNET_DISK_pipe_close (h->server_stdout);
836 h->server_stdout = NULL;
837 h->server_stdout_handle = NULL;
838 /* now try to restart it */
839 h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
840 h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
841 h->server_retry_delay);
842 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
849 for (i = 0; i < sizeof(mybuf); i++)
851 if (mybuf[i] == '\n')
856 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
859 port_start = &mybuf[i + 1];
863 /* construct socket address of sender */
864 memset (&sin_addr, 0, sizeof (sin_addr));
865 sin_addr.sin_family = AF_INET;
866 #if HAVE_SOCKADDR_IN_SIN_LEN
867 sin_addr.sin_len = sizeof (sin_addr);
869 if ( (NULL == port_start) ||
870 (1 != sscanf (port_start, "%d", &port)) ||
871 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
873 /* should we restart gnunet-helper-nat-server? */
874 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
876 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
879 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
880 h->server_stdout_handle,
885 sin_addr.sin_port = htons((uint16_t) port);
887 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
889 "gnunet-helper-nat-server read: %s:%d\n",
892 h->reversal_callback (h->callback_cls,
893 (const struct sockaddr*) &sin_addr,
895 h->server_read_task =
896 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
897 h->server_stdout_handle,
904 * Try to start the gnunet-helper-nat-server (if it is not
907 * @param h handle to NAT
910 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
912 if ( (h->behind_nat == GNUNET_YES) &&
913 (h->enable_nat_server == GNUNET_YES) &&
914 (h->internal_address != NULL) &&
915 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
920 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
922 "Starting %s at `%s'\n",
923 "gnunet-helper-nat-server",
924 h->internal_address);
926 /* Start the server process */
927 h->server_proc = GNUNET_OS_start_process (NULL,
929 "gnunet-helper-nat-server",
930 "gnunet-helper-nat-server",
933 if (h->server_proc == NULL)
935 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
937 _("Failed to start %s\n"),
938 "gnunet-helper-nat-server");
939 GNUNET_DISK_pipe_close (h->server_stdout);
940 h->server_stdout = NULL;
944 /* Close the write end of the read pipe */
945 GNUNET_DISK_pipe_close_end(h->server_stdout,
946 GNUNET_DISK_PIPE_END_WRITE);
947 h->server_stdout_handle
948 = GNUNET_DISK_pipe_handle (h->server_stdout,
949 GNUNET_DISK_PIPE_END_READ);
951 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
952 h->server_stdout_handle,
961 * Task to scan the local network interfaces for IP addresses.
963 * @param cls the NAT handle
964 * @param tc scheduler context
967 list_interfaces (void *cls,
968 const struct GNUNET_SCHEDULER_TaskContext *tc)
970 struct GNUNET_NAT_Handle *h = cls;
972 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
973 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
974 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
975 h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
976 &list_interfaces, h);
981 * Task to do a lookup on our hostname for IP addresses.
983 * @param cls the NAT handle
984 * @param tc scheduler context
987 resolve_hostname (void *cls,
988 const struct GNUNET_SCHEDULER_TaskContext *tc)
990 struct GNUNET_NAT_Handle *h = cls;
992 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
993 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
994 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
995 HOSTNAME_RESOLVE_TIMEOUT,
996 &process_hostname_ip,
1002 * Task to do DNS lookup on our external hostname to
1003 * get DynDNS-IP addresses.
1005 * @param cls the NAT handle
1006 * @param tc scheduler context
1009 resolve_dns (void *cls,
1010 const struct GNUNET_SCHEDULER_TaskContext *tc)
1012 struct GNUNET_NAT_Handle *h = cls;
1014 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1015 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
1016 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
1018 GNUNET_TIME_UNIT_MINUTES,
1019 &process_external_ip,
1025 * Attempt to enable port redirection and detect public IP address contacting
1026 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1027 * of the local host's addresses should the external port be mapped. The port
1028 * is taken from the corresponding sockaddr_in[6] field.
1030 * @param cfg configuration to use
1031 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1032 * @param adv_port advertised port (port we are either bound to or that our OS
1033 * locally performs redirection from to our bound port).
1034 * @param num_addrs number of addresses in 'addrs'
1035 * @param addr the local address packets should be redirected to
1036 * @param addrlen actual lenght of the address
1037 * @param address_callback function to call everytime the public IP address changes
1038 * @param reversal_callback function to call if someone wants connection reversal from us
1039 * @param callback_cls closure for callbacks
1040 * @return NULL on error, otherwise handle that can be used to unregister
1042 struct GNUNET_NAT_Handle *
1043 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1046 unsigned int num_addrs,
1047 const struct sockaddr **addrs,
1048 const socklen_t *addrlens,
1049 GNUNET_NAT_AddressCallback address_callback,
1050 GNUNET_NAT_ReversalCallback reversal_callback,
1053 struct GNUNET_NAT_Handle *h;
1054 struct in_addr in_addr;
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1059 (unsigned int) adv_port,
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]);
1085 GNUNET_CONFIGURATION_have_value (cfg,
1087 "INTERNAL_ADDRESS"))
1089 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1092 &h->internal_address);
1094 if ( (h->internal_address != NULL) &&
1095 (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) )
1097 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1099 _("Malformed %s `%s' given in configuration!\n"),
1101 h->internal_address);
1102 GNUNET_free (h->internal_address);
1103 h->internal_address = NULL;
1107 GNUNET_CONFIGURATION_have_value (cfg,
1109 "EXTERNAL_ADDRESS"))
1111 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1114 &h->external_address);
1116 if ( (h->external_address != NULL) &&
1117 (inet_pton(AF_INET, h->external_address, &in_addr) != 1) )
1119 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1121 _("Malformed %s `%s' given in configuration!\n"),
1123 h->external_address);
1124 GNUNET_free (h->external_address);
1125 h->external_address = NULL;
1127 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1130 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1133 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1135 "ENABLE_NAT_CLIENT");
1136 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1138 "ENABLE_NAT_SERVER");
1139 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1142 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1145 h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno(cfg,
1149 GNUNET_CONFIGURATION_get_value_time (cfg,
1152 &h->dyndns_frequency))
1153 h->dyndns_frequency = DYNDNS_FREQUENCY;
1155 GNUNET_CONFIGURATION_get_value_time (cfg,
1157 "IFC_SCAN_FREQUENCY",
1158 &h->ifc_scan_frequency))
1159 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1161 GNUNET_CONFIGURATION_get_value_time (cfg,
1163 "HOSTNAME_DNS_FREQUENCY",
1164 &h->hostname_dns_frequency))
1165 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1167 if (NULL == reversal_callback)
1168 h->enable_nat_server = GNUNET_NO;
1170 /* Check if NAT was hole-punched */
1171 if ( (NULL != h->address_callback) &&
1172 (h->external_address != NULL) &&
1173 (h->nat_punched == GNUNET_YES) )
1175 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1176 h->enable_nat_server = GNUNET_NO;
1177 h->enable_upnp = GNUNET_NO;
1180 /* Test for SUID binaries */
1181 if ( (h->behind_nat == GNUNET_YES) &&
1182 (GNUNET_YES == h->enable_nat_server) &&
1183 (GNUNET_YES != check_gnunet_nat_binary("gnunet-helper-nat-server")) )
1185 h->enable_nat_server = GNUNET_NO;
1186 GNUNET_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-server");
1190 if ( (GNUNET_YES == h->enable_nat_client) &&
1191 (GNUNET_YES != check_gnunet_nat_binary("gnunet-helper-nat-client")) )
1193 h->enable_nat_client = GNUNET_NO;
1194 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1195 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1196 "gnunet-helper-nat-client");
1199 start_gnunet_nat_server (h);
1201 /* FIXME: add support for UPnP, etc */
1203 if (NULL != h->address_callback)
1205 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1206 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1213 * Stop port redirection and public IP address detection for the given handle.
1214 * This frees the handle, after having sent the needed commands to close open ports.
1216 * @param h the handle to stop
1219 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1222 struct LocalAddressList *lal;
1224 if (h->ext_dns != NULL)
1226 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1229 if (NULL != h->hostname_dns)
1231 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1232 h->hostname_dns = NULL;
1234 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1236 GNUNET_SCHEDULER_cancel (h->server_read_task);
1237 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1239 if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1241 GNUNET_SCHEDULER_cancel (h->ifc_task);
1242 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1244 if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1246 GNUNET_SCHEDULER_cancel (h->hostname_task);
1247 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1249 if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1251 GNUNET_SCHEDULER_cancel (h->dns_task);
1252 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1254 if (NULL != h->server_proc)
1256 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1257 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1258 GNUNET_OS_process_wait (h->server_proc);
1259 GNUNET_OS_process_close (h->server_proc);
1260 h->server_proc = NULL;
1261 GNUNET_DISK_pipe_close (h->server_stdout);
1262 h->server_stdout = NULL;
1263 h->server_stdout_handle = NULL;
1265 if (NULL != h->server_stdout)
1267 GNUNET_DISK_pipe_close (h->server_stdout);
1268 h->server_stdout = NULL;
1269 h->server_stdout_handle = NULL;
1271 while (NULL != (lal = h->lal_head))
1273 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1276 h->address_callback (h->callback_cls,
1278 (const struct sockaddr*) &lal[1],
1282 for (i=0;i<h->num_local_addrs;i++)
1283 GNUNET_free (h->local_addrs[i]);
1284 GNUNET_free_non_null (h->local_addrs);
1285 GNUNET_free_non_null (h->local_addrlens);
1286 GNUNET_free_non_null (h->external_address);
1287 GNUNET_free_non_null (h->internal_address);
1293 * We learned about a peer (possibly behind NAT) so run the
1294 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1295 * that peer to connect to us (connection reversal).
1297 * @param h NAT handle for us (largely used for configuration)
1298 * @param sa the address of the peer (IPv4-only)
1301 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1302 const struct sockaddr_in *sa)
1304 char inet4[INET_ADDRSTRLEN];
1305 char port_as_string[6];
1306 struct GNUNET_OS_Process *proc;
1308 if (GNUNET_YES != h->enable_nat_client)
1309 return; /* not permitted / possible */
1311 if (h->internal_address == NULL)
1313 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1315 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1318 GNUNET_assert (sa->sin_family == AF_INET);
1319 if (NULL == inet_ntop (AF_INET,
1321 inet4, INET_ADDRSTRLEN))
1323 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1326 GNUNET_snprintf (port_as_string,
1327 sizeof (port_as_string),
1331 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1333 _("Running gnunet-helper-nat-client %s %s %u\n"),
1334 h->internal_address,
1336 (unsigned int) h->adv_port);
1338 proc = GNUNET_OS_start_process (NULL,
1340 "gnunet-helper-nat-client",
1341 "gnunet-helper-nat-client",
1342 h->internal_address,
1348 /* we know that the gnunet-helper-nat-client will terminate virtually
1350 GNUNET_OS_process_wait (proc);
1351 GNUNET_OS_process_close (proc);
1356 * Test if the given address is (currently) a plausible IP address for this peer.
1358 * @param h the handle returned by register
1359 * @param addr IP address to test (IPv4 or IPv6)
1360 * @param addrlen number of bytes in addr
1361 * @return GNUNET_YES if the address is plausible,
1362 * GNUNET_NO if the address is not plausible,
1363 * GNUNET_SYSERR if the address is malformed
1366 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1370 struct LocalAddressList *pos;
1371 const struct sockaddr_in *in4;
1372 const struct sockaddr_in6 *in6;
1374 if ( (addrlen != sizeof (struct in_addr)) &&
1375 (addrlen != sizeof (struct in6_addr)) )
1378 return GNUNET_SYSERR;
1383 if (pos->addrlen == sizeof (struct sockaddr_in))
1385 in4 = (struct sockaddr_in* ) &pos[1];
1386 if ( (addrlen == sizeof (struct in_addr)) &&
1387 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))) )
1390 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1392 in6 = (struct sockaddr_in6* ) &pos[1];
1393 if ( (addrlen == sizeof (struct in6_addr)) &&
1394 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))) )
1403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1404 "Asked to validate one of my addresses and validation failed!\n");