Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error when requested network interface not found in multicast #549

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading