]> code.octet-stream.net Git - netwatcher/blobdiff - src/list_win.rs
Fix typo in README
[netwatcher] / src / list_win.rs
index e842244ae47c9e8f30170ac7ffd143fa95b67c41..00b776987d015cc4c599dd9379554338f43273bc 100644 (file)
@@ -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<HashMap<IfIndex, Interface>, Error> {
+pub(crate) fn list_interfaces() -> Result<List, Error> {
     let mut ifs = HashMap::new();
     // Microsoft recommends a 15 KB initial buffer
     let start_size = 15 * 1024;
@@ -37,15 +38,15 @@ pub fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, Error> {
             );
             match WIN32_ERROR(res) {
                 ERROR_SUCCESS => break,
-                ERROR_ADDRESS_NOT_ASSOCIATED => return Err(Error::Internal),
+                ERROR_ADDRESS_NOT_ASSOCIATED => return Err(Error::AddressNotAssociated),
                 ERROR_BUFFER_OVERFLOW => {
                     buf.resize(sizepointer as usize, 0);
                     continue;
                 }
-                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
-                _ => return Err(Error::Internal), // TODO: Use FormatMessage to get a string
+                ERROR_INVALID_PARAMETER => return Err(Error::InvalidParameter),
+                ERROR_NOT_ENOUGH_MEMORY => return Err(Error::NotEnoughMemory),
+                ERROR_NO_DATA => return Ok(List(HashMap::new())), // there aren't any
+                _ => return Err(Error::UnexpectedWindowsResult(res)),
             }
         }
 
@@ -53,13 +54,17 @@ pub fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, 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 {
-                    write!(hw_addr, ":").map_err(|_| Error::Internal)?;
+                    write!(hw_addr, ":").map_err(|_| Error::FormatMacAddress)?;
                 }
                 write!(hw_addr, "{:02X}", adapter.PhysicalAddress[i])
-                    .map_err(|_| Error::Internal)?;
+                    .map_err(|_| Error::FormatMacAddress)?;
             }
             let mut ips = vec![];
             let mut unicast_ptr = adapter.FirstUnicastAddress;
@@ -98,16 +103,5 @@ pub fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, Error> {
         }
     }
 
-    Ok(ifs)
-}
-
-#[cfg(test)]
-mod test {
-    use super::list_interfaces;
-
-    #[test]
-    fn list() {
-        let ifaces = list_interfaces().unwrap();
-        println!("{:?}", ifaces);
-    }
+    Ok(List(ifs))
 }