X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/f9610b2cf9cf1c3f79fa906cf4c33a8e09eb3e1a..0ecc1ee8ddfdf50d813fe18794eb27ac4ec0cf56:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..af5b7af --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,71 @@ +use std::{ + collections::HashMap, + net::{IpAddr, Ipv4Addr, Ipv6Addr}, +}; + +#[cfg_attr(windows, path = "imp_win.rs")] +mod imp; + +type IfIndex = u32; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Interface { + pub index: u32, + pub name: String, + pub hw_addr: String, + pub ips: Vec, +} + +impl Interface { + pub fn ipv4_ips(&self) -> impl Iterator { + self.ips.iter().filter_map(|ip| match ip { + IpAddr::V4(v4) => Some(v4), + IpAddr::V6(_) => None, + }) + } + + pub fn ipv6_ips(&self) -> impl Iterator { + self.ips.iter().filter_map(|ip| match ip { + IpAddr::V4(_) => None, + IpAddr::V6(v6) => Some(v6), + }) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Update { + pub interfaces: HashMap, + pub diff: UpdateDiff, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct UpdateDiff { + pub added: Vec, + pub removed: Vec, + pub modified: HashMap, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct InterfaceDiff { + pub hw_addr_changed: bool, + pub addrs_added: Vec, + pub addrs_removed: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Error { + Internal, +} + +pub fn list_interfaces() -> Result, Error> { + imp::list_interfaces() +} + +pub struct WatchHandle; + +pub fn watch_interfaces(callback: F) -> WatchHandle { + // stop current worker thread + // post this into a thread that will use it + drop(callback); + WatchHandle +}