From d881b5d5893beead45a14fcec79271b9d8e5de97 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Fri, 23 Feb 2024 19:48:43 +0100 Subject: [PATCH] Move platfrom related code to zenoh-util --- commons/zenoh-util/src/std_only/net/mod.rs | 41 ++++++++++++++++++- io/zenoh-link-commons/src/unicast.rs | 28 ------------- io/zenoh-links/zenoh-link-tcp/src/unicast.rs | 9 +--- io/zenoh-links/zenoh-link-udp/src/unicast.rs | 9 +--- io/zenoh-transport/tests/unicast_openclose.rs | 2 + 5 files changed, 46 insertions(+), 43 deletions(-) diff --git a/commons/zenoh-util/src/std_only/net/mod.rs b/commons/zenoh-util/src/std_only/net/mod.rs index 3c4c5768a8..74d7783a31 100644 --- a/commons/zenoh-util/src/std_only/net/mod.rs +++ b/commons/zenoh-util/src/std_only/net/mod.rs @@ -11,7 +11,7 @@ // Contributors: // ZettaScale Zenoh Team, // -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; @@ -491,3 +491,42 @@ pub fn get_ipv6_ipaddrs(interface: Option) -> Vec { .chain(priv_ipv4_addrs) .collect() } + +#[cfg(target_os = "linux")] +fn set_bind_to_device(socket: std::os::raw::c_int, iface: &Option) { + 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) { + 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) { + 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: TcpSocket, _iface: &Option) { + 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) { + log::warn!("Listen at the interface is not supported for this platform"); +} diff --git a/io/zenoh-link-commons/src/unicast.rs b/io/zenoh-link-commons/src/unicast.rs index 278294b3ce..1237024ca9 100644 --- a/io/zenoh-link-commons/src/unicast.rs +++ b/io/zenoh-link-commons/src/unicast.rs @@ -114,31 +114,3 @@ pub fn get_ip_interface_names(addr: &SocketAddr) -> Vec { } } } - -pub fn set_bind_to_device(socket: std::os::raw::c_int, iface: &Option) { - #[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 - ); - } -} diff --git a/io/zenoh-links/zenoh-link-tcp/src/unicast.rs b/io/zenoh-links/zenoh-link-tcp/src/unicast.rs index e16b44aadd..34f56be586 100644 --- a/io/zenoh-links/zenoh-link-tcp/src/unicast.rs +++ b/io/zenoh-links/zenoh-link-tcp/src/unicast.rs @@ -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; @@ -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, @@ -222,15 +218,14 @@ impl LinkManagerUnicastTcp { async fn new_listener_inner( &self, addr: &SocketAddr, - #[warn(unused_variables)] iface: &Option, + iface: &Option, ) -> 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() diff --git a/io/zenoh-links/zenoh-link-udp/src/unicast.rs b/io/zenoh-links/zenoh-link-udp/src/unicast.rs index 99577ae44b..129ee9ae63 100644 --- a/io/zenoh-links/zenoh-link-udp/src/unicast.rs +++ b/io/zenoh-links/zenoh-link-udp/src/unicast.rs @@ -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; @@ -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>>>; type LinkInput = (Vec, usize); type LinkLeftOver = (Vec, usize, usize); @@ -308,7 +304,7 @@ impl LinkManagerUnicastUdp { async fn new_listener_inner( &self, addr: &SocketAddr, - #[warn(unused_variables)] iface: &Option, + iface: &Option, ) -> ZResult<(UdpSocket, SocketAddr)> { // Bind the UDP socket let socket = UdpSocket::bind(addr).await.map_err(|e| { @@ -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); diff --git a/io/zenoh-transport/tests/unicast_openclose.rs b/io/zenoh-transport/tests/unicast_openclose.rs index b2526d2c7b..224bf33574 100644 --- a/io/zenoh-transport/tests/unicast_openclose.rs +++ b/io/zenoh-transport/tests/unicast_openclose.rs @@ -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);