From 7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 21 Aug 2024 14:20:04 +1000 Subject: conf: Move adding of a nameserver from resolv.conf into subfunction get_dns() is already quite deeply nested, and future changes I have in mind will add more complexity. Prepare for this by splitting out the adding of a single nameserver to the configuration into its own function. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- conf.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/conf.c b/conf.c index dfe417b..4bb06b7 100644 --- a/conf.c +++ b/conf.c @@ -421,6 +421,29 @@ static unsigned add_dns6(struct ctx *c, struct in6_addr *addr, unsigned idx) return added; } +/** + * add_dns_resolv() - Possibly add ns from host resolv.conf to configuration + * @c: Execution context + * @nameserver: Nameserver address string from /etc/resolv.conf + * @idx4: Pointer to index of current entry in array of IPv4 resolvers + * @idx6: Pointer to index of current entry in array of IPv6 resolvers + * + * @idx4 or @idx6 may be NULL, in which case resolvers of the corresponding type + * are ignored. + */ +static void add_dns_resolv(struct ctx *c, const char *nameserver, + unsigned *idx4, unsigned *idx6) +{ + struct in6_addr ns6; + struct in_addr ns4; + + if (idx4 && inet_pton(AF_INET, nameserver, &ns4)) + *idx4 += add_dns4(c, &ns4, *idx4); + + if (idx6 && inet_pton(AF_INET6, nameserver, &ns6)) + *idx6 += add_dns6(c, &ns6, *idx6); +} + /** * get_dns() - Get nameserver addresses from local /etc/resolv.conf * @c: Execution context @@ -431,8 +454,6 @@ static void get_dns(struct ctx *c) unsigned dns4_idx = 0, dns6_idx = 0; struct fqdn *s = c->dns_search; struct lineread resolvconf; - struct in6_addr dns6_tmp; - struct in_addr dns4_tmp; ssize_t line_len; char *line, *end; const char *p; @@ -459,11 +480,9 @@ static void get_dns(struct ctx *c) if (end) *end = 0; - if (!dns4_set && inet_pton(AF_INET, p + 1, &dns4_tmp)) - dns4_idx += add_dns4(c, &dns4_tmp, dns4_idx); - - if (!dns6_set && inet_pton(AF_INET6, p + 1, &dns6_tmp)) - dns6_idx += add_dns6(c, &dns6_tmp, dns6_idx); + add_dns_resolv(c, p + 1, + dns4_set ? NULL : &dns4_idx, + dns6_set ? NULL : &dns6_idx); } else if (!dnss_set && strstr(line, "search ") == line && s == c->dns_search) { end = strpbrk(line, "\n"); -- cgit v1.2.3