2022-04-06 06:37:19 +00:00
|
|
|
#include "modules/network.hpp"
|
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
#include <linux/if.h>
|
2019-05-18 23:44:45 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-04-18 15:52:00 +00:00
|
|
|
#include <sys/eventfd.h>
|
2021-08-25 21:47:51 +00:00
|
|
|
|
2020-01-07 19:27:31 +00:00
|
|
|
#include <cassert>
|
2021-08-25 21:47:51 +00:00
|
|
|
#include <fstream>
|
2020-11-26 23:38:41 +00:00
|
|
|
#include <optional>
|
2022-04-06 06:37:19 +00:00
|
|
|
#include <sstream>
|
2021-08-25 21:47:51 +00:00
|
|
|
|
2019-09-23 20:25:54 +00:00
|
|
|
#include "util/format.hpp"
|
2020-08-14 18:58:48 +00:00
|
|
|
#ifdef WANT_RFKILL
|
2020-01-21 16:48:45 +00:00
|
|
|
#include "util/rfkill.hpp"
|
2020-08-14 18:58:48 +00:00
|
|
|
#endif
|
2019-05-18 03:39:51 +00:00
|
|
|
|
|
|
|
namespace {
|
2019-09-23 20:25:54 +00:00
|
|
|
using namespace waybar::util;
|
2021-06-07 18:24:38 +00:00
|
|
|
constexpr const char *DEFAULT_FORMAT = "{ifname}";
|
2021-08-25 21:47:51 +00:00
|
|
|
} // namespace
|
2019-05-21 15:38:47 +00:00
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
constexpr const char *NETDEV_FILE =
|
|
|
|
"/proc/net/dev"; // std::ifstream does not take std::string_view as param
|
|
|
|
std::optional<std::pair<unsigned long long, unsigned long long>>
|
|
|
|
waybar::modules::Network::readBandwidthUsage() {
|
|
|
|
std::ifstream netdev(NETDEV_FILE);
|
|
|
|
if (!netdev) {
|
|
|
|
spdlog::warn("Failed to open netdev file {}", NETDEV_FILE);
|
2019-05-21 15:38:47 +00:00
|
|
|
return {};
|
|
|
|
}
|
2019-05-18 03:39:51 +00:00
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
std::string line;
|
2021-08-26 18:30:06 +00:00
|
|
|
// skip the headers (first two lines)
|
2021-08-25 21:47:51 +00:00
|
|
|
std::getline(netdev, line);
|
|
|
|
std::getline(netdev, line);
|
2019-05-18 03:39:51 +00:00
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
unsigned long long receivedBytes = 0ull;
|
|
|
|
unsigned long long transmittedBytes = 0ull;
|
2021-08-26 18:30:06 +00:00
|
|
|
while (std::getline(netdev, line)) {
|
2021-08-25 21:47:51 +00:00
|
|
|
std::istringstream iss(line);
|
2019-05-18 03:39:51 +00:00
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
std::string ifacename;
|
2022-04-06 06:37:19 +00:00
|
|
|
iss >> ifacename; // ifacename contains "eth0:"
|
2021-08-25 21:47:51 +00:00
|
|
|
ifacename.pop_back(); // remove trailing ':'
|
2022-04-18 10:56:30 +00:00
|
|
|
if (ifacename != ifname_) {
|
2021-08-25 21:47:51 +00:00
|
|
|
continue;
|
2019-05-18 03:39:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
// The rest of the line consists of whitespace separated counts divided
|
2021-08-26 18:30:06 +00:00
|
|
|
// into two groups (receive and transmit). Each group has the following
|
|
|
|
// columns: bytes, packets, errs, drop, fifo, frame, compressed, multicast
|
|
|
|
//
|
|
|
|
// We only care about the bytes count, so we'll just ignore the 7 other
|
|
|
|
// columns.
|
2021-08-25 21:47:51 +00:00
|
|
|
unsigned long long r = 0ull;
|
|
|
|
unsigned long long t = 0ull;
|
|
|
|
// Read received bytes
|
|
|
|
iss >> r;
|
|
|
|
// Skip all the other columns in the received group
|
|
|
|
for (int colsToSkip = 7; colsToSkip > 0; colsToSkip--) {
|
|
|
|
// skip whitespace between columns
|
2022-04-06 06:37:19 +00:00
|
|
|
while (iss.peek() == ' ') {
|
|
|
|
iss.ignore();
|
|
|
|
}
|
2021-08-25 21:47:51 +00:00
|
|
|
// skip the irrelevant column
|
2022-04-06 06:37:19 +00:00
|
|
|
while (iss.peek() != ' ') {
|
|
|
|
iss.ignore();
|
|
|
|
}
|
2021-08-25 21:47:51 +00:00
|
|
|
}
|
|
|
|
// Read transmit bytes
|
|
|
|
iss >> t;
|
2019-05-18 03:39:51 +00:00
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
receivedBytes += r;
|
|
|
|
transmittedBytes += t;
|
2019-05-18 03:39:51 +00:00
|
|
|
}
|
2021-08-25 21:47:51 +00:00
|
|
|
|
|
|
|
return {{receivedBytes, transmittedBytes}};
|
2019-05-18 03:39:51 +00:00
|
|
|
}
|
2018-08-09 14:38:24 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
waybar::modules::Network::Network(const std::string &id, const Json::Value &config)
|
2021-06-07 18:24:38 +00:00
|
|
|
: ALabel(config, "network", id, DEFAULT_FORMAT, 60),
|
2019-05-16 09:22:22 +00:00
|
|
|
ifid_(-1),
|
2019-05-16 10:22:08 +00:00
|
|
|
family_(config["family"] == "ipv6" ? AF_INET6 : AF_INET),
|
2020-09-21 08:56:40 +00:00
|
|
|
efd_(-1),
|
|
|
|
ev_fd_(-1),
|
2022-03-10 08:53:02 +00:00
|
|
|
want_route_dump_(false),
|
2021-05-15 15:38:00 +00:00
|
|
|
want_link_dump_(false),
|
2021-05-15 15:38:00 +00:00
|
|
|
want_addr_dump_(false),
|
2021-05-15 15:38:00 +00:00
|
|
|
dump_in_progress_(false),
|
2021-05-15 15:38:00 +00:00
|
|
|
cidr_(0),
|
2019-04-18 15:52:00 +00:00
|
|
|
signal_strength_dbm_(0),
|
2019-05-17 09:27:38 +00:00
|
|
|
signal_strength_(0),
|
2020-08-14 18:58:48 +00:00
|
|
|
#ifdef WANT_RFKILL
|
|
|
|
rfkill_{RFKILL_TYPE_WLAN},
|
|
|
|
#endif
|
2021-12-14 18:36:46 +00:00
|
|
|
frequency_(0.0) {
|
2021-05-15 15:38:00 +00:00
|
|
|
|
|
|
|
// Start with some "text" in the module's label_, update() will then
|
|
|
|
// update it. Since the text should be different, update() will be able
|
|
|
|
// to show or hide the event_box_. This is to work around the case where
|
|
|
|
// the module start with no text, but the the event_box_ is shown.
|
|
|
|
label_.set_markup("<s></s>");
|
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
auto bandwidth = readBandwidthUsage();
|
|
|
|
if (bandwidth.has_value()) {
|
|
|
|
bandwidth_down_total_ = (*bandwidth).first;
|
|
|
|
bandwidth_up_total_ = (*bandwidth).second;
|
2019-05-18 03:39:51 +00:00
|
|
|
} else {
|
|
|
|
bandwidth_down_total_ = 0;
|
|
|
|
bandwidth_up_total_ = 0;
|
|
|
|
}
|
|
|
|
|
2021-05-15 15:38:00 +00:00
|
|
|
if (!config_["interface"].isString()) {
|
2021-11-09 18:04:05 +00:00
|
|
|
// "interface" isn't configured, then try to guess the external
|
2021-05-15 15:38:00 +00:00
|
|
|
// interface currently used for internet.
|
|
|
|
want_route_dump_ = true;
|
|
|
|
} else {
|
2021-05-15 15:38:00 +00:00
|
|
|
// Look for an interface that match "interface"
|
2021-05-15 15:38:00 +00:00
|
|
|
// and then find the address associated with it.
|
2021-05-15 15:38:00 +00:00
|
|
|
want_link_dump_ = true;
|
2021-05-15 15:38:00 +00:00
|
|
|
want_addr_dump_ = true;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 10:13:36 +00:00
|
|
|
createEventSocket();
|
2019-05-28 09:08:48 +00:00
|
|
|
createInfoSocket();
|
2021-05-15 15:38:00 +00:00
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
dp.emit();
|
2021-05-15 15:38:00 +00:00
|
|
|
// Ask for a dump of interfaces and then addresses to populate our
|
|
|
|
// information. First the interface dump, and once done, the callback
|
|
|
|
// will be called again which will ask for addresses dump.
|
|
|
|
askForStateDump();
|
2018-11-16 09:02:12 +00:00
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
waybar::modules::Network::~Network() {
|
2020-09-21 08:56:40 +00:00
|
|
|
if (ev_fd_ > -1) {
|
|
|
|
close(ev_fd_);
|
|
|
|
}
|
|
|
|
if (efd_ > -1) {
|
|
|
|
close(efd_);
|
|
|
|
}
|
2019-05-16 09:22:22 +00:00
|
|
|
if (ev_sock_ != nullptr) {
|
|
|
|
nl_socket_drop_membership(ev_sock_, RTNLGRP_LINK);
|
2019-05-26 18:06:27 +00:00
|
|
|
if (family_ == AF_INET) {
|
|
|
|
nl_socket_drop_membership(ev_sock_, RTNLGRP_IPV4_IFADDR);
|
|
|
|
} else {
|
|
|
|
nl_socket_drop_membership(ev_sock_, RTNLGRP_IPV6_IFADDR);
|
|
|
|
}
|
2019-05-16 09:22:22 +00:00
|
|
|
nl_close(ev_sock_);
|
|
|
|
nl_socket_free(ev_sock_);
|
2018-12-26 10:13:36 +00:00
|
|
|
}
|
2019-05-16 09:22:22 +00:00
|
|
|
if (sock_ != nullptr) {
|
|
|
|
nl_close(sock_);
|
|
|
|
nl_socket_free(sock_);
|
2018-12-26 10:13:36 +00:00
|
|
|
}
|
2018-11-16 09:02:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:08:48 +00:00
|
|
|
void waybar::modules::Network::createEventSocket() {
|
2019-05-16 09:22:22 +00:00
|
|
|
ev_sock_ = nl_socket_alloc();
|
|
|
|
nl_socket_disable_seq_check(ev_sock_);
|
|
|
|
nl_socket_modify_cb(ev_sock_, NL_CB_VALID, NL_CB_CUSTOM, handleEvents, this);
|
2021-05-15 15:38:00 +00:00
|
|
|
nl_socket_modify_cb(ev_sock_, NL_CB_FINISH, NL_CB_CUSTOM, handleEventsDone, this);
|
2019-05-28 09:08:48 +00:00
|
|
|
auto groups = RTMGRP_LINK | (family_ == AF_INET ? RTMGRP_IPV4_IFADDR : RTMGRP_IPV6_IFADDR);
|
|
|
|
nl_join_groups(ev_sock_, groups); // Deprecated
|
2019-05-16 09:22:22 +00:00
|
|
|
if (nl_connect(ev_sock_, NETLINK_ROUTE) != 0) {
|
2019-03-03 20:35:35 +00:00
|
|
|
throw std::runtime_error("Can't connect network socket");
|
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
if (nl_socket_set_nonblocking(ev_sock_)) {
|
|
|
|
throw std::runtime_error("Can't set non-blocking on network socket");
|
|
|
|
}
|
2019-05-16 09:22:22 +00:00
|
|
|
nl_socket_add_membership(ev_sock_, RTNLGRP_LINK);
|
2019-05-26 18:06:27 +00:00
|
|
|
if (family_ == AF_INET) {
|
|
|
|
nl_socket_add_membership(ev_sock_, RTNLGRP_IPV4_IFADDR);
|
|
|
|
} else {
|
|
|
|
nl_socket_add_membership(ev_sock_, RTNLGRP_IPV6_IFADDR);
|
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
if (!config_["interface"].isString()) {
|
|
|
|
if (family_ == AF_INET) {
|
|
|
|
nl_socket_add_membership(ev_sock_, RTNLGRP_IPV4_ROUTE);
|
|
|
|
} else {
|
|
|
|
nl_socket_add_membership(ev_sock_, RTNLGRP_IPV6_ROUTE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 08:56:40 +00:00
|
|
|
efd_ = epoll_create1(EPOLL_CLOEXEC);
|
|
|
|
if (efd_ < 0) {
|
|
|
|
throw std::runtime_error("Can't create epoll");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ev_fd_ = eventfd(0, EFD_NONBLOCK);
|
|
|
|
struct epoll_event event;
|
|
|
|
memset(&event, 0, sizeof(event));
|
|
|
|
event.events = EPOLLIN | EPOLLET;
|
|
|
|
event.data.fd = ev_fd_;
|
|
|
|
if (epoll_ctl(efd_, EPOLL_CTL_ADD, ev_fd_, &event) == -1) {
|
|
|
|
throw std::runtime_error("Can't add epoll event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2022-04-06 06:37:19 +00:00
|
|
|
auto fd = nl_socket_get_fd(ev_sock_);
|
2020-09-21 08:56:40 +00:00
|
|
|
struct epoll_event event;
|
|
|
|
memset(&event, 0, sizeof(event));
|
|
|
|
event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
|
|
|
|
event.data.fd = fd;
|
|
|
|
if (epoll_ctl(efd_, EPOLL_CTL_ADD, fd, &event) == -1) {
|
|
|
|
throw std::runtime_error("Can't add epoll event");
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 10:13:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:08:48 +00:00
|
|
|
void waybar::modules::Network::createInfoSocket() {
|
2019-05-16 09:22:22 +00:00
|
|
|
sock_ = nl_socket_alloc();
|
|
|
|
if (genl_connect(sock_) != 0) {
|
2018-12-26 10:13:36 +00:00
|
|
|
throw std::runtime_error("Can't connect to netlink socket");
|
|
|
|
}
|
2019-05-16 09:22:22 +00:00
|
|
|
if (nl_socket_modify_cb(sock_, NL_CB_VALID, NL_CB_CUSTOM, handleScan, this) < 0) {
|
2018-12-26 10:13:36 +00:00
|
|
|
throw std::runtime_error("Can't set callback");
|
|
|
|
}
|
2019-05-16 09:22:22 +00:00
|
|
|
nl80211_id_ = genl_ctrl_resolve(sock_, "nl80211");
|
2018-12-26 10:13:36 +00:00
|
|
|
if (nl80211_id_ < 0) {
|
2020-07-31 06:15:16 +00:00
|
|
|
spdlog::warn("Can't resolve nl80211 interface");
|
2018-12-26 10:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
void waybar::modules::Network::worker() {
|
2020-01-26 04:34:31 +00:00
|
|
|
// update via here not working
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_timer_ = [this] {
|
2019-05-18 13:32:40 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (ifid_ > 0) {
|
|
|
|
getInfo();
|
|
|
|
dp.emit();
|
|
|
|
}
|
2018-11-16 09:02:12 +00:00
|
|
|
}
|
2018-12-26 10:13:36 +00:00
|
|
|
thread_timer_.sleep_for(interval_);
|
|
|
|
};
|
2020-08-14 18:58:48 +00:00
|
|
|
#ifdef WANT_RFKILL
|
2021-02-03 03:17:06 +00:00
|
|
|
rfkill_.on_update.connect([this](auto &) {
|
2021-02-03 04:56:00 +00:00
|
|
|
/* If we are here, it's likely that the network thread already holds the mutex and will be
|
|
|
|
* holding it for a next few seconds.
|
|
|
|
* Let's delegate the update to the timer thread instead of blocking the main thread.
|
|
|
|
*/
|
|
|
|
thread_timer_.wake_up();
|
2021-02-03 03:17:06 +00:00
|
|
|
});
|
2020-08-14 18:58:48 +00:00
|
|
|
#else
|
2021-02-03 03:17:06 +00:00
|
|
|
spdlog::warn("Waybar has been built without rfkill support.");
|
2020-08-14 18:58:48 +00:00
|
|
|
#endif
|
2020-09-21 08:56:40 +00:00
|
|
|
thread_ = [this] {
|
|
|
|
std::array<struct epoll_event, EPOLL_MAX> events{};
|
|
|
|
|
|
|
|
int ec = epoll_wait(efd_, events.data(), EPOLL_MAX, -1);
|
|
|
|
if (ec > 0) {
|
|
|
|
for (auto i = 0; i < ec; i++) {
|
2021-05-15 15:38:00 +00:00
|
|
|
if (events[i].data.fd == nl_socket_get_fd(ev_sock_)) {
|
|
|
|
int rc = 0;
|
|
|
|
// Read as many message as possible, until the socket blocks
|
|
|
|
while (true) {
|
|
|
|
errno = 0;
|
|
|
|
rc = nl_recvmsgs_default(ev_sock_);
|
|
|
|
if (rc == -NLE_AGAIN || errno == EAGAIN) {
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc < 0) {
|
|
|
|
spdlog::error("nl_recvmsgs_default error: {}", nl_geterror(-rc));
|
|
|
|
thread_.stop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-21 08:56:40 +00:00
|
|
|
thread_.stop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-08-19 11:39:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 07:49:56 +00:00
|
|
|
const std::string waybar::modules::Network::getNetworkState() const {
|
2020-01-19 23:35:37 +00:00
|
|
|
if (ifid_ == -1) {
|
2020-08-15 08:36:15 +00:00
|
|
|
#ifdef WANT_RFKILL
|
2022-04-06 06:37:19 +00:00
|
|
|
if (rfkill_.getState()) return "disabled";
|
2020-08-15 08:36:15 +00:00
|
|
|
#endif
|
2020-01-21 16:48:45 +00:00
|
|
|
return "disconnected";
|
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
if (!carrier_) return "disconnected";
|
2019-05-24 07:49:56 +00:00
|
|
|
if (ipaddr_.empty()) return "linked";
|
|
|
|
if (essid_.empty()) return "ethernet";
|
|
|
|
return "wifi";
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto waybar::modules::Network::update() -> void {
|
2019-05-18 13:32:40 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2022-04-06 06:37:19 +00:00
|
|
|
std::string tooltip_format;
|
2019-05-18 03:39:51 +00:00
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
auto bandwidth = readBandwidthUsage();
|
|
|
|
auto bandwidth_down = 0ull;
|
|
|
|
auto bandwidth_up = 0ull;
|
|
|
|
if (bandwidth.has_value()) {
|
|
|
|
auto down_octets = (*bandwidth).first;
|
|
|
|
auto up_octets = (*bandwidth).second;
|
|
|
|
|
|
|
|
bandwidth_down = down_octets - bandwidth_down_total_;
|
|
|
|
bandwidth_down_total_ = down_octets;
|
2019-05-18 03:39:51 +00:00
|
|
|
|
2021-08-25 21:47:51 +00:00
|
|
|
bandwidth_up = up_octets - bandwidth_up_total_;
|
|
|
|
bandwidth_up_total_ = up_octets;
|
2019-05-18 03:39:51 +00:00
|
|
|
}
|
2021-08-25 21:47:51 +00:00
|
|
|
|
2019-05-24 07:49:56 +00:00
|
|
|
if (!alt_) {
|
|
|
|
auto state = getNetworkState();
|
|
|
|
if (!state_.empty() && label_.get_style_context()->has_class(state_)) {
|
|
|
|
label_.get_style_context()->remove_class(state_);
|
2019-01-13 21:36:37 +00:00
|
|
|
}
|
2019-05-24 07:49:56 +00:00
|
|
|
if (config_["format-" + state].isString()) {
|
|
|
|
default_format_ = config_["format-" + state].asString();
|
2021-06-07 18:24:38 +00:00
|
|
|
} else if (config_["format"].isString()) {
|
|
|
|
default_format_ = config_["format"].asString();
|
|
|
|
} else {
|
|
|
|
default_format_ = DEFAULT_FORMAT;
|
2019-03-03 20:35:35 +00:00
|
|
|
}
|
2019-05-24 07:49:56 +00:00
|
|
|
if (config_["tooltip-format-" + state].isString()) {
|
|
|
|
tooltip_format = config_["tooltip-format-" + state].asString();
|
|
|
|
}
|
|
|
|
if (!label_.get_style_context()->has_class(state)) {
|
|
|
|
label_.get_style_context()->add_class(state);
|
2018-08-17 12:24:00 +00:00
|
|
|
}
|
2019-01-13 21:36:37 +00:00
|
|
|
format_ = default_format_;
|
2019-05-24 07:49:56 +00:00
|
|
|
state_ = state;
|
2019-01-13 21:36:37 +00:00
|
|
|
}
|
2019-05-03 02:19:47 +00:00
|
|
|
getState(signal_strength_);
|
2019-05-21 15:38:47 +00:00
|
|
|
|
|
|
|
auto text = fmt::format(
|
2022-04-06 06:37:19 +00:00
|
|
|
format_, fmt::arg("essid", essid_), fmt::arg("signaldBm", signal_strength_dbm_),
|
2019-05-21 15:38:47 +00:00
|
|
|
fmt::arg("signalStrength", signal_strength_),
|
2022-04-06 06:37:19 +00:00
|
|
|
fmt::arg("signalStrengthApp", signal_strength_app_), fmt::arg("ifname", ifname_),
|
|
|
|
fmt::arg("netmask", netmask_), fmt::arg("ipaddr", ipaddr_), fmt::arg("gwaddr", gwaddr_),
|
|
|
|
fmt::arg("cidr", cidr_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)),
|
2019-05-24 07:49:56 +00:00
|
|
|
fmt::arg("icon", getIcon(signal_strength_, state_)),
|
2019-05-21 15:38:47 +00:00
|
|
|
fmt::arg("bandwidthDownBits", pow_format(bandwidth_down * 8ull / interval_.count(), "b/s")),
|
|
|
|
fmt::arg("bandwidthUpBits", pow_format(bandwidth_up * 8ull / interval_.count(), "b/s")),
|
2022-06-14 07:17:40 +00:00
|
|
|
fmt::arg("bandwidthTotalBits",
|
|
|
|
pow_format((bandwidth_up + bandwidth_down) * 8ull / interval_.count(), "b/s")),
|
2019-05-21 15:38:47 +00:00
|
|
|
fmt::arg("bandwidthDownOctets", pow_format(bandwidth_down / interval_.count(), "o/s")),
|
2022-03-18 09:35:19 +00:00
|
|
|
fmt::arg("bandwidthUpOctets", pow_format(bandwidth_up / interval_.count(), "o/s")),
|
2022-06-14 07:17:40 +00:00
|
|
|
fmt::arg("bandwidthTotalOctets",
|
|
|
|
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "o/s")),
|
2022-03-18 09:35:19 +00:00
|
|
|
fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / interval_.count(), "B/s")),
|
2022-05-28 08:54:10 +00:00
|
|
|
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
|
2022-06-14 07:17:40 +00:00
|
|
|
fmt::arg("bandwidthTotalBytes",
|
|
|
|
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")));
|
2020-02-15 16:51:18 +00:00
|
|
|
if (text.compare(label_.get_label()) != 0) {
|
2019-05-18 13:32:40 +00:00
|
|
|
label_.set_markup(text);
|
2019-12-30 12:44:06 +00:00
|
|
|
if (text.empty()) {
|
|
|
|
event_box_.hide();
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
|
|
|
}
|
2019-05-18 13:32:40 +00:00
|
|
|
}
|
2019-03-03 20:35:35 +00:00
|
|
|
if (tooltipEnabled()) {
|
2019-05-18 13:32:40 +00:00
|
|
|
if (tooltip_format.empty() && config_["tooltip-format"].isString()) {
|
|
|
|
tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
}
|
2019-03-03 20:35:35 +00:00
|
|
|
if (!tooltip_format.empty()) {
|
2019-05-21 15:38:47 +00:00
|
|
|
auto tooltip_text = fmt::format(
|
2022-04-06 06:37:19 +00:00
|
|
|
tooltip_format, fmt::arg("essid", essid_), fmt::arg("signaldBm", signal_strength_dbm_),
|
2019-05-21 15:38:47 +00:00
|
|
|
fmt::arg("signalStrength", signal_strength_),
|
2022-04-06 06:37:19 +00:00
|
|
|
fmt::arg("signalStrengthApp", signal_strength_app_), fmt::arg("ifname", ifname_),
|
|
|
|
fmt::arg("netmask", netmask_), fmt::arg("ipaddr", ipaddr_), fmt::arg("gwaddr", gwaddr_),
|
|
|
|
fmt::arg("cidr", cidr_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)),
|
2019-05-24 07:49:56 +00:00
|
|
|
fmt::arg("icon", getIcon(signal_strength_, state_)),
|
2019-05-21 15:38:47 +00:00
|
|
|
fmt::arg("bandwidthDownBits",
|
|
|
|
pow_format(bandwidth_down * 8ull / interval_.count(), "b/s")),
|
|
|
|
fmt::arg("bandwidthUpBits", pow_format(bandwidth_up * 8ull / interval_.count(), "b/s")),
|
2022-06-14 07:17:40 +00:00
|
|
|
fmt::arg("bandwidthTotalBits",
|
|
|
|
pow_format((bandwidth_up + bandwidth_down) * 8ull / interval_.count(), "b/s")),
|
2019-05-21 15:38:47 +00:00
|
|
|
fmt::arg("bandwidthDownOctets", pow_format(bandwidth_down / interval_.count(), "o/s")),
|
2022-03-18 09:35:19 +00:00
|
|
|
fmt::arg("bandwidthUpOctets", pow_format(bandwidth_up / interval_.count(), "o/s")),
|
2022-06-14 07:17:40 +00:00
|
|
|
fmt::arg("bandwidthTotalOctets",
|
|
|
|
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "o/s")),
|
2022-03-18 09:35:19 +00:00
|
|
|
fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / interval_.count(), "B/s")),
|
2022-05-28 08:54:10 +00:00
|
|
|
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
|
2022-06-14 07:17:40 +00:00
|
|
|
fmt::arg("bandwidthTotalBytes",
|
|
|
|
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")));
|
2020-08-18 21:09:35 +00:00
|
|
|
if (label_.get_tooltip_text() != tooltip_text) {
|
2022-08-30 15:05:34 +00:00
|
|
|
label_.set_tooltip_markup(tooltip_text);
|
2019-05-18 13:32:40 +00:00
|
|
|
}
|
2019-05-18 13:45:18 +00:00
|
|
|
} else if (label_.get_tooltip_text() != text) {
|
2022-08-30 15:05:34 +00:00
|
|
|
label_.set_tooltip_markup(text);
|
2019-03-03 20:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 16:30:21 +00:00
|
|
|
|
|
|
|
// Call parent update
|
|
|
|
ALabel::update();
|
2018-08-09 14:38:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 15:38:00 +00:00
|
|
|
bool waybar::modules::Network::checkInterface(std::string name) {
|
2019-05-16 09:22:22 +00:00
|
|
|
if (config_["interface"].isString()) {
|
|
|
|
return config_["interface"].asString() == name ||
|
|
|
|
wildcardMatch(config_["interface"].asString(), name);
|
|
|
|
}
|
2019-05-29 14:17:40 +00:00
|
|
|
return false;
|
2019-05-16 09:22:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 15:38:47 +00:00
|
|
|
void waybar::modules::Network::clearIface() {
|
2021-05-15 15:38:00 +00:00
|
|
|
ifid_ = -1;
|
2021-06-05 14:44:32 +00:00
|
|
|
ifname_.clear();
|
2019-05-21 15:38:47 +00:00
|
|
|
essid_.clear();
|
|
|
|
ipaddr_.clear();
|
2021-09-09 18:05:18 +00:00
|
|
|
gwaddr_.clear();
|
2019-05-21 15:38:47 +00:00
|
|
|
netmask_.clear();
|
2021-05-15 15:38:00 +00:00
|
|
|
carrier_ = false;
|
2019-05-21 15:38:47 +00:00
|
|
|
cidr_ = 0;
|
|
|
|
signal_strength_dbm_ = 0;
|
|
|
|
signal_strength_ = 0;
|
2021-12-14 18:37:39 +00:00
|
|
|
signal_strength_app_.clear();
|
2021-12-14 18:36:46 +00:00
|
|
|
frequency_ = 0.0;
|
2019-05-21 15:38:47 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 09:41:43 +00:00
|
|
|
int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
|
2022-04-06 06:37:19 +00:00
|
|
|
auto net = static_cast<waybar::modules::Network *>(data);
|
2019-05-16 09:22:22 +00:00
|
|
|
std::lock_guard<std::mutex> lock(net->mutex_);
|
2022-04-06 06:37:19 +00:00
|
|
|
auto nh = nlmsg_hdr(msg);
|
|
|
|
bool is_del_event = false;
|
2021-05-15 15:38:00 +00:00
|
|
|
|
|
|
|
switch (nh->nlmsg_type) {
|
2022-04-06 06:37:19 +00:00
|
|
|
case RTM_DELLINK:
|
|
|
|
is_del_event = true;
|
|
|
|
case RTM_NEWLINK: {
|
|
|
|
struct ifinfomsg *ifi = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
|
|
|
|
ssize_t attrlen = IFLA_PAYLOAD(nh);
|
|
|
|
struct rtattr *ifla = IFLA_RTA(ifi);
|
|
|
|
const char *ifname = NULL;
|
|
|
|
size_t ifname_len = 0;
|
|
|
|
std::optional<bool> carrier;
|
|
|
|
|
|
|
|
if (net->ifid_ != -1 && ifi->ifi_index != net->ifid_) {
|
|
|
|
return NL_OK;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
// Check if the interface goes "down" and if we want to detect the
|
|
|
|
// external interface.
|
|
|
|
if (net->ifid_ != -1 && !(ifi->ifi_flags & IFF_UP) && !net->config_["interface"].isString()) {
|
|
|
|
// The current interface is now down, all the routes associated with
|
|
|
|
// it have been deleted, so start looking for a new default route.
|
|
|
|
spdlog::debug("network: if{} down", net->ifid_);
|
|
|
|
net->clearIface();
|
|
|
|
net->dp.emit();
|
|
|
|
net->want_route_dump_ = true;
|
|
|
|
net->askForStateDump();
|
|
|
|
return NL_OK;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
|
|
|
|
for (; RTA_OK(ifla, attrlen); ifla = RTA_NEXT(ifla, attrlen)) {
|
|
|
|
switch (ifla->rta_type) {
|
|
|
|
case IFLA_IFNAME:
|
|
|
|
ifname = static_cast<const char *>(RTA_DATA(ifla));
|
|
|
|
ifname_len = RTA_PAYLOAD(ifla) - 1; // minus \0
|
|
|
|
break;
|
|
|
|
case IFLA_CARRIER: {
|
|
|
|
carrier = *(char *)RTA_DATA(ifla) == 1;
|
|
|
|
break;
|
2021-05-26 17:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 17:28:49 +00:00
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
|
|
|
|
if (!is_del_event && ifi->ifi_index == net->ifid_) {
|
|
|
|
// Update interface information
|
|
|
|
if (net->ifname_.empty() && ifname != NULL) {
|
|
|
|
std::string new_ifname(ifname, ifname_len);
|
|
|
|
net->ifname_ = new_ifname;
|
|
|
|
}
|
2021-05-26 17:28:49 +00:00
|
|
|
if (carrier.has_value()) {
|
2022-04-06 06:37:19 +00:00
|
|
|
if (net->carrier_ != *carrier) {
|
|
|
|
if (*carrier) {
|
|
|
|
// Ask for WiFi information
|
|
|
|
net->thread_timer_.wake_up();
|
|
|
|
} else {
|
|
|
|
// clear state related to WiFi connection
|
|
|
|
net->essid_.clear();
|
|
|
|
net->signal_strength_dbm_ = 0;
|
|
|
|
net->signal_strength_ = 0;
|
|
|
|
net->signal_strength_app_.clear();
|
|
|
|
net->frequency_ = 0.0;
|
|
|
|
}
|
|
|
|
}
|
2021-05-26 17:28:49 +00:00
|
|
|
net->carrier_ = carrier.value();
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
} else if (!is_del_event && net->ifid_ == -1) {
|
|
|
|
// Checking if it's an interface we care about.
|
|
|
|
std::string new_ifname(ifname, ifname_len);
|
|
|
|
if (net->checkInterface(new_ifname)) {
|
|
|
|
spdlog::debug("network: selecting new interface {}/{}", new_ifname, ifi->ifi_index);
|
|
|
|
|
|
|
|
net->ifname_ = new_ifname;
|
|
|
|
net->ifid_ = ifi->ifi_index;
|
|
|
|
if (carrier.has_value()) {
|
|
|
|
net->carrier_ = carrier.value();
|
|
|
|
}
|
|
|
|
net->thread_timer_.wake_up();
|
|
|
|
/* An address for this new interface should be received via an
|
|
|
|
* RTM_NEWADDR event either because we ask for a dump of both links
|
|
|
|
* and addrs, or because this interface has just been created and
|
|
|
|
* the addr will be sent after the RTM_NEWLINK event.
|
|
|
|
* So we don't need to do anything. */
|
|
|
|
}
|
|
|
|
} else if (is_del_event && net->ifid_ >= 0) {
|
|
|
|
// Our interface has been deleted, start looking/waiting for one we care.
|
|
|
|
spdlog::debug("network: interface {}/{} deleted", net->ifname_, net->ifid_);
|
2021-05-15 15:38:00 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
net->clearIface();
|
|
|
|
net->dp.emit();
|
|
|
|
}
|
|
|
|
break;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
case RTM_DELADDR:
|
|
|
|
is_del_event = true;
|
|
|
|
case RTM_NEWADDR: {
|
|
|
|
struct ifaddrmsg *ifa = static_cast<struct ifaddrmsg *>(NLMSG_DATA(nh));
|
|
|
|
ssize_t attrlen = IFA_PAYLOAD(nh);
|
|
|
|
struct rtattr *ifa_rta = IFA_RTA(ifa);
|
2021-05-15 15:38:00 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
if ((int)ifa->ifa_index != net->ifid_) {
|
|
|
|
return NL_OK;
|
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
if (ifa->ifa_family != net->family_) {
|
|
|
|
return NL_OK;
|
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
// We ignore address mark as scope for the link or host,
|
|
|
|
// which should leave scope global addresses.
|
|
|
|
if (ifa->ifa_scope >= RT_SCOPE_LINK) {
|
|
|
|
return NL_OK;
|
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
for (; RTA_OK(ifa_rta, attrlen); ifa_rta = RTA_NEXT(ifa_rta, attrlen)) {
|
|
|
|
switch (ifa_rta->rta_type) {
|
|
|
|
case IFA_ADDRESS: {
|
|
|
|
char ipaddr[INET6_ADDRSTRLEN];
|
|
|
|
if (!is_del_event) {
|
|
|
|
net->ipaddr_ = inet_ntop(ifa->ifa_family, RTA_DATA(ifa_rta), ipaddr, sizeof(ipaddr));
|
|
|
|
net->cidr_ = ifa->ifa_prefixlen;
|
|
|
|
switch (ifa->ifa_family) {
|
|
|
|
case AF_INET: {
|
|
|
|
struct in_addr netmask;
|
|
|
|
netmask.s_addr = htonl(~0 << (32 - ifa->ifa_prefixlen));
|
|
|
|
net->netmask_ = inet_ntop(ifa->ifa_family, &netmask, ipaddr, sizeof(ipaddr));
|
|
|
|
}
|
|
|
|
case AF_INET6: {
|
|
|
|
struct in6_addr netmask;
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
int v = (i + 1) * 8 - ifa->ifa_prefixlen;
|
|
|
|
if (v < 0) v = 0;
|
|
|
|
if (v > 8) v = 8;
|
|
|
|
netmask.s6_addr[i] = ~0 << v;
|
|
|
|
}
|
|
|
|
net->netmask_ = inet_ntop(ifa->ifa_family, &netmask, ipaddr, sizeof(ipaddr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spdlog::debug("network: {}, new addr {}/{}", net->ifname_, net->ipaddr_, net->cidr_);
|
|
|
|
} else {
|
|
|
|
net->ipaddr_.clear();
|
|
|
|
net->cidr_ = 0;
|
|
|
|
net->netmask_.clear();
|
|
|
|
spdlog::debug("network: {} addr deleted {}/{}", net->ifname_,
|
|
|
|
inet_ntop(ifa->ifa_family, RTA_DATA(ifa_rta), ipaddr, sizeof(ipaddr)),
|
|
|
|
ifa->ifa_prefixlen);
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
net->dp.emit();
|
|
|
|
break;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
break;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
char temp_gw_addr[INET6_ADDRSTRLEN];
|
|
|
|
case RTM_DELROUTE:
|
|
|
|
is_del_event = true;
|
|
|
|
case RTM_NEWROUTE: {
|
|
|
|
// Based on https://gist.github.com/Yawning/c70d804d4b8ae78cc698
|
|
|
|
// to find the interface used to reach the outside world
|
|
|
|
|
|
|
|
struct rtmsg *rtm = static_cast<struct rtmsg *>(NLMSG_DATA(nh));
|
|
|
|
ssize_t attrlen = RTM_PAYLOAD(nh);
|
|
|
|
struct rtattr *attr = RTM_RTA(rtm);
|
|
|
|
bool has_gateway = false;
|
|
|
|
bool has_destination = false;
|
|
|
|
int temp_idx = -1;
|
|
|
|
uint32_t priority = 0;
|
|
|
|
|
|
|
|
/* Find the message(s) concerting the main routing table, each message
|
|
|
|
* corresponds to a single routing table entry.
|
2021-05-15 15:38:00 +00:00
|
|
|
*/
|
2022-04-06 06:37:19 +00:00
|
|
|
if (rtm->rtm_table != RT_TABLE_MAIN) {
|
|
|
|
return NL_OK;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
/* Parse all the attributes for a single routing table entry. */
|
|
|
|
for (; RTA_OK(attr, attrlen); attr = RTA_NEXT(attr, attrlen)) {
|
|
|
|
/* Determine if this routing table entry corresponds to the default
|
|
|
|
* route by seeing if it has a gateway, and if a destination addr is
|
|
|
|
* set, that it is all 0s.
|
|
|
|
*/
|
|
|
|
switch (attr->rta_type) {
|
|
|
|
case RTA_GATEWAY:
|
|
|
|
/* The gateway of the route.
|
|
|
|
*
|
|
|
|
* If someone ever needs to figure out the gateway address as well,
|
|
|
|
* it's here as the attribute payload.
|
|
|
|
*/
|
|
|
|
inet_ntop(net->family_, RTA_DATA(attr), temp_gw_addr, sizeof(temp_gw_addr));
|
|
|
|
has_gateway = true;
|
|
|
|
break;
|
|
|
|
case RTA_DST: {
|
|
|
|
/* The destination address.
|
|
|
|
* Should be either missing, or maybe all 0s. Accept both.
|
|
|
|
*/
|
|
|
|
const uint32_t nr_zeroes = (net->family_ == AF_INET) ? 4 : 16;
|
|
|
|
unsigned char c = 0;
|
|
|
|
size_t dstlen = RTA_PAYLOAD(attr);
|
|
|
|
if (dstlen != nr_zeroes) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < dstlen; i += 1) {
|
|
|
|
c |= *((unsigned char *)RTA_DATA(attr) + i);
|
|
|
|
}
|
|
|
|
has_destination = (c == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RTA_OIF:
|
|
|
|
/* The output interface index. */
|
|
|
|
temp_idx = *static_cast<int *>(RTA_DATA(attr));
|
|
|
|
break;
|
|
|
|
case RTA_PRIORITY:
|
|
|
|
priority = *(uint32_t *)RTA_DATA(attr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
}
|
2021-05-30 14:56:56 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
// Check if we have a default route.
|
|
|
|
if (has_gateway && !has_destination && temp_idx != -1) {
|
|
|
|
// Check if this is the first default route we see, or if this new
|
|
|
|
// route have a higher priority.
|
|
|
|
if (!is_del_event && ((net->ifid_ == -1) || (priority < net->route_priority))) {
|
|
|
|
// Clear if's state for the case were there is a higher priority
|
|
|
|
// route on a different interface.
|
|
|
|
net->clearIface();
|
|
|
|
net->ifid_ = temp_idx;
|
|
|
|
net->route_priority = priority;
|
|
|
|
net->gwaddr_ = temp_gw_addr;
|
|
|
|
spdlog::debug("network: new default route via {} on if{} metric {}", temp_gw_addr,
|
|
|
|
temp_idx, priority);
|
|
|
|
|
|
|
|
/* Ask ifname associated with temp_idx as well as carrier status */
|
|
|
|
struct ifinfomsg ifinfo_hdr = {
|
|
|
|
.ifi_family = AF_UNSPEC,
|
|
|
|
.ifi_index = temp_idx,
|
|
|
|
};
|
|
|
|
int err;
|
|
|
|
err = nl_send_simple(net->ev_sock_, RTM_GETLINK, NLM_F_REQUEST, &ifinfo_hdr,
|
|
|
|
sizeof(ifinfo_hdr));
|
|
|
|
if (err < 0) {
|
|
|
|
spdlog::error("network: failed to ask link info: {}", err);
|
|
|
|
/* Ask for a dump of all links instead */
|
|
|
|
net->want_link_dump_ = true;
|
|
|
|
}
|
2021-05-30 14:56:56 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
/* Also ask for the address. Asking for a addresses of a specific
|
|
|
|
* interface doesn't seems to work so ask for a dump of all
|
|
|
|
* addresses. */
|
|
|
|
net->want_addr_dump_ = true;
|
|
|
|
net->askForStateDump();
|
|
|
|
net->thread_timer_.wake_up();
|
|
|
|
} else if (is_del_event && temp_idx == net->ifid_ && net->route_priority == priority) {
|
|
|
|
spdlog::debug("network: default route deleted {}/if{} metric {}", net->ifname_, temp_idx,
|
|
|
|
priority);
|
|
|
|
|
|
|
|
net->clearIface();
|
|
|
|
net->dp.emit();
|
|
|
|
/* Ask for a dump of all routes in case another one is already
|
|
|
|
* setup. If there's none, there'll be an event with new one
|
|
|
|
* later. */
|
|
|
|
net->want_route_dump_ = true;
|
|
|
|
net->askForStateDump();
|
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
break;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
2018-12-26 10:13:36 +00:00
|
|
|
}
|
2021-05-15 15:38:00 +00:00
|
|
|
|
|
|
|
return NL_OK;
|
2018-12-26 10:13:36 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 15:38:00 +00:00
|
|
|
void waybar::modules::Network::askForStateDump(void) {
|
|
|
|
/* We need to wait until the current dump is done before sending new
|
|
|
|
* messages. handleEventsDone() is called when a dump is done. */
|
2022-04-06 06:37:19 +00:00
|
|
|
if (dump_in_progress_) return;
|
2021-05-15 15:38:00 +00:00
|
|
|
|
|
|
|
struct rtgenmsg rt_hdr = {
|
2022-04-06 06:37:19 +00:00
|
|
|
.rtgen_family = AF_UNSPEC,
|
2021-05-15 15:38:00 +00:00
|
|
|
};
|
|
|
|
|
2021-05-15 15:38:00 +00:00
|
|
|
if (want_route_dump_) {
|
|
|
|
rt_hdr.rtgen_family = family_;
|
2022-04-06 06:37:19 +00:00
|
|
|
nl_send_simple(ev_sock_, RTM_GETROUTE, NLM_F_DUMP, &rt_hdr, sizeof(rt_hdr));
|
2021-05-15 15:38:00 +00:00
|
|
|
want_route_dump_ = false;
|
|
|
|
dump_in_progress_ = true;
|
|
|
|
|
|
|
|
} else if (want_link_dump_) {
|
2022-04-06 06:37:19 +00:00
|
|
|
nl_send_simple(ev_sock_, RTM_GETLINK, NLM_F_DUMP, &rt_hdr, sizeof(rt_hdr));
|
2021-05-15 15:38:00 +00:00
|
|
|
want_link_dump_ = false;
|
|
|
|
dump_in_progress_ = true;
|
|
|
|
|
2021-05-15 15:38:00 +00:00
|
|
|
} else if (want_addr_dump_) {
|
|
|
|
rt_hdr.rtgen_family = family_;
|
2022-04-06 06:37:19 +00:00
|
|
|
nl_send_simple(ev_sock_, RTM_GETADDR, NLM_F_DUMP, &rt_hdr, sizeof(rt_hdr));
|
2021-05-15 15:38:00 +00:00
|
|
|
want_addr_dump_ = false;
|
|
|
|
dump_in_progress_ = true;
|
2021-05-15 15:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int waybar::modules::Network::handleEventsDone(struct nl_msg *msg, void *data) {
|
|
|
|
auto net = static_cast<waybar::modules::Network *>(data);
|
|
|
|
net->dump_in_progress_ = false;
|
|
|
|
net->askForStateDump();
|
|
|
|
return NL_OK;
|
|
|
|
}
|
|
|
|
|
2018-12-26 10:13:36 +00:00
|
|
|
int waybar::modules::Network::handleScan(struct nl_msg *msg, void *data) {
|
2022-04-06 06:37:19 +00:00
|
|
|
auto net = static_cast<waybar::modules::Network *>(data);
|
|
|
|
auto gnlh = static_cast<genlmsghdr *>(nlmsg_data(nlmsg_hdr(msg)));
|
|
|
|
struct nlattr *tb[NL80211_ATTR_MAX + 1];
|
|
|
|
struct nlattr *bss[NL80211_BSS_MAX + 1];
|
2018-08-24 13:32:06 +00:00
|
|
|
struct nla_policy bss_policy[NL80211_BSS_MAX + 1]{};
|
|
|
|
bss_policy[NL80211_BSS_TSF].type = NLA_U64;
|
|
|
|
bss_policy[NL80211_BSS_FREQUENCY].type = NLA_U32;
|
|
|
|
bss_policy[NL80211_BSS_BSSID].type = NLA_UNSPEC;
|
|
|
|
bss_policy[NL80211_BSS_BEACON_INTERVAL].type = NLA_U16;
|
|
|
|
bss_policy[NL80211_BSS_CAPABILITY].type = NLA_U16;
|
|
|
|
bss_policy[NL80211_BSS_INFORMATION_ELEMENTS].type = NLA_UNSPEC;
|
|
|
|
bss_policy[NL80211_BSS_SIGNAL_MBM].type = NLA_U32;
|
|
|
|
bss_policy[NL80211_BSS_SIGNAL_UNSPEC].type = NLA_U8;
|
|
|
|
bss_policy[NL80211_BSS_STATUS].type = NLA_U32;
|
2018-08-09 14:38:24 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0),
|
|
|
|
nullptr) < 0) {
|
2018-08-24 13:32:06 +00:00
|
|
|
return NL_SKIP;
|
|
|
|
}
|
|
|
|
if (tb[NL80211_ATTR_BSS] == nullptr) {
|
2018-08-09 14:38:24 +00:00
|
|
|
return NL_SKIP;
|
2018-08-24 13:32:06 +00:00
|
|
|
}
|
|
|
|
if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy) != 0) {
|
|
|
|
return NL_SKIP;
|
|
|
|
}
|
|
|
|
if (!net->associatedOrJoined(bss)) {
|
|
|
|
return NL_SKIP;
|
|
|
|
}
|
|
|
|
net->parseEssid(bss);
|
|
|
|
net->parseSignal(bss);
|
2019-05-17 09:27:38 +00:00
|
|
|
net->parseFreq(bss);
|
2019-05-28 09:08:48 +00:00
|
|
|
return NL_OK;
|
2018-08-09 14:38:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
void waybar::modules::Network::parseEssid(struct nlattr **bss) {
|
2018-08-09 14:38:24 +00:00
|
|
|
if (bss[NL80211_BSS_INFORMATION_ELEMENTS] != nullptr) {
|
2022-04-06 06:37:19 +00:00
|
|
|
auto ies = static_cast<char *>(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]));
|
|
|
|
auto ies_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
|
2018-08-09 14:38:24 +00:00
|
|
|
const auto hdr_len = 2;
|
|
|
|
while (ies_len > hdr_len && ies[0] != 0) {
|
|
|
|
ies_len -= ies[1] + hdr_len;
|
|
|
|
ies += ies[1] + hdr_len;
|
|
|
|
}
|
|
|
|
if (ies_len > hdr_len && ies_len > ies[1] + hdr_len) {
|
2022-04-06 06:37:19 +00:00
|
|
|
auto essid_begin = ies + hdr_len;
|
|
|
|
auto essid_end = essid_begin + ies[1];
|
2019-03-23 18:48:26 +00:00
|
|
|
std::string essid_raw;
|
|
|
|
std::copy(essid_begin, essid_end, std::back_inserter(essid_raw));
|
|
|
|
essid_ = Glib::Markup::escape_text(essid_raw);
|
2018-08-09 14:38:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 12:29:41 +00:00
|
|
|
void waybar::modules::Network::parseSignal(struct nlattr **bss) {
|
2018-08-24 13:32:06 +00:00
|
|
|
if (bss[NL80211_BSS_SIGNAL_MBM] != nullptr) {
|
2018-12-26 10:13:36 +00:00
|
|
|
// signalstrength in dBm from mBm
|
2018-12-27 10:41:43 +00:00
|
|
|
signal_strength_dbm_ = nla_get_s32(bss[NL80211_BSS_SIGNAL_MBM]) / 100;
|
2021-12-02 01:24:35 +00:00
|
|
|
// WiFi-hardware usually operates in the range -90 to -30dBm.
|
2018-08-10 08:45:13 +00:00
|
|
|
|
2021-12-14 18:34:15 +00:00
|
|
|
// If a signal is too strong, it can overwhelm receiving circuity that is designed
|
|
|
|
// to pick up and process a certain signal level. The following percentage is scaled to
|
|
|
|
// punish signals that are too strong (>= -45dBm) or too weak (<= -45 dBm).
|
|
|
|
const int hardwareOptimum = -45;
|
2018-08-24 13:32:06 +00:00
|
|
|
const int hardwareMin = -90;
|
2019-09-19 21:07:38 +00:00
|
|
|
const int strength =
|
2022-04-06 06:37:19 +00:00
|
|
|
100 -
|
|
|
|
((abs(signal_strength_dbm_ - hardwareOptimum) / double{hardwareOptimum - hardwareMin}) *
|
|
|
|
100);
|
|
|
|
signal_strength_ = std::clamp(strength, 0, 100);
|
2021-12-14 18:37:39 +00:00
|
|
|
|
|
|
|
if (signal_strength_dbm_ >= -50) {
|
|
|
|
signal_strength_app_ = "Great Connectivity";
|
|
|
|
} else if (signal_strength_dbm_ >= -60) {
|
|
|
|
signal_strength_app_ = "Good Connectivity";
|
|
|
|
} else if (signal_strength_dbm_ >= -67) {
|
|
|
|
signal_strength_app_ = "Streaming";
|
|
|
|
} else if (signal_strength_dbm_ >= -70) {
|
|
|
|
signal_strength_app_ = "Web Surfing";
|
|
|
|
} else if (signal_strength_dbm_ >= -80) {
|
|
|
|
signal_strength_app_ = "Basic Connectivity";
|
|
|
|
} else {
|
|
|
|
signal_strength_app_ = "Poor Connectivity";
|
|
|
|
}
|
2018-08-09 20:05:15 +00:00
|
|
|
}
|
2018-12-26 10:13:36 +00:00
|
|
|
if (bss[NL80211_BSS_SIGNAL_UNSPEC] != nullptr) {
|
|
|
|
signal_strength_ = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
|
|
|
|
}
|
2018-08-24 13:32:06 +00:00
|
|
|
}
|
2018-08-09 20:05:15 +00:00
|
|
|
|
2019-05-17 09:27:38 +00:00
|
|
|
void waybar::modules::Network::parseFreq(struct nlattr **bss) {
|
|
|
|
if (bss[NL80211_BSS_FREQUENCY] != nullptr) {
|
2021-12-14 18:36:46 +00:00
|
|
|
// in GHz
|
2022-04-06 06:37:19 +00:00
|
|
|
frequency_ = (double)nla_get_u32(bss[NL80211_BSS_FREQUENCY]) / 1000;
|
2019-05-17 09:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
bool waybar::modules::Network::associatedOrJoined(struct nlattr **bss) {
|
2018-08-24 13:32:06 +00:00
|
|
|
if (bss[NL80211_BSS_STATUS] == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto status = nla_get_u32(bss[NL80211_BSS_STATUS]);
|
|
|
|
switch (status) {
|
|
|
|
case NL80211_BSS_STATUS_ASSOCIATED:
|
|
|
|
case NL80211_BSS_STATUS_IBSS_JOINED:
|
|
|
|
case NL80211_BSS_STATUS_AUTHENTICATED:
|
|
|
|
return true;
|
|
|
|
default:
|
2018-08-09 14:38:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-24 13:32:06 +00:00
|
|
|
}
|
2018-08-09 14:38:24 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto waybar::modules::Network::getInfo() -> void {
|
|
|
|
struct nl_msg *nl_msg = nlmsg_alloc();
|
2018-08-24 13:32:06 +00:00
|
|
|
if (nl_msg == nullptr) {
|
2018-08-09 14:38:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
if (genlmsg_put(nl_msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id_, 0, NLM_F_DUMP,
|
|
|
|
NL80211_CMD_GET_SCAN, 0) == nullptr ||
|
2019-04-18 15:52:00 +00:00
|
|
|
nla_put_u32(nl_msg, NL80211_ATTR_IFINDEX, ifid_) < 0) {
|
2018-12-28 16:09:25 +00:00
|
|
|
nlmsg_free(nl_msg);
|
|
|
|
return;
|
2018-08-09 14:38:24 +00:00
|
|
|
}
|
2019-05-16 09:22:22 +00:00
|
|
|
nl_send_sync(sock_, nl_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://gist.github.com/rressi/92af77630faf055934c723ce93ae2495
|
2019-05-22 20:20:50 +00:00
|
|
|
bool waybar::modules::Network::wildcardMatch(const std::string &pattern,
|
|
|
|
const std::string &text) const {
|
2019-05-16 09:22:22 +00:00
|
|
|
auto P = int(pattern.size());
|
|
|
|
auto T = int(text.size());
|
|
|
|
|
|
|
|
auto p = 0, fallback_p = -1;
|
|
|
|
auto t = 0, fallback_t = -1;
|
|
|
|
|
|
|
|
while (t < T) {
|
|
|
|
// Wildcard match:
|
|
|
|
if (p < P && pattern[p] == '*') {
|
|
|
|
fallback_p = p++; // starting point after failures
|
|
|
|
fallback_t = t; // starting point after failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple match:
|
|
|
|
else if (p < P && (pattern[p] == '?' || pattern[p] == text[t])) {
|
|
|
|
p++;
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failure, fall back just after last matched '*':
|
|
|
|
else if (fallback_p >= 0) {
|
|
|
|
p = fallback_p + 1; // position just after last matched '*"
|
|
|
|
t = ++fallback_t; // re-try to match text from here
|
|
|
|
}
|
|
|
|
|
|
|
|
// There were no '*' before, so we fail here:
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume all '*' at the end of pattern:
|
|
|
|
while (p < P && pattern[p] == '*') p++;
|
|
|
|
|
|
|
|
return p == P;
|
2018-08-09 14:38:24 +00:00
|
|
|
}
|