[][src]Struct ip_network::Ipv4Network

pub struct Ipv4Network { /* fields omitted */ }

IPv4 Network.

Methods

impl Ipv4Network[src]

pub const LENGTH: u8[src]

IPv4 address length in bits.

pub fn new(
    network_address: Ipv4Addr,
    netmask: u8
) -> Result<Self, IpNetworkError>
[src]

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

pub fn new_truncate(
    network_address: Ipv4Addr,
    netmask: u8
) -> Result<Self, IpNetworkError>
[src]

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

pub fn network_address(&self) -> Ipv4Addr[src]

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

pub fn broadcast_address(&self) -> Ipv4Addr[src]

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

pub fn netmask(&self) -> u8[src]

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

pub fn full_netmask(&self) -> Ipv4Addr[src]

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

pub fn contains(&self, ip: Ipv4Addr) -> bool[src]

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

pub fn hosts(&self) -> impl ExactSizeIterator<Item = Ipv4Addr>[src]

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

pub fn supernet(&self) -> Option<Self>[src]

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

pub fn subnets(&self) -> impl ExactSizeIterator<Item = Ipv4Network>[src]

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

pub fn subnets_with_prefix(
    &self,
    prefix: u8
) -> impl ExactSizeIterator<Item = Ipv4Network>
[src]

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

pub fn is_default_route(&self) -> bool[src]

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

pub fn is_local_identification(&self) -> bool[src]

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

pub fn is_unspecified(&self) -> bool[src]

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

pub fn is_loopback(&self) -> bool[src]

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

pub fn is_broadcast(&self) -> bool[src]

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

pub fn is_private(&self) -> bool[src]

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

pub fn is_ietf_protocol_assignments(&self) -> bool[src]

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

pub fn is_shared_address_space(&self) -> bool[src]

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

pub fn is_multicast(&self) -> bool[src]

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

pub fn is_benchmarking(&self) -> bool[src]

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

pub fn is_reserved(&self) -> bool[src]

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

pub fn is_documentation(&self) -> bool[src]

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

pub fn is_global(&self) -> bool[src]

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

pub fn summarize_address_range(first: Ipv4Addr, last: Ipv4Addr) -> Vec<Self>[src]

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

Trait Implementations

impl Copy for Ipv4Network[src]

impl PartialEq<Ipv4Network> for IpNetwork[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<IpNetwork> for Ipv4Network[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Ipv4Network> for Ipv4Network[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl Clone for Ipv4Network[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl Ord for Ipv4Network[src]

fn max(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl From<Ipv4Network> for IpNetwork[src]

impl From<Ipv4Addr> for Ipv4Network[src]

fn from(ip: Ipv4Addr) -> Self[src]

Converts Ipv4Addr to Ipv4Network with netmask 32.

impl IntoIterator for Ipv4Network[src]

type Item = Ipv4Addr

The type of the elements being iterated over.

type IntoIter = Ipv4RangeIterator

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

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

impl Eq for Ipv4Network[src]

impl PartialOrd<Ipv4Network> for IpNetwork[src]

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialOrd<IpNetwork> for Ipv4Network[src]

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialOrd<Ipv4Network> for Ipv4Network[src]

impl Display for Ipv4Network[src]

fn fmt(&self, fmt: &mut Formatter) -> Result[src]

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

impl Debug for Ipv4Network[src]

impl Hash for Ipv4Network[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl FromStr for Ipv4Network[src]

type Err = IpNetworkParseError

The associated error which can be returned from parsing.

fn from_str(s: &str) -> Result<Ipv4Network, IpNetworkParseError>[src]

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

impl<'expr> AsExpression<Cidr> for &'expr Ipv4Network[src]

type Expression = Bound<Cidr, Self>

The expression being returned

impl<'expr> AsExpression<Nullable<Cidr>> for &'expr Ipv4Network[src]

type Expression = Bound<Nullable<Cidr>, Self>

The expression being returned

impl AsExpression<Cidr> for Ipv4Network[src]

type Expression = Bound<Cidr, Self>

The expression being returned

impl AsExpression<Nullable<Cidr>> for Ipv4Network[src]

type Expression = Bound<Nullable<Cidr>, Self>

The expression being returned

impl<__ST, __DB> Queryable<__ST, __DB> for Ipv4Network where
    __DB: Backend,
    Self: FromSql<__ST, __DB>, 
[src]

type Row = Self

The Rust type you'd like to map from. Read more

impl ToSql<Cidr, Pg> for Ipv4Network[src]

impl<__DB> ToSql<Nullable<Cidr>, __DB> for Ipv4Network where
    __DB: Backend,
    Self: ToSql<Cidr, __DB>, 
[src]

impl FromSql<Cidr, Pg> for Ipv4Network[src]

impl<__ST, __DB> FromSqlRow<__ST, __DB> for Ipv4Network where
    __DB: Backend,
    Self: FromSql<__ST, __DB>, 
[src]

const FIELDS_NEEDED: usize[src]

The number of fields that this type will consume. Must be equal to the number of times you would call row.take() in build_from_row Read more

impl ToSql for Ipv4Network[src]

impl FromSql for Ipv4Network[src]

fn from_sql_null(
    ty: &Type
) -> Result<Self, Box<dyn Error + 'static + Sync + Send>>

Creates a new value of this type from a NULL SQL value. Read more

fn from_sql_nullable(
    ty: &Type,
    raw: Option<&[u8]>
) -> Result<Self, Box<dyn Error + 'static + Sync + Send>>

A convenience function that delegates to from_sql and from_sql_null depending on the value of raw. Read more

impl Serialize for Ipv4Network[src]

impl<'de> Deserialize<'de> for Ipv4Network[src]

Auto Trait Implementations

impl Send for Ipv4Network

impl Sync for Ipv4Network

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From for T[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> IntoSql for T[src]

fn into_sql<T>(self) -> Self::Expression where
    Self: AsExpression<T>, 
[src]

Convert self to an expression for Diesel's query builder. Read more

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
    &'a Self: AsExpression<T>, 
[src]

Convert &self to an expression for Diesel's query builder. Read more

impl<T> Same for T

type Output = T

Should always be Self

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]