public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Cc: Andrea Bolognani <abologna@redhat.com>
Subject: [PATCH 2/3] conf: Split add_dns{4,6}() out of get_dns()
Date: Thu, 23 Feb 2023 18:07:59 +0100	[thread overview]
Message-ID: <20230223170800.3888094-3-sbrivio@redhat.com> (raw)
In-Reply-To: <20230223170800.3888094-1-sbrivio@redhat.com>

The logic handling which resolvers we add, and whether to add them,
is getting rather cramped in get_dns(): split it into separate
functions.

No functional changes intended.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 conf.c | 86 ++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 51 insertions(+), 35 deletions(-)

diff --git a/conf.c b/conf.c
index 4dc0660..ed25e35 100644
--- a/conf.c
+++ b/conf.c
@@ -382,6 +382,53 @@ bind_fail:
 	die("Failed to bind any port for '-%c %s', exiting", optname, optarg);
 }
 
+/**
+ * add_dns4() - Possibly add the IPv4 address of a DNS resolver to configuration
+ * @c:		Execution context
+ * @addr:	Address found in /etc/resolv.conf
+ * @conf:	Pointer to reference of current entry in array of IPv4 resolvers
+ */
+static void add_dns4(struct ctx *c, struct in_addr *addr, struct in_addr **conf)
+{
+	/* Guest or container can only access local addresses via redirect */
+	if (IN4_IS_ADDR_LOOPBACK(addr)) {
+		if (!c->no_map_gw) {
+			**conf = c->ip4.gw;
+			(*conf)++;
+		}
+	} else {
+		**conf = *addr;
+		(*conf)++;
+	}
+
+	if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_host))
+		c->ip4.dns_host = *addr;
+}
+
+/**
+ * add_dns6() - Possibly add the IPv6 address of a DNS resolver to configuration
+ * @c:		Execution context
+ * @addr:	Address found in /etc/resolv.conf
+ * @conf:	Pointer to reference of current entry in array of IPv6 resolvers
+ */
+static void add_dns6(struct ctx *c,
+		     struct in6_addr *addr, struct in6_addr **conf)
+{
+	/* Guest or container can only access local addresses via redirect */
+	if (IN6_IS_ADDR_LOOPBACK(addr)) {
+		if (!c->no_map_gw) {
+			memcpy(*conf, &c->ip6.gw, sizeof(**conf));
+			(*conf)++;
+		}
+	} else {
+		memcpy(*conf, addr, sizeof(**conf));
+		(*conf)++;
+	}
+
+	if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_host))
+		c->ip6.dns_host = *addr;
+}
+
 /**
  * get_dns() - Get nameserver addresses from local /etc/resolv.conf
  * @c:		Execution context
@@ -420,44 +467,13 @@ static void get_dns(struct ctx *c)
 
 			if (!dns4_set &&
 			    dns4 - &c->ip4.dns[0] < ARRAY_SIZE(c->ip4.dns) - 1
-			    && inet_pton(AF_INET, p + 1, &dns4_tmp)) {
-				/* Guest or container can only access local
-				 * addresses via local redirect
-				 */
-				if (IN4_IS_ADDR_LOOPBACK(&dns4_tmp)) {
-					if (!c->no_map_gw) {
-						*dns4 = c->ip4.gw;
-						dns4++;
-					}
-				} else {
-					*dns4 = dns4_tmp;
-					dns4++;
-				}
-
-				if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_host))
-					c->ip4.dns_host = dns4_tmp;
-			}
+			    && inet_pton(AF_INET, p + 1, &dns4_tmp))
+				add_dns4(c, &dns4_tmp, &dns4);
 
 			if (!dns6_set &&
 			    dns6 - &c->ip6.dns[0] < ARRAY_SIZE(c->ip6.dns) - 1
-			    && inet_pton(AF_INET6, p + 1, &dns6_tmp)) {
-				/* Guest or container can only access local
-				 * addresses via local redirect
-				 */
-				if (IN6_IS_ADDR_LOOPBACK(&dns6_tmp)) {
-					if (!c->no_map_gw) {
-						memcpy(dns6, &c->ip6.gw,
-						       sizeof(*dns6));
-						dns6++;
-					}
-				} else {
-					memcpy(dns6, &dns6_tmp, sizeof(*dns6));
-					dns6++;
-				}
-
-				if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_host))
-					c->ip6.dns_host = dns6_tmp;
-			}
+			    && inet_pton(AF_INET6, p + 1, &dns6_tmp))
+				add_dns6(c, &dns6_tmp, &dns6);
 		} else if (!dnss_set && strstr(line, "search ") == line &&
 			   s == c->dns_search) {
 			end = strpbrk(line, "\n");
-- 
@@ -382,6 +382,53 @@ bind_fail:
 	die("Failed to bind any port for '-%c %s', exiting", optname, optarg);
 }
 
