2 This file is part of GNUnet.
3 Copyright (C) 2015, 2016 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 src/nat/gnunet-nat.c
23 * @brief Command-line tool to interact with the NAT service
24 * @author Christian Grothoff
25 * @author Bruno Cabral
28 #include "gnunet_util_lib.h"
29 #include "gnunet_nat_service.h"
32 * Value to return from #main().
34 static int global_ret;
37 * Handle to ongoing autoconfiguration.
39 static struct GNUNET_NAT_AutoHandle *ah;
44 static unsigned int adv_port;
47 * Flag set to 1 if we use IPPROTO_UDP.
52 * Flag set to 1 if we are to listen for connection reversal requests.
54 static int listen_reversal;
57 * Flag set to 1 if we use IPPROTO_TCP.
62 * If we do auto-configuration, should we write the result
68 * Configuration filename.
70 static const char *cfg_file;
73 * Original configuration.
75 static const struct GNUNET_CONFIGURATION_Handle *cfg;
83 * Address we are bound to (in test), or should bind to
84 * (if #do_stun is set).
86 static char *bind_addr;
89 * External IP address and port to use for the test.
90 * If not set, use #bind_addr.
92 static char *extern_addr;
95 * Local address to use for connection reversal request.
97 static char *local_addr;
100 * Remote address to use for connection reversal request.
102 static char *remote_addr;
105 * Should we actually bind to #bind_addr and receive and process STUN requests?
107 static unsigned int do_stun;
110 * Should we run autoconfiguration?
112 static unsigned int do_auto;
115 * Handle to a NAT test operation.
117 static struct GNUNET_NAT_Test *nt;
120 * Handle to NAT operation.
122 static struct GNUNET_NAT_Handle *nh;
125 * Listen socket for STUN processing.
127 static struct GNUNET_NETWORK_Handle *ls;
130 * Task for reading STUN packets.
132 static struct GNUNET_SCHEDULER_Task *rtask;
136 * Test if all activities have finished, and if so,
150 GNUNET_SCHEDULER_shutdown ();
155 * Function to iterate over sugested changes options
158 * @param section name of the section
159 * @param option name of the option
160 * @param value value of the option
163 auto_conf_iter (void *cls,
168 struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
174 GNUNET_CONFIGURATION_set_value_string (new_cfg,
182 * Function called with the result from the autoconfiguration.
185 * @param diff minimal suggested changes to the original configuration
186 * to make it work (as best as we can)
187 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
188 * @param type what the situation of the NAT
191 auto_config_cb (void *cls,
192 const struct GNUNET_CONFIGURATION_Handle *diff,
193 enum GNUNET_NAT_StatusCode result,
194 enum GNUNET_NAT_Type type)
196 const char *nat_type;
197 char unknown_type[64];
198 struct GNUNET_CONFIGURATION_Handle *new_cfg;
203 case GNUNET_NAT_TYPE_NO_NAT:
206 case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
207 nat_type = "NAT but we can traverse";
209 case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
210 nat_type = "NAT but STUN is able to identify the correct information";
212 case GNUNET_NAT_TYPE_UPNP_NAT:
213 nat_type = "NAT but UPNP opened the ports";
216 SPRINTF (unknown_type,
217 "NAT unknown, type %u",
219 nat_type = unknown_type;
223 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
224 "NAT status: %s/%s\n",
225 GNUNET_NAT_status2string (result),
228 /* Shortcut: if there are no changes suggested, bail out early. */
230 GNUNET_CONFIGURATION_is_dirty (diff))
236 /* Apply diff to original configuration and show changes
238 new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
242 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
243 _("Suggested configuration changes:\n"));
244 GNUNET_CONFIGURATION_iterate_section_values (diff,
250 /* If desired, write configuration to file; we write only the
251 changes to the defaults to keep things compact. */
255 struct GNUNET_CONFIGURATION_Handle *def_cfg;
257 GNUNET_CONFIGURATION_set_value_string (new_cfg,
261 def_cfg = GNUNET_CONFIGURATION_create ();
262 GNUNET_break (GNUNET_OK ==
263 GNUNET_CONFIGURATION_load (def_cfg,
266 GNUNET_CONFIGURATION_write_diffs (def_cfg,
270 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
271 _("Failed to write configuration to `%s'\n"),
277 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
278 _("Wrote updated configuration to `%s'\n"),
281 GNUNET_CONFIGURATION_destroy (def_cfg);
285 GNUNET_CONFIGURATION_destroy (new_cfg);
291 * Function called to report success or failure for
292 * NAT configuration test.
295 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
298 test_report_cb (void *cls,
299 enum GNUNET_NAT_StatusCode result)
302 PRINTF ("NAT test result: %s\n",
303 GNUNET_NAT_status2string (result));
309 * Signature of the callback passed to #GNUNET_NAT_register() for
310 * a function to call whenever our set of 'valid' addresses changes.
312 * @param cls closure, NULL
313 * @param add_remove #GNUNET_YES to add a new public IP address,
314 * #GNUNET_NO to remove a previous (now invalid) one
315 * @param ac address class the address belongs to
316 * @param addr either the previous or the new public IP address
317 * @param addrlen actual length of the @a addr
320 address_cb (void *cls,
322 enum GNUNET_NAT_AddressClass ac,
323 const struct sockaddr *addr,
326 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
328 add_remove ? "+" : "-",
336 * Signature of the callback passed to #GNUNET_NAT_register().
337 * for a function to call whenever someone asks us to do connection
340 * @param cls closure, NULL
341 * @param local_addr address where we received the request
342 * @param local_addrlen actual length of the @a local_addr
343 * @param remote_addr public IP address of the other peer
344 * @param remote_addrlen actual length of the @a remote_addr
347 reversal_cb (void *cls,
348 const struct sockaddr *local_addr,
349 socklen_t local_addrlen,
350 const struct sockaddr *remote_addr,
351 socklen_t remote_addrlen)
353 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
354 "Connection reversal requested by %s\n",
355 GNUNET_a2s (remote_addr,
361 * Task run on shutdown.
366 do_shutdown (void *cls)
370 GNUNET_NAT_autoconfig_cancel (ah);
375 GNUNET_NAT_test_stop (nt);
380 GNUNET_NAT_unregister (nh);
385 GNUNET_NETWORK_socket_close (ls);
390 GNUNET_SCHEDULER_cancel (rtask);
397 * Task to receive incoming packets for STUN processing.
400 stun_read_task (void *cls)
404 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
408 size = GNUNET_NETWORK_socket_recvfrom_amount (ls);
412 GNUNET_SCHEDULER_shutdown ();
418 struct sockaddr_storage sa;
419 socklen_t salen = sizeof (sa);
422 ret = GNUNET_NETWORK_socket_recvfrom (ls,
425 (struct sockaddr *) &sa,
430 GNUNET_SCHEDULER_shutdown ();
434 (void) GNUNET_NAT_stun_handle_packet (nh,
435 (const struct sockaddr *) &sa,
444 * Main function that will be run.
447 * @param args remaining command-line arguments
448 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
449 * @param c configuration
455 const struct GNUNET_CONFIGURATION_Handle *c)
458 struct sockaddr_in bind_sa;
459 struct sockaddr_in extern_sa;
460 struct sockaddr *local_sa;
461 struct sockaddr *remote_sa;
468 if (use_tcp && use_udp)
470 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
471 "Cannot use TCP and UDP\n");
481 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
486 ah = GNUNET_NAT_autoconfig_start (c,
494 return; /* all good, we just run auto config */
495 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
496 "Must specify either TCP or UDP\n");
500 if (NULL != bind_addr)
503 GNUNET_STRINGS_to_address_ipv4 (bind_addr,
507 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
508 "Invalid socket address `%s'\n",
514 if (NULL != extern_addr)
517 GNUNET_STRINGS_to_address_ipv4 (extern_addr,
518 strlen (extern_addr),
521 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
522 "Invalid socket address `%s'\n",
528 if (NULL != local_addr)
530 local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr,
535 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
536 "Invalid socket address `%s'\n",
542 if (NULL != remote_addr)
544 remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
549 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
550 "Invalid socket address `%s'\n",
557 if (NULL != bind_addr)
559 if (NULL == extern_addr)
561 nt = GNUNET_NAT_test_start (c,
564 ntohs (bind_sa.sin_port),
566 ntohs (extern_sa.sin_port),
571 if (NULL != local_addr)
573 nh = GNUNET_NAT_register (c,
577 (const struct sockaddr **) &local_sa,
580 (listen_reversal) ? &reversal_cb : NULL,
584 if (NULL != remote_addr)
589 (sizeof (struct sockaddr_in) != local_len) )
591 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
592 "Require IPv4 local address to initiate connection reversal\n");
594 GNUNET_SCHEDULER_shutdown ();
597 if (sizeof (struct sockaddr_in) != remote_len)
599 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
600 "Require IPv4 reversal target address\n");
602 GNUNET_SCHEDULER_shutdown ();
605 ret = GNUNET_NAT_request_reversal (nh,
606 (const struct sockaddr_in *) &local_sa,
607 (const struct sockaddr_in *) &remote_sa);
611 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
612 "Connection reversal internal error\n");
615 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
616 "Connection reversal unavailable\n");
619 /* operation in progress */
626 if (NULL == local_addr)
628 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
629 "Require local address to support STUN requests\n");
631 GNUNET_SCHEDULER_shutdown ();
634 if (IPPROTO_UDP != proto)
636 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
637 "STUN only supported over UDP\n");
639 GNUNET_SCHEDULER_shutdown ();
642 ls = GNUNET_NETWORK_socket_create (af,
646 GNUNET_NETWORK_socket_bind (ls,
650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
651 "Failed to bind to %s: %s\n",
652 GNUNET_a2s (local_sa,
656 GNUNET_SCHEDULER_shutdown ();
659 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
670 * Main function of gnunet-nat
672 * @param argc number of command-line arguments
673 * @param argv command line
674 * @return 0 on success, -1 on error
680 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
682 gettext_noop ("run autoconfiguration"),
683 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto },
684 {'b', "bind", "ADDRESS",
685 gettext_noop ("which IP and port are we bound to"),
686 GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr },
687 {'e', "external", "ADDRESS",
688 gettext_noop ("which external IP and port should be used to test"),
689 GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
690 {'i', "in", "ADDRESS",
691 gettext_noop ("which IP and port are we locally using to bind/listen to"),
692 GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
693 {'r', "remote", "ADDRESS",
694 gettext_noop ("which remote IP and port should be asked for connection reversal"),
695 GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
697 gettext_noop ("port to use to advertise"),
698 GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
700 gettext_noop ("enable STUN processing"),
701 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
703 gettext_noop ("use TCP"),
704 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp },
706 gettext_noop ("use UDP"),
707 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
709 gettext_noop ("write configuration file (for autoconfiguration)"),
710 GNUNET_NO, &GNUNET_GETOPT_set_one, &write_cfg },
712 gettext_noop ("watch for connection reversal requests"),
713 GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
714 GNUNET_GETOPT_OPTION_END
718 GNUNET_STRINGS_get_utf8_args (argc, argv,
722 GNUNET_PROGRAM_run (argc, argv,
723 "gnunet-nat [options]",
724 _("GNUnet NAT traversal autoconfigure daemon"),
731 GNUNET_free ((void*) argv);
736 /* end of gnunet-nat.c */