forked from eclipse-zenoh/zenoh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to binding on the interface (eclipse-zenoh#755)
* Add test binding * Add binding to the interface * Add UDP support, fix windows/macos build * Add tests, fix windows build * Fix windows/macos builds * Move platfrom-related code to the zenoh-util * Remove unused dependencies * Add interface binding on connection * String usage refactoring
- Loading branch information
Showing
7 changed files
with
246 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -210,12 +210,19 @@ pub fn get_multicast_interfaces() -> Vec<IpAddr> { | |
} | ||
} | ||
|
||
pub fn get_local_addresses() -> ZResult<Vec<IpAddr>> { | ||
pub fn get_local_addresses(interface: Option<&str>) -> ZResult<Vec<IpAddr>> { | ||
#[cfg(unix)] | ||
{ | ||
Ok(pnet_datalink::interfaces() | ||
.into_iter() | ||
.filter(|iface| iface.is_up() && iface.is_running()) | ||
.filter(|iface| { | ||
if let Some(interface) = interface.as_ref() { | ||
if iface.name != *interface { | ||
return false; | ||
} | ||
} | ||
iface.is_up() && iface.is_running() | ||
}) | ||
.flat_map(|iface| iface.ips) | ||
.map(|ipnet| ipnet.ip()) | ||
.collect()) | ||
|
@@ -232,6 +239,11 @@ pub fn get_local_addresses() -> ZResult<Vec<IpAddr>> { | |
let mut result = vec![]; | ||
let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref(); | ||
while let Some(iface) = next_iface { | ||
if let Some(interface) = interface.as_ref() { | ||
if ffi::pstr_to_string(iface.AdapterName) != *interface { | ||
continue; | ||
} | ||
} | ||
let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref(); | ||
while let Some(ucast_addr) = next_ucast_addr { | ||
if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) { | ||
|
@@ -412,8 +424,8 @@ pub fn get_interface_names_by_addr(addr: IpAddr) -> ZResult<Vec<String>> { | |
} | ||
} | ||
|
||
pub fn get_ipv4_ipaddrs() -> Vec<IpAddr> { | ||
get_local_addresses() | ||
pub fn get_ipv4_ipaddrs(interface: Option<&str>) -> Vec<IpAddr> { | ||
get_local_addresses(interface) | ||
.unwrap_or_else(|_| vec![]) | ||
.drain(..) | ||
.filter_map(|x| match x { | ||
|
@@ -425,12 +437,12 @@ pub fn get_ipv4_ipaddrs() -> Vec<IpAddr> { | |
.collect() | ||
} | ||
|
||
pub fn get_ipv6_ipaddrs() -> Vec<IpAddr> { | ||
pub fn get_ipv6_ipaddrs(interface: Option<&str>) -> Vec<IpAddr> { | ||
const fn is_unicast_link_local(addr: &Ipv6Addr) -> bool { | ||
(addr.segments()[0] & 0xffc0) == 0xfe80 | ||
} | ||
|
||
let ipaddrs = get_local_addresses().unwrap_or_else(|_| vec![]); | ||
let ipaddrs = get_local_addresses(interface).unwrap_or_else(|_| vec![]); | ||
|
||
// Get first all IPv4 addresses | ||
let ipv4_iter = ipaddrs | ||
|
@@ -479,3 +491,53 @@ pub fn get_ipv6_ipaddrs() -> Vec<IpAddr> { | |
.chain(priv_ipv4_addrs) | ||
.collect() | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn set_bind_to_device(socket: std::os::raw::c_int, iface: Option<&str>) { | ||
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_listener(socket: &TcpListener, iface: Option<&str>) { | ||
use std::os::fd::AsRawFd; | ||
set_bind_to_device(socket.as_raw_fd(), iface); | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
pub fn set_bind_to_device_tcp_stream(socket: &TcpStream, iface: Option<&str>) { | ||
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(socket: &UdpSocket, iface: Option<&str>) { | ||
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_listener(_socket: &TcpListener, _iface: Option<&str>) { | ||
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_tcp_stream(_socket: &TcpStream, _iface: Option<&str>) { | ||
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(_socket: &UdpSocket, _iface: Option<&str>) { | ||
log::warn!("Listen at the interface is not supported for this platform"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.