Struct net_adds::Ipv4AddrNetwork [−][src]
pub struct Ipv4AddrNetwork { /* fields omitted */ }
Expand description
An IPv4 address network.
See crate::IpAddrNetwork
for a type encompassing both IPv4 and IPv6 network.
The size of an Ipv4AddrNetwork
struct may vary depending on the target operating
system.
Textual representation
Ipv4AddrNetwork
provides a FromStr
implementation. The two parts are divided by /
.
The first part must contain an IPv4. The second part can either contain an IPv4 or an u8
between 0 and 32 which is valid as a netmask prefix.
Examples
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let ip = Ipv4Addr::new(192, 168, 0, 10); let network = Ipv4AddrNetwork::try_new(ip, 24).unwrap(); assert_eq!(Ok(network), "192.168.0.10/255.255.255.0".parse()); assert_eq!(Ok(network), "192.168.0.10/24".parse()); let netmask = Ipv4Addr::new(255, 255, 255, 0); let networkb = Ipv4AddrNetwork::try_new_with_addr(ip, netmask).unwrap(); assert_eq!(networkb, network); assert_eq!(Ok(networkb), "192.168.0.10/255.255.255.0".parse()); assert_eq!(Ok(networkb), "192.168.0.10/24".parse());
Implementations
Returns an IPv4 network.
If the netmask is not valid return an NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)
.
pub fn try_new_with_addr(
ip: Ipv4Addr,
netmask: Ipv4Addr
) -> Result<Ipv4AddrNetwork, NetAddsError>
pub fn try_new_with_addr(
ip: Ipv4Addr,
netmask: Ipv4Addr
) -> Result<Ipv4AddrNetwork, 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::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 30).unwrap(); assert_eq!(network.all(), vec![ Ipv4Addr::new(192, 168, 0, 8), Ipv4Addr::new(192, 168, 0, 9), Ipv4Addr::new(192, 168, 0, 10), Ipv4Addr::new(192, 168, 0, 11) ]);
Returns all hosts (exclude network & broadcast addr).
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 30).unwrap(); assert_eq!(network.hosts(), vec![ Ipv4Addr::new(192, 168, 0, 9), Ipv4Addr::new(192, 168, 0, 10) ]);
Returns the number of ip’s included in the network including the network and the broadcast addr.
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap(); assert_eq!(network.size(), 256);
Returns true if the ip argument is included in the network, else returns false.
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap(); assert!(network.has(Ipv4Addr::new(192, 168, 0, 0))); assert!(network.has(Ipv4Addr::new(192, 168, 0, 142))); assert!(network.has(Ipv4Addr::new(192, 168, 0, 255))); assert!(!network.has(Ipv4Addr::new(192, 169, 0, 0)));
Check the validity of a netmask under Ipv4Addr representation.
If the netmask is not valid return an NetAddsError::InvalidNetmask(InvalidNetmaskError)
.
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let netmask = Ipv4Addr::new(255, 255, 255, 0); assert_eq!(Ipv4AddrNetwork::validate_netmask(u32::from(netmask)), Ok(u32::from(netmask))); let netmask = Ipv4Addr::new(0, 0, 0, 255); assert!(Ipv4AddrNetwork::validate_netmask(u32::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::Ipv4Addr; use net_adds::Ipv4AddrNetwork; assert_eq!(Ipv4AddrNetwork::validate_prefix(32), Ok(32)); assert!(Ipv4AddrNetwork::validate_prefix(33).is_err());
Returns the Ipv4Addr representation of a CIDR prefix.
If the netmask prefix is not valid return an NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)
.
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; assert_eq!(Ipv4AddrNetwork::prefix_to_ip(0), Ok(0)); assert_eq!(Ipv4AddrNetwork::prefix_to_ip(32), Ok(u32::MAX)); assert!(Ipv4AddrNetwork::prefix_to_ip(33).is_err());
Returns the CIDR prefix representation of an Ipv4Addr.
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 is not valid return an NetAddsError::InvalidNetmask(InvalidNetmaskError)
.
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; assert_eq!(Ipv4AddrNetwork::ip_to_prefix(u32::from(Ipv4Addr::new(0, 0, 0, 0))), Ok(0)); assert_eq!(Ipv4AddrNetwork::ip_to_prefix(u32::from(Ipv4Addr::new(255, 255, 255, 255))), Ok(32)); assert!(Ipv4AddrNetwork::ip_to_prefix(u32::from(Ipv4Addr::new(0, 255, 255, 255))).is_err());
Trait Implementations
Create an IpAddrNetwork::V4
from an Ipv4AddrNetwork
.
Examples:
use std::net::Ipv4Addr; use net_adds::{IpAddrNetwork, Ipv4AddrNetwork}; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24).unwrap(); assert_eq!(IpAddrNetwork::from(network), IpAddrNetwork::V4(network));
Parse a string as Ipv4AddrNetwork
.
If the string representation is not valid return an NetAddsErrorAddrParse(NetworkAddrParseError)
.
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let network = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 10), 24); assert_eq!("192.168.0.10/24".parse(), network); assert_eq!("192.168.0.10/255.255.255.0".parse(), network);
type Err = NetAddsError
type Err = NetAddsError
The associated error which can be returned from parsing.
Create a Ipv4Addr
iterator. The iterator include the network and the broadcast.
Examples:
use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let mut iter = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 0), 30) .expect("invalid network") .into_iter(); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 0))); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 1))); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 2))); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 3))); assert_eq!(iter.next(), None);
Create a smart Ipv4Addr
iterator. The iterator include the network and the broadcast.
Examples:
use std::net::Ipv4Addr; use net_adds::{Ipv4AddrNetwork, IntoSmartIterator}; let mut iter = Ipv4AddrNetwork::try_new(Ipv4Addr::new(192, 168, 0, 0), 30) .expect("invalid network") .into_smart_iter(); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 0))); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 1))); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 2))); assert_eq!(iter.next(), Some(Ipv4Addr::new(192, 168, 0, 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 Ipv4AddrNetwork
from a tuple of two Ipv4Addr
.
Examples:
use std::convert::TryFrom; use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let ip = Ipv4Addr::new(192, 168, 0, 10); let netmask = Ipv4Addr::new(255, 255, 255, 0); assert_eq!(Ipv4AddrNetwork::try_from((ip, netmask)), Ipv4AddrNetwork::try_new(ip, 24)); let netmask = Ipv4Addr::new(255, 255, 117, 0); assert!(Ipv4AddrNetwork::try_from((ip, netmask)).is_err());
type Error = NetAddsError
type Error = NetAddsError
The type returned in the event of a conversion error.
Create an Ipv4AddrNetwork
from a tuple of two slots, Ipv4Addr
and u8
.
If the netmask prefix is not valid return an NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)
.
Examples:
use std::convert::TryFrom; use std::net::Ipv4Addr; use net_adds::Ipv4AddrNetwork; let ip = Ipv4Addr::new(192, 168, 0, 10); assert_eq!(Ipv4AddrNetwork::try_from((ip, 24)), Ipv4AddrNetwork::try_new(ip, 24)); assert!(Ipv4AddrNetwork::try_from((ip, 33)).is_err());
type Error = NetAddsError
type Error = NetAddsError
The type returned in the event of a conversion error.
Auto Trait Implementations
impl RefUnwindSafe for Ipv4AddrNetwork
impl Send for Ipv4AddrNetwork
impl Sync for Ipv4AddrNetwork
impl Unpin for Ipv4AddrNetwork
impl UnwindSafe for Ipv4AddrNetwork
Blanket Implementations
Mutably borrows from an owned value. Read more