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
102 * List of local addresses that we currently deem valid. Actual
103 * struct is followed by the 'struct sockaddr'. Note that the code
104 * intentionally makes no attempt to ensure that a particular address
105 * is only listed once (especially since it may come from different
106 * sources, and the source is an "internal" construct).
108 struct LocalAddressList
111 * This is a linked list.
113 struct LocalAddressList *next;
118 struct LocalAddressList *prev;
121 * Number of bytes of address that follow.
126 * Origin of the local address.
128 enum LocalAddressSource source;
133 * Handle for miniupnp-based NAT traversal actions.
139 * Doubly-linked list.
141 struct MiniList *next;
144 * Doubly-linked list.
146 struct MiniList *prev;
149 * Handle to mini-action.
151 struct GNUNET_NAT_MiniHandle *mini;
154 * Local port number that was mapped.
162 * Handle for active NAT registrations.
164 struct GNUNET_NAT_Handle
168 * Configuration to use.
170 const struct GNUNET_CONFIGURATION_Handle *cfg;
173 * Function to call when we learn about a new address.
175 GNUNET_NAT_AddressCallback address_callback;
178 * Function to call when we notice another peer asking for
179 * connection reversal.
181 GNUNET_NAT_ReversalCallback reversal_callback;
184 * Closure for 'callback'.
189 * Handle for (DYN)DNS lookup of our external IP.
191 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
194 * Handle for request of hostname resolution, non-NULL if pending.
196 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
199 * stdout pipe handle for the gnunet-helper-nat-server process
201 struct GNUNET_DISK_PipeHandle *server_stdout;
204 * stdout file handle (for reading) for the gnunet-helper-nat-server process
206 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
209 * Linked list of currently valid addresses (head).
211 struct LocalAddressList *lal_head;
214 * Linked list of currently valid addresses (tail).
216 struct LocalAddressList *lal_tail;
219 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
221 struct GNUNET_TIME_Relative server_retry_delay;
224 * ID of select gnunet-helper-nat-server stdout read task
226 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
229 * ID of interface IP-scan task
231 GNUNET_SCHEDULER_TaskIdentifier ifc_task;
234 * ID of hostname DNS lookup task
236 GNUNET_SCHEDULER_TaskIdentifier hostname_task;
239 * ID of DynDNS lookup task
241 GNUNET_SCHEDULER_TaskIdentifier dns_task;
244 * ID of task to add addresses from bind.
246 GNUNET_SCHEDULER_TaskIdentifier bind_task;
249 * How often do we scan for changes in our IP address from our local
252 struct GNUNET_TIME_Relative ifc_scan_frequency;
255 * How often do we scan for changes in how our hostname resolves?
257 struct GNUNET_TIME_Relative hostname_dns_frequency;
260 * How often do we scan for changes in how our external (dyndns) hostname resolves?
262 struct GNUNET_TIME_Relative dyndns_frequency;
265 * The process id of the server process (if behind NAT)
267 struct GNUNET_OS_Process *server_proc;
270 * LAN address as passed by the caller (array).
272 struct sockaddr **local_addrs;
275 * Length of the 'local_addrs'.
277 socklen_t *local_addrlens;
280 * List of handles for UPnP-traversal, one per local port (if
283 struct MiniList *mini_head;
286 * List of handles for UPnP-traversal, one per local port (if
289 struct MiniList *mini_tail;
292 * Number of entries in 'local_addrs' array.
294 unsigned int num_local_addrs;
297 * The our external address (according to config, UPnP may disagree...)
299 char *external_address;
302 * Presumably our internal address (according to config)
304 char *internal_address;
307 * Is this transport configured to be behind a NAT?
312 * Has the NAT been punched? (according to config)
317 * Is this transport configured to allow connections to NAT'd peers?
319 int enable_nat_client;
322 * Should we run the gnunet-helper-nat-server?
324 int enable_nat_server;
327 * Are we allowed to try UPnP/PMP for NAT traversal?
332 * Should we use local addresses (loopback)? (according to config)
334 int use_localaddresses;
337 * Should we do a DNS lookup of our hostname to find out our own IP?
342 * Is using IPv6 disabled?
347 * Is this TCP or UDP?
352 * Port we advertise to the outside.
360 * Try to start the gnunet-helper-nat-server (if it is not
363 * @param h handle to NAT
366 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
370 * Remove all addresses from the list of 'local' addresses
371 * that originated from the given source.
373 * @param plugin the plugin
374 * @param src source that identifies addresses to remove
377 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
378 enum LocalAddressSource src)
380 struct LocalAddressList *pos;
381 struct LocalAddressList *next;
384 while (NULL != (pos = next))
387 if (pos->source != src)
389 GNUNET_CONTAINER_DLL_remove (h->lal_head,
392 if (NULL != h->address_callback)
393 h->address_callback (h->callback_cls,
395 (const struct sockaddr* ) &pos[1],
403 * Add the given address to the list of 'local' addresses, thereby
404 * making it a 'legal' address for this peer to have.
406 * @param plugin the plugin
407 * @param src where did the local address originate from?
408 * @param arg the address, some 'struct sockaddr'
409 * @param arg_size number of bytes in arg
412 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
413 enum LocalAddressSource src,
414 const struct sockaddr *arg,
417 struct LocalAddressList *lal;
419 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
420 memcpy (&lal[1], arg, arg_size);
421 lal->addrlen = arg_size;
423 GNUNET_CONTAINER_DLL_insert (h->lal_head,
427 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
429 "Adding address `%s' from source %d\n",
430 GNUNET_a2s (arg, arg_size),
433 if (NULL != h->address_callback)
434 h->address_callback (h->callback_cls,
442 * Add the given address to the list of 'local' addresses, thereby
443 * making it a 'legal' address for this peer to have. Set the
444 * port number in the process to the advertised port and possibly
445 * also to zero (if we have the gnunet-helper-nat-server).
447 * @param plugin the plugin
448 * @param src where did the local address originate from?
449 * @param arg the address, some 'struct sockaddr'
450 * @param arg_size number of bytes in arg
453 add_to_address_list (struct GNUNET_NAT_Handle *h,
454 enum LocalAddressSource src,
455 const struct sockaddr *arg,
458 struct sockaddr_in s4;
459 const struct sockaddr_in *in4;
460 struct sockaddr_in6 s6;
461 const struct sockaddr_in6 *in6;
463 if (arg_size == sizeof (struct sockaddr_in))
465 in4 = (const struct sockaddr_in *) arg;
467 s4.sin_port = htons (h->adv_port);
468 add_to_address_list_as_is (h,
470 (const struct sockaddr*) &s4,
471 sizeof (struct sockaddr_in));
472 if (GNUNET_YES == h->enable_nat_server)
474 /* also add with PORT = 0 to indicate NAT server is enabled */
475 s4.sin_port = htons(0);
476 add_to_address_list_as_is (h,
478 (const struct sockaddr*) &s4,
479 sizeof (struct sockaddr_in));
482 else if (arg_size == sizeof (struct sockaddr_in6))
484 if (GNUNET_YES != h->disable_ipv6)
486 in6 = (const struct sockaddr_in6 *) arg;
488 s6.sin6_port = htons(h->adv_port);
489 add_to_address_list_as_is (h,
491 (const struct sockaddr*) &s6,
492 sizeof (struct sockaddr_in6));
503 * Add the given IP address to the list of 'local' addresses, thereby
504 * making it a 'legal' address for this peer to have.
506 * @param plugin the plugin
507 * @param src where did the local address originate from?
508 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
509 * @param arg_size number of bytes in arg
512 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
513 enum LocalAddressSource src,
517 struct sockaddr_in s4;
518 const struct in_addr *in4;
519 struct sockaddr_in6 s6;
520 const struct in6_addr *in6;
522 if (addrlen == sizeof (struct in_addr))
524 in4 = (const struct in_addr *) addr;
525 memset (&s4, 0, sizeof (s4));
526 s4.sin_family = AF_INET;
528 #if HAVE_SOCKADDR_IN_SIN_LEN
529 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
532 add_to_address_list (h,
534 (const struct sockaddr*) &s4,
535 sizeof (struct sockaddr_in));
536 if (GNUNET_YES == h->enable_nat_server)
538 /* also add with PORT = 0 to indicate NAT server is enabled */
539 s4.sin_port = htons(0);
540 add_to_address_list (h,
542 (const struct sockaddr*) &s4,
543 sizeof (struct sockaddr_in));
547 else if (addrlen == sizeof (struct in6_addr))
549 if (GNUNET_YES != h->disable_ipv6)
551 in6 = (const struct in6_addr *) addr;
552 memset (&s6, 0, sizeof (s6));
553 s6.sin6_family = AF_INET6;
554 s6.sin6_port = htons(h->adv_port);
555 #if HAVE_SOCKADDR_IN_SIN_LEN
556 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
559 add_to_address_list (h,
561 (const struct sockaddr*) &s6,
562 sizeof (struct sockaddr_in6));
573 * Task to do DNS lookup on our external hostname to
574 * get DynDNS-IP addresses.
576 * @param cls the NAT handle
577 * @param tc scheduler context
580 resolve_dns (void *cls,
581 const struct GNUNET_SCHEDULER_TaskContext *tc);
585 * Our (external) hostname was resolved and the configuration says that
586 * the NAT was hole-punched.
588 * @param cls the 'struct Plugin'
589 * @param addr NULL on error, otherwise result of DNS lookup
590 * @param addrlen number of bytes in addr
593 process_external_ip (void *cls,
594 const struct sockaddr *addr,
597 struct GNUNET_NAT_Handle *h = cls;
598 struct in_addr dummy;
603 if (1 == inet_pton (AF_INET,
606 return; /* repated lookup pointless: was numeric! */
607 h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
611 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
616 * Task to do a lookup on our hostname for IP addresses.
618 * @param cls the NAT handle
619 * @param tc scheduler context
622 resolve_hostname (void *cls,
623 const struct GNUNET_SCHEDULER_TaskContext *tc);
627 * Function called by the resolver for each address obtained from DNS
628 * for our own hostname. Add the addresses to the list of our IP
632 * @param addr one of the addresses of the host, NULL for the last address
633 * @param addrlen length of the address
636 process_hostname_ip (void *cls,
637 const struct sockaddr *addr, socklen_t addrlen)
639 struct GNUNET_NAT_Handle *h = cls;
643 h->hostname_dns = NULL;
644 h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
645 &resolve_hostname, h);
648 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
653 * Add the IP of our network interface to the list of
656 * @param cls the 'struct GNUNET_NAT_Handle'
657 * @param name name of the interface
658 * @param isDefault do we think this may be our default interface
659 * @param addr address of the interface
660 * @param addrlen number of bytes in addr
661 * @return GNUNET_OK to continue iterating
664 process_interfaces (void *cls,
667 const struct sockaddr *addr, socklen_t addrlen)
669 struct GNUNET_NAT_Handle *h = cls;
670 const struct sockaddr_in *s4;
671 const struct sockaddr_in6 *s6;
673 char buf[INET6_ADDRSTRLEN];
675 switch (addr->sa_family)
678 s4 = (struct sockaddr_in *) addr;
680 if (GNUNET_YES == h->use_localaddresses)
681 add_ip_to_address_list (h,
682 LAL_INTERFACE_ADDRESS,
684 sizeof (struct in_addr));
687 s6 = (struct sockaddr_in6 *) addr;
688 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
690 /* skip link local addresses */
694 if (GNUNET_YES == h->use_localaddresses)
695 add_ip_to_address_list (h,
696 LAL_INTERFACE_ADDRESS,
698 sizeof (struct in6_addr));
704 if ( (h->internal_address == NULL) &&
705 (h->server_proc == NULL) &&
706 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
707 (GNUNET_YES == isDefault) &&
708 ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) )
710 /* no internal address configured, but we found a "default"
711 interface, try using that as our 'internal' address */
712 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
716 start_gnunet_nat_server (h);
723 * Task that restarts the gnunet-helper-nat-server process after a crash
724 * after a certain delay.
726 * @param cls the 'struct GNUNET_NAT_Handle'
727 * @param tc scheduler context
730 restart_nat_server (void *cls,
731 const struct GNUNET_SCHEDULER_TaskContext *tc)
733 struct GNUNET_NAT_Handle *h = cls;
735 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
736 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
738 start_gnunet_nat_server (h);
743 * We have been notified that gnunet-helper-nat-server has written something to stdout.
744 * Handle the output, then reschedule this function to be called again once
747 * @param cls the NAT handle
748 * @param tc the scheduling context
751 nat_server_read (void *cls,
752 const struct GNUNET_SCHEDULER_TaskContext *tc)
754 struct GNUNET_NAT_Handle *h = cls;
759 const char *port_start;
760 struct sockaddr_in sin_addr;
762 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
763 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
765 memset (mybuf, 0, sizeof(mybuf));
766 bytes = GNUNET_DISK_file_read(h->server_stdout_handle,
772 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
774 "Finished reading from server stdout with code: %d\n",
777 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
778 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
779 GNUNET_OS_process_wait (h->server_proc);
780 GNUNET_OS_process_close (h->server_proc);
781 h->server_proc = NULL;
782 GNUNET_DISK_pipe_close (h->server_stdout);
783 h->server_stdout = NULL;
784 h->server_stdout_handle = NULL;
785 /* now try to restart it */
786 h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
787 h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
788 h->server_retry_delay);
789 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
796 for (i = 0; i < sizeof(mybuf); i++)
798 if (mybuf[i] == '\n')
803 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
806 port_start = &mybuf[i + 1];
810 /* construct socket address of sender */
811 memset (&sin_addr, 0, sizeof (sin_addr));
812 sin_addr.sin_family = AF_INET;
813 #if HAVE_SOCKADDR_IN_SIN_LEN
814 sin_addr.sin_len = sizeof (sin_addr);
816 if ( (NULL == port_start) ||
817 (1 != sscanf (port_start, "%d", &port)) ||
818 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
820 /* should we restart gnunet-helper-nat-server? */
821 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
823 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
826 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
827 h->server_stdout_handle,
832 sin_addr.sin_port = htons((uint16_t) port);
834 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
836 "gnunet-helper-nat-server read: %s:%d\n",
839 h->reversal_callback (h->callback_cls,
840 (const struct sockaddr*) &sin_addr,
842 h->server_read_task =
843 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
844 h->server_stdout_handle,
851 * Try to start the gnunet-helper-nat-server (if it is not
854 * @param h handle to NAT
857 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
859 if ( (h->behind_nat == GNUNET_YES) &&
860 (h->enable_nat_server == GNUNET_YES) &&
861 (h->internal_address != NULL) &&
862 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
867 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
869 "Starting %s at `%s'\n",
870 "gnunet-helper-nat-server",
871 h->internal_address);
873 /* Start the server process */
874 h->server_proc = GNUNET_OS_start_process (NULL,
876 "gnunet-helper-nat-server",
877 "gnunet-helper-nat-server",
880 if (h->server_proc == NULL)
882 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
884 _("Failed to start %s\n"),
885 "gnunet-helper-nat-server");
886 GNUNET_DISK_pipe_close (h->server_stdout);
887 h->server_stdout = NULL;
891 /* Close the write end of the read pipe */
892 GNUNET_DISK_pipe_close_end(h->server_stdout,
893 GNUNET_DISK_PIPE_END_WRITE);
894 h->server_stdout_handle
895 = GNUNET_DISK_pipe_handle (h->server_stdout,
896 GNUNET_DISK_PIPE_END_READ);
898 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
899 h->server_stdout_handle,
908 * Task to scan the local network interfaces for IP addresses.
910 * @param cls the NAT handle
911 * @param tc scheduler context
914 list_interfaces (void *cls,
915 const struct GNUNET_SCHEDULER_TaskContext *tc)
917 struct GNUNET_NAT_Handle *h = cls;
919 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
920 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
921 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
922 h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
923 &list_interfaces, h);
928 * Task to do a lookup on our hostname for IP addresses.
930 * @param cls the NAT handle
931 * @param tc scheduler context
934 resolve_hostname (void *cls,
935 const struct GNUNET_SCHEDULER_TaskContext *tc)
937 struct GNUNET_NAT_Handle *h = cls;
939 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
940 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
941 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
942 HOSTNAME_RESOLVE_TIMEOUT,
943 &process_hostname_ip,
949 * Task to do DNS lookup on our external hostname to
950 * get DynDNS-IP addresses.
952 * @param cls the NAT handle
953 * @param tc scheduler context
956 resolve_dns (void *cls,
957 const struct GNUNET_SCHEDULER_TaskContext *tc)
959 struct GNUNET_NAT_Handle *h = cls;
961 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
962 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
963 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
965 GNUNET_TIME_UNIT_MINUTES,
966 &process_external_ip,
972 * Add or remove UPnP-mapped addresses.
974 * @param cls the GNUNET_NAT_Handle
975 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
976 * the previous (now invalid) one
977 * @param addr either the previous or the new public IP address
978 * @param addrlen actual lenght of the address
983 const struct sockaddr *addr,
986 struct GNUNET_NAT_Handle *h = cls;
987 struct LocalAddressList *pos;
988 struct LocalAddressList *next;
990 if (GNUNET_YES == add_remove)
992 add_to_address_list (h,
999 while (NULL != (pos = next))
1002 if ( (pos->source != LAL_UPNP) ||
1003 (pos->addrlen != addrlen) ||
1004 (0 != memcmp (&pos[1],
1008 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1011 if (NULL != h->address_callback)
1012 h->address_callback (h->callback_cls,
1014 (const struct sockaddr* ) &pos[1],
1017 return; /* only remove once */
1019 /* asked to remove address that does not exist */
1025 * Try to add a port mapping using UPnP.
1027 * @param h overall NAT handle
1028 * @param port port to map with UPnP
1031 add_minis (struct GNUNET_NAT_Handle *h,
1034 struct MiniList *ml;
1039 if (port == ml->port)
1040 return; /* already got this port */
1043 ml = GNUNET_malloc (sizeof (struct MiniList));
1045 ml->mini = GNUNET_NAT_mini_map_start (port,
1048 GNUNET_CONTAINER_DLL_insert (h->mini_head,
1055 * Task to add addresses from original bind to set of valid addrs.
1057 * @param cls the NAT handle
1058 * @param tc scheduler context
1061 add_from_bind (void *cls,
1062 const struct GNUNET_SCHEDULER_TaskContext *tc)
1064 static struct in6_addr any = IN6ADDR_ANY_INIT;
1065 struct GNUNET_NAT_Handle *h = cls;
1067 struct sockaddr *sa;
1068 const struct sockaddr_in *v4;
1070 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1071 for (i=0;i<h->num_local_addrs;i++)
1073 sa = h->local_addrs[i];
1074 switch (sa->sa_family)
1077 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1082 v4 = (const struct sockaddr_in*) sa;
1083 if (0 != v4->sin_addr.s_addr)
1084 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, sizeof (struct sockaddr_in));
1086 add_minis (h, ntohs (v4->sin_port));
1089 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1094 if (0 != memcmp (&((const struct sockaddr_in6*) sa)->sin6_addr,
1096 sizeof (struct in6_addr)))
1097 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, sizeof (struct sockaddr_in6));
1108 * Attempt to enable port redirection and detect public IP address contacting
1109 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1110 * of the local host's addresses should the external port be mapped. The port
1111 * is taken from the corresponding sockaddr_in[6] field.
1113 * @param cfg configuration to use
1114 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1115 * @param adv_port advertised port (port we are either bound to or that our OS
1116 * locally performs redirection from to our bound port).
1117 * @param num_addrs number of addresses in 'addrs'
1118 * @param addr the local address packets should be redirected to
1119 * @param addrlen actual lenght of the address
1120 * @param address_callback function to call everytime the public IP address changes
1121 * @param reversal_callback function to call if someone wants connection reversal from us
1122 * @param callback_cls closure for callbacks
1123 * @return NULL on error, otherwise handle that can be used to unregister
1125 struct GNUNET_NAT_Handle *
1126 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1129 unsigned int num_addrs,
1130 const struct sockaddr **addrs,
1131 const socklen_t *addrlens,
1132 GNUNET_NAT_AddressCallback address_callback,
1133 GNUNET_NAT_ReversalCallback reversal_callback,
1136 struct GNUNET_NAT_Handle *h;
1137 struct in_addr in_addr;
1141 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1143 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1144 (unsigned int) adv_port,
1147 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1148 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1151 h->address_callback = address_callback;
1152 h->reversal_callback = reversal_callback;
1153 h->callback_cls = callback_cls;
1154 h->num_local_addrs = num_addrs;
1155 h->adv_port = adv_port;
1158 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr*));
1159 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1160 for (i=0;i<num_addrs;i++)
1162 GNUNET_assert (addrlens[i] > 0);
1163 GNUNET_assert (addrs[i] != NULL);
1164 h->local_addrlens[i] = addrlens[i];
1165 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1166 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1169 h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1171 GNUNET_CONFIGURATION_have_value (cfg,
1173 "INTERNAL_ADDRESS"))
1175 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1178 &h->internal_address);
1180 if ( (h->internal_address != NULL) &&
1181 (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) )
1183 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1185 _("Malformed %s `%s' given in configuration!\n"),
1187 h->internal_address);
1188 GNUNET_free (h->internal_address);
1189 h->internal_address = NULL;
1193 GNUNET_CONFIGURATION_have_value (cfg,
1195 "EXTERNAL_ADDRESS"))
1197 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1200 &h->external_address);
1202 if ( (h->external_address != NULL) &&
1203 (inet_pton(AF_INET, h->external_address, &in_addr) != 1) )
1205 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1207 _("Malformed %s `%s' given in configuration!\n"),
1209 h->external_address);
1210 GNUNET_free (h->external_address);
1211 h->external_address = NULL;
1213 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1216 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1219 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1221 "ENABLE_NAT_CLIENT");
1222 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1224 "ENABLE_NAT_SERVER");
1225 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1228 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1231 h->use_hostname = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1234 h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno(cfg,
1238 GNUNET_CONFIGURATION_get_value_time (cfg,
1241 &h->dyndns_frequency))
1242 h->dyndns_frequency = DYNDNS_FREQUENCY;
1244 GNUNET_CONFIGURATION_get_value_time (cfg,
1246 "IFC_SCAN_FREQUENCY",
1247 &h->ifc_scan_frequency))
1248 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1250 GNUNET_CONFIGURATION_get_value_time (cfg,
1252 "HOSTNAME_DNS_FREQUENCY",
1253 &h->hostname_dns_frequency))
1254 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1256 if (NULL == reversal_callback)
1257 h->enable_nat_server = GNUNET_NO;
1259 /* Check if NAT was hole-punched */
1260 if ( (NULL != h->address_callback) &&
1261 (h->external_address != NULL) &&
1262 (h->nat_punched == GNUNET_YES) )
1264 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1265 h->enable_nat_server = GNUNET_NO;
1266 h->enable_upnp = GNUNET_NO;
1269 /* Test for SUID binaries */
1270 if ( (h->behind_nat == GNUNET_YES) &&
1271 (GNUNET_YES == h->enable_nat_server) &&
1272 (GNUNET_YES != GNUNET_OS_check_helper_binary("gnunet-helper-nat-server")) )
1274 h->enable_nat_server = GNUNET_NO;
1275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1276 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1277 "gnunet-helper-nat-server");
1279 if ( (GNUNET_YES == h->enable_nat_client) &&
1280 (GNUNET_YES != GNUNET_OS_check_helper_binary("gnunet-helper-nat-client")) )
1282 h->enable_nat_client = GNUNET_NO;
1283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1285 "gnunet-helper-nat-client");
1288 start_gnunet_nat_server (h);
1290 /* FIXME: add support for UPnP, etc */
1292 if (NULL != h->address_callback)
1294 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1295 if (GNUNET_YES == h->use_hostname)
1296 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1304 * Stop port redirection and public IP address detection for the given handle.
1305 * This frees the handle, after having sent the needed commands to close open ports.
1307 * @param h the handle to stop
1310 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1313 struct LocalAddressList *lal;
1314 struct MiniList *ml;
1316 while (NULL != (ml = h->mini_head))
1318 GNUNET_CONTAINER_DLL_remove (h->mini_head,
1321 if (NULL != ml->mini)
1322 GNUNET_NAT_mini_map_stop (ml->mini);
1325 if (h->ext_dns != NULL)
1327 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1330 if (NULL != h->hostname_dns)
1332 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1333 h->hostname_dns = NULL;
1335 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1337 GNUNET_SCHEDULER_cancel (h->server_read_task);
1338 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1340 if (GNUNET_SCHEDULER_NO_TASK != h->bind_task)
1342 GNUNET_SCHEDULER_cancel (h->bind_task);
1343 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1345 if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1347 GNUNET_SCHEDULER_cancel (h->ifc_task);
1348 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1350 if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1352 GNUNET_SCHEDULER_cancel (h->hostname_task);
1353 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1355 if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1357 GNUNET_SCHEDULER_cancel (h->dns_task);
1358 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1360 if (NULL != h->server_proc)
1362 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1363 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1364 GNUNET_OS_process_wait (h->server_proc);
1365 GNUNET_OS_process_close (h->server_proc);
1366 h->server_proc = NULL;
1367 GNUNET_DISK_pipe_close (h->server_stdout);
1368 h->server_stdout = NULL;
1369 h->server_stdout_handle = NULL;
1371 if (NULL != h->server_stdout)
1373 GNUNET_DISK_pipe_close (h->server_stdout);
1374 h->server_stdout = NULL;
1375 h->server_stdout_handle = NULL;
1377 while (NULL != (lal = h->lal_head))
1379 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1382 if (NULL != h->address_callback)
1383 h->address_callback (h->callback_cls,
1385 (const struct sockaddr*) &lal[1],
1389 for (i=0;i<h->num_local_addrs;i++)
1390 GNUNET_free (h->local_addrs[i]);
1391 GNUNET_free_non_null (h->local_addrs);
1392 GNUNET_free_non_null (h->local_addrlens);
1393 GNUNET_free_non_null (h->external_address);
1394 GNUNET_free_non_null (h->internal_address);
1400 * We learned about a peer (possibly behind NAT) so run the
1401 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1402 * that peer to connect to us (connection reversal).
1404 * @param h NAT handle for us (largely used for configuration)
1405 * @param sa the address of the peer (IPv4-only)
1408 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1409 const struct sockaddr_in *sa)
1411 char inet4[INET_ADDRSTRLEN];
1412 char port_as_string[6];
1413 struct GNUNET_OS_Process *proc;
1415 if (GNUNET_YES != h->enable_nat_client)
1416 return; /* not permitted / possible */
1418 if (h->internal_address == NULL)
1420 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1422 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1425 GNUNET_assert (sa->sin_family == AF_INET);
1426 if (NULL == inet_ntop (AF_INET,
1428 inet4, INET_ADDRSTRLEN))
1430 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1433 GNUNET_snprintf (port_as_string,
1434 sizeof (port_as_string),
1438 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1440 _("Running gnunet-helper-nat-client %s %s %u\n"),
1441 h->internal_address,
1443 (unsigned int) h->adv_port);
1445 proc = GNUNET_OS_start_process (NULL,
1447 "gnunet-helper-nat-client",
1448 "gnunet-helper-nat-client",
1449 h->internal_address,
1455 /* we know that the gnunet-helper-nat-client will terminate virtually
1457 GNUNET_OS_process_wait (proc);
1458 GNUNET_OS_process_close (proc);
1463 * Test if the given address is (currently) a plausible IP address for this peer.
1465 * @param h the handle returned by register
1466 * @param addr IP address to test (IPv4 or IPv6)
1467 * @param addrlen number of bytes in addr
1468 * @return GNUNET_YES if the address is plausible,
1469 * GNUNET_NO if the address is not plausible,
1470 * GNUNET_SYSERR if the address is malformed
1473 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1477 struct LocalAddressList *pos;
1478 const struct sockaddr_in *in4;
1479 const struct sockaddr_in6 *in6;
1481 if ( (addrlen != sizeof (struct in_addr)) &&
1482 (addrlen != sizeof (struct in6_addr)) )
1485 return GNUNET_SYSERR;
1490 if (pos->addrlen == sizeof (struct sockaddr_in))
1492 in4 = (struct sockaddr_in* ) &pos[1];
1493 if ( (addrlen == sizeof (struct in_addr)) &&
1494 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))) )
1497 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1499 in6 = (struct sockaddr_in6* ) &pos[1];
1500 if ( (addrlen == sizeof (struct in6_addr)) &&
1501 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))) )
1510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1511 "Asked to validate one of my addresses and validation failed!\n");