Skip to content

Commit

Permalink
Return error when network interface not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Sep 12, 2023
1 parent d501634 commit 041d513
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
34 changes: 20 additions & 14 deletions commons/zenoh-util/src/std_only/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Check warning on line 18 in commons/zenoh-util/src/std_only/net/mod.rs

View workflow job for this annotation

GitHub Actions / Run tests on windows-latest

unused import: `zerror`

Check failure on line 18 in commons/zenoh-util/src/std_only/net/mod.rs

View workflow job for this annotation

GitHub Actions / Run checks on windows-latest

unused import: `zerror`

zconfigurable! {
static ref WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE: u32 = 8192;
Expand Down Expand Up @@ -285,19 +285,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 +359,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 +406,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 041d513

Please sign in to comment.