use dashmap::DashMap;
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::sync::atomic::{AtomicU8, AtomicU64, AtomicUsize, Ordering};
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::config::ServiceDetection;
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::range::IpRange;
use crate::model::ip::scoped::{ScopedIp, Zone, ZoneMap};
use crate::model::ip::set::{IpSet, Positions};
use crate::model::port::Protocol;
use crate::report::ScannerKind;
use crate::report::{Attachment, AttachmentSource, ProbeStats, Refusal, ScannerFailure};
use crate::scanner::handle::ScanHandle;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum Stage {
#[default]
Discovery,
Ports,
Services,
Detections,
Tls,
Os,
Traceroute,
Listening,
Finishing,
}
impl Stage {
pub const ALL: [Stage; 9] = [
Self::Discovery,
Self::Ports,
Self::Services,
Self::Detections,
Self::Tls,
Self::Os,
Self::Traceroute,
Self::Listening,
Self::Finishing,
];
const fn code(self) -> u8 {
match self {
Stage::Discovery => 0,
Stage::Ports => 1,
Stage::Services => 2,
Stage::Detections => 3,
Stage::Tls => 4,
Stage::Os => 5,
Stage::Traceroute => 6,
Stage::Listening => 7,
Stage::Finishing => 8,
}
}
const fn from_code(code: u8) -> Self {
match code {
1 => Stage::Ports,
2 => Stage::Services,
3 => Stage::Detections,
4 => Stage::Tls,
5 => Stage::Os,
6 => Stage::Traceroute,
7 => Stage::Listening,
8 => Stage::Finishing,
_ => Stage::Discovery,
}
}
}
impl std::fmt::Display for Stage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
Stage::Discovery => "discovery",
Stage::Ports => "ports",
Stage::Services => "services",
Stage::Detections => "detections",
Stage::Tls => "tls",
Stage::Os => "os",
Stage::Traceroute => "traceroute",
Stage::Listening => "listening",
Stage::Finishing => "finishing",
};
f.write_str(name)
}
}
#[derive(Debug, Default)]
pub(crate) struct Stages {
current: AtomicU8,
done: AtomicU64,
total: AtomicU64,
planned: Vec<Stage>,
reached: AtomicUsize,
}
impl Stages {
fn new(planned: Vec<Stage>) -> Self {
Self {
planned,
..Self::default()
}
}
fn overall(&self, within: Option<(u64, u64)>) -> Option<(u64, u64)> {
let stages = u64::try_from(self.planned.len()).ok().filter(|n| *n > 0)?;
let reached = u64::try_from(self.reached.load(Ordering::Relaxed)).unwrap_or(0);
let Some((done, total)) = within.filter(|_| self.planned.contains(&self.stage())) else {
return Some((reached.min(stages), stages));
};
Some((reached * total + done.min(total), stages * total))
}
fn enter(&self, stage: Stage, total: Option<u64>) -> bool {
let total = total.unwrap_or(0);
let reached = self
.planned
.iter()
.filter(|planned| planned.code() < stage.code())
.count();
self.reached.fetch_max(reached, Ordering::Relaxed);
if self.current.swap(stage.code(), Ordering::Relaxed) == stage.code() {
self.total.fetch_add(total, Ordering::Relaxed);
return false;
}
self.done.store(0, Ordering::Relaxed);
self.total.store(total, Ordering::Relaxed);
true
}
fn advance(&self) {
self.done.fetch_add(1, Ordering::Relaxed);
}
fn stage(&self) -> Stage {
Stage::from_code(self.current.load(Ordering::Relaxed))
}
fn done(&self) -> u64 {
self.done.load(Ordering::Relaxed)
}
fn total(&self) -> Option<u64> {
match self.total.load(Ordering::Relaxed) {
0 => None,
total => Some(total),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ScanEvent {
HostUpdated(ScopedIp),
ScannerFailed {
scanner: ScannerKind,
reason: String,
},
StageChanged {
stage: Stage,
},
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,
}
}
}
#[derive(Debug, Clone)]
pub struct Progress {
settlements: Arc<Settlements>,
planned: Option<u64>,
plan_stage: Stage,
stages: Arc<Stages>,
}
impl Progress {
fn new(
settlements: Arc<Settlements>,
planned: Option<u64>,
plan_stage: Stage,
stages: Arc<Stages>,
) -> Self {
Self {
settlements,
planned,
plan_stage,
stages,
}
}
pub fn stage(&self) -> Stage {
self.stages.stage()
}
pub fn stage_done(&self) -> u64 {
self.stages.done()
}
pub fn stage_total(&self) -> Option<u64> {
self.stages.total()
}
pub fn settled(&self) -> u64 {
self.settlements.settled_count()
}
pub const fn planned(&self) -> Option<u64> {
self.planned
}
pub fn remaining(&self) -> Option<u64> {
self.planned
.map(|planned| planned.saturating_sub(self.settled()))
}
pub fn fraction(&self) -> Option<f64> {
let (done, total) = self.counted()?;
Some(ratio(done, total))
}
pub fn overall(&self) -> Option<(u64, u64)> {
self.stages.overall(self.counted())
}
pub fn counted(&self) -> Option<(u64, u64)> {
match self.stage_total() {
Some(total) => Some((self.stage_done(), total)),
None if self.stage() == self.plan_stage => Some((self.settled(), self.planned?)),
None => None,
}
}
}
fn ratio(done: u64, total: u64) -> f64 {
if total == 0 {
return 1.0;
}
(done as f64 / total as f64).min(1.0)
}
pub struct ScanSession {
store: HostStore,
events: ScanEvents,
handle: ScanHandle,
progress: Progress,
}
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 progress(&self) -> &Progress {
&self.progress
}
pub fn into_parts(self) -> (HostStore, ScanEvents, ScanHandle, Progress) {
(self.store, self.events, self.handle, self.progress)
}
}
#[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 contains(&self, address: IpAddr) -> bool {
let entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.contains(&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 UnsweptLog {
entries: Mutex<std::collections::BTreeSet<IpAddr>>,
}
impl UnsweptLog {
fn insert(&self, address: IpAddr) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.insert(address);
}
fn snapshot(&self) -> Vec<IpAddr> {
let entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.iter().copied().collect()
}
}
#[derive(Debug, Default)]
pub(crate) struct RateLimitedLog {
entries: Mutex<std::collections::BTreeSet<IpAddr>>,
}
impl RateLimitedLog {
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 contains(&self, address: IpAddr) -> bool {
let entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.contains(&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 SilenceLog {
entries: Mutex<Silence>,
}
#[derive(Debug, Default)]
struct Silence {
set: IpSet,
merged: usize,
added: usize,
}
impl SilenceLog {
const MERGE_AFTER: usize = 4096;
fn insert(&self, address: IpAddr) {
let mut silence = self.entries.lock().unwrap_or_else(|e| e.into_inner());
silence.set.insert(address);
silence.added += 1;
if silence.added >= Self::MERGE_AFTER.max(silence.merged) {
silence.set.canonicalize();
silence.merged = silence.set.v4().len() + silence.set.v6().len();
silence.added = 0;
}
}
fn drain(&self) -> IpSet {
let mut silence = self.entries.lock().unwrap_or_else(|e| e.into_inner());
let mut taken = std::mem::take(&mut *silence).set;
taken.canonicalize();
taken
}
}
#[derive(Debug, Default)]
pub(crate) struct ConnectLog {
entries: Mutex<IpSet>,
}
impl ConnectLog {
fn extend(&self, targets: &IpSet) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
for range in targets.v4() {
entries.push_v4_range(*range);
}
for range in targets.v6() {
entries.push_v6_range(*range);
}
}
fn drain(&self) -> Vec<IpRange> {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
let mut taken = std::mem::take(&mut *entries);
taken.canonicalize();
let v4 = taken.v4().iter().copied().map(IpRange::V4);
let v6 = taken.v6().iter().copied().map(IpRange::V6);
v4.chain(v6).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 = crate::scanner::pacing::timer::later(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) icmp_rate_limited: Arc<RateLimitedLog>,
pub(crate) unswept: Arc<UnsweptLog>,
pub(crate) reached_by_connect: Arc<ConnectLog>,
pub(crate) silent: Arc<SilenceLog>,
pub(crate) undecided: Arc<SilenceLog>,
pub(crate) plan_stage: Stage,
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) stages: Arc<Stages>,
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,
pub(crate) forced: Arc<crate::transport::dial::ForcedSources>,
pub(crate) listen_only: Arc<BTreeSet<u16>>,
}
impl ScanContext {
pub(crate) fn egress_toward(&self, target: IpAddr) -> crate::transport::dial::Egress {
self.forced.toward(target)
}
pub(crate) fn listens_only(&self, number: u16, protocol: Protocol) -> bool {
protocol == Protocol::Tcp && self.listen_only.contains(&number)
}
pub(crate) fn service_detection_on(
&self,
detection: ServiceDetection,
number: u16,
protocol: Protocol,
) -> ServiceDetection {
if self.listens_only(number, protocol) {
detection.min(ServiceDetection::Banner)
} else {
detection
}
}
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 may_probe(&self, address: &IpAddr) -> bool {
!self.exclusions.excludes(address)
}
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);
if !self.exclusions.is_empty() {
let keep = |address: &IpAddr| !self.exclusions.excludes(address);
let before = host.ips().len();
host.retain_ips(keep);
if host.ips().len() < before {
info!(
verbosity = 2,
"an excluded address arrived beside {ip}; leaving it off the host"
);
}
if host.withhold_intermediaries(keep) {
info!(
verbosity = 2,
"an excluded router answered for a probe to {ip}; withholding its address"
);
}
}
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(crate) fn record_cut_short(&self, scanner: ScannerKind, reason: String) {
crate::warn!("{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 fn record_unswept(&self, address: IpAddr) {
self.unswept.insert(address);
}
pub fn unswept(&self) -> Vec<IpAddr> {
self.unswept.snapshot()
}
pub(crate) fn take_unroutable(&self) -> Vec<IpAddr> {
self.unroutable.drain()
}
pub(crate) fn is_unroutable(&self, address: IpAddr) -> bool {
self.unroutable.contains(address)
}
pub(crate) fn left_early(&self, address: IpAddr) -> bool {
self.timed_out.contains(address)
}
pub(crate) fn forget_silent(&self, keys: Vec<ScopedIp>) {
for key in keys {
self.silent.insert(key.addr());
self.store.remove(&key);
}
}
pub(crate) fn forget_undecided(&self, keys: Vec<ScopedIp>) {
for key in keys {
self.undecided.insert(key.addr());
self.store.remove(&key);
}
}
pub(crate) fn take_undecided(&self) -> IpSet {
self.undecided.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 host_probe_interval(&self) -> Option<Duration> {
self.spacing.minimum
}
pub(crate) fn take_timed_out(&self) -> Vec<IpAddr> {
self.timed_out.drain()
}
pub(crate) fn record_icmp_rate_limited(&self, address: IpAddr) {
self.icmp_rate_limited.insert(address);
}
pub(crate) fn take_icmp_rate_limited(&self) -> Vec<IpAddr> {
self.icmp_rate_limited.drain()
}
pub fn record_reached_by_connect(&self, targets: &IpSet) {
self.reached_by_connect.extend(targets);
}
pub(crate) fn take_reached_by_connect(&self) -> Vec<IpRange> {
self.reached_by_connect.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 enter_stage(&self, stage: Stage, total: Option<u64>) {
if self.stages.enter(stage, total) {
let _ = self.events_tx.send(ScanEvent::StageChanged { stage });
}
}
pub fn stage_advanced(&self) {
self.stages.advance();
}
pub fn record_many_outcomes(&self, outcome: Outcome, count: u64) {
self.settlements.record_many(outcome, count);
}
pub(crate) fn record_address_outcomes(&self, outcome: Outcome, count: u64) {
if self.plan_stage != Stage::Ports {
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));
}
if settled == Settled::Exhausted {
self.silent.insert(ip);
}
}
pub(crate) fn take_silent(&self) -> IpSet {
self.silent.drain()
}
pub fn settlements(&self) -> &Settlements {
&self.settlements
}
pub fn restore_hosts(&self, hosts: &[Host]) {
let keep = |address: &IpAddr| !self.exclusions.excludes(address);
for host in hosts {
let mut host = host.clone();
if !host.retain_ips(keep) {
info!(
verbosity = 2,
"every address of {} in the journal is excluded; leaving it out of this sitting",
host.primary_ip()
);
continue;
}
host.withhold_intermediaries(keep);
let key = host.scoped_ip();
match self.store.get_mut(&key) {
Some(mut existing) => existing.merge(host),
None => {
self.store.insert(key.clone(), host);
}
}
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,
planned: Option<u64>,
plan_stage: Stage,
staging: Vec<Stage>,
detections: crate::detect::Detections,
host_timeout: Option<Duration>,
scan_timeout: Option<Duration>,
host_probe_interval: Option<Duration>,
order_seed: Option<u64>,
send_source: Vec<IpAddr>,
listen_only: Option<BTreeSet<u16>>,
}
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 planning(mut self, stage: Stage, total: Option<u64>) -> Self {
self.plan_stage = stage;
self.planned = total;
self
}
pub fn staging(mut self, stages: Vec<Stage>) -> Self {
self.staging = stages;
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 send_source(mut self, sources: Vec<IpAddr>) -> Self {
self.send_source = sources;
self
}
pub fn listening_only_to(mut self, ports: BTreeSet<u16>) -> Self {
self.listen_only = Some(ports);
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 settlements = Arc::new(Settlements::resuming(&self.settled));
let stages = Arc::new(Stages::new(self.staging));
let session = ScanSession {
store: HostStore::new(store.clone()),
events: ScanEvents { rx },
handle: handle.clone(),
progress: Progress::new(
settlements.clone(),
self.planned,
self.plan_stage,
stages.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()),
icmp_rate_limited: Arc::new(RateLimitedLog::default()),
unswept: Arc::new(UnsweptLog::default()),
reached_by_connect: Arc::new(ConnectLog::default()),
silent: Arc::new(SilenceLog::default()),
undecided: Arc::new(SilenceLog::default()),
plan_stage: self.plan_stage,
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()),
stages,
settlements,
positions: Arc::new(self.positions),
order_seed: self.order_seed,
responses: Arc::new(Responses::default()),
tapes: Arc::new(Tapes::default()),
detections: self.detections,
forced: Arc::new(crate::transport::dial::ForcedSources::new(
&self.send_source,
)),
listen_only: Arc::new(
self.listen_only
.unwrap_or_else(|| crate::config::RAW_PRINT_PORTS.into_iter().collect()),
),
};
(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::{EvidenceSource, Hop, HostStatus, StatusProtocol, StatusReason};
#[test]
fn a_whole_run_reads_as_one_figure_rather_than_one_per_stage() {
let (session, ctx) = ScanSession::builder()
.planning(Stage::Ports, Some(4))
.staging(vec![Stage::Ports, Stage::Services, Stage::Detections])
.build();
ctx.enter_stage(Stage::Ports, None);
ctx.record_outcome(Outcome::Answered { position: 0 });
ctx.record_outcome(Outcome::Answered { position: 1 });
let (done, total) = session.progress().overall().expect("a staged run");
assert_eq!(
done * 6,
total,
"half of one stage in three: {done}/{total}"
);
ctx.enter_stage(Stage::Services, Some(2));
ctx.stage_advanced();
let (done, total) = session.progress().overall().expect("a staged run");
assert_eq!(done * 2, total, "half the run: {done}/{total}");
}
#[test]
fn a_stage_with_nothing_to_do_is_stepped_over_rather_than_waited_for() {
let (session, ctx) = ScanSession::builder()
.staging(vec![
Stage::Ports,
Stage::Services,
Stage::Detections,
Stage::Os,
])
.build();
ctx.enter_stage(Stage::Ports, Some(1));
ctx.stage_advanced();
ctx.enter_stage(Stage::Os, None);
let (done, total) = session.progress().overall().expect("a staged run");
assert_eq!(done * 4, total * 3, "three stages of four: {done}/{total}");
}
#[test]
fn a_session_with_no_running_order_reports_no_whole_run_figure() {
let (session, ctx) = ScanSession::new();
ctx.enter_stage(Stage::Services, Some(4));
ctx.stage_advanced();
let progress = session.progress();
assert_eq!(progress.overall(), None, "nothing said how many stages");
assert_eq!(
progress.counted(),
Some((1, 4)),
"the stage still answers for itself"
);
}
#[test]
fn a_stage_that_counted_itself_answers_for_itself() {
let (session, ctx) = ScanSession::builder()
.planning(Stage::Ports, Some(4))
.build();
ctx.enter_stage(Stage::Ports, None);
ctx.record_outcome(Outcome::Answered { position: 0 });
ctx.record_outcome(Outcome::Answered { position: 1 });
ctx.record_outcome(Outcome::Answered { position: 2 });
ctx.record_outcome(Outcome::Answered { position: 3 });
assert_eq!(
session.progress().fraction(),
Some(1.0),
"the ports are done"
);
ctx.enter_stage(Stage::Detections, Some(8));
ctx.stage_advanced();
ctx.stage_advanced();
let progress = session.progress();
assert_eq!(progress.stage(), Stage::Detections);
assert_eq!(progress.stage_done(), 2);
assert_eq!(progress.stage_total(), Some(8));
assert_eq!(
progress.fraction(),
Some(0.25),
"a quarter through the detections, not finished with the scan"
);
assert_eq!(progress.settled(), 4, "the plan is still settled in full");
}
#[test]
fn an_unsized_stage_that_is_not_the_plans_own_reports_nothing() {
let (session, ctx) = ScanSession::builder()
.planning(Stage::Ports, Some(1_000))
.build();
ctx.enter_stage(Stage::Discovery, None);
let progress = session.progress();
assert_eq!(progress.stage(), Stage::Discovery);
assert_eq!(
progress.fraction(),
None,
"a liveness pass is not nought percent of a port plan"
);
ctx.enter_stage(Stage::Ports, None);
assert_eq!(
session.progress().fraction(),
Some(0.0),
"the stage the plan was drawn for reads from the plan"
);
}
#[test]
fn re_entering_a_stage_adds_to_it_rather_than_starting_it_over() {
let (mut session, ctx) = ScanSession::new();
ctx.enter_stage(Stage::Services, Some(3));
ctx.stage_advanced();
ctx.enter_stage(Stage::Services, Some(2));
let progress = session.progress().clone();
assert_eq!(progress.stage_total(), Some(5), "both runs' ports");
assert_eq!(progress.stage_done(), 1, "and the one already finished");
let mut announced = 0;
while let Some(event) = session.events().try_recv() {
if matches!(event, ScanEvent::StageChanged { .. }) {
announced += 1;
}
}
assert_eq!(announced, 1, "one stage, announced once");
}
#[test]
fn the_list_of_stages_holds_every_one_of_them_in_running_order() {
let codes: Vec<u8> = Stage::ALL.iter().map(|stage| stage.code()).collect();
let mut sorted = codes.clone();
sorted.sort_unstable();
sorted.dedup();
assert_eq!(codes, sorted, "the stages are listed in the order they run");
assert_eq!(
codes.len(),
Stage::ALL.len(),
"a stage is listed once and no stage twice"
);
for stage in Stage::ALL {
assert_eq!(
Stage::from_code(stage.code()),
stage,
"{stage} does not survive its own code"
);
}
}
#[test]
fn a_stage_announces_itself_as_it_begins() {
let (mut session, ctx) = ScanSession::new();
ctx.enter_stage(Stage::Os, None);
let Some(ScanEvent::StageChanged { stage }) = session.events().try_recv() else {
panic!("entering a stage announces it");
};
assert_eq!(stage, Stage::Os);
}
#[test]
fn an_uncounted_plan_settles_targets_but_reports_no_fraction() {
let (session, ctx) = ScanSession::new();
ctx.record_outcome(Outcome::Answered { position: 0 });
ctx.record_outcome(Outcome::Exhausted { position: 1 });
let progress = session.progress();
assert_eq!(progress.settled(), 2, "both targets settled");
assert_eq!(
progress.planned(),
None,
"nothing said how big the plan was"
);
assert_eq!(
progress.fraction(),
None,
"so there is nothing to divide into"
);
assert_eq!(progress.remaining(), None);
}
#[test]
fn settling_targets_advances_the_fraction() {
let (session, ctx) = ScanSession::builder()
.planning(Stage::Discovery, Some(4))
.build();
assert_eq!(
session.progress().fraction(),
Some(0.0),
"nothing settled yet"
);
ctx.record_outcome(Outcome::Answered { position: 0 });
ctx.record_outcome(Outcome::Skipped { position: 1 });
let progress = session.progress();
assert_eq!(progress.settled(), 2);
assert_eq!(progress.remaining(), Some(2));
let fraction = progress.fraction().expect("a counted plan has a fraction");
assert!(
(fraction - 0.5).abs() < f64::EPSILON,
"two of four settled is half the plan, got {fraction}"
);
}
#[test]
fn an_unsettled_outcome_does_not_advance_the_plan() {
let (session, ctx) = ScanSession::builder()
.planning(Stage::Discovery, Some(2))
.build();
ctx.record_outcome(Outcome::Answered { position: 0 });
ctx.record_many_outcomes(Outcome::Unasked, 1);
let progress = session.progress();
assert_eq!(progress.settled(), 1, "only the answered target settled");
assert_eq!(
progress.remaining(),
Some(1),
"the unasked one is still owed"
);
}
#[test]
fn a_resumed_sitting_starts_part_way_through_the_plan() {
let earlier = crate::journal::cursor::Checkpoint::new(6, []);
let (session, ctx) = ScanSession::builder()
.resuming(&earlier)
.planning(Stage::Discovery, Some(8))
.build();
assert_eq!(
session.progress().settled(),
6,
"what the earlier sitting settled is already covered"
);
ctx.record_outcome(Outcome::Answered { position: 6 });
let fraction = session
.progress()
.fraction()
.expect("a counted plan has a fraction");
assert!(
(fraction - 0.875).abs() < f64::EPSILON,
"seven of eight, got {fraction}"
);
}
#[test]
fn a_fraction_saturates_rather_than_passing_one() {
let (empty, _ctx) = ScanSession::builder()
.planning(Stage::Discovery, Some(0))
.build();
assert_eq!(
empty.progress().fraction(),
Some(1.0),
"an empty plan is complete"
);
let (session, ctx) = ScanSession::builder()
.planning(Stage::Discovery, Some(1))
.build();
ctx.record_outcome(Outcome::Answered { position: 0 });
ctx.record_outcome(Outcome::Answered { position: 1 });
assert_eq!(
session.progress().fraction(),
Some(1.0),
"more settled than planned still reads as done"
);
assert_eq!(session.progress().remaining(), Some(0));
}
#[test]
fn an_excluded_address_reaches_neither_the_store_nor_the_stream() {
let excluded: IpAddr = "198.51.100.7".parse().expect("literal");
let allowed: IpAddr = "203.0.113.7".parse().expect("literal");
let mut ips = crate::model::ip::set::IpSet::new();
ips.insert_range("198.51.100.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 the_longest_gap_holds_a_host_rather_than_panicking() {
let (_session, ctx) = ScanSession::builder()
.host_probe_interval(Some(Duration::MAX))
.build();
let ip: IpAddr = "192.0.2.1".parse().expect("an address");
let now = Instant::now();
ctx.host_probed(ip, now);
let ready = ctx.host_ready_at(ip, now).expect("asked too recently");
assert!(ready > now + Duration::from_secs(365 * 24 * 60 * 60));
}
#[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());
}
#[test]
fn a_resume_leaves_out_an_address_this_sitting_may_not_report() {
let excluded: IpAddr = "198.51.100.7".parse().expect("literal");
let allowed: IpAddr = "203.0.113.7".parse().expect("literal");
let mut ips = crate::model::ip::set::IpSet::new();
ips.insert_range("198.51.100.0/24".parse().expect("a valid range"));
let (mut session, ctx) = ScanSession::builder()
.excluding(Exclusions::new(ips))
.build();
let restored = |ip: IpAddr| {
let mut host = Host::new(ip);
host.set_status(HostStatus::Up);
host
};
ctx.restore_hosts(&[restored(excluded), restored(allowed)]);
assert!(
!ctx.store.contains_key(&ScopedIp::unscoped(excluded)),
"the journal's own record of an excluded address must not come back"
);
assert!(
ctx.store.contains_key(&ScopedIp::unscoped(allowed)),
"and everything else still does"
);
assert_eq!(ctx.store.len(), 1);
let Some(ScanEvent::HostUpdated(announced)) = session.events().try_recv() else {
panic!("a restored host is announced");
};
assert_eq!(announced.addr(), allowed);
assert!(
session.events().try_recv().is_none(),
"an excluded address is not announced either"
);
}
fn forbidding(address: IpAddr) -> (ScanSession, ScanContext) {
let mut ips = crate::model::ip::set::IpSet::new();
ips.insert(address);
ScanSession::builder()
.excluding(Exclusions::new(ips))
.build()
}
#[test]
fn an_address_an_edit_attaches_is_held_to_the_exclusions() {
let key: IpAddr = "192.0.2.60".parse().expect("literal");
let excluded: IpAddr = "192.0.2.61".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
ctx.write_host(key, |host| host.add_ip(excluded));
let ips = ctx
.read_host(key, |host| host.ips().clone())
.expect("the permitted key is recorded");
assert!(!ips.contains(&excluded), "{ips:?}");
assert!(ips.contains(&key));
}
#[test]
fn an_excluded_address_does_not_become_the_one_a_host_is_reached_at() {
let key: IpAddr = "fe80::10".parse().expect("literal");
let excluded: IpAddr = "2001:db8::5".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
ctx.write_host(key, |host| host.consider_primary_ip(excluded));
let (primary, ips) = ctx
.read_host(key, |host| (host.primary_ip(), host.ips().clone()))
.expect("the permitted key is recorded");
assert_eq!(primary, key, "led by the address the policy allows");
assert!(!ips.contains(&excluded), "{ips:?}");
}
fn traced_through(target: IpAddr, router: IpAddr) -> [Hop; 3] {
[
Hop::answered(
1,
"203.0.113.1".parse().expect("literal"),
Some(Duration::from_millis(1)),
),
Hop::answered(2, router, Some(Duration::from_millis(4))),
Hop::answered(3, target, None),
]
}
#[test]
fn a_router_the_exclusions_forbid_is_withheld_from_a_traced_hosts_path() {
let target: IpAddr = "203.0.113.9".parse().expect("literal");
let excluded: IpAddr = "198.51.100.1".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
ctx.write_host(target, |host| {
for hop in traced_through(target, excluded) {
host.record_hop(hop);
}
true
});
let path = ctx
.read_host(target, |host| host.path().clone())
.expect("the traced host is recorded");
assert!(
path.hops()
.iter()
.all(|hop| hop.address() != Some(excluded)),
"{path:?}"
);
let at_two = path.hops()[1];
assert_eq!(at_two.distance(), 2, "the router keeps its place");
assert!(at_two.is_withheld(), "a router answered there: {at_two:?}");
assert_eq!(at_two.rtt(), None, "its timing is a fact about the router");
assert_eq!(path.length(), Some(3));
assert_eq!(path.at(1), Some("203.0.113.1".parse().expect("literal")));
}
#[test]
fn a_resume_withholds_a_router_this_sitting_may_not_report() {
let target: IpAddr = "203.0.113.9".parse().expect("literal");
let excluded: IpAddr = "198.51.100.1".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
let mut journalled = Host::new(target);
for hop in traced_through(target, excluded) {
journalled.record_hop(hop);
}
ctx.restore_hosts(&[journalled]);
let path = ctx
.read_host(target, |host| host.path().clone())
.expect("the host is restored");
assert!(
path.hops()
.iter()
.all(|hop| hop.address() != Some(excluded)),
"{path:?}"
);
assert!(path.hops()[1].is_withheld(), "{path:?}");
}
fn unreachable_from(sender: IpAddr) -> StatusReason {
StatusReason::new(StatusProtocol::IcmpUnreachable, "destination unreachable")
.from_source(sender)
}
#[test]
fn an_excluded_middlebox_is_withheld_from_a_permitted_hosts_evidence() {
let target: IpAddr = "203.0.113.9".parse().expect("literal");
let excluded: IpAddr = "198.51.100.1".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
ctx.write_host(target, |host| {
host.record_evidence(HostStatus::Down, unreachable_from(excluded));
true
});
let (status, reasons) = ctx
.read_host(target, |host| (host.status(), host.reasons().clone()))
.expect("the host the evidence is about is recorded");
assert_eq!(status, HostStatus::Down, "the evidence still counts");
let sources: Vec<EvidenceSource> = reasons.iter().map(|reason| reason.source).collect();
assert_eq!(sources, [EvidenceSource::Withheld], "{reasons:?}");
}
#[test]
fn a_resume_withholds_a_middlebox_this_sitting_may_not_report() {
let target: IpAddr = "203.0.113.9".parse().expect("literal");
let excluded: IpAddr = "198.51.100.1".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
let mut journalled = Host::new(target);
journalled.record_evidence(HostStatus::Down, unreachable_from(excluded));
ctx.restore_hosts(&[journalled]);
let sources: Vec<EvidenceSource> = ctx
.read_host(target, |host| {
host.reasons().iter().map(|reason| reason.source).collect()
})
.expect("the host is restored");
assert_eq!(sources, [EvidenceSource::Withheld]);
}
#[test]
fn a_resume_brings_back_none_of_a_hosts_excluded_addresses() {
let key: IpAddr = "192.0.2.60".parse().expect("literal");
let excluded: IpAddr = "192.0.2.61".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
let mut host = Host::new(key);
host.add_ip(excluded);
ctx.restore_hosts(&[host]);
let ips = ctx
.read_host(key, |host| host.ips().clone())
.expect("the host is restored");
assert!(!ips.contains(&excluded), "{ips:?}");
}
#[test]
fn a_resumed_host_led_by_an_excluded_address_is_kept_at_its_others() {
let other: IpAddr = "fe80::10".parse().expect("literal");
let excluded: IpAddr = "2001:db8::5".parse().expect("literal");
let (_session, ctx) = forbidding(excluded);
let mut host = Host::new(other);
host.consider_primary_ip(excluded);
host.set_status(HostStatus::Up);
assert_eq!(host.primary_ip(), excluded, "test premise");
ctx.restore_hosts(&[host]);
let (primary, ips) = ctx
.read_host(other, |host| (host.primary_ip(), host.ips().clone()))
.expect("the host is kept at the address the policy allows");
assert_eq!(primary, other);
assert!(!ips.contains(&excluded), "{ips:?}");
}
#[test]
fn a_liveness_pass_leaves_a_port_scans_counters_alone() {
let (_session, ports) = ScanSession::builder()
.planning(Stage::Ports, Some(1_024))
.build();
ports.record_address_outcomes(Outcome::Unasked, 300);
assert_eq!(ports.settlements().count(Outcome::Unasked), 0);
let (_session, sweep) = ScanSession::builder()
.planning(Stage::Discovery, Some(1_024))
.build();
sweep.record_address_outcomes(Outcome::Unasked, 300);
assert_eq!(sweep.settlements().count(Outcome::Unasked), 300);
}
}