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 // FIXME: 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.
222 * @param cls closure, NULL
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,
236 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
238 add_remove ? "+" : "-",
246 * Signature of the callback passed to #GNUNET_NAT_register().
247 * for a function to call whenever someone asks us to do connection
250 * @param cls closure, NULL
251 * @param local_addr address where we received the request
252 * @param local_addrlen actual length of the @a local_addr
253 * @param remote_addr public IP address of the other peer
254 * @param remote_addrlen actual length of the @a remote_addr
257 reversal_cb (void *cls,
258 const struct sockaddr *local_addr,
259 socklen_t local_addrlen,
260 const struct sockaddr *remote_addr,
261 socklen_t remote_addrlen)
263 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
264 "Connection reversal requested by %s\n",
265 GNUNET_a2s (remote_addr,
271 * Task run on shutdown.
276 do_shutdown (void *cls)
280 GNUNET_NAT_autoconfig_cancel (ah);
285 GNUNET_NAT_test_stop (nt);
290 GNUNET_NAT_unregister (nh);
297 * Main function that will be run.
300 * @param args remaining command-line arguments
301 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
302 * @param c configuration
308 const struct GNUNET_CONFIGURATION_Handle *c)
311 struct sockaddr_in bind_sa;
312 struct sockaddr_in extern_sa;
313 struct sockaddr *local_sa;
314 struct sockaddr *remote_sa;
318 if (use_tcp && use_udp)
320 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
321 "Cannot use TCP and UDP\n");
332 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
333 "Must specify either TCP or UDP\n");
337 if (NULL != bind_addr)
340 GNUNET_STRINGS_to_address_ipv4 (bind_addr,
344 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
345 "Invalid socket address `%s'\n",
351 if (NULL != extern_addr)
354 GNUNET_STRINGS_to_address_ipv4 (extern_addr,
355 strlen (extern_addr),
358 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
359 "Invalid socket address `%s'\n",
365 if (NULL != local_addr)
367 local_len = GNUNET_STRINGS_parse_socket_addr (local_addr,
372 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
373 "Invalid socket address `%s'\n",
379 if (NULL != remote_addr)
381 remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
386 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
387 "Invalid socket address `%s'\n",
394 if (NULL != bind_addr)
396 if (NULL == extern_addr)
398 nt = GNUNET_NAT_test_start (c,
401 ntohs (bind_sa.sin_port),
403 ntohs (extern_sa.sin_port),
408 if (NULL != local_addr)
410 nh = GNUNET_NAT_register (c,
414 (const struct sockaddr **) &local_sa,
417 (listen_reversal) ? &reversal_cb : NULL,
421 if (NULL != remote_addr)
426 (sizeof (struct sockaddr_in) != local_len) )
428 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
429 "Require IPv4 local address to initiate connection reversal\n");
431 GNUNET_SCHEDULER_shutdown ();
434 if (sizeof (struct sockaddr_in) != remote_len)
436 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
437 "Require IPv4 reversal target address\n");
439 GNUNET_SCHEDULER_shutdown ();
442 ret = GNUNET_NAT_request_reversal (nh,
443 (const struct sockaddr_in *) &local_sa,
444 (const struct sockaddr_in *) &remote_sa);
448 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
449 "Connection reversal internal error\n");
452 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
453 "Connection reversal unavailable\n");
456 /* operation in progress */
463 ah = GNUNET_NAT_autoconfig_start (c,
467 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
474 * Main function of gnunet-nat
476 * @param argc number of command-line arguments
477 * @param argv command line
478 * @return 0 on success, -1 on error
484 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
486 gettext_noop ("run autoconfiguration"),
487 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto },
488 {'b', "bind", "ADDRESS",
489 gettext_noop ("which IP and port are we bound to"),
490 GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr },
491 {'e', "external", "ADDRESS",
492 gettext_noop ("which external IP and port should be used to test"),
493 GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
494 {'l', "local", "ADDRESS",
495 gettext_noop ("which IP and port are we locally using to listen to for connection reversals"),
496 GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
497 {'r', "remote", "ADDRESS",
498 gettext_noop ("which remote IP and port should be asked for connection reversal"),
499 GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
500 {'L', "listen", NULL,
501 gettext_noop ("listen for connection reversal requests"),
502 GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
504 gettext_noop ("port to use to advertise"),
505 GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
507 gettext_noop ("enable STUN processing"),
508 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
509 // FIMXE: -s not implemented!
511 gettext_noop ("use TCP"),
512 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp },
514 gettext_noop ("use UDP"),
515 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
516 GNUNET_GETOPT_OPTION_END
520 GNUNET_STRINGS_get_utf8_args (argc, argv,
524 GNUNET_PROGRAM_run (argc, argv,
525 "gnunet-nat [options]",
526 _("GNUnet NAT traversal autoconfigure daemon"),
533 GNUNET_free ((void*) argv);
538 /* end of gnunet-nat.c */