Skip to content

Commit

Permalink
Windows net code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Feb 1, 2024
1 parent 2d6ac39 commit 98e06e5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 134 deletions.
176 changes: 46 additions & 130 deletions commons/zenoh-util/src/std_only/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ pub fn set_linger(socket: &TcpStream, dur: Option<Duration>) -> ZResult<()> {
}
}

#[cfg(windows)]
unsafe fn get_adapters_adresses() -> ZResult<Vec<u8>> {
use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

let mut ret;
let mut retries = 0;
let mut size: u32 = *WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE;
let mut buffer: Vec<u8>;
loop {
buffer = Vec::with_capacity(size as usize);
ret = winapi::um::iphlpapi::GetAdaptersAddresses(
winapi::shared::ws2def::AF_INET.try_into().unwrap(),
0,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
&mut size,
);
if ret != winapi::shared::winerror::ERROR_BUFFER_OVERFLOW {
break;
}
if retries >= *WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES {
break;
}
retries += 1;
}

if ret != 0 {
bail!("GetAdaptersAddresses returned {}", ret)
}

return Ok(buffer);
}

pub fn get_interface(name: &str) -> ZResult<Option<IpAddr>> {
#[cfg(unix)]
{
Expand All @@ -117,31 +150,7 @@ pub fn get_interface(name: &str) -> ZResult<Option<IpAddr>> {
use crate::ffi;
use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

let mut ret;
let mut retries = 0;
let mut size: u32 = *WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE;
let mut buffer: Vec<u8>;
loop {
buffer = Vec::with_capacity(size as usize);
ret = winapi::um::iphlpapi::GetAdaptersAddresses(
winapi::shared::ws2def::AF_INET.try_into().unwrap(),
0,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
&mut size,
);
if ret != winapi::shared::winerror::ERROR_BUFFER_OVERFLOW {
break;
}
if retries >= *WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES {
break;
}
retries += 1;
}

if ret != 0 {
bail!("GetAdaptersAddresses returned {}", ret)
}
let buffer = get_adapters_adresses()?;

let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
while let Some(iface) = next_iface {
Expand Down Expand Up @@ -218,33 +227,9 @@ pub fn get_local_addresses() -> ZResult<Vec<IpAddr>> {
use crate::ffi;
use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

let buffer = get_adapters_adresses()?;

let mut result = vec![];
let mut ret;
let mut retries = 0;
let mut size: u32 = *WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE;
let mut buffer: Vec<u8>;
loop {
buffer = Vec::with_capacity(size as usize);
ret = winapi::um::iphlpapi::GetAdaptersAddresses(
winapi::shared::ws2def::AF_UNSPEC.try_into().unwrap(),
0,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
&mut size,
);
if ret != winapi::shared::winerror::ERROR_BUFFER_OVERFLOW {
break;
}
if retries >= *WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES {
break;
}
retries += 1;
}

if ret != 0 {
bail!("GetAdaptersAddresses returned {}", ret)
}

let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
while let Some(iface) = next_iface {
let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
Expand Down Expand Up @@ -317,33 +302,9 @@ pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult<Vec<IpAddr>> {
use crate::ffi;
use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

let mut addrs = vec![];
let mut ret;
let mut retries = 0;
let mut size: u32 = *WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE;
let mut buffer: Vec<u8>;
loop {
buffer = Vec::with_capacity(size as usize);
ret = winapi::um::iphlpapi::GetAdaptersAddresses(
winapi::shared::ws2def::AF_INET.try_into().unwrap(),
0,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
&mut size,
);
if ret != winapi::shared::winerror::ERROR_BUFFER_OVERFLOW {
break;
}
if retries >= *WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES {
break;
}
retries += 1;
}

if ret != 0 {
bail!("GetAdaptersAddresses returned {}", ret);
}
let buffer = get_adapters_adresses()?;

let mut addrs = vec![];
let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
while let Some(iface) = next_iface {
if name == ffi::pstr_to_string(iface.AdapterName)
Expand Down Expand Up @@ -380,31 +341,7 @@ pub fn get_index_of_interface(addr: IpAddr) -> ZResult<u32> {
use crate::ffi;
use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

let mut ret;
let mut retries = 0;
let mut size: u32 = *WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE;
let mut buffer: Vec<u8>;
loop {
buffer = Vec::with_capacity(size as usize);
ret = winapi::um::iphlpapi::GetAdaptersAddresses(
winapi::shared::ws2def::AF_INET.try_into().unwrap(),
0,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
&mut size,
);
if ret != winapi::shared::winerror::ERROR_BUFFER_OVERFLOW {
break;
}
if retries >= *WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES {
break;
}
retries += 1;
}

if ret != 0 {
bail!("GetAdaptersAddresses returned {}", ret)
}
let buffer = get_adapters_adresses()?;

let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
while let Some(iface) = next_iface {
Expand All @@ -424,51 +361,30 @@ pub fn get_index_of_interface(addr: IpAddr) -> ZResult<u32> {
}
}

pub fn get_interface_by_addr(addr: IpAddr) -> Vec<String> {
pub fn get_interfaces_by_addr(addr: IpAddr) -> ZResult<Vec<String>> {
#[cfg(unix)]
{
if addr.is_unspecified() {
pnet_datalink::interfaces()
Ok(pnet_datalink::interfaces()
.iter()
.map(|iface| iface.name.clone())
.collect::<Vec<String>>()
.collect::<Vec<String>>())
} else {
pnet_datalink::interfaces()
Ok(pnet_datalink::interfaces()
.iter()
.filter(|iface| iface.ips.iter().any(|ipnet| ipnet.ip() == addr))
.map(|iface| iface.name.clone())
.collect::<Vec<String>>()
.collect::<Vec<String>>())
}
}
#[cfg(windows)]
{
// TODO(sashacmc): check and fix
let mut result = vec![];
unsafe {
use crate::ffi;
use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

let mut ret;
let mut retries = 0;
let mut size: u32 = *WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE;
let mut buffer: Vec<u8>;
loop {
buffer = Vec::with_capacity(size as usize);
ret = winapi::um::iphlpapi::GetAdaptersAddresses(
winapi::shared::ws2def::AF_INET.try_into().unwrap(),
0,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
&mut size,
);
if ret != winapi::shared::winerror::ERROR_BUFFER_OVERFLOW {
break;
}
if retries >= *WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES {
break;
}
retries += 1;
}
let buffer = get_adapters_adresses()?;

if addr.is_unspecified() {
let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
Expand All @@ -492,7 +408,7 @@ pub fn get_interface_by_addr(addr: IpAddr) -> Vec<String> {
}
}
}
result
Ok(result)
}
}

Expand Down
13 changes: 10 additions & 3 deletions io/zenoh-links/zenoh-link-tcp/src/unicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,16 @@ impl LinkUnicastTrait for LinkUnicastTcp {

#[inline(always)]
fn get_interfaces(&self) -> Vec<String> {
let res = zenoh_util::net::get_interface_by_addr(self.src_addr.ip());
log::debug!("get_interfaces for {:?}: {:?}", self.src_addr.ip(), res);
res
match zenoh_util::net::get_interfaces_by_addr(self.src_addr.ip()) {
Ok(interfaces) => {
log::debug!("get_interfaces for {:?}: {:?}", self.src_addr.ip(), interfaces);
interfaces
},
Err(e) => {
log::error!("get_interfaces for {:?} failed: {:?}", self.src_addr.ip(), e);
vec![]
},
}
}

#[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/net/routing/interceptor/downsampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::{Arc, Mutex};
use zenoh_config::DownsamplerConf;
use zenoh_protocol::core::key_expr::OwnedKeyExpr;

static RATELIMIT_STRATEGY: &'static str = "ratelimit";
const RATELIMIT_STRATEGY: &str = "ratelimit";

// TODO(sashacmc): this is ratelimit strategy, we can also add decimation (with "factor" option)

Expand Down

0 comments on commit 98e06e5

Please sign in to comment.