Skip to content

Commit 2fdfd10

Browse files
authored
Merge pull request #3959 from voiceroy/ip-address-display
Provide an option to show ipv4, ipv6 or both
2 parents 8476a7d + 5e4dac1 commit 2fdfd10

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

include/modules/network.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "util/rfkill.hpp"
1717
#endif
1818

19+
enum ip_addr_pref : uint8_t { IPV4, IPV6, IPV4_6 };
20+
1921
namespace waybar::modules {
2022

2123
class Network : public ALabel {
@@ -50,6 +52,7 @@ class Network : public ALabel {
5052
std::optional<std::pair<unsigned long long, unsigned long long>> readBandwidthUsage();
5153

5254
int ifid_;
55+
ip_addr_pref addr_pref_;
5356
struct sockaddr_nl nladdr_ = {0};
5457
struct nl_sock* sock_ = nullptr;
5558
struct nl_sock* ev_sock_ = nullptr;
@@ -73,9 +76,12 @@ class Network : public ALabel {
7376
bool carrier_;
7477
std::string ifname_;
7578
std::string ipaddr_;
79+
std::string ipaddr6_;
7680
std::string gwaddr_;
7781
std::string netmask_;
82+
std::string netmask6_;
7883
int cidr_;
84+
int cidr6_;
7985
int32_t signal_strength_dbm_;
8086
uint8_t signal_strength_;
8187
std::string signal_strength_app_;

man/waybar-network.5.scd

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Addressed by *network*
2424
*family*: ++
2525
typeof: string ++
2626
default: *ipv4* ++
27-
The address family that is used for the format replacement {ipaddr} and to determine if a network connection is present.
27+
The address family that is used for the format replacement {ipaddr} and to determine if a network connection is present. Set it to ipv4_6 to display both.
2828

2929
*format*: ++
3030
typeof: string ++
@@ -155,9 +155,13 @@ Addressed by *network*
155155

156156
*{gwaddr}*: The default gateway for the interface
157157

158-
*{netmask}*: The subnetmask corresponding to the IP.
158+
*{netmask}*: The subnetmask corresponding to the IP(V4).
159159

160-
*{cidr}*: The subnetmask corresponding to the IP in CIDR notation.
160+
*{netmask6}*: The subnetmask corresponding to the IP(V6).
161+
162+
*{cidr}*: The subnetmask corresponding to the IP(V4) in CIDR notation.
163+
164+
*{cidr6}*: The subnetmask corresponding to the IP(V6) in CIDR notation.
161165

162166
*{essid}*: Name (SSID) of the wireless network.
163167

src/modules/network.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ waybar::modules::Network::readBandwidthUsage() {
8080
waybar::modules::Network::Network(const std::string &id, const Json::Value &config)
8181
: ALabel(config, "network", id, DEFAULT_FORMAT, 60),
8282
ifid_(-1),
83+
addr_pref_(IPV4),
8384
efd_(-1),
8485
ev_fd_(-1),
8586
want_route_dump_(false),
@@ -88,6 +89,7 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
8889
dump_in_progress_(false),
8990
is_p2p_(false),
9091
cidr_(0),
92+
cidr6_(0),
9193
signal_strength_dbm_(0),
9294
signal_strength_(0),
9395
#ifdef WANT_RFKILL
@@ -101,6 +103,12 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
101103
// the module start with no text, but the event_box_ is shown.
102104
label_.set_markup("<s></s>");
103105

106+
if (config_["family"] == "ipv6") {
107+
addr_pref_ = IPV6;
108+
} else if (config["family"] == "ipv4_6") {
109+
addr_pref_ = IPV4_6;
110+
}
111+
104112
auto bandwidth = readBandwidthUsage();
105113
if (bandwidth.has_value()) {
106114
bandwidth_down_total_ = (*bandwidth).first;
@@ -270,7 +278,7 @@ const std::string waybar::modules::Network::getNetworkState() const {
270278
return "disconnected";
271279
}
272280
if (!carrier_) return "disconnected";
273-
if (ipaddr_.empty()) return "linked";
281+
if (ipaddr_.empty() && ipaddr6_.empty()) return "linked";
274282
if (essid_.empty()) return "ethernet";
275283
return "wifi";
276284
}
@@ -316,12 +324,24 @@ auto waybar::modules::Network::update() -> void {
316324
}
317325
getState(signal_strength_);
318326

327+
std::string final_ipaddr_;
328+
if (addr_pref_ == ip_addr_pref::IPV4) {
329+
final_ipaddr_ = ipaddr_;
330+
} else if (addr_pref_ == ip_addr_pref::IPV6) {
331+
final_ipaddr_ = ipaddr6_;
332+
} else if (addr_pref_ == ip_addr_pref::IPV4_6) {
333+
final_ipaddr_ = ipaddr_;
334+
final_ipaddr_ += '\n';
335+
final_ipaddr_ += ipaddr6_;
336+
}
337+
319338
auto text = fmt::format(
320339
fmt::runtime(format_), fmt::arg("essid", essid_), fmt::arg("bssid", bssid_),
321340
fmt::arg("signaldBm", signal_strength_dbm_), fmt::arg("signalStrength", signal_strength_),
322341
fmt::arg("signalStrengthApp", signal_strength_app_), fmt::arg("ifname", ifname_),
323-
fmt::arg("netmask", netmask_), fmt::arg("ipaddr", ipaddr_), fmt::arg("gwaddr", gwaddr_),
324-
fmt::arg("cidr", cidr_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)),
342+
fmt::arg("netmask", netmask_), fmt::arg("netmask6", netmask6_),
343+
fmt::arg("ipaddr", final_ipaddr_), fmt::arg("gwaddr", gwaddr_), fmt::arg("cidr", cidr_),
344+
fmt::arg("cidr6", cidr6_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)),
325345
fmt::arg("icon", getIcon(signal_strength_, state_)),
326346
fmt::arg("bandwidthDownBits", pow_format(bandwidth_down * 8ull / interval_.count(), "b/s")),
327347
fmt::arg("bandwidthUpBits", pow_format(bandwidth_up * 8ull / interval_.count(), "b/s")),
@@ -352,8 +372,9 @@ auto waybar::modules::Network::update() -> void {
352372
fmt::runtime(tooltip_format), fmt::arg("essid", essid_), fmt::arg("bssid", bssid_),
353373
fmt::arg("signaldBm", signal_strength_dbm_), fmt::arg("signalStrength", signal_strength_),
354374
fmt::arg("signalStrengthApp", signal_strength_app_), fmt::arg("ifname", ifname_),
355-
fmt::arg("netmask", netmask_), fmt::arg("ipaddr", ipaddr_), fmt::arg("gwaddr", gwaddr_),
356-
fmt::arg("cidr", cidr_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)),
375+
fmt::arg("netmask", netmask_), fmt::arg("netmask6", netmask6_),
376+
fmt::arg("ipaddr", final_ipaddr_), fmt::arg("gwaddr", gwaddr_), fmt::arg("cidr", cidr_),
377+
fmt::arg("cidr6", cidr6_), fmt::arg("frequency", fmt::format("{:.1f}", frequency_)),
357378
fmt::arg("icon", getIcon(signal_strength_, state_)),
358379
fmt::arg("bandwidthDownBits",
359380
pow_format(bandwidth_down * 8ull / interval_.count(), "b/s")),
@@ -394,10 +415,13 @@ void waybar::modules::Network::clearIface() {
394415
essid_.clear();
395416
bssid_.clear();
396417
ipaddr_.clear();
418+
ipaddr6_.clear();
397419
gwaddr_.clear();
398420
netmask_.clear();
421+
netmask6_.clear();
399422
carrier_ = false;
400423
cidr_ = 0;
424+
cidr6_ = 0;
401425
signal_strength_dbm_ = 0;
402426
signal_strength_ = 0;
403427
signal_strength_app_.clear();
@@ -521,38 +545,52 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
521545
if (ifa->ifa_scope >= RT_SCOPE_LINK) {
522546
return NL_OK;
523547
}
524-
525548
for (; RTA_OK(ifa_rta, attrlen); ifa_rta = RTA_NEXT(ifa_rta, attrlen)) {
526549
switch (ifa_rta->rta_type) {
527550
case IFA_ADDRESS:
528551
if (net->is_p2p_) continue;
529552
case IFA_LOCAL:
530553
char ipaddr[INET6_ADDRSTRLEN];
531554
if (!is_del_event) {
532-
net->ipaddr_ = inet_ntop(ifa->ifa_family, RTA_DATA(ifa_rta), ipaddr, sizeof(ipaddr));
533-
net->cidr_ = ifa->ifa_prefixlen;
555+
if ((net->addr_pref_ == ip_addr_pref::IPV4 ||
556+
net->addr_pref_ == ip_addr_pref::IPV4_6) &&
557+
net->cidr_ == 0 && ifa->ifa_family == AF_INET) {
558+
net->ipaddr_ =
559+
inet_ntop(ifa->ifa_family, RTA_DATA(ifa_rta), ipaddr, sizeof(ipaddr));
560+
net->cidr_ = ifa->ifa_prefixlen;
561+
} else if ((net->addr_pref_ == ip_addr_pref::IPV6 ||
562+
net->addr_pref_ == ip_addr_pref::IPV4_6) &&
563+
net->cidr6_ == 0 && ifa->ifa_family == AF_INET6) {
564+
net->ipaddr6_ =
565+
inet_ntop(ifa->ifa_family, RTA_DATA(ifa_rta), ipaddr, sizeof(ipaddr));
566+
net->cidr6_ = ifa->ifa_prefixlen;
567+
}
568+
534569
switch (ifa->ifa_family) {
535570
case AF_INET: {
536571
struct in_addr netmask;
537572
netmask.s_addr = htonl(~0 << (32 - ifa->ifa_prefixlen));
538573
net->netmask_ = inet_ntop(ifa->ifa_family, &netmask, ipaddr, sizeof(ipaddr));
539574
}
540575
case AF_INET6: {
541-
struct in6_addr netmask;
576+
struct in6_addr netmask6;
542577
for (int i = 0; i < 16; i++) {
543578
int v = (i + 1) * 8 - ifa->ifa_prefixlen;
544579
if (v < 0) v = 0;
545580
if (v > 8) v = 8;
546-
netmask.s6_addr[i] = ~0 << v;
581+
netmask6.s6_addr[i] = ~0 << v;
547582
}
548-
net->netmask_ = inet_ntop(ifa->ifa_family, &netmask, ipaddr, sizeof(ipaddr));
583+
net->netmask6_ = inet_ntop(ifa->ifa_family, &netmask6, ipaddr, sizeof(ipaddr));
549584
}
550585
}
551586
spdlog::debug("network: {}, new addr {}/{}", net->ifname_, net->ipaddr_, net->cidr_);
552587
} else {
553588
net->ipaddr_.clear();
589+
net->ipaddr6_.clear();
554590
net->cidr_ = 0;
591+
net->cidr6_ = 0;
555592
net->netmask_.clear();
593+
net->netmask6_.clear();
556594
spdlog::debug("network: {} addr deleted {}/{}", net->ifname_,
557595
inet_ntop(ifa->ifa_family, RTA_DATA(ifa_rta), ipaddr, sizeof(ipaddr)),
558596
ifa->ifa_prefixlen);

0 commit comments

Comments
 (0)