]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: add helper to extract server lists
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 2 Apr 2020 09:59:47 +0000 (11:59 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 10 Apr 2020 15:57:19 +0000 (17:57 +0200)
This code is repeable enough to define a common implementation.

src/network/networkd-dhcp-server.c

index bbaa10f5ab276e0b945144331178cbcd3be624db..f27f7247f4e3abbca93995b1a7ab9e0e3cd6e164 100644 (file)
@@ -458,30 +458,23 @@ int dhcp4_server_configure(Link *link) {
         return 0;
 }
 
-int config_parse_dhcp_server_dns(
+static int config_parse_dhcp_lease_server_list(
                 const char *unit,
                 const char *filename,
                 unsigned line,
-                const char *section,
-                unsigned section_line,
                 const char *lvalue,
-                int ltype,
                 const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        Network *n = data;
-        const char *p = rvalue;
-        int r;
+                struct in_addr **addresses,
+                unsigned *n_addresses) {
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
 
-        for (;;) {
+        for (const char *p = rvalue;;) {
                 _cleanup_free_ char *w = NULL;
                 union in_addr_union a;
-                struct in_addr *m;
+                int r;
 
                 r = extract_first_word(&p, &w, NULL, 0);
                 if (r == -ENOMEM)
@@ -492,27 +485,25 @@ int config_parse_dhcp_server_dns(
                         return 0;
                 }
                 if (r == 0)
-                        break;
+                        return 0;
 
                 r = in_addr_from_string(AF_INET, w, &a);
                 if (r < 0) {
                         log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to parse DNS server address '%s', ignoring assignment: %m", w);
+                                   "Failed to parse %s= address '%s', ignoring: %m", lvalue, w);
                         continue;
                 }
 
-                m = reallocarray(n->dhcp_server_dns, n->n_dhcp_server_dns + 1, sizeof(struct in_addr));
+                struct in_addr *m = reallocarray(*addresses, *n_addresses + 1, sizeof(struct in_addr));
                 if (!m)
                         return log_oom();
 
-                m[n->n_dhcp_server_dns++] = a.in;
-                n->dhcp_server_dns = m;
+                m[(*n_addresses)++] = a.in;
+                *addresses = m;
         }
-
-        return 0;
 }
 
-int config_parse_dhcp_server_ntp(
+int config_parse_dhcp_server_dns(
                 const char *unit,
                 const char *filename,
                 unsigned line,
@@ -525,43 +516,29 @@ int config_parse_dhcp_server_ntp(
                 void *userdata) {
 
         Network *n = data;
-        const char *p = rvalue;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
 
-        for (;;) {
-                _cleanup_free_ char *w = NULL;
-                union in_addr_union a;
-                struct in_addr *m;
-
-                r = extract_first_word(&p, &w, NULL, 0);
-                if (r == -ENOMEM)
-                        return log_oom();
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to extract word, ignoring: %s", rvalue);
-                        return 0;
-                }
-                if (r == 0)
-                        return 0;
+        return config_parse_dhcp_lease_server_list(unit, filename, line,
+                                                   lvalue, rvalue,
+                                                   &n->dhcp_server_dns, &n->n_dhcp_server_dns);
+}
 
-                r = in_addr_from_string(AF_INET, w, &a);
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to parse NTP server address '%s', ignoring: %m", w);
-                        continue;
-                }
+int config_parse_dhcp_server_ntp(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
 
-                m = reallocarray(n->dhcp_server_ntp, n->n_dhcp_server_ntp + 1, sizeof(struct in_addr));
-                if (!m)
-                        return log_oom();
+        Network *n = data;
 
-                m[n->n_dhcp_server_ntp++] = a.in;
-                n->dhcp_server_ntp = m;
-        }
+        return config_parse_dhcp_lease_server_list(unit, filename, line,
+                                                   lvalue, rvalue,
+                                                   &n->dhcp_server_ntp, &n->n_dhcp_server_ntp);
 }
 
 int config_parse_dhcp_server_sip(
@@ -577,45 +554,10 @@ int config_parse_dhcp_server_sip(
                 void *userdata) {
 
         Network *n = data;
-        const char *p = rvalue;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-
-        for (;;) {
-                _cleanup_free_ char *w = NULL;
-                union in_addr_union a;
-                struct in_addr *m;
-
-                r = extract_first_word(&p, &w, NULL, 0);
-                if (r == -ENOMEM)
-                        return log_oom();
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to extract word, ignoring: %s", rvalue);
-                        return 0;
-                }
-                if (r == 0)
-                        return 0;
 
-                r = in_addr_from_string(AF_INET, w, &a);
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to parse SIP server address '%s', ignoring: %m", w);
-                        continue;
-                }
-
-                m = reallocarray(n->dhcp_server_sip, n->n_dhcp_server_sip + 1, sizeof(struct in_addr));
-                if (!m)
-                        return log_oom();
-
-                m[n->n_dhcp_server_sip++] = a.in;
-                n->dhcp_server_sip = m;
-        }
-
-        return 0;
+        return config_parse_dhcp_lease_server_list(unit, filename, line,
+                                                   lvalue, rvalue,
+                                                   &n->dhcp_server_sip, &n->n_dhcp_server_sip);
 }
 
 int config_parse_dhcp_server_pop3_servers(
@@ -631,45 +573,10 @@ int config_parse_dhcp_server_pop3_servers(
                 void *userdata) {
 
         Network *n = data;
-        const char *p = rvalue;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-
-        for (;;) {
-                _cleanup_free_ char *w = NULL;
-                union in_addr_union a;
-                struct in_addr *m;
-
-                r = extract_first_word(&p, &w, NULL, 0);
-                if (r == -ENOMEM)
-                        return log_oom();
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to extract word, ignoring: %s", rvalue);
-                        return 0;
-                }
-                if (r == 0)
-                        return 0;
-
-                r = in_addr_from_string(AF_INET, w, &a);
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to parse POP3 server address '%s', ignoring: %m", w);
-                        continue;
-                }
 
-                m = reallocarray(n->dhcp_server_pop3, n->n_dhcp_server_pop3 + 1, sizeof(struct in_addr));
-                if (!m)
-                        return log_oom();
-
-                m[n->n_dhcp_server_pop3++] = a.in;
-                n->dhcp_server_pop3 = m;
-        }
-
-        return 0;
+        return config_parse_dhcp_lease_server_list(unit, filename, line,
+                                                   lvalue, rvalue,
+                                                   &n->dhcp_server_pop3, &n->n_dhcp_server_pop3);
 }
 
 int config_parse_dhcp_server_smtp_servers(
@@ -685,43 +592,9 @@ int config_parse_dhcp_server_smtp_servers(
                 void *userdata) {
 
         Network *n = data;
-        const char *p = rvalue;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-
-        for (;;) {
-                _cleanup_free_ char *w = NULL;
-                union in_addr_union a;
-                struct in_addr *m;
-
-                r = extract_first_word(&p, &w, NULL, 0);
-                if (r == -ENOMEM)
-                        return log_oom();
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to extract word, ignoring: %s", rvalue);
-                        return 0;
-                }
-                if (r == 0)
-                        return 0;
-
-                r = in_addr_from_string(AF_INET, w, &a);
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r,
-                                   "Failed to parse SMTP server address '%s', ignoring: %m", w);
-                        continue;
-                }
 
-                m = reallocarray(n->dhcp_server_smtp, n->n_dhcp_server_smtp + 1, sizeof(struct in_addr));
-                if (!m)
-                        return log_oom();
-
-                m[n->n_dhcp_server_smtp++] = a.in;
-                n->dhcp_server_smtp = m;
-        }
+        return config_parse_dhcp_lease_server_list(unit, filename, line,
+                                                   lvalue, rvalue,
+                                                   &n->dhcp_server_smtp, &n->n_dhcp_server_smtp);
 
-        return 0;
 }