Enum ipnet::IpNet [] [src]

pub enum IpNet {
    V4(Ipv4Net),
    V6(Ipv6Net),
}

An IP network address, either IPv4 or IPv6.

This enum can contain either an Ipv4Net or an Ipv6Net, see their respective documentation for more details.

Examples

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use ipnet::{IpNet, Ipv4Net, Ipv6Net};

let net_v4 = IpNet::from_str("10.1.1.0/24").unwrap();
let net_v6 = IpNet::from_str("fd00::/32").unwrap();
 
assert_eq!("10.1.1.0".parse(), Ok(net_v4.network()));
assert_eq!("fd00::".parse(), Ok(net_v6.network()));

Variants

Methods

impl IpNet
[src]

[src]

Return the address.

[src]

Return the prefix length.

[src]

Returns the network mask.

Examples

let net = IpNet::from_str("10.1.0.0/20").unwrap();
assert_eq!(net.netmask(), IpAddr::from_str("255.255.240.0").unwrap());

let net = IpNet::from_str("fd00::/24").unwrap();
assert_eq!(net.netmask(), IpAddr::from_str("ffff:ff00::").unwrap());

[src]

Returns the host mask.

Examples

let net = IpNet::from_str("10.1.0.0/20").unwrap();
assert_eq!(net.hostmask(), IpAddr::from_str("0.0.15.255").unwrap());

let net = IpNet::from_str("fd00::/24").unwrap();
assert_eq!(net.hostmask(), IpAddr::from_str("::ff:ffff:ffff:ffff:ffff:ffff:ffff").unwrap());

[src]

Returns the network address.

Examples

let net = IpNet::from_str("172.16.123.123/16").unwrap();
assert_eq!(net.network(), IpAddr::from_str("172.16.0.0").unwrap());

let net = IpNet::from_str("fd00:1234:5678::/24").unwrap();
assert_eq!(net.network(), IpAddr::from_str("fd00:1200::").unwrap());

[src]

Returns the broadcast address.

Examples

let net = IpNet::from_str("172.16.0.0/22").unwrap();
assert_eq!(net.broadcast(), IpAddr::from_str("172.16.3.255").unwrap());

let net = IpNet::from_str("fd00:1234:5678::/24").unwrap();
assert_eq!(net.broadcast(), IpAddr::from_str("fd00:12ff:ffff:ffff:ffff:ffff:ffff:ffff").unwrap());

[src]

Return a copy of the network with the address truncated to the prefix length.

Examples

assert_eq!(
    IpNet::from_str("192.168.1.2/16").unwrap().trunc(),
    IpNet::from_str("192.168.0.0/16").unwrap()
);

[src]

Returns the IpNet that contains this one.

Examples

let n1 = IpNet::from_str("172.16.1.0/24").unwrap();
let n2 = IpNet::from_str("172.16.0.0/23").unwrap();
let n3 = IpNet::from_str("172.16.0.0/0").unwrap();

assert_eq!(n1.supernet().unwrap(), n2);
assert_eq!(n3.supernet(), None);

let n1 = IpNet::from_str("fd00:ff00::/24").unwrap();
let n2 = IpNet::from_str("fd00:fe00::/23").unwrap();
let n3 = IpNet::from_str("fd00:fe00::/0").unwrap();

assert_eq!(n1.supernet().unwrap(), n2);
assert_eq!(n3.supernet(), None);

[src]

Returns true if this network and the given network are children of the same supernet.

Examples

let net4_1 = IpNet::from_str("10.1.0.0/24").unwrap();
let net4_2 = IpNet::from_str("10.1.1.0/24").unwrap();
let net4_3 = IpNet::from_str("10.1.2.0/24").unwrap();
let net6_1 = IpNet::from_str("fd00::/18").unwrap();
let net6_2 = IpNet::from_str("fd00:4000::/18").unwrap();
let net6_3 = IpNet::from_str("fd00:8000::/18").unwrap();

assert!( net4_1.is_sibling(&net4_2));
assert!(!net4_2.is_sibling(&net4_3));
assert!( net6_1.is_sibling(&net6_2));
assert!(!net6_2.is_sibling(&net6_3));
assert!(!net4_1.is_sibling(&net6_2));

[src]

Return an Iterator over the host addresses in this network.

See the implementations on Ipv4Net and Ipv6Net for more information.

Examples

let net = IpNet::from_str("10.0.0.0/30").unwrap();
assert_eq!(net.hosts().collect::<Vec<IpAddr>>(), vec![
    IpAddr::from_str("10.0.0.1").unwrap(),
    IpAddr::from_str("10.0.0.2").unwrap(),
]);

