refactor(Network): clean nl socket

This commit is contained in:
Alexis 2018-08-24 15:32:06 +02:00
parent 0dba3abc1d
commit 0ad2bc7516
2 changed files with 81 additions and 73 deletions

View File

@ -22,6 +22,7 @@ class Network : public ALabel {
static int scanCb(struct nl_msg*, void*);
void disconnected();
void initNL80211();
int getExternalInterface();
void parseEssid(struct nlattr**);
void parseSignal(struct nlattr**);
@ -33,6 +34,8 @@ class Network : public ALabel {
sa_family_t family_;
int sock_fd_;
struct sockaddr_nl nladdr_ = {0};
struct nl_sock* sk_ = nullptr;
int nl80211_id_;
std::string essid_;
std::string ifname_;

View File

@ -28,6 +28,7 @@ waybar::modules::Network::Network(const Json::Value& config)
ifname_ = ifname;
}
}
initNL80211();
label_.set_name("network");
// Trigger first values
getInfo();
@ -76,6 +77,7 @@ waybar::modules::Network::Network(const Json::Value& config)
waybar::modules::Network::~Network()
{
close(sock_fd_);
nl_socket_free(sk_);
}
auto waybar::modules::Network::update() -> void
@ -112,6 +114,24 @@ void waybar::modules::Network::disconnected()
ifid_ = -1;
}
void waybar::modules::Network::initNL80211()
{
sk_ = nl_socket_alloc();
if (genl_connect(sk_) != 0) {
nl_socket_free(sk_);
throw std::runtime_error("Can't connect to netlink socket");
}
if (nl_socket_modify_cb(sk_, NL_CB_VALID, NL_CB_CUSTOM, scanCb, this) < 0) {
nl_socket_free(sk_);
throw std::runtime_error("Can't connect to netlink socket");
}
nl80211_id_ = genl_ctrl_resolve(sk_, "nl80211");
if (nl80211_id_ < 0) {
nl_socket_free(sk_);
throw std::runtime_error("Can't resolve nl80211 interface");
}
}
// Based on https://gist.github.com/Yawning/c70d804d4b8ae78cc698
int waybar::modules::Network::getExternalInterface()
{
@ -328,7 +348,7 @@ void waybar::modules::Network::parseSignal(struct nlattr **bss) {
signal_strength_ = ((signal_strength_dbm_ - hardwareMin)
/ double{hardwareMax - hardwareMin}) * 100;
}
}
}
bool waybar::modules::Network::associatedOrJoined(struct nlattr** bss)
{
@ -344,35 +364,20 @@ bool waybar::modules::Network::associatedOrJoined(struct nlattr** bss)
default:
return false;
}
}
}
auto waybar::modules::Network::getInfo() -> void
{
struct nl_sock *sk = nl_socket_alloc();
if (genl_connect(sk) != 0) {
nl_socket_free(sk);
struct nl_msg* nl_msg = nlmsg_alloc();
if (nl_msg == nullptr) {
nl_socket_free(sk_);
return;
}
if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, scanCb, this) < 0) {
nl_socket_free(sk);
return;
}
const int nl80211_id = genl_ctrl_resolve(sk, "nl80211");
if (nl80211_id < 0) {
nl_socket_free(sk);
return;
}
struct nl_msg *msg = nlmsg_alloc();
if (msg == nullptr) {
nl_socket_free(sk);
return;
}
if (genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP,
if (genlmsg_put(nl_msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id_, 0, NLM_F_DUMP,
NL80211_CMD_GET_SCAN, 0) == nullptr
|| nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifid_) < 0) {
nlmsg_free(msg);
|| nla_put_u32(nl_msg, NL80211_ATTR_IFINDEX, ifid_) < 0) {
nlmsg_free(nl_msg);
return;
}
nl_send_sync(sk, msg);
nl_socket_free(sk);
nl_send_sync(sk_, nl_msg);
}