#cidr #ipv6 #ipv4 #ip #networking

cidr-utils

This crate provides functions for working with IPv4 CIDRs and IPv6 CIDRs

31 releases

0.6.1 Nov 29, 2023
0.5.11 Sep 8, 2023
0.5.10 Dec 31, 2022
0.5.9 Nov 2, 2022
0.3.5 Jul 28, 2019

#92 in Network programming

Download history 16200/week @ 2024-08-02 14128/week @ 2024-08-09 13763/week @ 2024-08-16 19902/week @ 2024-08-23 18866/week @ 2024-08-30 17149/week @ 2024-09-06 13273/week @ 2024-09-13 15032/week @ 2024-09-20 16950/week @ 2024-09-27 17608/week @ 2024-10-04 17547/week @ 2024-10-11 17106/week @ 2024-10-18 16853/week @ 2024-10-25 18869/week @ 2024-11-01 18327/week @ 2024-11-08 17967/week @ 2024-11-15

74,873 downloads per month
Used in 27 crates (22 directly)

MIT license

46KB
1K SLoC

CIDR Utils

CI

This crate provides functions for working with IPv4 CIDRs and IPv6 CIDRs.

Examples

Combine subnetworks to supernetworks.

use std::str::FromStr;

use cidr::Ipv4Cidr;
use cidr_utils::Ipv4CidrSize;
use cidr_utils::combiner::Ipv4CidrCombiner;

let mut combiner = Ipv4CidrCombiner::new();

combiner.push(Ipv4Cidr::from_str("192.168.51.100").unwrap());

assert_eq!(1, combiner.len());
assert_eq!("192.168.51.100".to_string(), combiner[0].to_string());

combiner.push(Ipv4Cidr::from_str("192.168.51.101").unwrap());

assert_eq!(1, combiner.len());
assert_eq!("192.168.51.100/31".to_string(), combiner[0].to_string());

combiner.push(Ipv4Cidr::from_str("192.168.51.102").unwrap());

assert_eq!(2, combiner.len());
assert_eq!("192.168.51.100/31".to_string(), combiner[0].to_string());
assert_eq!("192.168.51.102".to_string(), combiner[1].to_string());

combiner.push(Ipv4Cidr::from_str("192.168.51.103").unwrap());

assert_eq!(1, combiner.len());
assert_eq!("192.168.51.100/30".to_string(), combiner[0].to_string());

assert_eq!(true, combiner.contains(&[192, 168, 51, 102].into()));
assert_eq!(false, combiner.contains(&[192, 168, 51, 105].into()));

assert_eq!(4, combiner.size());

Separate a network into subnetworks.

use std::str::FromStr;

use cidr::Ipv4Cidr;
use cidr_utils::Ipv4CidrSize;
use cidr_utils::separator::Ipv4CidrSeparator;

let cidr = Ipv4Cidr::from_str("192.168.56.0/24").unwrap();

let result = Ipv4CidrSeparator::divide_by(&cidr, 4).unwrap();

assert_eq!(4, result.len());
assert_eq!(64, result[0].size());
assert_eq!(64, result[1].size());
assert_eq!(64, result[2].size());
assert_eq!(64, result[3].size());

assert_eq!("[192.168.56.0/26]".to_string(), result[0].to_string());
assert_eq!("[192.168.56.64/26]".to_string(), result[1].to_string());
assert_eq!("[192.168.56.128/26]".to_string(), result[2].to_string());
assert_eq!("[192.168.56.192/26]".to_string(), result[3].to_string());

let result = Ipv4CidrSeparator::divide_by(&cidr, 5).unwrap();

assert_eq!(5, result.len());
assert_eq!(51, result[0].size());
assert_eq!(51, result[1].size());
assert_eq!(51, result[2].size());
assert_eq!(51, result[3].size());
assert_eq!(52, result[4].size());

assert_eq!("[192.168.56.0/27, 192.168.56.32/28, 192.168.56.48/31, 192.168.56.50/32]".to_string(), result[0].to_string());
assert_eq!("[192.168.56.51/32, 192.168.56.52/30, 192.168.56.56/29, 192.168.56.64/27, 192.168.56.96/30, 192.168.56.100/31]".to_string(), result[1].to_string());
assert_eq!("[192.168.56.102/31, 192.168.56.104/29, 192.168.56.112/28, 192.168.56.128/28, 192.168.56.144/29, 192.168.56.152/32]".to_string(), result[2].to_string());
assert_eq!("[192.168.56.153/32, 192.168.56.154/31, 192.168.56.156/30, 192.168.56.160/27, 192.168.56.192/29, 192.168.56.200/30]".to_string(), result[3].to_string());
assert_eq!("[192.168.56.204/30, 192.168.56.208/28, 192.168.56.224/27]".to_string(), result[4].to_string());

let result = Ipv4CidrSeparator::sub_networks(&cidr, 26).unwrap();

assert_eq!(4, result.len());
assert_eq!(64, result[0].size());
assert_eq!(64, result[1].size());
assert_eq!(64, result[2].size());
assert_eq!(64, result[3].size());

assert_eq!("192.168.56.0/26".to_string(), result[0].to_string());
assert_eq!("192.168.56.64/26".to_string(), result[1].to_string());
assert_eq!("192.168.56.128/26".to_string(), result[2].to_string());
assert_eq!("192.168.56.192/26".to_string(), result[3].to_string());

Crates.io

https://crates.io/crates/cidr-utils

Documentation

https://docs.rs/cidr-utils

License

MIT

Dependencies

~665KB
~17K SLoC