Skip to content

Commit

Permalink
Return error when requested network interface not found in multicast (#…
Browse files Browse the repository at this point in the history
…549)

* Return error when network interface not found

* Fix cargo clippy
  • Loading branch information
Mallets authored Sep 14, 2023
1 parent dcefe81 commit 21ac10f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
34 changes: 21 additions & 13 deletions commons/zenoh-util/src/std_only/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -285,19 +287,24 @@ pub fn get_unicast_addresses_of_multicast_interfaces() -> Vec<IpAddr> {
pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult<Vec<IpAddr>> {
#[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::<Vec<IpAddr>>()
})
.collect();
Ok(addrs)
.collect::<Vec<IpAddr>>();
Ok(addrs)
}
None => bail!("Interface {name} not found"),
}
}

#[cfg(windows)]
Expand Down Expand Up @@ -354,13 +361,14 @@ pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult<Vec<IpAddr>> {
}
}

pub fn get_index_of_interface(addr: IpAddr) -> ZResult<Option<u32>> {
pub fn get_index_of_interface(addr: IpAddr) -> ZResult<u32> {
#[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)]
{
Expand Down Expand Up @@ -400,14 +408,14 @@ pub fn get_index_of_interface(addr: IpAddr) -> ZResult<Option<u32>> {
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}")
}
}
}
Expand Down
14 changes: 2 additions & 12 deletions io/zenoh-links/zenoh-link-udp/src/multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand Down

0 comments on commit 21ac10f

Please sign in to comment.