From: Michael Tremer Date: Wed, 1 Feb 2023 22:39:34 +0000 (+0000) Subject: networkd: zones: Try to read configuration automatically X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=00b5de56ae30d4d8f0288ec160570ab6129aec79;p=network.git networkd: zones: Try to read configuration automatically Signed-off-by: Michael Tremer --- diff --git a/src/networkd/string.h b/src/networkd/string.h index 52b5add1..4ae2dbf2 100644 --- a/src/networkd/string.h +++ b/src/networkd/string.h @@ -26,6 +26,9 @@ #include #include +#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 */ diff --git a/src/networkd/zone.c b/src/networkd/zone.c index 5066c4b2..4790d334 100644 --- a/src/networkd/zone.c +++ b/src/networkd/zone.c @@ -18,6 +18,7 @@ # # #############################################################################*/ +#include #include #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;