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:

committed by
Stefano Brivio

parent
3ff3a8a467
commit
1a20370b36
14
util.h
14
util.h
@@ -180,6 +180,20 @@ static inline const char *af_name(sa_family_t af)
|
||||
}
|
||||
}
|
||||
|
||||
#define UINT16_STRLEN (sizeof("65535"))
|
||||
|
||||
/* inet address (- '\0') + port (u16) (- '\0') + ':' + '\0' */
|
||||
#define SOCKADDR_INET_STRLEN \
|
||||
(INET_ADDRSTRLEN-1 + UINT16_STRLEN-1 + sizeof(":"))
|
||||
|
||||
/* inet6 address (- '\0') + port (u16) (- '\0') + '[' + ']' + ':' + '\0' */
|
||||
#define SOCKADDR_INET6_STRLEN \
|
||||
(INET6_ADDRSTRLEN-1 + UINT16_STRLEN-1 + sizeof("[]:"))
|
||||
|
||||
#define SOCKADDR_STRLEN MAX(SOCKADDR_INET_STRLEN, SOCKADDR_INET6_STRLEN)
|
||||
|
||||
const char *sockaddr_ntop(const void *sa, char *dst, socklen_t size);
|
||||
|
||||
/**
|
||||
* mod_sub() - Modular arithmetic subtraction
|
||||
* @a: Minued, unsigned value < @m
|
||||
|
Reference in New Issue
Block a user