From 9ea683eddb05362f02bca60e4337955dfc5871dc Mon Sep 17 00:00:00 2001 From: Thomas Karpiniec Date: Sat, 8 Jun 2024 17:33:42 +1000 Subject: [PATCH] Don't include "down" interfaces on Windows Tidy up in preparation for Linux support --- src/lib.rs | 4 ++-- src/{list_mac.rs => list_unix.rs} | 0 src/list_win.rs | 5 +++++ src/watch_mac.rs | 8 +++++--- src/watch_win.rs | 6 +++--- 5 files changed, 15 insertions(+), 8 deletions(-) rename src/{list_mac.rs => list_unix.rs} (100%) diff --git a/src/lib.rs b/src/lib.rs index 47c5ae1..46218d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,14 +5,14 @@ use std::{ }; #[cfg_attr(windows, path = "list_win.rs")] -#[cfg_attr(target_vendor = "apple", path = "list_mac.rs")] +#[cfg_attr(unix, path = "list_unix.rs")] mod list; #[cfg_attr(windows, path = "watch_win.rs")] #[cfg_attr(target_vendor = "apple", path = "watch_mac.rs")] mod watch; -#[cfg(target_vendor = "apple")] +#[cfg(unix)] mod util; type IfIndex = u32; diff --git a/src/list_mac.rs b/src/list_unix.rs similarity index 100% rename from src/list_mac.rs rename to src/list_unix.rs diff --git a/src/list_win.rs b/src/list_win.rs index 718f2bb..2ae79a6 100644 --- a/src/list_win.rs +++ b/src/list_win.rs @@ -12,6 +12,7 @@ use windows::Win32::NetworkManagement::IpHelper::{ use windows::Win32::NetworkManagement::IpHelper::{ GAA_FLAG_SKIP_ANYCAST, GAA_FLAG_SKIP_MULTICAST, IP_ADAPTER_ADDRESSES_LH, }; +use windows::Win32::NetworkManagement::Ndis::IfOperStatusDown; use windows::Win32::Networking::WinSock::{ AF_INET, AF_INET6, AF_UNSPEC, SOCKADDR, SOCKADDR_IN, SOCKADDR_IN6, }; @@ -53,6 +54,10 @@ pub(crate) fn list_interfaces() -> Result { let mut adapter_ptr = &buf[0] as *const _ as *const IP_ADAPTER_ADDRESSES_LH; while !adapter_ptr.is_null() { let adapter = &*adapter_ptr as &IP_ADAPTER_ADDRESSES_LH; + if adapter.OperStatus == IfOperStatusDown { + adapter_ptr = adapter.Next; + continue; + } let mut hw_addr = String::with_capacity(adapter.PhysicalAddressLength as usize * 3); for i in 0..adapter.PhysicalAddressLength as usize { if i != 0 { diff --git a/src/watch_mac.rs b/src/watch_mac.rs index 547db48..96e344b 100644 --- a/src/watch_mac.rs +++ b/src/watch_mac.rs @@ -1,10 +1,12 @@ use crate::Update; -pub struct WatchHandle; +pub(crate) struct WatchHandle; -pub fn watch_interfaces(callback: F) -> WatchHandle { +pub(crate) fn watch_interfaces( + callback: F, +) -> Result { // stop current worker thread // post this into a thread that will use it drop(callback); - WatchHandle + Ok(WatchHandle) } diff --git a/src/watch_win.rs b/src/watch_win.rs index 67e0d4c..41612b8 100644 --- a/src/watch_win.rs +++ b/src/watch_win.rs @@ -19,14 +19,14 @@ use crate::Error; use crate::List; use crate::Update; -pub struct WatchState { +struct WatchState { /// The last result that we captured, for diffing prev_list: List, /// User's callback cb: Box, } -pub struct WatchHandle { +pub(crate) struct WatchHandle { hnd: HANDLE, _state: Pin>>, } @@ -63,7 +63,7 @@ pub(crate) fn watch_interfaces( // Trigger an initial update. // This is allowed to race with true updates because it // will always calculate a diff and discard no-ops. - handle_notif(&mut *state.lock().unwrap()); + handle_notif(&mut state.lock().unwrap()); // Then return the handle Ok(WatchHandle { hnd, _state: state }) } -- 2.39.5