Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix /0 networks #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/ipv4.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::IpNetworkError;
use crate::parse::{cidr_parts, parse_prefix};
use std::{convert::TryFrom, fmt, net::Ipv4Addr, str::FromStr};
use std::{convert::TryFrom, fmt, net::Ipv4Addr, str::FromStr, num::Wrapping, ops::Shl};

const IPV4_BITS: u8 = 32;

Expand Down Expand Up @@ -94,7 +94,7 @@
/// addresses.
pub fn iter(self) -> Ipv4NetworkIterator {
let start = u32::from(self.network());
let end = start + (self.size() - 1);
let end = start + (self.size().wrapping_sub(1));
Ipv4NetworkIterator {
next: Some(start),
end,
Expand All @@ -111,7 +111,7 @@

/// Checks if the given `Ipv4Network` is a subnet of the other.
pub fn is_subnet_of(self, other: Ipv4Network) -> bool {
other.ip() <= self.ip() && other.broadcast() >= self.broadcast()
(other.ip() <= self.ip() && other.broadcast() >= self.broadcast()) || self.prefix == 0
}

/// Checks if the given `Ipv4Network` is a supernet of the other.
Expand Down Expand Up @@ -215,8 +215,11 @@
/// assert_eq!(tinynet.size(), 1);
/// ```
pub fn size(self) -> u32 {
1 << (u32::from(IPV4_BITS - self.prefix))
}
match self.prefix {
0 => u32::MAX,
p => Wrapping(1usize).shl((IPV4_BITS as usize).wrapping_sub(p as usize)).0 as u32
}
}

/// Returns the `n`:th address within this network.
/// The adresses are indexed from 0 and `n` must be smaller than the size of the network.
Expand Down Expand Up @@ -346,6 +349,7 @@
#[cfg(test)]
mod test {
use super::*;
use std::borrow::BorrowMut;

Check warning on line 352 in src/ipv4.rs

View workflow job for this annotation

GitHub Actions / build (stable)

unused import: `std::borrow::BorrowMut`

Check warning on line 352 in src/ipv4.rs

View workflow job for this annotation

GitHub Actions / build (beta)

unused import: `std::borrow::BorrowMut`

Check warning on line 352 in src/ipv4.rs

View workflow job for this annotation

GitHub Actions / build (nightly)

unused import: `std::borrow::BorrowMut`
use std::collections::HashMap;
use std::mem;
use std::net::Ipv4Addr;
Expand Down Expand Up @@ -446,7 +450,7 @@
#[test]
fn copy_compatibility_v4() {
let net = Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 1), 16).unwrap();
mem::drop(net);

Check warning on line 453 in src/ipv4.rs

View workflow job for this annotation

GitHub Actions / build (beta)

calls to `std::mem::drop` with a value that implements `Copy` does nothing

Check warning on line 453 in src/ipv4.rs

View workflow job for this annotation

GitHub Actions / build (nightly)

calls to `std::mem::drop` with a value that implements `Copy` does nothing
assert_eq!(16, net.prefix());
}

Expand Down Expand Up @@ -499,12 +503,15 @@
// Tests the entire IPv4 space to see if the iterator will stop at the correct place
// and not overflow or wrap around. Ignored since it takes a long time to run.
#[test]
#[ignore]
#[cfg_attr(debug_assertions, ignore)]
fn iterator_v4_huge() {
let cidr: Ipv4Network = "0/0".parse().unwrap();
let mut iter = cidr.iter();
for i in 0..(u32::max_value() as u64 + 1) {
assert_eq!(i as u32, u32::from(iter.next().unwrap()));
let cidr: Ipv4Network = "0.0.0.0/0".parse().unwrap();
assert_eq!(cidr.size(), u32::MAX);
let mut iter: &mut dyn Iterator<Item=Ipv4Addr> = &mut cidr.iter();
let mut skip = iter.skip((u32::MAX - 10) as usize);
iter = &mut skip;
for i in (1u32..11).rev() {
assert_eq!(u32::MAX - i, u32::from(iter.next().unwrap()));
}
assert_eq!(None, iter.next());
}
Expand Down Expand Up @@ -652,6 +659,20 @@
),
true,
);
test_cases.insert(
(
"10.0.0.0/24".parse().unwrap(),
"0.0.0.0/0".parse().unwrap(),
),
true,
);
test_cases.insert(
(
"0.0.0.0/0".parse().unwrap(),
"10.0.0.0/24".parse().unwrap(),
),
true,
);

for (key, val) in test_cases.iter() {
let (src, dest) = (key.0, key.1);
Expand All @@ -668,10 +689,12 @@
let other: Ipv4Network = "1.2.3.0/30".parse().unwrap();
let other2: Ipv4Network = "1.2.2.0/24".parse().unwrap();
let other3: Ipv4Network = "1.2.2.64/26".parse().unwrap();

let skynet: Ipv4Network = "1.2.3.0/24".parse().unwrap();
let uberskynet: Ipv4Network = "0.0.0.0/0".parse().unwrap();

assert!(skynet.overlaps(other));
assert!(!skynet.overlaps(other2));
assert!(uberskynet.overlaps(skynet));
assert!(other2.overlaps(other3));
}

Expand Down
13 changes: 10 additions & 3 deletions src/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Ipv6Network {

/// Checks if the given `Ipv6Network` is a subnet of the other.
pub fn is_subnet_of(self, other: Ipv6Network) -> bool {
other.ip() <= self.ip() && other.broadcast() >= self.broadcast()
(other.ip() <= self.ip() && other.broadcast() >= self.broadcast()) || self.prefix == 0
}

/// Checks if the given `Ipv6Network` is a supernet of the other.
Expand Down Expand Up @@ -244,8 +244,13 @@ impl Ipv6Network {
/// assert_eq!(tinynet.size(), 1);
/// ```
pub fn size(&self) -> u128 {
let host_bits = u32::from(IPV6_BITS - self.prefix);
2u128.pow(host_bits)
match self.prefix {
0 => u128::MAX,
p => {
let host_bits = u32::from(IPV6_BITS - p);
2u128.pow(host_bits)
}
}
}

/// Returns the `n`:th address within this network.
Expand Down Expand Up @@ -678,8 +683,10 @@ mod test {
fn test_overlaps() {
let other: Ipv6Network = "2001:DB8:ACAD::1/64".parse().unwrap();
let other2: Ipv6Network = "2001:DB8:ACAD::20:2/64".parse().unwrap();
let skynet: Ipv6Network = "::/0".parse().unwrap();

assert_eq!(other2.overlaps(other), true);
assert!(skynet.overlaps(other) && skynet.overlaps(other2));
}

#[test]
Expand Down