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]

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());

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());

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());

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());

Returns the IpNet that contains this one.

Examples

let net = IpNet::from_str("172.16.1.0/24").unwrap();
assert_eq!(net.supernet().network(), IpAddr::from_str("172.16.0.0").unwrap());

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

Returns the subnets of this network at the given prefix length.

Examples

let net = IpNet::from_str("10.1.0.0/16").unwrap();
assert_eq!(net.subnets(18), vec![
    IpNet::from_str("10.1.0.0/18").unwrap(),
    IpNet::from_str("10.1.64.0/18").unwrap(),
    IpNet::from_str("10.1.128.0/18").unwrap(),
    IpNet::from_str("10.1.192.0/18").unwrap(),
]);

let net = IpNet::from_str("fd00::/16").unwrap();
assert_eq!(net.subnets(18), 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(),
]);

Returns true if this network contains the given network.

Examples

let net1 = IpNet::from_str("10.1.0.0/16").unwrap();
let net2 = IpNet::from_str("10.1.1.0/24").unwrap();
assert!(net1.contains(&net2));

let net61 = IpNet::from_str("fd00::/16").unwrap();
let net62 = IpNet::from_str("fd00::/17").unwrap();
assert!(net61.contains(&net62));
assert!(!net1.contains(&net62))

Returns true if this network and the given network are both in the same supernet.

Examples

let net1 = IpNet::from_str("10.1.0.0/24").unwrap();
let net2 = IpNet::from_str("10.1.1.0/24").unwrap();
let net3 = IpNet::from_str("10.1.2.0/24").unwrap();
assert!(net1.sibling_of(&net2));
assert!(!net2.sibling_of(&net3));

let net61 = IpNet::from_str("fd00::/18").unwrap();
let net62 = IpNet::from_str("fd00:4000::/18").unwrap();
let net63 = IpNet::from_str("fd00:8000::/18").unwrap();
assert!(net61.sibling_of(&net62));
assert!(!net62.sibling_of(&net63));
assert!(!net1.sibling_of(&net62));

Trait Implementations

impl Copy for IpNet
[src]

impl Clone for IpNet
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Eq for IpNet
[src]

impl PartialEq for IpNet
[src]

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

This method tests for !=.

impl Hash for IpNet
[src]

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

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

impl PartialOrd for IpNet
[src]

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

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

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

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

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

impl Ord for IpNet
[src]

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

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

Compares and returns the maximum of two values. Read more

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

Compares and returns the minimum of two values. Read more

impl Debug for IpNet
[src]

Formats the value using the given formatter.

impl Display for IpNet
[src]

Formats the value using the given formatter. Read more

impl From<Ipv4Net> for IpNet
[src]

Performs the conversion.

impl From<Ipv6Net> for IpNet
[src]

Performs the conversion.

impl FromStr for IpNet
[src]

The associated error which can be returned from parsing.

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