ipv6 address added

This commit is contained in:
Özgür Kesim 2022-01-31 14:08:18 +01:00
parent 3805929479
commit 322b3c0624

View File

@ -3,15 +3,25 @@ use crate::types::ThreadsData;
pub async fn get_local_ip() -> ThreadsData {
let addrs = nix::ifaddrs::getifaddrs().unwrap();
let mut ip = String::new();
let mut ip = String::new();
let mut ip6 = String::new();
let mut found4 = false;
let mut found6 = false;
for ifaddr in addrs {
match ifaddr.address {
Some(address) => {
if found4 && found6 { break; }
if ifaddr.interface_name == CONFIG.local_ip.interface {
match address.family() {
nix::sys::socket::AddressFamily::Inet => {
if found4 { continue; }
ip = address.to_string().split(':').next().unwrap().to_string();
break;
found4 = true;
}
nix::sys::socket::AddressFamily::Inet6 => {
if found6 { continue; }
ip6 = address.to_string().rsplit_once(':').unwrap().0.to_string();
found6 = true;
}
_ => continue,
};
@ -23,6 +33,9 @@ pub async fn get_local_ip() -> ThreadsData {
if ip.is_empty() {
ip = String::from("Error!")
}
let data = format!(" {} {} {}", CONFIG.local_ip.icon, ip, CONFIG.seperator);
if !ip6.is_empty() {
ip6 = format!(" {}", ip6)
}
let data = format!(" {} {}{} {}", CONFIG.local_ip.icon, ip, ip6, CONFIG.seperator);
ThreadsData::LocalIp(data)
}