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,
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
428 "Adding address `%s' from source %d\n",
429 GNUNET_a2s (arg, arg_size),
431 if (NULL != h->address_callback)
432 h->address_callback (h->callback_cls,
440 * Add the given address to the list of 'local' addresses, thereby
441 * making it a 'legal' address for this peer to have. Set the
442 * port number in the process to the advertised port and possibly
443 * also to zero (if we have the gnunet-helper-nat-server).
445 * @param plugin the plugin
446 * @param src where did the local address originate from?
447 * @param arg the address, some 'struct sockaddr'
448 * @param arg_size number of bytes in arg
451 add_to_address_list (struct GNUNET_NAT_Handle *h,
452 enum LocalAddressSource src,
453 const struct sockaddr *arg,
456 struct sockaddr_in s4;
457 const struct sockaddr_in *in4;
458 struct sockaddr_in6 s6;
459 const struct sockaddr_in6 *in6;
461 if (arg_size == sizeof (struct sockaddr_in))
463 in4 = (const struct sockaddr_in *) arg;
465 s4.sin_port = htons (h->adv_port);
466 add_to_address_list_as_is (h,
468 (const struct sockaddr*) &s4,
469 sizeof (struct sockaddr_in));
470 if (GNUNET_YES == h->enable_nat_server)
472 /* also add with PORT = 0 to indicate NAT server is enabled */
473 s4.sin_port = htons(0);
474 add_to_address_list_as_is (h,
476 (const struct sockaddr*) &s4,
477 sizeof (struct sockaddr_in));
480 else if (arg_size == sizeof (struct sockaddr_in6))
482 if (GNUNET_YES != h->disable_ipv6)
484 in6 = (const struct sockaddr_in6 *) arg;
486 s6.sin6_port = htons(h->adv_port);
487 add_to_address_list_as_is (h,
489 (const struct sockaddr*) &s6,
490 sizeof (struct sockaddr_in6));
501 * Add the given IP address to the list of 'local' addresses, thereby
502 * making it a 'legal' address for this peer to have.
504 * @param plugin the plugin
505 * @param src where did the local address originate from?
506 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
507 * @param arg_size number of bytes in arg
510 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
511 enum LocalAddressSource src,
515 struct sockaddr_in s4;
516 const struct in_addr *in4;
517 struct sockaddr_in6 s6;
518 const struct in6_addr *in6;
520 if (addrlen == sizeof (struct in_addr))
522 in4 = (const struct in_addr *) addr;
523 memset (&s4, 0, sizeof (s4));
524 s4.sin_family = AF_INET;
526 #if HAVE_SOCKADDR_IN_SIN_LEN
527 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
530 add_to_address_list (h,
532 (const struct sockaddr*) &s4,
533 sizeof (struct sockaddr_in));
534 if (GNUNET_YES == h->enable_nat_server)
536 /* also add with PORT = 0 to indicate NAT server is enabled */
537 s4.sin_port = htons(0);
538 add_to_address_list (h,
540 (const struct sockaddr*) &s4,
541 sizeof (struct sockaddr_in));
545 else if (addrlen == sizeof (struct in6_addr))
547 if (GNUNET_YES != h->disable_ipv6)
549 in6 = (const struct in6_addr *) addr;
550 memset (&s6, 0, sizeof (s6));
551 s6.sin6_family = AF_INET6;
552 s6.sin6_port = htons(h->adv_port);
553 #if HAVE_SOCKADDR_IN_SIN_LEN
554 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
557 add_to_address_list (h,
559 (const struct sockaddr*) &s6,
560 sizeof (struct sockaddr_in6));
571 * Task to do DNS lookup on our external hostname to
572 * get DynDNS-IP addresses.
574 * @param cls the NAT handle
575 * @param tc scheduler context
578 resolve_dns (void *cls,
579 const struct GNUNET_SCHEDULER_TaskContext *tc);
583 * Our (external) hostname was resolved and the configuration says that
584 * the NAT was hole-punched.
586 * @param cls the 'struct Plugin'
587 * @param addr NULL on error, otherwise result of DNS lookup
588 * @param addrlen number of bytes in addr
591 process_external_ip (void *cls,
592 const struct sockaddr *addr,
595 struct GNUNET_NAT_Handle *h = cls;
596 struct in_addr dummy;
601 if (1 == inet_pton (AF_INET,
604 return; /* repated lookup pointless: was numeric! */
605 h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
609 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
614 * Task to do a lookup on our hostname for IP addresses.
616 * @param cls the NAT handle
617 * @param tc scheduler context
620 resolve_hostname (void *cls,
621 const struct GNUNET_SCHEDULER_TaskContext *tc);
625 * Function called by the resolver for each address obtained from DNS
626 * for our own hostname. Add the addresses to the list of our IP
630 * @param addr one of the addresses of the host, NULL for the last address
631 * @param addrlen length of the address
634 process_hostname_ip (void *cls,
635 const struct sockaddr *addr, socklen_t addrlen)
637 struct GNUNET_NAT_Handle *h = cls;
641 h->hostname_dns = NULL;
642 h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
643 &resolve_hostname, h);
646 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
651 * Add the IP of our network interface to the list of
654 * @param cls the 'struct GNUNET_NAT_Handle'
655 * @param name name of the interface
656 * @param isDefault do we think this may be our default interface
657 * @param addr address of the interface
658 * @param addrlen number of bytes in addr
659 * @return GNUNET_OK to continue iterating
662 process_interfaces (void *cls,
665 const struct sockaddr *addr, socklen_t addrlen)
667 struct GNUNET_NAT_Handle *h = cls;
668 const struct sockaddr_in *s4;
669 const struct sockaddr_in6 *s6;
671 char buf[INET6_ADDRSTRLEN];
673 switch (addr->sa_family)
676 s4 = (struct sockaddr_in *) addr;
678 if (GNUNET_YES == h->use_localaddresses)
679 add_ip_to_address_list (h,
680 LAL_INTERFACE_ADDRESS,
682 sizeof (struct in_addr));
685 s6 = (struct sockaddr_in6 *) addr;
686 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
688 /* skip link local addresses */
692 if (GNUNET_YES == h->use_localaddresses)
693 add_ip_to_address_list (h,
694 LAL_INTERFACE_ADDRESS,
696 sizeof (struct in6_addr));
702 if ( (h->internal_address == NULL) &&
703 (h->server_proc == NULL) &&
704 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
705 (GNUNET_YES == isDefault) &&
706 ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) )
708 /* no internal address configured, but we found a "default"
709 interface, try using that as our 'internal' address */
710 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
714 start_gnunet_nat_server (h);
721 * Task that restarts the gnunet-helper-nat-server process after a crash
722 * after a certain delay.
724 * @param cls the 'struct GNUNET_NAT_Handle'
725 * @param tc scheduler context
728 restart_nat_server (void *cls,
729 const struct GNUNET_SCHEDULER_TaskContext *tc)
731 struct GNUNET_NAT_Handle *h = cls;
733 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
734 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
736 start_gnunet_nat_server (h);
741 * We have been notified that gnunet-helper-nat-server has written something to stdout.
742 * Handle the output, then reschedule this function to be called again once
745 * @param cls the NAT handle
746 * @param tc the scheduling context
749 nat_server_read (void *cls,
750 const struct GNUNET_SCHEDULER_TaskContext *tc)
752 struct GNUNET_NAT_Handle *h = cls;
757 const char *port_start;
758 struct sockaddr_in sin_addr;
760 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
761 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
763 memset (mybuf, 0, sizeof(mybuf));
764 bytes = GNUNET_DISK_file_read(h->server_stdout_handle,
770 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
772 "Finished reading from server stdout with code: %d\n",
775 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
776 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
777 GNUNET_OS_process_wait (h->server_proc);
778 GNUNET_OS_process_close (h->server_proc);
779 h->server_proc = NULL;
780 GNUNET_DISK_pipe_close (h->server_stdout);
781 h->server_stdout = NULL;
782 h->server_stdout_handle = NULL;
783 /* now try to restart it */
784 h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
785 h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
786 h->server_retry_delay);
787 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
794 for (i = 0; i < sizeof(mybuf); i++)
796 if (mybuf[i] == '\n')
801 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
804 port_start = &mybuf[i + 1];
808 /* construct socket address of sender */
809 memset (&sin_addr, 0, sizeof (sin_addr));
810 sin_addr.sin_family = AF_INET;
811 #if HAVE_SOCKADDR_IN_SIN_LEN
812 sin_addr.sin_len = sizeof (sin_addr);
814 if ( (NULL == port_start) ||
815 (1 != sscanf (port_start, "%d", &port)) ||
816 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
818 /* should we restart gnunet-helper-nat-server? */
819 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
821 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
824 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
825 h->server_stdout_handle,
830 sin_addr.sin_port = htons((uint16_t) port);
832 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
834 "gnunet-helper-nat-server read: %s:%d\n",
837 h->reversal_callback (h->callback_cls,
838 (const struct sockaddr*) &sin_addr,
840 h->server_read_task =
841 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
842 h->server_stdout_handle,
849 * Try to start the gnunet-helper-nat-server (if it is not
852 * @param h handle to NAT
855 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
857 if ( (h->behind_nat == GNUNET_YES) &&
858 (h->enable_nat_server == GNUNET_YES) &&
859 (h->internal_address != NULL) &&
860 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
865 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
867 "Starting %s at `%s'\n",
868 "gnunet-helper-nat-server",
869 h->internal_address);
871 /* Start the server process */
872 h->server_proc = GNUNET_OS_start_process (NULL,
874 "gnunet-helper-nat-server",
875 "gnunet-helper-nat-server",
878 if (h->server_proc == NULL)
880 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
882 _("Failed to start %s\n"),
883 "gnunet-helper-nat-server");
884 GNUNET_DISK_pipe_close (h->server_stdout);
885 h->server_stdout = NULL;
889 /* Close the write end of the read pipe */
890 GNUNET_DISK_pipe_close_end(h->server_stdout,
891 GNUNET_DISK_PIPE_END_WRITE);
892 h->server_stdout_handle
893 = GNUNET_DISK_pipe_handle (h->server_stdout,
894 GNUNET_DISK_PIPE_END_READ);
896 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
897 h->server_stdout_handle,
906 * Task to scan the local network interfaces for IP addresses.
908 * @param cls the NAT handle
909 * @param tc scheduler context
912 list_interfaces (void *cls,
913 const struct GNUNET_SCHEDULER_TaskContext *tc)
915 struct GNUNET_NAT_Handle *h = cls;
917 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
918 remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
919 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
920 h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
921 &list_interfaces, h);
926 * Task to do a lookup on our hostname for IP addresses.
928 * @param cls the NAT handle
929 * @param tc scheduler context
932 resolve_hostname (void *cls,
933 const struct GNUNET_SCHEDULER_TaskContext *tc)
935 struct GNUNET_NAT_Handle *h = cls;
937 h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
938 remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
939 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
940 HOSTNAME_RESOLVE_TIMEOUT,
941 &process_hostname_ip,
947 * Task to do DNS lookup on our external hostname to
948 * get DynDNS-IP addresses.
950 * @param cls the NAT handle
951 * @param tc scheduler context
954 resolve_dns (void *cls,
955 const struct GNUNET_SCHEDULER_TaskContext *tc)
957 struct GNUNET_NAT_Handle *h = cls;
959 h->dns_task = GNUNET_SCHEDULER_NO_TASK;
960 remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
961 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
963 GNUNET_TIME_UNIT_MINUTES,
964 &process_external_ip,
970 * Add or remove UPnP-mapped addresses.
972 * @param cls the GNUNET_NAT_Handle
973 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
974 * the previous (now invalid) one
975 * @param addr either the previous or the new public IP address
976 * @param addrlen actual lenght of the address
981 const struct sockaddr *addr,
984 struct GNUNET_NAT_Handle *h = cls;
985 struct LocalAddressList *pos;
986 struct LocalAddressList *next;
988 if (GNUNET_YES == add_remove)
990 add_to_address_list (h,
997 while (NULL != (pos = next))
1000 if ( (pos->source != LAL_UPNP) ||
1001 (pos->addrlen != addrlen) ||
1002 (0 != memcmp (&pos[1],
1006 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1009 if (NULL != h->address_callback)
1010 h->address_callback (h->callback_cls,
1012 (const struct sockaddr* ) &pos[1],
1015 return; /* only remove once */
1017 /* asked to remove address that does not exist */
1023 * Try to add a port mapping using UPnP.
1025 * @param h overall NAT handle
1026 * @param port port to map with UPnP
1029 add_minis (struct GNUNET_NAT_Handle *h,
1032 struct MiniList *ml;
1037 if (port == ml->port)
1038 return; /* already got this port */
1041 ml = GNUNET_malloc (sizeof (struct MiniList));
1043 ml->mini = GNUNET_NAT_mini_map_start (port,
1046 GNUNET_CONTAINER_DLL_insert (h->mini_head,
1053 * Task to add addresses from original bind to set of valid addrs.
1055 * @param cls the NAT handle
1056 * @param tc scheduler context
1059 add_from_bind (void *cls,
1060 const struct GNUNET_SCHEDULER_TaskContext *tc)
1062 static struct in6_addr any = IN6ADDR_ANY_INIT;
1063 struct GNUNET_NAT_Handle *h = cls;
1065 struct sockaddr *sa;
1066 const struct sockaddr_in *v4;
1068 h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1069 for (i=0;i<h->num_local_addrs;i++)
1071 sa = h->local_addrs[i];
1072 switch (sa->sa_family)
1075 if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1080 v4 = (const struct sockaddr_in*) sa;
1081 if (0 != v4->sin_addr.s_addr)
1082 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, sizeof (struct sockaddr_in));
1084 add_minis (h, ntohs (v4->sin_port));
1087 if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1092 if (0 != memcmp (&((const struct sockaddr_in6*) sa)->sin6_addr,
1094 sizeof (struct in6_addr)))
1095 add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, sizeof (struct sockaddr_in6));
1106 * Attempt to enable port redirection and detect public IP address contacting
1107 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1108 * of the local host's addresses should the external port be mapped. The port
1109 * is taken from the corresponding sockaddr_in[6] field.
1111 * @param cfg configuration to use
1112 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1113 * @param adv_port advertised port (port we are either bound to or that our OS
1114 * locally performs redirection from to our bound port).
1115 * @param num_addrs number of addresses in 'addrs'
1116 * @param addr the local address packets should be redirected to
1117 * @param addrlen actual lenght of the address
1118 * @param address_callback function to call everytime the public IP address changes
1119 * @param reversal_callback function to call if someone wants connection reversal from us
1120 * @param callback_cls closure for callbacks
1121 * @return NULL on error, otherwise handle that can be used to unregister
1123 struct GNUNET_NAT_Handle *
1124 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1127 unsigned int num_addrs,
1128 const struct sockaddr **addrs,
1129 const socklen_t *addrlens,
1130 GNUNET_NAT_AddressCallback address_callback,
1131 GNUNET_NAT_ReversalCallback reversal_callback,
1134 struct GNUNET_NAT_Handle *h;
1135 struct in_addr in_addr;
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1141 "Registered with NAT service at port %u with %u IP bound local addresses\n",
1142 (unsigned int) adv_port,
1145 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1146 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1149 h->address_callback = address_callback;
1150 h->reversal_callback = reversal_callback;
1151 h->callback_cls = callback_cls;
1152 h->num_local_addrs = num_addrs;
1153 h->adv_port = adv_port;
1156 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr*));
1157 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1158 for (i=0;i<num_addrs;i++)
1160 GNUNET_assert (addrlens[i] > 0);
1161 GNUNET_assert (addrs[i] != NULL);
1162 h->local_addrlens[i] = addrlens[i];
1163 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1164 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1167 h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1169 GNUNET_CONFIGURATION_have_value (cfg,
1171 "INTERNAL_ADDRESS"))
1173 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1176 &h->internal_address);
1178 if ( (h->internal_address != NULL) &&
1179 (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) )
1181 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1183 _("Malformed %s `%s' given in configuration!\n"),
1185 h->internal_address);
1186 GNUNET_free (h->internal_address);
1187 h->internal_address = NULL;
1191 GNUNET_CONFIGURATION_have_value (cfg,
1193 "EXTERNAL_ADDRESS"))
1195 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1198 &h->external_address);
1200 if ( (h->external_address != NULL) &&
1201 (inet_pton(AF_INET, h->external_address, &in_addr) != 1) )
1203 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1205 _("Malformed %s `%s' given in configuration!\n"),
1207 h->external_address);
1208 GNUNET_free (h->external_address);
1209 h->external_address = NULL;
1211 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1214 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1217 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1219 "ENABLE_NAT_CLIENT");
1220 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1222 "ENABLE_NAT_SERVER");
1223 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1226 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1229 h->use_hostname = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1232 if (h->use_localaddresses)
1233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "USE LOCALADDR enabled!\n");
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");