public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>,
	Jon Maloy <jmaloy@redhat.com>
Subject: [PATCH 09/12] conf: Move address configuration into helper function
Date: Fri, 26 Jun 2026 17:10:00 +1000	[thread overview]
Message-ID: <20260626071003.3472194-10-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260626071003.3472194-1-david@gibson.dropbear.id.au>

The handling of the -a option is getting complex enough that it's pretty
bulky inside it's switch label.  Move it into a new conf_addr() function.
We also rename the bulky addr_has_prefix_len and prefix_len_from_opt
variables to the terser 'opt_a_is_prefix' and 'opt_n', since they are
specifically about those command line options.

Cc: Jon Maloy <jmaloy@redhat.com>

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 conf.c | 96 ++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 53 insertions(+), 43 deletions(-)

diff --git a/conf.c b/conf.c
index 2d1895d4..3614776c 100644
--- a/conf.c
+++ b/conf.c
@@ -1168,6 +1168,53 @@ int conf_tap_fd(const char *arg)
 	return val;
 }
 
+/**
+ * conf_addr() - Configure guest address with -a option
+ * @c:		Execution context
+ * @arg:	-a command line argument
+ * @opt_n:	Value from -n option, if any
+ */
+static bool conf_addr(struct ctx *c, char *arg, uint8_t opt_n)
+{
+	union inany_addr addr;
+	const char *p = arg;
+	uint8_t prefix_len;
+	bool is_prefix;
+
+	is_prefix = inany_prefix_pton(arg, &addr, &prefix_len);
+
+	if (is_prefix && opt_n)
+		die("Redundant prefix length specification");
+
+	if (!is_prefix &&
+	    !(parse_inany(&p, &addr) && parse_eoi(p)))
+		die("Invalid address: %s", arg);
+
+	if (opt_n && inany_v4(&addr))
+		prefix_len = opt_n;
+	else if (!is_prefix)
+		prefix_len = inany_default_prefix_len(&addr);
+
+	if (inany_is_unspecified(&addr) || inany_is_multicast(&addr) ||
+	    inany_is_loopback(&addr) || IN6_IS_ADDR_V4COMPAT(&addr.a6))
+		die("Invalid address: %s", arg);
+
+	if (inany_v4(&addr)) {
+		c->ip4.addr = *inany_v4(&addr);
+		c->ip4.prefix_len = prefix_len - 96;
+		c->ip4.addr_fixed = true;
+		if (c->mode == MODE_PASTA)
+			c->ip4.no_copy_addrs = true;
+	} else {
+		c->ip6.addr = addr.a6;
+		c->ip6.addr_fixed = true;
+		if (c->mode == MODE_PASTA)
+			c->ip6.no_copy_addrs = true;
+	}
+
+	return is_prefix;
+}
+
 /**
  * conf() - Process command-line arguments and set configuration
  * @c:		Execution context
@@ -1262,12 +1309,12 @@ void conf(struct ctx *c, int argc, char **argv)
 	unsigned dns4_idx = 0, dns6_idx = 0;
 	unsigned long max_mtu = IP_MAX_MTU;
 	struct fqdn *dnss = c->dns_search;
-	bool addr_has_prefix_len = false;
-	uint8_t prefix_len_from_opt = 0;
 	unsigned int ifi4 = 0, ifi6 = 0;
+	bool opt_a_is_prefix = false;
 	const char *logfile = NULL;
 	char *runas = NULL;
 	size_t logsize = 0;
+	uint8_t opt_n = 0;
 	int name, ret;
 	uid_t uid;
 	gid_t gid;
@@ -1567,52 +1614,15 @@ void conf(struct ctx *c, int argc, char **argv)
 			c->mtu = mtu;
 			break;
 		}
-		case 'a': {
-			union inany_addr addr;
-			uint8_t prefix_len;
-
-			addr_has_prefix_len = inany_prefix_pton(optarg, &addr,
-								&prefix_len);
-
-			if (addr_has_prefix_len && prefix_len_from_opt)
-				die("Redundant prefix length specification");
-
-			p = optarg;
-			if (!addr_has_prefix_len &&
-			    !(parse_inany(&p, &addr) && parse_eoi(p)))
-				die("Invalid address: %s", optarg);
-
-			if (prefix_len_from_opt && inany_v4(&addr))
-				prefix_len = prefix_len_from_opt;
-			else if (!addr_has_prefix_len)
-				prefix_len = inany_default_prefix_len(&addr);
-
-			if (inany_is_unspecified(&addr) ||
-			    inany_is_multicast(&addr) ||
-			    inany_is_loopback(&addr) ||
-			    IN6_IS_ADDR_V4COMPAT(&addr.a6))
-				die("Invalid address: %s", optarg);
-
-			if (inany_v4(&addr)) {
-				c->ip4.addr = *inany_v4(&addr);
-				c->ip4.prefix_len = prefix_len - 96;
-				c->ip4.addr_fixed = true;
-				if (c->mode == MODE_PASTA)
-					c->ip4.no_copy_addrs = true;
-			} else {
-				c->ip6.addr = addr.a6;
-				c->ip6.addr_fixed = true;
-				if (c->mode == MODE_PASTA)
-					c->ip6.no_copy_addrs = true;
-			}
+		case 'a':
+			opt_a_is_prefix = conf_addr(c, optarg, opt_n);
 			break;
-		}
 		case 'n':
-			if (addr_has_prefix_len)
+			if (opt_a_is_prefix)
 				die("Redundant prefix length specification");
 
 			c->ip4.prefix_len = conf_ip4_prefix(optarg);
-			prefix_len_from_opt = c->ip4.prefix_len + 96;
+			opt_n = c->ip4.prefix_len + 96;
 			break;
 		case 'M':
 			parse_mac(c->our_tap_mac, optarg);
-- 
2.54.0


  parent reply	other threads:[~2026-06-26  7:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26  7:09 [PATCH 00/12] Rework option parsing in preparation for destination remapping David Gibson
2026-06-26  7:09 ` [PATCH 01/12] Makefile: Add missing PESTO_HEADERS variable David Gibson
2026-06-26  7:09 ` [PATCH 02/12] conf: Use parameter instead of global in conf_nat() David Gibson
2026-06-26  7:09 ` [PATCH 03/12] parse: Start splitting out parsing helpers David Gibson
2026-06-26  7:09 ` [PATCH 04/12] conf: Remove duplicate parsing of -F option David Gibson
2026-06-26  7:09 ` [PATCH 05/12] conf: Clean up conf_ip4_prefix() David Gibson
2026-06-26  7:09 ` [PATCH 06/12] parse: Add helper to parse unsigned integer values David Gibson
2026-06-26  7:09 ` [PATCH 07/12] parse: Move parse_port_range() to new parsing framework David Gibson
2026-06-26  7:09 ` [PATCH 08/12] parse: Add helpers for parsing IP addresses David Gibson
2026-06-26  7:10 ` David Gibson [this message]
2026-06-26  7:10 ` [PATCH 10/12] conf: Use new parsing tools to handle -a option David Gibson
2026-06-26  7:10 ` [PATCH 11/12] fwd_rule: Allow "all" port specs to be combined with other options David Gibson
2026-06-26  7:10 ` [PATCH 12/12] fwd_rule: Rewrite forward rule parsing using parse.c helpers David Gibson

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=20260626071003.3472194-10-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=jmaloy@redhat.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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).