GNUnet  0.10.x
Functions | Variables
gnunet-nat-auto.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_nat_service.h"
#include "gnunet_nat_auto_service.h"
Include dependency graph for gnunet-nat-auto.c:

Go to the source code of this file.

Functions

static void test_finished ()
 Test if all activities have finished, and if so, terminate. More...
 
static void auto_conf_iter (void *cls, const char *section, const char *option, const char *value)
 Function to iterate over sugested changes options. More...
 
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. More...
 
static void test_report_cb (void *cls, enum GNUNET_NAT_StatusCode result)
 Function called to report success or failure for NAT configuration test. More...
 
static void do_shutdown (void *cls)
 Task run on shutdown. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
 Main function that will be run. More...
 
int main (int argc, char *const argv[])
 Main function of gnunet-nat-auto. More...
 

Variables

static int global_ret
 Value to return from main(). More...
 
static struct GNUNET_NAT_AUTO_AutoHandleah
 Handle to ongoing autoconfiguration. More...
 
static int write_cfg
 If we do auto-configuration, should we write the result to a file? More...
 
static const char * cfg_file
 Configuration filename. More...
 
static const struct GNUNET_CONFIGURATION_Handlecfg
 Original configuration. More...
 
static char * section_name
 Adapter we are supposed to test. More...
 
static int do_auto
 Should we run autoconfiguration? More...
 
static struct GNUNET_NAT_AUTO_Testnt
 Handle to a NAT test operation. More...
 
static int use_udp
 Flag set to 1 if we use IPPROTO_UDP. More...
 
static int use_tcp
 Flag set to 1 if we use IPPROTO_TCP. More...
 
static uint8_t proto
 Protocol to use. More...
 

Function Documentation

◆ test_finished()

static void test_finished ( )
static

Test if all activities have finished, and if so, terminate.

Definition at line 93 of file gnunet-nat-auto.c.

References GNUNET_SCHEDULER_shutdown().

Referenced by auto_config_cb(), run(), and test_report_cb().

94 {
95  if (NULL != ah)
96  return;
97  if (NULL != nt)
98  return;
100 }
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ auto_conf_iter()

static void auto_conf_iter ( void *  cls,
const char *  section,
const char *  option,
const char *  value 
)
static

Function to iterate over sugested changes options.

Parameters
clsclosure
sectionname of the section
optionname of the option
valuevalue of the option

Definition at line 112 of file gnunet-nat-auto.c.

References GNUNET_CONFIGURATION_set_value_string(), and PRINTF.

Referenced by auto_config_cb().

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 }
static char * section
Name of the section.
Definition: gnunet-config.c:33
static char * value
Value of the record to add/remove.
static char * option
Name of the option.
Definition: gnunet-config.c:38
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.
#define PRINTF
Definition: plibc.h:682
configuration data
Definition: configuration.c:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ auto_config_cb()

static void auto_config_cb ( void *  cls,
const struct GNUNET_CONFIGURATION_Handle diff,
enum GNUNET_NAT_StatusCode  result,
enum GNUNET_NAT_Type  type 
)
static

Function called with the result from the autoconfiguration.

Parameters
clsclosure
diffminimal suggested changes to the original configuration to make it work (as best as we can)
resultGNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
typewhat the situation of the NAT

Definition at line 140 of file gnunet-nat-auto.c.

References _, auto_conf_iter(), cfg_file, global_ret, GNUNET_break, GNUNET_CONFIGURATION_create(), GNUNET_CONFIGURATION_destroy(), GNUNET_CONFIGURATION_dup(), GNUNET_CONFIGURATION_is_dirty(), GNUNET_CONFIGURATION_iterate_section_values(), GNUNET_CONFIGURATION_load(), GNUNET_CONFIGURATION_set_value_string(), GNUNET_CONFIGURATION_write_diffs(), GNUNET_ERROR_TYPE_MESSAGE, GNUNET_log, GNUNET_NAT_AUTO_status2string(), GNUNET_NAT_TYPE_NO_NAT, GNUNET_NAT_TYPE_STUN_PUNCHED_NAT, GNUNET_NAT_TYPE_UNREACHABLE_NAT, GNUNET_NAT_TYPE_UPNP_NAT, GNUNET_NO, GNUNET_OK, SPRINTF, test_finished(), and write_cfg.

Referenced by run().

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 }
static int global_ret
Value to return from main().
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.
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
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
We are under a NAT but cannot traverse it.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
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 void test_finished()
Test if all activities have finished, and if so, terminate.
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.
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 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.
#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.
#define GNUNET_log(kind,...)
We have a direct connection.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static void auto_conf_iter(void *cls, const char *section, const char *option, const char *value)
Function to iterate over sugested changes options.
We can traverse using STUN.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_report_cb()

static void test_report_cb ( void *  cls,
enum GNUNET_NAT_StatusCode  result 
)
static

