diff options
author | Jakob P. Liljenberg <admin@qvantnet.com> | 2022-11-05 00:17:33 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-05 00:17:33 +0100 |
commit | c65c5e5d2233854dd9b46296210c15b487c9fbd7 (patch) | |
tree | 09f9da227d005f5fcf9df71565f7d06e0bf0d86b /src | |
parent | 6fc58c43601810215111bebcf487bad84eb6af29 (diff) | |
parent | bad9bbc160859b43764e6bb7886eb9a222058a0c (diff) |
Merge pull request #458 from correabuscar/replace__getnameinfo__with__inet_ntop
Replace getnameinfo with inet_ntop [on Linux]
Diffstat (limited to 'src')
-rw-r--r-- | src/linux/btop_collect.cpp | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index f35e6ec..282ab1b 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -26,6 +26,8 @@ tab-size = 4 #include <netdb.h> #include <ifaddrs.h> #include <net/if.h> +#include <arpa/inet.h> // for inet_ntop() + #if !(defined(STATIC_BUILD) && defined(__GLIBC__)) #include <pwd.h> @@ -1388,7 +1390,9 @@ namespace Net { return empty_net; } int family = 0; - char ip[NI_MAXHOST]; + static_assert(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); // 46 >= 16, compile-time assurance. + enum { IPBUFFER_MAXSIZE = INET6_ADDRSTRLEN }; // manually using the known biggest value, guarded by the above static_assert + char ip[IPBUFFER_MAXSIZE]; interfaces.clear(); string ipv4, ipv6; @@ -1410,17 +1414,28 @@ namespace Net { net[iface].ipv6.clear(); } + //? Get IPv4 address if (family == AF_INET) { - if (net[iface].ipv4.empty() and - getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) - net[iface].ipv4 = ip; + if (net[iface].ipv4.empty()) { + if (NULL != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr)->sin_addr), ip, IPBUFFER_MAXSIZE)) { + net[iface].ipv4 = ip; + } else { + int errsv = errno; + Logger::error("Net::collect() -> Failed to convert IPv4 to string for iface " + string(iface) + ", errno: " + strerror(errsv)); + } + } } //? Get IPv6 address else if (family == AF_INET6) { - if (net[iface].ipv6.empty() and - getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) - net[iface].ipv6 = ip; + if (net[iface].ipv6.empty()) { + if (NULL != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr)->sin6_addr), ip, IPBUFFER_MAXSIZE)) { + net[iface].ipv6 = ip; + } else { + int errsv = errno; + Logger::error("Net::collect() -> Failed to convert IPv6 to string for iface " + string(iface) + ", errno: " + strerror(errsv)); + } + } } //else, ignoring family==AF_PACKET (see man 3 getifaddrs) which is the first one in the `for` loop. } |