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 * 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 add_remove #GNUNET_YES to add a new public IP address,
114 * #GNUNET_NO to remove a previous (now invalid) one
115 * @param ac address class the address belongs to
116 * @param addr either the previous or the new public IP address
117 * @param addrlen actual length of the @a addr
120 address_cb (void *cls,
122 enum GNUNET_NAT_AddressClass ac,
123 const struct sockaddr *addr,
128 add_remove ? "+" : "-",
136 * Signature of the callback passed to #GNUNET_NAT_register().
137 * for a function to call whenever someone asks us to do connection
140 * @param cls closure, NULL
141 * @param remote_addr public IP address of the other peer
142 * @param remote_addrlen actual length of the @a remote_addr
145 reversal_cb (void *cls,
146 const struct sockaddr *remote_addr,
147 socklen_t remote_addrlen)
149 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
150 "Connection reversal requested by %s\n",
151 GNUNET_a2s (remote_addr,
157 * Task run on shutdown.
162 do_shutdown (void *cls)
166 GNUNET_NAT_unregister (nh);
171 GNUNET_NETWORK_socket_close (ls);
176 GNUNET_SCHEDULER_cancel (rtask);
183 * Task to receive incoming packets for STUN processing.
186 stun_read_task (void *cls)
190 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
194 size = GNUNET_NETWORK_socket_recvfrom_amount (ls);
198 GNUNET_SCHEDULER_shutdown ();
204 struct sockaddr_storage sa;
205 socklen_t salen = sizeof (sa);
208 ret = GNUNET_NETWORK_socket_recvfrom (ls,
211 (struct sockaddr *) &sa,
216 GNUNET_SCHEDULER_shutdown ();
220 (void) GNUNET_NAT_stun_handle_packet (nh,
221 (const struct sockaddr *) &sa,
230 * Main function that will be run.
233 * @param args remaining command-line arguments
234 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
235 * @param c configuration
241 const struct GNUNET_CONFIGURATION_Handle *c)
244 struct sockaddr *local_sa;
245 struct sockaddr *remote_sa;
249 if (use_tcp && use_udp)
251 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
252 "Cannot use TCP and UDP\n");
262 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
267 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
268 "Must specify either TCP or UDP\n");
276 if (NULL != local_addr)
278 local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr,
283 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
284 "Invalid socket address `%s'\n",
286 goto fail_and_shutdown;
290 if (NULL != remote_addr)
292 remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
297 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
298 "Invalid socket address `%s'\n",
300 goto fail_and_shutdown;
304 if (NULL != local_addr)
306 if (NULL == section_name)
307 section_name = GNUNET_strdup ("undefined");
308 nh = GNUNET_NAT_register (c,
312 (const struct sockaddr **) &local_sa,
315 (listen_reversal) ? &reversal_cb : NULL,
318 else if (listen_reversal)
320 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
321 "Use of `-W` only effective in combination with `-i`\n");
322 goto fail_and_shutdown;
325 if (NULL != remote_addr)
330 (sizeof (struct sockaddr_in) != local_len) )
332 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
333 "Require IPv4 local address to initiate connection reversal\n");
334 goto fail_and_shutdown;
336 if (sizeof (struct sockaddr_in) != remote_len)
338 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
339 "Require IPv4 reversal target address\n");
340 goto fail_and_shutdown;
342 GNUNET_assert (AF_INET == local_sa->sa_family);
343 GNUNET_assert (AF_INET == remote_sa->sa_family);
344 ret = GNUNET_NAT_request_reversal (nh,
345 (const struct sockaddr_in *) local_sa,
346 (const struct sockaddr_in *) remote_sa);
350 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
351 "Connection reversal internal error\n");
354 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
355 "Connection reversal unavailable\n");
358 /* operation in progress */
365 if (NULL == local_addr)
367 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
368 "Require local address to support STUN requests\n");
369 goto fail_and_shutdown;
371 if (IPPROTO_UDP != proto)
373 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
374 "STUN only supported over UDP\n");
375 goto fail_and_shutdown;
377 ls = GNUNET_NETWORK_socket_create (af,
382 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
383 "Failed to create socket\n");
384 goto fail_and_shutdown;
387 GNUNET_NETWORK_socket_bind (ls,
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 "Failed to bind to %s: %s\n",
393 GNUNET_a2s (local_sa,
396 goto fail_and_shutdown;
398 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
403 GNUNET_free_non_null (remote_sa);
404 GNUNET_free_non_null (local_sa);
409 GNUNET_SCHEDULER_shutdown ();
410 GNUNET_free_non_null (remote_sa);
411 GNUNET_free_non_null (local_sa);
416 * Main function of gnunet-nat
418 * @param argc number of command-line arguments
419 * @param argv command line
420 * @return 0 on success, -1 on error
426 struct GNUNET_GETOPT_CommandLineOption options[] = {
428 GNUNET_GETOPT_option_string ('i',
431 gettext_noop ("which IP and port are we locally using to bind/listen to"),
434 GNUNET_GETOPT_option_string ('r',
437 gettext_noop ("which remote IP and port should be asked for connection reversal"),
440 GNUNET_GETOPT_option_string ('S',
443 gettext_noop ("name of configuration section to find additional options, such as manual host punching data"),
446 GNUNET_GETOPT_option_flag ('s',
448 gettext_noop ("enable STUN processing"),
451 GNUNET_GETOPT_option_flag ('t',
453 gettext_noop ("use TCP"),
456 GNUNET_GETOPT_option_flag ('u',
458 gettext_noop ("use UDP"),
461 GNUNET_GETOPT_option_flag ('W',
463 gettext_noop ("watch for connection reversal requests"),
465 GNUNET_GETOPT_OPTION_END
469 GNUNET_STRINGS_get_utf8_args (argc, argv,
473 GNUNET_PROGRAM_run (argc, argv,
474 "gnunet-nat [options]",
475 _("GNUnet NAT traversal autoconfigure daemon"),
482 GNUNET_free ((void*) argv);
487 /* end of gnunet-nat.c */