From 2f67c6bef20e58676851df668f70fd261eea89ea Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 16 Jan 2023 16:50:32 +0100 Subject: [PATCH] src/socket_wrapper.c: pretty print ip addresses in tracing output. Signed-off-by: Stefan Metzmacher Reviewed-by: Andreas Schneider --- src/socket_wrapper.c | 75 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index bedda07..4c03e1c 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -1429,6 +1429,55 @@ static size_t socket_length(int family) return 0; } +struct swrap_sockaddr_buf { + char str[128]; +}; + +static const char *swrap_sockaddr_string(struct swrap_sockaddr_buf *buf, + const struct sockaddr *saddr) +{ + unsigned int port = 0; + char addr[64] = {0,}; + + switch (saddr->sa_family) { + case AF_INET: { + const struct sockaddr_in *in = + (const struct sockaddr_in *)(const void *)saddr; + + port = ntohs(in->sin_port); + + inet_ntop(saddr->sa_family, + &in->sin_addr, + addr, sizeof(addr)); + break; + } +#ifdef HAVE_IPV6 + case AF_INET6: { + const struct sockaddr_in6 *in6 = + (const struct sockaddr_in6 *)(const void *)saddr; + + port = ntohs(in6->sin6_port); + + inet_ntop(saddr->sa_family, + &in6->sin6_addr, + addr, sizeof(addr)); + break; + } +#endif + default: + snprintf(addr, sizeof(addr), + "", + saddr->sa_family); + break; + } + + snprintf(buf->str, sizeof(buf->str), + "addr[%s]/port[%u]", + addr, port); + + return buf->str; +} + static struct socket_info *swrap_get_socket_info(int si_index) { return (struct socket_info *)(&(sockets[si_index].info)); @@ -2064,13 +2113,10 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i type = u_type; iface = (addr & 0x000000FF); } else { - char str[256] = {0,}; - inet_ntop(inaddr->sa_family, - &in->sin_addr, - str, sizeof(str)); + struct swrap_sockaddr_buf buf = {}; SWRAP_LOG(SWRAP_LOG_WARN, - "str[%s] prt[%u]", - str, (unsigned)prt); + "%s", + swrap_sockaddr_string(&buf, inaddr)); errno = ENETUNREACH; return -1; } @@ -2106,13 +2152,10 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) { iface = in->sin6_addr.s6_addr[15]; } else { - char str[256] = {0,}; - inet_ntop(inaddr->sa_family, - &in->sin6_addr, - str, sizeof(str)); + struct swrap_sockaddr_buf buf = {}; SWRAP_LOG(SWRAP_LOG_WARN, - "str[%s] prt[%u]", - str, (unsigned)prt); + "%s", + swrap_sockaddr_string(&buf, inaddr)); errno = ENETUNREACH; return -1; } @@ -3984,6 +4027,7 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr, .sa_socklen = sizeof(struct sockaddr_un), }; struct socket_info *si = find_socket_info(s); + struct swrap_sockaddr_buf buf = {}; int bcast = 0; if (!si) { @@ -4032,7 +4076,8 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr, } SWRAP_LOG(SWRAP_LOG_TRACE, - "connect() path=%s, fd=%d", + "connect(%s) path=%s, fd=%d", + swrap_sockaddr_string(&buf, serv_addr), un_addr.sa.un.sun_path, s); @@ -4098,6 +4143,7 @@ static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen) .sa_socklen = sizeof(struct sockaddr_un), }; struct socket_info *si = find_socket_info(s); + struct swrap_sockaddr_buf buf = {}; int bind_error = 0; #if 0 /* FIXME */ bool in_use; @@ -4187,7 +4233,8 @@ static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen) ret = libc_bind(s, &un_addr.sa.s, un_addr.sa_socklen); SWRAP_LOG(SWRAP_LOG_TRACE, - "bind() path=%s, fd=%d", + "bind(%s) path=%s, fd=%d", + swrap_sockaddr_string(&buf, myaddr), un_addr.sa.un.sun_path, s); if (ret == 0) { -- 2.34.1