Struct ip_network::Ipv4Network
source · pub struct Ipv4Network { /* private fields */ }
Expand description
IPv4 Network.
Implementations§
source§impl Ipv4Network
impl Ipv4Network
sourcepub const DEFAULT_ROUTE: Self = _
pub const DEFAULT_ROUTE: Self = _
Default route that contains all IP addresses, IP network 0.0.0.0/0
sourcepub fn new(
network_address: Ipv4Addr,
netmask: u8,
) -> Result<Self, IpNetworkError>
pub fn new( network_address: Ipv4Addr, netmask: u8, ) -> Result<Self, IpNetworkError>
Constructs new Ipv4Network
based on Ipv4Addr
and netmask
.
Returns error if netmask is bigger than 32 or if host bits are set in network_address
.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.network_address(), Ipv4Addr::new(192, 168, 1, 0));
assert_eq!(ip_network.netmask(), 24);
sourcepub fn new_truncate(
network_address: Ipv4Addr,
netmask: u8,
) -> Result<Self, IpNetworkError>
pub fn new_truncate( network_address: Ipv4Addr, netmask: u8, ) -> Result<Self, IpNetworkError>
Constructs new Ipv4Network
based on Ipv4Addr
and netmask
with truncating host bits
from given network_address
.
Returns error if netmask is bigger than 32.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new_truncate(Ipv4Addr::new(192, 168, 1, 100), 24)?;
assert_eq!(ip_network.network_address(), Ipv4Addr::new(192, 168, 1, 0));
assert_eq!(ip_network.netmask(), 24);
sourcepub fn network_address(&self) -> Ipv4Addr
pub fn network_address(&self) -> Ipv4Addr
Returns network IP address (first address in range).
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.network_address(), Ipv4Addr::new(192, 168, 1, 0));
sourcepub fn broadcast_address(&self) -> Ipv4Addr
pub fn broadcast_address(&self) -> Ipv4Addr
Returns broadcast address of network (last address in range).
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.broadcast_address(), Ipv4Addr::new(192, 168, 1, 255));
sourcepub fn netmask(&self) -> u8
pub fn netmask(&self) -> u8
Returns network mask as integer.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.netmask(), 24);
sourcepub fn full_netmask(&self) -> Ipv4Addr
pub fn full_netmask(&self) -> Ipv4Addr
Returns network mask as IPv4 address.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.full_netmask(), Ipv4Addr::new(255, 255, 255, 0));
sourcepub fn contains(&self, ip: Ipv4Addr) -> bool
pub fn contains(&self, ip: Ipv4Addr) -> bool
Returns true
if given IPv4Addr
is inside this network.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert!(ip_network.contains(Ipv4Addr::new(192, 168, 1, 2)));
assert!(!ip_network.contains(Ipv4Addr::new(192, 168, 2, 2)));
sourcepub fn hosts(&self) -> impl ExactSizeIterator<Item = Ipv4Addr>
pub fn hosts(&self) -> impl ExactSizeIterator<Item = Ipv4Addr>
Returns iterator over host IP addresses in range (without network and broadcast address). You
can also use this method to check how much hosts address are in range by calling len()
method
on iterator (see Examples).
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip = Ipv4Addr::new(192, 168, 1, 0);
let mut hosts = Ipv4Network::new(ip, 24)?.hosts();
assert_eq!(254, hosts.len());
assert_eq!(hosts.next().unwrap(), Ipv4Addr::new(192, 168, 1, 1));
assert_eq!(hosts.last().unwrap(), Ipv4Addr::new(192, 168, 1, 254));
sourcepub fn supernet(&self) -> Option<Self>
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::Ipv4Addr;
use ip_network::Ipv4Network;
let ip = Ipv4Addr::new(192, 168, 1, 0);
let mut hosts = Ipv4Network::new(ip, 24)?;
assert_eq!(hosts.supernet(), Some(Ipv4Network::new(Ipv4Addr::new(192, 168, 0, 0), 23)?));
sourcepub fn subnets(&self) -> impl ExactSizeIterator<Item = Ipv4Network>
pub fn subnets(&self) -> impl ExactSizeIterator<Item = Ipv4Network>
Returns iterator over networks with bigger netmask by one. If netmask is already 32, iterator is empty.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
let mut iterator = ip_network.subnets();
assert_eq!(iterator.next().unwrap(), Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 25)?);
assert_eq!(iterator.last().unwrap(), Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 128), 25)?);
sourcepub fn subnets_with_prefix(
&self,
prefix: u8,
) -> impl ExactSizeIterator<Item = Ipv4Network>
pub fn subnets_with_prefix( &self, prefix: u8, ) -> impl ExactSizeIterator<Item = Ipv4Network>
Returns Ipv4NetworkIterator
over networks with defined netmask.
§Panics
This method panics when prefix is bigger than 32 or when prefix is lower or equal than netmask.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip = Ipv4Addr::new(192, 168, 1, 0);
let mut iterator = Ipv4Network::new(ip, 24)?.subnets_with_prefix(25);
assert_eq!(iterator.next().unwrap(), Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 25)?);
assert_eq!(iterator.last().unwrap(), Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 128), 25)?);
sourcepub fn is_default_route(&self) -> bool
pub fn is_default_route(&self) -> bool
sourcepub fn is_local_identification(&self) -> bool
pub fn is_local_identification(&self) -> bool
Returns true
for network in local identification range (0.0.0.0/8).
This property is defined by IETF RFC 1122.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(0, 0, 0, 0), 8)?.is_local_identification());
sourcepub fn is_unspecified(&self) -> bool
pub fn is_unspecified(&self) -> bool
Returns true
for the special ‘unspecified’ network (0.0.0.0/32).
This property is defined in UNIX Network Programming, Second Edition, W. Richard Stevens, p. 891; see also ip7.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(0, 0, 0, 0), 32)?.is_unspecified());
sourcepub fn is_loopback(&self) -> bool
pub fn is_loopback(&self) -> bool
Returns true
if this network is inside loopback address range (127.0.0.0/8).
This property is defined by IETF RFC 1122.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 0), 8)?.is_loopback());
sourcepub fn is_broadcast(&self) -> bool
pub fn is_broadcast(&self) -> bool
Returns true
if this is a broadcast network (255.255.255.255/32).
A broadcast address has all octets set to 255 as defined in IETF RFC 919.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(255, 255, 255, 255), 32)?.is_broadcast());
sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Returns true
if this whole network range is inside private address ranges.
The private address ranges are defined in IETF RFC 1918 and include:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?.is_private());
sourcepub fn is_ietf_protocol_assignments(&self) -> bool
pub fn is_ietf_protocol_assignments(&self) -> bool
Returns true
if this whole network is inside IETF Protocol Assignments range (192.0.0.0/24).
This property is defined by IETF RFC 6890, Section 2.1.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(192, 0, 0, 0), 24)?.is_ietf_protocol_assignments());
Returns true
if this whole network is inside Shared Address Space (100.64.0.0/10).
This property is defined by IETF RFC 6598.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(100, 64, 0, 0), 10)?.is_shared_address_space());
sourcepub fn is_link_local(&self) -> bool
pub fn is_link_local(&self) -> bool
Returns true
if the network is is inside link-local range (169.254.0.0/16).
This property is defined by IETF RFC 3927.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(169, 254, 1, 0), 24)?.is_link_local());
sourcepub fn is_multicast(&self) -> bool
pub fn is_multicast(&self) -> bool
Returns true
if this whole network is inside multicast address range (224.0.0.0/4).
Multicast network addresses have a most significant octet between 224 and 239, and is defined by IETF RFC 5771.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(224, 168, 1, 0), 24)?.is_multicast());
sourcepub fn is_benchmarking(&self) -> bool
pub fn is_benchmarking(&self) -> bool
Returns true
if this whole network is inside benchmarking address range (198.18.0.0/15).
This property is defined by IETF RFC 2544.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(198, 19, 1, 0), 24)?.is_benchmarking());
sourcepub fn is_reserved(&self) -> bool
pub fn is_reserved(&self) -> bool
Returns true
if this whole network is inside reserved address range (240.0.0.0/4), except
broadcast address (255.255.255.255/32).
Reserved network addresses have a most significant octet between 240 and 255, and is defined by IETF RFC 1112.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(240, 168, 1, 0), 24)?.is_reserved());
assert!(!Ipv4Network::new(Ipv4Addr::new(255, 255, 255, 255), 32)?.is_reserved());
sourcepub fn is_documentation(&self) -> bool
pub fn is_documentation(&self) -> bool
Returns true
if this network is in a range designated for documentation.
This is defined in IETF RFC 5737:
- 192.0.2.0/24 (TEST-NET-1)
- 198.51.100.0/24 (TEST-NET-2)
- 203.0.113.0/24 (TEST-NET-3)
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(Ipv4Network::new(Ipv4Addr::new(192, 0, 2, 0), 24)?.is_documentation());
sourcepub fn is_global(&self) -> bool
pub fn is_global(&self) -> bool
Returns true
if the network appears to be globally routable.
See IANA IPv4 Special-Purpose Address Registry.
The following return false
:
- local identification (0.0.0.0/8)
- private address (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16)
- Shared Address Space (100.64.0.0/10)
- the loopback address (127.0.0.0/8)
- the link-local address (169.254.0.0/16)
- IETF Protocol Assignments (192.0.0.0/24, except 192.0.0.9/32 and 192.0.0.10/32)
- the broadcast address (255.255.255.255/32)
- test addresses used for documentation (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24)
- benchmarking (198.18.0.0/15)
- reserved range (240.0.0.0/4)
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
assert!(!Ipv4Network::new(Ipv4Addr::new(10, 254, 0, 0), 16)?.is_global());
assert!(!Ipv4Network::new(Ipv4Addr::new(192, 168, 10, 65), 32)?.is_global());
assert!(!Ipv4Network::new(Ipv4Addr::new(172, 16, 10, 65), 32)?.is_global());
assert!(!Ipv4Network::new(Ipv4Addr::new(0, 0, 0, 0), 32)?.is_global());
assert!(Ipv4Network::new(Ipv4Addr::new(80, 9, 12, 3), 32)?.is_global());
sourcepub fn summarize_address_range(first: Ipv4Addr, last: Ipv4Addr) -> Vec<Self>
pub fn summarize_address_range(first: Ipv4Addr, last: Ipv4Addr) -> Vec<Self>
Return a vector of the summarized network range given the first and last IPv4 addresses.
Implementation of this method was inspired by Python ipaddress.summarize_address_range
method. If first IP address is bigger than last, empty vector is returned.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ranges = Ipv4Network::summarize_address_range(
Ipv4Addr::new(10, 254, 0, 0),
Ipv4Addr::new(10, 255, 255, 255),
);
assert_eq!(Ipv4Network::new(Ipv4Addr::new(10, 254, 0, 0), 15)?, ranges[0]);
sourcepub fn collapse_addresses(addresses: &[Self]) -> Vec<Self>
pub fn collapse_addresses(addresses: &[Self]) -> Vec<Self>
Return an iterator of the collapsed Ipv4Networks.
Implementation of this method was inspired by Python ipaddress.collapse_addresses
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let collapsed = Ipv4Network::collapse_addresses(&[
Ipv4Network::new(Ipv4Addr::new(192, 0, 2, 0), 25)?,
Ipv4Network::new(Ipv4Addr::new(192, 0, 2, 128), 25)?,
]);
assert_eq!(Ipv4Network::new(Ipv4Addr::new(192, 0, 2, 0), 24)?, collapsed[0]);
sourcepub fn from_str_truncate(s: &str) -> Result<Self, IpNetworkParseError>
pub fn from_str_truncate(s: &str) -> Result<Self, IpNetworkParseError>
Converts string in format X.X.X.X/Y (CIDR notation) to Ipv4Network
, but truncating host bits.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip_network = Ipv4Network::from_str_truncate("192.168.1.255/24")?;
assert_eq!(ip_network.network_address(), Ipv4Addr::new(192, 168, 1, 0));
assert_eq!(ip_network.netmask(), 24);
Trait Implementations§
source§impl Clone for Ipv4Network
impl Clone for Ipv4Network
source§fn clone(&self) -> Ipv4Network
fn clone(&self) -> Ipv4Network
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Ipv4Network
impl Debug for Ipv4Network
source§impl Display for Ipv4Network
impl Display for Ipv4Network
source§impl From<Ipv4Addr> for Ipv4Network
impl From<Ipv4Addr> for Ipv4Network
source§impl From<Ipv4Network> for IpNetwork
impl From<Ipv4Network> for IpNetwork
source§fn from(network: Ipv4Network) -> Self
fn from(network: Ipv4Network) -> Self
source§impl FromStr for Ipv4Network
impl FromStr for Ipv4Network
source§fn from_str(s: &str) -> Result<Ipv4Network, IpNetworkParseError>
fn from_str(s: &str) -> Result<Ipv4Network, IpNetworkParseError>
Converts string in format X.X.X.X/Y (CIDR notation) to Ipv4Network
.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
use std::str::FromStr;
let ip_network = Ipv4Network::from_str("192.168.1.0/24")?;
assert_eq!(ip_network.network_address(), Ipv4Addr::new(192, 168, 1, 0));
assert_eq!(ip_network.netmask(), 24);
§type Err = IpNetworkParseError
type Err = IpNetworkParseError
source§impl Hash for Ipv4Network
impl Hash for Ipv4Network
source§impl IntoIterator for Ipv4Network
impl IntoIterator for Ipv4Network
source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Returns iterator over all IP addresses in range including network and broadcast addresses.
§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;
let ip = Ipv4Addr::new(192, 168, 1, 0);
let mut iter = Ipv4Network::new(ip, 24)?.into_iter();
assert_eq!(iter.next().unwrap(), Ipv4Addr::new(192, 168, 1, 0));
assert_eq!(iter.next().unwrap(), Ipv4Addr::new(192, 168, 1, 1));
assert_eq!(iter.last().unwrap(), Ipv4Addr::new(192, 168, 1, 255));
§type IntoIter = Ipv4RangeIterator
type IntoIter = Ipv4RangeIterator
source§impl Ord for Ipv4Network
impl Ord for Ipv4Network
source§fn cmp(&self, other: &Ipv4Network) -> Ordering
fn cmp(&self, other: &Ipv4Network) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq<IpNetwork> for Ipv4Network
impl PartialEq<IpNetwork> for Ipv4Network
source§impl PartialEq<Ipv4Network> for IpNetwork
impl PartialEq<Ipv4Network> for IpNetwork
source§fn eq(&self, other: &Ipv4Network) -> bool
fn eq(&self, other: &Ipv4Network) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq for Ipv4Network
impl PartialEq for Ipv4Network
source§fn eq(&self, other: &Ipv4Network) -> bool
fn eq(&self, other: &Ipv4Network) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<IpNetwork> for Ipv4Network
impl PartialOrd<IpNetwork> for Ipv4Network
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Ipv4Network> for IpNetwork
impl PartialOrd<Ipv4Network> for IpNetwork
source§fn partial_cmp(&self, other: &Ipv4Network) -> Option<Ordering>
fn partial_cmp(&self, other: &Ipv4Network) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd for Ipv4Network
impl PartialOrd for Ipv4Network
source§fn partial_cmp(&self, other: &Ipv4Network) -> Option<Ordering>
fn partial_cmp(&self, other: &Ipv4Network) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Copy for Ipv4Network
impl Eq for Ipv4Network
Auto Trait Implementations§
impl Freeze for Ipv4Network
impl RefUnwindSafe for Ipv4Network
impl Send for Ipv4Network
impl Sync for Ipv4Network
impl Unpin for Ipv4Network
impl UnwindSafe for Ipv4Network
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)