diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2022-11-17 16:59:05 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2022-11-25 01:35:55 +0100 |
commit | ebf56c7b56dfb61ef600890ea061b251dec08cc7 (patch) | |
tree | 0037a935c580b1b832d336d9840ea7f22585fe58 /tcp.c | |
parent | 023213facd95d19117308b09db6308684e8c5bbf (diff) | |
download | passt-ebf56c7b56dfb61ef600890ea061b251dec08cc7.tar passt-ebf56c7b56dfb61ef600890ea061b251dec08cc7.tar.gz passt-ebf56c7b56dfb61ef600890ea061b251dec08cc7.tar.bz2 passt-ebf56c7b56dfb61ef600890ea061b251dec08cc7.tar.lz passt-ebf56c7b56dfb61ef600890ea061b251dec08cc7.tar.xz passt-ebf56c7b56dfb61ef600890ea061b251dec08cc7.tar.zst passt-ebf56c7b56dfb61ef600890ea061b251dec08cc7.zip |
tcp: Consolidate tcp_sock_init[46]
Previous cleanups mean that tcp_sock_init4() and tcp_sock_init6() are
almost identical, and the remaining differences can be easily
parameterized. Combine both into a single tcp_sock_init_af() function.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'tcp.c')
-rw-r--r-- | tcp.c | 50 |
1 files changed, 15 insertions, 35 deletions
@@ -2978,52 +2978,32 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, } /** - * tcp_sock_init4() - Initialise listening sockets for a given IPv4 port + * tcp_sock_init_af() - Initialise listening socket for a given af and port * @c: Execution context + * @af: Address family to listen on + * @port: Port, host order * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured - * @port: Port, host order + * + * Return: fd for the new listening socket, or -1 on failure */ -static void tcp_sock_init4(const struct ctx *c, const struct in_addr *addr, - const char *ifname, in_port_t port) +static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port, + const struct in_addr *addr, const char *ifname) { in_port_t idx = port + c->tcp.fwd_in.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx }; int s; - s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + s = sock_l4(c, af, IPPROTO_TCP, addr, ifname, port, tref.u32); if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V4] = s; -} - -/** - * tcp_sock_init6() - Initialise listening sockets for a given IPv6 port - * @c: Execution context - * @addr: Pointer to address for binding, NULL if not configured - * @ifname: Name of interface to bind to, NULL if not configured - * @port: Port, host order - */ -static void tcp_sock_init6(const struct ctx *c, - const struct in6_addr *addr, const char *ifname, - in_port_t port) -{ - in_port_t idx = port + c->tcp.fwd_in.delta[port]; - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx }; - int s; + tcp_sock_init_ext[port][(af == AF_INET) ? V4 : V6] = s; - s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + if (s < 0) + return -1; - if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V6] = s; + tcp_sock_set_bufsize(c, s); + return s; } /** @@ -3038,9 +3018,9 @@ void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, const char *ifname, in_port_t port) { if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) - tcp_sock_init4(c, addr, ifname, port); + tcp_sock_init_af(c, AF_INET, port, addr, ifname); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) - tcp_sock_init6(c, addr, ifname, port); + tcp_sock_init_af(c, AF_INET6, port, addr, ifname); } /** |