From dcf5c0eb1e877f1de84c94bb07c30d109cb85b70 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 3 Nov 2023 13:23:02 +1100 Subject: port_fwd: Move port scanning /proc fds into struct port_fwd Currently we store /proc/net fds used to implement automatic port forwarding in the proc_net_{tcp,udp} fields of the main context structure. However, in fact each of those is associated with a particular direction of forwarding, and we already have struct port_fwd which collects all other information related to a particular direction of port forwarding. We can simplify things a bit by moving the /proc fds into struct port_fwd. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- port_fwd.c | 62 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'port_fwd.c') diff --git a/port_fwd.c b/port_fwd.c index 40e633d..9502463 100644 --- a/port_fwd.c +++ b/port_fwd.c @@ -74,19 +74,19 @@ static void procfs_scan_listen(int fd, unsigned int lstate, */ void get_bound_ports_tcp(struct ctx *c, int ns) { - uint8_t *map, *excl; + struct port_fwd *fwd, *rev; if (ns) { - map = c->tcp.fwd_in.map; - excl = c->tcp.fwd_out.map; + fwd = &c->tcp.fwd_in; + rev = &c->tcp.fwd_out; } else { - map = c->tcp.fwd_out.map; - excl = c->tcp.fwd_in.map; + fwd = &c->tcp.fwd_out; + rev = &c->tcp.fwd_in; } - memset(map, 0, PORT_BITMAP_SIZE); - procfs_scan_listen(c->proc_net_tcp[V4][ns], TCP_LISTEN, map, excl); - procfs_scan_listen(c->proc_net_tcp[V6][ns], TCP_LISTEN, map, excl); + memset(fwd->map, 0, PORT_BITMAP_SIZE); + procfs_scan_listen(fwd->scan4, TCP_LISTEN, fwd->map, rev->map); + procfs_scan_listen(fwd->scan6, TCP_LISTEN, fwd->map, rev->map); } /** @@ -96,27 +96,29 @@ void get_bound_ports_tcp(struct ctx *c, int ns) */ void get_bound_ports_udp(struct ctx *c, int ns) { - uint8_t *map, *excl; + struct port_fwd *fwd, *rev, *tcp; if (ns) { - map = c->udp.fwd_in.f.map; - excl = c->udp.fwd_out.f.map; + fwd = &c->udp.fwd_in.f; + rev = &c->udp.fwd_out.f; + tcp = &c->tcp.fwd_in; } else { - map = c->udp.fwd_out.f.map; - excl = c->udp.fwd_in.f.map; + fwd = &c->udp.fwd_out.f; + rev = &c->udp.fwd_in.f; + tcp = &c->tcp.fwd_out; } - memset(map, 0, PORT_BITMAP_SIZE); - procfs_scan_listen(c->proc_net_udp[V4][ns], UDP_LISTEN, map, excl); - procfs_scan_listen(c->proc_net_udp[V6][ns], UDP_LISTEN, map, excl); + memset(fwd->map, 0, PORT_BITMAP_SIZE); + procfs_scan_listen(fwd->scan4, UDP_LISTEN, fwd->map, rev->map); + procfs_scan_listen(fwd->scan6, UDP_LISTEN, fwd->map, rev->map); /* Also forward UDP ports with the same numbers as bound TCP ports. * This is useful for a handful of protocols (e.g. iperf3) where a TCP * control port is used to set up transfers on a corresponding UDP * port. */ - procfs_scan_listen(c->proc_net_tcp[V4][ns], TCP_LISTEN, map, excl); - procfs_scan_listen(c->proc_net_tcp[V6][ns], TCP_LISTEN, map, excl); + procfs_scan_listen(tcp->scan4, TCP_LISTEN, fwd->map, rev->map); + procfs_scan_listen(tcp->scan6, TCP_LISTEN, fwd->map, rev->map); } /** @@ -127,29 +129,29 @@ void port_fwd_init(struct ctx *c) { const int flags = O_RDONLY | O_CLOEXEC; - c->proc_net_tcp[V4][0] = c->proc_net_tcp[V4][1] = -1; - c->proc_net_tcp[V6][0] = c->proc_net_tcp[V6][1] = -1; - c->proc_net_udp[V4][0] = c->proc_net_udp[V4][1] = -1; - c->proc_net_udp[V6][0] = c->proc_net_udp[V6][1] = -1; + c->tcp.fwd_in.scan4 = c->tcp.fwd_in.scan6 = -1; + c->tcp.fwd_out.scan4 = c->tcp.fwd_out.scan6 = -1; + c->udp.fwd_in.f.scan4 = c->udp.fwd_in.f.scan6 = -1; + c->udp.fwd_out.f.scan4 = c->udp.fwd_out.f.scan6 = -1; if (c->tcp.fwd_in.mode == FWD_AUTO) { - c->proc_net_tcp[V4][1] = open_in_ns(c, "/proc/net/tcp", flags); - c->proc_net_tcp[V6][1] = open_in_ns(c, "/proc/net/tcp6", flags); + 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); get_bound_ports_tcp(c, 1); } if (c->udp.fwd_in.f.mode == FWD_AUTO) { - c->proc_net_udp[V4][1] = open_in_ns(c, "/proc/net/udp", flags); - c->proc_net_udp[V6][1] = open_in_ns(c, "/proc/net/udp6", flags); + c->udp.fwd_in.f.scan4 = open_in_ns(c, "/proc/net/udp", flags); + c->udp.fwd_in.f.scan6 = open_in_ns(c, "/proc/net/udp6", flags); get_bound_ports_udp(c, 1); } if (c->tcp.fwd_out.mode == FWD_AUTO) { - c->proc_net_tcp[V4][0] = open("/proc/net/tcp", flags); - c->proc_net_tcp[V6][0] = open("/proc/net/tcp6", flags); + c->tcp.fwd_out.scan4 = open("/proc/net/tcp", flags); + c->tcp.fwd_out.scan6 = open("/proc/net/tcp6", flags); get_bound_ports_tcp(c, 0); } if (c->udp.fwd_out.f.mode == FWD_AUTO) { - c->proc_net_udp[V4][0] = open("/proc/net/udp", flags); - c->proc_net_udp[V6][0] = open("/proc/net/udp6", flags); + c->udp.fwd_out.f.scan4 = open("/proc/net/udp", flags); + c->udp.fwd_out.f.scan6 = open("/proc/net/udp6", flags); get_bound_ports_udp(c, 0); } } -- cgit v1.2.3