mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-16 16:58:50 +00:00
ipnetwork: add netmask constructors
This adds constructors to build all network types from a network address and network mask pair.
This commit is contained in:
35
src/ipv4.rs
35
src/ipv4.rs
@ -32,6 +32,7 @@ impl Serialize for Ipv4Network {
|
|||||||
|
|
||||||
impl Ipv4Network {
|
impl Ipv4Network {
|
||||||
/// Constructs a new `Ipv4Network` from any `Ipv4Addr` and a prefix denoting the network size.
|
/// 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`.
|
/// If the prefix is larger than 32 this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn new(addr: Ipv4Addr, prefix: u8) -> Result<Ipv4Network, IpNetworkError> {
|
pub fn new(addr: Ipv4Addr, prefix: u8) -> Result<Ipv4Network, IpNetworkError> {
|
||||||
if prefix > IPV4_BITS {
|
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<Ipv4Network, IpNetworkError> {
|
||||||
|
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
|
/// 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
|
/// `Ipv4Addr` in the given network. `None` will be returned when there are no more
|
||||||
/// addresses.
|
/// addresses.
|
||||||
@ -257,6 +273,7 @@ impl Iterator for Ipv4NetworkIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a `Ipv4Addr` network mask into a prefix.
|
/// Converts a `Ipv4Addr` network mask into a prefix.
|
||||||
|
///
|
||||||
/// If the mask is invalid this will return an `IpNetworkError::InvalidPrefix`.
|
/// If the mask is invalid this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn ipv4_mask_to_prefix(mask: Ipv4Addr) -> Result<u8, IpNetworkError> {
|
pub fn ipv4_mask_to_prefix(mask: Ipv4Addr) -> Result<u8, IpNetworkError> {
|
||||||
let mask = u32::from(mask);
|
let mask = u32::from(mask);
|
||||||
@ -449,6 +466,24 @@ mod test {
|
|||||||
assert!(prefix.is_err());
|
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]
|
#[test]
|
||||||
fn ipv4network_from_ipv4addr() {
|
fn ipv4network_from_ipv4addr() {
|
||||||
let net = Ipv4Network::from(Ipv4Addr::new(127, 0, 0, 1));
|
let net = Ipv4Network::from(Ipv4Addr::new(127, 0, 0, 1));
|
||||||
|
32
src/ipv6.rs
32
src/ipv6.rs
@ -33,6 +33,7 @@ impl Serialize for Ipv6Network {
|
|||||||
|
|
||||||
impl Ipv6Network {
|
impl Ipv6Network {
|
||||||
/// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size.
|
/// 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`.
|
/// If the prefix is larger than 128 this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn new(addr: Ipv6Addr, prefix: u8) -> Result<Ipv6Network, IpNetworkError> {
|
pub fn new(addr: Ipv6Addr, prefix: u8) -> Result<Ipv6Network, IpNetworkError> {
|
||||||
if prefix > IPV6_BITS {
|
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<Self, IpNetworkError> {
|
||||||
|
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
|
/// 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
|
/// `Ipv6Addr` in the given network. `None` will be returned when there are no more
|
||||||
/// addresses.
|
/// addresses.
|
||||||
@ -366,6 +379,25 @@ mod test {
|
|||||||
assert!(prefix.is_err());
|
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]
|
#[test]
|
||||||
fn iterator_v6() {
|
fn iterator_v6() {
|
||||||
let cidr: Ipv6Network = "2001:db8::/126".parse().unwrap();
|
let cidr: Ipv6Network = "2001:db8::/126".parse().unwrap();
|
||||||
|
@ -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<Self, IpNetworkError> {
|
||||||
|
let prefix = ip_mask_to_prefix(netmask)?;
|
||||||
|
Self::new(netaddr, prefix)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the IP part of a given `IpNetwork`
|
/// Returns the IP part of a given `IpNetwork`
|
||||||
pub fn ip(&self) -> IpAddr {
|
pub fn ip(&self) -> IpAddr {
|
||||||
match *self {
|
match *self {
|
||||||
|
Reference in New Issue
Block a user