diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2023-11-03 13:23:02 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2023-11-07 09:53:29 +0100 |
commit | dcf5c0eb1e877f1de84c94bb07c30d109cb85b70 (patch) | |
tree | 3c4e8351befa829f1b94a2a6a803f83402e42a5c /port_fwd.c | |
parent | 1a40d0089506e120dac83c000777e86f6bcdd362 (diff) | |
download | passt-dcf5c0eb1e877f1de84c94bb07c30d109cb85b70.tar passt-dcf5c0eb1e877f1de84c94bb07c30d109cb85b70.tar.gz passt-dcf5c0eb1e877f1de84c94bb07c30d109cb85b70.tar.bz2 passt-dcf5c0eb1e877f1de84c94bb07c30d109cb85b70.tar.lz passt-dcf5c0eb1e877f1de84c94bb07c30d109cb85b70.tar.xz passt-dcf5c0eb1e877f1de84c94bb07c30d109cb85b70.tar.zst passt-dcf5c0eb1e877f1de84c94bb07c30d109cb85b70.zip |
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 <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'port_fwd.c')
-rw-r--r-- | port_fwd.c | 62 |
1 files changed, 32 insertions, 30 deletions
@@ -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); } } |