From 1bd09c3059d471934bc0f482f672d35bbfdd54f7 Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Thu, 15 Feb 2024 15:53:04 +0800 Subject: [PATCH] Add the missing Windows snippet --- commons/zenoh-util/src/std_only/net/mod.rs | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/commons/zenoh-util/src/std_only/net/mod.rs b/commons/zenoh-util/src/std_only/net/mod.rs index a260f3f22b..bf0e0db07e 100644 --- a/commons/zenoh-util/src/std_only/net/mod.rs +++ b/commons/zenoh-util/src/std_only/net/mod.rs @@ -22,6 +22,38 @@ zconfigurable! { static ref WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES: u32 = 3; } +#[cfg(windows)] +unsafe fn get_adapters_adresses(af_spec: i32) -> ZResult> { + 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; + loop { + buffer = Vec::with_capacity(size as usize); + ret = winapi::um::iphlpapi::GetAdaptersAddresses( + af_spec.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) + } + + Ok(buffer) +} pub fn get_interface(name: &str) -> ZResult> { #[cfg(unix)] {