X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/1c34fe3947aaf8af2d773d59bdebf19e17d78527..4abfa61b20e567fdd69ac3ca47a9c218971a30ff:/src/list_win.rs diff --git a/src/list_win.rs b/src/list_win.rs index e842244..2ae79a6 100644 --- a/src/list_win.rs +++ b/src/list_win.rs @@ -12,13 +12,14 @@ 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, }; -use crate::{Error, IfIndex, Interface}; +use crate::{Error, Interface, List}; -pub fn list_interfaces() -> Result, Error> { +pub(crate) fn list_interfaces() -> Result { let mut ifs = HashMap::new(); // Microsoft recommends a 15 KB initial buffer let start_size = 15 * 1024; @@ -44,7 +45,7 @@ pub fn list_interfaces() -> Result, Error> { } ERROR_INVALID_PARAMETER => return Err(Error::Internal), ERROR_NOT_ENOUGH_MEMORY => return Err(Error::Internal), - ERROR_NO_DATA => return Ok(HashMap::new()), // there aren't any + ERROR_NO_DATA => return Ok(List(HashMap::new())), // there aren't any _ => return Err(Error::Internal), // TODO: Use FormatMessage to get a string } } @@ -53,6 +54,10 @@ pub fn list_interfaces() -> Result, 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 { @@ -98,7 +103,7 @@ pub fn list_interfaces() -> Result, Error> { } } - Ok(ifs) + Ok(List(ifs)) } #[cfg(test)] @@ -107,7 +112,7 @@ mod test { #[test] fn list() { - let ifaces = list_interfaces().unwrap(); + let ifaces = list_interfaces().unwrap().0; println!("{:?}", ifaces); } }