aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-08-21 14:20:04 +1000
committerStefano Brivio <sbrivio@redhat.com>2024-08-21 12:00:04 +0200
commit7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7 (patch)
tree6488da828f2c5096ce2ca6cdc7bf711742c632c5
parent1d10760c9f9bdede3b9a8a13a739fd5915aa2b19 (diff)
downloadpasst-7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7.tar
passt-7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7.tar.gz
passt-7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7.tar.bz2
passt-7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7.tar.lz
passt-7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7.tar.xz
passt-7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7.tar.zst
passt-7c083ee41c9aaa92d1215c6cef87bcd4eb6e20a7.zip
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 <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--conf.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/conf.c b/conf.c
index dfe417b..4bb06b7 100644
--- a/conf.c
+++ b/conf.c
@@ -422,6 +422,29 @@ static unsigned add_dns6(struct ctx *c, struct in6_addr *addr, unsigned idx)
}
/**
+ * 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");