Struct ip_network::Ipv6Network

source ·
pub struct Ipv6Network { /* private fields */ }
Expand description

IPv6 Network.

Implementations§

source§

impl Ipv6Network

source

pub const LENGTH: u8 = 128u8

IPv6 address length in bits.

source

pub const DEFAULT_ROUTE: Self = _

Default route that contains all IP addresses, IP network ::/0

source

pub fn new( network_address: Ipv6Addr, netmask: u8, ) -> Result<Self, IpNetworkError>

Constructs new Ipv6Network based on Ipv6Addr and netmask.

Returns error if netmask is bigger than 128 or if host bits are set in network_address.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0);
let ip_network = Ipv6Network::new(ip, 32)?;
assert_eq!(ip_network.network_address(), ip);
assert_eq!(ip_network.netmask(), 32);
source

pub fn new_truncate( network_address: Ipv6Addr, netmask: u8, ) -> Result<Self, IpNetworkError>

Constructs new Ipv6Network based on Ipv6Addr and netmask with truncating host bits from given network_address.

Returns error if netmask is bigger than 128.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 1, 0, 0);
let ip_network = Ipv6Network::new_truncate(ip, 32)?;
assert_eq!(ip_network.network_address(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0));
assert_eq!(ip_network.netmask(), 32);
source

pub fn network_address(&self) -> Ipv6Addr

Returns network IP address (first address in range).

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0);
let ip_network = Ipv6Network::new(ip, 32)?;
assert_eq!(ip_network.network_address(), ip);
source

pub fn last_address(&self) -> Ipv6Addr

Returns last IP address in range. Similar as broadcast_address for IPv4.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0);
let ip_network = Ipv6Network::new(ip, 32)?;
assert_eq!(ip_network.last_address(), Ipv6Addr::new(0x2001, 0xdb8, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff));
source

pub fn netmask(&self) -> u8

Returns network mask.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0);
let ip_network = Ipv6Network::new(ip, 32)?;
assert_eq!(ip_network.netmask(), 32);
source

pub fn contains(&self, ip: Ipv6Addr) -> bool

Returns true if given IPv6Addr is inside this network.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip_network = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 64)?;
assert!(ip_network.contains(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)));
assert!(!ip_network.contains(Ipv6Addr::new(0x2001, 0xdb9, 0, 0, 0, 0, 0, 0)));
source

pub fn supernet(&self) -> Option<Self>

Returns network with smaller netmask by one. If netmask is already zero, None will be returned.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let network = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32)?;
assert_eq!(network.supernet(), Some(Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 31)?));
source

pub fn subnets(&self) -> Ipv6NetworkIterator

Returns Ipv6NetworkIterator over networks with netmask bigger one. If netmask is already 128, empty iterator will be returned.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip_network = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32)?;
let mut iterator = ip_network.subnets();
assert_eq!(iterator.next().unwrap(), Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 33)?);
assert_eq!(iterator.last().unwrap(), Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0x8000, 0, 0, 0, 0, 0), 33)?);
source

pub fn subnets_with_prefix(&self, prefix: u8) -> Ipv6NetworkIterator

Returns Ipv6NetworkIterator over networks with defined netmask. Because len() method returns usize and number of networks can be bigger than usize, you can use real_len() method to get exact number of networks.

§Panics

This method panics when prefix is bigger than 128 or when prefix is lower or equal than netmask.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let network = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32)?;
let mut iterator = network.subnets_with_prefix(33);
assert_eq!(2, iterator.real_len());
assert_eq!(iterator.next().unwrap(), Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 33)?);
assert_eq!(iterator.last().unwrap(), Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0x8000, 0, 0, 0, 0, 0), 33)?);
source

pub fn is_default_route(&self) -> bool

Returns true for the default route network (::/0), that contains all IPv6 addresses.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 0)?.is_default_route());
source

pub fn is_unspecified(&self) -> bool

Returns true for the special ‘unspecified’ network (::/128).

This property is defined in IETF RFC 4291.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_unspecified());
assert!(Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 128)?.is_unspecified());
source

pub fn is_loopback(&self) -> bool

Returns true if this is a loopback network (::1/128).

This property is defined in IETF RFC 4291.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1), 128)?.is_loopback());
assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_loopback());
source

pub fn is_global(&self) -> bool

Returns true if the address appears to be globally routable.

The following return false:

  • the loopback network
  • link-local, site-local, and unique local unicast networks
  • interface-, link-, realm-, admin- and site-local multicast networks
§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_global());
assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1), 128)?.is_global());
assert!(Ipv6Network::new(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1), 128)?.is_global());
source

pub fn is_unique_local(&self) -> bool

Returns true if this is a part of unique local network (fc00::/7).

