From: Zbigniew Jędrzejewski-Szmek Date: Thu, 2 Apr 2020 09:59:47 +0000 (+0200) Subject: network: add helper to extract server lists X-Git-Tag: v246-rc1~551^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c1997a5bf7db4ee862e3c07653e5ac9295484ba3;p=thirdparty%2Fsystemd.git network: add helper to extract server lists This code is repeable enough to define a common implementation. --- diff --git a/src/network/networkd-dhcp-server.c b/src/network/networkd-dhcp-server.c index bbaa10f5ab2..f27f7247f4e 100644 --- a/src/network/networkd-dhcp-server.c +++ b/src/network/networkd-dhcp-server.c @@ -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; }