From d23efed20f5aea6e2067e7c9ec65f2ceca83ba7a Mon Sep 17 00:00:00 2001 From: Will Toohey Date: Fri, 8 Dec 2023 10:21:58 +1000 Subject: [PATCH 1/2] Windows: Dynamically determine interface count --- src/win.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/win.rs b/src/win.rs index f404fd2..e5020d9 100644 --- a/src/win.rs +++ b/src/win.rs @@ -14,6 +14,7 @@ use winapi::ctypes::{c_char, c_int}; use winapi::shared::inaddr::*; use winapi::shared::minwindef::DWORD; use winapi::shared::minwindef::{INT, LPDWORD}; +use winapi::shared::winerror::ERROR_BUFFER_OVERFLOW; use winapi::shared::ws2def::LPWSAMSG; use winapi::shared::ws2def::*; use winapi::shared::ws2ipdef::*; @@ -171,18 +172,15 @@ fn create_on_interfaces( }) } -/// The amount of ip's per interface we can support -/// This was an increase when I suddenly got 60 different IPs registered on my computer -/// Let's hope we can keep it like that (or increase it even further) -const PER_INTERFACE_IP_SUPPORT: usize = 5; - fn build_address_table(interfaces: HashSet) -> io::Result> { - let mut buffer = vec![ - 0; - mem::size_of::() - * interfaces.len() - * PER_INTERFACE_IP_SUPPORT - ]; + let mut size = 0u32; + let r = unsafe { winapi::um::iphlpapi::GetAdaptersInfo(ptr::null_mut(), &mut size) }; + if r != ERROR_BUFFER_OVERFLOW { + return Err(io::Error::last_os_error()); + } + + let mut buffer = + vec![0; mem::size_of::() * interfaces.len() * (size as usize)]; let mut adapter_info = buffer.as_mut_ptr() as iptypes::PIP_ADAPTER_INFO; let mut size = buffer.len() as u32; let r = unsafe { winapi::um::iphlpapi::GetAdaptersInfo(adapter_info, &mut size) }; From b289c8bd112d30d226224fba1853e80d6457c4ea Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Sun, 17 Dec 2023 19:27:56 -0300 Subject: [PATCH 2/2] (win) Adjust allocated buffer size for Interface information to match the returned interface length --- src/win.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/win.rs b/src/win.rs index 5736e5f..2a0dd41 100644 --- a/src/win.rs +++ b/src/win.rs @@ -179,8 +179,7 @@ fn build_address_table(interfaces: HashSet) -> io::Result() * interfaces.len() * (size as usize)]; + let mut buffer = vec![0; mem::size_of::() * (size as usize)]; let mut adapter_info = buffer.as_mut_ptr() as iptypes::PIP_ADAPTER_INFO; let mut size = buffer.len() as u32; let r = unsafe { winapi::um::iphlpapi::GetAdaptersInfo(adapter_info, &mut size) };