network: Fix use of carrier information

Some RTM_NEWLINK messages may not have the IFLA_CARRIER information.
This is the case when a WiFi interface report scan result are
available. `carrier` is used regardless of if it is present in the
message or not. This would result in the interface appearing
"disconnected" in waybar when it isn't.

This patch now check that `carrier` is available before using it.

The same thing could potentially happen to `ifname` so check if it's
set before recording it.

Fixes: c1427ff (network: Handle carrier information)
Fixes #388
This commit is contained in:
Anthony PERARD 2021-05-26 18:28:49 +01:00
parent 94a882bf95
commit 28dfb0ba41

View File

@ -425,7 +425,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
struct rtattr *ifla = IFLA_RTA(ifi); struct rtattr *ifla = IFLA_RTA(ifi);
const char *ifname = NULL; const char *ifname = NULL;
size_t ifname_len = 0; size_t ifname_len = 0;
bool carrier = false; std::optional<bool> carrier;
if (net->ifid_ != -1 && ifi->ifi_index != net->ifid_) { if (net->ifid_ != -1 && ifi->ifi_index != net->ifid_) {
return NL_OK; return NL_OK;
@ -446,11 +446,13 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
if (!is_del_event && ifi->ifi_index == net->ifid_) { if (!is_del_event && ifi->ifi_index == net->ifid_) {
// Update inferface information // Update inferface information
if (net->ifname_.empty()) { if (net->ifname_.empty() && ifname != NULL) {
std::string new_ifname (ifname, ifname_len); std::string new_ifname (ifname, ifname_len);
net->ifname_ = new_ifname; net->ifname_ = new_ifname;
} }
net->carrier_ = carrier; if (carrier.has_value()) {
net->carrier_ = carrier.value();
}
} else if (!is_del_event && net->ifid_ == -1) { } else if (!is_del_event && net->ifid_ == -1) {
// Checking if it's an interface we care about. // Checking if it's an interface we care about.
std::string new_ifname (ifname, ifname_len); std::string new_ifname (ifname, ifname_len);
@ -459,7 +461,9 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
net->ifname_ = new_ifname; net->ifname_ = new_ifname;
net->ifid_ = ifi->ifi_index; net->ifid_ = ifi->ifi_index;
net->carrier_ = carrier; if (carrier.has_value()) {
net->carrier_ = carrier.value();
}
net->thread_timer_.wake_up(); net->thread_timer_.wake_up();
/* An address for this new interface should be received via an /* An address for this new interface should be received via an
* RTM_NEWADDR event either because we ask for a dump of both links * RTM_NEWADDR event either because we ask for a dump of both links