use dashmap::DashMap;
use std::net::IpAddr;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};
use tokio::sync::broadcast;
use tokio::sync::broadcast::error::{RecvError, TryRecvError};
use tracing::error;
use crate::detect::compute::DetectionRunRecord;
use crate::info;
use crate::journal::settle::{Outcome, Settled, Settlements};
use crate::model::exclusion::Exclusions;
use crate::model::host::Host;
use crate::model::ip::scoped::{ScopedIp, Zone, ZoneMap};
use crate::model::ip::set::Positions;
use crate::model::port::Protocol;
use crate::report::ScannerKind;
use crate::report::{Attachment, AttachmentSource, ProbeStats, Refusal, ScannerFailure};
use crate::scanner::handle::ScanHandle;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ScanEvent {
HostUpdated(ScopedIp),
ScannerFailed {
scanner: ScannerKind,
reason: String,
},
EventsDropped {
count: u64,
},
}
#[derive(Debug, Clone, Default)]
pub struct HostStore {
inner: Arc<DashMap<ScopedIp, Host>>,
}
impl HostStore {
fn new(inner: Arc<DashMap<ScopedIp, Host>>) -> Self {
Self { inner }
}
pub fn get(&self, ip: impl Into<ScopedIp>) -> Option<Host> {
self.inner
.get(&ip.into())
.map(|entry| entry.value().clone())
}
pub fn read<R>(&self, ip: impl Into<ScopedIp>, read: impl FnOnce(&Host) -> R) -> Option<R> {
self.inner.get(&ip.into()).map(|entry| read(entry.value()))
}
pub fn contains(&self, ip: impl Into<ScopedIp>) -> bool {
self.inner.contains_key(&ip.into())
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn snapshot(&self) -> Vec<Host> {
let mut hosts: Vec<Host> = self
.inner
.iter()
.map(|entry| entry.value().clone())
.collect();
hosts.sort_by_cached_key(Host::scoped_ip);
hosts
}
#[cfg(test)]
pub(crate) fn insert(&self, ip: impl Into<ScopedIp>, host: Host) {
self.inner.insert(ip.into(), host);
}
}
#[derive(Debug)]
pub struct ScanEvents {
rx: broadcast::Receiver<ScanEvent>,
}
impl ScanEvents {
pub const CAPACITY: usize = 1024;
pub async fn recv(&mut self) -> Option<ScanEvent> {
match self.rx.recv().await {
Ok(event) => Some(event),
Err(RecvError::Lagged(count)) => Some(ScanEvent::EventsDropped { count }),
Err(RecvError::Closed) => None,
}
}
pub fn try_recv(&mut self) -> Option<ScanEvent> {
match self.rx.try_recv() {
Ok(event) => Some(event),
Err(TryRecvError::Lagged(count)) => Some(ScanEvent::EventsDropped { count }),
Err(TryRecvError::Empty | TryRecvError::Closed) => None,
}
}
}
pub struct ScanSession {
store: HostStore,
events: ScanEvents,
handle: ScanHandle,
}
impl ScanSession {
pub fn hosts(&self) -> &HostStore {
&self.store
}
pub fn events(&mut self) -> &mut ScanEvents {
&mut self.events
}
pub fn handle(&self) -> &ScanHandle {
&self.handle
}
pub fn into_parts(self) -> (HostStore, ScanEvents, ScanHandle) {
(self.store, self.events, self.handle)
}
}
#[derive(Debug, Default)]
pub(crate) struct ProbeStatsLog {
entries: Mutex<Vec<ProbeStats>>,
}
impl ProbeStatsLog {
fn push(&self, stats: ProbeStats) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.push(stats);
}
fn drain(&self) -> Vec<ProbeStats> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries)
}
fn snapshot(&self) -> Vec<ProbeStats> {
self.entries
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
}
#[derive(Default)]
pub(crate) struct Responses {
inner: DashMap<(ScopedIp, u16, Protocol), Vec<String>>,
}
impl Responses {
fn record(&self, ip: ScopedIp, number: u16, protocol: Protocol, banners: Vec<String>) {
if !banners.is_empty() {
self.inner.insert((ip, number, protocol), banners);
}
}
fn take(&self, ip: &ScopedIp, number: u16, protocol: Protocol) -> Vec<String> {
self.inner
.remove(&(ip.clone(), number, protocol))
.map(|(_, banners)| banners)
.unwrap_or_default()
}
}
#[derive(Debug, Default)]
pub(crate) struct Tapes {
inner: Mutex<Vec<DetectionRunRecord>>,
}
impl Tapes {
pub(crate) fn record(&self, run: DetectionRunRecord) {
self.inner
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.push(run);
}
pub(crate) fn take(&self) -> Vec<DetectionRunRecord> {
std::mem::take(
&mut self
.inner
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner),
)
}
}
#[derive(Debug, Default)]
pub(crate) struct RefusalLog {
entries: Mutex<Vec<Refusal>>,
}
impl RefusalLog {
fn push(&self, refusal: Refusal) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
if !entries.contains(&refusal) {
entries.push(refusal);
}
}
fn drain(&self) -> Vec<Refusal> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries)
}
fn snapshot(&self) -> Vec<Refusal> {
self.entries
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
}
#[derive(Debug, Default)]
pub(crate) struct UnroutableLog {
entries: Mutex<std::collections::BTreeSet<IpAddr>>,
}
impl UnroutableLog {
fn insert(&self, address: IpAddr) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.insert(address);
}
fn drain(&self) -> Vec<IpAddr> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries).into_iter().collect()
}
}
#[derive(Debug, Default)]
pub(crate) struct TimedOutLog {
entries: Mutex<std::collections::BTreeSet<IpAddr>>,
}
impl TimedOutLog {
fn insert(&self, address: IpAddr) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.insert(address);
}
fn drain(&self) -> Vec<IpAddr> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries).into_iter().collect()
}
}
#[derive(Debug, Default)]
pub(crate) struct HostClocks {
budget: Option<Duration>,
started: DashMap<IpAddr, Instant>,
}
#[derive(Debug, Default)]
pub(crate) struct HostSpacing {
minimum: Option<Duration>,
last_sent: DashMap<IpAddr, Instant>,
}
impl HostSpacing {
fn ready_at(&self, address: IpAddr, now: Instant) -> Option<Instant> {
let minimum = self.minimum?;
let last = *self.last_sent.get(&address)?;
let ready = last + minimum;
(ready > now).then_some(ready)
}
fn sent(&self, address: IpAddr, now: Instant) {
if self.minimum.is_none() {
return;
}
self.last_sent.insert(address, now);
}
}
impl HostClocks {
fn expired(&self, address: IpAddr) -> bool {
let Some(budget) = self.budget else {
return false;
};
if let Some(started) = self.started.get(&address) {
return started.elapsed() >= budget;
}
let started = *self.started.entry(address).or_insert_with(Instant::now);
started.elapsed() >= budget
}
}
#[derive(Debug, Default)]
pub(crate) struct SweptLinks {
entries: Mutex<std::collections::BTreeMap<String, Zone>>,
}
impl SweptLinks {
fn insert(&self, zone: Zone) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.insert(zone.name().to_owned(), zone);
}
fn drain(&self) -> Vec<Zone> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries).into_values().collect()
}
}
#[derive(Debug, Default)]
pub(crate) struct Attachments {
entries: Mutex<std::collections::BTreeMap<(String, AttachmentSource), Attachment>>,
}
impl Attachments {
fn insert(&self, attachment: Attachment) {
let key = (attachment.link().name().to_owned(), attachment.source());
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.insert(key, attachment);
}
fn drain(&self) -> Vec<Attachment> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries).into_values().collect()
}
}
fn snapshot_of(store: &DashMap<ScopedIp, Host>) -> Vec<Host> {
let mut hosts: Vec<Host> = store.iter().map(|entry| entry.value().clone()).collect();
hosts.sort_by_cached_key(Host::scoped_ip);
hosts
}
fn changed_since(store: &DashMap<ScopedIp, Host>, changed: &ChangedHosts) -> Vec<Host> {
changed
.drain()
.into_iter()
.filter_map(|key| store.get(&key).map(|host| host.clone()))
.collect()
}
#[derive(Debug, Clone)]
pub struct ScanProgress {
store: Arc<DashMap<ScopedIp, Host>>,
changed: Arc<ChangedHosts>,
settlements: Arc<Settlements>,
failures: Arc<FailureLog>,
tapes: Arc<Tapes>,
}
impl ScanProgress {
pub fn settlements(&self) -> &Settlements {
&self.settlements
}
pub fn take_changed_hosts(&self) -> Vec<Host> {
changed_since(&self.store, &self.changed)
}
pub fn take_tapes(&self) -> Vec<DetectionRunRecord> {
self.tapes.take()
}
pub fn hosts_snapshot(&self) -> Vec<Host> {
snapshot_of(&self.store)
}
pub fn host_count(&self) -> usize {
self.store.len()
}
pub fn record_failure(&self, scanner: ScannerKind, reason: String) {
error!("{scanner:?} failed: {reason}");
self.failures.push(ScannerFailure::new(scanner, reason));
}
}
#[derive(Debug, Default)]
pub(crate) struct ChangedHosts {
entries: Mutex<std::collections::BTreeSet<ScopedIp>>,
}
impl ChangedHosts {
fn insert(&self, ip: ScopedIp) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.insert(ip);
}
fn drain(&self) -> Vec<ScopedIp> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries).into_iter().collect()
}
}
#[derive(Debug, Default)]
pub(crate) struct FailureLog {
entries: Mutex<Vec<ScannerFailure>>,
}
impl FailureLog {
fn push(&self, failure: ScannerFailure) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.push(failure);
}
fn drain(&self) -> Vec<ScannerFailure> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *entries)
}
fn snapshot(&self) -> Vec<ScannerFailure> {
self.entries
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
}
#[derive(Clone)]
pub struct ScanContext {
pub(crate) handle: ScanHandle,
pub(crate) store: Arc<DashMap<ScopedIp, Host>>,
pub(crate) events_tx: broadcast::Sender<ScanEvent>,
pub(crate) failures: Arc<FailureLog>,
pub(crate) refusals: Arc<RefusalLog>,
pub(crate) probe_stats: Arc<ProbeStatsLog>,
pub(crate) unroutable: Arc<UnroutableLog>,
pub(crate) timed_out: Arc<TimedOutLog>,
pub(crate) clocks: Arc<HostClocks>,
pub(crate) spacing: Arc<HostSpacing>,
pub(crate) zones: Arc<OnceLock<ZoneMap>>,
pub(crate) swept_links: Arc<SweptLinks>,
pub(crate) attachments: Arc<Attachments>,
pub(crate) exclusions: Arc<Exclusions>,
pub(crate) changed: Arc<ChangedHosts>,
pub(crate) settlements: Arc<Settlements>,
pub(crate) positions: Arc<Positions>,
pub(crate) order_seed: Option<u64>,
pub(crate) responses: Arc<Responses>,
pub(crate) tapes: Arc<Tapes>,
pub(crate) detections: crate::detect::Detections,
}
impl ScanContext {
pub(crate) fn record_responses(
&self,
ip: ScopedIp,
number: u16,
protocol: Protocol,
banners: Vec<String>,
) {
self.responses.record(ip, number, protocol, banners);
}
pub(crate) fn take_responses(
&self,
ip: &ScopedIp,
number: u16,
protocol: Protocol,
) -> Vec<String> {
self.responses.take(ip, number, protocol)
}
pub fn write_host(
&self,
key: impl Into<ScopedIp>,
edit: impl FnOnce(&mut Host) -> bool,
) -> bool {
let key = self.key(key);
let ip = key.addr();
if self.exclusions.excludes(&ip) {
info!(
verbosity = 2,
"excluded address {ip} answered a probe it was not addressed; dropping the finding"
);
return false;
}
let mut is_new = false;
let mut host = self.store.entry(key.clone()).or_insert_with(|| {
is_new = true;
let mut host = Host::new(ip);
if let Some(zone) = key.zone() {
host.set_zone(zone.clone());
}
host
});
let announce = edit(&mut host);
drop(host);
self.changed.insert(key.clone());
if announce || is_new {
let _ = self.events_tx.send(ScanEvent::HostUpdated(key));
}
is_new
}
pub fn read_host<R>(
&self,
ip: impl Into<ScopedIp>,
read: impl FnOnce(&Host) -> R,
) -> Option<R> {
self.store
.get(&self.key(ip))
.map(|entry| read(entry.value()))
}
fn key(&self, ip: impl Into<ScopedIp>) -> ScopedIp {
let key = ip.into();
match key.is_unusable() {
true => self
.zones
.get()
.map_or(key.clone(), |zones| zones.key(key.addr())),
false => key,
}
}
pub(crate) fn learn_zones(&self, zones: ZoneMap) {
let _ = self.zones.set(zones);
}
pub fn contains_host(&self, ip: &ScopedIp) -> bool {
self.store.contains_key(&self.key(ip.clone()))
}
pub fn host_addresses(&self) -> Vec<ScopedIp> {
self.store.iter().map(|entry| entry.key().clone()).collect()
}
pub fn record_failure(&self, scanner: ScannerKind, reason: String) {
error!("scanner {scanner:?} failed: {reason}");
self.failures
.push(ScannerFailure::new(scanner, reason.clone()));
let _ = self
.events_tx
.send(ScanEvent::ScannerFailed { scanner, reason });
}
pub fn record_refusal(&self, refusal: Refusal) {
info!(verbosity = 1, "not covered: {}", refusal.reason());
self.refusals.push(refusal);
}
pub(crate) fn take_refusals(&self) -> Vec<Refusal> {
self.refusals.drain()
}
pub fn refusals_snapshot(&self) -> Vec<Refusal> {
self.refusals.snapshot()
}
pub fn record_probe_stats(&self, stats: ProbeStats) {
self.probe_stats.push(stats);
}
pub(crate) fn take_probe_stats(&self) -> Vec<ProbeStats> {
self.probe_stats.drain()
}
pub fn probe_stats_snapshot(&self) -> Vec<ProbeStats> {
self.probe_stats.snapshot()
}
pub(crate) fn take_failures(&self) -> Vec<ScannerFailure> {
self.failures.drain()
}
pub fn record_unroutable(&self, address: IpAddr) {
self.unroutable.insert(address);
}
pub(crate) fn take_unroutable(&self) -> Vec<IpAddr> {
self.unroutable.drain()
}
pub fn host_expired(&self, address: IpAddr) -> bool {
if !self.clocks.expired(address) {
return false;
}
self.timed_out.insert(address);
true
}
pub fn host_ready_at(&self, address: IpAddr, now: Instant) -> Option<Instant> {
self.spacing.ready_at(address, now)
}
pub fn host_probed(&self, address: IpAddr, now: Instant) {
self.spacing.sent(address, now);
}
pub(crate) fn take_timed_out(&self) -> Vec<IpAddr> {
self.timed_out.drain()
}
pub fn record_sweep(&self, zone: Zone) {
self.swept_links.insert(zone);
}
pub(crate) fn take_swept_links(&self) -> Vec<Zone> {
self.swept_links.drain()
}
pub fn record_attachment(&self, attachment: Attachment) {
self.attachments.insert(attachment);
}
pub(crate) fn take_attachments(&self) -> Vec<Attachment> {
self.attachments.drain()
}
pub fn record_outcome(&self, outcome: Outcome) {
self.settlements.record(outcome);
}
pub fn record_many_outcomes(&self, outcome: Outcome, count: u64) {
self.settlements.record_many(outcome, count);
}
pub fn settle_address(&self, ip: IpAddr, settled: Settled) {
if let Some(position) = self.positions.find(ip) {
self.record_outcome(settled.at(position));
}
}
pub fn settlements(&self) -> &Settlements {
&self.settlements
}
pub fn restore_hosts(&self, hosts: &[Host]) {
for host in hosts {
let key = host.scoped_ip();
match self.store.get_mut(&key) {
Some(mut existing) => existing.merge(host.clone()),
None => {
self.store.insert(key.clone(), host.clone());
}
}
let _ = self.events_tx.send(ScanEvent::HostUpdated(key));
}
}
pub fn progress(&self) -> ScanProgress {
ScanProgress {
store: Arc::clone(&self.store),
changed: Arc::clone(&self.changed),
settlements: Arc::clone(&self.settlements),
failures: Arc::clone(&self.failures),
tapes: Arc::clone(&self.tapes),
}
}
pub fn hosts_snapshot(&self) -> Vec<Host> {
let mut hosts: Vec<Host> = self
.store
.iter()
.map(|entry| entry.value().clone())
.collect();
hosts.sort_by_cached_key(Host::scoped_ip);
hosts
}
pub fn host_count(&self) -> usize {
self.store.len()
}
pub fn take_changed_hosts(&self) -> Vec<Host> {
changed_since(&self.store, &self.changed)
}
pub fn failures_snapshot(&self) -> Vec<ScannerFailure> {
self.failures.snapshot()
}
pub fn update_host(&self, ip: impl Into<ScopedIp>, update: impl FnOnce(&mut Host)) -> bool {
self.write_host(ip, |host| {
update(host);
true
})
}
}
#[must_use]
#[derive(Debug, Default)]
pub struct SessionBuilder {
exclusions: Exclusions,
settled: crate::journal::cursor::Checkpoint,
positions: Positions,
detections: crate::detect::Detections,
host_timeout: Option<Duration>,
scan_timeout: Option<Duration>,
host_probe_interval: Option<Duration>,
order_seed: Option<u64>,
}
impl SessionBuilder {
pub fn excluding(mut self, exclusions: Exclusions) -> Self {
self.exclusions = exclusions;
self
}
pub fn resuming(mut self, settled: &crate::journal::cursor::Checkpoint) -> Self {
self.settled = settled.clone();
self
}
pub fn counting(mut self, positions: Positions) -> Self {
self.positions = positions;
self
}
pub fn ordering(mut self, seed: Option<u64>) -> Self {
self.order_seed = seed;
self
}
pub fn detections(mut self, detections: crate::detect::Detections) -> Self {
self.detections = detections;
self
}
pub fn host_timeout(mut self, budget: Option<Duration>) -> Self {
self.host_timeout = budget;
self
}
pub fn scan_timeout(mut self, budget: Option<Duration>) -> Self {
self.scan_timeout = budget;
self
}
pub fn host_probe_interval(mut self, minimum: Option<Duration>) -> Self {
self.host_probe_interval = minimum;
self
}
pub fn build(self) -> (ScanSession, ScanContext) {
let store = Arc::new(DashMap::new());
let handle = ScanHandle::bounded(self.scan_timeout);
let (events_tx, rx) = broadcast::channel(ScanEvents::CAPACITY);
let session = ScanSession {
store: HostStore::new(store.clone()),
events: ScanEvents { rx },
handle: handle.clone(),
};
let ctx = ScanContext {
handle,
store,
events_tx,
failures: Arc::new(FailureLog::default()),
refusals: Arc::new(RefusalLog::default()),
probe_stats: Arc::new(ProbeStatsLog::default()),
unroutable: Arc::new(UnroutableLog::default()),
timed_out: Arc::new(TimedOutLog::default()),
clocks: Arc::new(HostClocks {
budget: self.host_timeout,
started: DashMap::new(),
}),
spacing: Arc::new(HostSpacing {
minimum: self.host_probe_interval,
last_sent: DashMap::new(),
}),
zones: Arc::new(OnceLock::new()),
swept_links: Arc::new(SweptLinks::default()),
attachments: Arc::new(Attachments::default()),
exclusions: Arc::new(self.exclusions),
changed: Arc::new(ChangedHosts::default()),
settlements: Arc::new(Settlements::resuming(&self.settled)),
positions: Arc::new(self.positions),
order_seed: self.order_seed,
responses: Arc::new(Responses::default()),
tapes: Arc::new(Tapes::default()),
detections: self.detections,
};
(session, ctx)
}
}
impl ScanSession {
pub fn new() -> (Self, ScanContext) {
Self::builder().build()
}
pub fn builder() -> SessionBuilder {
SessionBuilder::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::host::{HostStatus, StatusProtocol, StatusReason};
#[test]
fn an_excluded_address_reaches_neither_the_store_nor_the_stream() {
let excluded: IpAddr = "10.0.5.7".parse().expect("literal");
let allowed: IpAddr = "10.0.6.7".parse().expect("literal");
let mut ips = crate::model::ip::set::IpSet::new();
ips.insert_range("10.0.5.0/24".parse().expect("a valid range"));
let (mut session, ctx) = ScanSession::builder()
.excluding(Exclusions::new(ips))
.build();
assert!(
!ctx.write_host(excluded, |_| unreachable!(
"an excluded address must not reach the caller's edit"
)),
"an excluded address is never reported as a new host"
);
assert!(ctx.write_host(allowed, |_| true));
assert_eq!(ctx.store.len(), 1);
assert!(!ctx.store.contains_key(&ScopedIp::unscoped(excluded)));
let ScanEvent::HostUpdated(announced) =
session.events().try_recv().expect("the allowed host")
else {
panic!("expected a host update");
};
assert_eq!(announced, ScopedIp::unscoped(allowed));
assert!(session.events().try_recv().is_none());
}
#[test]
fn two_link_locals_on_different_segments_are_two_hosts() {
let shared: IpAddr = "fe80::1".parse().expect("literal");
let (_session, ctx) = ScanSession::new();
ctx.write_host(ScopedIp::scoped(shared, Zone::new(1, "en0")), |host| {
host.set_status(HostStatus::Up);
true
});
ctx.write_host(ScopedIp::scoped(shared, Zone::new(2, "en1")), |host| {
host.set_status(HostStatus::Up);
true
});
assert_eq!(ctx.store.len(), 2, "two segments, two machines");
let mut zones: Vec<String> = ctx
.store
.iter()
.filter_map(|entry| entry.value().zone().map(|zone| zone.name().to_owned()))
.collect();
zones.sort();
assert_eq!(zones, ["en0", "en1"]);
}
#[test]
fn a_port_verdict_on_a_bare_link_local_lands_on_the_host_the_scan_named() {
use crate::model::ip::range::Ipv6Range;
let address: IpAddr = "fe80::1".parse().expect("literal");
let (_session, ctx) = ScanSession::new();
let mut zones = ZoneMap::new();
let IpAddr::V6(v6) = address else {
panic!("a v6 literal");
};
zones.insert(
Ipv6Range::scoped(v6, v6, Some(1)).expect("a scoped range"),
&[(1, "en0")],
);
ctx.learn_zones(zones);
ctx.write_host(ScopedIp::scoped(address, Zone::new(1, "en0")), |host| {
host.set_status(HostStatus::Up);
true
});
ctx.write_host(address, |host| {
host.set_status(HostStatus::Up);
true
});
assert_eq!(ctx.store.len(), 1, "one machine, one record");
assert!(
ctx.read_host(address, |host| host.zone().is_some())
.expect("the bare address reaches it too"),
"and it is still the host on en0"
);
}
#[test]
fn one_global_address_seen_on_two_interfaces_is_one_host() {
let global: IpAddr = "2001:db8::1".parse().expect("literal");
let (_session, ctx) = ScanSession::new();
ctx.write_host(ScopedIp::scoped(global, Zone::new(1, "en0")), |_| true);
ctx.write_host(ScopedIp::scoped(global, Zone::new(2, "en1")), |_| true);
assert_eq!(ctx.store.len(), 1, "one address, one machine");
}
#[test]
fn a_finding_written_back_under_a_key_from_the_store_lands_on_the_same_host() {
let shared: IpAddr = "fe80::1".parse().expect("literal");
let (_session, ctx) = ScanSession::new();
ctx.write_host(ScopedIp::scoped(shared, Zone::new(1, "en0")), |host| {
host.set_status(HostStatus::Up);
true
});
for key in ctx.host_addresses() {
ctx.write_host(key, |host| {
host.add_reason(StatusReason::new(StatusProtocol::IcmpEcho, "echo answered"));
true
});
}
assert_eq!(ctx.store.len(), 1, "the same host, enriched");
assert_eq!(
ctx.store
.iter()
.next()
.expect("the host")
.value()
.reasons()
.len(),
1,
"and the finding reached it"
);
}
#[test]
fn a_failure_survives_a_consumer_that_never_listens() {
let (session, ctx) = ScanSession::new();
drop(session);
ctx.record_failure(ScannerKind::Routed, "raw socket unavailable".into());
let failures = ctx.take_failures();
assert_eq!(failures.len(), 1);
assert_eq!(failures[0].scanner(), ScannerKind::Routed);
assert_eq!(failures[0].reason(), "raw socket unavailable");
}
#[test]
fn a_failure_reaches_both_the_log_and_the_stream() {
let (mut session, ctx) = ScanSession::new();
ctx.record_failure(ScannerKind::Local, "eth0: no address".into());
match session.events().try_recv() {
Some(ScanEvent::ScannerFailed { scanner, reason }) => {
assert_eq!(scanner, ScannerKind::Local);
assert_eq!(reason, "eth0: no address");
}
other => panic!("expected a ScannerFailed event, got {other:?}"),
}
assert_eq!(ctx.take_failures().len(), 1);
}
#[test]
fn snapshotting_failures_leaves_them_for_the_report() {
let (_session, ctx) = ScanSession::new();
ctx.record_failure(ScannerKind::Local, "eth0: no address".into());
assert_eq!(ctx.failures_snapshot().len(), 1);
assert_eq!(ctx.failures_snapshot().len(), 1, "reading is not taking");
assert_eq!(ctx.take_failures().len(), 1, "and the report still gets it");
}
#[test]
fn taking_failures_empties_the_log() {
let (_session, ctx) = ScanSession::new();
ctx.record_failure(ScannerKind::Connect, "refused".into());
assert_eq!(ctx.take_failures().len(), 1);
assert!(ctx.take_failures().is_empty());
}
#[test]
fn failures_from_every_clone_land_in_one_log() {
let (_session, ctx) = ScanSession::new();
let clone = ctx.clone();
ctx.record_failure(ScannerKind::Local, "eth0".into());
clone.record_failure(ScannerKind::Routed, "gateway".into());
assert_eq!(ctx.take_failures().len(), 2);
}
#[test]
fn a_resuming_session_starts_from_the_earlier_cursor() {
use crate::journal::cursor::Checkpoint;
let settled = Checkpoint {
watermark: 12,
settled_above: vec![14],
};
let (_session, ctx) = ScanSession::builder()
.excluding(Exclusions::none())
.resuming(&settled)
.build();
assert_eq!(ctx.settlements().settled_count(), 13);
assert_eq!(ctx.settlements().checkpoint(), settled);
let (_session, fresh) = ScanSession::new();
assert_eq!(fresh.settlements().settled_count(), 0);
}
#[test]
fn a_write_nobody_needs_announcing_is_still_a_write() {
let ip: IpAddr = "192.0.2.1".parse().expect("an address");
let (mut session, ctx) = ScanSession::new();
ctx.update_host(ip, |host| host.set_status(HostStatus::Up));
assert_eq!(ctx.take_changed_hosts().len(), 1);
assert!(
session.events().try_recv().is_some(),
"a new host is announced"
);
ctx.write_host(ip, |host| {
host.record_evidence(
HostStatus::Up,
StatusReason::new(StatusProtocol::IcmpEcho, "echo reply to an OS probe"),
);
false
});
let changed = ctx.take_changed_hosts();
assert_eq!(
changed.len(),
1,
"the record moved, so a journal has something to write"
);
assert!(
changed[0]
.reasons()
.iter()
.any(|reason| reason.protocol == StatusProtocol::IcmpEcho),
"and what it writes is what the pass learned"
);
assert!(
session.events().try_recv().is_none(),
"a host already announced is not announced again"
);
}
#[test]
fn a_stream_nobody_reads_drops_the_oldest_and_says_how_many() {
let flooded: IpAddr = "192.0.2.1".parse().expect("literal");
let last: IpAddr = "192.0.2.2".parse().expect("literal");
let (mut session, ctx) = ScanSession::new();
let overflow = 64;
let sent = ScanEvents::CAPACITY + overflow;
for _ in 0..sent - 1 {
ctx.update_host(flooded, |host| host.set_status(HostStatus::Up));
}
ctx.update_host(last, |host| host.set_status(HostStatus::Up));
assert_eq!(
ctx.store.len(),
2,
"a full event buffer does not stop the scan writing findings"
);
let mut dropped = 0u64;
let mut delivered = Vec::new();
while let Some(event) = session.events().try_recv() {
match event {
ScanEvent::EventsDropped { count } => dropped += count,
ScanEvent::HostUpdated(ip) => delivered.push(ip),
other => panic!("unexpected event {other:?}"),
}
}
assert_eq!(
delivered.len(),
ScanEvents::CAPACITY,
"the stream holds its stated capacity and no more"
);
assert_eq!(
dropped as usize + delivered.len(),
sent,
"every event is either delivered or declared missing"
);
assert_eq!(
delivered.last(),
Some(&ScopedIp::unscoped(last)),
"the newest event survives, which is what drop-oldest means"
);
}
#[test]
fn a_scan_with_no_host_budget_leaves_no_host() {
let (_session, ctx) = ScanSession::new();
let ip: IpAddr = "192.0.2.1".parse().expect("an address");
assert!(!ctx.host_expired(ip));
assert!(!ctx.host_expired(ip));
assert!(ctx.take_timed_out().is_empty());
}
#[test]
fn a_spent_host_budget_is_filed_once() {
let (_session, ctx) = ScanSession::builder()
.host_timeout(Some(Duration::ZERO))
.build();
let ip: IpAddr = "192.0.2.1".parse().expect("an address");
assert!(ctx.host_expired(ip));
assert!(ctx.host_expired(ip));
assert_eq!(ctx.take_timed_out(), vec![ip]);
}
#[test]
fn a_scan_with_no_gap_never_holds_a_probe() {
let (_session, ctx) = ScanSession::new();
let ip: IpAddr = "192.0.2.1".parse().expect("an address");
let now = Instant::now();
assert!(ctx.host_ready_at(ip, now).is_none());
ctx.host_probed(ip, now);
assert!(
ctx.host_ready_at(ip, now).is_none(),
"a recorded send moves nothing while there is no gap to move it against"
);
}
#[test]
fn a_gap_starts_at_the_first_probe_and_runs_from_the_last() {
let gap = Duration::from_secs(3600);
let (_session, ctx) = ScanSession::builder()
.host_probe_interval(Some(gap))
.build();
let ip: IpAddr = "192.0.2.1".parse().expect("an address");
let now = Instant::now();
assert!(
ctx.host_ready_at(ip, now).is_none(),
"an address nothing has probed is ready"
);
ctx.host_probed(ip, now);
let ready = ctx.host_ready_at(ip, now).expect("asked too recently");
assert_eq!(ready, now + gap);
assert!(
ctx.host_ready_at(ip, now + gap).is_none(),
"the gap having run, the host is ready again"
);
}
#[test]
fn a_gap_at_one_host_leaves_every_other_host_ready() {
let (_session, ctx) = ScanSession::builder()
.host_probe_interval(Some(Duration::from_secs(3600)))
.build();
let probed: IpAddr = "192.0.2.1".parse().expect("an address");
let other: IpAddr = "192.0.2.2".parse().expect("an address");
let now = Instant::now();
ctx.host_probed(probed, now);
assert!(ctx.host_ready_at(probed, now).is_some());
assert!(ctx.host_ready_at(other, now).is_none());
}
#[test]
fn a_budget_still_running_leaves_the_host_alone() {
let (_session, ctx) = ScanSession::builder()
.host_timeout(Some(Duration::from_secs(3600)))
.build();
let ip: IpAddr = "192.0.2.1".parse().expect("an address");
assert!(!ctx.host_expired(ip));
assert!(ctx.take_timed_out().is_empty());
}
}