Struct ip_network::Ipv4Network

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

IPv4 Network.

Implementations§

source§

impl Ipv4Network

source

pub const LENGTH: u8 = 32u8

IPv4 address length in bits.

source

pub const DEFAULT_ROUTE: Self = _

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

source

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);
source

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);
source

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));
source

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));
source

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);
source

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));
source

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)));
source

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));
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::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)?));
source

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)?);
source

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)?);
source

pub fn is_default_route(&self) -> bool

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

§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;

assert!(Ipv4Network::new(Ipv4Addr::new(0, 0, 0, 0), 0)?.is_default_route());
source

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());
source

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());
source

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());
source

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());
source

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());
source

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());
source

pub fn is_shared_address_space(&self) -> bool

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());

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());
source

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());
source

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());
source

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());
source

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());
source

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());
source

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]);
source

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]);
source

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

source§

fn clone(&self) -> Ipv4Network

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 Ipv4Network

source§

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

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

impl Display for Ipv4Network

source§

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

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

§Examples
use std::net::Ipv4Addr;
use ip_network::Ipv4Network;

assert_eq!(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?.to_string(), "192.168.1.0/24");
source§

impl From<Ipv4Addr> for Ipv4Network

source§

fn from(ip: Ipv4Addr) -> Self

Converts Ipv4Addr to Ipv4Network with netmask 32.

source§

impl From<Ipv4Network> for IpNetwork

source§

fn from(network: Ipv4Network) -> Self

Converts to this type from the input type.
source§

impl FromStr for Ipv4Network

source§

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

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

impl Hash for Ipv4Network

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 IntoIterator for Ipv4Network

source§

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 Item = Ipv4Addr

The type of the elements being iterated over.
§

type IntoIter = Ipv4RangeIterator

Which kind of iterator are we turning this into?
source§

impl Ord for Ipv4Network

source§

fn cmp(&self, other: &Ipv4Network) -> 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 Ipv4Network

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<Ipv4Network> for IpNetwork

source§

fn eq(&self, other: &Ipv4Network) -> 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 Ipv4Network

source§

fn eq(&self, other: &Ipv4Network) -> 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 Ipv4Network

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<Ipv4Network> for IpNetwork

source§

fn partial_cmp(&self, other: &Ipv4Network) -> 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 Ipv4Network

source§

fn partial_cmp(&self, other: &Ipv4Network) -> 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 Ipv4Network

source§

impl Eq for Ipv4Network

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.