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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 (GNUNET_TIME_UNIT_SECONDS, 10)
44 * Phases of the auto configuration.
49 * Initial start value.
54 * Test our external IP.
59 * Test our external IP.
64 * Test our internal IP.
69 * Test if NAT was punched.
74 * Test if UPnP is working.
79 * Test if ICMP server works.
84 * Test if ICMP client works.
89 * Last phase, we're done.
97 * Handle to auto-configuration in progress.
99 struct GNUNET_NAT_AutoHandle
103 * Handle to the active NAT test.
105 struct GNUNET_NAT_Test *tst;
108 * Function to call when done.
110 GNUNET_NAT_AutoResultCallback fin_cb;
113 * Closure for @e fin_cb.
118 * Handle for active 'GNUNET_NAT_mini_get_external_ipv4'-operation.
120 struct GNUNET_NAT_ExternalHandle *eh;
123 * Current configuration (with updates from previous phases)
125 struct GNUNET_CONFIGURATION_Handle *cfg;
128 * Original configuration (used to calculate differences)
130 struct GNUNET_CONFIGURATION_Handle *initial_cfg;
133 * Task identifier for the timeout.
135 struct GNUNET_SCHEDULER_Task * task;
138 * Where are we in the test?
140 enum AutoPhase phase;
144 * Situation of the NAT
146 enum GNUNET_NAT_Type type;
154 * UPnP already set the external ip address ?
156 int upnp_set_external_address;
159 * Did the external server connected back ?
164 * Address detected by STUN
170 * Internal IP is the same as the public one ?
172 int internal_ip_is_public;
175 * Error code for better debugging and user feedback
177 enum GNUNET_NAT_StatusCode ret;
186 * The listen socket of the service for IPv4
188 static struct GNUNET_NETWORK_Handle *lsock4;
192 * The listen task ID for IPv4
194 static struct GNUNET_SCHEDULER_Task * ltask4;
200 * The port the test service is running on (default 7895)
202 static unsigned long long port = 7895;
204 static char *stun_server = "stun.ekiga.net";
205 static int stun_port = 3478;
210 * Run the next phase of the auto test.
212 * @param ah auto test handle
215 next_phase (struct GNUNET_NAT_AutoHandle *ah);
221 process_stun_reply(struct sockaddr_in* answer, struct GNUNET_NAT_AutoHandle *ah)
224 ah->stun_ip = inet_ntoa(answer->sin_addr);
225 ah->stun_port = ntohs(answer->sin_port);
226 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "External IP is: %s , with port %d\n", ah->stun_ip, ah->stun_port);
234 * Function that terminates the test.
239 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping STUN and quitting...\n");
244 GNUNET_SCHEDULER_cancel (ltask4);
251 GNUNET_NETWORK_socket_close (lsock4);
257 * Activity on our incoming socket. Read data from the
258 * incoming connection.
263 do_udp_read (void *cls)
265 struct GNUNET_NAT_AutoHandle *ah = cls;
266 unsigned char reply_buf[1024];
268 struct sockaddr_in answer;
269 const struct GNUNET_SCHEDULER_TaskContext *tc;
271 tc = GNUNET_SCHEDULER_get_task_context ();
272 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
273 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
276 rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf));
278 //Lets handle the packet
279 memset(&answer, 0, sizeof(struct sockaddr_in));
280 if(ah->phase == AUTO_NAT_PUNCHED)
282 //Destroy the connection
283 GNUNET_NETWORK_socket_close (lsock4);
284 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
285 "The external server was able to connect back");
286 ah->connected_back = GNUNET_YES;
291 if (GNUNET_OK == GNUNET_NAT_stun_handle_packet (reply_buf, rlen, &answer))
294 process_stun_reply (&answer, ah);
304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
305 "TIMEOUT while waiting for an answer\n");
306 if (ah->phase == AUTO_NAT_PUNCHED)
320 * Create an IPv4 listen socket bound to our port.
322 * @return NULL on error
324 static struct GNUNET_NETWORK_Handle *
327 struct GNUNET_NETWORK_Handle *ls;
328 struct sockaddr_in sa4;
331 memset (&sa4, 0, sizeof (sa4));
332 sa4.sin_family = AF_INET;
333 sa4.sin_port = htons (port);
334 #if HAVE_SOCKADDR_IN_SIN_LEN
335 sa4.sin_len = sizeof (sa4);
337 ls = GNUNET_NETWORK_socket_create (AF_INET,
343 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
347 GNUNET_NETWORK_socket_close (ls);
357 static void request_callback (void *cls,
358 enum GNUNET_NAT_StatusCode result)
360 // struct GNUNET_NAT_AutoHandle *ah = cls;
362 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Request callback: stop and quit\n");
365 // next_phase (ah); FIXME this always will be NULL, as called in test_stun()
373 * Function called by NAT to report the outcome of the nat-test.
374 * Clean up and update GUI.
376 * @param cls the auto handle
377 * @param success currently always #GNUNET_OK
378 * @param emsg NULL on success, otherwise an error message
381 result_callback (void *cls,
382 enum GNUNET_NAT_StatusCode ret)
384 struct GNUNET_NAT_AutoHandle *ah = cls;
386 if (GNUNET_NAT_ERROR_SUCCESS == ret)
387 GNUNET_NAT_test_stop (ah->tst);
390 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
391 GNUNET_NAT_ERROR_SUCCESS == ret
392 ? _("NAT traversal with ICMP Server succeeded.\n")
393 : _("NAT traversal with ICMP Server failed.\n"));
394 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER",
395 GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES");
401 * Main function for the connection reversal test.
403 * @param cls the `struct GNUNET_NAT_AutoHandle`
406 reversal_test (void *cls)
408 struct GNUNET_NAT_AutoHandle *ah = cls;
411 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
412 _("Testing connection reversal with ICMP server.\n"));
413 GNUNET_RESOLVER_connect (ah->cfg);
414 ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
415 &result_callback, ah);
420 * Set our external IPv4 address based on the UPnP.
423 * @param cls closure with our setup context
424 * @param addr the address, NULL on errors
425 * @param emsg NULL on success, otherwise an error message
428 set_external_ipv4 (void *cls,
429 const struct in_addr *addr,
430 enum GNUNET_NAT_StatusCode ret)
432 struct GNUNET_NAT_AutoHandle *ah = cls;
433 char buf[INET_ADDRSTRLEN];
437 if (GNUNET_NAT_ERROR_SUCCESS != ret)
442 /* enable 'behind nat' */
443 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
444 _("Detected external IP `%s'\n"),
449 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES");
451 /* set external IP address */
452 if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf)))
455 /* actually, this should never happen, as the caller already executed just
456 * this check, but for consistency (eg: future changes in the caller)
457 * we still need to report this error...
459 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
463 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
465 ah->upnp_set_external_address = GNUNET_YES;
471 * Determine our external IPv4 address.
473 * @param ah auto setup context
476 test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
478 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
482 /* try to detect external IP */
483 ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT,
484 &set_external_ipv4, ah);
489 * Determine our external IPv4 address and port using an external STUN server
491 * @param ah auto setup context
494 test_stun (struct GNUNET_NAT_AutoHandle *ah)
497 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
499 /* Get port from the configuration */
501 GNUNET_CONFIGURATION_get_value_number (ah->cfg,
509 //Lets create the socket
513 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
519 //Lets call our function now when it accepts
520 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
521 lsock4, &do_udp_read, ah);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "STUN service listens on port %u\n",
529 if (GNUNET_NO == GNUNET_NAT_stun_make_request (stun_server, stun_port,
530 lsock4, &request_callback,
533 /*An error happened*/
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n");
543 * Process list of local IP addresses. Find and set the
544 * one of the default interface.
546 * @param cls our `struct GNUNET_NAT_AutoHandle`
547 * @param name name of the interface (can be NULL for unknown)
548 * @param isDefault is this presumably the default interface
549 * @param addr address of this interface (can be NULL for unknown or unassigned)
550 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
551 * @param netmask the network mask (can be NULL for unknown or unassigned))
552 * @param addrlen length of the @a addr and @a broadcast_addr
553 * @return GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
556 process_if (void *cls,
559 const struct sockaddr *addr,
560 const struct sockaddr *broadcast_addr,
561 const struct sockaddr *netmask,
564 struct GNUNET_NAT_AutoHandle *ah = cls;
565 const struct sockaddr_in *in;
566 char buf[INET_ADDRSTRLEN];
569 if ( (sizeof (struct sockaddr_in6) == addrlen) &&
570 (0 != memcmp (&in6addr_loopback, &((const struct sockaddr_in6 *) addr)->sin6_addr,
571 sizeof (struct in6_addr))) &&
572 (! IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *) addr)->sin6_addr)) )
574 ah->have_v6 = GNUNET_YES;
575 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
576 _("This system has a global IPv6 address, setting IPv6 to supported.\n"));
580 if (addrlen != sizeof (struct sockaddr_in))
582 in = (const struct sockaddr_in *) addr;
585 /* set internal IP address */
586 if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf)))
591 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
593 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
594 _("Detected internal network address `%s'.\n"),
598 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
600 /* Check if our internal IP is the same as the External detect by STUN*/
601 if(ah->stun_ip && (strcmp(buf, ah->stun_ip) == 0) )
603 ah->internal_ip_is_public = GNUNET_YES;
604 GNUNET_log (GNUNET_ERROR_TYPE_INFO,"A internal IP is the sameas the external");
605 /* No need to continue*/
606 return GNUNET_SYSERR;
609 /* no need to continue iteration if we found the default */
613 return GNUNET_SYSERR;
618 * Determine our local IP addresses; detect internal IP & IPv6-support
620 * @param ah auto setup context
623 test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
625 ah->have_v6 = GNUNET_NO;
626 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
627 GNUNET_OS_network_interfaces_list (&process_if, ah);
629 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6",
630 (GNUNET_YES == ah->have_v6) ? "NO" : "YES");
636 * Test if NAT has been punched
638 * @param ah auto setup context
641 test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
644 struct GNUNET_CLIENT_Connection *client;
645 struct GNUNET_NAT_TestMessage msg;
650 LOG (GNUNET_ERROR_TYPE_INFO,
651 "Asking gnunet-nat-server to connect to `%s'\n",
655 msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
656 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
657 msg.dst_ipv4 = inet_addr(ah->stun_ip);
658 msg.dport = htons(ah->stun_port);
660 msg.is_tcp = htonl ((uint32_t) GNUNET_NO);
662 client = GNUNET_CLIENT_connect ("gnunet-nat-server", ah->cfg);
665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
666 _("Failed to connect to `gnunet-nat-server'\n"));
670 GNUNET_break (GNUNET_OK ==
671 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
677 GNUNET_SCHEDULER_cancel (ltask4);
678 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
679 lsock4, &do_udp_read, ah);
685 LOG (GNUNET_ERROR_TYPE_INFO,
686 "We don't have a STUN IP");
697 * Test if UPnPC works.
699 * @param ah auto setup context
702 test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
707 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
710 // test if upnpc is available
711 have_upnpc = (GNUNET_SYSERR !=
712 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL));
713 //FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP
714 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
716 ? _("upnpc found, enabling its use\n")
717 : _("upnpc not found\n"));
718 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_UPNP",
719 (GNUNET_YES == have_upnpc) ? "YES" : "NO");
726 * Test if ICMP server is working
728 * @param ah auto setup context
731 test_icmp_server (struct GNUNET_NAT_AutoHandle *ah)
744 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
746 GNUNET_CONFIGURATION_get_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
747 &tmp)) && (0 < strlen (tmp))){
749 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we have no public IPv4 address\n"));
755 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")){
757 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we are not behind NAT\n"));
763 GNUNET_OS_check_helper_binary (helper, GNUNET_YES, "-d 127.0.0.1" )){
764 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
765 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("No working gnunet-helper-nat-server found\n"));
768 GNUNET_free_non_null (tmp);
769 GNUNET_free (helper);
771 if (GNUNET_OK == ext_ip && GNUNET_YES == nated && GNUNET_OK == binary)
772 ah->task = GNUNET_SCHEDULER_add_now (&reversal_test, ah);
780 * Test if ICMP client is working
782 * @param ah auto setup context
785 test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
793 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
795 GNUNET_CONFIGURATION_get_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
796 &tmp)) && (0 < strlen (tmp)))
798 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_client not possible, as we have no internal IPv4 address\n"));
804 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")){
805 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we are not behind NAT\n"));
811 GNUNET_OS_check_helper_binary (helper, GNUNET_YES, "-d 127.0.0.1 127.0.0.2 42")){
812 // none of these parameters are actually used in privilege testing mode
813 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("No working gnunet-helper-nat-server found\n"));
816 GNUNET_free_non_null (tmp);
817 GNUNET_free (helper);
825 * Run the next phase of the auto test.
828 next_phase (struct GNUNET_NAT_AutoHandle *ah)
830 struct GNUNET_CONFIGURATION_Handle *diff;
838 case AUTO_EXTERNAL_IP:
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Will run AUTO_EXTERNAL_IP\n");
840 test_external_ip (ah);
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Will run AUTO_STUN\n");
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Will run AUTO_LOCAL_IP\n");
850 case AUTO_NAT_PUNCHED:
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Will run AUTO_NAT_PUNCHED\n");
852 test_nat_punched (ah);
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Will run AUTO_UPNPC\n");
858 case AUTO_ICMP_SERVER:
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Will run AUTO_ICMP_SERVER\n");
860 test_icmp_server (ah);
862 case AUTO_ICMP_CLIENT:
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Will run AUTO_ICMP_CLIENT\n");
864 test_icmp_client (ah);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Done with tests\n");
868 if (!ah->internal_ip_is_public)
870 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES");
872 if (ah->connected_back)
874 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "PUNCHED_NAT", "YES");
878 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "PUNCHED_NAT", "NO");
883 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
885 if (ah->connected_back)
887 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT;
888 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "USE_STUN", "YES");
892 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
893 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "USE_STUN", "NO");
899 GNUNET_CONFIGURATION_set_value_number (ah->cfg, "transport-udp",
907 //The internal IP is the same as public, but we didn't got a incoming connection
908 if (ah->connected_back)
910 ah->type = GNUNET_NAT_TYPE_NO_NAT;
911 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "NO");
915 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES");
916 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
919 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
924 GNUNET_CONFIGURATION_set_value_number (ah->cfg, "transport-udp",
932 diff = GNUNET_CONFIGURATION_get_diff (ah->initial_cfg,
936 ah->fin_cb (ah->fin_cb_cls,
940 GNUNET_CONFIGURATION_destroy (diff);
941 GNUNET_NAT_autoconfig_cancel (ah);
952 * Start auto-configuration routine. The resolver service should
953 * be available when this function is called.
955 * @param cfg initial configuration
956 * @param cb function to call with autoconfiguration result
957 * @param cb_cls closure for @a cb
958 * @return handle to cancel operation
960 struct GNUNET_NAT_AutoHandle *
961 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
962 GNUNET_NAT_AutoResultCallback cb,
965 struct GNUNET_NAT_AutoHandle *ah;
967 ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
969 ah->fin_cb_cls = cb_cls;
970 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
971 ah->cfg = GNUNET_CONFIGURATION_dup (cfg);
972 ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg);
974 /* never use loopback addresses if user wanted autoconfiguration */
975 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat",
985 * Abort autoconfiguration.
987 * @param ah handle for operation to abort
990 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
994 GNUNET_NAT_test_stop (ah->tst);
999 GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh);
1002 if (NULL != ah->task)
1004 GNUNET_SCHEDULER_cancel (ah->task);
1007 GNUNET_CONFIGURATION_destroy (ah->cfg);
1008 GNUNET_CONFIGURATION_destroy (ah->initial_cfg);
1013 /* end of nat_auto.c */