X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/db2bad72106542d4e785e6ed277e73ac8a11c9bc..487dfaa43e0370e63157dc302d55543cff2949cf:/src/list_unix.rs diff --git a/src/list_unix.rs b/src/list_unix.rs index a6b2bd3..4a49140 100644 --- a/src/list_unix.rs +++ b/src/list_unix.rs @@ -1,11 +1,9 @@ use std::fmt::Write; use std::{collections::HashMap, net::IpAddr}; -use block2::Block; -use nix::libc::c_long; use nix::{ifaddrs::getifaddrs, net::if_::if_nametoindex}; -use crate::{Error, IfIndex, Interface}; +use crate::{Error, Interface, List}; struct CandidateInterface { name: String, @@ -14,12 +12,13 @@ struct CandidateInterface { ips: Vec, } -pub(crate) fn list_interfaces() -> Result, Error> { - let addrs = getifaddrs().map_err(|_| Error::Internal)?; +pub(crate) fn list_interfaces() -> Result { + let addrs = getifaddrs().map_err(|e| Error::Getifaddrs(e.to_string()))?; let mut candidates = HashMap::new(); for addr in addrs { - let index = if_nametoindex(addr.interface_name.as_str()).map_err(|_| Error::Internal)?; + let index = if_nametoindex(addr.interface_name.as_str()) + .map_err(|e| Error::GetInterfaceName(e.to_string()))?; let candidate = candidates .entry(addr.interface_name.clone()) .or_insert_with(|| CandidateInterface { @@ -59,16 +58,16 @@ pub(crate) fn list_interfaces() -> Result, Error> { }) }) .collect(); - Ok(ifs) + Ok(List(ifs)) } fn format_mac(bytes: &[u8]) -> Result { let mut mac = String::with_capacity(bytes.len() * 3); - for i in 0..bytes.len() { + for (i, b) in bytes.iter().enumerate() { if i != 0 { - write!(mac, ":").map_err(|_| Error::Internal)?; + write!(mac, ":").map_err(|_| Error::FormatMacAddress)?; } - write!(mac, "{:02X}", bytes[i]).map_err(|_| Error::Internal)?; + write!(mac, "{:02X}", b).map_err(|_| Error::FormatMacAddress)?; } Ok(mac) }