Add ip_mask_to_prefix, ipv4_mask_to_prefix and ipv6_mask_to_prefix

This commit is contained in:
Edward Barnard
2016-07-14 21:37:39 +02:00
parent e9aeab4cb4
commit 8463a3fc5e
3 changed files with 86 additions and 2 deletions

View File

@ -10,8 +10,8 @@ mod ipv4;
mod ipv6;
mod common;
pub use ipv4::Ipv4Network;
pub use ipv6::Ipv6Network;
pub use ipv4::{Ipv4Network, ipv4_mask_to_prefix};
pub use ipv6::{Ipv6Network, ipv6_mask_to_prefix};
pub use common::IpNetworkError;
// A network
@ -43,3 +43,12 @@ impl IpNetwork {
}
}
}
/// Converts a `IpAddr` network mask into a prefix.
/// If the mask is invalid this will return an `IpNetworkError::InvalidPrefix`.
pub fn ip_mask_to_prefix(mask: IpAddr) -> Result<u8, IpNetworkError> {
match mask {
IpAddr::V4(mask) => ipv4_mask_to_prefix(mask),
IpAddr::V6(mask) => ipv6_mask_to_prefix(mask),
}
}