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 trying to resolve our own hostname?
56 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
60 * Where did the given local address originate from?
61 * To be used for debugging as well as in the future
62 * to remove all addresses from a certain source when
63 * we reevaluate the source.
65 enum LocalAddressSource
68 * Address was obtained by DNS resolution of the external hostname
69 * given in the configuration (i.e. hole-punched DynDNS setup).
74 * Address was obtained by looking up our own hostname in DNS.
79 * Address was obtained by scanning our hosts's network interfaces
80 * and taking their address (no DNS involved).
82 LAL_INTERFACE_ADDRESS,
85 * Addresses we were explicitly bound to.
90 * Addresses from UPnP or PMP
103 * List of local addresses that we currently deem valid. Actual
104 * struct is followed by the 'struct sockaddr'. Note that the code
105 * intentionally makes no attempt to ensure that a particular address
106 * is only listed once (especially since it may come from different
107 * sources, and the source is an "internal" construct).
109 struct LocalAddressList
112 * This is a linked list.
114 struct LocalAddressList *next;
119 struct LocalAddressList *prev;
122 * Number of bytes of address that follow.
127 * Origin of the local address.
129 enum LocalAddressSource source;
134 * Handle for miniupnp-based NAT traversal actions.
140 * Doubly-linked list.
142 struct MiniList *next;
145 * Doubly-linked list.
147 struct MiniList *prev;
150 * Handle to mini-action.
152 struct GNUNET_NAT_MiniHandle *mini;
155 * Local port number that was mapped.
163 * Handle for active NAT registrations.
165 struct GNUNET_NAT_Handle
169 * Configuration to use.
171 const struct GNUNET_CONFIGURATION_Handle *cfg;
174 * Function to call when we learn about a new address.
176 GNUNET_NAT_AddressCallback address_callback;
179 * Function to call when we notice another peer asking for
180 * connection reversal.
182 GNUNET_NAT_ReversalCallback reversal_callback;
185 * Closure for 'callback'.
190 * Handle for (DYN)DNS lookup of our external IP.
192 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
195 * Handle for request of hostname resolution, non-NULL if pending.
197 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
200 * stdout pipe handle for the gnunet-helper-nat-server process
202 struct GNUNET_DISK_PipeHandle *server_stdout;
205 * stdout file handle (for reading) for the gnunet-helper-nat-server process
207 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
210 * Linked list of currently valid addresses (head).
212 struct LocalAddressList *lal_head;
215 * Linked list of currently valid addresses (tail).
217 struct LocalAddressList *lal_tail;
220 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
222 struct GNUNET_TIME_Relative server_retry_delay;
225 * ID of select gnunet-helper-nat-server stdout read task
227 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
230 * ID of interface IP-scan task
232 GNUNET_SCHEDULER_TaskIdentifier ifc_task;
235 * ID of hostname DNS lookup task
237 GNUNET_SCHEDULER_TaskIdentifier hostname_task;
240 * ID of DynDNS lookup task
242 GNUNET_SCHEDULER_TaskIdentifier dns_task;
245 * ID of task to add addresses from bind.
247 GNUNET_SCHEDULER_TaskIdentifier bind_task;
250 * How often do we scan for changes in our IP address from our local
253 struct GNUNET_TIME_Relative ifc_scan_frequency;
256 * How often do we scan for changes in how our hostname resolves?
258 struct GNUNET_TIME_Relative hostname_dns_frequency;
261 * How often do we scan for changes in how our external (dyndns) hostname resolves?
263 struct GNUNET_TIME_Relative dyndns_frequency;
266 * The process id of the server process (if behind NAT)
268 struct GNUNET_OS_Process *server_proc;
271 * LAN address as passed by the caller (array).
273 struct sockaddr **local_addrs;
276 * Length of the 'local_addrs'.
278 socklen_t *local_addrlens;
281 * List of handles for UPnP-traversal, one per local port (if
284 struct MiniList *mini_head;
287 * List of handles for UPnP-traversal, one per local port (if
290 struct MiniList *mini_tail;
293 * Number of entries in 'local_addrs' array.
295 unsigned int num_local_addrs;
298 * The our external address (according to config, UPnP may disagree...)
300 char *external_address;
303 * Presumably our internal address (according to config)
305 char *internal_address;
308 * Is this transport configured to be behind a NAT?
313 * Has the NAT been punched? (according to config)
318 * Is this transport configured to allow connections to NAT'd peers?
320 int enable_nat_client;
323 * Should we run the gnunet-helper-nat-server?
325 int enable_nat_server;
328 * Are we allowed to try UPnP/PMP for NAT traversal?
333 * Should we use local addresses (loopback)? (according to config)
335 int use_localaddresses;
338 * Should we do a DNS lookup of our hostname to find out our own IP?
343 * Is using IPv6 disabled?
348 * Is this TCP or UDP?
353 * Port we advertise to the outside.
361 * Try to start the gnunet-helper-nat-server (if it is not
364 * @param h handle to NAT
367 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
371 * Remove all addresses from the list of 'local' addresses
372 * that originated from the given source.
374 * @param plugin the plugin
375 * @param src source that identifies addresses to remove
378 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
379 enum LocalAddressSource src)
381 struct LocalAddressList *pos;
382 struct LocalAddressList *next;
385 while (NULL != (pos = next))
388 if (pos->source != src)
390 GNUNET_CONTAINER_DLL_remove (h->lal_head,
393 if (NULL != h->address_callback)
394 h->address_callback (h->callback_cls,
396 (const struct sockaddr* ) &pos[1],
404 * Add the given address to the list of 'local' addresses, thereby
405 * making it a 'legal' address for this peer to have.
407 * @param plugin the plugin
408 * @param src where did the local address originate from?
409 * @param arg the address, some 'struct sockaddr'
410 * @param arg_size number of bytes in arg
413 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
414 enum LocalAddressSource src,
415 const struct sockaddr *arg,
418 struct LocalAddressList *lal;
420 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
421 memcpy (&lal[1], arg, arg_size);
422 lal->addrlen = arg_size;
424 GNUNET_CONTAINER_DLL_insert (h->lal_head,
427 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
429 "Adding address `%s' from source %d\n",
430 GNUNET_a2s (arg, arg_size),
432 if (NULL != h->address_callback)
433 h->address_callback (h->callback_cls,
441 * Add the given address to the list of 'local' addresses, thereby
442 * making it a 'legal' address for this peer to have. Set the
443 * port number in the process to the advertised port and possibly
444 * also to zero (if we have the gnunet-helper-nat-server).
446 * @param plugin the plugin
447 * @param src where did the local address originate from?
448 * @param arg the address, some 'struct sockaddr'
449 * @param arg_size number of bytes in arg
452 add_to_address_list (struct GNUNET_NAT_Handle *h,
453 enum LocalAddressSource src,
454 const struct sockaddr *arg,
457 struct sockaddr_in s4;
458 const struct sockaddr_in *in4;
459 struct sockaddr_in6 s6;
460 const struct sockaddr_in6 *in6;
462 if (arg_size == sizeof (struct sockaddr_in))
464 in4 = (const struct sockaddr_in *) arg;
466 s4.sin_port = htons (h->adv_port);
467 add_to_address_list_as_is (h,
469 (const struct sockaddr*) &s4,
470 sizeof (struct sockaddr_in));
471 if (GNUNET_YES == h->enable_nat_server)
473 /* also add with PORT = 0 to indicate NAT server is enabled */
474 s4.sin_port = htons(0);
475 add_to_address_list_as_is (h,
477 (const struct sockaddr*) &s4,
478 sizeof (struct sockaddr_in));
481 else if (arg_size == sizeof (struct sockaddr_in6))
483 if (GNUNET_YES != h->disable_ipv6)
485 in6 = (const struct sockaddr_in6 *) arg;
487 s6.sin6_port = htons(h->adv_port);
488 add_to_address_list_as_is (h,
490 (const struct sockaddr*) &s6,
491 sizeof (struct sockaddr_in6));
502 * Add the given IP address to the list of 'local' addresses, thereby
503 * making it a 'legal' address for this peer to have.
505 * @param plugin the plugin
506 * @param src where did the local address originate from?
507 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
508 * @param arg_size number of bytes in arg
511 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
512 enum LocalAddressSource src,
516 struct sockaddr_in s4;
517 const struct in_addr *in4;
518 struct sockaddr_in6 s6;
519 const struct in6_addr *in6;
521 if (addrlen == sizeof (struct in_addr))
523 in4 = (const struct in_addr *) addr;
524 memset (&s4, 0, sizeof (s4));
525 s4.sin_family = AF_INET;
527 #if HAVE_SOCKADDR_IN_SIN_LEN
528 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
531 add_to_address_list (h,
533 (const struct sockaddr*) &s4,
534 sizeof (struct sockaddr_in));
535 if (GNUNET_YES == h->enable_nat_server)
537 /* also add with PORT = 0 to indicate NAT server is enabled */
538 s4.sin_port = htons(0);
539 add_to_address_list (h,
541 (const struct sockaddr*) &s4,
542 sizeof (struct sockaddr_in));
546 else if (addrlen == sizeof (struct in6_addr))
548 if (GNUNET_YES != h->disable_ipv6)
550 in6 = (const struct in6_addr *) addr;
551 memset (&s6, 0, sizeof (s6));
552 s6.sin6_family = AF_INET6;
553 s6.sin6_port = htons(h->adv_port);
554 #if HAVE_SOCKADDR_IN_SIN_LEN
555 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
558 add_to_address_list (h,
560 (const struct sockaddr*) &s6,
561 sizeof (struct sockaddr_in6));
572 * Task to do DNS lookup on our external hostname to
573 * get DynDNS-IP addresses.
575 * @param cls the NAT handle
576 * @param tc scheduler context
579 resolve_dns (void *cls,
580 const struct GNUNET_SCHEDULER_TaskContext *tc);
584 * Our (external) hostname was resolved and the configuration says that
585 * the NAT was hole-punched.
587 * @param cls the 'struct Plugin'
588 * @param addr NULL on error, otherwise result of DNS lookup
589 * @param addrlen number of bytes in addr
592 process_external_ip (void *cls,
593 const struct sockaddr *addr,
596 struct GNUNET_NAT_Handle *h = cls;
597 struct in_addr dummy;
602 if (1 == inet_pton (AF_INET,
605 return; /* repated lookup pointless: was numeric! */
606 h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
610 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
615 * Task to do a lookup on our hostname for IP addresses.
617 * @param cls the NAT handle
618 * @param tc scheduler context
621 resolve_hostname (void *cls,
622 const struct GNUNET_SCHEDULER_TaskContext *tc);
626 * Function called by the resolver for each address obtained from DNS
627 * for our own hostname. Add the addresses to the list of our IP
631 * @param addr one of the addresses of the host, NULL for the last address
632 * @param addrlen length of the address
635 process_hostname_ip (void *cls,
636 const struct sockaddr *addr, socklen_t addrlen)
638 struct GNUNET_NAT_Handle *h = cls;
642 h->hostname_dns = NULL;
643 h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
644 &resolve_hostname, h);
647 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
652 * Add the IP of our network interface to the list of
655 * @param cls the 'struct GNUNET_NAT_Handle'
656 * @param name name of the interface
657 * @param isDefault do we think this may be our default interface
658 * @param addr address of the interface
659 * @param addrlen number of bytes in addr
660 * @return GNUNET_OK to continue iterating
663 process_interfaces (void *cls,
666 const struct sockaddr *addr, socklen_t addrlen)
668 struct GNUNET_NAT_Handle *h = cls;
669 const struct sockaddr_in *s4;
670 const struct sockaddr_in6 *s6;
672 char buf[INET6_ADDRSTRLEN];
674 switch (addr->sa_family)
677 s4 = (struct sockaddr_in *) addr;
679 if (GNUNET_YES == h->use_localaddresses)
680 add_ip_to_address_list (h,
681 LAL_INTERFACE_ADDRESS,
683 sizeof (struct in_addr));
686 s6 = (struct sockaddr_in6 *) addr;
687 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
689 /* skip link local addresses */
693 if (GNUNET_YES == h->use_localaddresses)
694 add_ip_to_address_list (h,
695 LAL_INTERFACE_ADDRESS,
697 sizeof (struct in6_addr));
703 if ( (h->internal_address == NULL) &&
704 (h->server_proc == NULL) &&
705 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
706 (GNUNET_YES == isDefault) &&
707 ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) )
709 /* no internal address configured, but we found a "default"
710 interface, try using that as our 'internal' address */
711 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
715 start_gnunet_nat_server (h);
722 * Task that restarts the gnunet-helper-nat-server process after a crash
723 * after a certain delay.
725 * @param cls the 'struct GNUNET_NAT_Handle'
726 * @param tc scheduler context
729 restart_nat_server (void *cls,
730 const struct GNUNET_SCHEDULER_TaskContext *tc)
732 struct GNUNET_NAT_Handle *h = cls;
734 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
735 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
737 start_gnunet_nat_server (h);
742 * We have been notified that gnunet-helper-nat-server has written something to stdout.
743 * Handle the output, then reschedule this function to be called again once
746 * @param cls the NAT handle
747 * @param tc the scheduling context
750 nat_server_read (void *cls,
751 const struct GNUNET_SCHEDULER_TaskContext *tc)
753 struct GNUNET_NAT_Handle *h = cls;
758 const char *port_start;
759 struct sockaddr_in sin_addr;
761 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
762 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
764 memset (mybuf, 0, sizeof(mybuf));
765 bytes = GNUNET_DISK_file_read(h->server_stdout_handle,
771 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
773 "Finished reading from server stdout with code: %d\n",
776 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
777 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
778 GNUNET_OS_process_wait (h->server_proc);
779 GNUNET_OS_process_close (h->server_proc);
780 h->server_proc = NULL;
781 GNUNET_DISK_pipe_close (h->server_stdout);
782 h->server_stdout = NULL;
783 h->server_stdout_handle = NULL;
784 /* now try to restart it */
785 h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
786 h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
787 h->server_retry_delay);
788 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
795 for (i = 0; i < sizeof(mybuf); i++)
797 if (mybuf[i] == '\n')
802 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
805 port_start = &mybuf[i + 1];
809 /* construct socket address of sender */
810 memset (&sin_addr, 0, sizeof (sin_addr));
811 sin_addr.sin_family = AF_INET;
812 #if HAVE_SOCKADDR_IN_SIN_LEN
813 sin_addr.sin_len = sizeof (sin_addr);
815 if ( (NULL == port_start) ||
816 (1 != sscanf (port_start, "%d", &port)) ||
817 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
819 /* should we restart gnunet-helper-nat-server? */
820 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
822 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
825 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
826 h->server_stdout_handle,
831 sin_addr.sin_port = htons((uint16_t) port);
833 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
835 "gnunet-helper-nat-server read: %s:%d\n",
838 h->reversal_callback (h->callback_cls,
839 (const struct sockaddr*) &sin_addr,
841 h->server_read_task =
842 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
843 h->server_stdout_handle,
850 * Try to start the gnunet-helper-nat-server (if it is not
853 * @param h handle to NAT
856 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
858 if ( (h->behind_nat == GNUNET_YES) &&
859 (h->enable_nat_server == GNUNET_YES) &&
860 (h->internal_address != NULL) &&
861 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
866 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
868 "Starting %s at `%s'\n",
869 "gnunet-helper-nat-server",
870 h->internal_address);
872 /* Start the server process */
873 h->server_proc = GNUNET_OS_start_process (NULL,
875 "gnunet-helper-nat-server",
876 "gnunet-helper-nat-server",
879 if (h->server_proc == NULL)
881 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
883 _("Failed to start %s\n"),
884 "gnunet-helper-nat-server");
885 GNUNET_DISK_pipe_close (h->server_stdout);
886 h->server_stdout = NULL;
890 /* Close the write end of the read pipe */
891 GNUNET_DISK_pipe_close_end(h->server_stdout,
892 GNUNET_DISK_PIPE_END_WRITE);
893 h->server_stdout_handle
894 = GNUNET_DISK_pipe_handle (h->server_stdout,
895 GNUNET_DISK_PIPE_END_READ);
897 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
898 h->server_stdout_handle,
907 * Task to scan the local network interfaces for IP addresses.
909 * @param cls the NAT handle
910 * @param tc scheduler context
913 list_interfaces (void *cls,
914 const struct GNUNET_SCHEDULER_TaskContext *tc)
916 struct GNUNET_NAT_Handle *h = cls;
918 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
919 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
920 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
921 h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
922 &list_interfaces, h);
927 * Task to do a lookup on our hostname for IP addresses.
929 * @param cls the NAT handle
930 * @param tc scheduler context
933 resolve_hostname (void *cls,
934 const struct GNUNET_SCHEDULER_TaskContext *tc)
936 struct GNUNET_NAT_Handle *h = cls;
938 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
939 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
940 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
941 HOSTNAME_RESOLVE_TIMEOUT,
942 &process_hostname_ip,
948 * Task to do DNS lookup on our external hostname to
949 * get DynDNS-IP addresses.
951 * @param cls the NAT handle
952 * @param tc scheduler context
955 resolve_dns (void *cls,
956 const struct GNUNET_SCHEDULER_TaskContext *tc)
958 struct GNUNET_NAT_Handle *h = cls;
960 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
961 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
962 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
964 GNUNET_TIME_UNIT_MINUTES,
965 &process_external_ip,
971 * Add or remove UPnP-mapped addresses.
973 * @param cls the GNUNET_NAT_Handle
974 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
975 * the previous (now invalid) one
976 * @param addr either the previous or the new public IP address
977 * @param addrlen actual lenght of the address
982 const struct sockaddr *addr,
985 struct GNUNET_NAT_Handle *h = cls;
986 struct LocalAddressList *pos;
987 struct LocalAddressList *next;
989 if (GNUNET_YES == add_remove)
991 add_to_address_list (h,
998 while (NULL != (pos = next))
1001 if ( (pos->source != LAL_UPNP) ||
1002 (pos->addrlen != addrlen) ||
1003 (0 != memcmp (&pos[1],
1007 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1010 if (NULL != h->address_callback)
1011 h->address_callback (h->callback_cls,
1013 (const struct sockaddr* ) &pos[1],
1016 return; /* only remove once */
1018 /* asked to remove address that does not exist */
1024 * Try to add a port mapping using UPnP.
1026 * @param h overall NAT handle
1027 * @param port port to map with UPnP
1030 add_minis (struct GNUNET_NAT_Handle *h,
1033 struct MiniList *ml;
1038 if (port == ml->port)
1039 return; /* already got this port */
1042 ml = GNUNET_malloc (sizeof (struct MiniList));
1044 ml->mini = GNUNET_NAT_mini_map_start (port,
1047 GNUNET_CONTAINER_DLL_insert (h->mini_head,
1054 * Task to add addresses from original bind to set of valid addrs.
1056 * @param cls the NAT handle
1057 * @param tc scheduler context
1060 add_from_bind (void *cls,
1061 const struct GNUNET_SCHEDULER_TaskContext *tc)
1063 static struct in6_addr any = IN6ADDR_ANY_INIT;
1064 struct GNUNET_NAT_Handle *h = cls;
1066 struct sockaddr *sa;
1067 const struct sockaddr_in *v4;
1069 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1070 for (i=0;i<h->num_local_addrs;i++)
1072 sa = h->local_addrs[i];
1073 switch (sa->sa_family)
1076 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1081 v4 = (const struct sockaddr_in*) sa;
1082 if (0 != v4->sin_addr.s_addr)
1083 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, sizeof (struct sockaddr_in));
1085 add_minis (h, ntohs (v4->sin_port));
1088 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1093 if (0 != memcmp (&((const struct sockaddr_in6*) sa)->sin6_addr,
1095 sizeof (struct in6_addr)))
1096 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, sizeof (struct sockaddr_in6));
1107 * Attempt to enable port redirection and detect public IP address contacting
1108 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1109 * of the local host's addresses should the external port be mapped. The port
1110 * is taken from the corresponding sockaddr_in[6] field.
1112 * @param cfg configuration to use
1113 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1114 * @param adv_port advertised port (port we are either bound to or that our OS
1115 * locally performs redirection from to our bound port).
1116 * @param num_addrs number of addresses in 'addrs'
1117 * @param addr the local address packets should be redirected to
1118 * @param addrlen actual lenght of the address
1119 * @param address_callback function to call everytime the public IP address changes
1120 * @param reversal_callback function to call if someone wants connection reversal from us
1121 * @param callback_cls closure for callbacks
1122 * @return NULL on error, otherwise handle that can be used to unregister
1124 struct GNUNET_NAT_Handle *
1125 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1128 unsigned int num_addrs,
1129 const struct sockaddr **addrs,
1130 const socklen_t *addrlens,
1131 GNUNET_NAT_AddressCallback address_callback,
1132 GNUNET_NAT_ReversalCallback reversal_callback,
1135 struct GNUNET_NAT_Handle *h;
1136 struct in_addr in_addr;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1141 (unsigned int) adv_port,
1143 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1144 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1147 h->address_callback = address_callback;
1148 h->reversal_callback = reversal_callback;
1149 h->callback_cls = callback_cls;
1150 h->num_local_addrs = num_addrs;
1151 h->adv_port = adv_port;
1154 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr*));
1155 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1156 for (i=0;i<num_addrs;i++)
1158 GNUNET_assert (addrlens[i] > 0);
1159 GNUNET_assert (addrs[i] != NULL);
1160 h->local_addrlens[i] = addrlens[i];
1161 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1162 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1165 h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1167 GNUNET_CONFIGURATION_have_value (cfg,
1169 "INTERNAL_ADDRESS"))
1171 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1174 &h->internal_address);
1176 if ( (h->internal_address != NULL) &&
1177 (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) )
1179 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1181 _("Malformed %s `%s' given in configuration!\n"),
1183 h->internal_address);
1184 GNUNET_free (h->internal_address);
1185 h->internal_address = NULL;
1189 GNUNET_CONFIGURATION_have_value (cfg,
1191 "EXTERNAL_ADDRESS"))
1193 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1196 &h->external_address);
1198 if ( (h->external_address != NULL) &&
1199 (inet_pton(AF_INET, h->external_address, &in_addr) != 1) )
1201 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1203 _("Malformed %s `%s' given in configuration!\n"),
1205 h->external_address);
1206 GNUNET_free (h->external_address);
1207 h->external_address = NULL;
1209 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1212 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1215 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1217 "ENABLE_NAT_CLIENT");
1218 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1220 "ENABLE_NAT_SERVER");
1221 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1224 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1227 h->use_hostname = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1230 if (h->use_localaddresses)
1231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "USE LOCALADDR enabled!\n");
1232 h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno(cfg,
1236 GNUNET_CONFIGURATION_get_value_time (cfg,
1239 &h->dyndns_frequency))
1240 h->dyndns_frequency = DYNDNS_FREQUENCY;
1242 GNUNET_CONFIGURATION_get_value_time (cfg,
1244 "IFC_SCAN_FREQUENCY",
1245 &h->ifc_scan_frequency))
1246 h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1248 GNUNET_CONFIGURATION_get_value_time (cfg,
1250 "HOSTNAME_DNS_FREQUENCY",
1251 &h->hostname_dns_frequency))
1252 h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1254 if (NULL == reversal_callback)
1255 h->enable_nat_server = GNUNET_NO;
1257 /* Check if NAT was hole-punched */
1258 if ( (NULL != h->address_callback) &&
1259 (h->external_address != NULL) &&
1260 (h->nat_punched == GNUNET_YES) )
1262 h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1263 h->enable_nat_server = GNUNET_NO;
1264 h->enable_upnp = GNUNET_NO;
1267 /* Test for SUID binaries */
1268 if ( (h->behind_nat == GNUNET_YES) &&
1269 (GNUNET_YES == h->enable_nat_server) &&
1270 (GNUNET_YES != GNUNET_OS_check_helper_binary("gnunet-helper-nat-server")) )
1272 h->enable_nat_server = GNUNET_NO;
1273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1274 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1275 "gnunet-helper-nat-server");
1277 if ( (GNUNET_YES == h->enable_nat_client) &&
1278 (GNUNET_YES != GNUNET_OS_check_helper_binary("gnunet-helper-nat-client")) )
1280 h->enable_nat_client = GNUNET_NO;
1281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1282 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1283 "gnunet-helper-nat-client");
1286 start_gnunet_nat_server (h);
1288 /* FIXME: add support for UPnP, etc */
1290 if (NULL != h->address_callback)
1292 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1293 if (GNUNET_YES == h->use_hostname)
1294 h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1302 * Stop port redirection and public IP address detection for the given handle.
1303 * This frees the handle, after having sent the needed commands to close open ports.
1305 * @param h the handle to stop
1308 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1311 struct LocalAddressList *lal;
1312 struct MiniList *ml;
1314 while (NULL != (ml = h->mini_head))
1316 GNUNET_CONTAINER_DLL_remove (h->mini_head,
1319 if (NULL != ml->mini)
1320 GNUNET_NAT_mini_map_stop (ml->mini);
1323 if (h->ext_dns != NULL)
1325 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1328 if (NULL != h->hostname_dns)
1330 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1331 h->hostname_dns = NULL;
1333 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1335 GNUNET_SCHEDULER_cancel (h->server_read_task);
1336 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1338 if (GNUNET_SCHEDULER_NO_TASK != h->bind_task)
1340 GNUNET_SCHEDULER_cancel (h->bind_task);
1341 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1343 if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1345 GNUNET_SCHEDULER_cancel (h->ifc_task);
1346 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1348 if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1350 GNUNET_SCHEDULER_cancel (h->hostname_task);
1351 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1353 if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1355 GNUNET_SCHEDULER_cancel (h->dns_task);
1356 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1358 if (NULL != h->server_proc)
1360 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1361 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1362 GNUNET_OS_process_wait (h->server_proc);
1363 GNUNET_OS_process_close (h->server_proc);
1364 h->server_proc = NULL;
1365 GNUNET_DISK_pipe_close (h->server_stdout);
1366 h->server_stdout = NULL;
1367 h->server_stdout_handle = NULL;
1369 if (NULL != h->server_stdout)
1371 GNUNET_DISK_pipe_close (h->server_stdout);
1372 h->server_stdout = NULL;
1373 h->server_stdout_handle = NULL;
1375 while (NULL != (lal = h->lal_head))
1377 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1380 if (NULL != h->address_callback)
1381 h->address_callback (h->callback_cls,
1383 (const struct sockaddr*) &lal[1],
1387 for (i=0;i<h->num_local_addrs;i++)
1388 GNUNET_free (h->local_addrs[i]);
1389 GNUNET_free_non_null (h->local_addrs);
1390 GNUNET_free_non_null (h->local_addrlens);
1391 GNUNET_free_non_null (h->external_address);
1392 GNUNET_free_non_null (h->internal_address);
1398 * We learned about a peer (possibly behind NAT) so run the
1399 * gnunet-helper-nat-client to send dummy ICMP responses to cause
1400 * that peer to connect to us (connection reversal).
1402 * @param h NAT handle for us (largely used for configuration)
1403 * @param sa the address of the peer (IPv4-only)
1406 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1407 const struct sockaddr_in *sa)
1409 char inet4[INET_ADDRSTRLEN];
1410 char port_as_string[6];
1411 struct GNUNET_OS_Process *proc;
1413 if (GNUNET_YES != h->enable_nat_client)
1414 return; /* not permitted / possible */
1416 if (h->internal_address == NULL)
1418 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1420 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1423 GNUNET_assert (sa->sin_family == AF_INET);
1424 if (NULL == inet_ntop (AF_INET,
1426 inet4, INET_ADDRSTRLEN))
1428 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1431 GNUNET_snprintf (port_as_string,
1432 sizeof (port_as_string),
1436 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1438 _("Running gnunet-helper-nat-client %s %s %u\n"),
1439 h->internal_address,
1441 (unsigned int) h->adv_port);
1443 proc = GNUNET_OS_start_process (NULL,
1445 "gnunet-helper-nat-client",
1446 "gnunet-helper-nat-client",
1447 h->internal_address,
1453 /* we know that the gnunet-helper-nat-client will terminate virtually
1455 GNUNET_OS_process_wait (proc);
1456 GNUNET_OS_process_close (proc);
1461 * Test if the given address is (currently) a plausible IP address for this peer.
1463 * @param h the handle returned by register
1464 * @param addr IP address to test (IPv4 or IPv6)
1465 * @param addrlen number of bytes in addr
1466 * @return GNUNET_YES if the address is plausible,
1467 * GNUNET_NO if the address is not plausible,
1468 * GNUNET_SYSERR if the address is malformed
1471 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1475 struct LocalAddressList *pos;
1476 const struct sockaddr_in *in4;
1477 const struct sockaddr_in6 *in6;
1479 if ( (addrlen != sizeof (struct in_addr)) &&
1480 (addrlen != sizeof (struct in6_addr)) )
1483 return GNUNET_SYSERR;
1488 if (pos->addrlen == sizeof (struct sockaddr_in))
1490 in4 = (struct sockaddr_in* ) &pos[1];
1491 if ( (addrlen == sizeof (struct in_addr)) &&
1492 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))) )
1495 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1497 in6 = (struct sockaddr_in6* ) &pos[1];
1498 if ( (addrlen == sizeof (struct in6_addr)) &&
1499 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))) )
1508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1509 "Asked to validate one of my addresses and validation failed!\n");