let net = IpNet::from_str("10.0.0.0/31").unwrap();
assert_eq!(net.hosts().collect::<Vec<IpAddr>>(), vec![
    IpAddr::from_str("10.0.0.0").unwrap(),
    IpAddr::from_str("10.0.0.1").unwrap(),
]);

let net = IpNet::from_str("fd00::/126").unwrap();
assert_eq!(net.hosts().collect::<Vec<IpAddr>>(), vec![
    IpAddr::from_str("fd00::").unwrap(),
    IpAddr::from_str("fd00::1").unwrap(),
    IpAddr::from_str("fd00::2").unwrap(),
    IpAddr::from_str("fd00::3").unwrap(),
]);

[src]

Returns an Iterator over the subnets of this network with the given prefix length.

Examples

let net = IpNet::from_str("10.0.0.0/24").unwrap();
assert_eq!(net.subnets(26).unwrap().collect::<Vec<IpNet>>(), vec![
    IpNet::from_str("10.0.0.0/26").unwrap(),
    IpNet::from_str("10.0.0.64/26").unwrap(),
    IpNet::from_str("10.0.0.128/26").unwrap(),
    IpNet::from_str("10.0.0.192/26").unwrap(),
]);

let net = IpNet::from_str("fd00::/16").unwrap();
assert_eq!(net.subnets(18).unwrap().collect::<Vec<IpNet>>(), vec![
    IpNet::from_str("fd00::/18").unwrap(),
    IpNet::from_str("fd00:4000::/18").unwrap(),
    IpNet::from_str("fd00:8000::/18").unwrap(),
    IpNet::from_str("fd00:c000::/18").unwrap(),
]);

let net = IpNet::from_str("10.0.0.0/24").unwrap();
assert_eq!(net.subnets(23), Err(PrefixLenError));

let net = IpNet::from_str("10.0.0.0/24").unwrap();
assert_eq!(net.subnets(33), Err(PrefixLenError));

let net = IpNet::from_str("fd00::/16").unwrap();
assert_eq!(net.subnets(15), Err(PrefixLenError));

let net = IpNet::from_str("fd00::/16").unwrap();
assert_eq!(net.subnets(129), Err(PrefixLenError));

[src]

Aggregate a Vec of IpNets and return the result as a new Vec.

Examples

let ipnets = vec![
    IpNet::from_str("10.0.0.0/24").unwrap(),
    IpNet::from_str("10.0.1.0/24").unwrap(),
    IpNet::from_str("10.0.2.0/24").unwrap(),
    IpNet::from_str("fd00::/18").unwrap(),
    IpNet::from_str("fd00:4000::/18").unwrap(),
    IpNet::from_str("fd00:8000::/18").unwrap(),
];
assert_eq!(IpNet::aggregate(&ipnets), vec![
    IpNet::from_str("10.0.0.0/23").unwrap(),
    IpNet::from_str("10.0.2.0/24").unwrap(),
    IpNet::from_str("fd00::/17").unwrap(),
    IpNet::from_str("fd00:8000::/18").unwrap(),
]);

Trait Implementations

impl Copy for IpNet
[src]

impl Clone for IpNet
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl Eq for IpNet
[src]

impl PartialEq for IpNet
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl Ord for IpNet
[src]

[src]

This method returns an Ordering between self and other. Read more

[src]

🔬 This is a nightly-only experimental API. (ord_max_min)

Compares and returns the maximum of two values. Read more

[src]

🔬 This is a nightly-only experimental API. (ord_max_min)

Compares and returns the minimum of two values. Read more

impl PartialOrd for IpNet
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

[src]

This method tests less than (for self and other) and is used by the < operator. Read more

[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Hash for IpNet
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl Debug for IpNet
[src]

[src]

Formats the value using the given formatter.

impl Display for IpNet
[src]

[src]

Formats the value using the given formatter. Read more

impl From<Ipv4Net> for IpNet
[src]

[src]

Performs the conversion.

impl From<Ipv6Net> for IpNet
[src]

[src]

Performs the conversion.

impl<'a> Contains<&'a IpNet> for IpNet
[src]

[src]

Returns true if this network contains the given network or address. Read more

impl<'a> Contains<&'a IpAddr> for IpNet
[src]

[src]

Returns true if this network contains the given network or address. Read more

impl FromStr for IpNet
[src]

The associated error which can be returned from parsing.

[src]

Parses a string s to return a value of this type. Read more