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_nat_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
35 * How long do we wait for the NAT test to report success?
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
40 * Phases of the auto configuration.
45 * Initial start value.
50 * Test if we are online.
55 * Test our external IP.
60 * Test our internal IP.
65 * Test if NAT was punched.
70 * Test if UPnP is working.
75 * Test if ICMP server works.
80 * Test if ICMP client works.
85 * Last phase, we're done.
93 * Handle to auto-configuration in progress.
95 struct GNUNET_NAT_AutoHandle
99 * Handle to the active NAT test.
101 struct GNUNET_NAT_Test *tst;
104 * Function to call when done.
106 GNUNET_NAT_AutoResultCallback fin_cb;
109 * Closure for 'fin_cb'.
114 * Handle for active 'GNUNET_NAT_mini_get_external_ipv4'-operation.
116 struct GNUNET_NAT_ExternalHandle *eh;
119 * Current configuration (with updates from previous phases)
121 struct GNUNET_CONFIGURATION_Handle *cfg;
124 * Original configuration (used to calculate differences)
126 struct GNUNET_CONFIGURATION_Handle *initial_cfg;
129 * Task identifier for the timeout.
131 GNUNET_SCHEDULER_TaskIdentifier tsk;
134 * Where are we in the test?
136 enum AutoPhase phase;
142 * Run the next phase of the auto test.
144 * @param ac auto test handle
147 next_phase (struct GNUNET_NAT_AutoHandle *ac);
154 gtk_toggle_button_set_active (button, on ? TRUE : FALSE);
160 * Function called if NAT failed to confirm success.
161 * Clean up and update GUI (with failure).
163 * @param cls closure with setup context
164 * @param tc scheduler callback
167 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 struct GNUNET_NAT_AutoHandle *ac = cls;
171 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
172 _("NAT traversal with ICMP Server timed out.\n"));
173 GNUNET_assert (NULL != ac->tst);
174 ac->tsk = GNUNET_SCHEDULER_NO_TASK;
175 GNUNET_NAT_test_stop (ac->tst);
177 update_icmp_server_enable_button (GNUNET_NO);
179 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "ENABLE_ICMP_SERVER", "NO");
185 * Main function for the connection reversal test.
187 * @param cls the 'int*' for the result
188 * @param tc scheduler context
191 reversal_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 struct GNUNET_NAT_AutoHandle *ac = cls;
195 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
196 _("Testing connection reversal with ICMP server.\n"));
197 GNUNET_assert (NULL != cfg);
198 GNUNET_RESOLVER_connect (cfg);
199 ac->tst = GNUNET_NAT_test_start (cfg, GNUNET_YES, 0, 0, &result_callback, ac);
205 ac->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, ac);
210 * Test if we are online at all.
212 * @param ac auto setup context
215 test_online (struct GNUNET_NAT_AutoHandle *ac)
217 // FIXME: not implemented
223 * Set our external IPv4 address.
225 * @param cls closure with our setup context
226 * @param addr the address, NULL on errors
229 set_external_ipv4 (void *cls, const struct in_addr *addr)
231 struct GNUNET_NAT_AutoHandle *ac = cls;
232 char buf[INET_ADDRSTRLEN];
241 /* enable 'behind nat' */
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243 _("Detected external IP `%s'\n"),
249 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "BEHIND_NAT", "YES");
250 o = GNUNET_SETUP_get_object ("GNUNET_setup_transport_nat_checkbutton");
251 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (o), TRUE);
253 /* set external IP address */
254 if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf)))
261 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "EXTERNAL_ADDRESS",
263 o = GNUNET_SETUP_get_object ("GNUNET_setup_transport_external_ip_address_entry");
264 gtk_entry_set_text (GTK_ENTRY (o), buf);
270 * Determine our external IPv4 address.
272 * @param ac auto setup context
275 test_external_ip (struct GNUNET_NAT_AutoHandle *ac)
278 /* try to detect external IP */
279 ac->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT,
280 &set_external_ipv4, ac);
285 * Process list of local IP addresses. Find and set the
286 * one of the default interface.
288 * @param cls pointer to int to store if we have a non-local IPv6 address
289 * @param name name of the interface (can be NULL for unknown)
290 * @param isDefault is this presumably the default interface
291 * @param addr address of this interface (can be NULL for unknown or unassigned)
292 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
293 * @param netmask the network mask (can be NULL for unknown or unassigned))
294 * @param addrlen length of the address
295 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
298 nipo (void *cls, const char *name, int isDefault, const struct sockaddr *addr,
299 const struct sockaddr *broadcast_addr, const struct sockaddr *netmask,
303 const struct sockaddr_in *in;
304 char buf[INET_ADDRSTRLEN];
309 if ( (sizeof (struct sockaddr_in6) == addrlen) &&
310 (0 != memcmp (&in6addr_loopback, addr,
312 (! IN6_IS_ADDR_LINKLOCAL(addr)) )
314 *have_v6 = GNUNET_YES;
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 _("This system has a global IPv6 address, setting IPv6 to supported.\n"));
319 if (addrlen != sizeof (struct sockaddr_in))
321 in = (const struct sockaddr_in *) addr;
323 /* set internal IP address */
324 if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf)))
329 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "INTERNAL_ADDRESS", buf);
330 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
331 _("Detected internal network address `%s'.\n"),
334 GTK_ENTRY (GNUNET_SETUP_get_object
335 ("GNUNET_setup_transport_internal_ip_entry"));
336 gtk_entry_set_text (entry, buf);
337 /* no need to continue iteration */
338 return GNUNET_SYSERR;
343 * Determine our local IP addresses; detect internal IP & IPv6-support
345 * @param ac auto setup context
348 test_local_ip (struct GNUNET_NAT_AutoHandle *ac)
350 GtkToggleButton *button;
354 GNUNET_OS_network_interfaces_list (&nipo, &have_v6);
355 button = GTK_TOGGLE_BUTTON (GNUNET_SETUP_get_object ("GNUNET_setup_transport_disable_ipv6_checkbutton"));
356 gtk_toggle_button_set_active (button,
357 (GNUNET_YES == have_v6) ? FALSE : TRUE);
359 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "DISABLEV6",
360 (GNUNET_YES == have_v6) ? "NO" : "YES");
366 * Test if NAT has been punched
368 * @param ac auto setup context
371 test_nat_punched (struct GNUNET_NAT_AutoHandle *ac)
373 // FIXME: not implemented
379 * Test if UPnPC works.
381 * @param ac auto setup context
384 test_upnpc (struct GNUNET_NAT_AutoHandle *ac)
387 GtkToggleButton *button;
389 /* test if upnpc is available */
390 button = GTK_TOGGLE_BUTTON (GNUNET_SETUP_get_object ("GNUNET_setup_transport_upnp_enable_checkbutton"));
391 have_upnpc = (GNUNET_SYSERR !=
392 GNUNET_OS_check_helper_binary ("upnpc"));
393 /* FIXME: test if upnpc is actually working, that is, if transports
394 start to work once we use UPnP */
395 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
397 ? _("upnpc found, enabling its use\n")
398 : _("upnpc not found\n"));
399 gtk_toggle_button_set_active (button,
404 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "ENABLE_UPNP",
405 (GNUNET_YES == have_upnpc) ? "YES" : "NO");
411 * Test if ICMP server is working
413 * @param ac auto setup context
416 test_icmp_server (struct GNUNET_NAT_AutoHandle *ac)
423 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
426 GNUNET_CONFIGURATION_get_value_string (cfg, "nat", "EXTERNAL_ADDRESS",
427 &tmp)) && (0 < strlen (tmp)) &&
429 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT")) &&
431 GNUNET_OS_check_helper_binary (binary)));
432 GNUNET_free_non_null (tmp);
433 GNUNET_free (binary);
434 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
436 ? _("gnunet-helper-nat-server found, testing it\n")
437 : _("No working gnunet-helper-nat-server found\n"));
439 GNUNET_SCHEDULER_add_now (&reversal_test, ac);
446 * Test if ICMP client is working
448 * @param ac auto setup context
451 test_icmp_client (struct GNUNET_NAT_AutoHandle *ac)
453 GtkToggleButton *button;
459 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
462 GNUNET_CONFIGURATION_get_value_string (cfg, "nat", "INTERNAL_ADDRESS",
463 &tmp)) && (0 < strlen (tmp)) &&
465 GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT")) &&
467 GNUNET_OS_check_helper_binary (binary)));
468 GNUNET_free_non_null (tmp);
469 GNUNET_free (binary);
470 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
472 ? _("gnunet-helper-nat-client found, enabling it\n")
473 : _("gnunet-helper-nat-client not found or behind NAT, disabling it\n"));
475 GTK_TOGGLE_BUTTON (GNUNET_SETUP_get_object
476 ("GNUNET_setup_transport_icmp_client_enable_checkbutton"));
477 gtk_toggle_button_set_active (button, hnc ? TRUE : FALSE);
483 * Run the next phase of the auto test.
486 next_phase (struct GNUNET_NAT_AutoHandle *ac)
497 case AUTO_EXTERNAL_IP:
498 test_external_ip (ac);
503 case AUTO_NAT_PUNCHED:
504 test_nat_punched (ac);
509 case AUTO_ICMP_SERVER:
510 test_icmp_server (ac);
512 case AUTO_ICMP_CLIENT:
513 test_icmp_client (ac);
516 ac->fin_cb (ac->fin_cb_cls);
525 * Start auto-configuration routine. The resolver service should
526 * be available when this function is called.
528 * @param cfg initial configuration
529 * @param cb function to call with autoconfiguration result
530 * @param cb_cls closure for cb
531 * @return handle to cancel operation
533 struct GNUNET_NAT_AutoHandle *
534 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
535 GNUNET_NAT_AutoResultCallback cb,
538 struct GNUNET_NAT_AutoHandle *ac;
540 ac = GNUNET_malloc (sizeof (struct GNUNET_NAT_AutoHandle));
542 ac->fin_cb_cls = cb_cls;
543 ac->cfg = GNUNET_CONFIGURATION_dup (cfg);
544 ac->init_cfg = GNUNET_CONFIGURATION_dup (cfg);
546 /* never use loopback addresses if user wanted autoconfiguration */
547 GNUNET_CONFIGURATION_set_value_string (ac->cfg, "nat",
557 * Abort autoconfiguration.
559 * @param ah handle for operation to abort
562 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
568 /* end of nat_auto.c */