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=E/e79gz9; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 45E0E5A0620 for ; Sat, 11 Oct 2025 06:49:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202510; t=1760158165; bh=6m9bZTYgD7pwX6R79ChyZXCiC4F7JhULjKOTVTSIy7E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E/e79gz9N6Ol1DYhk4v6+AUd17tshRv1sakAmDJ3yS925aIUtdb0XnxYePag+MTnj TUFH+KnpKKNf8E94Ex1NhxGh2JwAUN0tDxoTbd6mNUfP4paun/vOvahVYtkeESOQ/S dm+hGNgvTvLKdbKZxtV/zeA/VFU9p84zBxzh8OGI4ONSz4hpjGdQzfnS0puYTDhGlj hX35NwzJeIDr//gH1no0UzVfVnw4SzJvIOWvPORFhereoYQlV1bS0DbU5xAr+kCNul 4y/0+RhFv6mOCY7UGr0AZSoi9jqUIMip1pCtkAifTqRkvNWVeecx/QALmJo7GXutjP DkGP+Hc4AfUWg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4ckB3T5VBdz4wD5; Sat, 11 Oct 2025 15:49:25 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 5/8] fwd: Share port scanning logic between init and timer cases Date: Sat, 11 Oct 2025 15:48:24 +1100 Message-ID: <20251011044827.862757-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251011044827.862757-1-david@gibson.dropbear.id.au> References: <20251011044827.862757-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: Y6FVL6WWSWTWTTSH2QUBGMB6UTRCWH3L X-Message-ID-Hash: Y6FVL6WWSWTWTTSH2QUBGMB6UTRCWH3L 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 c7b768d5..6f89acf5 100644 --- a/fwd.c +++ b/fwd.c @@ -191,6 +191,26 @@ static void fwd_scan_ports_udp(struct fwd_ports *fwd, bitmap_andc(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 @@ -207,25 +227,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 */ @@ -246,18 +261,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