tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / configuration.c
index ecc0990b561d3650262206769d97283bfcadff98..a38438ba833c6ebd58cd4f6534683a5dc99ac3f8 100644 (file)
@@ -3,7 +3,7 @@
      Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
 
      GNUnet is free software: you can redistribute it and/or modify it
-     under the terms of the GNU General Public License as published
+     under the terms of the GNU Affero General Public License as published
      by the Free Software Foundation, either version 3 of the License,
      or (at your option) any later version.
 
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
+
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -134,6 +139,41 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
 }
 
 
+/**
+ * Parse a configuration file @a filename and run the function
+ * @a cb with the resulting configuration object. Then free the
+ * configuration object and return the status value from @a cb.
+ *
+ * @param filename configuration to parse, NULL for "default"
+ * @param cb function to run
+ * @param cb_cls closure for @a cb
+ * @return #GNUNET_SYSERR if parsing the configuration failed,
+ *   otherwise return value from @a cb.
+ */
+int
+GNUNET_CONFIGURATION_parse_and_run (const char *filename,
+                                   GNUNET_CONFIGURATION_Callback cb,
+                                   void *cb_cls)
+{
+  struct GNUNET_CONFIGURATION_Handle *cfg;
+  int ret;
+
+  cfg = GNUNET_CONFIGURATION_create ();
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_load (cfg,
+                                 filename))
+  {
+    GNUNET_break (0);
+    GNUNET_CONFIGURATION_destroy (cfg);
+    return GNUNET_SYSERR;
+  }
+  ret = cb (cb_cls,
+           cfg);
+  GNUNET_CONFIGURATION_destroy (cfg);
+  return ret;
+}
+
+
 /**
  * De-serializes configuration
  *
@@ -230,7 +270,7 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
        GNUNET_asprintf (&fn,
                         "%s/%s",
                         basedir,
-                        value);        
+                        value);
        if (GNUNET_OK !=
            GNUNET_CONFIGURATION_parse (cfg,
                                        fn))
@@ -519,7 +559,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
     GNUNET_free (fn);
     GNUNET_free (cfg_buf);
     LOG (GNUNET_ERROR_TYPE_WARNING,
-        "Writing configration to file `%s' failed\n",
+        "Writing configuration to file `%s' failed\n",
          filename);
     cfg->dirty = GNUNET_SYSERR; /* last write failed */
     return GNUNET_SYSERR;
@@ -890,22 +930,27 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
-                                       *cfg, const char *section,
+GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                      const char *section,
                                        const char *option,
                                        unsigned long long *number)
 {
   struct ConfigEntry *e;
+  char dummy[2];
 
   if (NULL == (e = find_entry (cfg, section, option)))
     return GNUNET_SYSERR;
   if (NULL == e->val)
     return GNUNET_SYSERR;
-  if (1 != SSCANF (e->val, "%llu", number))
+  if (1 != SSCANF (e->val,
+                  "%llu%1s",
+                  number,
+                  dummy))
     return GNUNET_SYSERR;
   return GNUNET_OK;
 }
 
+
 /**
  * Get a configuration value that should be a floating point number.
  *
@@ -916,18 +961,22 @@ GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_CONFIGURATION_get_value_float  (const struct GNUNET_CONFIGURATION_Handle
-                                       *cfg, const char *section,
+GNUNET_CONFIGURATION_get_value_float  (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                      const char *section,
                                        const char *option,
                                        float *number)
 {
   struct ConfigEntry *e;
+  char dummy[2];
 
   if (NULL == (e = find_entry (cfg, section, option)))
     return GNUNET_SYSERR;
   if (NULL == e->val)
     return GNUNET_SYSERR;
-  if (1 != SSCANF (e->val, "%f", number))
+  if (1 != SSCANF (e->val,
+                  "%f%1s",
+                  number,
+                  dummy))
     return GNUNET_SYSERR;
   return GNUNET_OK;
 }
@@ -950,12 +999,22 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *c
                                      struct GNUNET_TIME_Relative *time)
 {
   struct ConfigEntry *e;
+  int ret;
 
-  if (NULL == (e = find_entry (cfg, section, option)))
+  if (NULL == (e = find_entry (cfg,
+                               section,
+                               option)))
     return GNUNET_SYSERR;
   if (NULL == e->val)
     return GNUNET_SYSERR;
-  return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
+  ret = GNUNET_STRINGS_fancy_time_to_relative (e->val,
+                                               time);
+  if (GNUNET_OK != ret)
+    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+                               section,
+                               option,
+                               _("Not a valid relative time specification"));
+  return ret;
 }
 
 
@@ -1691,7 +1750,9 @@ GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
                                const char *defaults_d)
 {
   if (GNUNET_SYSERR ==
-      GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
+      GNUNET_DISK_directory_scan (defaults_d,
+                                  &parse_configuration_file,
+                                  cfg))
     return GNUNET_SYSERR;       /* no configuration at all found */
   return GNUNET_OK;
 }