CIDR Utils

Build Status Build status

This crate provides data structures and functions to deal with IPv4 CIDRs and IPv6 CIDRs.

Examples

```rust extern crate cidr_utils;

use cidr_utils::cidr::IpCidr;

asserteq!(true, IpCidr::isipcidr("192.168.1.0/24")); asserteq!(true, IpCidr::isipcidr("192.168.1.1/24")); asserteq!(true, IpCidr::isipcidr("192.168.1.0/0")); asserteq!(false, IpCidr::isipcidr("192.168.1.0/33"));

asserteq!(true, IpCidr::isipcidr("192.168.1.0/255.255.255.0")); asserteq!(true, IpCidr::isipcidr("192.168.1.0/255.255.254.0")); asserteq!(false, IpCidr::isip_cidr("192.168.1.0/255.255.255.1"));

asserteq!(true, IpCidr::isipcidr("192.168.1.0")); // 192.168.1.0/32 asserteq!(true, IpCidr::isipcidr("192.168.1")); // 192.168.1/24 asserteq!(true, IpCidr::isipcidr("192.168")); // 192.168/16 asserteq!(true, IpCidr::isipcidr("192")); // 192/8

asserteq!(true, IpCidr::isipcidr("192/8")); asserteq!(true, IpCidr::isipcidr("192/255.0.0.0")); asserteq!(false, IpCidr::isipcidr("192/16")); asserteq!(false, IpCidr::isipcidr("192/255.255.0.0"));

asserteq!(true, IpCidr::isipcidr("2001:4f8:3:ba::/64")); asserteq!(true, IpCidr::isipcidr("2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128")); asserteq!(true, IpCidr::isipcidr("::ffff:1.2.3.0/120")); asserteq!(true, IpCidr::isipcidr("::ffff:1.2.3.1/120")); asserteq!(false, IpCidr::isip_cidr("::ffff:1.2.3.0/129"));

asserteq!(true, IpCidr::isipcidr("2001:4f8:3:ba:2e0:81ff:fe22:d1f1")); // 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128 asserteq!(true, IpCidr::isipcidr("2001:4f8:3:ba::")); // 2001:4f8:3:ba::/128 ```

```rust extern crate cidr_utils;

use std::net::IpAddr; use std::str::FromStr;

use cidr_utils::cidr::IpCidr;

let cidr = IpCidr::from_str("192.168.51.0/24").unwrap();

asserteq!(true, cidr.contains(IpAddr::fromstr("192.168.51.103").unwrap())); asserteq!(false, cidr.contains(IpAddr::fromstr("192.168.50.103").unwrap())); ```

```rust extern crate cidr_utils;

use std::net::Ipv4Addr;

use cidr_utils::cidr::Ipv4Cidr;

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

asserteq!(true, cidr.contains([192, 168, 51, 103])); asserteq!(true, cidr.contains(Ipv4Addr::new(192, 168, 51, 103))); assert_eq!(false, cidr.contains([192, 168, 50, 103])); ```

```rust extern crate cidr_utils;

use cidrutils::cidr::Ipv4Cidr; use cidrutils::utils::Ipv4CidrCombiner;

let mut combiner = Ipv4CidrCombiner::new();

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

asserteq!(1, combiner.len()); asserteq!("192.168.51.100/32".tostring(), combiner[0].tostring());

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

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

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

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

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

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

asserteq!(true, combiner.contains([192, 168, 51, 102])); asserteq!(false, combiner.contains([192, 168, 51, 105])); ```

Crates.io

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

Documentation

https://docs.rs/cidr-utils

License

MIT