Struct ip_network::Ipv4Network[][src]

pub struct Ipv4Network { /* fields omitted */ }
Expand description

IPv4 Network.

Implementations

IPv4 address length in bits.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

The expression being returned

Perform the conversion

The expression being returned

Perform the conversion

The expression being returned

Perform the conversion

The expression being returned

Perform the conversion

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

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

Converts Ipv4Addr to Ipv4Network with netmask 32.

Performs the conversion.

Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more

Determines if a value of this type can be created from the specified Postgres Type. Read more

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

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

See the trait documentation.

See the trait documentation.

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

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

The associated error which can be returned from parsing.

Feeds this value into the given Hasher. Read more

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

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

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

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

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

Construct an instance of this type

Serialize this value into the given Serde serializer. Read more

Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more

Determines if a value of this type can be converted to the specified Postgres Type. Read more

An adaptor method used internally by Rust-Postgres. Read more

See the trait documentation.

See the trait documentation.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns a reference to self as a ToSql trait object.

Performs the conversion.

Performs the conversion.

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

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

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

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

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.