#![doc = include_str!("../README.md")]
use std::net::IpAddr;
#[cfg(target_os = "macos")]
pub mod darwin;
mod family;
mod id;
#[cfg(target_os = "linux")]
pub mod linux;
mod netmon;
#[cfg(windows)]
pub mod windows;
pub use family::{Family, FamilyOrBoth};
pub use id::{InterfaceId, MonType};
pub use netmon::{BoxStream, Netmon, PlatformMon, platform_mon};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
AddrUpsert(InterfaceId, ipnet::IpNet),
AddrRemoved(InterfaceId, ipnet::IpNet),
InterfaceUpsert(Interface),
InterfaceRemoved(InterfaceId),
RouteUpsert(InterfaceId, Route),
RouteRemoved(InterfaceId, Route),
DefaultRouteInterface(Option<InterfaceId>, Family),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Interface {
pub id: InterfaceId,
pub up: bool,
pub name: String,
pub mtu: Option<usize>,
pub hardware_addr: Option<smallvec::SmallVec<[u8; 6]>>,
}
pub type RouteUnique = (ipnet::IpNet, smallvec::SmallVec<[IpAddr; 1]>);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Route {
pub dst: ipnet::IpNet,
pub gateway: smallvec::SmallVec<[IpAddr; 1]>,
pub metric: usize,
}
impl Route {
pub fn is_default_route(&self) -> bool {
self.dst.prefix_len() == 0 && !self.gateway.is_empty()
}
pub fn family(&self) -> Family {
if self.dst.addr().is_ipv4() {
Family::Ipv4
} else {
Family::Ipv6
}
}
pub fn unique(&self) -> RouteUnique {
(self.dst, self.gateway.clone())
}
pub const fn unique_ref(&self) -> (&ipnet::IpNet, &smallvec::SmallVec<[IpAddr; 1]>) {
(&self.dst, &self.gateway)
}
}