use dashmap::DashMap;
use std::net::IpAddr;
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use tracing::error;
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::Zone;
use crate::model::ip::set::Positions;
use crate::model::technique::TcpScanTechnique;
use crate::scanner::handle::ScanHandle;
use crate::scanner::report::{ProbeStats, ScannerFailure};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScannerKind {
Local,
Routed,
SynPort,
TcpPort,
Connect,
ConnectUdp,
UdpPort,
OsEcho,
OsSeries,
OsSnmp,
Composite,
}
impl ScannerKind {
pub const fn for_raw_tcp(technique: TcpScanTechnique) -> Self {
match technique {
TcpScanTechnique::Syn => Self::SynPort,
_ => Self::TcpPort,
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ScanEvent {
HostUpdated(IpAddr),
ScannerFailed {
scanner: ScannerKind,
reason: String,
},
}
#[derive(Debug, Clone, Default)]
pub struct HostStore {
inner: Arc<DashMap<IpAddr, Host>>,
}
impl HostStore {
fn new(inner: Arc<DashMap<IpAddr, Host>>) -> Self {
Self { inner }
}
pub fn get(&self, ip: &IpAddr) -> Option<Host> {
self.inner.get(ip).map(|entry| entry.value().clone())
}
pub fn read<R>(&self, ip: &IpAddr, read: impl FnOnce(&Host) -> R) -> Option<R> {
self.inner.get(ip).map(|entry| read(entry.value()))
}
pub fn contains(&self, ip: &IpAddr) -> bool {
self.inner.contains_key(ip)
}
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_key(Host::primary_ip);
hosts
}
#[cfg(test)]
pub(crate) fn insert(&self, ip: IpAddr, host: Host) {
self.inner.insert(ip, host);
}
}
#[derive(Debug)]
pub struct ScanEvents {
rx: mpsc::UnboundedReceiver<ScanEvent>,
}
impl ScanEvents {
pub async fn recv(&mut self) -> Option<ScanEvent> {
self.rx.recv().await
}
pub fn try_recv(&mut self) -> Option<ScanEvent> {
self.rx.try_recv().ok()
}
}
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(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 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, Clone)]
pub struct ScanProgress {
store: Arc<DashMap<IpAddr, Host>>,
changed: Arc<ChangedHosts>,
settlements: Arc<Settlements>,
failures: Arc<FailureLog>,
}
impl ScanProgress {
pub fn settlements(&self) -> &Settlements {
&self.settlements
}
pub fn take_changed_hosts(&self) -> Vec<Host> {
self.changed
.drain()
.into_iter()
.filter_map(|ip| self.store.get(&ip).map(|host| host.clone()))
.collect()
}
pub fn hosts_snapshot(&self) -> Vec<Host> {
self.store
.iter()
.map(|entry| entry.value().clone())
.collect()
}
pub fn host_count(&self) -> usize {
self.store.len()
}
pub fn record_failure(&self, scanner: ScannerKind, reason: String) {
error!("journal: {reason}");
self.failures.push(ScannerFailure::new(scanner, reason));
}
}
#[derive(Debug, Default)]
pub(crate) struct ChangedHosts {
entries: Mutex<std::collections::BTreeSet<IpAddr>>,
}
impl ChangedHosts {
fn insert(&self, ip: IpAddr) {
let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner());
entries.insert(ip);
}
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 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<IpAddr, Host>>,
pub(crate) events_tx: mpsc::UnboundedSender<ScanEvent>,
pub(crate) failures: Arc<FailureLog>,
pub(crate) probe_stats: Arc<ProbeStatsLog>,
pub(crate) unroutable: Arc<UnroutableLog>,
pub(crate) swept_links: Arc<SweptLinks>,
pub(crate) exclusions: Arc<Exclusions>,
pub(crate) changed: Arc<ChangedHosts>,
pub(crate) settlements: Arc<Settlements>,
pub(crate) positions: Arc<Positions>,
}
impl ScanContext {
pub fn write_host(&self, ip: IpAddr, edit: impl FnOnce(&mut Host) -> bool) -> bool {
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(ip).or_insert_with(|| {
is_new = true;
Host::new(ip)
});
let announce = edit(&mut host);
drop(host);
self.changed.insert(ip);
if announce || is_new {
let _ = self.events_tx.send(ScanEvent::HostUpdated(ip));
}
is_new
}
pub fn read_host<R>(&self, ip: &IpAddr, read: impl FnOnce(&Host) -> R) -> Option<R> {
self.store.get(ip).map(|entry| read(entry.value()))
}
pub fn host_addresses(&self) -> Vec<IpAddr> {
self.store.iter().map(|entry| *entry.key()).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_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 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_outcome(&self, outcome: Outcome) {
self.settlements.record(outcome);
}
pub fn record_many(&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 ip = host.primary_ip();
match self.store.get_mut(&ip) {
Some(mut existing) => existing.merge(host.clone()),
None => {
self.store.insert(ip, host.clone());
}
}
let _ = self.events_tx.send(ScanEvent::HostUpdated(ip));
}
}
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),
}
}
pub fn hosts_snapshot(&self) -> Vec<Host> {
self.store
.iter()
.map(|entry| entry.value().clone())
.collect()
}
pub fn host_count(&self) -> usize {
self.store.len()
}
pub fn take_changed_hosts(&self) -> Vec<Host> {
self.changed
.drain()
.into_iter()
.filter_map(|ip| self.store.get(&ip).map(|host| host.clone()))
.collect()
}
pub fn failures_snapshot(&self) -> Vec<ScannerFailure> {
self.failures.snapshot()
}
pub fn update_host(&self, ip: IpAddr, update: impl FnOnce(&mut Host)) -> bool {
self.write_host(ip, |host| {
update(host);
true
})
}
}
impl ScanSession {
pub fn new() -> (Self, ScanContext) {
Self::with_exclusions(Exclusions::none())
}
pub fn with_exclusions(exclusions: Exclusions) -> (Self, ScanContext) {
Self::resuming(exclusions, &crate::journal::cursor::Checkpoint::default())
}
pub fn resuming(
exclusions: Exclusions,
settled: &crate::journal::cursor::Checkpoint,
) -> (Self, ScanContext) {
Self::sweeping(exclusions, settled, Positions::default())
}
pub fn sweeping(
exclusions: Exclusions,
settled: &crate::journal::cursor::Checkpoint,
positions: Positions,
) -> (Self, ScanContext) {
let store = Arc::new(DashMap::new());
let handle = ScanHandle::new();
let (events_tx, rx) = mpsc::unbounded_channel();
let session = Self {
store: HostStore::new(store.clone()),
events: ScanEvents { rx },
handle: handle.clone(),
};
let ctx = ScanContext {
handle,
store,
events_tx,
failures: Arc::new(FailureLog::default()),
probe_stats: Arc::new(ProbeStatsLog::default()),
unroutable: Arc::new(UnroutableLog::default()),
swept_links: Arc::new(SweptLinks::default()),
exclusions: Arc::new(exclusions),
changed: Arc::new(ChangedHosts::default()),
settlements: Arc::new(Settlements::resuming(settled)),
positions: Arc::new(positions),
};
(session, ctx)
}
}
#[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::with_exclusions(Exclusions::new(ips));
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(&excluded));
let ScanEvent::HostUpdated(announced) =
session.events().try_recv().expect("the allowed host")
else {
panic!("expected a host update");
};
assert_eq!(announced, allowed);
assert!(session.events().try_recv().is_none());
}
#[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::resuming(Exclusions::none(), &settled);
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"
);
}
}