]> code.octet-stream.net Git - netwatcher/commitdiff
Don't include "down" interfaces on Windows
authorThomas Karpiniec <tom.karpiniec@outlook.com>
Sat, 8 Jun 2024 07:33:42 +0000 (17:33 +1000)
committerThomas Karpiniec <tom.karpiniec@outlook.com>
Sat, 8 Jun 2024 07:33:42 +0000 (17:33 +1000)
Tidy up in preparation for Linux support

src/lib.rs
src/list_mac.rs [deleted file]
src/list_unix.rs [new file with mode: 0644]
src/list_win.rs
src/watch_mac.rs
src/watch_win.rs

index 47c5ae11957466f7e233e0f2690105d431fad154..46218d2ceec0469263dd2ca48e61d90402b8af43 100644 (file)
@@ -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_mac.rs
deleted file mode 100644 (file)
index 4ae0fc7..0000000
+++ /dev/null
@@ -1,99 +0,0 @@
-use std::{collections::HashMap, net::IpAddr};
-
-use block2::Block;
-use nix::libc::c_long;
-use nix::{ifaddrs::getifaddrs, net::if_::if_nametoindex};
-
-use crate::util::format_mac;
-use crate::{Error, IfIndex, Interface};
-
-struct CandidateInterface {
-    name: String,
-    index: u32,
-    hw_addr: Option<String>,
-    ips: Vec<IpAddr>,
-}
-
-pub(crate) fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, Error> {
-    let addrs = getifaddrs().map_err(|_| Error::Internal)?;
-    let mut candidates = HashMap::new();
-
-    for addr in addrs {
-        let index = if_nametoindex(addr.interface_name.as_str()).map_err(|_| Error::Internal)?;
-        let candidate = candidates
-            .entry(addr.interface_name.clone())
-            .or_insert_with(|| CandidateInterface {
-                name: addr.interface_name.clone(),
-                index,
-                hw_addr: None,
-                ips: vec![],
-            });
-        if let Some(a) = addr.address {
-            if let Some(a) = a.as_link_addr() {
-                if let Some(raw_addr) = a.addr() {
-                    candidate.hw_addr = Some(format_mac(&raw_addr)?);
-                }
-            }
-            if let Some(a) = a.as_sockaddr_in() {
-                candidate.ips.push(IpAddr::V4(a.ip()));
-            }
-            if let Some(a) = a.as_sockaddr_in6() {
-                candidate.ips.push(IpAddr::V6(a.ip()));
-            }
-        }
-    }
-
-    let ifs = candidates
-        .drain()
-        .flat_map(|(_, c)| {
-            c.hw_addr.map(|hw_addr| {
-                (
-                    c.index,
-                    Interface {
-                        index: c.index,
-                        hw_addr,
-                        name: c.name,
-                        ips: c.ips,
-                    },
-                )
-            })
-        })
-        .collect();
-    Ok(ifs)
-}
-
-// The "objc2" project aims to provide bindings for all frameworks but Network.framework
-// isn't ready yet so let's kick it old-school
-
-struct nw_path_monitor;
-type nw_path_monitor_t = *mut nw_path_monitor;
-struct nw_path;
-type nw_path_t = *mut nw_path;
-struct dispatch_queue;
-type dispatch_queue_t = *mut dispatch_queue;
-const QOS_CLASS_BACKGROUND: usize = 0x09;
-
-#[link(name = "Network", kind = "framework")]
-extern "C" {
-    fn nw_path_monitor_create() -> nw_path_monitor_t;
-    fn nw_path_monitor_set_update_handler(
-        monitor: nw_path_monitor_t,
-        update_handler: &Block<dyn Fn(nw_path_t)>,
-    );
-    fn nw_path_monitor_set_queue(monitor: nw_path_monitor_t, queue: dispatch_queue_t);
-    fn nw_path_monitor_start(monitor: nw_path_monitor_t);
-    fn nw_path_monitor_cancel(monitor: nw_path_monitor_t);
-
-    fn dispatch_get_global_queue(identifier: usize, flag: usize) -> dispatch_queue_t;
-}
-
-#[cfg(test)]
-mod test {
-    use super::list_interfaces;
-
-    #[test]
-    fn list() {
-        let ifaces = list_interfaces().unwrap();
-        println!("{:?}", ifaces);
-    }
-}
diff --git a/src/list_unix.rs b/src/list_unix.rs
new file mode 100644 (file)
index 0000000..4ae0fc7
--- /dev/null
@@ -0,0 +1,99 @@
+use std::{collections::HashMap, net::IpAddr};
+
+use block2::Block;
+use nix::libc::c_long;
+use nix::{ifaddrs::getifaddrs, net::if_::if_nametoindex};
+
+use crate::util::format_mac;
+use crate::{Error, IfIndex, Interface};
+
+struct CandidateInterface {
+    name: String,
+    index: u32,
+    hw_addr: Option<String>,
+    ips: Vec<IpAddr>,
+}
+
+pub(crate) fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, Error> {
+    let addrs = getifaddrs().map_err(|_| Error::Internal)?;
+    let mut candidates = HashMap::new();
+
+    for addr in addrs {
+        let index = if_nametoindex(addr.interface_name.as_str()).map_err(|_| Error::Internal)?;
+        let candidate = candidates
+            .entry(addr.interface_name.clone())
+            .or_insert_with(|| CandidateInterface {
+                name: addr.interface_name.clone(),
+                index,
+                hw_addr: None,
+                ips: vec![],
+            });
+        if let Some(a) = addr.address {
+            if let Some(a) = a.as_link_addr() {
+                if let Some(raw_addr) = a.addr() {
+                    candidate.hw_addr = Some(format_mac(&raw_addr)?);
+                }
+            }
+            if let Some(a) = a.as_sockaddr_in() {
+                candidate.ips.push(IpAddr::V4(a.ip()));
+            }
+            if let Some(a) = a.as_sockaddr_in6() {
+                candidate.ips.push(IpAddr::V6(a.ip()));
+            }
+        }
+    }
+
+    let ifs = candidates
+        .drain()
+        .flat_map(|(_, c)| {
+            c.hw_addr.map(|hw_addr| {
+                (
+                    c.index,
+                    Interface {
+                        index: c.index,
+                        hw_addr,
+                        name: c.name,
+                        ips: c.ips,
+                    },
+                )
+            })
+        })
+        .collect();
+    Ok(ifs)
+}
+
+// The "objc2" project aims to provide bindings for all frameworks but Network.framework
+// isn't ready yet so let's kick it old-school
+
+struct nw_path_monitor;
+type nw_path_monitor_t = *mut nw_path_monitor;
+struct nw_path;
+type nw_path_t = *mut nw_path;
+struct dispatch_queue;
+type dispatch_queue_t = *mut dispatch_queue;
+const QOS_CLASS_BACKGROUND: usize = 0x09;
+
+#[link(name = "Network", kind = "framework")]
+extern "C" {
+    fn nw_path_monitor_create() -> nw_path_monitor_t;
+    fn nw_path_monitor_set_update_handler(
+        monitor: nw_path_monitor_t,
+        update_handler: &Block<dyn Fn(nw_path_t)>,
+    );
+    fn nw_path_monitor_set_queue(monitor: nw_path_monitor_t, queue: dispatch_queue_t);
+    fn nw_path_monitor_start(monitor: nw_path_monitor_t);
+    fn nw_path_monitor_cancel(monitor: nw_path_monitor_t);
+
+    fn dispatch_get_global_queue(identifier: usize, flag: usize) -> dispatch_queue_t;
+}
+
+#[cfg(test)]
+mod test {
+    use super::list_interfaces;
+
+    #[test]
+    fn list() {
+        let ifaces = list_interfaces().unwrap();
+        println!("{:?}", ifaces);
+    }
+}
index 718f2bb752b640e0c1f7b2bcc1f68379ef2c02e3..2ae79a66e9dd2dcab0bb3bd0f6eaf95b524367d1 100644 (file)
@@ -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<List, Error> {
         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 {
index 547db4801fd2bd156e9cda7d0a0c7d37195a7650..96e344bb97795bb495afead5c4024bb042b27d66 100644 (file)
@@ -1,10 +1,12 @@
 use crate::Update;\r
 \r
-pub struct WatchHandle;\r
+pub(crate) struct WatchHandle;\r
 \r
-pub fn watch_interfaces<F: FnMut(Update)>(callback: F) -> WatchHandle {\r
+pub(crate) fn watch_interfaces<F: FnMut(Update) + 'static>(\r
+    callback: F,\r
+) -> Result<WatchHandle, Error> {\r
     // stop current worker thread\r
     // post this into a thread that will use it\r
     drop(callback);\r
-    WatchHandle\r
+    Ok(WatchHandle)\r
 }\r
index 67e0d4c88fc27d3216a5760a2d7ddc13afb294bb..41612b8ae5f51d979fcf99f697ae30646766401b 100644 (file)
@@ -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<dyn FnMut(Update) + 'static>,
 }
 
-pub struct WatchHandle {
+pub(crate) struct WatchHandle {
     hnd: HANDLE,
     _state: Pin<Box<Mutex<WatchState>>>,
 }
@@ -63,7 +63,7 @@ pub(crate) fn watch_interfaces<F: FnMut(Update) + 'static>(
             // 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 })
         }