use std::collections::{HashMap, HashSet};
use std::net::IpAddr;
use std::sync::Arc;
use crate::model::host::Host;
use crate::model::mac::MacAddr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum HostIdentity {
PrimaryAddress,
#[default]
AnyAddress,
Hardware,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Component {
pub(crate) baseline: Vec<usize>,
pub(crate) current: Vec<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Token {
Address(IpAddr, Option<Arc<str>>),
Hardware(MacAddr),
}
pub(crate) fn components(
baseline: &[&Host],
current: &[&Host],
identity: HostIdentity,
) -> Vec<Component> {
let baseline_tokens: Vec<HashSet<Token>> =
baseline.iter().map(|host| tokens(host, identity)).collect();
let current_tokens: Vec<HashSet<Token>> =
current.iter().map(|host| tokens(host, identity)).collect();
let baseline_index = index(&baseline_tokens);
let current_index = index(¤t_tokens);
let mut baseline_seen = vec![false; baseline.len()];
let mut current_seen = vec![false; current.len()];
let mut components = Vec::new();
for start in 0..baseline.len() {
if baseline_seen[start] {
continue;
}
baseline_seen[start] = true;
let mut component = Component {
baseline: Vec::new(),
current: Vec::new(),
};
let mut baseline_queue = vec![start];
let mut current_queue: Vec<usize> = Vec::new();
while !baseline_queue.is_empty() || !current_queue.is_empty() {
while let Some(i) = baseline_queue.pop() {
component.baseline.push(i);
for token in &baseline_tokens[i] {
for &j in current_index.get(token).into_iter().flatten() {
if !current_seen[j] {
current_seen[j] = true;
current_queue.push(j);
}
}
}
}
while let Some(j) = current_queue.pop() {
component.current.push(j);
for token in ¤t_tokens[j] {
for &i in baseline_index.get(token).into_iter().flatten() {
if !baseline_seen[i] {
baseline_seen[i] = true;
baseline_queue.push(i);
}
}
}
}
}
component.baseline.sort_unstable();
component.current.sort_unstable();
components.push(component);
}
for (j, seen) in current_seen.iter().enumerate() {
if !seen {
components.push(Component {
baseline: Vec::new(),
current: vec![j],
});
}
}
components.sort_by_key(|component| {
(
component.baseline.first().copied().unwrap_or(usize::MAX),
component.current.first().copied().unwrap_or(usize::MAX),
)
});
components
}
fn tokens(host: &Host, identity: HostIdentity) -> HashSet<Token> {
let zone = host.zone().map(|zone| Arc::from(zone.name()));
let mut tokens: HashSet<Token> = match identity {
HostIdentity::PrimaryAddress => {
let ip = host.primary_ip();
HashSet::from([Token::Address(ip, scope_of(&ip, &zone))])
}
HostIdentity::AnyAddress | HostIdentity::Hardware => host
.ips()
.iter()
.map(|ip| Token::Address(*ip, scope_of(ip, &zone)))
.collect(),
};
if identity == HostIdentity::Hardware
&& let Some(hardware) = host.hardware()
{
tokens.extend(hardware.macs().keys().copied().map(Token::Hardware));
}
tokens
}
fn scope_of(ip: &IpAddr, zone: &Option<Arc<str>>) -> Option<Arc<str>> {
is_link_local(ip).then(|| zone.clone()).flatten()
}
fn is_link_local(ip: &IpAddr) -> bool {
match ip {
IpAddr::V4(v4) => v4.is_link_local(),
IpAddr::V6(v6) => (v6.segments()[0] & 0xffc0) == 0xfe80,
}
}
fn index(tokens: &[HashSet<Token>]) -> HashMap<&Token, Vec<usize>> {
let mut index: HashMap<&Token, Vec<usize>> = HashMap::new();
for (i, set) in tokens.iter().enumerate() {
for token in set {
index.entry(token).or_default().push(i);
}
}
index
}