gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [gnunet] 01/03: implement writing of configuration file in


From: gnunet
Subject: [GNUnet-SVN] [gnunet] 01/03: implement writing of configuration file in autoconfiguration
Date: Sun, 18 Dec 2016 07:38:17 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository gnunet.

commit bc4a5aeac8a6f1a1d25b118a26233d4fe7d23741
Author: Christian Grothoff <address@hidden>
AuthorDate: Sun Dec 18 07:06:20 2016 +0100

    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 a43223f..9cc8b12 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 <https://gnunet.org/bugs/> or by sending 
electronic mail to <address@hidden>
 
diff --git a/src/include/gnunet_configuration_lib.h 
b/src/include/gnunet_configuration_lib.h
index 945f3ca..746dea6 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 7d10167..f3e26ff 100644
--- a/src/nat/gnunet-nat.c
+++ b/src/nat/gnunet-nat.c
@@ -59,6 +59,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.
  */
 static uint8_t proto;
@@ -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
   };
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]