X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/5dd5aea6e6c70688ad4288df1eaafd97ac05720a..f9df7170ac929ccc915642c2e6415e768973bb54:/src/watch_linux.rs?ds=sidebyside diff --git a/src/watch_linux.rs b/src/watch_linux.rs index 74c1465..95ef106 100644 --- a/src/watch_linux.rs +++ b/src/watch_linux.rs @@ -1,13 +1,108 @@ -use crate::Update; -use crate::Error; - -pub(crate) struct WatchHandle; - -pub(crate) fn watch_interfaces( - callback: F, -) -> Result { - // stop current worker thread - // post this into a thread that will use it - drop(callback); - Ok(WatchHandle) -} +use std::os::fd::AsRawFd; +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; +use nix::sys::socket::AddressFamily; +use nix::sys::socket::MsgFlags; +use nix::sys::socket::NetlinkAddr; +use nix::sys::socket::SockFlag; +use nix::sys::socket::SockProtocol; +use nix::sys::socket::SockType; +use nix::unistd::pipe; + +use crate::Error; +use crate::List; +use crate::Update; + +pub(crate) struct WatchHandle { + // Dropping will close the fd which will be detected by poll + _pipefd: OwnedFd, +} + +pub(crate) fn watch_interfaces( + callback: F, +) -> Result { + let pipefd = start_watcher_thread(callback)?; + Ok(WatchHandle { _pipefd: pipefd }) +} + +fn start_watcher_thread( + mut callback: F, +) -> Result { + let sockfd = socket( + AddressFamily::Netlink, + SockType::Raw, + SockFlag::empty(), + Some(SockProtocol::NetlinkRoute), + ) + .map_err(|_| Error::Internal)?; // TODO: proper errors + 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)?; + + let mut prev_list = List::default(); + let mut handle_update = move |new_list: List| { + if new_list == prev_list { + return; + } + let update = Update { + interfaces: new_list.0.clone(), + diff: new_list.diff_from(&prev_list), + }; + (callback)(update); + prev_list = new_list; + }; + + // Now that netlink socket is open, provide an initial update. + // By having this outside the thread we can return an error synchronously if it + // looks like we're going to have trouble listing interfaces. + handle_update(crate::list::list_interfaces()?); + + std::thread::spawn(move || { + let mut buf = [0u8; 4096]; + + loop { + let mut fds = [ + pollfd { + fd: sockfd.as_raw_fd(), + events: POLLIN, + revents: 0, + }, + pollfd { + fd: pipe_rd.as_raw_fd(), + events: POLLIN, + revents: 0, + }, + ]; + unsafe { + poll(&mut fds as *mut _, 2, -1); + } + if fds[0].revents != 0 { + // netlink socket had something happen + if recv(sockfd.as_raw_fd(), &mut buf, MsgFlags::empty()).is_ok() { + let Ok(new_list) = crate::list::list_interfaces() else { + continue; + }; + handle_update(new_list); + } + } + if fds[1].revents != 0 { + // pipe had something happen + break; + } + } + }); + + Ok(pipe_wr) +}