use std::net::IpAddr;
use crate::journal::cursor::Checkpoint;
use crate::journal::settle::Outcome;
use crate::model::ip::set::IpSet;
use crate::model::target::{PlannedTarget, TargetMap};
use crate::scanner::handle::ScanHandle;
use crate::scanner::session::ScanContext;
use rand::seq::SliceRandom;
use tokio::sync::mpsc;
pub fn shuffled_addresses(
ips: IpSet,
batch_size: usize,
scan_handle: &ScanHandle,
) -> mpsc::Receiver<IpAddr> {
let (tx, rx) = mpsc::channel(batch_size * 2);
let scan_handle = scan_handle.clone();
tokio::spawn(async move {
let mut batch = Vec::with_capacity(batch_size);
for ip in ips.iter() {
batch.push(ip);
if batch.len() < batch_size {
continue;
}
if !drain(&mut batch, &tx, &scan_handle).await {
return;
}
}
drain(&mut batch, &tx, &scan_handle).await;
});
rx
}
async fn drain(
batch: &mut Vec<IpAddr>,
tx: &mpsc::Sender<IpAddr>,
scan_handle: &ScanHandle,
) -> bool {
batch.shuffle(&mut rand::rng());
for ip in batch.drain(..) {
if tx.send(ip).await.is_err() || scan_handle.should_stop() {
return false;
}
}
true
}
pub struct Dispatcher {
target_map: TargetMap,
batch_size: usize,
settled: Checkpoint,
live: Option<IpSet>,
}
impl Dispatcher {
pub fn new(target_map: TargetMap) -> Self {
Self {
target_map,
batch_size: 8192,
settled: Checkpoint::default(),
live: None,
}
}
pub fn resuming(mut self, settled: Checkpoint) -> Self {
self.settled = settled;
self
}
pub fn only_live(mut self, live: IpSet) -> Self {
self.live = Some(live);
self
}
pub fn with_batch_size(mut self, batch_size: usize) -> Self {
self.batch_size = batch_size;
self
}
pub fn run_shuffled(self, ctx: &ScanContext) -> mpsc::Receiver<PlannedTarget> {
let (tx, rx) = mpsc::channel(self.batch_size * 2);
let scan_handle = ctx.handle.clone();
let ctx = ctx.clone();
tokio::spawn(async move {
let mut batch = Vec::with_capacity(self.batch_size);
for planned in self.settled.remaining(self.target_map.iter()) {
if let Some(live) = &self.live
&& !live.contains(&planned.target.ip)
{
ctx.record_outcome(Outcome::Skipped {
position: planned.position,
});
continue;
}
batch.push(planned);
if batch.len() >= self.batch_size {
batch.shuffle(&mut rand::rng());
for t in batch.drain(..) {
if tx.send(t).await.is_err() || scan_handle.should_stop() {
return;
}
}
}
}
if !batch.is_empty() {
batch.shuffle(&mut rand::rng());
for t in batch {
if tx.send(t).await.is_err() || scan_handle.should_stop() {
return;
}
}
}
});
rx
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::ip::set::IpSet;
use crate::model::port::PortSet;
use crate::model::target::Target;
use crate::model::target::TargetSet;
use crate::scanner::session::ScanSession;
use std::net::IpAddr;
fn context() -> (ScanSession, ScanContext) {
ScanSession::new()
}
#[tokio::test]
async fn dispatcher_emits_all_targets_shuffled() {
let mut target_map = TargetMap::new();
let ip_set: IpSet = "192.168.1.1-192.168.1.10".parse().unwrap();
let port_set: PortSet = "80".parse().unwrap();
let unit = TargetSet::new(ip_set, port_set);
target_map.units.push(unit);
let (_session, ctx) = context();
let dispatcher = Dispatcher::new(target_map).with_batch_size(4);
let mut rx = dispatcher.run_shuffled(&ctx);
let mut received = Vec::new();
while let Some(target) = rx.recv().await {
received.push(target);
}
assert_eq!(received.len(), 10);
let is_ordered = received.windows(2).all(|w| match (w[0].ip(), w[1].ip()) {
(IpAddr::V4(a), IpAddr::V4(b)) => a.octets()[3] < b.octets()[3],
_ => false,
});
assert!(!is_ordered, "Targets were not shuffled");
}
#[tokio::test]
async fn the_dispatcher_emits_exactly_the_plans_enumeration() {
let mut target_map = TargetMap::new();
target_map.units.push(TargetSet::new(
"192.168.1.1-192.168.1.10".parse().unwrap(),
"80,443".parse().unwrap(),
));
target_map.units.push(TargetSet::new(
"10.0.0.1-10.0.0.3".parse().unwrap(),
"22".parse().unwrap(),
));
let expected: Vec<Target> = target_map.iter().collect();
let (_session, ctx) = context();
let mut rx = Dispatcher::new(target_map.clone())
.with_batch_size(4)
.run_shuffled(&ctx);
let mut received = Vec::new();
while let Some(target) = rx.recv().await {
received.push(target);
}
let key = |t: &Target| (t.ip.to_string(), t.port, t.protocol);
let mut received_sorted: Vec<Target> =
received.iter().map(|planned| planned.target).collect();
let mut expected_sorted = expected.clone();
received_sorted.sort_by_key(key);
expected_sorted.sort_by_key(key);
assert_eq!(
received_sorted, expected_sorted,
"the dispatcher and the journal must walk one enumeration"
);
}
#[tokio::test]
async fn dispatcher_stops_early_on_abort() {
let mut target_map = TargetMap::new();
let ip_set: IpSet = "192.168.1.1-192.168.1.100".parse().unwrap();
let port_set: PortSet = "80".parse().unwrap();
let unit = TargetSet::new(ip_set, port_set);
target_map.units.push(unit);
let (_session, ctx) = context();
let dispatcher = Dispatcher::new(target_map).with_batch_size(10);
let mut rx = dispatcher.run_shuffled(&ctx);
let mut count = 0;
while let Some(_target) = rx.recv().await {
count += 1;
if count == 15 {
ctx.handle.abort();
}
}
assert!((15..100).contains(&count));
}
#[tokio::test]
async fn liveness_never_moves_a_position() {
let plan = || {
let mut map = TargetMap::new();
map.add_unit(TargetSet::new(
"192.0.2.1-192.0.2.4".parse::<IpSet>().expect("a range"),
"80".parse::<PortSet>().expect("ports"),
));
map
};
let one = numbered(plan(), Some("192.0.2.4".parse().expect("a range"))).await;
let two = numbered(
plan(),
Some("192.0.2.2-192.0.2.4".parse().expect("a range")),
)
.await;
let all = numbered(plan(), None).await;
for emitted in [&one, &two] {
for (ip, position) in emitted {
assert_eq!(
all.get(ip),
Some(position),
"{ip} moved when the liveness answer changed"
);
}
}
assert_eq!(one.len(), 1, "one host answered");
assert_eq!(two.len(), 3);
}
#[tokio::test]
async fn a_target_whose_host_is_down_settles_rather_than_vanishing() {
let mut map = TargetMap::new();
map.add_unit(TargetSet::new(
"192.0.2.1-192.0.2.4".parse::<IpSet>().expect("a range"),
"80".parse::<PortSet>().expect("ports"),
));
let (_session, ctx) = context();
let mut rx = Dispatcher::new(map)
.only_live("192.0.2.4".parse::<IpSet>().expect("a range"))
.run_shuffled(&ctx);
let mut emitted = 0;
while rx.recv().await.is_some() {
emitted += 1;
}
assert_eq!(emitted, 1, "only the live host is probed");
assert_eq!(
ctx.settlements().count(Outcome::Skipped { position: 0 }),
3,
"the three that were not probed have to be accounted for"
);
assert_eq!(
ctx.settlements().checkpoint().watermark,
3,
"and their positions are the ones below the live host"
);
}
async fn numbered(
map: TargetMap,
live: Option<IpSet>,
) -> std::collections::HashMap<IpAddr, u64> {
let (_session, ctx) = context();
let mut dispatcher = Dispatcher::new(map);
if let Some(live) = live {
dispatcher = dispatcher.only_live(live);
}
let mut rx = dispatcher.run_shuffled(&ctx);
let mut found = std::collections::HashMap::new();
while let Some(planned) = rx.recv().await {
found.insert(planned.target.ip, planned.position);
}
found
}
}