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
V4(Ipv4Net)
V6(Ipv6Net)
Methods
impl IpNet
[src]
fn addr(&self) -> IpAddr
[src]
Return the address.
fn prefix_len(&self) -> u8
[src]
Return the prefix length.
fn netmask(&self) -> IpAddr
[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());
fn hostmask(&self) -> IpAddr
[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());
fn network(&self) -> IpAddr
[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());
fn broadcast(&self) -> IpAddr
[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());
fn trunc(&self) -> IpNet
[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() );
fn supernet(&self) -> Option<IpNet>
[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);
fn is_sibling(&self, other: &IpNet) -> bool
[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));
fn hosts(&self) -> IpAddrIter<IpAddr>
[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(), ]);
fn subnets(&self, new_prefix_len: u8) -> Result<Subnets<IpAddr>, PrefixLenError>
[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));
fn aggregate(networks: &Vec<IpNet>) -> Vec<IpNet>
[src]
Aggregate a Vec
of IpNet
s 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]
fn clone(&self) -> IpNet
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Eq for IpNet
[src]
impl PartialEq for IpNet
[src]
fn eq(&self, __arg_0: &IpNet) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &IpNet) -> bool
[src]
This method tests for !=
.
impl Ord for IpNet
[src]
fn cmp(&self, __arg_0: &IpNet) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
[src]
ord_max_min
)Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
[src]
ord_max_min
)Compares and returns the minimum of two values. Read more
impl PartialOrd for IpNet
[src]
fn partial_cmp(&self, __arg_0: &IpNet) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &IpNet) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &IpNet) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &IpNet) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &IpNet) -> bool
[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]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl Debug for IpNet
[src]
impl Display for IpNet
[src]
fn fmt(&self, fmt: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl From<Ipv4Net> for IpNet
[src]
impl From<Ipv6Net> for IpNet
[src]
impl<'a> Contains<&'a IpNet> for IpNet
[src]
fn contains(&self, other: &IpNet) -> bool
[src]
Returns true
if this network contains the given network or address. Read more
impl<'a> Contains<&'a IpAddr> for IpNet
[src]
fn contains(&self, other: &IpAddr) -> bool
[src]
Returns true
if this network contains the given network or address. Read more
impl FromStr for IpNet
[src]
type Err = AddrParseError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<IpNet, AddrParseError>
[src]
Parses a string s
to return a value of this type. Read more