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?
75 static unsigned int do_stun;
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,
126 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
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");
272 if (NULL != local_addr)
274 local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr,
279 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
280 "Invalid socket address `%s'\n",
286 if (NULL != remote_addr)
288 remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
293 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
294 "Invalid socket address `%s'\n",
301 if (NULL != local_addr)
303 nh = GNUNET_NAT_register (c,
307 (const struct sockaddr **) &local_sa,
310 (listen_reversal) ? &reversal_cb : NULL,
313 else if (listen_reversal)
315 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
316 "Use of `-W` only effective in combination with `-i`\n");
318 GNUNET_SCHEDULER_shutdown ();
322 if (NULL != remote_addr)
327 (sizeof (struct sockaddr_in) != local_len) )
329 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
330 "Require IPv4 local address to initiate connection reversal\n");
332 GNUNET_SCHEDULER_shutdown ();
335 if (sizeof (struct sockaddr_in) != remote_len)
337 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
338 "Require IPv4 reversal target address\n");
340 GNUNET_SCHEDULER_shutdown ();
343 ret = GNUNET_NAT_request_reversal (nh,
344 (const struct sockaddr_in *) &local_sa,
345 (const struct sockaddr_in *) &remote_sa);
349 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
350 "Connection reversal internal error\n");
353 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
354 "Connection reversal unavailable\n");
357 /* operation in progress */
364 if (NULL == local_addr)
366 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
367 "Require local address to support STUN requests\n");
369 GNUNET_SCHEDULER_shutdown ();
372 if (IPPROTO_UDP != proto)
374 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
375 "STUN only supported over UDP\n");
377 GNUNET_SCHEDULER_shutdown ();
380 ls = GNUNET_NETWORK_socket_create (af,
384 GNUNET_NETWORK_socket_bind (ls,
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 "Failed to bind to %s: %s\n",
390 GNUNET_a2s (local_sa,
394 GNUNET_SCHEDULER_shutdown ();
397 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
408 * Main function of gnunet-nat
410 * @param argc number of command-line arguments
411 * @param argv command line
412 * @return 0 on success, -1 on error
418 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
419 {'i', "in", "ADDRESS",
420 gettext_noop ("which IP and port are we locally using to bind/listen to"),
421 GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
422 {'r', "remote", "ADDRESS",
423 gettext_noop ("which remote IP and port should be asked for connection reversal"),
424 GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
425 {'S', "section", NULL,
426 gettext_noop ("name of configuration section to find additional options, such as manual host punching data"),
427 GNUNET_YES, &GNUNET_GETOPT_set_string, §ion_name },
429 gettext_noop ("enable STUN processing"),
430 GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
432 gettext_noop ("use TCP"),
433 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp },
435 gettext_noop ("use UDP"),
436 GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
438 gettext_noop ("watch for connection reversal requests"),
439 GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
440 GNUNET_GETOPT_OPTION_END
444 GNUNET_STRINGS_get_utf8_args (argc, argv,
448 GNUNET_PROGRAM_run (argc, argv,
449 "gnunet-nat [options]",
450 _("GNUnet NAT traversal autoconfigure daemon"),
457 GNUNET_free ((void*) argv);
462 /* end of gnunet-nat.c */