IPv4Network

Struct IPv4Network 

Source
pub struct IPv4Network { /* private fields */ }
Expand description

An Internet Protocol Version 4 Network, an IPv4Address and a Netmask/CIDR.

Implementations§

Source§

impl IPv4Network

Source

pub fn from_cidr( network_id: IPv4Address, cidr: u8, ) -> Result<IPv4Network, NetworkError>

Creates a new IPv4Network with the specified CIDR number.

§Example

 let addr = IPv4Address::from_be_bytes(&[127,0,0,0]);
 let network = IPv4Network::from_cidr(addr, 16).unwrap();

 assert_eq!("127.0.0.0/16", format!("{network}"));
Source

pub fn from_address_count( network_id: IPv4Address, address_count: u32, ) -> Result<IPv4Network, NetworkError>

Creates a new IPv4Network from a specified power-of-two count of network addresses. This is semantically equivalent to a CIDR, using 2^(32-cidr). For a /24 network, use 256.

§Example
 let addr = IPv4Address::from_be_bytes(&[127,0,0,0]);
 let network = IPv4Network::from_address_count(addr, 256).unwrap();

 assert_eq!("127.0.0.0/24", format!("{network}"));
Source

pub fn from_network_mask( network_id: IPv4Address, network_mask: u32, ) -> Result<IPv4Network, NetworkError>

Creates a new IPv4Network using the specified network ID and network Mask.

§Example

 let addr = IPv4Address::from_be_bytes(&[127,0,0,0]);
 let network = IPv4Network::from_network_mask(addr, 0xFFFFFF00).unwrap();

 assert_eq!("127.0.0.0/24", format!("{network}"));
Source

pub fn from_host_mask( network_id: IPv4Address, host_mask: u32, ) -> Result<IPv4Network, NetworkError>

Creates an IPv4Network from a network ID and host mask. A host mask is the inverted form of a network mask. If a /24 is represented by 0xFFFFFF00, then the equivalent host mask is 0x000000FF

§Example

 let addr = IPv4Address::from_be_bytes(&[127,0,0,0]);
 let network = IPv4Network::from_host_mask(addr, 0x000000FF).unwrap();

 assert_eq!("127.0.0.0/24", format!("{network}"));
Source

pub fn from_net_and_cidr( network_id: &[u8; 4], cidr: u8, ) -> Result<IPv4Network, NetworkError>

Creates a IPv4Network using the specified IPv4 Network ID and the specified CIDR.

§Example
let network = IPv4Network::from_net_and_cidr(&[127,0,0,0], 24).unwrap();

assert_eq!("127.0.0.0/24", format!("{network}"));
Source

pub fn is_network_address(&self, address: IPv4Address) -> bool

Returns true if the specified address is the network address for this network.

§Example
let net_addr = IPv4Address::from(&[127,0,0,0]);
let host_addr = IPv4Address::from(&[127,0,0,1]);
let network = IPv4Network::from_cidr(net_addr, 24).unwrap();

assert_eq!(true, network.is_network_address(net_addr));
assert_eq!(false, network.is_network_address(host_addr));
Source

pub fn is_broadcast_address(&self, address: IPv4Address) -> bool

Returns true if the specified address is the broadcast address for this network.

§Example
let net_addr = IPv4Address::from(&[127,0,0,0]);
let broadcast = IPv4Address::from(&[127,0,0,255]);
let network = IPv4Network::from_cidr(net_addr, 24).unwrap();

assert_eq!(true, network.is_broadcast_address(broadcast));
assert_eq!(false, network.is_broadcast_address(net_addr));
Source

pub fn is_private_address(&self) -> bool

Returns true if this address represents a private address range, in 10.0.0.0/8 or 172.16.0.0/12 or 192.168.0.0/16

§Example
let home_network = IPv4Network::from_net_and_cidr(&[192,168,0,0], 24).unwrap();
assert_eq!(true, home_network.is_private_address());

let enterprise_network = IPv4Network::from_net_and_cidr(&[10,10,0,0], 16).unwrap();
assert_eq!(true, enterprise_network.is_private_address());

let hotel_network = IPv4Network::from_net_and_cidr(&[172,20,0,0], 14).unwrap();
assert_eq!(true, hotel_network.is_private_address());

let quad_eight = IPv4Network::from_net_and_cidr(&[8,8,8,8], 32).unwrap();
assert_eq!(false, quad_eight.is_private_address());

Returns true if this network address represents a link-local address, in 169.254.0.0/16

§Example
let link_local = IPv4Network::from_net_and_cidr(&[169,254,55,228], 32).unwrap();
assert_eq!(true, link_local.is_link_local());

let quad_eight = IPv4Network::from_net_and_cidr(&[8,8,8,8], 32).unwrap();
assert_eq!(false, quad_eight.is_link_local());
Source

pub fn is_loopback(&self) -> bool

Returns true if this network address represents a loopback address, in 127.0.0.0/8

§Example
let loopback = IPv4Network::from_net_and_cidr(&[127,0,0,53], 32).unwrap();
assert_eq!(true, loopback.is_loopback());

let quad_eight = IPv4Network::from_net_and_cidr(&[8,8,8,8], 32).unwrap();
assert_eq!(false, quad_eight.is_loopback());
Source

pub fn is_shared_carrier_nat(&self) -> bool

Returns true if this network represents a carrier-grade NAT address, in 100.64.0.0/10

§Example
let carrier_nat = IPv4Network::from_net_and_cidr(&[100,80,0,0], 12).unwrap();
assert_eq!(true, carrier_nat.is_shared_carrier_nat());

let quad_eight = IPv4Network::from_net_and_cidr(&[8,8,8,8], 32).unwrap();
assert_eq!(false, quad_eight.is_shared_carrier_nat());
Source

pub fn host_in_network(&self, host: IPv4Address) -> bool

Source

pub fn get_network_address(&self) -> IPv4Address

Source

pub fn get_broadcast_address(&self) -> IPv4Address

Trait Implementations§

Source§

impl Clone for IPv4Network

Source§

fn clone(&self) -> IPv4Network

Returns a duplicate 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, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for IPv4Network

Source§

fn eq(&self, other: &IPv4Network) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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

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

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

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

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

Source§

impl StructuralPartialEq 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: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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, U> MaybeInto<U> for T
where U: MaybeFrom<T>,

Source§

fn maybe_into(self) -> Option<U>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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§

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>,

Source§

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>,

Source§

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.