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.
67 * Address we are bound to (in test), or should bind to
68 * (if #do_stun is set).
70 static char *bind_addr;
73 * External IP address and port to use for the test.
74 * If not set, use #bind_addr.
76 static char *extern_addr;
79 * Local address to use for connection reversal request.
81 static char *local_addr;
84 * Remote address to use for connection reversal request.
86 static char *remote_addr;
89 * Should we actually bind to #bind_addr and receive and process STUN requests?
91 static unsigned int do_stun;
94 * Should we run autoconfiguration?
96 static unsigned int do_auto;
99 * Handle to a NAT test operation.
101 static struct GNUNET_NAT_Test *nt;
104 * Handle to NAT operation.
106 static struct GNUNET_NAT_Handle *nh;
110 * Test if all activities have finished, and if so,
122 GNUNET_SCHEDULER_shutdown ();
127 * Function to iterate over sugested changes options
130 * @param section name of the section
131 * @param option name of the option
132 * @param value value of the option
135 auto_conf_iter (void *cls,
147 * Function called with the result from the autoconfiguration.
150 * @param diff minimal suggested changes to the original configuration
151 * to make it work (as best as we can)
152 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
153 * @param type what the situation of the NAT
156 auto_config_cb (void *cls,
157 const struct GNUNET_CONFIGURATION_Handle *diff,
158 enum GNUNET_NAT_StatusCode result,
159 enum GNUNET_NAT_Type type)
161 const char *nat_type;
162 char unknown_type[64];
167 case GNUNET_NAT_TYPE_NO_NAT:
170 case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
171 nat_type = "NAT but we can traverse";
173 case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
174 nat_type = "NAT but STUN is able to identify the correct information";
176 case GNUNET_NAT_TYPE_UPNP_NAT:
177 nat_type = "NAT but UPNP opened the ports";
180 SPRINTF (unknown_type,
181 "NAT unknown, type %u",
183 nat_type = unknown_type;
186 PRINTF ("NAT status: %s/%s\n",
187 GNUNET_NAT_status2string (result),
190 PRINTF ("SUGGESTED CHANGES:\n");
191 GNUNET_CONFIGURATION_iterate_section_values (diff,
195 // Have option to save config
201 * Function called to report success or failure for
202 * NAT configuration test.
205 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
208 test_report_cb (void *cls,
209 enum GNUNET_NAT_StatusCode result)
212 PRINTF ("NAT test result: %s\n",
213 GNUNET_NAT_status2string (result));
219 * Signature of the callback passed to #GNUNET_NAT_register() for
220 * a function to call whenever our set of 'valid' addresses changes.
223 * @param add_remove #GNUNET_YES to add a new public IP address,
224 * #GNUNET_NO to remove a previous (now invalid) one
225 * @param ac address class the address belongs to
226 * @param addr either the previous or the new public IP address
227 * @param addrlen actual length of the @a addr
230 address_cb (void *cls,
232 enum GNUNET_NAT_AddressClass ac,
233 const struct sockaddr *addr,
241 * Signature of the callback passed to #GNUNET_NAT_register().
242 * for a function to call whenever someone asks us to do connection
246 * @param local_addr address where we received the request
247 * @param local_addrlen actual length of the @a local_addr
248 * @param remote_addr public IP address of the other peer
249 * @param remote_addrlen actual length of the @a remote_addr
252 reversal_cb (void *cls,
253 const struct sockaddr *local_addr,
254 socklen_t local_addrlen,
255 const struct sockaddr *remote_addr,
256 socklen_t remote_addrlen)
263 * Task run on shutdown.
268 do_shutdown (void *cls)
272 GNUNET_NAT_autoconfig_cancel (ah);
277 GNUNET_NAT_test_stop (nt);
282 GNUNET_NAT_unregister (nh);
289 * Main function that will be run.
292 * @param args remaining command-line arguments
293 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
294 * @param c configuration
300 const struct GNUNET_CONFIGURATION_Handle *c)
303 struct sockaddr_in bind_sa;
304 struct sockaddr_in extern_sa;
305 struct sockaddr *local_sa;
306 struct sockaddr *remote_sa;
310 if (use_tcp && use_udp)
312 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
313 "Cannot use TCP and UDP\n");
324 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
325 "Must specify either TCP or UDP\n");
329 if (NULL != bind_addr)
332 GNUNET_STRINGS_to_address_ipv4 (bind_addr,
336 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
337 "Invalid socket address `%s'\n",
343 if (NULL != extern_addr)
346 GNUNET_STRINGS_to_address_ipv4 (extern_addr,
347 strlen (extern_addr),
350 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
351 "Invalid socket address `%s'\n",
357 if (NULL != local_addr)
359 local_len = GNUNET_STRINGS_parse_socket_addr (local_addr,
364 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
365 "Invalid socket address `%s'\n",
371 if (NULL != remote_addr)
373 remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
378 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
379 "Invalid socket address `%s'\n",
386 if (NULL != bind_addr)
388 if (NULL == extern_addr)
390 nt = GNUNET_NAT_test_start (c,
393 ntohs (bind_sa.sin_port),
395 ntohs (extern_sa.sin_port),
400 if (NULL != local_addr)
402 nh = GNUNET_NAT_register (c,
406 (const struct sockaddr **) &local_sa,
409 (listen_reversal) ? &reversal_cb : NULL,
413 if (NULL != remote_addr)
418 (sizeof (struct sockaddr_in) != local_len) )
420 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
421 "Require IPv4 local address to initiate connection reversal\n");
423 GNUNET_SCHEDULER_shutdown ();
426 if (sizeof (struct sockaddr_in) != remote_len)
428 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
429 "Require IPv4 reversal target address\n");
431 GNUNET_SCHEDULER_shutdown ();
434 ret = GNUNET_NAT_request_reversal (nh,
435 (const struct sockaddr_in *) &local_sa,
436 (const struct sockaddr_in *) &remote_sa);
440 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
441 "Connection reversal internal error\n");
444 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
445 "Connection reversal unavailable\n");
448 /* operation in progress */
455 ah = GNUNET_NAT_autoconfig_start (c,
459 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
466 * Main function of gnunet-nat
468 * @param argc number of command-line arguments
469 * @param argv command line
470 * @return 0 on success, -1 on error
476 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
478 gettext_noop ("run autoconfiguration"),
479 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto },
480 {'b', "bind", "ADDRESS",
481 gettext_noop ("which IP and port are we bound to"),
482 GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr },
483 {'e', "external", "ADDRESS",
484 gettext_noop ("which external IP and port should be used to test"),
485 GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
486 {'l', "local", "ADDRESS",
487 gettext_noop ("which IP and port are we locally using to listen to for connection reversals"),
488 GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
489 {'r', "remote", "ADDRESS",
490 gettext_noop ("which remote IP and port should be asked for connection reversal"),
491 GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
492 {'L', "listen", NULL,
493 gettext_noop ("listen for connection reversal requests"),
494 GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
496 gettext_noop ("port to use to advertise"),
497 GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
499 gettext_noop ("enable STUN processing"),
500 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
502 gettext_noop ("use TCP"),
503 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp },
505 gettext_noop ("use UDP"),
506 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
507 GNUNET_GETOPT_OPTION_END
511 GNUNET_STRINGS_get_utf8_args (argc, argv,
515 GNUNET_PROGRAM_run (argc, argv,
516 "gnunet-nat [options]",
517 _("GNUnet NAT traversal autoconfigure daemon"),
524 GNUNET_free ((void*) argv);
529 /* end of gnunet-nat.c */