Skip to content

Commit

Permalink
Turn the socket interface binding to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanYuYuan committed Mar 20, 2024
1 parent 0a3b7a9 commit 093091b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
20 changes: 8 additions & 12 deletions commons/zenoh-util/src/std_only/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,29 +424,25 @@ pub fn get_ipv6_ipaddrs(interface: Option<&str>) -> Vec<IpAddr> {
}

#[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(())
}
8 changes: 6 additions & 2 deletions io/zenoh-links/zenoh-link-tcp/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions io/zenoh-links/zenoh-link-udp/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 093091b

Please sign in to comment.