2 This file is part of GNUnet.
3 Copyright (C) 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file nat/nat_auto.c
23 * @brief functions for auto-configuration of the network
24 * @author Christian Grothoff
25 * @author Bruno Cabral
28 #include "gnunet_util_lib.h"
29 #include "gnunet_resolver_service.h"
30 #include "gnunet_nat_lib.h"
33 #define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
37 * How long do we wait for the NAT test to report success?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
41 #define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply ( \
42 GNUNET_TIME_UNIT_SECONDS, 10)
45 * Phases of the auto configuration.
50 * Initial start value.
55 * Test our external IP.
60 * Test our external IP.
65 * Test our internal IP.
70 * Test if NAT was punched.
75 * Test if UPnP is working.
80 * Test if ICMP server works.
85 * Test if ICMP client works.
90 * Last phase, we're done.
97 * Handle to auto-configuration in progress.
99 struct GNUNET_NAT_AutoHandle
102 * Handle to the active NAT test.
104 struct GNUNET_NAT_Test *tst;
107 * Function to call when done.
109 GNUNET_NAT_AutoResultCallback fin_cb;
112 * Closure for @e fin_cb.
117 * Handle for active 'GNUNET_NAT_mini_get_external_ipv4'-operation.
119 struct GNUNET_NAT_ExternalHandle *eh;
122 * Current configuration (with updates from previous phases)
124 struct GNUNET_CONFIGURATION_Handle *cfg;
127 * Original configuration (used to calculate differences)
129 struct GNUNET_CONFIGURATION_Handle *initial_cfg;
132 * Task identifier for the timeout.
134 struct GNUNET_SCHEDULER_Task *task;
137 * Message queue to the gnunet-nat-server.
139 struct GNUNET_MQ_Handle *mq;
142 * Where are we in the test?
144 enum AutoPhase phase;
147 * Situation of the NAT
149 enum GNUNET_NAT_Type type;
157 * UPnP already set the external ip address ?
159 int upnp_set_external_address;
162 * Did the external server connected back ?
167 * Address detected by STUN
171 unsigned int stun_port;
174 * Internal IP is the same as the public one ?
176 int internal_ip_is_public;
179 * Error code for better debugging and user feedback
181 enum GNUNET_NAT_StatusCode ret;
186 * The listen socket of the service for IPv4
188 static struct GNUNET_NETWORK_Handle *lsock4;
191 * The listen task ID for IPv4
193 static struct GNUNET_SCHEDULER_Task *ltask4;
196 * The port the test service is running on (default 7895)
198 static unsigned long long port = 7895;
200 static char *stun_server = "stun.ekiga.net";
202 static unsigned int stun_port = 3478;
206 * Run the next phase of the auto test.
208 * @param ah auto test handle
211 next_phase (struct GNUNET_NAT_AutoHandle *ah);
215 process_stun_reply (struct sockaddr_in *answer,
216 struct GNUNET_NAT_AutoHandle *ah)
218 ah->stun_ip = inet_ntoa (answer->sin_addr);
219 ah->stun_port = ntohs (answer->sin_port);
220 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
221 "External IP is: %s , with port %u\n",
229 * Function that terminates the test.
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "Stopping STUN and quitting...\n");
239 GNUNET_SCHEDULER_cancel (ltask4);
245 GNUNET_NETWORK_socket_close (lsock4);
252 * Activity on our incoming socket. Read data from the
253 * incoming connection.
258 do_udp_read (void *cls)
260 struct GNUNET_NAT_AutoHandle *ah = cls;
261 unsigned char reply_buf[1024];
263 struct sockaddr_in answer;
264 const struct GNUNET_SCHEDULER_TaskContext *tc;
266 tc = GNUNET_SCHEDULER_get_task_context ();
267 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
268 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
271 rlen = GNUNET_NETWORK_socket_recv (lsock4,
275 // Lets handle the packet
276 memset (&answer, 0, sizeof(struct sockaddr_in));
277 if (ah->phase == AUTO_NAT_PUNCHED)
279 // Destroy the connection
280 GNUNET_NETWORK_socket_close (lsock4);
281 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
282 "The external server was able to connect back");
283 ah->connected_back = GNUNET_YES;
289 GNUNET_NAT_stun_handle_packet (reply_buf, rlen, &answer))
291 // Process the answer
292 process_stun_reply (&answer, ah);
302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
303 "TIMEOUT while waiting for an answer\n");
304 if (ah->phase == AUTO_NAT_PUNCHED)
315 * Create an IPv4 listen socket bound to our port.
317 * @return NULL on error
319 static struct GNUNET_NETWORK_Handle *
322 struct GNUNET_NETWORK_Handle *ls;
323 struct sockaddr_in sa4;
326 memset (&sa4, 0, sizeof(sa4));
327 sa4.sin_family = AF_INET;
328 sa4.sin_port = htons (port);
329 #if HAVE_SOCKADDR_IN_SIN_LEN
330 sa4.sin_len = sizeof(sa4);
332 ls = GNUNET_NETWORK_socket_create (AF_INET,
338 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
342 GNUNET_NETWORK_socket_close (ls);
351 request_callback (void *cls,
352 enum GNUNET_NAT_StatusCode result)
354 // struct GNUNET_NAT_AutoHandle *ah = cls;
356 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
357 "Request callback: stop and quit\n");
360 // next_phase (ah); FIXME this always will be NULL, as called in test_stun()
365 * Function called by NAT to report the outcome of the nat-test.
366 * Clean up and update GUI.
368 * @param cls the auto handle
369 * @param success currently always #GNUNET_OK
370 * @param emsg NULL on success, otherwise an error message
373 result_callback (void *cls,
374 enum GNUNET_NAT_StatusCode ret)
376 struct GNUNET_NAT_AutoHandle *ah = cls;
378 if (GNUNET_NAT_ERROR_SUCCESS == ret)
379 GNUNET_NAT_test_stop (ah->tst);
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
383 GNUNET_NAT_ERROR_SUCCESS == ret
384 ? _ ("NAT traversal with ICMP Server succeeded.\n")
385 : _ ("NAT traversal with ICMP Server failed.\n"));
386 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER",
387 GNUNET_NAT_ERROR_SUCCESS == ret ?
394 * Main function for the connection reversal test.
396 * @param cls the `struct GNUNET_NAT_AutoHandle`
399 reversal_test (void *cls)
401 struct GNUNET_NAT_AutoHandle *ah = cls;
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
405 _ ("Testing connection reversal with ICMP server.\n"));
406 GNUNET_RESOLVER_connect (ah->cfg);
407 ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
408 &result_callback, ah);
413 * Set our external IPv4 address based on the UPnP.
416 * @param cls closure with our setup context
417 * @param addr the address, NULL on errors
418 * @param emsg NULL on success, otherwise an error message
421 set_external_ipv4 (void *cls,
422 const struct in_addr *addr,
423 enum GNUNET_NAT_StatusCode ret)
425 struct GNUNET_NAT_AutoHandle *ah = cls;
426 char buf[INET_ADDRSTRLEN];
430 if (GNUNET_NAT_ERROR_SUCCESS != ret)
435 /* enable 'behind nat' */
436 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
437 _ ("Detected external IP `%s'\n"),
442 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES");
444 /* set external IP address */
445 if (NULL == inet_ntop (AF_INET, addr, buf, sizeof(buf)))
448 /* actually, this should never happen, as the caller already executed just
449 * this check, but for consistency (eg: future changes in the caller)
450 * we still need to report this error...
452 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
456 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
458 ah->upnp_set_external_address = GNUNET_YES;
464 * Determine our external IPv4 address.
466 * @param ah auto setup context
469 test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
471 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
475 /* try to detect external IP */
476 ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT,
477 &set_external_ipv4, ah);
482 * Determine our external IPv4 address and port using an external STUN server
484 * @param ah auto setup context
487 test_stun (struct GNUNET_NAT_AutoHandle *ah)
489 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
491 /* Get port from the configuration */
493 GNUNET_CONFIGURATION_get_value_number (ah->cfg,
501 // Lets create the socket
505 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
511 // Lets call our function now when it accepts
512 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "STUN service listens on port %u\n",
521 (unsigned int) port);
523 GNUNET_NAT_stun_make_request (stun_server,
529 /*An error happened*/
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n");
538 * Process list of local IP addresses. Find and set the
539 * one of the default interface.
541 * @param cls our `struct GNUNET_NAT_AutoHandle`
542 * @param name name of the interface (can be NULL for unknown)
543 * @param isDefault is this presumably the default interface
544 * @param addr address of this interface (can be NULL for unknown or unassigned)
545 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
546 * @param netmask the network mask (can be NULL for unknown or unassigned))
547 * @param addrlen length of the @a addr and @a broadcast_addr
548 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
551 process_if (void *cls,
554 const struct sockaddr *addr,
555 const struct sockaddr *broadcast_addr,
556 const struct sockaddr *netmask,
559 struct GNUNET_NAT_AutoHandle *ah = cls;
560 const struct sockaddr_in *in;
561 char buf[INET_ADDRSTRLEN];
564 if ((sizeof(struct sockaddr_in6) == addrlen) &&
565 (0 != GNUNET_memcmp (&in6addr_loopback, &((const struct
566 sockaddr_in6 *) addr)->
568 (! IN6_IS_ADDR_LINKLOCAL (&((const struct
569 sockaddr_in6 *) addr)->sin6_addr)))
571 ah->have_v6 = GNUNET_YES;
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
574 "This system has a global IPv6 address, setting IPv6 to supported.\n"));
578 if (addrlen != sizeof(struct sockaddr_in))
580 in = (const struct sockaddr_in *) addr;
583 /* set internal IP address */
584 if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof(buf)))
589 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
591 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
592 _ ("Detected internal network address `%s'.\n"),
596 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
598 /* Check if our internal IP is the same as the External detect by STUN*/
599 if (ah->stun_ip && (strcmp (buf, ah->stun_ip) == 0))
601 ah->internal_ip_is_public = GNUNET_YES;
602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
603 "A internal IP is the sameas the external");
604 /* No need to continue*/
605 return GNUNET_SYSERR;
608 /* no need to continue iteration if we found the default */
612 return GNUNET_SYSERR;
617 * Determine our local IP addresses; detect internal IP & IPv6-support
619 * @param ah auto setup context
622 test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
624 ah->have_v6 = GNUNET_NO;
625 ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP
626 GNUNET_OS_network_interfaces_list (&process_if, ah);
628 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6",
629 (GNUNET_YES == ah->have_v6) ? "NO" :
636 * We got disconnected from the NAT server. Stop
637 * waiting for a reply.
639 * @param cls the `struct GNUNET_NAT_AutoHandle`
640 * @param error error code
643 mq_error_handler (void *cls,
644 enum GNUNET_MQ_Error error)
646 struct GNUNET_NAT_AutoHandle *ah = cls;
648 GNUNET_MQ_destroy (ah->mq);
650 /* wait a bit first? */
656 * Test if NAT has been punched
658 * @param ah auto setup context
661 test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
663 struct GNUNET_NAT_TestMessage *msg;
664 struct GNUNET_MQ_Envelope *env;
668 LOG (GNUNET_ERROR_TYPE_INFO,
669 "We don't have a STUN IP");
674 LOG (GNUNET_ERROR_TYPE_INFO,
675 "Asking gnunet-nat-server to connect to `%s'\n",
677 ah->mq = GNUNET_CLIENT_connect (ah->cfg,
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685 _ ("Failed to connect to `gnunet-nat-server'\n"));
689 env = GNUNET_MQ_msg (msg,
690 GNUNET_MESSAGE_TYPE_NAT_TEST);
691 msg->dst_ipv4 = inet_addr (ah->stun_ip);
692 msg->dport = htons (ah->stun_port);
694 msg->is_tcp = htonl ((uint32_t) GNUNET_NO);
695 GNUNET_MQ_send (ah->mq,
699 GNUNET_SCHEDULER_cancel (ltask4);
700 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
709 * Test if UPnPC works.
711 * @param ah auto setup context
714 test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
718 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
721 // test if upnpc is available
722 have_upnpc = (GNUNET_SYSERR !=
723 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL));
724 // FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP
725 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
727 ? _ ("upnpc found, enabling its use\n")
728 : _ ("upnpc not found\n"));
729 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_UPNP",
730 (GNUNET_YES == have_upnpc) ? "YES" :
737 * Test if ICMP server is working
739 * @param ah auto setup context
742 test_icmp_server (struct GNUNET_NAT_AutoHandle *ah)
755 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
757 GNUNET_CONFIGURATION_get_value_string (ah->cfg,
764 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
766 "test_icmp_server not possible, as we have no public IPv4 address\n"));
772 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg,
777 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
779 "test_icmp_server not possible, as we are not behind NAT\n"));
785 GNUNET_OS_check_helper_binary (helper,
789 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
790 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
791 _ ("No working gnunet-helper-nat-server found\n"));
794 GNUNET_free_non_null (tmp);
795 GNUNET_free (helper);
797 if ((GNUNET_OK == ext_ip) &&
798 (GNUNET_YES == nated) &&
799 (GNUNET_OK == binary))
800 ah->task = GNUNET_SCHEDULER_add_now (&reversal_test,
808 * Test if ICMP client is working
810 * @param ah auto setup context
813 test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
819 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
821 GNUNET_CONFIGURATION_get_value_string (ah->cfg,
827 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
829 "test_icmp_client not possible, as we have no internal IPv4 address\n"));
835 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg,
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
841 "test_icmp_server not possible, as we are not behind NAT\n"));
847 GNUNET_OS_check_helper_binary (helper,
849 "-d 127.0.0.1 127.0.0.2 42"))
851 // none of these parameters are actually used in privilege testing mode
852 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
853 _ ("No working gnunet-helper-nat-server found\n"));
856 GNUNET_free_non_null (tmp);
857 GNUNET_free (helper);
864 * Run the next phase of the auto test.
867 next_phase (struct GNUNET_NAT_AutoHandle *ah)
869 struct GNUNET_CONFIGURATION_Handle *diff;
878 case AUTO_EXTERNAL_IP:
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Will run AUTO_EXTERNAL_IP\n");
881 test_external_ip (ah);
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Will run AUTO_STUN\n");
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Will run AUTO_LOCAL_IP\n");
896 case AUTO_NAT_PUNCHED:
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Will run AUTO_NAT_PUNCHED\n");
899 test_nat_punched (ah);
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Will run AUTO_UPNPC\n");
908 case AUTO_ICMP_SERVER:
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "Will run AUTO_ICMP_SERVER\n");
911 test_icmp_server (ah);
914 case AUTO_ICMP_CLIENT:
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Will run AUTO_ICMP_CLIENT\n");
917 test_icmp_client (ah);
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Done with tests\n");
923 if (! ah->internal_ip_is_public)
925 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
930 if (ah->connected_back)
932 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
939 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
947 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
951 if (ah->connected_back)
953 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT;
954 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
961 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
962 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
968 if (0 != ah->stun_port)
970 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
978 // The internal IP is the same as public, but we didn't got a incoming connection
979 if (ah->connected_back)
981 ah->type = GNUNET_NAT_TYPE_NO_NAT;
982 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
989 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
993 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
996 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
1001 if (0 != ah->stun_port)
1003 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
1011 diff = GNUNET_CONFIGURATION_get_diff (ah->initial_cfg,
1015 ah->fin_cb (ah->fin_cb_cls,
1019 GNUNET_CONFIGURATION_destroy (diff);
1020 GNUNET_NAT_autoconfig_cancel (ah);
1026 * Start auto-configuration routine. The resolver service should
1027 * be available when this function is called.
1029 * @param cfg initial configuration
1030 * @param cb function to call with autoconfiguration result
1031 * @param cb_cls closure for @a cb
1032 * @return handle to cancel operation
1034 struct GNUNET_NAT_AutoHandle *
1035 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1036 GNUNET_NAT_AutoResultCallback cb,
1039 struct GNUNET_NAT_AutoHandle *ah;
1041 ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
1043 ah->fin_cb_cls = cb_cls;
1044 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
1045 ah->cfg = GNUNET_CONFIGURATION_dup (cfg);
1046 ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg);
1048 /* never use loopback addresses if user wanted autoconfiguration */
1049 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
1060 * Abort autoconfiguration.
1062 * @param ah handle for operation to abort
1065 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
1067 if (NULL != ah->tst)
1069 GNUNET_NAT_test_stop (ah->tst);
1074 GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh);
1079 GNUNET_MQ_destroy (ah->mq);
1082 if (NULL != ah->task)
1084 GNUNET_SCHEDULER_cancel (ah->task);
1087 GNUNET_CONFIGURATION_destroy (ah->cfg);
1088 GNUNET_CONFIGURATION_destroy (ah->initial_cfg);
1093 /* end of nat_auto.c */