From bc4a5aeac8a6f1a1d25b118a26233d4fe7d23741 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 18 Dec 2016 07:06:20 +0100 Subject: [PATCH] implement writing of configuration file in autoconfiguration --- doc/man/gnunet-nat.1 | 4 ++ src/include/gnunet_configuration_lib.h | 2 +- src/nat/gnunet-nat.c | 92 ++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/doc/man/gnunet-nat.1 b/doc/man/gnunet-nat.1 index a43223fa8..9cc8b1237 100644 --- a/doc/man/gnunet-nat.1 +++ b/doc/man/gnunet-nat.1 @@ -58,6 +58,10 @@ Use TCP. .IP "\-u, \-\-udp" Use UDP. +.B +.IP "\-w, \-\-write" +Write configuration to configuration file, useful in combination with autoconfiguration (\-a). + .SH BUGS Report bugs by using Mantis or by sending electronic mail to diff --git a/src/include/gnunet_configuration_lib.h b/src/include/gnunet_configuration_lib.h index 945f3ca59..746dea61f 100644 --- a/src/include/gnunet_configuration_lib.h +++ b/src/include/gnunet_configuration_lib.h @@ -89,7 +89,7 @@ GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, /** * Load default configuration. This function will parse the - * defaults from the given defaults_d directory. + * defaults from the given @a defaults_d directory. * * @param cfg configuration to update * @param defaults_d directory with the defaults diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c index 7d10167ce..f3e26ffb1 100644 --- a/src/nat/gnunet-nat.c +++ b/src/nat/gnunet-nat.c @@ -58,6 +58,22 @@ static int listen_reversal; */ static int use_tcp; +/** + * If we do auto-configuration, should we write the result + * to a file? + */ +static int write_cfg; + +/** + * Configuration filename. + */ +static const char *cfg_file; + +/** + * Original configuration. + */ +static const struct GNUNET_CONFIGURATION_Handle *cfg; + /** * Protocol to use. */ @@ -149,9 +165,16 @@ auto_conf_iter (void *cls, const char *option, const char *value) { + struct GNUNET_CONFIGURATION_Handle *new_cfg = cls; + PRINTF ("%s: %s\n", option, value); + if (NULL != new_cfg) + GNUNET_CONFIGURATION_set_value_string (new_cfg, + section, + option, + value); } @@ -172,6 +195,7 @@ auto_config_cb (void *cls, { const char *nat_type; char unknown_type[64]; + struct GNUNET_CONFIGURATION_Handle *new_cfg; ah = NULL; switch (type) @@ -196,19 +220,69 @@ auto_config_cb (void *cls, break; } - PRINTF ("NAT status: %s/%s\n", - GNUNET_NAT_status2string (result), - nat_type); + GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, + "NAT status: %s/%s\n", + GNUNET_NAT_status2string (result), + nat_type); + + /* Shortcut: if there are no changes suggested, bail out early. */ + if (GNUNET_NO == + GNUNET_CONFIGURATION_is_dirty (diff)) + { + test_finished (); + return; + } + /* Apply diff to original configuration and show changes + to the user */ + new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL; + if (NULL != diff) { - PRINTF ("SUGGESTED CHANGES:\n"); + GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, + _("Suggested configuration changes:\n")); GNUNET_CONFIGURATION_iterate_section_values (diff, "nat", &auto_conf_iter, - NULL); + new_cfg); + } + + /* If desired, write configuration to file; we write only the + changes to the defaults to keep things compact. */ + if ( (write_cfg) && + (NULL != diff) ) + { + struct GNUNET_CONFIGURATION_Handle *def_cfg; + + GNUNET_CONFIGURATION_set_value_string (new_cfg, + "ARM", + "CONFIG", + NULL); + def_cfg = GNUNET_CONFIGURATION_create (); + GNUNET_break (GNUNET_OK == + GNUNET_CONFIGURATION_load (def_cfg, + NULL)); + if (GNUNET_OK != + GNUNET_CONFIGURATION_write_diffs (def_cfg, + new_cfg, + cfg_file)) + { + GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, + _("Failed to write configuration to `%s'\n"), + cfg_file); + global_ret = 1; + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, + _("Wrote updated configuration to `%s'\n"), + cfg_file); + } + GNUNET_CONFIGURATION_destroy (def_cfg); } - // FIXME: have option to save config + + if (NULL != new_cfg) + GNUNET_CONFIGURATION_destroy (new_cfg); test_finished (); } @@ -387,6 +461,9 @@ run (void *cls, struct sockaddr *remote_sa; size_t local_len; size_t remote_len; + + cfg_file = cfgfile; + cfg = c; if (use_tcp && use_udp) { @@ -631,6 +708,9 @@ main (int argc, {'u', "udp", NULL, gettext_noop ("use UDP"), GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp }, + {'w', "write", NULL, + gettext_noop ("write configuration file (for autoconfiguration)"), + GNUNET_NO, &GNUNET_GETOPT_set_one, &write_cfg }, GNUNET_GETOPT_OPTION_END }; -- 2.25.1