mirror of https://github.com/aristocratos/btop.git
Merge pull request #464 from correabuscar/osx_replace__getnameinfo__with__inet_ntop
osx: replace getnameinfo with inet_ntop
This commit is contained in:
commit
9dc57534b1
|
@ -41,6 +41,7 @@ tab-size = 4
|
||||||
#include <sys/statvfs.h>
|
#include <sys/statvfs.h>
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <netinet/in.h> // for inet_ntop
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
@ -863,7 +864,9 @@ namespace Net {
|
||||||
return empty_net;
|
return empty_net;
|
||||||
}
|
}
|
||||||
int family = 0;
|
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();
|
interfaces.clear();
|
||||||
string ipv4, ipv6;
|
string ipv4, ipv6;
|
||||||
|
|
||||||
|
@ -884,16 +887,26 @@ namespace Net {
|
||||||
}
|
}
|
||||||
//? Get IPv4 address
|
//? Get IPv4 address
|
||||||
if (family == AF_INET) {
|
if (family == AF_INET) {
|
||||||
if (net[iface].ipv4.empty() and
|
if (net[iface].ipv4.empty()) {
|
||||||
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0)
|
if (NULL != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr)->sin_addr), ip, IPBUFFER_MAXSIZE)) {
|
||||||
net[iface].ipv4 = ip;
|
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
|
//? Get IPv6 address
|
||||||
else if (family == AF_INET6) {
|
else if (family == AF_INET6) {
|
||||||
if (net[iface].ipv6.empty() and
|
if (net[iface].ipv6.empty()) {
|
||||||
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0)
|
if (NULL != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr)->sin6_addr), ip, IPBUFFER_MAXSIZE)) {
|
||||||
net[iface].ipv6 = ip;
|
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_LINK (see man 3 getifaddrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
unordered_flat_map<string, std::tuple<uint64_t, uint64_t>> ifstats;
|
unordered_flat_map<string, std::tuple<uint64_t, uint64_t>> ifstats;
|
||||||
|
|
Loading…
Reference in New Issue