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>
Subject: [PATCH 05/12] conf: Clean up conf_ip4_prefix()
Date: Fri, 26 Jun 2026 17:09:56 +1000	[thread overview]
Message-ID: <20260626071003.3472194-6-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260626071003.3472194-1-david@gibson.dropbear.id.au>

Restructure conf_ip4_prefix() so that success is the early exit path and
failure is the "default" path.  Also move the error handling (die()) into
it from the caller.

This saves a handful of lines for now, and will make integration with
upcoming parsing changes nicer.

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

diff --git a/conf.c b/conf.c
index 32163f56..ea299e18 100644
--- a/conf.c
+++ b/conf.c
@@ -350,9 +350,9 @@ static void conf_pasta_ns(int *netns_only, char *userns, char *netns,
 /** conf_ip4_prefix() - Parse an IPv4 prefix length or netmask
  * @arg:	Netmask in dotted decimal or prefix length
  *
- * Return: validated prefix length on success, -1 on failure
+ * Return: validated prefix length; dies on bad argument
  */
-static int conf_ip4_prefix(const char *arg)
+static uint8_t conf_ip4_prefix(const char *arg)
 {
 	struct in_addr mask;
 	unsigned long len;
@@ -360,16 +360,16 @@ static int conf_ip4_prefix(const char *arg)
 	if (inet_pton(AF_INET, arg, &mask)) {
 		in_addr_t hmask = ntohl(mask.s_addr);
 		len = __builtin_popcount(hmask);
-		if ((hmask << len) != 0)
-			return -1;
+		if ((hmask << len) == 0)
+			return len;
 	} else {
 		errno = 0;
 		len = strtoul(arg, NULL, 0);
-		if (len > 32 || errno)
-			return -1;
+		if (!errno && len <= 32)
+			return len;
 	}
 
-	return len;
+	die("Invalid prefix length: %s", arg);
 }
 
 /**
@@ -1606,20 +1606,13 @@ void conf(struct ctx *c, int argc, char **argv)
 			}
 			break;
 		}
-		case 'n': {
-			int plen;
-
+		case 'n':
 			if (addr_has_prefix_len)
 				die("Redundant prefix length specification");
 
-			plen = conf_ip4_prefix(optarg);
-			if (plen < 0)
-				die("Invalid prefix length: %s", optarg);
-
-			prefix_len_from_opt = plen + 96;
-			c->ip4.prefix_len = plen;
+			c->ip4.prefix_len = conf_ip4_prefix(optarg);
+			prefix_len_from_opt = c->ip4.prefix_len + 96;
 			break;
-		}
 		case 'M':
 			parse_mac(c->our_tap_mac, optarg);
 			break;
-- 
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 ` David Gibson [this message]
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 ` [PATCH 09/12] conf: Move address configuration into helper function David Gibson
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-6-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --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).