[Tarantool-patches] [PATCH 12/15] sio: rework sio_strfaddr()
Vladislav Shpilevoy
v.shpilevoy at tarantool.org
Thu Mar 25 00:24:24 MSK 2021
The function was overcomplicated, and made it harder to update it
in the next patches with functional changes.
The main source of the complication was usage of both inet_ntoa()
and getnameinfo(). The latter is more universal, it can cover the
case of the former.
The patch makes it use only getnameinfo() for IP addresses
regardless of v4 or v6.
Needed for #5632
(cherry picked from commit 441cb814d4cdd1ec4999b812d72e04cb5528687f)
---
src/sio.cc | 39 ++++++++++++++++-----------------------
1 file changed, 16 insertions(+), 23 deletions(-)
diff --git a/src/sio.cc b/src/sio.cc
index 3becd1ee2..0ba19346c 100644
--- a/src/sio.cc
+++ b/src/sio.cc
@@ -514,29 +514,22 @@ const char *
sio_strfaddr(struct sockaddr *addr, socklen_t addrlen)
{
static __thread char name[NI_MAXHOST + _POSIX_PATH_MAX + 2];
- switch(addr->sa_family) {
- case AF_UNIX:
- if (addrlen >= sizeof(sockaddr_un)) {
- snprintf(name, sizeof(name), "unix/:%s",
- ((struct sockaddr_un *)addr)->sun_path);
- } else {
- snprintf(name, sizeof(name),
- "unix/:(socket)");
- }
- break;
- default: {
- char host[NI_MAXHOST], serv[NI_MAXSERV];
- if (getnameinfo(addr, addrlen, host, sizeof(host),
- serv, sizeof(serv),
- NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
- snprintf(name, sizeof(name),
- addr->sa_family == AF_INET
- ? "%s:%s" : "[%s]:%s", host, serv);
- } else {
- snprintf(name, sizeof(name), "(host):(port)");
- }
- break;
- }
+ if (addr->sa_family == AF_UNIX) {
+ struct sockaddr_un *u = (struct sockaddr_un *)addr;
+ if (addrlen >= sizeof(*u))
+ snprintf(name, sizeof(name), "unix/:%s", u->sun_path);
+ else
+ snprintf(name, sizeof(name), "unix/:(socket)");
+ } else {
+ char host[NI_MAXHOST], serv[NI_MAXSERV];
+ int flags = NI_NUMERICHOST | NI_NUMERICSERV;
+ if (getnameinfo(addr, addrlen, host, sizeof(host),
+ serv, sizeof(serv), flags) != 0)
+ snprintf(name, sizeof(name), "(host):(port)");
+ else if (addr->sa_family == AF_INET)
+ snprintf(name, sizeof(name), "%s:%s", host, serv);
+ else
+ snprintf(name, sizeof(name), "[%s]:%s", host, serv);
}
return name;
}
--
2.24.3 (Apple Git-128)
More information about the Tarantool-patches
mailing list