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
30 * - repeatedly perform certain checks again to notice changes
33 #include "gnunet_util_lib.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_nat_lib.h"
39 * How long until we give up on transmitting the welcome message?
41 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
45 * Where did the given local address originate from?
46 * To be used for debugging as well as in the future
47 * to remove all addresses from a certain source when
48 * we reevaluate the source.
50 enum LocalAddressSource
53 * Address was obtained by DNS resolution of the external hostname
54 * given in the configuration (i.e. hole-punched DynDNS setup).
55 * FIXME: repeatedly do the lookup to notice changes!
60 * Address was obtained by looking up our own hostname in DNS.
61 * FIXME: repeatedly do the lookup to notice changes!
66 * Address was obtained by scanning our hosts's network interfaces
67 * and taking their address (no DNS involved).
68 * FIXME: repeatedly do the lookup to notice changes!
70 LAL_INTERFACE_ADDRESS,
72 /* TODO: add UPnP, etc. */
83 * List of local addresses that we currently deem valid. Actual
84 * struct is followed by the 'struct sockaddr'. Note that the code
85 * intentionally makes no attempt to ensure that a particular address
86 * is only listed once (especially since it may come from different
87 * sources, and the source is an "internal" construct).
89 struct LocalAddressList
92 * This is a linked list.
94 struct LocalAddressList *next;
99 struct LocalAddressList *prev;
102 * Number of bytes of address that follow.
107 * Origin of the local address.
109 enum LocalAddressSource source;
114 * Handle for active NAT registrations.
116 struct GNUNET_NAT_Handle
120 * Configuration to use.
122 const struct GNUNET_CONFIGURATION_Handle *cfg;
125 * Function to call when we learn about a new address.
127 GNUNET_NAT_AddressCallback address_callback;
130 * Function to call when we notice another peer asking for
131 * connection reversal.
133 GNUNET_NAT_ReversalCallback reversal_callback;
136 * Closure for 'callback'.
141 * Handle for (DYN)DNS lookup of our external IP.
143 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
146 * Handle for request of hostname resolution, non-NULL if pending.
148 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
151 * stdout pipe handle for the gnunet-nat-server process
153 struct GNUNET_DISK_PipeHandle *server_stdout;
156 * stdout file handle (for reading) for the gnunet-nat-server process
158 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
161 * Linked list of currently valid addresses (head).
163 struct LocalAddressList *lal_head;
166 * Linked list of currently valid addresses (tail).
168 struct LocalAddressList *lal_tail;
171 * How long do we wait for restarting a crashed gnunet-nat-server?
173 struct GNUNET_TIME_Relative server_retry_delay;
176 * ID of select gnunet-nat-server stdout read task
178 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
181 * The process id of the server process (if behind NAT)
183 struct GNUNET_OS_Process *server_proc;
186 * LAN address as passed by the caller (array).
188 struct sockaddr **local_addrs;
191 * Length of the 'local_addrs'.
193 socklen_t *local_addrlens;
196 * Number of entries in 'local_addrs' array.
198 unsigned int num_local_addrs;
201 * The our external address (according to config, UPnP may disagree...)
203 char *external_address;
206 * Presumably our internal address (according to config)
208 char *internal_address;
211 * Is this transport configured to be behind a NAT?
216 * Has the NAT been punched? (according to config)
221 * Is this transport configured to allow connections to NAT'd peers?
223 int enable_nat_client;
226 * Should we run the gnunet-nat-server?
228 int enable_nat_server;
231 * Are we allowed to try UPnP/PMP for NAT traversal?
236 * Should we use local addresses (loopback)? (according to config)
238 int use_localaddresses;
241 * Is using IPv6 disabled?
246 * Is this TCP or UDP?
251 * Port we advertise to the outside.
259 * Try to start the gnunet-nat-server (if it is not
262 * @param h handle to NAT
265 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
269 * Add the given address to the list of 'local' addresses, thereby
270 * making it a 'legal' address for this peer to have.
272 * @param plugin the plugin
273 * @param src where did the local address originate from?
274 * @param arg the address, some 'struct sockaddr'
275 * @param arg_size number of bytes in arg
278 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
279 enum LocalAddressSource src,
280 const struct sockaddr *arg,
283 struct LocalAddressList *lal;
285 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
286 memcpy (&lal[1], arg, arg_size);
287 lal->addrlen = arg_size;
289 GNUNET_CONTAINER_DLL_insert (h->lal_head,
292 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
294 "Adding address `%s' from source %d\n",
295 GNUNET_a2s (arg, arg_size),
297 h->address_callback (h->callback_cls,
305 * Add the given address to the list of 'local' addresses, thereby
306 * making it a 'legal' address for this peer to have. Set the
307 * port number in the process to the advertised port and possibly
308 * also to zero (if we have the gnunet-nat-server).
310 * @param plugin the plugin
311 * @param src where did the local address originate from?
312 * @param arg the address, some 'struct sockaddr'
313 * @param arg_size number of bytes in arg
316 add_to_address_list (struct GNUNET_NAT_Handle *h,
317 enum LocalAddressSource src,
318 const struct sockaddr *arg,
321 struct sockaddr_in s4;
322 const struct sockaddr_in *in4;
323 struct sockaddr_in6 s6;
324 const struct sockaddr_in6 *in6;
326 if (arg_size == sizeof (struct sockaddr_in))
328 in4 = (const struct sockaddr_in *) arg;
330 s4.sin_port = htons (h->adv_port);
331 add_to_address_list_as_is (h,
333 (const struct sockaddr*) &s4,
334 sizeof (struct sockaddr_in));
335 if (GNUNET_YES == h->enable_nat_server)
337 /* also add with PORT = 0 to indicate NAT server is enabled */
338 s4.sin_port = htons(0);
339 add_to_address_list_as_is (h,
341 (const struct sockaddr*) &s4,
342 sizeof (struct sockaddr_in));
345 else if (arg_size == sizeof (struct sockaddr_in6))
347 if (GNUNET_YES != h->disable_ipv6)
349 in6 = (const struct sockaddr_in6 *) arg;
351 s6.sin6_port = htons(h->adv_port);
352 add_to_address_list_as_is (h,
354 (const struct sockaddr*) &s6,
355 sizeof (struct sockaddr_in6));
366 * Add the given IP address to the list of 'local' addresses, thereby
367 * making it a 'legal' address for this peer to have.
369 * @param plugin the plugin
370 * @param src where did the local address originate from?
371 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
372 * @param arg_size number of bytes in arg
375 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
376 enum LocalAddressSource src,
380 struct sockaddr_in s4;
381 const struct in_addr *in4;
382 struct sockaddr_in6 s6;
383 const struct in6_addr *in6;
385 if (addrlen == sizeof (struct in_addr))
387 in4 = (const struct in_addr *) addr;
388 memset (&s4, 0, sizeof (s4));
389 s4.sin_family = AF_INET;
391 #if HAVE_SOCKADDR_IN_SIN_LEN
392 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
395 add_to_address_list (h,
397 (const struct sockaddr*) &s4,
398 sizeof (struct sockaddr_in));
399 if (GNUNET_YES == h->enable_nat_server)
401 /* also add with PORT = 0 to indicate NAT server is enabled */
402 s4.sin_port = htons(0);
403 add_to_address_list (h,
405 (const struct sockaddr*) &s4,
406 sizeof (struct sockaddr_in));
410 else if (addrlen == sizeof (struct in6_addr))
412 if (GNUNET_YES != h->disable_ipv6)
414 in6 = (const struct in6_addr *) addr;
415 memset (&s6, 0, sizeof (s6));
416 s6.sin6_family = AF_INET6;
417 s6.sin6_port = htons(h->adv_port);
418 #if HAVE_SOCKADDR_IN_SIN_LEN
419 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
422 add_to_address_list (h,
424 (const struct sockaddr*) &s6,
425 sizeof (struct sockaddr_in6));
436 * Our (external) hostname was resolved and the configuration says that
437 * the NAT was hole-punched.
439 * @param cls the 'struct Plugin'
440 * @param addr NULL on error, otherwise result of DNS lookup
441 * @param addrlen number of bytes in addr
444 process_external_ip (void *cls,
445 const struct sockaddr *addr,
448 struct GNUNET_NAT_Handle *h = cls;
453 /* FIXME: schedule task to resolve IP again in the
454 future, and if the result changes, update the
455 local address list accordingly */
458 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
463 * Function called by the resolver for each address obtained from DNS
464 * for our own hostname. Add the addresses to the list of our IP
468 * @param addr one of the addresses of the host, NULL for the last address
469 * @param addrlen length of the address
472 process_hostname_ip (void *cls,
473 const struct sockaddr *addr, socklen_t addrlen)
475 struct GNUNET_NAT_Handle *h = cls;
479 h->hostname_dns = NULL;
480 /* FIXME: schedule task to resolve IP again in the
481 future, and if the result changes, update the
482 address list accordingly */
485 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
490 * Add the IP of our network interface to the list of
493 * @param cls the 'struct GNUNET_NAT_Handle'
494 * @param name name of the interface
495 * @param isDefault do we think this may be our default interface
496 * @param addr address of the interface
497 * @param addrlen number of bytes in addr
498 * @return GNUNET_OK to continue iterating
501 process_interfaces (void *cls,
504 const struct sockaddr *addr, socklen_t addrlen)
506 struct GNUNET_NAT_Handle *h = cls;
507 const struct sockaddr_in *s4;
508 const struct sockaddr_in6 *s6;
510 char buf[INET6_ADDRSTRLEN];
512 switch (addr->sa_family)
515 s4 = (struct sockaddr_in *) addr;
517 if (GNUNET_YES == h->use_localaddresses)
518 add_ip_to_address_list (h,
519 LAL_INTERFACE_ADDRESS,
521 sizeof (struct in_addr));
524 s6 = (struct sockaddr_in6 *) addr;
525 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
527 /* skip link local addresses */
531 if (GNUNET_YES == h->use_localaddresses)
532 add_ip_to_address_list (h,
533 LAL_INTERFACE_ADDRESS,
535 sizeof (struct in6_addr));
541 if ( (h->internal_address == NULL) &&
542 (h->server_proc == NULL) &&
543 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
544 (GNUNET_YES == isDefault) &&
545 ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) )
547 /* no internal address configured, but we found a "default"
548 interface, try using that as our 'internal' address */
549 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
553 start_gnunet_nat_server (h);
560 * Return the actual path to a file found in the current
561 * PATH environment variable.
563 * @param binary the name of the file to find
564 * @return path to binary, NULL if not found
567 get_path_from_PATH (const char *binary)
578 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
580 _("PATH environment variable is unset.\n"));
583 path = GNUNET_strdup (p); /* because we write on it */
584 buf = GNUNET_malloc (strlen (path) + 20);
587 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
590 sprintf (buf, "%s/%s", pos, binary);
591 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
598 sprintf (buf, "%s/%s", pos, binary);
599 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
611 * Check whether the suid bit is set on a file.
612 * Attempts to find the file using the current
613 * PATH environment variable as a search path.
615 * @param binary the name of the file to check
616 * @return GNUNET_YES if the file is SUID,
618 * GNUNET_SYSERR on error
621 check_gnunet_nat_binary (const char *binary)
629 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
630 p = get_path_from_PATH (binaryexe);
633 p = get_path_from_PATH (binary);
637 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
639 _("Could not find binary `%s' in PATH!\n"),
643 if (0 != STAT (p, &statbuf))
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 _("stat (%s) failed: %s\n"),
650 return GNUNET_SYSERR;
654 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
655 (statbuf.st_uid == 0) )
659 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
660 if (INVALID_SOCKET == rawsock)
662 DWORD err = GetLastError ();
663 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
665 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
666 return GNUNET_NO; /* not running as administrator */
668 closesocket (rawsock);
675 * Task that restarts the gnunet-nat-server process after a crash
676 * after a certain delay.
678 * @param cls the 'struct GNUNET_NAT_Handle'
679 * @param tc scheduler context
682 restart_nat_server (void *cls,
683 const struct GNUNET_SCHEDULER_TaskContext *tc)
685 struct GNUNET_NAT_Handle *h = cls;
687 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
688 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
690 start_gnunet_nat_server (h);
695 * We have been notified that gnunet-nat-server has written something to stdout.
696 * Handle the output, then reschedule this function to be called again once
699 * @param cls the NAT handle
700 * @param tc the scheduling context
703 nat_server_read (void *cls,
704 const struct GNUNET_SCHEDULER_TaskContext *tc)
706 struct GNUNET_NAT_Handle *h = cls;
711 const char *port_start;
712 struct sockaddr_in sin_addr;
714 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
715 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
717 memset (mybuf, 0, sizeof(mybuf));
718 bytes = GNUNET_DISK_file_read(h->server_stdout_handle,
724 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
726 "Finished reading from server stdout with code: %d\n",
729 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
730 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
731 GNUNET_OS_process_wait (h->server_proc);
732 GNUNET_OS_process_close (h->server_proc);
733 h->server_proc = NULL;
734 GNUNET_DISK_pipe_close (h->server_stdout);
735 h->server_stdout = NULL;
736 h->server_stdout_handle = NULL;
737 /* now try to restart it */
738 h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
739 h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
740 h->server_retry_delay);
741 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
748 for (i = 0; i < sizeof(mybuf); i++)
750 if (mybuf[i] == '\n')
755 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
758 port_start = &mybuf[i + 1];
762 /* construct socket address of sender */
763 memset (&sin_addr, 0, sizeof (sin_addr));
764 sin_addr.sin_family = AF_INET;
765 #if HAVE_SOCKADDR_IN_SIN_LEN
766 sin_addr.sin_len = sizeof (sin_addr);
768 if ( (NULL == port_start) ||
769 (1 != sscanf (port_start, "%d", &port)) ||
770 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
772 /* should we restart gnunet-nat-server? */
773 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
775 _("gnunet-nat-server generated malformed address `%s'\n"),
778 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
779 h->server_stdout_handle,
784 sin_addr.sin_port = htons((uint16_t) port);
786 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
788 "gnunet-nat-server read: %s:%d\n",
791 h->reversal_callback (h->callback_cls,
792 (const struct sockaddr*) &sin_addr,
794 h->server_read_task =
795 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
796 h->server_stdout_handle,
803 * Try to start the gnunet-nat-server (if it is not
806 * @param h handle to NAT
809 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
811 if ( (h->behind_nat == GNUNET_YES) &&
812 (h->enable_nat_server == GNUNET_YES) &&
813 (h->internal_address != NULL) &&
814 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
819 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
821 "Starting %s at `%s'\n",
823 h->internal_address);
825 /* Start the server process */
826 h->server_proc = GNUNET_OS_start_process (NULL,
832 if (h->server_proc == NULL)
834 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
836 _("Failed to start %s\n"),
837 "gnunet-nat-server");
838 GNUNET_DISK_pipe_close (h->server_stdout);
839 h->server_stdout = NULL;
843 /* Close the write end of the read pipe */
844 GNUNET_DISK_pipe_close_end(h->server_stdout,
845 GNUNET_DISK_PIPE_END_WRITE);
846 h->server_stdout_handle
847 = GNUNET_DISK_pipe_handle (h->server_stdout,
848 GNUNET_DISK_PIPE_END_READ);
850 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
851 h->server_stdout_handle,
860 * Attempt to enable port redirection and detect public IP address contacting
861 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
862 * of the local host's addresses should the external port be mapped. The port
863 * is taken from the corresponding sockaddr_in[6] field.
865 * @param cfg configuration to use
866 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
867 * @param adv_port advertised port (port we are either bound to or that our OS
868 * locally performs redirection from to our bound port).
869 * @param num_addrs number of addresses in 'addrs'
870 * @param addr the local address packets should be redirected to
871 * @param addrlen actual lenght of the address
872 * @param address_callback function to call everytime the public IP address changes
873 * @param reversal_callback function to call if someone wants connection reversal from us
874 * @param callback_cls closure for callbacks
875 * @return NULL on error, otherwise handle that can be used to unregister
877 struct GNUNET_NAT_Handle *
878 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
881 unsigned int num_addrs,
882 const struct sockaddr **addrs,
883 const socklen_t *addrlens,
884 GNUNET_NAT_AddressCallback address_callback,
885 GNUNET_NAT_ReversalCallback reversal_callback,
888 struct GNUNET_NAT_Handle *h;
889 struct in_addr in_addr;
892 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
893 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
896 h->address_callback = address_callback;
897 h->reversal_callback = reversal_callback;
898 h->callback_cls = callback_cls;
899 h->num_local_addrs = num_addrs;
900 h->adv_port = adv_port;
903 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr*));
904 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
905 for (i=0;i<num_addrs;i++)
907 h->local_addrlens[i] = addrlens[i];
908 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
909 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
914 GNUNET_CONFIGURATION_have_value (cfg,
918 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
921 &h->internal_address);
923 if ( (h->internal_address != NULL) &&
924 (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) )
926 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
928 _("Malformed %s `%s' given in configuration!\n"),
930 h->internal_address);
931 GNUNET_free (h->internal_address);
932 h->internal_address = NULL;
936 GNUNET_CONFIGURATION_have_value (cfg,
940 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
943 &h->external_address);
945 if ( (h->external_address != NULL) &&
946 (inet_pton(AF_INET, h->external_address, &in_addr) != 1) )
948 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
950 _("Malformed %s `%s' given in configuration!\n"),
952 h->external_address);
953 GNUNET_free (h->external_address);
954 h->external_address = NULL;
956 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
959 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
962 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
964 "ENABLE_NAT_CLIENT");
965 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
967 "ENABLE_NAT_SERVER");
968 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
971 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
974 h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno(cfg,
977 if (NULL == reversal_callback)
978 h->enable_nat_server = GNUNET_NO;
980 /* Check if NAT was hole-punched */
981 if ( (NULL != h->address_callback) &&
982 (h->external_address != NULL) &&
983 (h->nat_punched == GNUNET_YES) )
985 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
987 GNUNET_TIME_UNIT_MINUTES,
988 &process_external_ip,
990 h->enable_nat_server = GNUNET_NO;
991 h->enable_upnp = GNUNET_NO;
994 /* Test for SUID binaries */
995 if ( (h->behind_nat == GNUNET_YES) &&
996 (GNUNET_YES == h->enable_nat_server) &&
997 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
999 h->enable_nat_server = GNUNET_NO;
1000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1001 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1002 "gnunet-nat-server");
1004 if ( (GNUNET_YES == h->enable_nat_client) &&
1005 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
1007 h->enable_nat_client = GNUNET_NO;
1008 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1009 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1010 "gnunet-nat-client");
1013 start_gnunet_nat_server (h);
1015 /* FIXME: add support for UPnP, etc */
1017 if (NULL != h->address_callback)
1019 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
1020 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
1021 HOSTNAME_RESOLVE_TIMEOUT,
1022 &process_hostname_ip,
1030 * Stop port redirection and public IP address detection for the given handle.
1031 * This frees the handle, after having sent the needed commands to close open ports.
1033 * @param h the handle to stop
1036 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1039 struct LocalAddressList *lal;
1041 if (h->ext_dns != NULL)
1043 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1046 if (NULL != h->hostname_dns)
1048 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1049 h->hostname_dns = NULL;
1051 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1053 GNUNET_SCHEDULER_cancel (h->server_read_task);
1054 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1056 if (NULL != h->server_proc)
1058 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1059 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1060 GNUNET_OS_process_wait (h->server_proc);
1061 GNUNET_OS_process_close (h->server_proc);
1062 h->server_proc = NULL;
1063 GNUNET_DISK_pipe_close (h->server_stdout);
1064 h->server_stdout = NULL;
1065 h->server_stdout_handle = NULL;
1067 if (NULL != h->server_stdout)
1069 GNUNET_DISK_pipe_close (h->server_stdout);
1070 h->server_stdout = NULL;
1071 h->server_stdout_handle = NULL;
1073 while (NULL != (lal = h->lal_head))
1075 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1078 h->address_callback (h->callback_cls,
1080 (const struct sockaddr*) &lal[1],
1084 for (i=0;i<h->num_local_addrs;i++)
1085 GNUNET_free (h->local_addrs[i]);
1086 GNUNET_free_non_null (h->local_addrs);
1087 GNUNET_free_non_null (h->local_addrlens);
1088 GNUNET_free_non_null (h->external_address);
1089 GNUNET_free_non_null (h->internal_address);
1095 * We learned about a peer (possibly behind NAT) so run the
1096 * gnunet-nat-client to send dummy ICMP responses to cause
1097 * that peer to connect to us (connection reversal).
1099 * @param h NAT handle for us (largely used for configuration)
1100 * @param sa the address of the peer (IPv4-only)
1103 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1104 const struct sockaddr_in *sa)
1106 char inet4[INET_ADDRSTRLEN];
1107 char port_as_string[6];
1108 struct GNUNET_OS_Process *proc;
1110 if (GNUNET_YES != h->enable_nat_client)
1111 return; /* not permitted / possible */
1113 if (h->internal_address == NULL)
1115 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1117 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1120 GNUNET_assert (sa->sin_family == AF_INET);
1121 if (NULL == inet_ntop (AF_INET,
1123 inet4, INET_ADDRSTRLEN))
1125 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1128 GNUNET_snprintf(port_as_string,
1129 sizeof (port_as_string),
1133 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1135 _("Running gnunet-nat-client %s %s %u\n"),
1136 h->internal_address,
1138 (unsigned int) h->adv_port);
1140 proc = GNUNET_OS_start_process (NULL,
1142 "gnunet-nat-client",
1143 "gnunet-nat-client",
1144 h->internal_address,
1150 /* we know that the gnunet-nat-client will terminate virtually
1152 GNUNET_OS_process_wait (proc);
1153 GNUNET_OS_process_close (proc);
1158 * Test if the given address is (currently) a plausible IP address for this peer.
1160 * @param h the handle returned by register
1161 * @param addr IP address to test (IPv4 or IPv6)
1162 * @param addrlen number of bytes in addr
1163 * @return GNUNET_YES if the address is plausible,
1164 * GNUNET_NO if the address is not plausible,
1165 * GNUNET_SYSERR if the address is malformed
1168 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1172 struct LocalAddressList *pos;
1173 const struct sockaddr_in *in4;
1174 const struct sockaddr_in6 *in6;
1176 if ( (addrlen != sizeof (struct in_addr)) &&
1177 (addrlen != sizeof (struct in6_addr)) )
1180 return GNUNET_SYSERR;
1185 if (pos->addrlen == sizeof (struct sockaddr_in))
1187 in4 = (struct sockaddr_in* ) &pos[1];
1188 if ( (addrlen == sizeof (struct in_addr)) &&
1189 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))) )
1192 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1194 in6 = (struct sockaddr_in6* ) &pos[1];
1195 if ( (addrlen == sizeof (struct in6_addr)) &&
1196 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))) )