From: Thomas Karpiniec Date: Wed, 26 Jun 2024 10:26:26 +0000 (+1000) Subject: Make some error cases more specific X-Git-Tag: v0.1.0~8 X-Git-Url: https://code.octet-stream.net/netwatcher/commitdiff_plain/473c9605820f4531f9d40823338fa4bf8718dd6f?ds=sidebyside;hp=addb589bf51bdc5b5e253c0b5cbd47394e293ba7 Make some error cases more specific --- diff --git a/src/lib.rs b/src/lib.rs index 08a0a36..6d22c98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,13 +4,18 @@ use std::{ ops::Sub, }; +use nix::errno::Errno; + #[cfg_attr(windows, path = "list_win.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")] -#[cfg_attr(any(target_os = "linux", target_os = "android"), path = "watch_linux.rs")] +#[cfg_attr( + any(target_os = "linux", target_os = "android"), + path = "watch_linux.rs" +)] mod watch; type IfIndex = u32; @@ -71,7 +76,12 @@ pub struct InterfaceDiff { /// Errors in netwatcher or in one of the underlying platform integratinos. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Error { - // TODO: handle all cases with proper sources + CreateSocket(Errno), + Bind(Errno), + CreatePipe(Errno), + Getifaddrs(Errno), + GetInterfaceName(Errno), + FormatMacAddress, Internal, } diff --git a/src/list_unix.rs b/src/list_unix.rs index fad4306..a7cd881 100644 --- a/src/list_unix.rs +++ b/src/list_unix.rs @@ -13,11 +13,12 @@ struct CandidateInterface { } pub(crate) fn list_interfaces() -> Result { - let addrs = getifaddrs().map_err(|_| Error::Internal)?; + let addrs = getifaddrs().map_err(|e| Error::Getifaddrs(e))?; 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))?; let candidate = candidates .entry(addr.interface_name.clone()) .or_insert_with(|| CandidateInterface { @@ -64,9 +65,9 @@ fn format_mac(bytes: &[u8]) -> Result { let mut mac = String::with_capacity(bytes.len() * 3); 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}", b).map_err(|_| Error::Internal)?; + write!(mac, "{:02X}", b).map_err(|_| Error::FormatMacAddress)?; } Ok(mac) } diff --git a/src/watch_linux.rs b/src/watch_linux.rs index c1f08a2..e8e1ffc 100644 --- a/src/watch_linux.rs +++ b/src/watch_linux.rs @@ -44,13 +44,14 @@ fn start_watcher_thread( SockFlag::empty(), Some(SockProtocol::NetlinkRoute), ) - .map_err(|_| Error::Internal)?; // TODO: proper errors + .map_err(|e| Error::CreateSocket(e))?; + // TODO: set nonblocking let sa_nl = NetlinkAddr::new( 0, (RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR) as u32, ); - bind(sockfd.as_raw_fd(), &sa_nl).map_err(|_| Error::Internal)?; // TODO: proper errors - let (pipe_rd, pipe_wr) = pipe().map_err(|_| Error::Internal)?; + bind(sockfd.as_raw_fd(), &sa_nl).map_err(|e| Error::Bind(e))?; + let (pipe_rd, pipe_wr) = pipe().map_err(|e| Error::CreatePipe(e))?; let mut prev_list = List::default(); let mut handle_update = move |new_list: List| { diff --git a/src/watch_mac.rs b/src/watch_mac.rs index de0e650..42e7217 100644 --- a/src/watch_mac.rs +++ b/src/watch_mac.rs @@ -42,7 +42,9 @@ pub(crate) struct WatchHandle { impl Drop for WatchHandle { fn drop(&mut self) { - unsafe { nw_path_monitor_cancel(self.path_monitor); } + unsafe { + nw_path_monitor_cancel(self.path_monitor); + } } } @@ -83,7 +85,5 @@ pub(crate) fn watch_interfaces( nw_path_monitor_set_queue(path_monitor, queue); nw_path_monitor_start(path_monitor); } - Ok(WatchHandle { - path_monitor, - }) + Ok(WatchHandle { path_monitor }) }