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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 * Name of section in configuration file to use for
40 static char *section_name;
43 * Flag set to 1 if we use IPPROTO_UDP.
48 * Flag set to 1 if we are to listen for connection reversal requests.
50 static int listen_reversal;
53 * Flag set to 1 if we use IPPROTO_TCP.
63 * Local address to use for connection reversal request.
65 static char *local_addr;
68 * Remote address to use for connection reversal request.
70 static char *remote_addr;
73 * Should we actually bind to #bind_addr and receive and process STUN requests?
78 * Handle to NAT operation.
80 static struct GNUNET_NAT_Handle *nh;
83 * Listen socket for STUN processing.
85 static struct GNUNET_NETWORK_Handle *ls;
88 * Task for reading STUN packets.
90 static struct GNUNET_SCHEDULER_Task *rtask;
94 * Test if all activities have finished, and if so,
104 GNUNET_SCHEDULER_shutdown ();
109 * Signature of the callback passed to #GNUNET_NAT_register() for
110 * a function to call whenever our set of 'valid' addresses changes.
112 * @param cls closure, NULL
113 * @param app_ctx[in,out] location where the app can store stuff
114 * on add and retrieve it on remove
115 * @param add_remove #GNUNET_YES to add a new public IP address,
116 * #GNUNET_NO to remove a previous (now invalid) one
117 * @param ac address class the address belongs to
118 * @param addr either the previous or the new public IP address
119 * @param addrlen actual length of the @a addr
122 address_cb (void *cls,
125 enum GNUNET_NAT_AddressClass ac,
126 const struct sockaddr *addr,
134 add_remove ? "+" : "-",
142 * Signature of the callback passed to #GNUNET_NAT_register().
143 * for a function to call whenever someone asks us to do connection
146 * @param cls closure, NULL
147 * @param remote_addr public IP address of the other peer
148 * @param remote_addrlen actual length of the @a remote_addr
151 reversal_cb (void *cls,
152 const struct sockaddr *remote_addr,
153 socklen_t remote_addrlen)
155 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
156 "Connection reversal requested by %s\n",
157 GNUNET_a2s (remote_addr,
163 * Task run on shutdown.
168 do_shutdown (void *cls)
172 GNUNET_NAT_unregister (nh);
177 GNUNET_NETWORK_socket_close (ls);
182 GNUNET_SCHEDULER_cancel (rtask);
189 * Task to receive incoming packets for STUN processing.
192 stun_read_task (void *cls)
196 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
200 size = GNUNET_NETWORK_socket_recvfrom_amount (ls);
204 GNUNET_SCHEDULER_shutdown ();
210 struct sockaddr_storage sa;
211 socklen_t salen = sizeof (sa);
214 ret = GNUNET_NETWORK_socket_recvfrom (ls,
217 (struct sockaddr *) &sa,
222 GNUNET_SCHEDULER_shutdown ();
226 (void) GNUNET_NAT_stun_handle_packet (nh,
227 (const struct sockaddr *) &sa,
236 * Main function that will be run.
239 * @param args remaining command-line arguments
240 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
241 * @param c configuration
247 const struct GNUNET_CONFIGURATION_Handle *c)
250 struct sockaddr *local_sa;
251 struct sockaddr *remote_sa;
255 if (use_tcp && use_udp)
257 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
258 "Cannot use TCP and UDP\n");
268 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
273 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
274 "Must specify either TCP or UDP\n");
282 if (NULL != local_addr)
284 local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr,
289 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
290 "Invalid socket address `%s'\n",
292 goto fail_and_shutdown;
296 if (NULL != remote_addr)
298 remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
303 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
304 "Invalid socket address `%s'\n",
306 goto fail_and_shutdown;
310 if (NULL != local_addr)
312 if (NULL == section_name)
313 section_name = GNUNET_strdup ("undefined");
314 nh = GNUNET_NAT_register (c,
318 (const struct sockaddr **) &local_sa,
321 (listen_reversal) ? &reversal_cb : NULL,
324 else if (listen_reversal)
326 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
327 "Use of `-W` only effective in combination with `-i`\n");
328 goto fail_and_shutdown;
331 if (NULL != remote_addr)
336 (sizeof (struct sockaddr_in) != local_len) )
338 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
339 "Require IPv4 local address to initiate connection reversal\n");
340 goto fail_and_shutdown;
342 if (sizeof (struct sockaddr_in) != remote_len)
344 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
345 "Require IPv4 reversal target address\n");
346 goto fail_and_shutdown;
348 GNUNET_assert (AF_INET == local_sa->sa_family);
349 GNUNET_assert (AF_INET == remote_sa->sa_family);
350 ret = GNUNET_NAT_request_reversal (nh,
351 (const struct sockaddr_in *) local_sa,
352 (const struct sockaddr_in *) remote_sa);
356 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
357 "Connection reversal internal error\n");
360 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
361 "Connection reversal unavailable\n");
364 /* operation in progress */
371 if (NULL == local_addr)
373 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
374 "Require local address to support STUN requests\n");
375 goto fail_and_shutdown;
377 if (IPPROTO_UDP != proto)
379 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
380 "STUN only supported over UDP\n");
381 goto fail_and_shutdown;
383 ls = GNUNET_NETWORK_socket_create (af,
388 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
389 "Failed to create socket\n");
390 goto fail_and_shutdown;
393 GNUNET_NETWORK_socket_bind (ls,
397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
398 "Failed to bind to %s: %s\n",
399 GNUNET_a2s (local_sa,
402 goto fail_and_shutdown;
404 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
409 GNUNET_free_non_null (remote_sa);
410 GNUNET_free_non_null (local_sa);
415 GNUNET_SCHEDULER_shutdown ();
416 GNUNET_free_non_null (remote_sa);
417 GNUNET_free_non_null (local_sa);
422 * Main function of gnunet-nat
424 * @param argc number of command-line arguments
425 * @param argv command line
426 * @return 0 on success, -1 on error
432 struct GNUNET_GETOPT_CommandLineOption options[] = {
434 GNUNET_GETOPT_option_string ('i',
437 gettext_noop ("which IP and port are we locally using to bind/listen to"),
440 GNUNET_GETOPT_option_string ('r',
443 gettext_noop ("which remote IP and port should be asked for connection reversal"),
446 GNUNET_GETOPT_option_string ('S',
449 gettext_noop ("name of configuration section to find additional options, such as manual host punching data"),
452 GNUNET_GETOPT_option_flag ('s',
454 gettext_noop ("enable STUN processing"),
457 GNUNET_GETOPT_option_flag ('t',
459 gettext_noop ("use TCP"),
462 GNUNET_GETOPT_option_flag ('u',
464 gettext_noop ("use UDP"),
467 GNUNET_GETOPT_option_flag ('W',
469 gettext_noop ("watch for connection reversal requests"),
471 GNUNET_GETOPT_OPTION_END
475 GNUNET_STRINGS_get_utf8_args (argc, argv,
479 GNUNET_PROGRAM_run (argc, argv,
480 "gnunet-nat [options]",
481 _("GNUnet NAT traversal autoconfigure daemon"),
488 GNUNET_free ((void*) argv);
493 /* end of gnunet-nat.c */