aggregate_ips

Function aggregate_ips 

Source
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:

  1. Converting IPs to their u32 integer representation.
  2. Sorting the integers to process them in order.
  3. Iteratively finding the largest contiguous, properly aligned block starting at each IP.
  4. Calculating the CIDR prefix for each block and creating a Cidr struct.

§Arguments

  • ip_list - A slice of Ipv4Addr to 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.