Skip to content

Commit

Permalink
Fix for 0::/0 network (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeauGieskens authored Jan 7, 2025
1 parent 551d1a6 commit e1d3ac6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ impl Ipv6Network {
pub fn mask(&self) -> Ipv6Addr {
debug_assert!(self.prefix <= IPV6_BITS);

if self.prefix == 0 {
return Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
}
let mask = u128::MAX << (IPV6_BITS - self.prefix);
Ipv6Addr::from(mask)
}
Expand Down Expand Up @@ -279,6 +282,10 @@ impl Ipv6Network {
/// ```
pub fn size(&self) -> u128 {
debug_assert!(self.prefix <= IPV6_BITS);

if self.prefix == 0 {
return u128::MAX;
}
1 << (IPV6_BITS - self.prefix)
}

Expand Down Expand Up @@ -757,4 +764,17 @@ mod test {
);
assert_eq!(net.nth(net.size()), None);
}

#[test]
fn test_mask_with_prefix_0() {
let network: Ipv6Network = "0::/0".parse().unwrap();
let mask = network.mask();
assert_eq!(mask, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
}

#[test]
fn test_size_with_prefix_0() {
let network: Ipv6Network = "0::/0".parse().unwrap();
assert_eq!(network.size(), u128::MAX);
}
}

0 comments on commit e1d3ac6

Please sign in to comment.