fix(network): display of IPv6 address
This commit is contained in:
parent
c0d4867421
commit
1145788ab3
|
@ -439,7 +439,6 @@ out:
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::Network::getInterfaceAddress() {
|
void waybar::modules::Network::getInterfaceAddress() {
|
||||||
unsigned int cidrRaw;
|
|
||||||
struct ifaddrs *ifaddr, *ifa;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
cidr_ = 0;
|
cidr_ = 0;
|
||||||
int success = getifaddrs(&ifaddr);
|
int success = getifaddrs(&ifaddr);
|
||||||
|
@ -451,18 +450,34 @@ void waybar::modules::Network::getInterfaceAddress() {
|
||||||
if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family == family_ &&
|
if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family == family_ &&
|
||||||
ifa->ifa_name == ifname_) {
|
ifa->ifa_name == ifname_) {
|
||||||
char ipaddr[INET6_ADDRSTRLEN];
|
char ipaddr[INET6_ADDRSTRLEN];
|
||||||
ipaddr_ = inet_ntop(family_,
|
|
||||||
&reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr)->sin_addr,
|
|
||||||
ipaddr,
|
|
||||||
INET6_ADDRSTRLEN);
|
|
||||||
char netmask[INET6_ADDRSTRLEN];
|
char netmask[INET6_ADDRSTRLEN];
|
||||||
auto net_addr = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_netmask);
|
|
||||||
netmask_ = inet_ntop(family_, &net_addr->sin_addr, netmask, INET6_ADDRSTRLEN);
|
|
||||||
cidrRaw = net_addr->sin_addr.s_addr;
|
|
||||||
unsigned int cidr = 0;
|
unsigned int cidr = 0;
|
||||||
while (cidrRaw) {
|
if (family_ == AF_INET) {
|
||||||
cidr += cidrRaw & 1;
|
ipaddr_ = inet_ntop(AF_INET,
|
||||||
cidrRaw >>= 1;
|
&reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr)->sin_addr,
|
||||||
|
ipaddr,
|
||||||
|
INET_ADDRSTRLEN);
|
||||||
|
auto net_addr = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_netmask);
|
||||||
|
netmask_ = inet_ntop(AF_INET, &net_addr->sin_addr, netmask, INET_ADDRSTRLEN);
|
||||||
|
unsigned int cidrRaw = net_addr->sin_addr.s_addr;
|
||||||
|
while (cidrRaw) {
|
||||||
|
cidr += cidrRaw & 1;
|
||||||
|
cidrRaw >>= 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ipaddr_ = inet_ntop(AF_INET6,
|
||||||
|
&reinterpret_cast<struct sockaddr_in6 *>(ifa->ifa_addr)->sin6_addr,
|
||||||
|
ipaddr,
|
||||||
|
INET6_ADDRSTRLEN);
|
||||||
|
auto net_addr = reinterpret_cast<struct sockaddr_in6 *>(ifa->ifa_netmask);
|
||||||
|
netmask_ = inet_ntop(AF_INET6, &net_addr->sin6_addr, netmask, INET6_ADDRSTRLEN);
|
||||||
|
for (size_t i = 0; i < sizeof(net_addr->sin6_addr.s6_addr); ++i) {
|
||||||
|
unsigned char cidrRaw = net_addr->sin6_addr.s6_addr[i];
|
||||||
|
while (cidrRaw) {
|
||||||
|
cidr += cidrRaw & 1;
|
||||||
|
cidrRaw >>= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cidr_ = cidr;
|
cidr_ = cidr;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue