GNUnet  0.10.x
gnunet-nat-auto.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2015, 2016, 2017 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_nat_service.h"
31 
35 static int global_ret;
36 
41 
46 static int write_cfg;
47 
51 static const char *cfg_file;
52 
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
57 
61 static char *section_name;
62 
66 static int do_auto;
67 
71 static struct GNUNET_NAT_AUTO_Test *nt;
72 
76 static int use_udp;
77 
81 static int use_tcp;
82 
86 static uint8_t proto;
87 
92 static void
94 {
95  if (NULL != ah)
96  return;
97  if (NULL != nt)
98  return;
100 }
101 
102 
111 static void
112 auto_conf_iter (void *cls,
113  const char *section,
114  const char *option,
115  const char *value)
116 {
117  struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
118 
119  PRINTF ("%s: %s\n",
120  option,
121  value);
122  if (NULL != new_cfg)
124  section,
125  option,
126  value);
127 }
128 
129 
139 static void
140 auto_config_cb (void *cls,
141  const struct GNUNET_CONFIGURATION_Handle *diff,
143  enum GNUNET_NAT_Type type)
144 {
145  const char *nat_type;
146  char unknown_type[64];
147  struct GNUNET_CONFIGURATION_Handle *new_cfg;
148 
149  ah = NULL;
150  switch (type)
151  {
153  nat_type = "NO NAT";
154  break;
156  nat_type = "NAT but we can traverse";
157  break;
159  nat_type = "NAT but STUN is able to identify the correct information";
160  break;
162  nat_type = "NAT but UPNP opened the ports";
163  break;
164  default:
165  SPRINTF (unknown_type,
166  "NAT unknown, type %u",
167  type);
168  nat_type = unknown_type;
169  break;
170  }
171 
173  "NAT status: %s/%s\n",
175  nat_type);
176 
177  if (NULL == diff)
178  return;
179 
180  /* Shortcut: if there are no changes suggested, bail out early. */
181  if (GNUNET_NO ==
183  {
184  test_finished ();
185  return;
186  }
187 
188  /* Apply diff to original configuration and show changes
189  to the user */
190  new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
191 
193  _("Suggested configuration changes:\n"));
195  "nat",
197  new_cfg);
198 
199  /* If desired, write configuration to file; we write only the
200  changes to the defaults to keep things compact. */
201  if (write_cfg)
202  {
203  struct GNUNET_CONFIGURATION_Handle *def_cfg;
204 
206  "ARM",
207  "CONFIG",
208  NULL);
209  def_cfg = GNUNET_CONFIGURATION_create ();
211  GNUNET_CONFIGURATION_load (def_cfg,
212  NULL));
213  if (GNUNET_OK !=
215  new_cfg,
216  cfg_file))
217  {
219  _("Failed to write configuration to `%s'\n"),
220  cfg_file);
221  global_ret = 1;
222  }
223  else
224  {
226  _("Wrote updated configuration to `%s'\n"),
227  cfg_file);
228  }
230  }
231 
232  if (NULL != new_cfg)
234  test_finished ();
235 }
236 
237 
245 static void
246 test_report_cb (void *cls,
248 {
249  nt = NULL;
250  PRINTF ("NAT test result: %s\n",
252  test_finished ();
253 }
254 
255 
261 static void
262 do_shutdown (void *cls)
263 {
264  if (NULL != ah)
265  {
267  ah = NULL;
268  }
269  if (NULL != nt)
270  {
272  nt = NULL;
273  }
274 }
275 
276 
285 static void
286 run (void *cls,
287  char *const *args,
288  const char *cfgfile,
289  const struct GNUNET_CONFIGURATION_Handle *c)
290 {
291  cfg_file = cfgfile;
292  cfg = c;
293 
295  NULL);
296 
297  if (do_auto)
298  {
301  NULL);
302  }
303 
304  if (use_tcp && use_udp)
305  {
306  if (do_auto)
307  return;
309  "Cannot use TCP and UDP\n");
310  global_ret = 1;
311  return;
312  }
313  proto = 0;
314  if (use_tcp)
315  proto = IPPROTO_TCP;
316  if (use_udp)
317  proto = IPPROTO_UDP;
318 
319  if (NULL != section_name)
320  {
322  proto,
323  section_name,
325  NULL);
326  }
327  test_finished ();
328 }
329 
330 
338 int
339 main (int argc,
340  char *const argv[])
341 {
342  struct GNUNET_GETOPT_CommandLineOption options[] = {
344  "auto",
345  gettext_noop ("run autoconfiguration"),
346  &do_auto),
347 
349  "section",
350  "NAME",
351  gettext_noop ("section name providing the configuration for the adapter"),
352  &section_name),
353 
355  "tcp",
356  gettext_noop ("use TCP"),
357  &use_tcp),
358 
360  "udp",
361  gettext_noop ("use UDP"),
362  &use_udp),
363 
365  "write",
366  gettext_noop ("write configuration file (for autoconfiguration)"),
367  &write_cfg),
369  };
370 
371  if (GNUNET_OK !=
372  GNUNET_STRINGS_get_utf8_args (argc, argv,
373  &argc, &argv))
374  return 2;
375  if (GNUNET_OK !=
376  GNUNET_PROGRAM_run (argc, argv,
377  "gnunet-nat-auto [options]",
378  _("GNUnet NAT traversal autoconfiguration"),
379  options,
380  &run,
381  NULL))
382  {
383  global_ret = 1;
384  }
385  GNUNET_free ((void*) argv);
386  return global_ret;
387 }
388 
389 
390 /* end of gnunet-nat-auto.c */
static int use_udp
Flag set to 1 if we use IPPROTO_UDP.
static int global_ret
Value to return from main().
static char * section_name
Adapter we are supposed to test.
void GNUNET_CONFIGURATION_iterate_section_values(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over values of a section in the configuration.
void GNUNET_NAT_AUTO_autoconfig_cancel(struct GNUNET_NAT_AUTO_AutoHandle *ah)
Abort autoconfiguration.
Definition: nat_auto_api.c:267
static void do_shutdown(void *cls)
Task run on shutdown.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1293
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
Handle to a NAT test.
const char * GNUNET_NAT_AUTO_status2string(enum GNUNET_NAT_StatusCode err)
Converts enum GNUNET_NAT_StatusCode to string.
Definition: nat_auto_api.c:71
#define GNUNET_NO
Definition: gnunet_common.h:81
void GNUNET_NAT_AUTO_test_stop(struct GNUNET_NAT_AUTO_Test *tst)
Stop an active NAT test.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Definition of a command line option.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
We are under a NAT but cannot traverse it.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
int GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static char * section
Name of the section.
Definition: gnunet-config.c:33
static void auto_config_cb(void *cls, const struct GNUNET_CONFIGURATION_Handle *diff, enum GNUNET_NAT_StatusCode result, enum GNUNET_NAT_Type type)
Function called with the result from the autoconfiguration.
static void test_report_cb(void *cls, enum GNUNET_NAT_StatusCode result)
Function called to report success or failure for NAT configuration test.
static void test_finished()
Test if all activities have finished, and if so, terminate.
static char * value
Value of the record to add/remove.
int GNUNET_CONFIGURATION_write_diffs(const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new, const char *filename)
Write only configuration entries that have been changed to configuration file.
int GNUNET_CONFIGURATION_is_dirty(const struct GNUNET_CONFIGURATION_Handle *cfg)
Test if there are configuration options that were changed since the last save.
GNUNET_NAT_StatusCode
Error Types for the NAT subsystem (which can then later be converted/resolved to a string) ...
static int write_cfg
If we do auto-configuration, should we write the result to a file?
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
static char * option
Name of the option.
Definition: gnunet-config.c:38
static int result
Global testing status.
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
static const char * cfg_file
Configuration filename.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Original configuration.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
#define PRINTF
Definition: plibc.h:682
int main(int argc, char *const argv[])
Main function of gnunet-nat-auto.
Handle to auto-configuration in progress.
Definition: nat_auto_api.c:38
#define SPRINTF
Definition: plibc.h:689
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
configuration data
Definition: configuration.c:85
We can traverse using UPNP.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
static int use_tcp
Flag set to 1 if we use IPPROTO_TCP.
#define GNUNET_log(kind,...)
We have a direct connection.
static uint8_t proto
Protocol to use.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:361
static int do_auto
Should we run autoconfiguration?
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
struct GNUNET_NAT_AUTO_AutoHandle * GNUNET_NAT_AUTO_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_NAT_AUTO_AutoResultCallback cb, void *cb_cls)
Start auto-configuration routine.
Definition: nat_auto_api.c:208
GNUNET_NAT_Type
What the situation of the NAT connectivity.
static void auto_conf_iter(void *cls, const char *section, const char *option, const char *value)
Function to iterate over sugested changes options.
struct GNUNET_NAT_AUTO_Test * GNUNET_NAT_AUTO_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg, uint8_t proto, const char *section_name, GNUNET_NAT_TestCallback report, void *report_cls)
Start testing if NAT traversal works using the given configuration.
We can traverse using STUN.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69