Struct ip_network::Ipv4Network [−][src]
pub struct Ipv4Network { /* fields omitted */ }
Expand description
IPv4 Network.
Implementations
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 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
type Expression = Bound<Cidr, Self>
type Expression = Bound<Cidr, Self>
The expression being returned
Perform the conversion
type Expression = Bound<Cidr, Self>
type Expression = Bound<Cidr, Self>
The expression being returned
Perform the conversion
type Expression = Bound<Nullable<Cidr>, Self>
type Expression = Bound<Nullable<Cidr>, Self>
The expression being returned
Perform the conversion
type Expression = Bound<Nullable<Cidr>, Self>
type Expression = Bound<Nullable<Cidr>, Self>
The expression being returned
Perform the conversion
Deserialize this value from the given Serde deserializer. Read more
Performs the conversion.
impl<__ST, __DB> FromSqlRow<__ST, __DB> for Ipv4Network where
__DB: Backend,
Self: FromSql<__ST, __DB>,
impl<__ST, __DB> FromSqlRow<__ST, __DB> for Ipv4Network where
__DB: Backend,
Self: FromSql<__ST, __DB>,
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);
type Err = IpNetworkParseError
type Err = IpNetworkParseError
The associated error which can be returned from parsing.
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
Which kind of iterator are we turning this into?
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 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 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
impl<__ST, __DB> Queryable<__ST, __DB> for Ipv4Network where
__DB: Backend,
Self: FromSql<__ST, __DB>,
impl<__ST, __DB> Queryable<__ST, __DB> for Ipv4Network where
__DB: Backend,
Self: FromSql<__ST, __DB>,
Auto Trait Implementations
impl RefUnwindSafe for Ipv4Network
impl Send for Ipv4Network
impl Sync for Ipv4Network
impl Unpin for Ipv4Network
impl UnwindSafe for Ipv4Network
Blanket Implementations
Mutably borrows from an owned value. Read more
Returns a reference to self
as a ToSql
trait object.
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>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
&'a Self: AsExpression<T>,
Convert &self
to an expression for Diesel’s query builder. Read more