Skip to content

Commit

Permalink
Move platfrom-related code to the zenoh-util
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Feb 23, 2024
1 parent 409b885 commit f252c7d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
41 changes: 40 additions & 1 deletion commons/zenoh-util/src/std_only/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use async_std::net::TcpStream;
use async_std::net::{TcpListener, TcpStream, UdpSocket};
use std::net::{IpAddr, Ipv6Addr};
use std::time::Duration;
use zenoh_core::zconfigurable;
Expand Down Expand Up @@ -491,3 +491,42 @@ pub fn get_ipv6_ipaddrs(interface: Option<String>) -> Vec<IpAddr> {
.chain(priv_ipv4_addrs)
.collect()
}

#[cfg(target_os = "linux")]
fn set_bind_to_device(socket: std::os::raw::c_int, iface: &Option<String>) {
if let Some(iface) = iface {
// @TODO: switch to bind_device after tokio porting
log::debug!("Listen at the interface: {}", iface);
unsafe {
libc::setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_BINDTODEVICE,
iface.as_ptr() as *const std::os::raw::c_void,
iface.len() as libc::socklen_t,
);
}
}
}

#[cfg(target_os = "linux")]
pub fn set_bind_to_device_tcp(socket: &TcpListener, iface: &Option<String>) {
use std::os::fd::AsRawFd;
set_bind_to_device(socket.as_raw_fd(), iface);
}

#[cfg(target_os = "linux")]
pub fn set_bind_to_device_udp(socket: &UdpSocket, iface: &Option<String>) {
use std::os::fd::AsRawFd;
set_bind_to_device(socket.as_raw_fd(), iface);
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
pub fn set_bind_to_device_tcp(_socket: &TcpListener, _iface: &Option<String>) {
log::warn!("Listen at the interface is not supported for this platform");
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
pub fn set_bind_to_device_udp(_socket: &UdpSocket, _iface: &Option<String>) {
log::warn!("Listen at the interface is not supported for this platform");
}
28 changes: 0 additions & 28 deletions io/zenoh-link-commons/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,3 @@ pub fn get_ip_interface_names(addr: &SocketAddr) -> Vec<String> {
}
}
}

pub fn set_bind_to_device(socket: std::os::raw::c_int, iface: &Option<String>) {
#[cfg(target_os = "linux")]
{
if let Some(iface) = iface {
// @TODO: switch to bind_device after tokio porting
log::debug!("Listen at the interface: {}", iface);
unsafe {
libc::setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_BINDTODEVICE,
iface.as_ptr() as *const std::os::raw::c_void,
iface.len() as libc::socklen_t,
);
}
}
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
{
log::warn!(
"Listen at the interface ({:?}, {:?}) is not supported for this platform",
socket,
iface
);
}
}
9 changes: 2 additions & 7 deletions io/zenoh-links/zenoh-link-tcp/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use async_trait::async_trait;
use std::convert::TryInto;
use std::fmt;
use std::net::Shutdown;
use std::os::fd::AsRawFd;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -30,9 +29,6 @@ use zenoh_protocol::core::{EndPoint, Locator};
use zenoh_result::{bail, zerror, Error as ZError, ZResult};
use zenoh_sync::Signal;

#[cfg(unix)]
use zenoh_link_commons::set_bind_to_device;

use super::{
get_tcp_addrs, TCP_ACCEPT_THROTTLE_TIME, TCP_DEFAULT_MTU, TCP_LINGER_TIMEOUT,
TCP_LOCATOR_PREFIX,
Expand Down Expand Up @@ -222,15 +218,14 @@ impl LinkManagerUnicastTcp {
async fn new_listener_inner(
&self,
addr: &SocketAddr,
#[warn(unused_variables)] iface: &Option<String>,
iface: &Option<String>,
) -> ZResult<(TcpListener, SocketAddr)> {
// Bind the TCP socket
let socket = TcpListener::bind(addr)
.await
.map_err(|e| zerror!("{}: {}", addr, e))?;

#[cfg(unix)]
set_bind_to_device(socket.as_raw_fd(), iface);
zenoh_util::net::set_bind_to_device_tcp(&socket, iface);

let local_addr = socket
.local_addr()
Expand Down
9 changes: 2 additions & 7 deletions io/zenoh-links/zenoh-link-udp/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use async_std::task;
use async_trait::async_trait;
use std::collections::HashMap;
use std::fmt;
use std::os::fd::AsRawFd;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;
Expand All @@ -36,9 +35,6 @@ use zenoh_result::{bail, zerror, Error as ZError, ZResult};
use zenoh_sync::Mvar;
use zenoh_sync::Signal;

#[cfg(unix)]
use zenoh_link_commons::set_bind_to_device;

type LinkHashMap = Arc<Mutex<HashMap<(SocketAddr, SocketAddr), Weak<LinkUnicastUdpUnconnected>>>>;
type LinkInput = (Vec<u8>, usize);
type LinkLeftOver = (Vec<u8>, usize, usize);
Expand Down Expand Up @@ -308,7 +304,7 @@ impl LinkManagerUnicastUdp {
async fn new_listener_inner(
&self,
addr: &SocketAddr,
#[warn(unused_variables)] iface: &Option<String>,
iface: &Option<String>,
) -> ZResult<(UdpSocket, SocketAddr)> {
// Bind the UDP socket
let socket = UdpSocket::bind(addr).await.map_err(|e| {
Expand All @@ -317,8 +313,7 @@ impl LinkManagerUnicastUdp {
e
})?;

#[cfg(unix)]
set_bind_to_device(socket.as_raw_fd(), iface);
zenoh_util::net::set_bind_to_device_udp(&socket, iface);

let local_addr = socket.local_addr().map_err(|e| {
let e = zerror!("Can not create a new UDP listener on {}: {}", addr, e);
Expand Down
2 changes: 2 additions & 0 deletions io/zenoh-transport/tests/unicast_openclose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use zenoh_transport::{
DummyTransportPeerEventHandler, TransportEventHandler, TransportManager,
TransportMulticastEventHandler, TransportPeer, TransportPeerEventHandler,
};

#[cfg(target_os = "linux")]
use zenoh_util::net::get_ipv4_ipaddrs;

const TIMEOUT: Duration = Duration::from_secs(60);
Expand Down

0 comments on commit f252c7d

Please sign in to comment.