1
0
Fork 0
mirror of https://passt.top/passt synced 2025-06-19 05:25:34 +02:00

util, tcp: Add helper to display socket addresses

When reporting errors, we sometimes want to show a relevant socket address.
Doing so by extracting the various relevant fields can be pretty awkward,
so introduce a sockaddr_ntop() helper to make it simpler.  For now we just
have one user in tcp.c, but I have further upcoming patches which can make
use of it.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-05-21 14:48:03 +10:00 committed by Stefano Brivio
parent 3ff3a8a467
commit 1a20370b36
3 changed files with 79 additions and 14 deletions

56
util.c
View file

@ -553,3 +553,59 @@ int write_remainder(int fd, const struct iovec *iov, int iovcnt, size_t skip)
return 0;
}
/** sockaddr_ntop() - Convert a socket address to text format
* @sa: Socket address
* @dst: output buffer, minimum SOCKADDR_STRLEN bytes
* @size: size of buffer at @dst
*
* Return: On success, a non-null pointer to @dst, NULL on failure
*/
const char *sockaddr_ntop(const void *sa, char *dst, socklen_t size)
{
sa_family_t family = ((const struct sockaddr *)sa)->sa_family;
socklen_t off = 0;
#define IPRINTF(...) \
do { \
off += snprintf(dst + off, size - off, __VA_ARGS__); \
if (off >= size) \
return NULL; \
} while (0)
#define INTOP(af, addr) \
do { \
if (!inet_ntop((af), (addr), dst + off, size - off)) \
return NULL; \
off += strlen(dst + off); \
} while (0)
switch (family) {
case AF_INET: {
const struct sockaddr_in *sa4 = sa;
INTOP(AF_INET, &sa4->sin_addr);
IPRINTF(":%hu", ntohs(sa4->sin_port));
break;
}
case AF_INET6: {
const struct sockaddr_in6 *sa6 = sa;
IPRINTF("[");
INTOP(AF_INET6, &sa6->sin6_addr);
IPRINTF("]:%hu", ntohs(sa6->sin6_port));
break;
}
/* FIXME: Implement AF_UNIX */
default:
errno = EAFNOSUPPORT;
return NULL;
}
#undef IPRINTF
#undef INTOP
return dst;
}