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=202510 header.b=zM/PLLS3; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id E60B25A0652 for ; Fri, 31 Oct 2025 05:19:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202510; t=1761884372; bh=aLjFFfzAj0tXhmuWmdwVT9pz+WEzgfJjDva7KhieLpQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zM/PLLS3qezO+GylMKD6RkQzNxstQORoDIy3MQxlE1rPepniP6z96UI9oe+T2tyb3 B/SIT1IwD1L6KVx8YHYMlk83VAf3oNt/+WIsSgHO6vlfhhNmln5LUudw5swXyFL9vC r3MTbo856/3dcvFPxoLVUEhWtY31bVPvGMHKd9uMja2OhpZxpQ+TT2ebOFEYGeAJ2Y 64LKEk2Cz0QjQB0gy1tpzV53NBDROQc6HYJoo5kxKwAFdGpmFtGG+qZ/YOL2VjluCJ ESg5+KwM1Kj3StehE5Oku6+kALB7HYcDlL7EZmBbS1GXFskO+mYemmcwyBPuH5iETe MHR8jXRjI12SA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4cySRm2RBYz4xH3; Fri, 31 Oct 2025 15:19:32 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 5/8] fwd: Share port scanning logic between init and timer cases Date: Fri, 31 Oct 2025 15:19:27 +1100 Message-ID: <20251031041930.1272259-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251031041930.1272259-1-david@gibson.dropbear.id.au> References: <20251031041930.1272259-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 4C5VVBF3XJP2WR2OCGNRX6DMA77WDDLB X-Message-ID-Hash: 4C5VVBF3XJP2WR2OCGNRX6DMA77WDDLB 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: When using -t auto and the like, we scan for listening ports once at startup, then repeatedly on a timer. With previous rearrangements the logic for each of these cases is very nearly repeated. Factor it out into a fwd_scan_ports() function. Signed-off-by: David Gibson --- fwd.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/fwd.c b/fwd.c index 279b7ddd..40453086 100644 --- a/fwd.c +++ b/fwd.c @@ -402,6 +402,26 @@ static void fwd_scan_ports_udp(struct fwd_ports *fwd, bitmap_and_not(fwd->map, PORT_BITMAP_SIZE, fwd->map, tcp_rev->map); } +/** + * fwd_scan_ports() - Scan automatic port forwarding information + * @c: Execution context + */ +static void fwd_scan_ports(struct ctx *c) +{ + if (c->tcp.fwd_out.mode == FWD_AUTO) + fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in); + if (c->tcp.fwd_in.mode == FWD_AUTO) + fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out); + if (c->udp.fwd_out.mode == FWD_AUTO) { + fwd_scan_ports_udp(&c->udp.fwd_out, &c->udp.fwd_in, + &c->tcp.fwd_out, &c->tcp.fwd_in); + } + if (c->udp.fwd_in.mode == FWD_AUTO) { + fwd_scan_ports_udp(&c->udp.fwd_in, &c->udp.fwd_out, + &c->tcp.fwd_in, &c->tcp.fwd_out); + } +} + /** * fwd_scan_ports_init() - Initial setup for automatic port forwarding * @c: Execution context @@ -418,25 +438,20 @@ void fwd_scan_ports_init(struct ctx *c) if (c->tcp.fwd_in.mode == FWD_AUTO) { c->tcp.fwd_in.scan4 = open_in_ns(c, "/proc/net/tcp", flags); c->tcp.fwd_in.scan6 = open_in_ns(c, "/proc/net/tcp6", flags); - fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out); } if (c->udp.fwd_in.mode == FWD_AUTO) { c->udp.fwd_in.scan4 = open_in_ns(c, "/proc/net/udp", flags); c->udp.fwd_in.scan6 = open_in_ns(c, "/proc/net/udp6", flags); - fwd_scan_ports_udp(&c->udp.fwd_in, &c->udp.fwd_out, - &c->tcp.fwd_in, &c->tcp.fwd_out); } if (c->tcp.fwd_out.mode == FWD_AUTO) { c->tcp.fwd_out.scan4 = open("/proc/net/tcp", flags); c->tcp.fwd_out.scan6 = open("/proc/net/tcp6", flags); - fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in); } if (c->udp.fwd_out.mode == FWD_AUTO) { c->udp.fwd_out.scan4 = open("/proc/net/udp", flags); c->udp.fwd_out.scan6 = open("/proc/net/udp6", flags); - fwd_scan_ports_udp(&c->udp.fwd_out, &c->udp.fwd_in, - &c->tcp.fwd_out, &c->tcp.fwd_in); } + fwd_scan_ports(c); } /* Last time we scanned for open ports */ @@ -457,18 +472,7 @@ void fwd_scan_ports_timer(struct ctx *c, const struct timespec *now) scan_ports_run = *now; - if (c->tcp.fwd_out.mode == FWD_AUTO) - fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in); - if (c->tcp.fwd_in.mode == FWD_AUTO) - fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out); - if (c->udp.fwd_out.mode == FWD_AUTO) { - fwd_scan_ports_udp(&c->udp.fwd_out, &c->udp.fwd_in, - &c->tcp.fwd_out, &c->tcp.fwd_in); - } - if (c->udp.fwd_in.mode == FWD_AUTO) { - fwd_scan_ports_udp(&c->udp.fwd_in, &c->udp.fwd_out, - &c->tcp.fwd_in, &c->tcp.fwd_out); - } + fwd_scan_ports(c); if (!c->no_tcp) tcp_port_rebind_all(c); -- 2.51.0