X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/1c34fe3947aaf8af2d773d59bdebf19e17d78527..cc3e849e9ff8568609f160d4bcdb83966ffdb6c5:/src/watch_win.rs?ds=inline diff --git a/src/watch_win.rs b/src/watch_win.rs index e495e9a..c88cd7c 100644 --- a/src/watch_win.rs +++ b/src/watch_win.rs @@ -1,8 +1,103 @@ -use crate::Update; - -pub struct WatchHandle; - -pub fn watch_interfaces(callback: F) -> WatchHandle { - drop(callback); - WatchHandle -} +use std::ffi::c_void; +use std::pin::Pin; +use std::sync::Mutex; + +use windows::Win32::Foundation::ERROR_INVALID_HANDLE; +use windows::Win32::Foundation::ERROR_INVALID_PARAMETER; +use windows::Win32::Foundation::ERROR_NOT_ENOUGH_MEMORY; +use windows::Win32::Foundation::NO_ERROR; +use windows::Win32::NetworkManagement::IpHelper::CancelMibChangeNotify2; +use windows::Win32::NetworkManagement::IpHelper::MIB_NOTIFICATION_TYPE; +use windows::Win32::NetworkManagement::IpHelper::MIB_UNICASTIPADDRESS_ROW; +use windows::Win32::{ + Foundation::{BOOLEAN, HANDLE}, + NetworkManagement::IpHelper::NotifyUnicastIpAddressChange, + Networking::WinSock::AF_UNSPEC, +}; + +use crate::Error; +use crate::List; +use crate::Update; + +pub struct WatchState { + /// The last result that we captured, for diffing + prev_list: List, + /// User's callback + cb: Box, +} + +pub struct WatchHandle { + hnd: HANDLE, + _state: Pin>>, +} + +impl Drop for WatchHandle { + fn drop(&mut self) { + unsafe { + let _ = CancelMibChangeNotify2(self.hnd); + } + } +} + +pub(crate) fn watch_interfaces( + mut callback: F, +) -> Result { + let null_list = List::default(); + let prev_list = crate::list::list_interfaces()?; + callback(Update { + interfaces: prev_list.0.clone(), + diff: prev_list.diff_from(&null_list), + }); + + // TODO: Can wo do something about the race condition? + let state = Box::pin(Mutex::new(WatchState { + prev_list, + cb: Box::new(callback), + })); + let state_ctx = &*state.as_ref() as *const _ as *const c_void; + + let mut hnd = HANDLE::default(); + let res = unsafe { + NotifyUnicastIpAddressChange( + AF_UNSPEC, + Some(notif), + Some(state_ctx), + BOOLEAN(0), + &mut hnd, + ) + }; + match res { + NO_ERROR => Ok(WatchHandle { hnd, _state: state }), + ERROR_INVALID_HANDLE => Err(Error::Internal), + ERROR_INVALID_PARAMETER => Err(Error::Internal), + ERROR_NOT_ENOUGH_MEMORY => Err(Error::Internal), + _ => Err(Error::Internal), // TODO: Use FormatMessage and get real error + } +} + +unsafe extern "system" fn notif( + ctx: *const c_void, + _row: *const MIB_UNICASTIPADDRESS_ROW, + _notification_type: MIB_NOTIFICATION_TYPE, +) { + println!("There was a change!"); + let Ok(new_list) = crate::list::list_interfaces() else { + println!("Failed to get list of interfaces on change"); + return; + }; + let state_ptr = ctx as *const Mutex; + unsafe { + let state_guard = &mut *state_ptr.as_ref().unwrap().lock().unwrap(); + if new_list == state_guard.prev_list { + // TODO: Hitting this a lot, is it true? + println!("Interfaces seem to be the same, ignoring"); + return; + } + let update = Update { + interfaces: new_list.0.clone(), + diff: new_list.diff_from(&state_guard.prev_list), + }; + (state_guard.cb)(update); + state_guard.prev_list = new_list; + } +}