ipnetwork

Enum IpNetwork

Source
pub enum IpNetwork {
    V4(Ipv4Network),
    V6(Ipv6Network),
}
Expand description

Represents a generic network range. This type can have two variants: the v4 and the v6 case.

Variants§

Implementations§

Source§

impl IpNetwork

Source

pub fn new(ip: IpAddr, prefix: u8) -> Result<IpNetwork, IpNetworkError>

Constructs a new IpNetwork from a given IpAddr and a prefix denoting the network size. If the prefix is larger than 32 (for IPv4) or 128 (for IPv6), this will raise an IpNetworkError::InvalidPrefix error. Support for IPv6 is not complete yet.

Source

pub fn with_netmask( netaddr: IpAddr, netmask: IpAddr, ) -> Result<Self, IpNetworkError>

Constructs a new IpNetwork from a network address and a network mask.

If the netmask is not valid this will return an IpNetworkError::InvalidPrefix.

Source

pub fn ip(&self) -> IpAddr

Returns the IP part of a given IpNetwork

Source

pub fn prefix(&self) -> u8

Returns the prefix of the given IpNetwork

§Example
use ipnetwork::IpNetwork;

assert_eq!(IpNetwork::V4("10.9.0.1".parse().unwrap()).prefix(), 32u8);
assert_eq!(IpNetwork::V4("10.9.0.32/16".parse().unwrap()).prefix(), 16u8);

assert_eq!(IpNetwork::V6("ff01::0".parse().unwrap()).prefix(), 128u8);
assert_eq!(IpNetwork::V6("ff01::0/32".parse().unwrap()).prefix(), 32u8);
Source

pub fn network(&self) -> IpAddr

Returns the address of the network denoted by this IpNetwork. This means the lowest possible IP address inside of the network.

§Examples
use std::net::{Ipv4Addr, Ipv6Addr};
use ipnetwork::IpNetwork;

let net: IpNetwork = "10.1.9.32/16".parse().unwrap();
assert_eq!(net.network(), Ipv4Addr::new(10, 1, 0, 0));
let net: IpNetwork = "2001:db8::/96".parse().unwrap();
assert_eq!(net.network(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0));
Source

pub fn broadcast(&self) -> IpAddr

Returns the broadcasting address of this IpNetwork. This means the highest possible IP address inside of the network.

§Examples
use std::net::Ipv4Addr;
use ipnetwork::{IpNetwork, Ipv4Network};

let net: Ipv4Network = "10.9.0.32/16".parse().unwrap();
assert_eq!(net.broadcast(), Ipv4Addr::new(10, 9, 255, 255));
Source

pub fn mask(&self) -> IpAddr

Returns the mask for this IpNetwork. That means the prefix most significant bits will be 1 and the rest 0

§Example
use ipnetwork::IpNetwork;
use std::net::{Ipv4Addr, Ipv6Addr};

let v4_net: IpNetwork = "10.9.0.1".parse().unwrap();
assert_eq!(v4_net.mask(), Ipv4Addr::new(255, 255, 255, 255));
let v4_net: IpNetwork = "10.9.0.32/16".parse().unwrap();
assert_eq!(v4_net.mask(), Ipv4Addr::new(255, 255, 0, 0));

let v6_net: IpNetwork = "ff01::0".parse().unwrap();
assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff));
let v6_net: IpNetwork = "ff01::0/32".parse().unwrap();
assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0, 0, 0, 0, 0, 0));
Source

pub fn is_ipv4(&self) -> bool

Returns true if the IP in this IpNetwork is a valid IPv4 address, false if it’s a valid IPv6 address.

§Example
 use ipnetwork::IpNetwork;

 let v4: IpNetwork = IpNetwork::V4("10.9.0.32/16".parse().unwrap());
 assert_eq!(v4.is_ipv4(), true);
 assert_eq!(v4.is_ipv6(), false);
Source

pub fn is_ipv6(&self) -> bool

Returns true if the IP in this IpNetwork is a valid IPv6 address, false if it’s a valid IPv4 address.

§Example
 use ipnetwork::IpNetwork;

 let v6: IpNetwork = IpNetwork::V6("ff01::0/32".parse().unwrap());
 assert_eq!(v6.is_ipv6(), true);
 assert_eq!(v6.is_ipv4(), false);
Source

pub fn contains(&self, ip: IpAddr) -> bool

Checks if a given IpAddr is in this IpNetwork

§Examples
use std::net::IpAddr;
use ipnetwork::IpNetwork;

let net: IpNetwork = "127.0.0.0/24".parse().unwrap();
let ip1: IpAddr = "127.0.0.1".parse().unwrap();
let ip2: IpAddr = "172.0.0.1".parse().unwrap();
let ip4: IpAddr = "::1".parse().unwrap();
assert!(net.contains(ip1));
assert!(!net.contains(ip2));
assert!(!net.contains(ip4));
Source

pub fn size(&self) -> NetworkSize

Returns the number of possible host addresses in this IpAddr

§Examples
use ipnetwork::{IpNetwork, NetworkSize};


let net: IpNetwork = "127.0.0.0/24".parse().unwrap();
assert_eq!(net.size(), NetworkSize::V4(256))
Source

pub fn iter(&self) -> IpNetworkIterator

Returns an iterator over the addresses contained in the network.

This lists all the addresses in the network range, in ascending order.

Trait Implementations§

Source§

impl Clone for IpNetwork

Source§

fn clone(&self) -> IpNetwork

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 IpNetwork

Source§

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

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

impl<'de> Deserialize<'de> for IpNetwork

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for IpNetwork

Source§

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

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

impl From<IpAddr> for IpNetwork

Source§

fn from(addr: IpAddr) -> IpNetwork

Converts to this type from the input type.
Source§

impl From<Ipv4Network> for IpNetwork

Source§

fn from(v4: Ipv4Network) -> IpNetwork

Converts to this type from the input type.
Source§

impl From<Ipv6Network> for IpNetwork

Source§

fn from(v6: Ipv6Network) -> IpNetwork

Converts to this type from the input type.
Source§

impl FromStr for IpNetwork

Tries to parse the given string into a IpNetwork. Will first try to parse it as an Ipv4Network and if that fails as an Ipv6Network. If both fails it will return an InvalidAddr error.

§Examples

use std::net::Ipv4Addr;
use ipnetwork::{IpNetwork, Ipv4Network};

let expected = IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(10, 1, 9, 32), 16).unwrap());
let from_cidr: IpNetwork = "10.1.9.32/16".parse().unwrap();
assert_eq!(expected, from_cidr);
Source§

type Err = IpNetworkError

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

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for IpNetwork

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 &IpNetwork

Source§

type IntoIter = IpNetworkIterator

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

type Item = IpAddr

The type of the elements being iterated over.
Source§

fn into_iter(self) -> IpNetworkIterator

Creates an iterator from a value. Read more
Source§

impl Ord for IpNetwork

Source§

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

Source§

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

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

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

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&str> for IpNetwork

Source§

type Error = IpNetworkError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for IpNetwork

Source§

impl Eq for IpNetwork

Source§

impl StructuralPartialEq for IpNetwork

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, dst: *mut u8)

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

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§

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

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.
Source§

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