From bd7018ed9685c5f342bc6dd7204019f79bc9aa28 Mon Sep 17 00:00:00 2001 From: correabuscar Date: Sun, 6 Nov 2022 08:36:06 +0100 Subject: [PATCH] OSX: use the first IP of the interface ...instead of the last. Side effect of this is that it also detects when the current IP gets removed from the interface, instead of keep displaying the old IP. This is PR #457 but for OSX, not Linux. --- src/osx/btop_collect.cpp | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index b0f09b6..65a3f1a 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -19,7 +19,6 @@ tab-size = 4 #include #include #include -#include #include #include #include @@ -28,7 +27,12 @@ tab-size = 4 #include #include #include +// BUGS +// If both and are being included, must be +// included before . +// from: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getifaddrs.3.html #include +#include #include #include #include @@ -868,21 +872,27 @@ namespace Net { if (ifa->ifa_addr == NULL) continue; family = ifa->ifa_addr->sa_family; const auto &iface = ifa->ifa_name; - //? Get IPv4 address - if (family == AF_INET) { - if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) - net[iface].ipv4 = ip; - } - //? Get IPv6 address - else if (family == AF_INET6) { - if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) - net[iface].ipv6 = ip; - } - //? Update available interfaces vector and get status of interface if (not v_contains(interfaces, iface)) { interfaces.push_back(iface); net[iface].connected = (ifa->ifa_flags & IFF_RUNNING); + // An interface can have more than one IP of the same family associated with it, + // but we pick only the first one to show in the NET box. + // Note: Interfaces without any IPv4 and IPv6 set are still valid and monitorable! + net[iface].ipv4.clear(); + 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; + } + //? 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; } }