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=202602 header.b=KTZirnOS; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 814265A0627 for ; Fri, 10 Apr 2026 03:03:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1775782993; bh=4d/AY3R3f+XBhzk9C8mYoIHHAZr5rXFQmhhVlbEPP9k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KTZirnOSxdRCG50QDKm7IQ2A5Pb6yTlOkuI4Otxt90BEObW6ELVEulGNHmYWs2iqJ Q8yZmUkfR4mYcX4ckJvHMEDZhG5KdAgQBRjlMdF9AKVFOcOsQzpZCGqpIhWzG8vo/a /tmwic4jkdOkUlFrVEGVttS/WRhkRlBYXaQx2PFCKKHiYezkn3Q9UbGnVoWSQT1Va7 A/IIr57Uiu7tbjO0l/XIM/UQ71OoVeUOFZt4FyYLYSNer5YuQSQj82OZMWNIwECWEg mL/IQLMTnDIXRP1yZMcdfCxSUpNfGoN/mT3FhzS13OD61XU48kuIwrRRvbvoXNrxOY NfONrhcbjPVgw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fsJSx07hYz4wTL; Fri, 10 Apr 2026 11:03:13 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 08/23] conf: Pass protocol explicitly to conf_ports_range_except() Date: Fri, 10 Apr 2026 11:02:54 +1000 Message-ID: <20260410010309.736855-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260410010309.736855-1-david@gibson.dropbear.id.au> References: <20260410010309.736855-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: S4ZTSEHVAFGF6QWQNYLBJT6QMDC2JBY5 X-Message-ID-Hash: S4ZTSEHVAFGF6QWQNYLBJT6QMDC2JBY5 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: Currently conf_ports_range_except() deduces the protocol to use from the option name. This is correct, but a DRY violation, since we check the option name at several points in the callchain. Instead pass the protocol explicitly to conf_ports_range_except() and conf_ports_spec(), computing it from the option name in conf_ports(). This is redundant for now, but means the optname and optarg parameters to the lower-level functions are used only for debugging output and will be removable in future. Signed-off-by: David Gibson --- conf.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/conf.c b/conf.c index 318bb915..a0cf9454 100644 --- a/conf.c +++ b/conf.c @@ -134,6 +134,7 @@ static int parse_port_range(const char *s, const char **endptr, * @optname: Short option name, t, T, u, or U * @optarg: Option argument (port specification) * @fwd: Forwarding table to be updated + * @proto: Protocol to forward * @addr: Listening address * @ifname: Listening interface * @first: First port to forward @@ -144,7 +145,7 @@ static int parse_port_range(const char *s, const char **endptr, */ static void conf_ports_range_except(const struct ctx *c, char optname, const char *optarg, struct fwd_table *fwd, - const union inany_addr *addr, + uint8_t proto, const union inany_addr *addr, const char *ifname, uint16_t first, uint16_t last, const uint8_t *exclude, uint16_t to, @@ -152,17 +153,9 @@ static void conf_ports_range_except(const struct ctx *c, char optname, { unsigned delta = to - first; unsigned base, i; - uint8_t proto; assert(first != 0); - if (optname == 't' || optname == 'T') - proto = IPPROTO_TCP; - else if (optname == 'u' || optname == 'U') - proto = IPPROTO_UDP; - else - assert(0); - for (base = first; base <= last; base++) { if (exclude && bitmap_isset(exclude, base)) continue; @@ -221,13 +214,14 @@ enum fwd_mode { * @optname: Short option name, t, T, u, or U * @optarg: Option argument (port specification) * @fwd: Forwarding table to be updated + * @proto: Protocol to forward * @addr: Listening address for forwarding * @ifname: Interface name for listening * @spec: Port range(s) specifier */ static void conf_ports_spec(const struct ctx *c, char optname, const char *optarg, - struct fwd_table *fwd, + struct fwd_table *fwd, uint8_t proto, const union inany_addr *addr, const char *ifname, const char *spec) { @@ -262,7 +256,7 @@ static void conf_ports_spec(const struct ctx *c, fwd_port_map_ephemeral(exclude); conf_ports_range_except(c, optname, optarg, fwd, - addr, ifname, + proto, addr, ifname, 1, NUM_PORTS - 1, exclude, 1, FWD_WEAK); return; @@ -299,7 +293,7 @@ static void conf_ports_spec(const struct ctx *c, } conf_ports_range_except(c, optname, optarg, fwd, - addr, ifname, + proto, addr, ifname, orig_range.first, orig_range.last, exclude, mapped_range.first, 0); @@ -323,6 +317,14 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, { union inany_addr addr_buf = inany_any6, *addr = &addr_buf; char buf[BUFSIZ], *spec, *ifname = NULL, *p; + uint8_t proto; + + if (optname == 't' || optname == 'T') + proto = IPPROTO_TCP; + else if (optname == 'u' || optname == 'U') + proto = IPPROTO_UDP; + else + assert(0); if (!strcmp(optarg, "none")) { if (*mode) @@ -332,9 +334,9 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, return; } - if ((optname == 't' || optname == 'T') && c->no_tcp) + if (proto == IPPROTO_TCP && c->no_tcp) die("TCP port forwarding requested but TCP is disabled"); - if ((optname == 'u' || optname == 'U') && c->no_udp) + if (proto == IPPROTO_UDP && c->no_udp) die("UDP port forwarding requested but UDP is disabled"); if (!strcmp(optarg, "auto")) { @@ -346,7 +348,8 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, *mode = FWD_MODE_AUTO; - conf_ports_range_except(c, optname, optarg, fwd, NULL, NULL, + conf_ports_range_except(c, optname, optarg, fwd, + proto, NULL, NULL, 1, NUM_PORTS - 1, NULL, 1, FWD_SCAN); return; @@ -364,7 +367,7 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, fwd_port_map_ephemeral(exclude); conf_ports_range_except(c, optname, optarg, fwd, - NULL, NULL, + proto, NULL, NULL, 1, NUM_PORTS - 1, exclude, 1, FWD_WEAK); return; @@ -438,7 +441,7 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, if ((optname == 'T' || optname == 'U') && !ifname) ifname = "lo"; - conf_ports_spec(c, optname, optarg, fwd, addr, ifname, spec); + conf_ports_spec(c, optname, optarg, fwd, proto, addr, ifname, spec); return; mode_conflict: -- 2.53.0