Struct net_adds::Ipv6AddrNetwork [−][src]
pub struct Ipv6AddrNetwork { /* fields omitted */ }
Expand description
An IPv6 address network.
See crate::IpAddrNetwork
for a type encompassing both IPv4 and IPv6 network.
The size of an Ipv6AddrNetwork
struct may vary depending on the target operating
system.
Textual representation
Ipv6AddrNetwork
provides a FromStr
implementation. The two parts are divided by /
.
The first part must contain an IPv6. The second part can either contain an IPv6 or an u8
between 0 and 128 which is valid as a netmask prefix.
Examples
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let ip = Ipv6Addr::from(0x1); let network = Ipv6AddrNetwork::try_new(ip, 120).unwrap(); assert_eq!(Ok(network), "::1/120".parse()); assert_eq!(Ok(network), "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse()); let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFF00); let networkb = Ipv6AddrNetwork::try_new_with_addr(ip, netmask).unwrap(); assert_eq!(networkb, network); assert_eq!(Ok(networkb), "::1/120".parse()); assert_eq!(Ok(networkb), "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse());
Implementations
Returns an IPv4 network.
If the netmask is not valid return an NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)
.
pub fn try_new_with_addr(
ip: Ipv6Addr,
netmask: Ipv6Addr
) -> Result<Ipv6AddrNetwork, NetAddsError>
pub fn try_new_with_addr(
ip: Ipv6Addr,
netmask: Ipv6Addr
) -> Result<Ipv6AddrNetwork, NetAddsError>
Returns an IPv4 network.
If the netmask is not valid return an NetAddsError::InvalidNetmask(InvalidNetmaskError)
.
Returns all ip of the network including the network and the broadcast addr.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); assert_eq!(network.all(), vec![ Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3) ]);
Returns all hosts (exclude network & broadcast addr).
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); assert_eq!(network.hosts(), vec![ Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2) ]);
Returns the number of ip’s included in the network including the network and the broadcast addr.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 120).unwrap(); assert_eq!(network.size(), 256);
Returns true if the ip argument is included in the network, else returns false.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 64).unwrap(); assert!(network.has(Ipv6Addr::from(0x1))); assert!(network.has(Ipv6Addr::from(0xA))); assert!(network.has(Ipv6Addr::from(0x00FF))); assert!(!network.has(Ipv6Addr::from(0xFFFFFFFFFFFFFFFFFF00000000000000)));
Check the validity of a netmask under Ipv6Addr representation.
If the netmask is not valid return an NetAddsError::InvalidNetmask(InvalidNetmaskError)
.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let netmask = Ipv6Addr::from(0xFFFFFFFF000000000000000000000000); assert_eq!(Ipv6AddrNetwork::validate_netmask(u128::from(netmask)), Ok(u128::from(netmask))); let netmask = Ipv6Addr::from(0x0000000000000000000000000000FFFF); assert!(Ipv6AddrNetwork::validate_netmask(u128::from(netmask)).is_err());
Check the validity of a netmask under CIDR prefix representation.
If the netmask prefix is not valid return an NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)
.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; assert_eq!(Ipv6AddrNetwork::validate_prefix(128), Ok(128)); assert!(Ipv6AddrNetwork::validate_prefix(129).is_err());
Returns the Ipv6Addr representation of a CIDR prefix.
If the netmask prefix is not valid return an NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)
.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; assert_eq!(Ipv6AddrNetwork::prefix_to_ip(128), Ok(u128::MAX)); assert!(Ipv6AddrNetwork::prefix_to_ip(129).is_err());
Returns the CIDR prefix representation of an Ipv6Addr.
We count the bit that are equals to 0 by shifting the sequence to the right (bitwise >>). Then we subtract the number of bits equal to 0 from the maximum number of bits available on the netmask.
If the netmask IPv6 is not valid return an NetAddsError::InvalidNetmask(InvalidNetmaskError)
.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let netmask = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0); assert_eq!(Ipv6AddrNetwork::ip_to_prefix(u128::from(netmask)), Ok(0)); let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF); assert_eq!(Ipv6AddrNetwork::ip_to_prefix(u128::from(netmask)), Ok(128)); let netmask = Ipv6Addr::new(0x0, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF); assert!(Ipv6AddrNetwork::ip_to_prefix(u128::from(netmask)).is_err());
Trait Implementations
Create an IpAddrNetwork::V6
from an Ipv6AddrNetwork
.
Examples:
use std::net::Ipv6Addr; use net_adds::{IpAddrNetwork, Ipv6AddrNetwork}; let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 120).unwrap(); assert_eq!(IpAddrNetwork::from(network), IpAddrNetwork::V6(network));
Parse a string as Ipv6AddrNetwork
.
If the string representation is not valid return an NetAddsError::NetworkAddrParse(NetworkAddrParseError)
.
Examples:
use std::convert::TryFrom; use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let network = Ipv6AddrNetwork::try_from((Ipv6Addr::from(1), 126)).expect("invalid network"); assert_eq!("::1/126".parse(), Ok(network)); assert_eq!("::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc".parse(), Ok(network));
type Err = NetAddsError
type Err = NetAddsError
The associated error which can be returned from parsing.
Create a Ipv6Addr
iterator. The iterator include the network and the broadcast.
Examples:
use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let mut iter = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126) .expect("invalid network") .into_iter(); assert_eq!(iter.next(), Some(Ipv6Addr::from(0))); assert_eq!(iter.next(), Some(Ipv6Addr::from(1))); assert_eq!(iter.next(), Some(Ipv6Addr::from(2))); assert_eq!(iter.next(), Some(Ipv6Addr::from(3))); assert_eq!(iter.next(), None);
Create a smart Ipv6Addr
iterator. The iterator include the network and the broadcast.
Examples:
use std::net::Ipv6Addr; use net_adds::{Ipv6AddrNetwork, IntoSmartIterator}; let mut iter = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126) .expect("invalid network") .into_smart_iter(); assert_eq!(iter.next(), Some(Ipv6Addr::from(0))); assert_eq!(iter.next(), Some(Ipv6Addr::from(1))); assert_eq!(iter.next(), Some(Ipv6Addr::from(2))); assert_eq!(iter.next(), Some(Ipv6Addr::from(3))); assert_eq!(iter.next(), None);
Which kind of iterator are we turning this into?
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
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
Create an Ipv6AddrNetwork
from a tuple of two Ipv6Addr
.
Examples:
use std::convert::TryFrom; use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let ip = Ipv6Addr::from(1); let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFF00); assert_eq!(Ipv6AddrNetwork::try_from((ip, netmask)), Ipv6AddrNetwork::try_new_with_addr(ip, netmask)); let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x2002); assert!(Ipv6AddrNetwork::try_from((ip, netmask)).is_err());
type Error = NetAddsError
type Error = NetAddsError
The type returned in the event of a conversion error.
Create an Ipv6AddrNetwork
from a tuple of two slots, Ipv6Addr
and u8
.
If the netmask prefix is not valid return an NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)
.
Examples:
use std::convert::TryFrom; use std::net::Ipv6Addr; use net_adds::Ipv6AddrNetwork; let ip = Ipv6Addr::from(0x1); assert_eq!(Ipv6AddrNetwork::try_from((ip, 92)), Ipv6AddrNetwork::try_new(ip, 92)); assert!(Ipv6AddrNetwork::try_from((ip, 129)).is_err());
type Error = NetAddsError
type Error = NetAddsError
The type returned in the event of a conversion error.
Auto Trait Implementations
impl RefUnwindSafe for Ipv6AddrNetwork
impl Send for Ipv6AddrNetwork
impl Sync for Ipv6AddrNetwork
impl Unpin for Ipv6AddrNetwork
impl UnwindSafe for Ipv6AddrNetwork
Blanket Implementations
Mutably borrows from an owned value. Read more