diff --git a/commons/zenoh-util/src/std_only/net/mod.rs b/commons/zenoh-util/src/std_only/net/mod.rs index 8ca2482681..20dc52b735 100644 --- a/commons/zenoh-util/src/std_only/net/mod.rs +++ b/commons/zenoh-util/src/std_only/net/mod.rs @@ -15,7 +15,7 @@ use async_std::net::TcpStream; use std::net::{IpAddr, Ipv6Addr}; use std::time::Duration; use zenoh_core::zconfigurable; -use zenoh_result::{bail, ZResult}; +use zenoh_result::{bail, zerror, ZResult}; zconfigurable! { static ref WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE: u32 = 8192; @@ -285,19 +285,24 @@ pub fn get_unicast_addresses_of_multicast_interfaces() -> Vec { pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult> { #[cfg(unix)] { - let addrs = pnet_datalink::interfaces() + match pnet_datalink::interfaces() .into_iter() - .filter(|iface| iface.is_up() && iface.name == name) - .flat_map(|iface| { - iface + .find(|iface| iface.name == name) + { + Some(iface) => { + if !iface.is_up() { + bail!("Interface {name} is not up"); + } + let addrs = iface .ips .iter() .filter(|ip| !ip.ip().is_multicast()) .map(|x| x.ip()) - .collect::>() - }) - .collect(); - Ok(addrs) + .collect::>(); + Ok(addrs) + } + None => bail!("Interface {name} not found"), + } } #[cfg(windows)] @@ -354,13 +359,14 @@ pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult> { } } -pub fn get_index_of_interface(addr: IpAddr) -> ZResult> { +pub fn get_index_of_interface(addr: IpAddr) -> ZResult { #[cfg(unix)] { - Ok(pnet_datalink::interfaces() + pnet_datalink::interfaces() .iter() .find(|iface| iface.ips.iter().any(|ipnet| ipnet.ip() == addr)) - .map(|iface| iface.index)) + .map(|iface| iface.index) + .ok_or_else(|| zerror!("No interface found with address {addr}").into()) } #[cfg(windows)] { @@ -400,14 +406,14 @@ pub fn get_index_of_interface(addr: IpAddr) -> ZResult> { while let Some(ucast_addr) = next_ucast_addr { if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) { if ifaddr.ip() == addr { - return Ok(Some(iface.Ipv6IfIndex)); + return Ok(iface.Ipv6IfIndex); } } next_ucast_addr = ucast_addr.Next.as_ref(); } next_iface = iface.Next.as_ref(); } - Ok(None) + bail!("No interface found with address {addr}") } } } diff --git a/io/zenoh-links/zenoh-link-udp/src/multicast.rs b/io/zenoh-links/zenoh-link-udp/src/multicast.rs index 17669a842c..d5ab9b89ed 100644 --- a/io/zenoh-links/zenoh-link-udp/src/multicast.rs +++ b/io/zenoh-links/zenoh-link-udp/src/multicast.rs @@ -223,20 +223,10 @@ impl LinkManagerMulticastUdp { .map_err(|e| zerror!("{}: {}", mcast_addr, e))?; } IpAddr::V6(_) => match zenoh_util::net::get_index_of_interface(local_addr) { - Ok(Some(idx)) => ucast_sock + Ok(idx) => ucast_sock .set_multicast_if_v6(idx) .map_err(|e| zerror!("{}: {}", mcast_addr, e))?, - Ok(None) => bail!( - "{}: Unable to find index of network interface for local id address {}", - mcast_addr, - local_addr - ), - Err(e) => bail!( - "{}: Unable to find index of network interface for local id address {}: {}", - mcast_addr, - local_addr, - e - ), + Err(e) => bail!("{}: {}", mcast_addr, e), }, }