use super::NetworkEventOptions;
use crate::NetError;
use std::time::SystemTime;
use tokio::sync::Semaphore;
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct InterfaceEventOptions {
pub wlan: bool,
pub default_route: bool,
pub capacity: usize,
pub max_bytes: usize,
}
impl Default for InterfaceEventOptions {
fn default() -> Self {
let queue = NetworkEventOptions::default();
Self {
wlan: true,
default_route: true,
capacity: queue.capacity,
max_bytes: queue.max_bytes,
}
}
}
impl InterfaceEventOptions {
pub(crate) fn validate(&self) -> Result<(), NetError> {
if !self.wlan && !self.default_route {
return Err(NetError::config(
"interface_events.sources",
"select at least one source",
));
}
if self.capacity == 0 || self.capacity > Semaphore::MAX_PERMITS {
return Err(NetError::config(
"interface_events.capacity",
"must be a positive supported capacity",
));
}
if self.max_bytes == 0
|| self.max_bytes > Semaphore::MAX_PERMITS
|| self.max_bytes > u32::MAX as usize
{
return Err(NetError::config(
"interface_events.max_bytes",
"must be a positive supported byte allowance",
));
}
Ok(())
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum InterfaceEventSource {
Wlan,
DefaultRoute,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum InterfaceAddressFamily {
Ipv4,
Ipv6,
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DefaultRouteState {
NoRoute,
Ambiguous,
Unique { interface_id: String },
}
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum InterfaceEventKind {
SourceReady,
SourceUnavailable {
error: NetError,
native_code: Option<i64>,
},
SourceGap {
error: NetError,
native_code: Option<i64>,
},
WlanConnected {
interface_id: String,
native_code: u32,
reason_code: Option<u32>,
},
WlanDisconnected {
interface_id: String,
native_code: u32,
reason_code: Option<u32>,
},
DefaultRouteObserved {
family: InterfaceAddressFamily,
state: DefaultRouteState,
},
DefaultRouteChanged {
family: InterfaceAddressFamily,
previous: String,
current: String,
},
DefaultRouteQueryFailed {
family: InterfaceAddressFamily,
error: NetError,
native_code: Option<i64>,
},
}
#[derive(Clone, Debug)]
pub struct InterfaceEvent {
pub sequence: u64,
pub observed_at: SystemTime,
pub native_time: Option<SystemTime>,
pub source: InterfaceEventSource,
pub kind: InterfaceEventKind,
}
#[cfg(windows)]
impl InterfaceEvent {
pub(crate) fn accounted_bytes(&self) -> usize {
use InterfaceEventKind::*;
let extra = match &self.kind {
WlanConnected { interface_id, .. }
| WlanDisconnected { interface_id, .. }
| DefaultRouteObserved {
state: DefaultRouteState::Unique { interface_id },
..
} => interface_id.capacity(),
DefaultRouteChanged {
previous, current, ..
} => previous.capacity().saturating_add(current.capacity()),
#[cfg(feature = "ws-client")]
SourceUnavailable { .. } | SourceGap { .. } | DefaultRouteQueryFailed { .. } => {
std::mem::size_of::<crate::error::ErrorContext>()
}
_ => 0,
};
std::mem::size_of::<Self>().saturating_add(extra)
}
}