From f6e6e8ad40eed1cbd48122c67a2ca8fd3a293992 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 28 Feb 2024 22:25:04 +1100 Subject: inany: Introduce union sockaddr_inany There are a number of places where we want to handle either a sockaddr_in or a sockaddr_in6. In some of those we use a void *, which works ok and matches some standard library interfaces, but doesn't give a signature level hint that we're dealing with only sockaddr_in or sockaddr_in6, not (say) sockaddr_un or another type of socket address. Other places we use a sockaddr_storage, which also works, but has the same problem in addition to allocating more on the stack than we need to. Introduce union sockaddr_inany to explictly handle this case: it has variants for sockaddr_in and sockaddr_in6. Use it in a number of places where it's easy to do so. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- tcp.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'tcp.c') diff --git a/tcp.c b/tcp.c index dbe787b..53502b1 100644 --- a/tcp.c +++ b/tcp.c @@ -2689,7 +2689,7 @@ static void tcp_snat_inbound(const struct ctx *c, union inany_addr *addr) static void tcp_tap_conn_from_sock(struct ctx *c, union tcp_listen_epoll_ref ref, struct tcp_tap_conn *conn, int s, - const struct sockaddr *sa, + const union sockaddr_inany *sa, const struct timespec *now) { conn->f.type = FLOW_TCP; @@ -2725,7 +2725,7 @@ static void tcp_tap_conn_from_sock(struct ctx *c, void tcp_listen_handler(struct ctx *c, union epoll_ref ref, const struct timespec *now) { - struct sockaddr_storage sa; + union sockaddr_inany sa; socklen_t sl = sizeof(sa); union flow *flow; int s; @@ -2733,17 +2733,16 @@ void tcp_listen_handler(struct ctx *c, union epoll_ref ref, if (c->no_tcp || !(flow = flow_alloc())) return; - s = accept4(ref.fd, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK); + s = accept4(ref.fd, &sa.sa, &sl, SOCK_NONBLOCK); if (s < 0) goto cancel; if (c->mode == MODE_PASTA && tcp_splice_conn_from_sock(c, ref.tcp_listen, &flow->tcp_splice, - s, (struct sockaddr *)&sa)) + s, &sa)) return; - tcp_tap_conn_from_sock(c, ref.tcp_listen, &flow->tcp, s, - (struct sockaddr *)&sa, now); + tcp_tap_conn_from_sock(c, ref.tcp_listen, &flow->tcp, s, &sa, now); return; cancel: -- cgit v1.2.3