From ecec02c8be7b040e9f0843da85741a874d097109 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 26 May 2019 22:36:26 +0200 Subject: [PATCH] refactor(network): better events handler --- include/modules/network.hpp | 1 + src/modules/network.cpp | 76 ++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 9f2a0515..53bf77d6 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -41,6 +41,7 @@ class Network : public ALabel { bool checkInterface(struct ifinfomsg* rtif, std::string name); int getPreferredIface(int skip_idx = -1) const; auto getInfo() -> void; + void checkNewInterface(struct ifinfomsg* rtif); const std::string getNetworkState() const; void clearIface(); bool wildcardMatch(const std::string& pattern, const std::string& text) const; diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 58cf12c6..8ac0189f 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -576,12 +576,53 @@ void waybar::modules::Network::clearIface() { linked_ = false; } +void waybar::modules::Network::checkNewInterface(struct ifinfomsg *rtif) { + auto new_iface = getPreferredIface(rtif->ifi_index); + if (new_iface != -1) { + ifid_ = new_iface; + char ifname[IF_NAMESIZE]; + if_indextoname(new_iface, ifname); + ifname_ = ifname; + getInterfaceAddress(); + thread_timer_.wake_up(); + } else { + ifid_ = -1; + dp.emit(); + } +} + int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { auto net = static_cast(data); std::lock_guard lock(net->mutex_); auto nh = nlmsg_hdr(msg); auto rtif = static_cast(NLMSG_DATA(nh)); - if (nh->nlmsg_type == RTM_NEWADDR) { + if (nh->nlmsg_type == RTM_DELADDR) { + // Check for valid interface + if (rtif->ifi_index == net->ifid_) { + net->ipaddr_.clear(); + net->netmask_.clear(); + net->cidr_ = 0; + if (!(rtif->ifi_flags & IFF_RUNNING)) { + net->clearIface(); + // Check for a new interface and get info + net->checkNewInterface(rtif); + } + net->dp.emit(); + } + } else if (nh->nlmsg_type == RTM_NEWLINK || nh->nlmsg_type == RTM_DELLINK) { + char ifname[IF_NAMESIZE]; + if_indextoname(rtif->ifi_index, ifname); + // Check for valid interface + if (rtif->ifi_flags & IFF_RUNNING && net->checkInterface(rtif, ifname)) { + net->linked_ = true; + net->ifname_ = ifname; + net->ifid_ = rtif->ifi_index; + } else if (rtif->ifi_index == net->ifid_ && !(rtif->ifi_flags & IFF_RUNNING)) { + net->clearIface(); + // Check for a new interface and get info + net->checkNewInterface(rtif); + } + } else { char ifname[IF_NAMESIZE]; if_indextoname(rtif->ifi_index, ifname); // Auto detected network can also be assigned here @@ -601,39 +642,6 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { net->getInterfaceAddress(); net->thread_timer_.wake_up(); } - } else if (nh->nlmsg_type == RTM_DELADDR) { - // Check for valid interface - if (rtif->ifi_index == net->ifid_) { - net->ipaddr_.clear(); - net->netmask_.clear(); - net->cidr_ = 0; - net->dp.emit(); - } - } else if (nh->nlmsg_type < RTM_NEWADDR) { - char ifname[IF_NAMESIZE]; - if_indextoname(rtif->ifi_index, ifname); - // Check for valid interface - if (rtif->ifi_flags & IFF_RUNNING && net->checkInterface(rtif, ifname)) { - net->linked_ = true; - net->ifname_ = ifname; - net->ifid_ = rtif->ifi_index; - net->dp.emit(); - } else if (rtif->ifi_index == net->ifid_) { - net->clearIface(); - // Check for a new interface and get info - auto new_iface = net->getPreferredIface(rtif->ifi_index); - if (new_iface != -1) { - net->ifid_ = new_iface; - char ifname[IF_NAMESIZE]; - if_indextoname(new_iface, ifname); - net->ifname_ = ifname; - net->getInterfaceAddress(); - net->thread_timer_.wake_up(); - } else { - net->ifid_ = -1; - net->dp.emit(); - } - } } return NL_SKIP; }