From 322b3c0624028d059613b557a6298001939e4ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Mon, 31 Jan 2022 14:08:18 +0100 Subject: [PATCH] ipv6 address added --- src/utils/local_ip.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/utils/local_ip.rs b/src/utils/local_ip.rs index 6df7016..07f5c19 100644 --- a/src/utils/local_ip.rs +++ b/src/utils/local_ip.rs @@ -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) }