mod discovery;
use std::collections::{HashMap, HashSet, VecDeque};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::{Duration, Instant};
use async_trait::async_trait;
use pnet::datalink::{MacAddr, NetworkInterface};
use pnet::packet::ethernet::EthernetPacket;
use tokio::sync::mpsc::UnboundedSender;
use tokio::time::Interval;
use crate::core::models::deadline::{AdaptiveDeadline, AdaptiveDeadlineConfig};
use crate::core::models::host::{HostStatus, StatusProtocol, StatusReason};
use crate::core::models::ip::set::IpSet;
use crate::core::models::retry::{Due, ProbeLedger, RetryConfig, RetryPolicy};
use crate::core::models::timer::ScanBudget;
use crate::core::session::ScanContext;
use crate::network::channel::{self, EthernetHandle};
use crate::network::mac::IntoCoreMac;
use crate::protocols::{self as protocol, ethernet};
use crate::scanner::NetworkExplorer;
use crate::system::interface::NetworkInterfaceExtension;
use crate::{error, info};
use discovery::{ArpProtocol, DiscoveryProtocol, Icmpv6Protocol, ProtocolMatch};
type Ledger = ProbeLedger<IpAddr, ()>;
const RETRY_POLICY: RetryPolicy = RetryPolicy::new(
3,
Duration::from_millis(150),
Duration::from_millis(25),
Duration::from_secs(1),
2.0,
0.2,
None,
);
#[derive(Debug, thiserror::Error)]
pub enum LocalScannerError {
#[error("interface has no mac address")]
NoMacAddress,
#[error("unmapped RTT source: {0}")]
UnmappedRttSource(IpAddr),
#[error("packet originated from this host")]
SelfSourcedPacket,
#[error("{0} is not in the scanned range")]
AddressOutOfRange(IpAddr),
}
const DEADLINE_CONFIG: AdaptiveDeadlineConfig = AdaptiveDeadlineConfig::new(
ScanBudget::new(
Duration::from_millis(2_000),
Duration::from_millis(20),
Duration::from_secs(120),
),
ScanBudget::new(
Duration::from_millis(800),
Duration::from_millis(7),
Duration::from_millis(5_000),
),
Duration::from_millis(250),
Duration::from_millis(2_000),
4.0,
20,
);
const SEND_INTERVAL: Duration = Duration::from_micros(1000);
const SOLICITATION_ATTEMPTS: u8 = 3;
const SOLICITATION_INTERVAL: Duration = Duration::from_millis(600);
const SOLICITATION_WINDOW: Duration = Duration::from_millis(1_500);
#[derive(Debug, Default)]
struct Solicitation {
sent: u8,
next_due: Option<Instant>,
last_sent_at: Option<Instant>,
}
impl Solicitation {
fn record_sent(&mut self, now: Instant) {
self.sent = self.sent.saturating_add(1);
self.last_sent_at = Some(now);
self.next_due = (self.sent < SOLICITATION_ATTEMPTS).then(|| now + SOLICITATION_INTERVAL);
}
fn is_due(&self, now: Instant) -> bool {
self.next_due.is_some_and(|due| due <= now)
}
fn window_open(&self, now: Instant) -> bool {
if self.next_due.is_some() {
return true;
}
self.last_sent_at
.is_some_and(|sent_at| now < sent_at + SOLICITATION_WINDOW)
}
fn next_wakeup(&self) -> Option<Instant> {
self.next_due.or_else(|| {
self.last_sent_at
.map(|sent_at| sent_at + SOLICITATION_WINDOW)
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Scope {
Sweep,
Targeted,
}
struct SourceIdentity {
mac: MacAddr,
ipv4: Option<Ipv4Addr>,
link_local_ipv6: Option<Ipv6Addr>,
}
impl SourceIdentity {
fn resolve(intf: &NetworkInterface, ip_set: &IpSet) -> Result<Self, LocalScannerError> {
let mac = intf.mac.ok_or(LocalScannerError::NoMacAddress)?;
let mut ipv4 = None;
for net in intf.get_ipv4_nets() {
if ipv4.is_none() && !net.ip().is_loopback() {
ipv4 = Some(net.ip());
}
if ip_set
.v4()
.iter()
.any(|range| net.contains(range.start_addr))
{
ipv4 = Some(net.ip());
break;
}
}
let link_local_ipv6 = intf
.get_ipv6_nets()
.into_iter()
.find(|net| net.ip().is_unicast_link_local())
.map(|net| net.ip());
Ok(Self {
mac,
ipv4,
link_local_ipv6,
})
}
}
pub struct LocalScanner {
ctx: ScanContext,
ip_set: IpSet,
identity: SourceIdentity,
eth_handle: EthernetHandle,
deadline: AdaptiveDeadline,
protocols: Vec<Box<dyn DiscoveryProtocol>>,
ledger: Ledger,
due: Vec<Due<IpAddr>>,
retries: VecDeque<IpAddr>,
solicitation: Solicitation,
dns_tx: Option<UnboundedSender<IpAddr>>,
mac_to_ip: HashMap<MacAddr, IpAddr>,
scope: Scope,
responded: HashSet<IpAddr>,
}
#[async_trait]
impl NetworkExplorer for LocalScanner {
async fn discover_hosts(mut self: Box<Self>) -> anyhow::Result<()> {
let mut packet_iter = protocol::eth_packet_iter(
&self.identity.mac,
&self.identity.ipv4,
&self.identity.link_local_ipv6,
&self.ip_set,
matches!(self.scope, Scope::Sweep),
)?;
let mut sending_finished = false;
let mut send_interval: Interval = tokio::time::interval(SEND_INTERVAL);
send_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
let now = Instant::now();
self.service_retries(now);
if self.ctx.handle.should_stop() || self.deadline.hard_deadline_passed() {
break;
}
if sending_finished && self.all_targets_responded() {
break;
}
if sending_finished && self.idle(now) && self.deadline.has_expired() {
break;
}
let sending =
!sending_finished || !self.retries.is_empty() || self.solicitation.is_due(now);
let idle_delay = self.tick_delay(now);
tokio::select! {
pkt = self.eth_handle.rx.recv() => {
match pkt {
Some(bytes) => _ = self.process_eth_packet(&bytes, Instant::now()),
None => break,
}
}
_ = send_interval.tick(), if sending => {
let now = Instant::now();
if let Some(target) = self.retries.pop_front() {
self.send_arp_request(target, now);
} else if self.solicitation.is_due(now) {
self.send_solicitation(now);
} else if !sending_finished {
match packet_iter.next() {
Some((packet, ip)) => {
self.record_probe(ip, Instant::now());
self.eth_handle.tx.send_to(&packet, None);
},
None => {
sending_finished = true;
},
}
}
}
_ = tokio::time::sleep(idle_delay), if !sending => {}
}
}
Ok(())
}
}
impl LocalScanner {
pub fn new(
intf: NetworkInterface,
ip_set: IpSet,
ctx: ScanContext,
dns_tx: Option<UnboundedSender<IpAddr>>,
scope: Scope,
retry: RetryConfig,
) -> anyhow::Result<Self> {
let eth_handle: EthernetHandle = channel::start_capture(&intf)?;
Self::build(
intf,
ip_set,
ctx,
dns_tx,
scope,
eth_handle,
RETRY_POLICY.configured(retry),
)
}
#[cfg(any(test, feature = "test-support"))]
pub fn with_handle(
intf: NetworkInterface,
ip_set: IpSet,
ctx: ScanContext,
dns_tx: Option<UnboundedSender<IpAddr>>,
scope: Scope,
eth_handle: EthernetHandle,
) -> anyhow::Result<Self> {
Self::build(intf, ip_set, ctx, dns_tx, scope, eth_handle, RETRY_POLICY)
}
#[allow(clippy::too_many_arguments)]
fn build(
intf: NetworkInterface,
ip_set: IpSet,
ctx: ScanContext,
dns_tx: Option<UnboundedSender<IpAddr>>,
scope: Scope,
eth_handle: EthernetHandle,
retry: RetryPolicy,
) -> anyhow::Result<Self> {
let identity = SourceIdentity::resolve(&intf, &ip_set)?;
let target_count = ip_set.len() as usize;
let deadline_config = DEADLINE_CONFIG.allowing_for(retry.worst_case_probe_lifetime());
let deadline = AdaptiveDeadline::new(deadline_config, target_count);
Ok(Self {
ctx,
ip_set,
identity,
eth_handle,
deadline,
protocols: vec![Box::new(ArpProtocol), Box::new(Icmpv6Protocol)],
ledger: Ledger::new(retry, target_count),
due: Vec::new(),
retries: VecDeque::new(),
solicitation: Solicitation::default(),
dns_tx,
mac_to_ip: HashMap::new(),
scope,
responded: HashSet::new(),
})
}
fn record_probe(&mut self, ip: IpAddr, now: Instant) {
if Some(ip) == self.identity.link_local_ipv6.map(IpAddr::V6) {
self.solicitation.record_sent(now);
return;
}
self.ledger.arm(ip, ip, (), now);
}
fn send_solicitation(&mut self, now: Instant) {
let Some(link_local) = self.identity.link_local_ipv6 else {
return;
};
match protocol::icmp::create_all_nodes_echo_request_v6(&self.identity.mac, &link_local) {
Ok(packet) => {
self.eth_handle.tx.send_to(&packet, None);
self.solicitation.record_sent(now);
}
Err(e) => {
error!(
verbosity = 1,
"Failed to rebuild all-nodes solicitation: {e}"
);
self.solicitation.record_sent(now);
}
}
}
fn send_arp_request(&mut self, target: IpAddr, now: Instant) {
let (IpAddr::V4(target_v4), Some(source_v4)) = (target, self.identity.ipv4) else {
return;
};
match protocol::arp::create_packet(
&self.identity.mac,
MacAddr::broadcast(),
&source_v4,
target_v4,
) {
Ok(packet) => {
self.eth_handle.tx.send_to(&packet, None);
self.ledger.arm(target, target, (), now);
}
Err(e) => error!(
verbosity = 1,
"Failed to rebuild ARP request for {target}: {e}"
),
}
}
fn service_retries(&mut self, now: Instant) {
self.ledger.drain_due(now, &mut self.due);
for event in self.due.drain(..) {
if let Due::Retry { key, .. } = event {
self.retries.push_back(key);
}
}
}
fn idle(&self, now: Instant) -> bool {
self.retries.is_empty() && self.ledger.is_empty() && !self.solicitation.window_open(now)
}
fn tick_delay(&self, now: Instant) -> Duration {
let mut delay = self.deadline.time_until_next_tick();
for wakeup in [self.ledger.next_due(), self.solicitation.next_wakeup()]
.into_iter()
.flatten()
{
delay = delay.min(wakeup.saturating_duration_since(now));
}
delay
}
fn process_eth_packet(&mut self, bytes: &[u8], now: Instant) -> anyhow::Result<()> {
let eth_frame: EthernetPacket = ethernet::get_packet_from_u8(bytes)?;
let source_mac = eth_frame.get_source();
if source_mac == self.identity.mac {
return Err(LocalScannerError::SelfSourcedPacket.into());
}
let source_addr: IpAddr = protocol::get_ip_addr_from_eth(ð_frame)?;
let out_of_range = match self.scope {
Scope::Targeted => !self.ip_set.contains(&source_addr),
Scope::Sweep => source_addr.is_ipv4() && !self.ip_set.contains(&source_addr),
};
if out_of_range {
return Err(LocalScannerError::AddressOutOfRange(source_addr).into());
}
let Some((matched, protocol)) = self.interpret_response(ð_frame) else {
return Ok(());
};
let rtt = match matched {
ProtocolMatch::Unhandled => return Ok(()),
ProtocolMatch::Solicited => self
.ledger
.resolve(&source_addr, None, now)
.and_then(|resolution| resolution.rtt),
ProtocolMatch::AllNodes => {
if self.solicitation.sent == 0 {
return Err(LocalScannerError::UnmappedRttSource(source_addr).into());
}
None
}
};
if self.ip_set.contains(&source_addr) {
self.responded.insert(source_addr);
}
self.record_response(source_mac, source_addr, rtt, protocol);
Ok(())
}
fn all_targets_responded(&self) -> bool {
self.responded.len() as u128 >= self.ip_set.len()
}
fn interpret_response(
&mut self,
frame: &EthernetPacket,
) -> Option<(ProtocolMatch, StatusProtocol)> {
for protocol in &self.protocols {
match protocol.interpret(frame) {
Ok(ProtocolMatch::Unhandled) => continue,
Ok(matched) => return Some((matched, protocol.status_protocol())),
Err(e) => {
error!(verbosity = 1, "Failed to interpret discovery response: {e}");
return None;
}
}
}
None
}
fn record_response(
&mut self,
source_mac: MacAddr,
source_addr: IpAddr,
rtt: Option<Duration>,
protocol: StatusProtocol,
) {
let primary_ip = *self.mac_to_ip.entry(source_mac).or_insert(source_addr);
let mut is_new_ip = false;
let is_new_host = self.ctx.write_host(primary_ip, |host| {
if host.mac().is_none() {
host.set_mac(source_mac.into_core());
}
let was_up = host.status().is_up();
host.record_evidence(HostStatus::Up, StatusReason::basic(protocol.clone()));
let mut changed = rtt.is_some() || !was_up;
if let Some(rtt) = rtt {
host.add_rtt(rtt);
}
is_new_ip = host.add_ip(source_addr);
changed |= is_new_ip;
if source_addr.is_ipv4() && host.primary_ip().is_ipv6() {
host.set_primary_ip(source_addr);
changed = true;
}
changed
});
if is_new_host {
self.deadline.mark_activity();
}
if let Some(rtt) = rtt {
info!(
incoming,
verbosity = 2,
"{source_addr} responded in {}ms",
rtt.as_millis()
);
self.deadline.record_rtt(rtt);
}
if (is_new_host || is_new_ip)
&& let Some(tx) = &self.dns_tx
{
let _ = tx.send(source_addr);
}
}
}