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 * ID of interface IP-scan task
183 GNUNET_SCHEDULER_TaskIdentifier ifc_task;
186 * The process id of the server process (if behind NAT)
188 struct GNUNET_OS_Process *server_proc;
191 * LAN address as passed by the caller (array).
193 struct sockaddr **local_addrs;
196 * Length of the 'local_addrs'.
198 socklen_t *local_addrlens;
201 * Number of entries in 'local_addrs' array.
203 unsigned int num_local_addrs;
206 * The our external address (according to config, UPnP may disagree...)
208 char *external_address;
211 * Presumably our internal address (according to config)
213 char *internal_address;
216 * Is this transport configured to be behind a NAT?
221 * Has the NAT been punched? (according to config)
226 * Is this transport configured to allow connections to NAT'd peers?
228 int enable_nat_client;
231 * Should we run the gnunet-nat-server?
233 int enable_nat_server;
236 * Are we allowed to try UPnP/PMP for NAT traversal?
241 * Should we use local addresses (loopback)? (according to config)
243 int use_localaddresses;
246 * Is using IPv6 disabled?
251 * Is this TCP or UDP?
256 * Port we advertise to the outside.
264 * Try to start the gnunet-nat-server (if it is not
267 * @param h handle to NAT
270 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
274 * Add the given address to the list of 'local' addresses, thereby
275 * making it a 'legal' address for this peer to have.
277 * @param plugin the plugin
278 * @param src where did the local address originate from?
279 * @param arg the address, some 'struct sockaddr'
280 * @param arg_size number of bytes in arg
283 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
284 enum LocalAddressSource src,
285 const struct sockaddr *arg,
288 struct LocalAddressList *lal;
290 lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
291 memcpy (&lal[1], arg, arg_size);
292 lal->addrlen = arg_size;
294 GNUNET_CONTAINER_DLL_insert (h->lal_head,
297 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
299 "Adding address `%s' from source %d\n",
300 GNUNET_a2s (arg, arg_size),
302 h->address_callback (h->callback_cls,
310 * Add the given address to the list of 'local' addresses, thereby
311 * making it a 'legal' address for this peer to have. Set the
312 * port number in the process to the advertised port and possibly
313 * also to zero (if we have the gnunet-nat-server).
315 * @param plugin the plugin
316 * @param src where did the local address originate from?
317 * @param arg the address, some 'struct sockaddr'
318 * @param arg_size number of bytes in arg
321 add_to_address_list (struct GNUNET_NAT_Handle *h,
322 enum LocalAddressSource src,
323 const struct sockaddr *arg,
326 struct sockaddr_in s4;
327 const struct sockaddr_in *in4;
328 struct sockaddr_in6 s6;
329 const struct sockaddr_in6 *in6;
331 if (arg_size == sizeof (struct sockaddr_in))
333 in4 = (const struct sockaddr_in *) arg;
335 s4.sin_port = htons (h->adv_port);
336 add_to_address_list_as_is (h,
338 (const struct sockaddr*) &s4,
339 sizeof (struct sockaddr_in));
340 if (GNUNET_YES == h->enable_nat_server)
342 /* also add with PORT = 0 to indicate NAT server is enabled */
343 s4.sin_port = htons(0);
344 add_to_address_list_as_is (h,
346 (const struct sockaddr*) &s4,
347 sizeof (struct sockaddr_in));
350 else if (arg_size == sizeof (struct sockaddr_in6))
352 if (GNUNET_YES != h->disable_ipv6)
354 in6 = (const struct sockaddr_in6 *) arg;
356 s6.sin6_port = htons(h->adv_port);
357 add_to_address_list_as_is (h,
359 (const struct sockaddr*) &s6,
360 sizeof (struct sockaddr_in6));
371 * Add the given IP address to the list of 'local' addresses, thereby
372 * making it a 'legal' address for this peer to have.
374 * @param plugin the plugin
375 * @param src where did the local address originate from?
376 * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
377 * @param arg_size number of bytes in arg
380 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
381 enum LocalAddressSource src,
385 struct sockaddr_in s4;
386 const struct in_addr *in4;
387 struct sockaddr_in6 s6;
388 const struct in6_addr *in6;
390 if (addrlen == sizeof (struct in_addr))
392 in4 = (const struct in_addr *) addr;
393 memset (&s4, 0, sizeof (s4));
394 s4.sin_family = AF_INET;
396 #if HAVE_SOCKADDR_IN_SIN_LEN
397 s4.sin_len = (u_char) sizeof (struct sockaddr_in);
400 add_to_address_list (h,
402 (const struct sockaddr*) &s4,
403 sizeof (struct sockaddr_in));
404 if (GNUNET_YES == h->enable_nat_server)
406 /* also add with PORT = 0 to indicate NAT server is enabled */
407 s4.sin_port = htons(0);
408 add_to_address_list (h,
410 (const struct sockaddr*) &s4,
411 sizeof (struct sockaddr_in));
415 else if (addrlen == sizeof (struct in6_addr))
417 if (GNUNET_YES != h->disable_ipv6)
419 in6 = (const struct in6_addr *) addr;
420 memset (&s6, 0, sizeof (s6));
421 s6.sin6_family = AF_INET6;
422 s6.sin6_port = htons(h->adv_port);
423 #if HAVE_SOCKADDR_IN_SIN_LEN
424 s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
427 add_to_address_list (h,
429 (const struct sockaddr*) &s6,
430 sizeof (struct sockaddr_in6));
441 * Our (external) hostname was resolved and the configuration says that
442 * the NAT was hole-punched.
444 * @param cls the 'struct Plugin'
445 * @param addr NULL on error, otherwise result of DNS lookup
446 * @param addrlen number of bytes in addr
449 process_external_ip (void *cls,
450 const struct sockaddr *addr,
453 struct GNUNET_NAT_Handle *h = cls;
458 /* FIXME: schedule task to resolve IP again in the
459 future, and if the result changes, update the
460 local address list accordingly */
463 add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
468 * Function called by the resolver for each address obtained from DNS
469 * for our own hostname. Add the addresses to the list of our IP
473 * @param addr one of the addresses of the host, NULL for the last address
474 * @param addrlen length of the address
477 process_hostname_ip (void *cls,
478 const struct sockaddr *addr, socklen_t addrlen)
480 struct GNUNET_NAT_Handle *h = cls;
484 h->hostname_dns = NULL;
485 /* FIXME: schedule task to resolve IP again in the
486 future, and if the result changes, update the
487 address list accordingly */
490 add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
495 * Add the IP of our network interface to the list of
498 * @param cls the 'struct GNUNET_NAT_Handle'
499 * @param name name of the interface
500 * @param isDefault do we think this may be our default interface
501 * @param addr address of the interface
502 * @param addrlen number of bytes in addr
503 * @return GNUNET_OK to continue iterating
506 process_interfaces (void *cls,
509 const struct sockaddr *addr, socklen_t addrlen)
511 struct GNUNET_NAT_Handle *h = cls;
512 const struct sockaddr_in *s4;
513 const struct sockaddr_in6 *s6;
515 char buf[INET6_ADDRSTRLEN];
517 switch (addr->sa_family)
520 s4 = (struct sockaddr_in *) addr;
522 if (GNUNET_YES == h->use_localaddresses)
523 add_ip_to_address_list (h,
524 LAL_INTERFACE_ADDRESS,
526 sizeof (struct in_addr));
529 s6 = (struct sockaddr_in6 *) addr;
530 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
532 /* skip link local addresses */
536 if (GNUNET_YES == h->use_localaddresses)
537 add_ip_to_address_list (h,
538 LAL_INTERFACE_ADDRESS,
540 sizeof (struct in6_addr));
546 if ( (h->internal_address == NULL) &&
547 (h->server_proc == NULL) &&
548 (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
549 (GNUNET_YES == isDefault) &&
550 ( (addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6) ) )
552 /* no internal address configured, but we found a "default"
553 interface, try using that as our 'internal' address */
554 h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
558 start_gnunet_nat_server (h);
565 * Return the actual path to a file found in the current
566 * PATH environment variable.
568 * @param binary the name of the file to find
569 * @return path to binary, NULL if not found
572 get_path_from_PATH (const char *binary)
583 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
585 _("PATH environment variable is unset.\n"));
588 path = GNUNET_strdup (p); /* because we write on it */
589 buf = GNUNET_malloc (strlen (path) + 20);
592 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
595 sprintf (buf, "%s/%s", pos, binary);
596 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
603 sprintf (buf, "%s/%s", pos, binary);
604 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
616 * Check whether the suid bit is set on a file.
617 * Attempts to find the file using the current
618 * PATH environment variable as a search path.
620 * @param binary the name of the file to check
621 * @return GNUNET_YES if the file is SUID,
623 * GNUNET_SYSERR on error
626 check_gnunet_nat_binary (const char *binary)
634 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
635 p = get_path_from_PATH (binaryexe);
638 p = get_path_from_PATH (binary);
642 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
644 _("Could not find binary `%s' in PATH!\n"),
648 if (0 != STAT (p, &statbuf))
650 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
651 _("stat (%s) failed: %s\n"),
655 return GNUNET_SYSERR;
659 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
660 (statbuf.st_uid == 0) )
664 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
665 if (INVALID_SOCKET == rawsock)
667 DWORD err = GetLastError ();
668 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
670 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
671 return GNUNET_NO; /* not running as administrator */
673 closesocket (rawsock);
680 * Task that restarts the gnunet-nat-server process after a crash
681 * after a certain delay.
683 * @param cls the 'struct GNUNET_NAT_Handle'
684 * @param tc scheduler context
687 restart_nat_server (void *cls,
688 const struct GNUNET_SCHEDULER_TaskContext *tc)
690 struct GNUNET_NAT_Handle *h = cls;
692 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
693 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
695 start_gnunet_nat_server (h);
700 * We have been notified that gnunet-nat-server has written something to stdout.
701 * Handle the output, then reschedule this function to be called again once
704 * @param cls the NAT handle
705 * @param tc the scheduling context
708 nat_server_read (void *cls,
709 const struct GNUNET_SCHEDULER_TaskContext *tc)
711 struct GNUNET_NAT_Handle *h = cls;
716 const char *port_start;
717 struct sockaddr_in sin_addr;
719 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
720 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
722 memset (mybuf, 0, sizeof(mybuf));
723 bytes = GNUNET_DISK_file_read(h->server_stdout_handle,
729 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
731 "Finished reading from server stdout with code: %d\n",
734 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
735 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
736 GNUNET_OS_process_wait (h->server_proc);
737 GNUNET_OS_process_close (h->server_proc);
738 h->server_proc = NULL;
739 GNUNET_DISK_pipe_close (h->server_stdout);
740 h->server_stdout = NULL;
741 h->server_stdout_handle = NULL;
742 /* now try to restart it */
743 h->server_retry_delay = GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
744 h->server_retry_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
745 h->server_retry_delay);
746 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
753 for (i = 0; i < sizeof(mybuf); i++)
755 if (mybuf[i] == '\n')
760 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
763 port_start = &mybuf[i + 1];
767 /* construct socket address of sender */
768 memset (&sin_addr, 0, sizeof (sin_addr));
769 sin_addr.sin_family = AF_INET;
770 #if HAVE_SOCKADDR_IN_SIN_LEN
771 sin_addr.sin_len = sizeof (sin_addr);
773 if ( (NULL == port_start) ||
774 (1 != sscanf (port_start, "%d", &port)) ||
775 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
777 /* should we restart gnunet-nat-server? */
778 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
780 _("gnunet-nat-server generated malformed address `%s'\n"),
783 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
784 h->server_stdout_handle,
789 sin_addr.sin_port = htons((uint16_t) port);
791 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
793 "gnunet-nat-server read: %s:%d\n",
796 h->reversal_callback (h->callback_cls,
797 (const struct sockaddr*) &sin_addr,
799 h->server_read_task =
800 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
801 h->server_stdout_handle,
808 * Try to start the gnunet-nat-server (if it is not
811 * @param h handle to NAT
814 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
816 if ( (h->behind_nat == GNUNET_YES) &&
817 (h->enable_nat_server == GNUNET_YES) &&
818 (h->internal_address != NULL) &&
819 (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
824 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
826 "Starting %s at `%s'\n",
828 h->internal_address);
830 /* Start the server process */
831 h->server_proc = GNUNET_OS_start_process (NULL,
837 if (h->server_proc == NULL)
839 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
841 _("Failed to start %s\n"),
842 "gnunet-nat-server");
843 GNUNET_DISK_pipe_close (h->server_stdout);
844 h->server_stdout = NULL;
848 /* Close the write end of the read pipe */
849 GNUNET_DISK_pipe_close_end(h->server_stdout,
850 GNUNET_DISK_PIPE_END_WRITE);
851 h->server_stdout_handle
852 = GNUNET_DISK_pipe_handle (h->server_stdout,
853 GNUNET_DISK_PIPE_END_READ);
855 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
856 h->server_stdout_handle,
865 * Task to scan the local network interfaces for IP addresses.
867 * @param cls the NAT handle
868 * @param tc scheduler context
871 list_interfaces (void *cls,
872 const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct GNUNET_NAT_Handle *h = cls;
876 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
877 GNUNET_OS_network_interfaces_list (&process_interfaces, h);
879 h->ifc_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FIXME,
880 &list_interfaces, h);
886 * Attempt to enable port redirection and detect public IP address contacting
887 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
888 * of the local host's addresses should the external port be mapped. The port
889 * is taken from the corresponding sockaddr_in[6] field.
891 * @param cfg configuration to use
892 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
893 * @param adv_port advertised port (port we are either bound to or that our OS
894 * locally performs redirection from to our bound port).
895 * @param num_addrs number of addresses in 'addrs'
896 * @param addr the local address packets should be redirected to
897 * @param addrlen actual lenght of the address
898 * @param address_callback function to call everytime the public IP address changes
899 * @param reversal_callback function to call if someone wants connection reversal from us
900 * @param callback_cls closure for callbacks
901 * @return NULL on error, otherwise handle that can be used to unregister
903 struct GNUNET_NAT_Handle *
904 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
907 unsigned int num_addrs,
908 const struct sockaddr **addrs,
909 const socklen_t *addrlens,
910 GNUNET_NAT_AddressCallback address_callback,
911 GNUNET_NAT_ReversalCallback reversal_callback,
914 struct GNUNET_NAT_Handle *h;
915 struct in_addr in_addr;
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919 "Registered with NAT service at port %u with %u IP bound local addresses\n",
920 (unsigned int) adv_port,
922 h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
923 h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
926 h->address_callback = address_callback;
927 h->reversal_callback = reversal_callback;
928 h->callback_cls = callback_cls;
929 h->num_local_addrs = num_addrs;
930 h->adv_port = adv_port;
933 h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr*));
934 h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
935 for (i=0;i<num_addrs;i++)
937 h->local_addrlens[i] = addrlens[i];
938 h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
939 memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
944 GNUNET_CONFIGURATION_have_value (cfg,
948 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
951 &h->internal_address);
953 if ( (h->internal_address != NULL) &&
954 (inet_pton(AF_INET, h->internal_address, &in_addr) != 1) )
956 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
958 _("Malformed %s `%s' given in configuration!\n"),
960 h->internal_address);
961 GNUNET_free (h->internal_address);
962 h->internal_address = NULL;
966 GNUNET_CONFIGURATION_have_value (cfg,
970 (void) GNUNET_CONFIGURATION_get_value_string (cfg,
973 &h->external_address);
975 if ( (h->external_address != NULL) &&
976 (inet_pton(AF_INET, h->external_address, &in_addr) != 1) )
978 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
980 _("Malformed %s `%s' given in configuration!\n"),
982 h->external_address);
983 GNUNET_free (h->external_address);
984 h->external_address = NULL;
986 h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
989 h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
992 h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
994 "ENABLE_NAT_CLIENT");
995 h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
997 "ENABLE_NAT_SERVER");
998 h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1001 h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1004 h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno(cfg,
1007 if (NULL == reversal_callback)
1008 h->enable_nat_server = GNUNET_NO;
1010 /* Check if NAT was hole-punched */
1011 if ( (NULL != h->address_callback) &&
1012 (h->external_address != NULL) &&
1013 (h->nat_punched == GNUNET_YES) )
1015 h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
1017 GNUNET_TIME_UNIT_MINUTES,
1018 &process_external_ip,
1020 h->enable_nat_server = GNUNET_NO;
1021 h->enable_upnp = GNUNET_NO;
1024 /* Test for SUID binaries */
1025 if ( (h->behind_nat == GNUNET_YES) &&
1026 (GNUNET_YES == h->enable_nat_server) &&
1027 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
1029 h->enable_nat_server = GNUNET_NO;
1030 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1031 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1032 "gnunet-nat-server");
1034 if ( (GNUNET_YES == h->enable_nat_client) &&
1035 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
1037 h->enable_nat_client = GNUNET_NO;
1038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1039 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
1040 "gnunet-nat-client");
1043 start_gnunet_nat_server (h);
1045 /* FIXME: add support for UPnP, etc */
1047 if (NULL != h->address_callback)
1049 h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1050 h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
1051 HOSTNAME_RESOLVE_TIMEOUT,
1052 &process_hostname_ip,
1060 * Stop port redirection and public IP address detection for the given handle.
1061 * This frees the handle, after having sent the needed commands to close open ports.
1063 * @param h the handle to stop
1066 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1069 struct LocalAddressList *lal;
1071 if (h->ext_dns != NULL)
1073 GNUNET_RESOLVER_request_cancel (h->ext_dns);
1076 if (NULL != h->hostname_dns)
1078 GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1079 h->hostname_dns = NULL;
1081 if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1083 GNUNET_SCHEDULER_cancel (h->server_read_task);
1084 h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1086 if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1088 GNUNET_SCHEDULER_cancel (h->ifc_task);
1089 h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1091 if (NULL != h->server_proc)
1093 if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1094 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1095 GNUNET_OS_process_wait (h->server_proc);
1096 GNUNET_OS_process_close (h->server_proc);
1097 h->server_proc = NULL;
1098 GNUNET_DISK_pipe_close (h->server_stdout);
1099 h->server_stdout = NULL;
1100 h->server_stdout_handle = NULL;
1102 if (NULL != h->server_stdout)
1104 GNUNET_DISK_pipe_close (h->server_stdout);
1105 h->server_stdout = NULL;
1106 h->server_stdout_handle = NULL;
1108 while (NULL != (lal = h->lal_head))
1110 GNUNET_CONTAINER_DLL_remove (h->lal_head,
1113 h->address_callback (h->callback_cls,
1115 (const struct sockaddr*) &lal[1],
1119 for (i=0;i<h->num_local_addrs;i++)
1120 GNUNET_free (h->local_addrs[i]);
1121 GNUNET_free_non_null (h->local_addrs);
1122 GNUNET_free_non_null (h->local_addrlens);
1123 GNUNET_free_non_null (h->external_address);
1124 GNUNET_free_non_null (h->internal_address);
1130 * We learned about a peer (possibly behind NAT) so run the
1131 * gnunet-nat-client to send dummy ICMP responses to cause
1132 * that peer to connect to us (connection reversal).
1134 * @param h NAT handle for us (largely used for configuration)
1135 * @param sa the address of the peer (IPv4-only)
1138 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1139 const struct sockaddr_in *sa)
1141 char inet4[INET_ADDRSTRLEN];
1142 char port_as_string[6];
1143 struct GNUNET_OS_Process *proc;
1145 if (GNUNET_YES != h->enable_nat_client)
1146 return; /* not permitted / possible */
1148 if (h->internal_address == NULL)
1150 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1152 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1155 GNUNET_assert (sa->sin_family == AF_INET);
1156 if (NULL == inet_ntop (AF_INET,
1158 inet4, INET_ADDRSTRLEN))
1160 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1163 GNUNET_snprintf(port_as_string,
1164 sizeof (port_as_string),
1168 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1170 _("Running gnunet-nat-client %s %s %u\n"),
1171 h->internal_address,
1173 (unsigned int) h->adv_port);
1175 proc = GNUNET_OS_start_process (NULL,
1177 "gnunet-nat-client",
1178 "gnunet-nat-client",
1179 h->internal_address,
1185 /* we know that the gnunet-nat-client will terminate virtually
1187 GNUNET_OS_process_wait (proc);
1188 GNUNET_OS_process_close (proc);
1193 * Test if the given address is (currently) a plausible IP address for this peer.
1195 * @param h the handle returned by register
1196 * @param addr IP address to test (IPv4 or IPv6)
1197 * @param addrlen number of bytes in addr
1198 * @return GNUNET_YES if the address is plausible,
1199 * GNUNET_NO if the address is not plausible,
1200 * GNUNET_SYSERR if the address is malformed
1203 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1207 struct LocalAddressList *pos;
1208 const struct sockaddr_in *in4;
1209 const struct sockaddr_in6 *in6;
1211 if ( (addrlen != sizeof (struct in_addr)) &&
1212 (addrlen != sizeof (struct in6_addr)) )
1215 return GNUNET_SYSERR;
1220 if (pos->addrlen == sizeof (struct sockaddr_in))
1222 in4 = (struct sockaddr_in* ) &pos[1];
1223 if ( (addrlen == sizeof (struct in_addr)) &&
1224 (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))) )
1227 else if (pos->addrlen == sizeof (struct sockaddr_in6))
1229 in6 = (struct sockaddr_in6* ) &pos[1];
1230 if ( (addrlen == sizeof (struct in6_addr)) &&
1231 (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))) )
1240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1241 "Asked to validate one of my addresses and validation failed!\n");