4 releases

new 0.2.3 Oct 24, 2024
0.2.2 Oct 24, 2024
0.2.0 Oct 17, 2024
0.1.0 Oct 17, 2024

#1069 in Network programming

Download history 368/week @ 2024-10-14 231/week @ 2024-10-21

599 downloads per month

Apache-2.0

36KB
710 lines

IPTable

This crate contains an efficient data structure for storing and querying values by CIDR.

Examples

use iptable::Ipv4Table;
use ipnetwork::Ipv4Network;

let mut table = Ipv4Table::new();
table.insert("192.168.2.0/24".parse::<Ipv4Network>().unwrap(), 42);
table.insert("192.168.1.0/24".parse::<Ipv4Network>().unwrap(), 43);

assert_eq!(table.get("192.168.2.0/24".parse::<Ipv4Network>().unwrap()), Some(&42));

// Iterate over the prefixes under a given prefix
let ipnet: Ipv4Network = "192.168.0.0/16".parse().unwrap();
let relevant = table.iter_prefix(ipnet).collect::<Vec<_>>();
assert_eq!(2, relevant.len());

// Merge entries with longer prefixes
let merged_table = table.merge_longer_prefixes(16, |a, b| a + b);
assert_eq!(
    merged_table.iter().collect::<Vec<_>>(),
    vec![(&"192.168.0.0/16".parse().unwrap(), &85)]
);

lib.rs:

IPTable

This crate contains an efficient data structure for storing and querying values by CIDR.

Examples

use iptable::Ipv4Table;
use ipnetwork::Ipv4Network;

let mut table = Ipv4Table::new();
table.insert("192.168.2.0/24".parse::<Ipv4Network>().unwrap(), 42);
table.insert("192.168.1.0/24".parse::<Ipv4Network>().unwrap(), 43);

assert_eq!(table.get("192.168.2.0/24".parse::<Ipv4Network>().unwrap()), Some(&42));

// Iterate over the prefixes under a given prefix
let ipnet: Ipv4Network = "192.168.0.0/16".parse().unwrap();
let relevant = table.iter_prefix(ipnet).collect::<Vec<_>>();
assert_eq!(2, relevant.len());

// Find unassigned /20s in the table
let unassigned = table.gaps(
    "192.168.0.0/18".parse::<Ipv4Network>().unwrap(), Some(20))
    .collect::<Vec<_>>();

assert_eq!(
   unassigned, vec![
       "192.168.16.0/20".parse().unwrap(),
       "192.168.32.0/19".parse().unwrap()]);

// Merge entries with longer prefixes
let merged_table = table.merge_longer_prefixes(16, |a, b| a + b);
assert_eq!(
    merged_table.iter().collect::<Vec<_>>(),
    vec![(&"192.168.0.0/16".parse().unwrap(), &85)]
);

Dependencies

~170–410KB