This property is defined in IETF RFC 4193.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0), 16)?.is_unique_local());
assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_unique_local());

Returns true if the network is part of unicast and link-local (fe80::/10).

This property is defined in IETF RFC 4291.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0), 16)?.is_unicast_link_local());
assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_unicast_link_local());
source

pub fn is_unicast_site_local(&self) -> bool

Returns true if this is a deprecated unicast site-local network (fec0::/10).

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0), 16)?.is_unicast_site_local());
assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_unicast_site_local());
source

pub fn is_documentation(&self) -> bool

Returns true if this is a part of network reserved for documentation (2001:db8::/32).

This property is defined in IETF RFC 3849.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32)?.is_documentation());
assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_documentation());
source

pub fn is_unicast_global(&self) -> bool

Returns true if the network is a globally routable unicast network.

The following return false:

  • the loopback network
  • the link-local network
  • the (deprecated) site-local network
  • unique local network
  • the unspecified network
  • the network range reserved for documentation
§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(!Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32)?.is_unicast_global());
assert!(Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_unicast_global());
source

pub fn is_multicast(&self) -> bool

Returns true if this is a part of multicast network (ff00::/8).

This property is defined by IETF RFC 4291.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

assert!(Ipv6Network::new(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0), 8)?.is_multicast());
assert!(!Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.is_multicast());
source

pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope>

Returns the network’s multicast scope if the network is multicast.

These scopes are defined in IETF RFC 7346.

§Examples
use std::net::Ipv6Addr;
use ip_network::{Ipv6Network, Ipv6MulticastScope};

assert_eq!(Ipv6Network::new(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0), 32)?.multicast_scope(),
                             Some(Ipv6MulticastScope::Global));
assert_eq!(Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff), 128)?.multicast_scope(), None);
source

pub fn from_str_truncate(s: &str) -> Result<Self, IpNetworkParseError>

Converts string in format X:X::X/Y (CIDR notation) to Ipv6Network, but truncating host bits.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip_network = Ipv6Network::from_str_truncate("2001:db8::1/32")?;
assert_eq!(ip_network.network_address(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0));
assert_eq!(ip_network.netmask(), 32);
source

pub fn collapse_addresses(addresses: &[Self]) -> Vec<Self>

Return an iterator of the collapsed Ipv6Networks.

Implementation of this method was inspired by Python ipaddress.collapse_addresses

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;
use std::str::FromStr;

let collapsed = Ipv6Network::collapse_addresses(&[
    Ipv6Network::from_str("2001::/120")?,
    Ipv6Network::from_str("2001::/96")?,
]);

assert_eq!(Ipv6Network::from_str("2001::/96")?, collapsed[0]);

Trait Implementations§

source§

impl Clone for Ipv6Network

source§

fn clone(&self) -> Ipv6Network

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Ipv6Network

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Ipv6Network

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Converts Ipv6Network to string in format X:X::X/Y (CIDR notation).

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;

let ip_network = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32)?;
assert_eq!(ip_network.to_string(), "2001:db8::/32");
source§

impl From<Ipv6Addr> for Ipv6Network

source§

fn from(ip: Ipv6Addr) -> Self

Converts Ipv6Addr to Ipv6Network with netmask 128.

source§

impl From<Ipv6Network> for IpNetwork

source§

fn from(network: Ipv6Network) -> Self

Converts to this type from the input type.
source§

impl FromStr for Ipv6Network

source§

fn from_str(s: &str) -> Result<Ipv6Network, IpNetworkParseError>

Converts string in format X:X::X/Y (CIDR notation) to Ipv6Network.

§Examples
use std::net::Ipv6Addr;
use ip_network::Ipv6Network;
use std::str::FromStr;

let ip_network = Ipv6Network::from_str("2001:db8::/32")?;
assert_eq!(ip_network.network_address(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0));
assert_eq!(ip_network.netmask(), 32);
§

type Err = IpNetworkParseError

The associated error which can be returned from parsing.
source§

impl Hash for Ipv6Network

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Ipv6Network

source§

fn cmp(&self, other: &Ipv6Network) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<IpNetwork> for Ipv6Network

source§

fn eq(&self, other: &IpNetwork) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Ipv6Network> for IpNetwork

source§

fn eq(&self, other: &Ipv6Network) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for Ipv6Network

source§

fn eq(&self, other: &Ipv6Network) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<IpNetwork> for Ipv6Network

source§

fn partial_cmp(&self, other: &IpNetwork) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl PartialOrd<Ipv6Network> for IpNetwork

source§

fn partial_cmp(&self, other: &Ipv6Network) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl PartialOrd for Ipv6Network

source§

fn partial_cmp(&self, other: &Ipv6Network) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl Copy for Ipv6Network

source§

impl Eq for Ipv6Network

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.