diff --git a/commons/zenoh-util/src/std_only/net/mod.rs b/commons/zenoh-util/src/std_only/net/mod.rs index 15ea8fc99d..dd570b364d 100644 --- a/commons/zenoh-util/src/std_only/net/mod.rs +++ b/commons/zenoh-util/src/std_only/net/mod.rs @@ -424,29 +424,25 @@ pub fn get_ipv6_ipaddrs(interface: Option<&str>) -> Vec { } #[cfg(any(target_os = "linux", target_os = "android"))] -pub fn set_bind_to_device_tcp_socket(socket: &TcpSocket, iface: Option<&str>) -> ZResult<()> { - if let Some(iface) = iface { - socket.bind_device(Some(iface.as_bytes()))?; - } +pub fn set_bind_to_device_tcp_socket(socket: &TcpSocket, iface: &str) -> ZResult<()> { + socket.bind_device(Some(iface.as_bytes()))?; Ok(()) } #[cfg(any(target_os = "linux", target_os = "android"))] -pub fn set_bind_to_device_udp_socket(socket: &UdpSocket, iface: Option<&str>) -> ZResult<()> { - if let Some(iface) = iface { - socket.bind_device(Some(iface.as_bytes()))?; - } +pub fn set_bind_to_device_udp_socket(socket: &UdpSocket, iface: &str) -> ZResult<()> { + socket.bind_device(Some(iface.as_bytes()))?; Ok(()) } #[cfg(any(target_os = "macos", target_os = "windows"))] -pub fn set_bind_to_device_tcp_socket(socket: &TcpSocket, iface: Option<&str>) -> ZResult<()> { - log::warn!("Binding the socket {socket:?} to the interface {iface:?} is not supported on macOS and Windows"); +pub fn set_bind_to_device_tcp_socket(socket: &TcpSocket, iface: &str) -> ZResult<()> { + log::warn!("Binding the socket {socket:?} to the interface {iface} is not supported on macOS and Windows"); Ok(()) } #[cfg(any(target_os = "macos", target_os = "windows"))] -pub fn set_bind_to_device_udp_socket(socket: &UdpSocket, iface: Option<&str>) -> ZResult<()> { - log::warn!("Binding the socket {socket:?} to the interface {iface:?} is not supported on macOS and Windows"); +pub fn set_bind_to_device_udp_socket(socket: &UdpSocket, iface: &str) -> ZResult<()> { + log::warn!("Binding the socket {socket:?} to the interface {iface} is not supported on macOS and Windows"); Ok(()) } diff --git a/io/zenoh-links/zenoh-link-tcp/src/unicast.rs b/io/zenoh-links/zenoh-link-tcp/src/unicast.rs index 5909b2ffe7..7137ac0212 100644 --- a/io/zenoh-links/zenoh-link-tcp/src/unicast.rs +++ b/io/zenoh-links/zenoh-link-tcp/src/unicast.rs @@ -218,7 +218,9 @@ impl LinkManagerUnicastTcp { SocketAddr::V6(_) => TcpSocket::new_v6(), }?; - zenoh_util::net::set_bind_to_device_tcp_socket(&socket, iface)?; + if let Some(iface) = iface { + zenoh_util::net::set_bind_to_device_tcp_socket(&socket, iface)?; + } // Build a TcpStream from TcpSocket // https://docs.rs/tokio/latest/tokio/net/struct.TcpSocket.html @@ -248,7 +250,9 @@ impl LinkManagerUnicastTcp { SocketAddr::V6(_) => TcpSocket::new_v6(), }?; - zenoh_util::net::set_bind_to_device_tcp_socket(&socket, iface)?; + if let Some(iface) = iface { + zenoh_util::net::set_bind_to_device_tcp_socket(&socket, iface)?; + } // Build a TcpListener from TcpSocket // https://docs.rs/tokio/latest/tokio/net/struct.TcpSocket.html diff --git a/io/zenoh-links/zenoh-link-udp/src/unicast.rs b/io/zenoh-links/zenoh-link-udp/src/unicast.rs index 0862928e1a..1cd4a0b1ec 100644 --- a/io/zenoh-links/zenoh-link-udp/src/unicast.rs +++ b/io/zenoh-links/zenoh-link-udp/src/unicast.rs @@ -277,7 +277,9 @@ impl LinkManagerUnicastUdp { e })?; - zenoh_util::net::set_bind_to_device_udp_socket(&socket, iface)?; + if let Some(iface) = iface { + zenoh_util::net::set_bind_to_device_udp_socket(&socket, iface)?; + } // Connect the socket to the remote address socket.connect(dst_addr).await.map_err(|e| { @@ -314,7 +316,9 @@ impl LinkManagerUnicastUdp { e })?; - zenoh_util::net::set_bind_to_device_udp_socket(&socket, iface)?; + if let Some(iface) = iface { + zenoh_util::net::set_bind_to_device_udp_socket(&socket, iface)?; + } let local_addr = socket.local_addr().map_err(|e| { let e = zerror!("Can not create a new UDP listener on {}: {}", addr, e);