Skip to content

Commit

Permalink
Add tests, fix windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Feb 23, 2024
1 parent cb88fb9 commit 2e2afd8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
11 changes: 7 additions & 4 deletions commons/zenoh-util/src/std_only/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ pub fn get_local_addresses(interface: Option<String>) -> ZResult<Vec<IpAddr>> {
let mut result = vec![];
let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
while let Some(iface) = next_iface {
if let Some(interface) = interface.clone() {
if ffi::pstr_to_string(iface.AdapterName) != interface {
continue;
}
}
let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
while let Some(ucast_addr) = next_ucast_addr {
if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) {
Expand Down Expand Up @@ -427,8 +432,7 @@ pub fn get_ipv4_ipaddrs(interface: Option<String>) -> Vec<IpAddr> {
IpAddr::V4(a) => Some(a),
IpAddr::V6(_) => None,
})
.filter(|x| !x.is_multicast())
// .filter(|x| !x.is_loopback() && !x.is_multicast()) // TODO(sashacmc): Why we exclude loopback?
.filter(|x| !x.is_loopback() && !x.is_multicast())
.map(IpAddr::V4)
.collect()
}
Expand All @@ -448,8 +452,7 @@ pub fn get_ipv6_ipaddrs(interface: Option<String>) -> Vec<IpAddr> {
IpAddr::V6(_) => None,
})
.filter(|x| {
// !x.is_loopback() && !x.is_link_local() && !x.is_multicast() && !x.is_broadcast() // TODO(sashacmc): Why we exclude loopback?
!x.is_multicast() && !x.is_broadcast()
!x.is_loopback() && !x.is_link_local() && !x.is_multicast() && !x.is_broadcast()
});

// Get next all IPv6 addresses
Expand Down
6 changes: 5 additions & 1 deletion io/zenoh-link-commons/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ pub fn set_bind_to_device(socket: std::os::raw::c_int, iface: &Option<String>) {

#[cfg(any(target_os = "macos", target_os = "windows"))]
{
log::warn!("Listen at the interface is not supported for this platform");
log::warn!(
"Listen at the interface ({}, {}) is not supported for this platform",
socket,
iface
);
}
}
1 change: 1 addition & 0 deletions io/zenoh-links/zenoh-link-tcp/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ impl LinkManagerUnicastTcp {
.await
.map_err(|e| zerror!("{}: {}", addr, e))?;

#[cfg(unix)]
set_bind_to_device(socket.as_raw_fd(), iface);

let local_addr = socket
Expand Down
1 change: 1 addition & 0 deletions io/zenoh-links/zenoh-link-udp/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ impl LinkManagerUnicastUdp {
e
})?;

#[cfg(unix)]
set_bind_to_device(socket.as_raw_fd(), iface);

let local_addr = socket.local_addr().map_err(|e| {
Expand Down
45 changes: 42 additions & 3 deletions io/zenoh-transport/tests/unicast_openclose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use zenoh_transport::{
DummyTransportPeerEventHandler, TransportEventHandler, TransportManager,
TransportMulticastEventHandler, TransportPeer, TransportPeerEventHandler,
};
use zenoh_util::net::get_ipv4_ipaddrs;

const TIMEOUT: Duration = Duration::from_secs(60);
const SLEEP: Duration = Duration::from_millis(100);
Expand Down Expand Up @@ -153,10 +154,10 @@ async fn openclose_transport(endpoint: &EndPoint, lowlatency_transport: bool) {
let mut links_num = 1;

println!("Transport Open Close [1c1]");
let res = ztimeout!(client01_manager.open_transport_unicast(endpoint.clone()));
let open_res = ztimeout!(client01_manager.open_transport_unicast(endpoint.clone()));
println!("Transport Open Close [1c2]: {res:?}");
assert!(res.is_ok());
let c_ses1 = res.unwrap();
assert!(open_res.is_ok());
let c_ses1 = open_res.unwrap();
println!("Transport Open Close [1d1]");
let transports = ztimeout!(client01_manager.get_transports_unicast());
println!("Transport Open Close [1d2]: {transports:?}");
Expand Down Expand Up @@ -786,3 +787,41 @@ R+IdLiXcyIkg0m9N8I17p0ljCSkbrgGMD3bbePRTfg==

task::block_on(openclose_universal_transport(&endpoint));
}

#[cfg(feature = "transport_tcp")]
#[cfg(unix)]
#[test]
#[should_panic(expected = "assertion failed: open_res.is_ok()")]
fn openclose_tcp_only_with_interface_restriction() {
let addrs = get_ipv4_ipaddrs(None);

let _ = env_logger::try_init();
task::block_on(async {
zasync_executor_init!();
});

// should not connect to local interface and external address
let endpoint: EndPoint = format!("tcp/{}:{}#iface=lo", addrs[0], 13001)
.parse()
.unwrap();
task::block_on(openclose_universal_transport(&endpoint));
}

#[cfg(feature = "transport_udp")]
#[cfg(unix)]
#[test]
#[should_panic(expected = "assertion failed: open_res.is_ok()")]
fn openclose_udp_only_with_interface_restriction() {
let addrs = get_ipv4_ipaddrs(None);

let _ = env_logger::try_init();
task::block_on(async {
zasync_executor_init!();
});

// should not connect to local interface and external address
let endpoint: EndPoint = format!("udp/{}:{}#iface=lo", addrs[0], 13011)
.parse()
.unwrap();
task::block_on(openclose_universal_transport(&endpoint));
}

0 comments on commit 2e2afd8

Please sign in to comment.