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 --- icmp.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'icmp.c') diff --git a/icmp.c b/icmp.c index faa38c8..fb2fcaf 100644 --- a/icmp.c +++ b/icmp.c @@ -36,6 +36,8 @@ #include "passt.h" #include "tap.h" #include "log.h" +#include "siphash.h" +#include "inany.h" #include "icmp.h" #define ICMP_ECHO_TIMEOUT 60 /* s, timeout for ICMP socket activity */ @@ -67,13 +69,9 @@ void icmp_sock_handler(const struct ctx *c, sa_family_t af, union epoll_ref ref) struct icmp_id_sock *const id_sock = af == AF_INET ? &icmp_id_map[V4][ref.icmp.id] : &icmp_id_map[V6][ref.icmp.id]; const char *const pname = af == AF_INET ? "ICMP" : "ICMPv6"; - char buf[USHRT_MAX]; - union { - struct sockaddr sa; - struct sockaddr_in sa4; - struct sockaddr_in6 sa6; - } sr; + union sockaddr_inany sr; socklen_t sl = sizeof(sr); + char buf[USHRT_MAX]; uint16_t seq; ssize_t n; @@ -86,7 +84,7 @@ void icmp_sock_handler(const struct ctx *c, sa_family_t af, union epoll_ref ref) pname, strerror(errno)); return; } - if (sr.sa.sa_family != af) + if (sr.sa_family != af) goto unexpected; if (af == AF_INET) { @@ -214,11 +212,7 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, const struct pool *p, const struct timespec *now) { const char *const pname = af == AF_INET ? "ICMP" : "ICMPv6"; - union { - struct sockaddr sa; - struct sockaddr_in sa4; - struct sockaddr_in6 sa6; - } sa = { .sa.sa_family = af }; + union sockaddr_inany sa = { .sa_family = af }; const socklen_t sl = af == AF_INET ? sizeof(sa.sa4) : sizeof(sa.sa6); struct icmp_id_sock *id_sock; uint16_t id, seq; -- cgit v1.2.3