From 0a8a326730248131880430596c83a8529c0ffe38 Mon Sep 17 00:00:00 2001 From: NBonaparte Date: Thu, 2 Nov 2017 23:05:26 -0700 Subject: [PATCH] feat(net): Add unknown-as-up option Fixes #457 --- include/adapters/net.hpp | 2 ++ include/modules/network.hpp | 1 + src/adapters/net.cpp | 11 ++++++++++- src/modules/network.cpp | 3 +++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/adapters/net.hpp b/include/adapters/net.hpp index daeefcad..248a8851 100644 --- a/include/adapters/net.hpp +++ b/include/adapters/net.hpp @@ -68,6 +68,7 @@ namespace net { string ip() const; string downspeed(int minwidth = 3) const; string upspeed(int minwidth = 3) const; + void set_unknown_up(bool unknown = true); protected: void check_tuntap(); @@ -78,6 +79,7 @@ namespace net { link_status m_status{}; string m_interface; bool m_tuntap{false}; + bool m_unknown_up{false}; }; // }}} diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 4165a2bf..87d0c44c 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -51,6 +51,7 @@ namespace modules { int m_ping_nth_update{0}; int m_udspeed_minwidth{0}; bool m_accumulate{false}; + bool m_unknown_up{false}; }; } diff --git a/src/adapters/net.cpp b/src/adapters/net.cpp index 3418e3bf..97d3b818 100644 --- a/src/adapters/net.cpp +++ b/src/adapters/net.cpp @@ -141,6 +141,13 @@ namespace net { return format_speedrate(bytes_diff, minwidth); } + /** + * Set if unknown counts as up + */ + void network::set_unknown_up(bool unknown) { + m_unknown_up = unknown; + } + /** * Query driver info to check if the * interface is a TUN/TAP device @@ -174,7 +181,9 @@ namespace net { * Test if the network interface is in a valid state */ bool network::test_interface() const { - return file_util::contents("/sys/class/net/" + m_interface + "/operstate").compare(0, 2, "up") == 0; + auto operstate = file_util::contents("/sys/class/net/" + m_interface + "/operstate"); + bool up = operstate.compare(0, 2, "up") == 0; + return m_unknown_up ? (up || operstate.compare(0, 7, "unknown") == 0) : up; } /** diff --git a/src/modules/network.cpp b/src/modules/network.cpp index c46bb29e..ebfddc57 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -20,6 +20,7 @@ namespace modules { m_udspeed_minwidth = m_conf.get(name(), "udspeed-minwidth", m_udspeed_minwidth); m_accumulate = m_conf.get(name(), "accumulate-stats", m_accumulate); m_interval = m_conf.get(name(), "interval", 1s); + m_unknown_up = m_conf.get(name(), "unknown-as-up", false); m_conf.warn_deprecated(name(), "udspeed-minwidth", "%downspeed:min:max% and %upspeed:min:max%"); @@ -62,8 +63,10 @@ namespace modules { // Get an intstance of the network interface if (net::is_wireless_interface(m_interface)) { m_wireless = factory_util::unique(m_interface); + m_wireless->set_unknown_up(m_unknown_up); } else { m_wired = factory_util::unique(m_interface); + m_wired->set_unknown_up(m_unknown_up); }; // We only need to start the subthread if the packetloss animation is used