use std::net::IpAddr;
use std::sync::Arc;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum HostStatus {
Unknown,
Down,
Filtered,
Up,
}
impl HostStatus {
pub const ALL: [HostStatus; 4] = [Self::Unknown, Self::Down, Self::Filtered, Self::Up];
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum StatusProtocol {
Arp,
Ndp,
IcmpEcho,
IcmpTimestamp,
IcmpUnreachable,
TcpSyn,
Tcp,
Dhcp,
Udp,
Sctp,
Custom(Arc<str>),
}
impl StatusProtocol {
pub const ALL: &'static [StatusProtocol] = &[
Self::Arp,
Self::Ndp,
Self::IcmpEcho,
Self::IcmpTimestamp,
Self::IcmpUnreachable,
Self::TcpSyn,
Self::Tcp,
Self::Dhcp,
Self::Udp,
Self::Sctp,
];
}
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct StatusReason {
pub protocol: StatusProtocol,
pub source: EvidenceSource,
pub details: Option<Arc<str>>,
}
impl StatusReason {
pub fn new(protocol: StatusProtocol, details: impl Into<Arc<str>>) -> Self {
Self {
protocol,
source: EvidenceSource::Host,
details: Some(details.into()),
}
}
pub fn basic(protocol: StatusProtocol) -> Self {
Self {
protocol,
source: EvidenceSource::Host,
details: None,
}
}
pub fn from_source(mut self, source: IpAddr) -> Self {
self.source = EvidenceSource::Intermediary(source);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EvidenceSource {
Host,
Intermediary(IpAddr),
Withheld,
}
impl EvidenceSource {
pub fn address(&self) -> Option<IpAddr> {
match self {
Self::Intermediary(address) => Some(*address),
Self::Host | Self::Withheld => None,
}
}
pub fn is_withheld(&self) -> bool {
*self == Self::Withheld
}
pub(crate) fn withhold(&mut self, keep: impl Fn(&IpAddr) -> bool) -> bool {
match self {
Self::Intermediary(address) if !keep(address) => {
*self = Self::Withheld;
true
}
Self::Host | Self::Intermediary(_) | Self::Withheld => false,
}
}
}
impl HostStatus {
#[inline]
pub fn is_up(&self) -> bool {
matches!(self, HostStatus::Up)
}
#[inline]
pub fn is_down(&self) -> bool {
matches!(self, HostStatus::Down)
}
#[inline]
pub fn is_alive(&self) -> bool {
matches!(self, HostStatus::Up | HostStatus::Filtered)
}
}
impl std::fmt::Display for HostStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HostStatus::Unknown => write!(f, "Unknown"),
HostStatus::Down => write!(f, "Down"),
HostStatus::Filtered => write!(f, "Filtered"),
HostStatus::Up => write!(f, "Up"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_variant_order_ranks_evidence_from_weakest_to_strongest() {
assert!(HostStatus::Unknown < HostStatus::Down);
assert!(HostStatus::Down < HostStatus::Filtered);
assert!(HostStatus::Filtered < HostStatus::Up);
}
#[test]
fn a_filtered_host_counts_as_alive_and_an_unanswered_one_does_not() {
assert!(HostStatus::Up.is_alive());
assert!(HostStatus::Filtered.is_alive());
assert!(!HostStatus::Down.is_alive());
assert!(!HostStatus::Unknown.is_alive());
}
#[test]
fn every_status_renders_under_its_own_name() {
let rendered: Vec<String> = [
HostStatus::Unknown,
HostStatus::Down,
HostStatus::Filtered,
HostStatus::Up,
]
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(rendered, ["Unknown", "Down", "Filtered", "Up"]);
}
#[test]
fn a_reason_records_its_protocol_and_who_it_came_from() {
let unattributed = StatusReason::new(
StatusProtocol::Custom(Arc::from("dns-probe")),
"Resolved A record successfully",
);
assert_eq!(
unattributed.protocol,
StatusProtocol::Custom(Arc::from("dns-probe"))
);
assert_eq!(
unattributed.details.as_deref(),
Some("Resolved A record successfully")
);
assert_eq!(
unattributed.source,
EvidenceSource::Host,
"unqualified means the host answered for itself"
);
let router: IpAddr = "192.0.2.1".parse().expect("a valid address");
let attributed = StatusReason::basic(StatusProtocol::IcmpUnreachable).from_source(router);
assert_eq!(attributed.source, EvidenceSource::Intermediary(router));
assert_eq!(attributed.details, None);
}
#[test]
fn only_a_refused_sender_is_withheld() {
let refused: IpAddr = "198.51.100.1".parse().expect("a valid address");
let allowed: IpAddr = "192.0.2.1".parse().expect("a valid address");
let keep = |address: &IpAddr| *address != refused;
let mut named = EvidenceSource::Intermediary(refused);
assert!(named.withhold(keep));
assert_eq!(named, EvidenceSource::Withheld);
assert_eq!(named.address(), None);
for mut untouched in [
EvidenceSource::Host,
EvidenceSource::Intermediary(allowed),
EvidenceSource::Withheld,
] {
let before = untouched;
assert!(!untouched.withhold(keep), "{before:?}");
assert_eq!(untouched, before);
}
}
}