Skip to content

Commit

Permalink
feat(net): Add unknown-as-up option
Browse files Browse the repository at this point in the history
  • Loading branch information
NBonaparte committed Nov 3, 2017
1 parent dc0edfb commit a04a8eb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/adapters/net.hpp
Expand Up @@ -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();
Expand All @@ -78,6 +79,7 @@ namespace net {
link_status m_status{};
string m_interface;
bool m_tuntap{false};
bool m_unknown_up{false};
};

// }}}
Expand Down
1 change: 1 addition & 0 deletions include/modules/network.hpp
Expand Up @@ -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};
};
}

Expand Down
11 changes: 10 additions & 1 deletion src/adapters/net.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/modules/network.cpp
Expand Up @@ -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<decltype(m_interval)>(name(), "interval", 1s);
m_unknown_up = m_conf.get<bool>(name(), "unknown-as-up", false);

m_conf.warn_deprecated(name(), "udspeed-minwidth", "%downspeed:min:max% and %upspeed:min:max%");

Expand Down Expand Up @@ -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<net::wireless_network>(m_interface);
m_wireless->set_unknown_up(m_unknown_up);
} else {
m_wired = factory_util::unique<net::wired_network>(m_interface);
m_wired->set_unknown_up(m_unknown_up);
};

// We only need to start the subthread if the packetloss animation is used
Expand Down

0 comments on commit a04a8eb

Please sign in to comment.