diff --git a/benches/parse_bench.rs b/benches/parse_bench.rs index 0e3341c..5efd8f0 100644 --- a/benches/parse_bench.rs +++ b/benches/parse_bench.rs @@ -3,30 +3,40 @@ use ipnetwork::{Ipv4Network, Ipv6Network}; use std::net::{Ipv4Addr, Ipv6Addr}; fn parse_ipv4_benchmark(c: &mut Criterion) { - c.bench_function("parse ipv4", |b| b.iter(|| { - "127.1.0.0/24".parse::().unwrap() - })); + c.bench_function("parse ipv4", |b| { + b.iter(|| "127.1.0.0/24".parse::().unwrap()) + }); } fn parse_ipv6_benchmark(c: &mut Criterion) { - c.bench_function("parse ipv6", |b| b.iter(|| { - "FF01:0:0:17:0:0:0:2/64".parse::().unwrap() - })); + c.bench_function("parse ipv6", |b| { + b.iter(|| "FF01:0:0:17:0:0:0:2/64".parse::().unwrap()) + }); } fn contains_ipv4_benchmark(c: &mut Criterion) { - c.bench_function("contains ipv4", |b| b.iter(|| { - let cidr = "74.125.227.0/25".parse::().unwrap(); - cidr.contains(Ipv4Addr::new(74, 125, 227, 4)) - })); + c.bench_function("contains ipv4", |b| { + b.iter(|| { + let cidr = "74.125.227.0/25".parse::().unwrap(); + cidr.contains(Ipv4Addr::new(74, 125, 227, 4)) + }) + }); } fn contains_ipv6_benchmark(c: &mut Criterion) { - c.bench_function("contains ipv6", |b| b.iter(|| { - let cidr = "FF01:0:0:17:0:0:0:2/65".parse::().unwrap(); - cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2)) - })); + c.bench_function("contains ipv6", |b| { + b.iter(|| { + let cidr = "FF01:0:0:17:0:0:0:2/65".parse::().unwrap(); + cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2)) + }) + }); } -criterion_group!(benches, parse_ipv4_benchmark, parse_ipv6_benchmark, contains_ipv4_benchmark, contains_ipv6_benchmark); +criterion_group!( + benches, + parse_ipv4_benchmark, + parse_ipv6_benchmark, + contains_ipv4_benchmark, + contains_ipv6_benchmark +); criterion_main!(benches); diff --git a/src/common.rs b/src/common.rs index ac52028..466df22 100644 --- a/src/common.rs +++ b/src/common.rs @@ -37,11 +37,10 @@ pub fn cidr_parts(cidr: &str) -> Result<(&str, Option<&str>), IpNetworkError> { // Error if cidr has multiple slashes if prefix[1..].find('/').is_some() { return Err(IpNetworkError::InvalidCidrFormat(format!( - "CIDR must contain a single '/': {}", - cidr + "CIDR must contain a single '/': {}", + cidr ))); - } - else { + } else { // Handle the case when cidr has exactly one slash return Ok((ip, Some(&prefix[1..]))); } diff --git a/src/ipv4.rs b/src/ipv4.rs index 9051d7a..95e2a27 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -32,6 +32,7 @@ impl Serialize for Ipv4Network { impl Ipv4Network { /// Constructs a new `Ipv4Network` from any `Ipv4Addr` and a prefix denoting the network size. + /// /// If the prefix is larger than 32 this will return an `IpNetworkError::InvalidPrefix`. pub fn new(addr: Ipv4Addr, prefix: u8) -> Result { if prefix > IPV4_BITS { @@ -41,6 +42,21 @@ impl Ipv4Network { } } + /// Constructs a new `Ipv4Network` from a network address and a network mask. + /// + /// If the netmask is not valid this will return an `IpNetworkError::InvalidPrefix`. + pub fn with_netmask( + netaddr: Ipv4Addr, + netmask: Ipv4Addr, + ) -> Result { + let prefix = ipv4_mask_to_prefix(netmask)?; + let net = Self { + addr: netaddr, + prefix, + }; + Ok(net) + } + /// Returns an iterator over `Ipv4Network`. Each call to `next` will return the next /// `Ipv4Addr` in the given network. `None` will be returned when there are no more /// addresses. @@ -70,13 +86,9 @@ impl Ipv4Network { /// Checks if the given `Ipv4Network` is partly contained in other. pub fn overlaps(self, other: Ipv4Network) -> bool { - other.contains(self.ip()) || ( - other.contains(self.broadcast()) || ( - self.contains(other.ip()) || ( - self.contains(other.broadcast()) - ) - ) - ) + other.contains(self.ip()) + || (other.contains(self.broadcast()) + || (self.contains(other.ip()) || (self.contains(other.broadcast())))) } /// Returns the mask for this `Ipv4Network`. @@ -261,6 +273,7 @@ impl Iterator for Ipv4NetworkIterator { } /// Converts a `Ipv4Addr` network mask into a prefix. +/// /// If the mask is invalid this will return an `IpNetworkError::InvalidPrefix`. pub fn ipv4_mask_to_prefix(mask: Ipv4Addr) -> Result { let mask = u32::from(mask); @@ -453,6 +466,24 @@ mod test { assert!(prefix.is_err()); } + #[test] + fn ipv4network_with_netmask() { + { + // Positive test-case. + let addr = Ipv4Addr::new(127, 0, 0, 1); + let mask = Ipv4Addr::new(255, 0, 0, 0); + let net = Ipv4Network::with_netmask(addr, mask).unwrap(); + let expected = Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 1), 8).unwrap(); + assert_eq!(net, expected); + } + { + // Negative test-case. + let addr = Ipv4Addr::new(127, 0, 0, 1); + let mask = Ipv4Addr::new(255, 0, 255, 0); + Ipv4Network::with_netmask(addr, mask).unwrap_err(); + } + } + #[test] fn ipv4network_from_ipv4addr() { let net = Ipv4Network::from(Ipv4Addr::new(127, 0, 0, 1)); @@ -477,14 +508,44 @@ mod test { fn test_is_subnet_of() { let mut test_cases: HashMap<(Ipv4Network, Ipv4Network), bool> = HashMap::new(); - test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false); - test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.0.0/24".parse().unwrap()), true); - test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false); - test_cases.insert(("10.0.1.0/24".parse().unwrap(), "10.0.0.0/30".parse().unwrap()), false); + test_cases.insert( + ( + "10.0.0.0/30".parse().unwrap(), + "10.0.1.0/24".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "10.0.0.0/30".parse().unwrap(), + "10.0.0.0/24".parse().unwrap(), + ), + true, + ); + test_cases.insert( + ( + "10.0.0.0/30".parse().unwrap(), + "10.0.1.0/24".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "10.0.1.0/24".parse().unwrap(), + "10.0.0.0/30".parse().unwrap(), + ), + false, + ); for (key, val) in test_cases.iter() { let (src, dest) = (key.0, key.1); - assert_eq!(src.is_subnet_of(dest), *val, "testing with {} and {}", src, dest); + assert_eq!( + src.is_subnet_of(dest), + *val, + "testing with {} and {}", + src, + dest + ); } } @@ -492,14 +553,44 @@ mod test { fn test_is_supernet_of() { let mut test_cases: HashMap<(Ipv4Network, Ipv4Network), bool> = HashMap::new(); - test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false); - test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.0.0/24".parse().unwrap()), false); - test_cases.insert(("10.0.0.0/30".parse().unwrap(), "10.0.1.0/24".parse().unwrap()), false); - test_cases.insert(("10.0.0.0/24".parse().unwrap(), "10.0.0.0/30".parse().unwrap()), true); + test_cases.insert( + ( + "10.0.0.0/30".parse().unwrap(), + "10.0.1.0/24".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "10.0.0.0/30".parse().unwrap(), + "10.0.0.0/24".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "10.0.0.0/30".parse().unwrap(), + "10.0.1.0/24".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "10.0.0.0/24".parse().unwrap(), + "10.0.0.0/30".parse().unwrap(), + ), + true, + ); for (key, val) in test_cases.iter() { let (src, dest) = (key.0, key.1); - assert_eq!(src.is_supernet_of(dest), *val, "testing with {} and {}", src, dest); + assert_eq!( + src.is_supernet_of(dest), + *val, + "testing with {} and {}", + src, + dest + ); } } diff --git a/src/ipv6.rs b/src/ipv6.rs index d382161..0790f93 100644 --- a/src/ipv6.rs +++ b/src/ipv6.rs @@ -33,6 +33,7 @@ impl Serialize for Ipv6Network { impl Ipv6Network { /// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size. + /// /// If the prefix is larger than 128 this will return an `IpNetworkError::InvalidPrefix`. pub fn new(addr: Ipv6Addr, prefix: u8) -> Result { if prefix > IPV6_BITS { @@ -42,6 +43,18 @@ impl Ipv6Network { } } + /// Constructs a new `Ipv6Network` from a network address and a network mask. + /// + /// If the netmask is not valid this will return an `IpNetworkError::InvalidPrefix`. + pub fn with_netmask(netaddr: Ipv6Addr, netmask: Ipv6Addr) -> Result { + let prefix = ipv6_mask_to_prefix(netmask)?; + let net = Self { + addr: netaddr, + prefix, + }; + Ok(net) + } + /// Returns an iterator over `Ipv6Network`. Each call to `next` will return the next /// `Ipv6Addr` in the given network. `None` will be returned when there are no more /// addresses. @@ -118,13 +131,9 @@ impl Ipv6Network { /// Checks if the given `Ipv6Network` is partly contained in other. pub fn overlaps(self, other: Ipv6Network) -> bool { - other.contains(self.ip()) || ( - other.contains(self.broadcast()) || ( - self.contains(other.ip()) || ( - self.contains(other.broadcast()) - ) - ) - ) + other.contains(self.ip()) + || (other.contains(self.broadcast()) + || (self.contains(other.ip()) || (self.contains(other.broadcast())))) } /// Returns the mask for this `Ipv6Network`. @@ -370,6 +379,25 @@ mod test { assert!(prefix.is_err()); } + #[test] + fn ipv6network_with_netmask() { + { + // Positive test-case. + let addr = Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2); + let mask = Ipv6Addr::new(0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0); + let net = Ipv6Network::with_netmask(addr, mask).unwrap(); + let expected = + Ipv6Network::new(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2), 48).unwrap(); + assert_eq!(net, expected); + } + { + // Negative test-case. + let addr = Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2); + let mask = Ipv6Addr::new(0, 0, 0xffff, 0xffff, 0, 0, 0, 0); + Ipv6Network::with_netmask(addr, mask).unwrap_err(); + } + } + #[test] fn iterator_v6() { let cidr: Ipv6Network = "2001:db8::/126".parse().unwrap(); @@ -459,14 +487,44 @@ mod test { fn test_is_subnet_of() { let mut test_cases: HashMap<(Ipv6Network, Ipv6Network), bool> = HashMap::new(); - test_cases.insert(("2000:999::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false); - test_cases.insert(("2000:aaa::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), true); - test_cases.insert(("2000:bbb::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false); - test_cases.insert(("2000:aaa::/48".parse().unwrap(), "2000:aaa::/56".parse().unwrap()), false); + test_cases.insert( + ( + "2000:999::/56".parse().unwrap(), + "2000:aaa::/48".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "2000:aaa::/56".parse().unwrap(), + "2000:aaa::/48".parse().unwrap(), + ), + true, + ); + test_cases.insert( + ( + "2000:bbb::/56".parse().unwrap(), + "2000:aaa::/48".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "2000:aaa::/48".parse().unwrap(), + "2000:aaa::/56".parse().unwrap(), + ), + false, + ); for (key, val) in test_cases.iter() { let (src, dest) = (key.0, key.1); - assert_eq!(src.is_subnet_of(dest), *val, "testing with {} and {}", src, dest); + assert_eq!( + src.is_subnet_of(dest), + *val, + "testing with {} and {}", + src, + dest + ); } } @@ -474,14 +532,44 @@ mod test { fn test_is_supernet_of() { let mut test_cases: HashMap<(Ipv6Network, Ipv6Network), bool> = HashMap::new(); - test_cases.insert(("2000:999::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false); - test_cases.insert(("2000:aaa::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false); - test_cases.insert(("2000:bbb::/56".parse().unwrap(), "2000:aaa::/48".parse().unwrap()), false); - test_cases.insert(("2000:aaa::/48".parse().unwrap(), "2000:aaa::/56".parse().unwrap()), true); + test_cases.insert( + ( + "2000:999::/56".parse().unwrap(), + "2000:aaa::/48".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "2000:aaa::/56".parse().unwrap(), + "2000:aaa::/48".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "2000:bbb::/56".parse().unwrap(), + "2000:aaa::/48".parse().unwrap(), + ), + false, + ); + test_cases.insert( + ( + "2000:aaa::/48".parse().unwrap(), + "2000:aaa::/56".parse().unwrap(), + ), + true, + ); for (key, val) in test_cases.iter() { let (src, dest) = (key.0, key.1); - assert_eq!(src.is_supernet_of(dest), *val, "testing with {} and {}", src, dest); + assert_eq!( + src.is_supernet_of(dest), + *val, + "testing with {} and {}", + src, + dest + ); } } diff --git a/src/lib.rs b/src/lib.rs index c645ce0..24abf7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,6 +63,14 @@ impl IpNetwork { } } + /// Constructs a new `IpNetwork` from a network address and a network mask. + /// + /// If the netmask is not valid this will return an `IpNetworkError::InvalidPrefix`. + pub fn with_netmask(netaddr: IpAddr, netmask: IpAddr) -> Result { + let prefix = ip_mask_to_prefix(netmask)?; + Self::new(netaddr, prefix) + } + /// Returns the IP part of a given `IpNetwork` pub fn ip(&self) -> IpAddr { match *self { @@ -313,8 +321,9 @@ mod test { fn deserialize_from_serde_json_value() { use super::*; let network = IpNetwork::from_str("0.0.0.0/0").unwrap(); - let val: serde_json::value::Value = serde_json::from_str(&serde_json::to_string(&network).unwrap()).unwrap(); - let _deser: IpNetwork = - serde_json::from_value(val).expect("Fails to deserialize from json_value::value::Value"); + let val: serde_json::value::Value = + serde_json::from_str(&serde_json::to_string(&network).unwrap()).unwrap(); + let _deser: IpNetwork = serde_json::from_value(val) + .expect("Fails to deserialize from json_value::value::Value"); } }