]> git.ipfire.org Git - network.git/commitdiff
networkd: zones: Try to read configuration automatically
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Feb 2023 22:39:34 +0000 (22:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Feb 2023 22:39:34 +0000 (22:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/string.h
src/networkd/zone.c

index 52b5add16c605b6dd6ce519bf3dc0227dbc1bed3..4ae2dbf281cc9f63886ab6551c49777425769579 100644 (file)
@@ -26,6 +26,9 @@
 #include <stdio.h>
 #include <string.h>
 
+#define nw_string_vformat(s, format, ...) \
+       __nw_string_vformat(s, sizeof(s), format, __VA_ARGS__)
+
 static inline int __nw_string_vformat(char* s, const size_t length,
                const char* format, va_list args) {
        // Write string to buffer
@@ -76,4 +79,26 @@ static inline int __nw_string_set(char* s, const size_t length, const char* valu
        return __nw_string_format(s, length, "%s", value);
 }
 
+/*
+       Paths
+*/
+
+#define nw_path_join(s, first, second) \
+       __nw_path_join(s, sizeof(s), first, second)
+
+static inline int __nw_path_join(char* s, const size_t length,
+               const char* first, const char* second) {
+       if (!first)
+               return __nw_string_format(s, length, "%s", second);
+
+       if (!second)
+               return __nw_string_format(s, length, "%s", first);
+
+       // Remove leading slashes from second argument
+       while (*second == '/')
+               second++;
+
+       return __nw_string_format(s, length, "%s/%s", first, second);
+}
+
 #endif /* NETWORKD_STRING_H */
index 5066c4b2bebf1b47510803d75f1e870ca0df16d6..4790d334de03f9ecc1c977ea012cb40bf2df7e35 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <limits.h>
 #include <stdlib.h>
 
 #include "config.h"
@@ -33,6 +34,32 @@ struct nw_zone {
        struct nw_config *config;
 };
 
+#define nw_zone_path(zone, path, format, ...) \
+       __nw_zone_path(zone, path, sizeof(path), format, __VA_ARGS__)
+
+static int __nw_zone_path(struct nw_zone* zone, char* p, const size_t length,
+               const char* format, ...) {
+       char prefix[NAME_MAX];
+       char suffix[NAME_MAX];
+       va_list args;
+       int r;
+
+       // Format the prefix
+       r = nw_string_format(prefix, "%s/zones/%s", CONFIG_DIR, zone->name);
+       if (r)
+               return r;
+
+       // Format the suffix
+       va_start(args, format);
+       r = nw_string_vformat(suffix, format, args);
+       va_end(args);
+       if (r)
+               return r;
+
+       // Join the two parts together
+       return __nw_path_join(p, length, prefix, suffix);
+}
+
 static void nw_zone_free(struct nw_zone* zone) {
        if (zone->config)
                nw_config_unref(zone->config);
@@ -40,6 +67,23 @@ static void nw_zone_free(struct nw_zone* zone) {
        free(zone);
 }
 
+static int nw_zone_setup(struct nw_zone* zone) {
+       char path[PATH_MAX];
+       int r;
+
+       // Compose the path to the main configuration file
+       r = nw_zone_path(zone, path, "%s", "settings");
+       if (r)
+               return r;
+
+       // Initialize the configuration
+       r = nw_config_create(&zone->config, path);
+       if (r)
+               return r;
+
+       return 0;
+}
+
 int nw_zone_create(struct nw_zone** zone, const char* name) {
        int r;
 
@@ -56,6 +100,11 @@ int nw_zone_create(struct nw_zone** zone, const char* name) {
        if (r)
                goto ERROR;
 
+       // Setup the zone
+       r = nw_zone_setup(z);
+       if (r)
+               goto ERROR;
+
        *zone = z;
        return 0;