Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn the socket interface binding to be optional #850

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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