Enum ip_network::IpNetwork [−][src]
pub enum IpNetwork { V4(Ipv4Network), V6(Ipv6Network), }
Holds IPv4 or IPv6 network.
Variants
V4(Ipv4Network)
V6(Ipv6Network)
Implementations
impl IpNetwork
[src]
impl IpNetwork
[src]pub fn new<I: Into<IpAddr>>(
network_address: I,
netmask: u8
) -> Result<Self, IpNetworkError>
[src]
network_address: I,
netmask: u8
) -> Result<Self, IpNetworkError>
Constructs new IpNetwork
based on IpAddr
and netmask
.
Examples
use std::net::{IpAddr, Ipv4Addr}; use std::str::FromStr; use ip_network::{IpNetwork, Ipv4Network}; let network_address = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)); let ip_network = IpNetwork::new(network_address, 24)?; assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0))); assert_eq!(ip_network.netmask(), 24);
pub fn new_truncate<I: Into<IpAddr>>(
network_address: I,
netmask: u8
) -> Result<Self, IpNetworkError>
[src]
network_address: I,
netmask: u8
) -> Result<Self, IpNetworkError>
Constructs new IpNetwork
based on IpAddr
and netmask
with truncating host bits
from given network_address
.
Returns error if netmask is bigger than 32 for IPv4 and 128 for IPv6.
Examples
use std::net::{IpAddr, Ipv4Addr}; use ip_network::IpNetwork; let network_address = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 128)); let ip_network = IpNetwork::new_truncate(network_address, 24)?; assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0))); assert_eq!(ip_network.netmask(), 24);
pub fn network_address(&self) -> IpAddr
[src]
Returns network IP address.
Examples
use std::net::{IpAddr, Ipv4Addr}; use ip_network::IpNetwork; let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?; assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)));
pub fn netmask(&self) -> u8
[src]
Returns network mask as integer.
Examples
use std::net::{IpAddr, Ipv4Addr}; use ip_network::IpNetwork; let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?; assert_eq!(ip_network.netmask(), 24);
pub fn is_ipv4(&self) -> bool
[src]
Returns true
if IpNetwork
contains Ipv4Network
struct.
pub fn is_ipv6(&self) -> bool
[src]
Returns true
if IpNetwork
contains Ipv6Network
struct.
pub fn contains<I: Into<IpAddr>>(&self, ip: I) -> bool
[src]
Returns true
if IpNetwork
contains IpAddr
. For different network type
(for example IpNetwork is IPv6 and IpAddr is IPv4) always returns false
.
Examples
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use ip_network::IpNetwork; let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?; assert!(ip_network.contains(Ipv4Addr::new(192, 168, 1, 25))); assert!(!ip_network.contains(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 1, 0, 0)));
pub fn is_default_route(&self) -> bool
[src]
Returns true
if the network is default route, that contains all IP addresses.
pub fn is_multicast(&self) -> bool
[src]
Returns true
if the network is part of multicast network range.
pub fn is_documentation(&self) -> bool
[src]
Returns true
if this is a part of network reserved for documentation.
pub fn is_loopback(&self) -> bool
[src]
Returns true
if this network is inside loopback address range.
pub fn is_global(&self) -> bool
[src]
Returns true
if the network appears to be globally routable.
Trait Implementations
impl Display for IpNetwork
[src]
impl Display for IpNetwork
[src]fn fmt(&self, f: &mut Formatter<'_>) -> Result
[src]
Converts IpNetwork
to string in format X.X.X.X/Y for IPv4 and X:X::X/Y for IPv6 (CIDR notation).
Examples
use std::net::Ipv4Addr; use ip_network::{IpNetwork, Ipv4Network}; let ip_network = IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?); assert_eq!(ip_network.to_string(), "192.168.1.0/24");
impl From<Ipv4Network> for IpNetwork
[src]
impl From<Ipv4Network> for IpNetwork
[src]fn from(network: Ipv4Network) -> Self
[src]
impl From<Ipv6Network> for IpNetwork
[src]
impl From<Ipv6Network> for IpNetwork
[src]fn from(network: Ipv6Network) -> Self
[src]
impl FromStr for IpNetwork
[src]
impl FromStr for IpNetwork
[src]type Err = IpNetworkParseError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<IpNetwork, IpNetworkParseError>
[src]
Converts string in format IPv4 (X.X.X.X/Y) or IPv6 (X:X::X/Y) CIDR notation to IpNetwork
.
Examples
use std::net::Ipv4Addr; use std::str::FromStr; use ip_network::{IpNetwork, Ipv4Network}; let ip_network = IpNetwork::from_str("192.168.1.0/24").unwrap(); assert_eq!(ip_network, IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24).unwrap()));
impl PartialEq<IpNetwork> for Ipv4Network
[src]
impl PartialEq<IpNetwork> for Ipv4Network
[src]impl PartialEq<IpNetwork> for Ipv6Network
[src]
impl PartialEq<IpNetwork> for Ipv6Network
[src]impl PartialEq<Ipv4Network> for IpNetwork
[src]
impl PartialEq<Ipv4Network> for IpNetwork
[src]impl PartialEq<Ipv6Network> for IpNetwork
[src]
impl PartialEq<Ipv6Network> for IpNetwork
[src]impl PartialOrd<IpNetwork> for IpNetwork
[src]
impl PartialOrd<IpNetwork> for IpNetwork
[src]impl PartialOrd<IpNetwork> for Ipv4Network
[src]
impl PartialOrd<IpNetwork> for Ipv4Network
[src]impl PartialOrd<IpNetwork> for Ipv6Network
[src]
impl PartialOrd<IpNetwork> for Ipv6Network
[src]impl PartialOrd<Ipv4Network> for IpNetwork
[src]
impl PartialOrd<Ipv4Network> for IpNetwork
[src]impl PartialOrd<Ipv6Network> for IpNetwork
[src]
impl PartialOrd<Ipv6Network> for IpNetwork
[src]