From 21ac10fbc3d19cbdc01cbd01afce47e05dee47b4 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Thu, 14 Sep 2023 15:56:03 +0200 Subject: [PATCH] Return error when requested network interface not found in multicast (#549) * Return error when network interface not found * Fix cargo clippy --- commons/zenoh-util/src/std_only/net/mod.rs | 34 ++++++++++++------- .../zenoh-link-udp/src/multicast.rs | 14 ++------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/commons/zenoh-util/src/std_only/net/mod.rs b/commons/zenoh-util/src/std_only/net/mod.rs index 8ca2482681..d8b2c3acc8 100644 --- a/commons/zenoh-util/src/std_only/net/mod.rs +++ b/commons/zenoh-util/src/std_only/net/mod.rs @@ -15,6 +15,8 @@ use async_std::net::TcpStream; use std::net::{IpAddr, Ipv6Addr}; use std::time::Duration; use zenoh_core::zconfigurable; +#[cfg(unix)] +use zenoh_result::zerror; use zenoh_result::{bail, ZResult}; zconfigurable! { @@ -285,19 +287,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 +361,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 +408,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), }, }