Blame SOURCES/0002-conf-Split-add_dns-4-6-out-of-get_dns.patch

f07426
From bf19154051b6c920f702c8394cd6821bb00f531a Mon Sep 17 00:00:00 2001
f07426
From: Stefano Brivio <sbrivio@redhat.com>
f07426
Date: Thu, 23 Feb 2023 13:32:30 +0000
f07426
Subject: [PATCH 02/20] conf: Split add_dns{4,6}() out of get_dns()
f07426
f07426
The logic handling which resolvers we add, and whether to add them,
f07426
is getting rather cramped in get_dns(): split it into separate
f07426
functions.
f07426
f07426
No functional changes intended.
f07426
f07426
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
f07426
Tested-by: Andrea Bolognani <abologna@redhat.com>
f07426
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
f07426
(cherry picked from commit 8ca907a3f0a095691cdaf56ad610fd802df88146)
f07426
---
f07426
 conf.c | 86 ++++++++++++++++++++++++++++++++++------------------------
f07426
 1 file changed, 51 insertions(+), 35 deletions(-)
f07426
f07426
diff --git a/conf.c b/conf.c
f07426
index 4dc0660..ed25e35 100644
f07426
--- a/conf.c
f07426
+++ b/conf.c
f07426
@@ -382,6 +382,53 @@ bind_fail:
f07426
 	die("Failed to bind any port for '-%c %s', exiting", optname, optarg);
f07426
 }
f07426
 
f07426
+/**
f07426
+ * add_dns4() - Possibly add the IPv4 address of a DNS resolver to configuration
f07426
+ * @c:		Execution context
f07426
+ * @addr:	Address found in /etc/resolv.conf
f07426
+ * @conf:	Pointer to reference of current entry in array of IPv4 resolvers
f07426
+ */
f07426
+static void add_dns4(struct ctx *c, struct in_addr *addr, struct in_addr **conf)
f07426
+{
f07426
+	/* Guest or container can only access local addresses via redirect */
f07426
+	if (IN4_IS_ADDR_LOOPBACK(addr)) {
f07426
+		if (!c->no_map_gw) {
f07426
+			**conf = c->ip4.gw;
f07426
+			(*conf)++;
f07426
+		}
f07426
+	} else {
f07426
+		**conf = *addr;
f07426
+		(*conf)++;
f07426
+	}
f07426
+
f07426
+	if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_host))
f07426
+		c->ip4.dns_host = *addr;
f07426
+}
f07426
+
f07426
+/**
f07426
+ * add_dns6() - Possibly add the IPv6 address of a DNS resolver to configuration
f07426
+ * @c:		Execution context
f07426
+ * @addr:	Address found in /etc/resolv.conf
f07426
+ * @conf:	Pointer to reference of current entry in array of IPv6 resolvers
f07426
+ */
f07426
+static void add_dns6(struct ctx *c,
f07426
+		     struct in6_addr *addr, struct in6_addr **conf)
f07426
+{
f07426
+	/* Guest or container can only access local addresses via redirect */
f07426
+	if (IN6_IS_ADDR_LOOPBACK(addr)) {
f07426
+		if (!c->no_map_gw) {
f07426
+			memcpy(*conf, &c->ip6.gw, sizeof(**conf));
f07426
+			(*conf)++;
f07426
+		}
f07426
+	} else {
f07426
+		memcpy(*conf, addr, sizeof(**conf));
f07426
+		(*conf)++;
f07426
+	}
f07426
+
f07426
+	if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_host))
f07426
+		c->ip6.dns_host = *addr;
f07426
+}
f07426
+
f07426
 /**
f07426
  * get_dns() - Get nameserver addresses from local /etc/resolv.conf
f07426
  * @c:		Execution context
f07426
@@ -420,44 +467,13 @@ static void get_dns(struct ctx *c)
f07426
 
f07426
 			if (!dns4_set &&
f07426
 			    dns4 - &c->ip4.dns[0] < ARRAY_SIZE(c->ip4.dns) - 1
f07426
-			    && inet_pton(AF_INET, p + 1, &dns4_tmp)) {
f07426
-				/* Guest or container can only access local
f07426
-				 * addresses via local redirect
f07426
-				 */
f07426
-				if (IN4_IS_ADDR_LOOPBACK(&dns4_tmp)) {
f07426
-					if (!c->no_map_gw) {
f07426
-						*dns4 = c->ip4.gw;
f07426
-						dns4++;
f07426
-					}
f07426
-				} else {
f07426
-					*dns4 = dns4_tmp;
f07426
-					dns4++;
f07426
-				}
f07426
-
f07426
-				if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_host))
f07426
-					c->ip4.dns_host = dns4_tmp;
f07426
-			}
f07426
+			    && inet_pton(AF_INET, p + 1, &dns4_tmp))
f07426
+				add_dns4(c, &dns4_tmp, &dns4);
f07426
 
f07426
 			if (!dns6_set &&
f07426
 			    dns6 - &c->ip6.dns[0] < ARRAY_SIZE(c->ip6.dns) - 1
f07426
-			    && inet_pton(AF_INET6, p + 1, &dns6_tmp)) {
f07426
-				/* Guest or container can only access local
f07426
-				 * addresses via local redirect
f07426
-				 */
f07426
-				if (IN6_IS_ADDR_LOOPBACK(&dns6_tmp)) {
f07426
-					if (!c->no_map_gw) {
f07426
-						memcpy(dns6, &c->ip6.gw,
f07426
-						       sizeof(*dns6));
f07426
-						dns6++;
f07426
-					}
f07426
-				} else {
f07426
-					memcpy(dns6, &dns6_tmp, sizeof(*dns6));
f07426
-					dns6++;
f07426
-				}
f07426
-
f07426
-				if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_host))
f07426
-					c->ip6.dns_host = dns6_tmp;
f07426
-			}
f07426
+			    && inet_pton(AF_INET6, p + 1, &dns6_tmp))
f07426
+				add_dns6(c, &dns6_tmp, &dns6);
f07426
 		} else if (!dnss_set && strstr(line, "search ") == line &&
f07426
 			   s == c->dns_search) {
f07426
 			end = strpbrk(line, "\n");
f07426
-- 
f07426
2.39.2
f07426