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=YMZRZ8c0; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id ED5895A061C for ; Tue, 07 Apr 2026 05:16:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1775531792; bh=CoC4Qgc3n+DG5i51TBlddJsjkZW9sBnyTDnkEFWBMDI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YMZRZ8c0HEtRKK0Njrx+AAN45JLXto+zJs12lF8UL+EWR0+EPKO2bvfAM3PJZhnSO svkK0uJOeXe4HJ/yTLxmRTcsVyZFTkCkCuf4S16bVgVPoMyjD4IZ8+xWaLXBFuZ5IT jMoUjppoOjjIv5akKYmTJtnRLzAPJ/X8XMUsIB44CrBS+jsN4Y5lMMTnAEHeLibT+p Ef8WfbRwPIfJzjmmZWfjuGUdjq+5mbsuRtjtan19HgImXhZdGUSpoCqNQK9pSIyhcI 1LkVXu19BGVrW4WaRTgcbSXHyhTLEZ4JP4WEdv5KKSsH9z39rAJh9HZlcOz83Bkqoi lfib83HY/lL+A== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fqWZ85cT7z4wL9; Tue, 07 Apr 2026 13:16:32 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 08/18] conf: Pass protocol explicitly to conf_ports_range_except() Date: Tue, 7 Apr 2026 13:16:20 +1000 Message-ID: <20260407031630.2457081-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260407031630.2457081-1-david@gibson.dropbear.id.au> References: <20260407031630.2457081-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: DL5KAVLI3EMPPRVGNGTFZTVPYH7S7G7Y X-Message-ID-Hash: DL5KAVLI3EMPPRVGNGTFZTVPYH7S7G7Y 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 a3452bc9..23125d2c 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