How to use addr #14
-
How can i use pub fn network_interfaces() -> Vec<NetworkInfo> {
let mut all_network_interfaces = Vec::new();
for network in network_interface::NetworkInterface::show().unwrap().iter() {
all_network_interfaces.push(NetworkInfo {
name: network.name.to_string(),
address: network.addr.unwrap().ip, // i want to return ip
})
}
all_network_interfaces
} |
Beta Was this translation helpful? Give feedback.
Answered by
EstebanBorai
Jul 30, 2022
Replies: 1 comment 1 reply
-
Hi @xhyrom! Thanks for opening this question. As I understand, you want to have the Network Interface's name and IP to build a your use network_interface::{Addr, NetworkInterface, NetworkInterfaceConfig};
use std::net::IpAddr;
struct NetworkInfo {
name: String,
addr: IpAddr,
}
fn main() {
let network_interfaces = NetworkInterface::show().unwrap();
let ifaces = network_interfaces.iter().filter_map(|netifa| {
if let Some(netifa_addr) = netifa.addr {
let addr: IpAddr = match netifa_addr {
Addr::V4(ip_addr) => IpAddr::V4(ip_addr.ip),
Addr::V6(ip_addr) => IpAddr::V6(ip_addr.ip),
};
return Some(NetworkInfo {
name: netifa.name.clone(),
addr,
});
}
None
}).collect::<Vec<NetworkInfo>>();
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
xhyrom
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @xhyrom! Thanks for opening this question.
As I understand, you want to have the Network Interface's name and IP to build a your
NetworkInfo
instance, I assume the type foraddress
to be an instance ofstd::net::IpAddr
. What do you think about this approach?