refactor(Network): clean nl socket
This commit is contained in:
parent
0dba3abc1d
commit
0ad2bc7516
|
@ -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_;
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
|
@ -348,31 +368,16 @@ bool waybar::modules::Network::associatedOrJoined(struct nlattr** bss)
|
|||
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue