use std::{io, net::Ipv4Addr};
pub fn ifname_to_v4_iface(name: &str) -> io::Result<Option<Ipv4Addr>> {
let idx = super::name_to_idx::ifname_to_index(name)?;
let iface = super::interface_by_index(idx)?;
match iface {
Some(iface) => {
let addrs = iface.ipv4_addrs_by_filter(|ip| !ip.is_link_local())?;
Ok(addrs.into_iter().next().map(|net| net.addr()))
}
None => Ok(None),
}
}
pub fn ifname_to_v6_iface(name: &str) -> io::Result<Option<u32>> {
super::name_to_idx::ifname_to_index(name).map(|idx| (idx != 0).then_some(idx))
}
pub fn ifname_to_iface(name: &str) -> io::Result<(Option<Ipv4Addr>, Option<u32>)> {
let idx = super::name_to_idx::ifname_to_index(name)?;
let v6_iface = (idx != 0).then_some(idx);
let iface = super::interface_by_index(idx)?;
match iface {
Some(iface) => {
let addrs = iface.ipv4_addrs_by_filter(|ip| !ip.is_link_local())?;
let v4_iface = addrs.into_iter().next().map(|net| net.addr());
Ok((v4_iface, v6_iface))
}
None => Ok((None, v6_iface)),
}
}