From ab0bef204a248609ac05948e531108a8032b8e63 Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Fri, 9 Feb 2024 13:53:42 +0100 Subject: [PATCH] collect: Share ifaddrs wrapper and use uniq_ptr-like syntax --- src/btop_shared.hpp | 11 +++++++++++ src/freebsd/btop_collect.cpp | 19 ++++--------------- src/linux/btop_collect.cpp | 18 ++++-------------- src/openbsd/btop_collect.cpp | 19 ++++--------------- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index e60da98..8031321 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -307,6 +307,17 @@ namespace Net { bool connected{}; }; + class IfAddrsPtr { + struct ifaddrs* ifaddr; + int status; + public: + IfAddrsPtr() { status = getifaddrs(&ifaddr); } + ~IfAddrsPtr() { freeifaddrs(ifaddr); } + [[nodiscard]] constexpr auto operator()() -> struct ifaddrs* { return ifaddr; } + [[nodiscard]] constexpr auto get() -> struct ifaddrs* { return ifaddr; } + [[nodiscard]] constexpr auto get_status() const noexcept -> int { return status; }; + }; + extern std::unordered_map current_net; //* Collect net upload/download stats diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 094d78a..d64a878 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -811,17 +811,6 @@ namespace Net { bool rescale = true; uint64_t timestamp = 0; - //* RAII wrapper for getifaddrs - class getifaddr_wrapper { - struct ifaddrs *ifaddr; - - public: - int status; - getifaddr_wrapper() { status = getifaddrs(&ifaddr); } - ~getifaddr_wrapper() { freeifaddrs(ifaddr); } - auto operator()() -> struct ifaddrs * { return ifaddr; } - }; - auto collect(bool no_update) -> net_info & { auto &net = current_net; auto &config_iface = Config::getS("net_iface"); @@ -831,10 +820,10 @@ namespace Net { if (not no_update and errors < 3) { //? Get interface list using getifaddrs() wrapper - getifaddr_wrapper if_wrap{}; - if (if_wrap.status != 0) { + IfAddrsPtr if_addrs {}; + if (if_addrs.get_status() != 0) { errors++; - Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status)); + Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status())); redraw = true; return empty_net; } @@ -846,7 +835,7 @@ namespace Net { string ipv4, ipv6; //? Iteration over all items in getifaddrs() list - for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) { + for (auto *ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_addr == nullptr) continue; family = ifa->ifa_addr->sa_family; const auto &iface = ifa->ifa_name; diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index eb8c2b7..7096ad9 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -2151,16 +2151,6 @@ namespace Net { bool rescale{true}; uint64_t timestamp{}; - //* RAII wrapper for getifaddrs - class getifaddr_wrapper { - struct ifaddrs* ifaddr; - public: - int status; - getifaddr_wrapper() { status = getifaddrs(&ifaddr); } - ~getifaddr_wrapper() { freeifaddrs(ifaddr); } - auto operator()() -> struct ifaddrs* { return ifaddr; } - }; - auto collect(bool no_update) -> net_info& { if (Runner::stopping) return empty_net; auto& net = current_net; @@ -2171,10 +2161,10 @@ namespace Net { if (not no_update and errors < 3) { //? Get interface list using getifaddrs() wrapper - getifaddr_wrapper if_wrap {}; - if (if_wrap.status != 0) { + IfAddrsPtr if_addrs {}; + if (if_addrs.get_status() != 0) { errors++; - Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status)); + Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status())); redraw = true; return empty_net; } @@ -2186,7 +2176,7 @@ namespace Net { string ipv4, ipv6; //? Iteration over all items in getifaddrs() list - for (auto* ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) { + for (auto* ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_addr == nullptr) continue; family = ifa->ifa_addr->sa_family; const auto& iface = ifa->ifa_name; diff --git a/src/openbsd/btop_collect.cpp b/src/openbsd/btop_collect.cpp index cc24388..b0d36a5 100644 --- a/src/openbsd/btop_collect.cpp +++ b/src/openbsd/btop_collect.cpp @@ -768,17 +768,6 @@ namespace Net { bool rescale = true; uint64_t timestamp = 0; - //* RAII wrapper for getifaddrs - class getifaddr_wrapper { - struct ifaddrs *ifaddr; - - public: - int status; - getifaddr_wrapper() { status = getifaddrs(&ifaddr); } - ~getifaddr_wrapper() { freeifaddrs(ifaddr); } - auto operator()() -> struct ifaddrs * { return ifaddr; } - }; - auto collect(bool no_update) -> net_info & { auto &net = current_net; auto &config_iface = Config::getS("net_iface"); @@ -788,10 +777,10 @@ namespace Net { if (not no_update and errors < 3) { //? Get interface list using getifaddrs() wrapper - getifaddr_wrapper if_wrap{}; - if (if_wrap.status != 0) { + IfAddrsPtr if_addrs {}; + if (if_addrs.get_status() != 0) { errors++; - Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status)); + Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status())); redraw = true; return empty_net; } @@ -803,7 +792,7 @@ namespace Net { string ipv4, ipv6; //? Iteration over all items in getifaddrs() list - for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) { + for (auto *ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_addr == nullptr) continue; family = ifa->ifa_addr->sa_family; const auto &iface = ifa->ifa_name;