Skip to content
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ features = [
"Win32_NetworkManagement_IpHelper",
"Win32_Networking_WinSock",
"Win32_NetworkManagement_Ndis",
"Win32_System_LibraryLoader",
]

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
Expand Down
32 changes: 30 additions & 2 deletions src/os/windows/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ use std::mem::MaybeUninit;
#[cfg(feature = "gateway")]
use std::net::Ipv4Addr;
#[cfg(feature = "gateway")]
use windows_sys::Win32::NetworkManagement::IpHelper::{GetIpNetEntry2, MIB_IPNET_ROW2, SendARP};
use std::sync::OnceLock;
#[cfg(feature = "gateway")]
use windows_sys::Win32::Foundation::WIN32_ERROR;
#[cfg(feature = "gateway")]
use windows_sys::Win32::NetworkManagement::IpHelper::{MIB_IPNET_ROW2, SendARP};
#[cfg(feature = "gateway")]
use windows_sys::Win32::NetworkManagement::Ndis::NET_LUID_LH;
#[cfg(feature = "gateway")]
use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};

fn sanitize_u64(val: u64) -> Option<u64> {
if val == u64::MAX { None } else { Some(val) }
Expand Down Expand Up @@ -120,14 +126,36 @@ fn physical_address_to_mac(address: &[u8], length: u32) -> Option<MacAddr> {
Some(MacAddr::from_octets(address[..6].try_into().unwrap()))
}

#[cfg(feature = "gateway")]
type GetIpNetEntry2 = unsafe extern "system" fn(*mut MIB_IPNET_ROW2) -> WIN32_ERROR;

/// Resolve `GetIpNetEntry2`, which some `iphlpapi.dll` implementations do not export.
#[cfg(feature = "gateway")]
fn get_ip_net_entry2() -> Option<GetIpNetEntry2> {
static ENTRY: OnceLock<Option<GetIpNetEntry2>> = OnceLock::new();
*ENTRY.get_or_init(|| {
// `iphlpapi.dll` is already loaded: this module statically imports `GetAdaptersAddresses`.
let module = unsafe { GetModuleHandleA(c"iphlpapi.dll".as_ptr().cast()) };
if module.is_null() {
return None;
}
let proc = unsafe { GetProcAddress(module, c"GetIpNetEntry2".as_ptr().cast()) }?;
// SAFETY: the export matches the `GetIpNetEntry2` signature.
Some(unsafe {
std::mem::transmute::<unsafe extern "system" fn() -> isize, GetIpNetEntry2>(proc)
})
})
}

#[cfg(feature = "gateway")]
fn get_neighbor_mac(address: SOCKADDR_INET, interface_luid: NET_LUID_LH) -> Option<MacAddr> {
let get_ip_net_entry2 = get_ip_net_entry2()?;
let mut row = MIB_IPNET_ROW2 {
Address: address,
InterfaceLuid: interface_luid,
..Default::default()
};
let result = unsafe { GetIpNetEntry2(&mut row) };
let result = unsafe { get_ip_net_entry2(&mut row) };
if result != NO_ERROR {
return None;
}
Expand Down
Loading