+/**
+ * add_dns4() - Possibly add the IPv4 address of a DNS resolver to configuration
+ * @c:		Execution context
+ * @addr:	Address found in /etc/resolv.conf
+ * @conf:	Pointer to reference of current entry in array of IPv4 resolvers
+ */
+static void add_dns4(struct ctx *c, struct in_addr *addr, struct in_addr **conf)
+{
+	/* Guest or container can only access local addresses via redirect */
+	if (IN4_IS_ADDR_LOOPBACK(addr)) {
+		if (!c->no_map_gw) {
+			**conf = c->ip4.gw;
+			(*conf)++;
+		}
+	} else {
+		**conf = *addr;
+		(*conf)++;
+	}
+
+	if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_host))
+		c->ip4.dns_host = *addr;
+}
+
+/**
+ * add_dns6() - Possibly add the IPv6 address of a DNS resolver to configuration
+ * @c:		Execution context
+ * @addr:	Address found in /etc/resolv.conf
+ * @conf:	Pointer to reference of current entry in array of IPv6 resolvers
+ */
+static void add_dns6(struct ctx *c,
+		     struct in6_addr *addr, struct in6_addr **conf)
+{
+	/* Guest or container can only access local addresses via redirect */
+	if (IN6_IS_ADDR_LOOPBACK(addr)) {
+		if (!c->no_map_gw) {
+			memcpy(*conf, &c->ip6.gw, sizeof(**conf));
+			(*conf)++;
+		}
+	} else {
+		memcpy(*conf, addr, sizeof(**conf));
+		(*conf)++;
+	}
+
+	if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_host))
+		c->ip6.dns_host = *addr;
+}
+
 /**
  * get_dns() - Get nameserver addresses from local /etc/resolv.conf
  * @c:		Execution context
@@ -420,44 +467,13 @@ static void get_dns(struct ctx *c)
 
 			if (!dns4_set &&
 			    dns4 - &c->ip4.dns[0] < ARRAY_SIZE(c->ip4.dns) - 1
-			    && inet_pton(AF_INET, p + 1, &dns4_tmp)) {
-				/* Guest or container can only access local
-				 * addresses via local redirect
-				 */
-				if (IN4_IS_ADDR_LOOPBACK(&dns4_tmp)) {
-					if (!c->no_map_gw) {
-						*dns4 = c->ip4.gw;
-						dns4++;
-					}
-				} else {
-					*dns4 = dns4_tmp;
-					dns4++;
-				}
-
-				if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_host))
-					c->ip4.dns_host = dns4_tmp;
-			}
+			    && inet_pton(AF_INET, p + 1, &dns4_tmp))
+				add_dns4(c, &dns4_tmp, &dns4);
 
 			if (!dns6_set &&
 			    dns6 - &c->ip6.dns[0] < ARRAY_SIZE(c->ip6.dns) - 1
-			    && inet_pton(AF_INET6, p + 1, &dns6_tmp)) {
-				/* Guest or container can only access local
-				 * addresses via local redirect
-				 */
-				if (IN6_IS_ADDR_LOOPBACK(&dns6_tmp)) {
-					if (!c->no_map_gw) {
-						memcpy(dns6, &c->ip6.gw,
-						       sizeof(*dns6));
-						dns6++;
-					}
-				} else {
-					memcpy(dns6, &dns6_tmp, sizeof(*dns6));
-					dns6++;
-				}
-
-				if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_host))
-					c->ip6.dns_host = dns6_tmp;
-			}
+			    && inet_pton(AF_INET6, p + 1, &dns6_tmp))
+				add_dns6(c, &dns6_tmp, &dns6);
 		} else if (!dnss_set && strstr(line, "search ") == line &&
 			   s == c->dns_search) {
 			end = strpbrk(line, "\n");
-- 
2.39.1


  parent reply	other threads:[~2023-02-23 17:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-23 17:07 [PATCH 0/3] Allow non-127.0.0.1 loopback address as host resolver Stefano Brivio
2023-02-23 17:07 ` [PATCH 1/3] udp: Actually use host resolver to forward DNS queries Stefano Brivio
2023-02-27 12:02   ` David Gibson
2023-02-23 17:07 ` Stefano Brivio [this message]
2023-02-27 12:04   ` [PATCH 2/3] conf: Split add_dns{4,6}() out of get_dns() David Gibson
2023-02-23 17:08 ` [PATCH 3/3] conf, udp: Allow any loopback address to be used as resolver Stefano Brivio
2023-02-27 12:07   ` David Gibson
2023-02-23 17:53 ` [PATCH 0/3] Allow non-127.0.0.1 loopback address as host resolver Andrea Bolognani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230223170800.3888094-3-sbrivio@redhat.com \
    --to=sbrivio@redhat.com \
    --cc=abologna@redhat.com \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).