X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/f9df7170ac929ccc915642c2e6415e768973bb54..a26e228658fabc4495f3d671850903c8e105d9a1:/src/watch_linux.rs diff --git a/src/watch_linux.rs b/src/watch_linux.rs index 95ef106..c38be37 100644 --- a/src/watch_linux.rs +++ b/src/watch_linux.rs @@ -4,9 +4,6 @@ use std::os::fd::OwnedFd; use nix::libc::poll; use nix::libc::pollfd; use nix::libc::POLLIN; -use nix::libc::RTMGRP_IPV4_IFADDR; -use nix::libc::RTMGRP_IPV6_IFADDR; -use nix::libc::RTMGRP_LINK; use nix::sys::socket::bind; use nix::sys::socket::recv; use nix::sys::socket::socket; @@ -22,6 +19,10 @@ use crate::Error; use crate::List; use crate::Update; +const RTMGRP_IPV4_IFADDR: u32 = 0x10; +const RTMGRP_IPV6_IFADDR: u32 = 0x20; +const RTMGRP_LINK: u32 = 0x01; + pub(crate) struct WatchHandle { // Dropping will close the fd which will be detected by poll _pipefd: OwnedFd, @@ -43,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.to_string()))?; + sockfd.set_nonblocking(true); 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.to_string()))?; + let (pipe_rd, pipe_wr) = pipe().map_err(|e| Error::CreatePipe(e.to_string()))?; let mut prev_list = List::default(); let mut handle_update = move |new_list: List| {