use std::net::IpAddr;
use crate::model::ip::range::IpRange;
use crate::model::ip::set::IpSet;
use crate::model::target::{TargetMap, TargetSet};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Exclusions {
set: IpSet,
}
impl Exclusions {
pub fn none() -> Self {
Self::default()
}
pub fn new(mut ips: IpSet) -> Self {
ips.canonicalize();
Self { set: ips }
}
pub fn is_empty(&self) -> bool {
self.set.is_empty()
}
pub fn excludes(&self, ip: &IpAddr) -> bool {
!self.set.is_empty() && self.set.contains(ip)
}
pub fn ranges(&self) -> Vec<IpRange> {
let v4 = self.set.v4().iter().copied().map(IpRange::V4);
let v6 = self.set.v6().iter().copied().map(IpRange::V6);
v4.chain(v6).collect()
}
pub fn extend(&mut self, other: &Exclusions) {
if other.is_empty() {
return;
}
for range in other.set.v4() {
self.set.push_v4_range(*range);
}
for range in other.set.v6() {
self.set.push_v6_range(*range);
}
self.set.canonicalize();
}
pub fn withhold(&self, ips: &mut IpSet) -> u128 {
if self.is_empty() {
return 0;
}
let before = ips.len();
ips.subtract(&self.set);
before.saturating_sub(ips.len())
}
pub fn withhold_targets(&self, map: &mut TargetMap) -> u128 {
if self.is_empty() {
return 0;
}
let mut withheld: u128 = 0;
let mut kept = Vec::with_capacity(map.units.len());
for unit in std::mem::take(&mut map.units) {
let (mut ips, ports) = unit.into_parts();
withheld = withheld.saturating_add(self.withhold(&mut ips));
if !ips.is_empty() {
kept.push(TargetSet::new(ips, ports));
}
}
map.units = kept;
withheld
}
}
impl From<IpSet> for Exclusions {
fn from(ips: IpSet) -> Self {
Self::new(ips)
}
}
#[cfg(test)]
mod tests {
use std::net::{Ipv4Addr, Ipv6Addr};
use super::*;
use crate::model::ip::range::Ipv6Range;
use crate::model::port::PortSet;
fn ips(spec: &str) -> IpSet {
spec.parse().expect("a valid address expression")
}
fn v4(a: u8, b: u8, c: u8, d: u8) -> IpAddr {
IpAddr::V4(Ipv4Addr::new(a, b, c, d))
}
#[test]
fn an_excluded_range_leaves_the_scope_and_stays_out_of_the_gate() {
let policy = Exclusions::new(ips("10.0.5.0/24"));
let mut scope = ips("10.0.0.0/16");
assert_eq!(policy.withhold(&mut scope), 256);
assert_eq!(scope.len(), 65536 - 256);
assert!(!scope.contains(&v4(10, 0, 5, 7)));
assert!(scope.contains(&v4(10, 0, 6, 7)));
assert!(policy.excludes(&v4(10, 0, 5, 7)));
assert!(!policy.excludes(&v4(10, 0, 6, 7)));
}
#[test]
fn a_policy_that_does_not_overlap_withholds_nothing() {
let policy = Exclusions::new(ips("192.168.9.0/24"));
let mut scope = ips("10.0.0.0/24");
assert_eq!(policy.withhold(&mut scope), 0);
assert_eq!(scope.len(), 256);
}
#[test]
fn layers_accumulate_and_emptied_units_are_dropped() {
let mut policy = Exclusions::new(ips("10.0.5.0/24"));
policy.extend(&Exclusions::new(ips("10.0.7.0/24")));
assert!(policy.excludes(&v4(10, 0, 5, 1)));
assert!(policy.excludes(&v4(10, 0, 7, 1)));
let mut map = TargetMap::new();
map.add_unit(TargetSet::new(ips("10.0.5.0/24"), PortSet::top_tcp(2)));
map.add_unit(TargetSet::new(ips("10.0.6.0/24"), PortSet::top_tcp(2)));
let withheld = policy.withhold_targets(&mut map);
assert_eq!(withheld, 256);
assert_eq!(map.units.len(), 1, "the fully excluded unit is dropped");
assert_eq!(map.gross_ips().expect("small"), 256);
}
#[test]
fn a_zoned_exclusion_withholds_the_address_everywhere() {
let address: Ipv6Addr = "fe80::5".parse().expect("literal");
let mut named_on_one_interface = IpSet::new();
named_on_one_interface
.push_v6_range(Ipv6Range::scoped(address, address, Some(1)).expect("start <= end"));
let policy = Exclusions::new(named_on_one_interface);
assert!(policy.excludes(&IpAddr::V6(address)));
let mut scope = IpSet::new();
scope.push_v6_range(Ipv6Range::scoped(address, address, Some(2)).expect("start <= end"));
scope.canonicalize();
assert_eq!(policy.withhold(&mut scope), 1);
assert!(scope.is_empty());
}
}