2 This file is part of GNUnet.
3 (C) 2012 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.
22 * @file nat/nat_auto.c
23 * @brief functions for auto-configuration of the network
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_resolver_service.h"
29 #include "gnunet_nat_lib.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
36 * How long do we wait for the NAT test to report success?
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
41 * Phases of the auto configuration.
46 * Initial start value.
51 * Test if we are online.
56 * Test our external IP.
61 * Test our internal IP.
66 * Test if NAT was punched.
71 * Test if UPnP is working.
76 * Test if ICMP server works.
81 * Test if ICMP client works.
86 * Last phase, we're done.
94 * Handle to auto-configuration in progress.
96 struct GNUNET_NAT_AutoHandle
100 * Handle to the active NAT test.
102 struct GNUNET_NAT_Test *tst;
105 * Function to call when done.
107 GNUNET_NAT_AutoResultCallback fin_cb;
110 * Closure for @e fin_cb.
115 * Handle for active 'GNUNET_NAT_mini_get_external_ipv4'-operation.
117 struct GNUNET_NAT_ExternalHandle *eh;
120 * Current configuration (with updates from previous phases)
122 struct GNUNET_CONFIGURATION_Handle *cfg;
125 * Original configuration (used to calculate differences)
127 struct GNUNET_CONFIGURATION_Handle *initial_cfg;
130 * Task identifier for the timeout.
132 GNUNET_SCHEDULER_TaskIdentifier task;
135 * Where are we in the test?
137 enum AutoPhase phase;
145 * Error code for better debugging and user feedback
147 enum GNUNET_NAT_FailureCode ret;
152 * Run the next phase of the auto test.
154 * @param ah auto test handle
157 next_phase (struct GNUNET_NAT_AutoHandle *ah);
161 * Function called by NAT to report the outcome of the nat-test.
162 * Clean up and update GUI.
164 * @param cls the auto handle
165 * @param success currently always #GNUNET_OK
166 * @param emsg NULL on success, otherwise an error message
169 result_callback (void *cls,
170 enum GNUNET_NAT_FailureCode ret)
172 struct GNUNET_NAT_AutoHandle *ah = cls;
173 GNUNET_NAT_test_stop (ah->tst);
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
177 GNUNET_NAT_ERROR_SUCCESS == ret
178 ? _("NAT traversal with ICMP Server succeeded.\n")
179 : _("NAT traversal with ICMP Server failed.\n"));
180 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER",
181 GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES");
187 * Main function for the connection reversal test.
189 * @param cls the `struct GNUNET_NAT_AutoHandle`
190 * @param tc scheduler context
193 reversal_test (void *cls,
194 const struct GNUNET_SCHEDULER_TaskContext *tc)
196 struct GNUNET_NAT_AutoHandle *ah = cls;
198 ah->task = GNUNET_SCHEDULER_NO_TASK;
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200 _("Testing connection reversal with ICMP server.\n"));
201 GNUNET_RESOLVER_connect (ah->cfg);
202 ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
203 &result_callback, ah);
208 * Test if we are online at all.
210 * @param ah auto setup context
213 test_online (struct GNUNET_NAT_AutoHandle *ah)
215 // FIXME: not implemented
218 * ah->ret = GNUNET_NAT_ERROR_NOT_ONLINE;
225 * Set our external IPv4 address.
227 * @param cls closure with our setup context
228 * @param addr the address, NULL on errors
229 * @param emsg NULL on success, otherwise an error message
232 set_external_ipv4 (void *cls,
233 const struct in_addr *addr,
234 enum GNUNET_NAT_FailureCode ret)
236 struct GNUNET_NAT_AutoHandle *ah = cls;
237 char buf[INET_ADDRSTRLEN];
241 if (GNUNET_NAT_ERROR_SUCCESS != ret)
246 /* enable 'behind nat' */
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
248 _("Detected external IP `%s'\n"),
253 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES");
255 /* set external IP address */
256 if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf)))
259 /* actually, this should never happen, as the caller already executed just
260 * this check, but for consistency (eg: future changes in the caller)
261 * we still need to report this error...
263 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
267 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
274 * Determine our external IPv4 address.
276 * @param ah auto setup context
279 test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
281 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
285 /* try to detect external IP */
286 ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT,
287 &set_external_ipv4, ah);
292 * Process list of local IP addresses. Find and set the
293 * one of the default interface.
295 * @param cls our `struct GNUNET_NAT_AutoHandle`
296 * @param name name of the interface (can be NULL for unknown)
297 * @param isDefault is this presumably the default interface
298 * @param addr address of this interface (can be NULL for unknown or unassigned)
299 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
300 * @param netmask the network mask (can be NULL for unknown or unassigned))
301 * @param addrlen length of the @a addr and @a broadcast_addr
302 * @return GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
305 process_if (void *cls,
308 const struct sockaddr *addr,
309 const struct sockaddr *broadcast_addr,
310 const struct sockaddr *netmask,
313 struct GNUNET_NAT_AutoHandle *ah = cls;
314 const struct sockaddr_in *in;
315 char buf[INET_ADDRSTRLEN];
319 if ( (sizeof (struct sockaddr_in6) == addrlen) &&
320 (0 != memcmp (&in6addr_loopback, &((const struct sockaddr_in6 *) addr)->sin6_addr,
321 sizeof (struct in6_addr))) &&
322 (! IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *) addr)->sin6_addr)) )
324 ah->have_v6 = GNUNET_YES;
325 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
326 _("This system has a global IPv6 address, setting IPv6 to supported.\n"));
329 if (addrlen != sizeof (struct sockaddr_in))
331 in = (const struct sockaddr_in *) addr;
333 /* set internal IP address */
334 if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf)))
339 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
341 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
342 _("Detected internal network address `%s'.\n"),
344 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
345 /* no need to continue iteration */
346 return GNUNET_SYSERR;
351 * Determine our local IP addresses; detect internal IP & IPv6-support
353 * @param ah auto setup context
356 test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
358 ah->have_v6 = GNUNET_NO;
359 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
360 GNUNET_OS_network_interfaces_list (&process_if, ah);
362 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6",
363 (GNUNET_YES == ah->have_v6) ? "NO" : "YES");
369 * Test if NAT has been punched
371 * @param ah auto setup context
374 test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
376 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
379 // FIXME: not implemented
386 * Test if UPnPC works.
388 * @param ah auto setup context
391 test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
395 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
398 /* test if upnpc is available */
399 have_upnpc = (GNUNET_SYSERR !=
400 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL));
401 /* FIXME: test if upnpc is actually working, that is, if transports
402 start to work once we use UPnP */
403 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
405 ? _("upnpc found, enabling its use\n")
406 : _("upnpc not found\n"));
407 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_UPNP",
408 (GNUNET_YES == have_upnpc) ? "YES" : "NO");
414 * Test if ICMP server is working
416 * @param ah auto setup context
419 test_icmp_server (struct GNUNET_NAT_AutoHandle *ah)
431 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
433 GNUNET_CONFIGURATION_get_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
434 &tmp)) && (0 < strlen (tmp))){
436 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we have no public IPv4 address\n"));
442 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")){
444 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we are not behind NAT\n"));
450 GNUNET_OS_check_helper_binary (helper, GNUNET_YES, "-d 127.0.0.1" )){
451 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
452 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("No working gnunet-helper-nat-server found\n"));
455 GNUNET_free_non_null (tmp);
456 GNUNET_free (helper);
458 if (GNUNET_OK == ext_ip && GNUNET_YES == nated && GNUNET_OK == binary)
459 ah->task = GNUNET_SCHEDULER_add_now (&reversal_test, ah);
466 * Test if ICMP client is working
468 * @param ah auto setup context
471 test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
477 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
479 GNUNET_CONFIGURATION_get_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
480 &tmp)) && (0 < strlen (tmp)))
482 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_client not possible, as we have no internal IPv4 address\n"));
488 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")){
489 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we are not behind NAT\n"));
495 GNUNET_OS_check_helper_binary (helper, GNUNET_YES, "-d 127.0.0.1 127.0.0.2 42")){
496 // none of these parameters are actually used in privilege testing mode
497 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("No working gnunet-helper-nat-server found\n"));
500 GNUNET_free_non_null (tmp);
501 GNUNET_free (helper);
508 * Run the next phase of the auto test.
511 next_phase (struct GNUNET_NAT_AutoHandle *ah)
513 struct GNUNET_CONFIGURATION_Handle *diff;
524 case AUTO_EXTERNAL_IP:
525 test_external_ip (ah);
530 case AUTO_NAT_PUNCHED:
531 test_nat_punched (ah);
536 case AUTO_ICMP_SERVER:
537 test_icmp_server (ah);
539 case AUTO_ICMP_CLIENT:
540 test_icmp_client (ah);
543 diff = GNUNET_CONFIGURATION_get_diff (ah->initial_cfg,
545 ah->fin_cb (ah->fin_cb_cls,
548 GNUNET_CONFIGURATION_destroy (diff);
549 GNUNET_NAT_autoconfig_cancel (ah);
556 * Start auto-configuration routine. The resolver service should
557 * be available when this function is called.
559 * @param cfg initial configuration
560 * @param cb function to call with autoconfiguration result
561 * @param cb_cls closure for @a cb
562 * @return handle to cancel operation
564 struct GNUNET_NAT_AutoHandle *
565 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
566 GNUNET_NAT_AutoResultCallback cb,
569 struct GNUNET_NAT_AutoHandle *ah;
571 ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
573 ah->fin_cb_cls = cb_cls;
574 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
575 ah->cfg = GNUNET_CONFIGURATION_dup (cfg);
576 ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg);
578 /* never use loopback addresses if user wanted autoconfiguration */
579 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat",
588 * Abort autoconfiguration.
590 * @param ah handle for operation to abort
593 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
597 GNUNET_NAT_test_stop (ah->tst);
602 GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh);
605 if (GNUNET_SCHEDULER_NO_TASK != ah->task)
607 GNUNET_SCHEDULER_cancel (ah->task);
608 ah->task = GNUNET_SCHEDULER_NO_TASK;
610 GNUNET_CONFIGURATION_destroy (ah->cfg);
611 GNUNET_CONFIGURATION_destroy (ah->initial_cfg);
616 /* end of nat_auto.c */