]> git.ipfire.org Git - zone-sync.git/commitdiff
main: Add a convenience function to parse dns_name_t from string
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 11 May 2026 14:44:26 +0000 (14:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 11 May 2026 14:44:26 +0000 (14:44 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
main.c

diff --git a/main.c b/main.c
index 157e4f6e76f1a52c9820128b3c22de7eb8e11862..3dcbbe606e50180bd220f201b65a6eb51cbef217 100644 (file)
--- a/main.c
+++ b/main.c
@@ -81,6 +81,8 @@ static ctx_t ctx = {
        .path = DEFAULT_PATH,
 };
 
+static dns_fixedname_t fixed = {};
+
 static void logger(int priority, const char* format, ...) {
        char buffer[4096];
        FILE* f = NULL;
@@ -160,6 +162,32 @@ static void setup_logging(void) {
        isc_log_setcontext(ctx.log);
 }
 
+static isc_result_t dns_name_from_string(dns_name_t** name, const char *text) {
+       isc_buffer_t buffer = {};
+       dns_name_t* n = NULL;
+       int r;
+
+       // Allocate some memory
+       n = dns_fixedname_initname(&fixed);
+
+       // Determine the length of the input
+       size_t l = strlen(text);
+
+       // Add the name to the buffer
+       isc_buffer_constinit(&buffer, text, l);
+       isc_buffer_add(&buffer, l);
+
+       // Create a new dns_name_t object from the buffer
+       r = dns_name_fromtext(n, &buffer, dns_rootname, 0, NULL);
+       if (r)
+               return r;
+
+       // Return the name
+       *name = n;
+
+       return 0;
+}
+
 static void zone_done(dns_zone_t* zone) {
        // Release the zone from the manager
     dns_zonemgr_releasezone(ctx.zonemgr, zone);
@@ -265,10 +293,8 @@ ERROR:
 }
 
 static void do_zone(const char* name) {
-       dns_fixedname_t fixed = {};
        dns_name_t* origin = NULL;
        dns_zone_t* zone = NULL;
-       isc_buffer_t buffer;
        char path[PATH_MAX];
        int r;
 
@@ -276,12 +302,9 @@ static void do_zone(const char* name) {
        ctx.running++;
 
        // Create the origin
-       origin = dns_fixedname_initname(&fixed);
-
-       isc_buffer_constinit(&buffer, name, strlen(name));
-       isc_buffer_add(&buffer, strlen(name));
-
-       dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL);
+       r = dns_name_from_string(&origin, name);
+       if (r)
+               goto ERROR;
 
        DEBUG("Processing zone %s\n", name);