pub fn aggregate_ips(ip_list: &[Ipv4Addr]) -> Vec<Cidr>Expand description
Aggregate a list of IPv4 addresses into the smallest possible set of CIDR blocks.
This function takes a slice of IPv4 addresses and returns a Vec<Cidr>
representing the minimal set of CIDR blocks that cover all the input IPs.
It works by:
- Converting IPs to their
u32integer representation. - Sorting the integers to process them in order.
- Iteratively finding the largest contiguous, properly aligned block starting at each IP.
- Calculating the CIDR prefix for each block and creating a
Cidrstruct.
§Arguments
ip_list- A slice ofIpv4Addrto aggregate.
§Returns
A Vec<Cidr> containing the aggregated CIDR blocks.
§Example
use std::net::Ipv4Addr;
use ys1r::ipaggregator::aggregate_ips;
use ys1r::ipaggregator::cidr_to_acl;
let mut ip_list: Vec<Ipv4Addr> = Vec::new();
for i in 1..=254 {
ip_list.push(Ipv4Addr::new(10, 0, 0, i));
}
let cidrs = aggregate_ips(&ip_list);
for c in &cidrs {
println!("{}\t<=\t{:?}", cidr_to_acl(c), &c);
}This will produce the minimal CIDR blocks covering 10.0.0.1–10.0.0.254.