aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2025-10-31 15:19:27 +1100
committerStefano Brivio <sbrivio@redhat.com>2025-11-01 00:23:02 +0100
commit06c3dcc2a6e2a814b52dc2549ef3db8c0771b454 (patch)
tree0305bac059ea93d748192c016a620aa2759fb408
parent1754f61e9ac81c051b5c165059a549a3e9156dfc (diff)
downloadpasst-06c3dcc2a6e2a814b52dc2549ef3db8c0771b454.tar
passt-06c3dcc2a6e2a814b52dc2549ef3db8c0771b454.tar.gz
passt-06c3dcc2a6e2a814b52dc2549ef3db8c0771b454.tar.bz2
passt-06c3dcc2a6e2a814b52dc2549ef3db8c0771b454.tar.lz
passt-06c3dcc2a6e2a814b52dc2549ef3db8c0771b454.tar.xz
passt-06c3dcc2a6e2a814b52dc2549ef3db8c0771b454.tar.zst
passt-06c3dcc2a6e2a814b52dc2549ef3db8c0771b454.zip
fwd: Share port scanning logic between init and timer cases
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 <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--fwd.c40
1 files changed, 22 insertions, 18 deletions
diff --git a/fwd.c b/fwd.c
index 279b7dd..4045308 100644
--- a/fwd.c
+++ b/fwd.c
@@ -403,6 +403,26 @@ static void fwd_scan_ports_udp(struct fwd_ports *fwd,
}
/**
+ * 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);