Function called to report success or failure for NAT configuration test.

Parameters
clsclosure
resultGNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code

Definition at line 246 of file gnunet-nat-auto.c.

References GNUNET_NAT_AUTO_status2string(), PRINTF, and test_finished().

Referenced by run().

248 {
249  nt = NULL;
250  PRINTF ("NAT test result: %s\n",
252  test_finished ();
253 }
const char * GNUNET_NAT_AUTO_status2string(enum GNUNET_NAT_StatusCode err)
Converts enum GNUNET_NAT_StatusCode to string.
Definition: nat_auto_api.c:71
static void test_finished()
Test if all activities have finished, and if so, terminate.
static int result
Global testing status.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
#define PRINTF
Definition: plibc.h:682
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_shutdown()

static void do_shutdown ( void *  cls)
static

Task run on shutdown.

Parameters
clsNULL

Definition at line 262 of file gnunet-nat-auto.c.

References GNUNET_NAT_AUTO_autoconfig_cancel(), and GNUNET_NAT_AUTO_test_stop().

Referenced by run().

263 {
264  if (NULL != ah)
265  {
267  ah = NULL;
268  }
269  if (NULL != nt)
270  {
272  nt = NULL;
273  }
274 }
void GNUNET_NAT_AUTO_autoconfig_cancel(struct GNUNET_NAT_AUTO_AutoHandle *ah)
Abort autoconfiguration.
Definition: nat_auto_api.c:267
void GNUNET_NAT_AUTO_test_stop(struct GNUNET_NAT_AUTO_Test *tst)
Stop an active NAT test.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle c 
)
static

Main function that will be run.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cconfiguration

Definition at line 286 of file gnunet-nat-auto.c.

References auto_config_cb(), cfg_file, do_auto, do_shutdown(), global_ret, GNUNET_ERROR_TYPE_MESSAGE, GNUNET_log, GNUNET_NAT_AUTO_autoconfig_start(), GNUNET_NAT_AUTO_test_start(), GNUNET_SCHEDULER_add_shutdown(), proto, section_name, test_finished(), test_report_cb(), use_tcp, and use_udp.

Referenced by main().

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 }
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.
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
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 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.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
static int use_tcp
Flag set to 1 if we use IPPROTO_TCP.
#define GNUNET_log(kind,...)
static uint8_t proto
Protocol to use.
static int do_auto
Should we run autoconfiguration?
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
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const  argv[] 
)

Main function of gnunet-nat-auto.

Parameters
argcnumber of command-line arguments
argvcommand line
Returns
0 on success, -1 on error

Definition at line 339 of file gnunet-nat-auto.c.

References _, do_auto, gettext_noop, global_ret, GNUNET_free, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_GETOPT_option_string(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), run(), section_name, use_tcp, use_udp, and write_cfg.

341 {
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 }
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.
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
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
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Definition of a command line option.
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static int write_cfg
If we do auto-configuration, should we write the result to a file?
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.
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?
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:

Variable Documentation

◆ global_ret

int global_ret
static

Value to return from main().

Definition at line 35 of file gnunet-nat-auto.c.

Referenced by auto_config_cb(), main(), and run().

◆ ah

struct GNUNET_NAT_AUTO_AutoHandle* ah
static

◆ write_cfg

int write_cfg
static

If we do auto-configuration, should we write the result to a file?

Definition at line 46 of file gnunet-nat-auto.c.

Referenced by auto_config_cb(), and main().

◆ cfg_file

const char* cfg_file
static

Configuration filename.

Definition at line 51 of file gnunet-nat-auto.c.

Referenced by auto_config_cb(), and run().

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Original configuration.

Definition at line 56 of file gnunet-nat-auto.c.

◆ section_name

char* section_name
static

Adapter we are supposed to test.

Definition at line 61 of file gnunet-nat-auto.c.

Referenced by main(), and run().

◆ do_auto

int do_auto
static

Should we run autoconfiguration?

Definition at line 66 of file gnunet-nat-auto.c.

Referenced by main(), and run().

◆ nt

struct GNUNET_NAT_AUTO_Test* nt
static

Handle to a NAT test operation.

Definition at line 71 of file gnunet-nat-auto.c.

Referenced by GNUNET_TRANSPORT_communicator_address_add(), GNUNET_TRANSPORT_communicator_mq_add(), nat_address_cb(), and sock_read().

◆ use_udp

int use_udp
static

Flag set to 1 if we use IPPROTO_UDP.

Definition at line 76 of file gnunet-nat-auto.c.

Referenced by main(), and run().

◆ use_tcp

int use_tcp
static

Flag set to 1 if we use IPPROTO_TCP.

Definition at line 81 of file gnunet-nat-auto.c.

Referenced by main(), and run().

◆ proto

uint8_t proto
static

Protocol to use.

Definition at line 86 of file gnunet-nat-auto.c.

Referenced by run().