From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202606 header.b=lZ3IR9Gi; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 5B3625A0619 for ; Fri, 26 Jun 2026 09:10:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202606; t=1782457808; bh=oGkAW7HPQlD3SDC5hG1qEEVQJZpICdLwO0JHpNSL73M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lZ3IR9Gilbyp2tsHxnMZ9cpkz7uvmrKc/kxK9rXQNiHdaVUECc1jG4y33SG1MTdQD x/ehHuZKvclyBT4UGFAVSF1xFh4HPxRjrRgJvwuhYSl/lU0VU1mmdfUahwfHckf5q7 qwO961suk8LhmCWJbowFLTf4CNPkJwwSzJxBmCYv7vN3md7NfuzL51qHPtAT8jsnXz z1bRvr1h/BBEfTVxiNonWrtXPRkM/vBacgorVJoqZ1nWtVHtYx7M50JwU5CTfQ8NAU MsFPcOKqamOTgdLM434NGkpFsJw9a9Udxw1lr4AODhNg51ZjqEMUSVPOl8Naswjy+V lELmwTLsldXGQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gmmym4bJQz4whf; Fri, 26 Jun 2026 17:10:08 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 05/12] conf: Clean up conf_ip4_prefix() Date: Fri, 26 Jun 2026 17:09:56 +1000 Message-ID: <20260626071003.3472194-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260626071003.3472194-1-david@gibson.dropbear.id.au> References: <20260626071003.3472194-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: IM3HCURZCGIPFPV5FA2RK7ZDCGPNMYJJ X-Message-ID-Hash: IM3HCURZCGIPFPV5FA2RK7ZDCGPNMYJJ X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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