diff options
author | correabuscar <correabuscar+githubcommits@gmail.com> | 2022-11-06 08:17:38 +0100 |
---|---|---|
committer | correabuscar <correabuscar+githubcommits@gmail.com> | 2022-11-06 08:23:30 +0100 |
commit | 16ce8d21b151db87ec14d1c7f09a0d8eb6357ba0 (patch) | |
tree | ed473ceb2d513d32aab1a7946ceb29a0ddd940b6 | |
parent | c65c5e5d2233854dd9b46296210c15b487c9fbd7 (diff) |
FreeBSD: 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 FreeBSD, not Linux.
-rw-r--r-- | src/freebsd/btop_collect.cpp | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index c0d5163..f6d51b2 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -19,9 +19,10 @@ tab-size = 4 #include <cstdio> #include <cstdlib> #include <cstring> -#include <ifaddrs.h> #include <libproc.h> +// man 3 getifaddrs: "BUGS: If both <net/if.h> and <ifaddrs.h> are being included, <net/if.h> must be included before <ifaddrs.h>" #include <net/if.h> +#include <ifaddrs.h> #include <net/if_dl.h> #include <net/route.h> #include <netdb.h> @@ -829,22 +830,30 @@ namespace Net { if (ifa->ifa_addr == NULL) continue; family = ifa->ifa_addr->sa_family; const auto &iface = ifa->ifa_name; + //? 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 (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) + if (net[iface].ipv4.empty() and + getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr), 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) + // 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; // } - //? 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); - } } unordered_flat_map<string, std::tuple<uint64_t, uint64_t>> ifstats; |