mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-17 01:08:53 +00:00
Compare commits
40 Commits
Author | SHA1 | Date | |
---|---|---|---|
61550eda45 | |||
3503e4c094 | |||
91528476a9 | |||
501e5533d7 | |||
7120b93837 | |||
a57afd9e1a | |||
8ad43541cd | |||
9651b3b721 | |||
5764406000 | |||
4cbd1f2424 | |||
772db6c569 | |||
c9b2bc5519 | |||
fe9f1ba8d5 | |||
9921661ebe | |||
14108098d6 | |||
1f3e10f73d | |||
c26c836b39 | |||
8fcff82c7b | |||
46cc64d424 | |||
e15e8cb812 | |||
0b98eecf6a | |||
6c6bdb333d | |||
eded5f13a9 | |||
1d4665dfe6 | |||
e80eab0c80 | |||
8f1c6b59bb | |||
a9f4547c82 | |||
fa128680b5 | |||
620ec4d42c | |||
b6397ca2bc | |||
c0257fc59d | |||
45fbf458b3 | |||
ca1d332e71 | |||
121820add8 | |||
65bd6724c2 | |||
41e0f8a8a3 | |||
f23ea4d7d6 | |||
fc958f16dc | |||
23b95c3b85 | |||
38460c0598 |
7
.github/dependabot.yml
vendored
Normal file
7
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: cargo
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: daily
|
||||||
|
open-pull-requests-limit: 10
|
14
Cargo.toml
14
Cargo.toml
@ -1,23 +1,22 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ipnetwork"
|
name = "ipnetwork"
|
||||||
version = "0.16.0" # When updating version, also modify html_root_url in the lib.rs
|
version = "0.19.0"
|
||||||
authors = ["Abhishek Chanda <abhishek.becs@gmail.com>", "Linus Färnstrand <faern@faern.net>"]
|
authors = ["Abhishek Chanda <abhishek.becs@gmail.com>", "Linus Färnstrand <faern@faern.net>"]
|
||||||
description = "A library to work with IP CIDRs in Rust"
|
description = "A library to work with IP CIDRs in Rust"
|
||||||
license = "Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
repository = "https://github.com/achanda/ipnetwork"
|
repository = "https://github.com/achanda/ipnetwork"
|
||||||
keywords = ["network", "ip", "address", "cidr"]
|
keywords = ["network", "ip", "address", "cidr"]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
categories = ["network-programming", "parser-implementations"]
|
categories = ["network-programming", "parser-implementations"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = {version = "0.0.302", optional = true}
|
serde = { version = "1", optional = true }
|
||||||
serde = { version = ">=0.8.0, <2.0", optional = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_derive = ">=0.8.0, <2.0"
|
serde_derive = "1"
|
||||||
criterion = "0.3.0"
|
criterion = {version = "0.3.4", features= ["html_reports"]}
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
travis-ci = { repository = "achanda/ipnetwork" }
|
travis-ci = { repository = "achanda/ipnetwork" }
|
||||||
@ -25,7 +24,6 @@ maintenance = { status = "passively-maintained" }
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["serde"]
|
default = ["serde"]
|
||||||
dev = ["clippy"]
|
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "parse_bench"
|
name = "parse_bench"
|
||||||
|
25
LICENSE-MIT.md
Normal file
25
LICENSE-MIT.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
Copyright 2020 Developers of the ipnetwork project
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any
|
||||||
|
person obtaining a copy of this software and associated
|
||||||
|
documentation files (the "Software"), to deal in the
|
||||||
|
Software without restriction, including without
|
||||||
|
limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software
|
||||||
|
is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice
|
||||||
|
shall be included in all copies or substantial portions
|
||||||
|
of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||||
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||||
|
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||||
|
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||||
|
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
@ -1,14 +1,14 @@
|
|||||||
ipnetwork
|
ipnetwork
|
||||||
===
|
===
|
||||||
This is a library to work with IPv4 and v6 CIDRs in rust
|
This is a library to work with IPv4 and IPv6 CIDRs in Rust
|
||||||
The IPv4 implementation is stable, IPv6 implementation is not done yet.
|
|
||||||
|
|
||||||
[](https://travis-ci.org/achanda/ipnetwork)
|
[](https://travis-ci.org/achanda/ipnetwork)
|
||||||
[](https://crates.io/crates/ipnetwork)
|
[](https://crates.io/crates/ipnetwork)
|
||||||
|
|
||||||
Run Clippy by doing
|
Run Clippy by doing
|
||||||
```
|
```
|
||||||
cargo test --features "dev"
|
rustup component add clippy
|
||||||
|
cargo clippy
|
||||||
```
|
```
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
@ -20,7 +20,7 @@ cargo test
|
|||||||
```
|
```
|
||||||
|
|
||||||
You can also add `ipnetwork` as a dependency to your project's `Cargo.toml`:
|
You can also add `ipnetwork` as a dependency to your project's `Cargo.toml`:
|
||||||
```
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ipnetwork = "*"
|
ipnetwork = "*"
|
||||||
```
|
```
|
||||||
|
@ -21,18 +21,18 @@ fn parse_ipv4_netmask_benchmark(c: &mut Criterion) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn contains_ipv4_benchmark(c: &mut Criterion) {
|
fn contains_ipv4_benchmark(c: &mut Criterion) {
|
||||||
|
let cidr = "74.125.227.0/25".parse::<Ipv4Network>().unwrap();
|
||||||
c.bench_function("contains ipv4", |b| {
|
c.bench_function("contains ipv4", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let cidr = "74.125.227.0/25".parse::<Ipv4Network>().unwrap();
|
|
||||||
cidr.contains(Ipv4Addr::new(74, 125, 227, 4))
|
cidr.contains(Ipv4Addr::new(74, 125, 227, 4))
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn contains_ipv6_benchmark(c: &mut Criterion) {
|
fn contains_ipv6_benchmark(c: &mut Criterion) {
|
||||||
|
let cidr = "FF01:0:0:17:0:0:0:2/65".parse::<Ipv6Network>().unwrap();
|
||||||
c.bench_function("contains ipv6", |b| {
|
c.bench_function("contains ipv6", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let cidr = "FF01:0:0:17:0:0:0:2/65".parse::<Ipv6Network>().unwrap();
|
|
||||||
cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2))
|
cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2))
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -36,17 +36,17 @@ pub fn cidr_parts(cidr: &str) -> Result<(&str, Option<&str>), IpNetworkError> {
|
|||||||
let (ip, prefix) = cidr.split_at(sep);
|
let (ip, prefix) = cidr.split_at(sep);
|
||||||
// Error if cidr has multiple slashes
|
// Error if cidr has multiple slashes
|
||||||
if prefix[1..].find('/').is_some() {
|
if prefix[1..].find('/').is_some() {
|
||||||
return Err(IpNetworkError::InvalidCidrFormat(format!(
|
Err(IpNetworkError::InvalidCidrFormat(format!(
|
||||||
"CIDR must contain a single '/': {}",
|
"CIDR must contain a single '/': {}",
|
||||||
cidr
|
cidr
|
||||||
)));
|
)))
|
||||||
} else {
|
} else {
|
||||||
// Handle the case when cidr has exactly one slash
|
// Handle the case when cidr has exactly one slash
|
||||||
return Ok((ip, Some(&prefix[1..])));
|
Ok((ip, Some(&prefix[1..])))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Handle the case when cidr does not have a slash
|
// Handle the case when cidr does not have a slash
|
||||||
return Ok((cidr, None));
|
Ok((cidr, None))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
src/ipv4.rs
35
src/ipv4.rs
@ -1,5 +1,5 @@
|
|||||||
use crate::common::{cidr_parts, parse_prefix, IpNetworkError};
|
use crate::common::{cidr_parts, parse_prefix, IpNetworkError};
|
||||||
use std::{fmt, net::Ipv4Addr, str::FromStr};
|
use std::{fmt, net::Ipv4Addr, str::FromStr, convert::TryFrom};
|
||||||
|
|
||||||
const IPV4_BITS: u8 = 32;
|
const IPV4_BITS: u8 = 32;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ impl serde::Serialize for Ipv4Network {
|
|||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_str(&self.to_string())
|
serializer.collect_str(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ impl Ipv4Network {
|
|||||||
/// Constructs a new `Ipv4Network` from any `Ipv4Addr` and a prefix denoting the network size.
|
/// Constructs a new `Ipv4Network` from any `Ipv4Addr` and a prefix denoting the network size.
|
||||||
///
|
///
|
||||||
/// If the prefix is larger than 32 this will return an `IpNetworkError::InvalidPrefix`.
|
/// If the prefix is larger than 32 this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn new(addr: Ipv4Addr, prefix: u8) -> Result<Ipv4Network, IpNetworkError> {
|
pub const fn new(addr: Ipv4Addr, prefix: u8) -> Result<Ipv4Network, IpNetworkError> {
|
||||||
if prefix > IPV4_BITS {
|
if prefix > IPV4_BITS {
|
||||||
Err(IpNetworkError::InvalidPrefix)
|
Err(IpNetworkError::InvalidPrefix)
|
||||||
} else {
|
} else {
|
||||||
@ -61,7 +61,7 @@ impl Ipv4Network {
|
|||||||
/// Returns an iterator over `Ipv4Network`. Each call to `next` will return the next
|
/// Returns an iterator over `Ipv4Network`. Each call to `next` will return the next
|
||||||
/// `Ipv4Addr` in the given network. `None` will be returned when there are no more
|
/// `Ipv4Addr` in the given network. `None` will be returned when there are no more
|
||||||
/// addresses.
|
/// addresses.
|
||||||
pub fn iter(&self) -> Ipv4NetworkIterator {
|
pub fn iter(self) -> Ipv4NetworkIterator {
|
||||||
let start = u32::from(self.network());
|
let start = u32::from(self.network());
|
||||||
let end = start + (self.size() - 1);
|
let end = start + (self.size() - 1);
|
||||||
Ipv4NetworkIterator {
|
Ipv4NetworkIterator {
|
||||||
@ -70,11 +70,11 @@ impl Ipv4Network {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ip(&self) -> Ipv4Addr {
|
pub fn ip(self) -> Ipv4Addr {
|
||||||
self.addr
|
self.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prefix(&self) -> u8 {
|
pub fn prefix(self) -> u8 {
|
||||||
self.prefix
|
self.prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ impl Ipv4Network {
|
|||||||
/// let net: Ipv4Network = "127.0.0.0/16".parse().unwrap();
|
/// let net: Ipv4Network = "127.0.0.0/16".parse().unwrap();
|
||||||
/// assert_eq!(net.mask(), Ipv4Addr::new(255, 255, 0, 0));
|
/// assert_eq!(net.mask(), Ipv4Addr::new(255, 255, 0, 0));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn mask(&self) -> Ipv4Addr {
|
pub fn mask(self) -> Ipv4Addr {
|
||||||
let prefix = self.prefix;
|
let prefix = self.prefix;
|
||||||
let mask = !(0xffff_ffff as u64 >> prefix) as u32;
|
let mask = !(0xffff_ffff as u64 >> prefix) as u32;
|
||||||
Ipv4Addr::from(mask)
|
Ipv4Addr::from(mask)
|
||||||
@ -127,7 +127,7 @@ impl Ipv4Network {
|
|||||||
/// let net: Ipv4Network = "10.1.9.32/16".parse().unwrap();
|
/// let net: Ipv4Network = "10.1.9.32/16".parse().unwrap();
|
||||||
/// assert_eq!(net.network(), Ipv4Addr::new(10, 1, 0, 0));
|
/// assert_eq!(net.network(), Ipv4Addr::new(10, 1, 0, 0));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn network(&self) -> Ipv4Addr {
|
pub fn network(self) -> Ipv4Addr {
|
||||||
let mask = u32::from(self.mask());
|
let mask = u32::from(self.mask());
|
||||||
let ip = u32::from(self.addr) & mask;
|
let ip = u32::from(self.addr) & mask;
|
||||||
Ipv4Addr::from(ip)
|
Ipv4Addr::from(ip)
|
||||||
@ -145,7 +145,7 @@ impl Ipv4Network {
|
|||||||
/// let net: Ipv4Network = "10.9.0.32/16".parse().unwrap();
|
/// let net: Ipv4Network = "10.9.0.32/16".parse().unwrap();
|
||||||
/// assert_eq!(net.broadcast(), Ipv4Addr::new(10, 9, 255, 255));
|
/// assert_eq!(net.broadcast(), Ipv4Addr::new(10, 9, 255, 255));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn broadcast(&self) -> Ipv4Addr {
|
pub fn broadcast(self) -> Ipv4Addr {
|
||||||
let mask = u32::from(self.mask());
|
let mask = u32::from(self.mask());
|
||||||
let broadcast = u32::from(self.addr) | !mask;
|
let broadcast = u32::from(self.addr) | !mask;
|
||||||
Ipv4Addr::from(broadcast)
|
Ipv4Addr::from(broadcast)
|
||||||
@ -163,7 +163,8 @@ impl Ipv4Network {
|
|||||||
/// assert!(net.contains(Ipv4Addr::new(127, 0, 0, 70)));
|
/// assert!(net.contains(Ipv4Addr::new(127, 0, 0, 70)));
|
||||||
/// assert!(!net.contains(Ipv4Addr::new(127, 0, 1, 70)));
|
/// assert!(!net.contains(Ipv4Addr::new(127, 0, 1, 70)));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn contains(&self, ip: Ipv4Addr) -> bool {
|
#[inline]
|
||||||
|
pub fn contains(self, ip: Ipv4Addr) -> bool {
|
||||||
let mask = !(0xffff_ffff as u64 >> self.prefix) as u32;
|
let mask = !(0xffff_ffff as u64 >> self.prefix) as u32;
|
||||||
let net = u32::from(self.addr) & mask;
|
let net = u32::from(self.addr) & mask;
|
||||||
(u32::from(ip) & mask) == net
|
(u32::from(ip) & mask) == net
|
||||||
@ -183,7 +184,7 @@ impl Ipv4Network {
|
|||||||
/// let tinynet: Ipv4Network = "0.0.0.0/32".parse().unwrap();
|
/// let tinynet: Ipv4Network = "0.0.0.0/32".parse().unwrap();
|
||||||
/// assert_eq!(tinynet.size(), 1);
|
/// assert_eq!(tinynet.size(), 1);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn size(&self) -> u32 {
|
pub fn size(self) -> u32 {
|
||||||
let host_bits = u32::from(IPV4_BITS - self.prefix);
|
let host_bits = u32::from(IPV4_BITS - self.prefix);
|
||||||
(2 as u32).pow(host_bits)
|
(2 as u32).pow(host_bits)
|
||||||
}
|
}
|
||||||
@ -205,7 +206,7 @@ impl Ipv4Network {
|
|||||||
/// let net2: Ipv4Network = "10.0.0.0/16".parse().unwrap();
|
/// let net2: Ipv4Network = "10.0.0.0/16".parse().unwrap();
|
||||||
/// assert_eq!(net2.nth(256).unwrap(), Ipv4Addr::new(10, 0, 1, 0));
|
/// assert_eq!(net2.nth(256).unwrap(), Ipv4Addr::new(10, 0, 1, 0));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn nth(&self, n: u32) -> Option<Ipv4Addr> {
|
pub fn nth(self, n: u32) -> Option<Ipv4Addr> {
|
||||||
if n < self.size() {
|
if n < self.size() {
|
||||||
let net = u32::from(self.network());
|
let net = u32::from(self.network());
|
||||||
Some(Ipv4Addr::from(net + n))
|
Some(Ipv4Addr::from(net + n))
|
||||||
@ -236,7 +237,7 @@ impl fmt::Display for Ipv4Network {
|
|||||||
/// ```
|
/// ```
|
||||||
impl FromStr for Ipv4Network {
|
impl FromStr for Ipv4Network {
|
||||||
type Err = IpNetworkError;
|
type Err = IpNetworkError;
|
||||||
fn from_str(s: &str) -> Result<Ipv4Network, IpNetworkError> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let (addr_str, prefix_str) = cidr_parts(s)?;
|
let (addr_str, prefix_str) = cidr_parts(s)?;
|
||||||
let addr = Ipv4Addr::from_str(addr_str)
|
let addr = Ipv4Addr::from_str(addr_str)
|
||||||
.map_err(|_| IpNetworkError::InvalidAddr(addr_str.to_string()))?;
|
.map_err(|_| IpNetworkError::InvalidAddr(addr_str.to_string()))?;
|
||||||
@ -254,6 +255,14 @@ impl FromStr for Ipv4Network {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&str> for Ipv4Network {
|
||||||
|
type Error = IpNetworkError;
|
||||||
|
|
||||||
|
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||||
|
Ipv4Network::from_str(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Ipv4Addr> for Ipv4Network {
|
impl From<Ipv4Addr> for Ipv4Network {
|
||||||
fn from(a: Ipv4Addr) -> Ipv4Network {
|
fn from(a: Ipv4Addr) -> Ipv4Network {
|
||||||
Ipv4Network {
|
Ipv4Network {
|
||||||
|
32
src/ipv6.rs
32
src/ipv6.rs
@ -1,5 +1,5 @@
|
|||||||
use crate::common::{cidr_parts, parse_prefix, IpNetworkError};
|
use crate::common::{cidr_parts, parse_prefix, IpNetworkError};
|
||||||
use std::{cmp, fmt, net::Ipv6Addr, str::FromStr};
|
use std::{cmp, fmt, net::Ipv6Addr, str::FromStr, convert::TryFrom};
|
||||||
|
|
||||||
const IPV6_BITS: u8 = 128;
|
const IPV6_BITS: u8 = 128;
|
||||||
const IPV6_SEGMENT_BITS: u8 = 16;
|
const IPV6_SEGMENT_BITS: u8 = 16;
|
||||||
@ -28,7 +28,7 @@ impl serde::Serialize for Ipv6Network {
|
|||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_str(&self.to_string())
|
serializer.collect_str(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ impl Ipv6Network {
|
|||||||
/// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size.
|
/// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size.
|
||||||
///
|
///
|
||||||
/// If the prefix is larger than 128 this will return an `IpNetworkError::InvalidPrefix`.
|
/// If the prefix is larger than 128 this will return an `IpNetworkError::InvalidPrefix`.
|
||||||
pub fn new(addr: Ipv6Addr, prefix: u8) -> Result<Ipv6Network, IpNetworkError> {
|
pub const fn new(addr: Ipv6Addr, prefix: u8) -> Result<Ipv6Network, IpNetworkError> {
|
||||||
if prefix > IPV6_BITS {
|
if prefix > IPV6_BITS {
|
||||||
Err(IpNetworkError::InvalidPrefix)
|
Err(IpNetworkError::InvalidPrefix)
|
||||||
} else {
|
} else {
|
||||||
@ -72,7 +72,7 @@ impl Ipv6Network {
|
|||||||
|
|
||||||
Ipv6NetworkIterator {
|
Ipv6NetworkIterator {
|
||||||
next: Some(start),
|
next: Some(start),
|
||||||
end: end,
|
end,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,6 +174,7 @@ impl Ipv6Network {
|
|||||||
/// assert!(net.contains(Ipv6Addr::new(0xff01, 0, 0, 0, 0, 0, 0, 0x1)));
|
/// assert!(net.contains(Ipv6Addr::new(0xff01, 0, 0, 0, 0, 0, 0, 0x1)));
|
||||||
/// assert!(!net.contains(Ipv6Addr::new(0xffff, 0, 0, 0, 0, 0, 0, 0x1)));
|
/// assert!(!net.contains(Ipv6Addr::new(0xffff, 0, 0, 0, 0, 0, 0, 0x1)));
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
pub fn contains(&self, ip: Ipv6Addr) -> bool {
|
pub fn contains(&self, ip: Ipv6Addr) -> bool {
|
||||||
let a = self.addr.segments();
|
let a = self.addr.segments();
|
||||||
let b = ip.segments();
|
let b = ip.segments();
|
||||||
@ -205,9 +206,22 @@ impl Ipv6Network {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates an `Ipv6Network` from parsing a string in CIDR notation.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::net::Ipv6Addr;
|
||||||
|
/// use ipnetwork::Ipv6Network;
|
||||||
|
///
|
||||||
|
/// let new = Ipv6Network::new(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0, 0, 0, 0x2), 65).unwrap();
|
||||||
|
/// let from_cidr: Ipv6Network = "FF01:0:0:17:0:0:0:2/65".parse().unwrap();
|
||||||
|
/// assert_eq!(new.ip(), from_cidr.ip());
|
||||||
|
/// assert_eq!(new.prefix(), from_cidr.prefix());
|
||||||
|
/// ```
|
||||||
impl FromStr for Ipv6Network {
|
impl FromStr for Ipv6Network {
|
||||||
type Err = IpNetworkError;
|
type Err = IpNetworkError;
|
||||||
fn from_str(s: &str) -> Result<Ipv6Network, IpNetworkError> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let (addr_str, prefix_str) = cidr_parts(s)?;
|
let (addr_str, prefix_str) = cidr_parts(s)?;
|
||||||
let addr = Ipv6Addr::from_str(addr_str)
|
let addr = Ipv6Addr::from_str(addr_str)
|
||||||
.map_err(|_| IpNetworkError::InvalidAddr(addr_str.to_string()))?;
|
.map_err(|_| IpNetworkError::InvalidAddr(addr_str.to_string()))?;
|
||||||
@ -219,6 +233,14 @@ impl FromStr for Ipv6Network {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&str> for Ipv6Network {
|
||||||
|
type Error = IpNetworkError;
|
||||||
|
|
||||||
|
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||||
|
Ipv6Network::from_str(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Ipv6Addr> for Ipv6Network {
|
impl From<Ipv6Addr> for Ipv6Network {
|
||||||
fn from(a: Ipv6Addr) -> Ipv6Network {
|
fn from(a: Ipv6Addr) -> Ipv6Network {
|
||||||
Ipv6Network {
|
Ipv6Network {
|
||||||
|
29
src/lib.rs
29
src/lib.rs
@ -1,20 +1,23 @@
|
|||||||
//! The `ipnetwork` crate provides a set of APIs to work with IP CIDRs in
|
//! The `ipnetwork` crate provides a set of APIs to work with IP CIDRs in
|
||||||
//! Rust.
|
//! Rust.
|
||||||
#![cfg_attr(feature = "dev", feature(plugin))]
|
|
||||||
#![cfg_attr(feature = "dev", plugin(clippy))]
|
|
||||||
#![crate_type = "lib"]
|
#![crate_type = "lib"]
|
||||||
#![doc(html_root_url = "https://docs.rs/ipnetwork/0.16.0")]
|
|
||||||
|
|
||||||
use std::{fmt, net::IpAddr, str::FromStr};
|
#![deny(
|
||||||
|
missing_debug_implementations,
|
||||||
|
unsafe_code,
|
||||||
|
unused_extern_crates,
|
||||||
|
unused_import_braces)]
|
||||||
|
|
||||||
|
use std::{fmt, net::IpAddr, str::FromStr, convert::TryFrom};
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
mod ipv4;
|
mod ipv4;
|
||||||
mod ipv6;
|
mod ipv6;
|
||||||
|
|
||||||
pub use crate::common::IpNetworkError;
|
pub use crate::common::IpNetworkError;
|
||||||
use crate::ipv4::Ipv4NetworkIterator;
|
pub use crate::ipv4::Ipv4NetworkIterator;
|
||||||
pub use crate::ipv4::{ipv4_mask_to_prefix, Ipv4Network};
|
pub use crate::ipv4::{ipv4_mask_to_prefix, Ipv4Network};
|
||||||
use crate::ipv6::Ipv6NetworkIterator;
|
pub use crate::ipv6::Ipv6NetworkIterator;
|
||||||
pub use crate::ipv6::{ipv6_mask_to_prefix, Ipv6Network};
|
pub use crate::ipv6::{ipv6_mask_to_prefix, Ipv6Network};
|
||||||
|
|
||||||
/// Represents a generic network range. This type can have two variants:
|
/// Represents a generic network range. This type can have two variants:
|
||||||
@ -42,7 +45,7 @@ impl serde::Serialize for IpNetwork {
|
|||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_str(&self.to_string())
|
serializer.collect_str(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,6 +227,7 @@ impl IpNetwork {
|
|||||||
/// assert!(!net.contains(ip2));
|
/// assert!(!net.contains(ip2));
|
||||||
/// assert!(!net.contains(ip4));
|
/// assert!(!net.contains(ip4));
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
pub fn contains(&self, ip: IpAddr) -> bool {
|
pub fn contains(&self, ip: IpAddr) -> bool {
|
||||||
match (*self, ip) {
|
match (*self, ip) {
|
||||||
(IpNetwork::V4(net), IpAddr::V4(ip)) => net.contains(ip),
|
(IpNetwork::V4(net), IpAddr::V4(ip)) => net.contains(ip),
|
||||||
@ -278,7 +282,7 @@ impl IpNetwork {
|
|||||||
/// ```
|
/// ```
|
||||||
impl FromStr for IpNetwork {
|
impl FromStr for IpNetwork {
|
||||||
type Err = IpNetworkError;
|
type Err = IpNetworkError;
|
||||||
fn from_str(s: &str) -> Result<IpNetwork, IpNetworkError> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
if let Ok(net) = Ipv4Network::from_str(s) {
|
if let Ok(net) = Ipv4Network::from_str(s) {
|
||||||
Ok(IpNetwork::V4(net))
|
Ok(IpNetwork::V4(net))
|
||||||
} else if let Ok(net) = Ipv6Network::from_str(s) {
|
} else if let Ok(net) = Ipv6Network::from_str(s) {
|
||||||
@ -289,6 +293,14 @@ impl FromStr for IpNetwork {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&str> for IpNetwork {
|
||||||
|
type Error = IpNetworkError;
|
||||||
|
|
||||||
|
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||||
|
IpNetwork::from_str(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Ipv4Network> for IpNetwork {
|
impl From<Ipv4Network> for IpNetwork {
|
||||||
fn from(v4: Ipv4Network) -> IpNetwork {
|
fn from(v4: Ipv4Network) -> IpNetwork {
|
||||||
IpNetwork::V4(v4)
|
IpNetwork::V4(v4)
|
||||||
@ -325,6 +337,7 @@ enum IpNetworkIteratorInner {
|
|||||||
V6(Ipv6NetworkIterator),
|
V6(Ipv6NetworkIterator),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct IpNetworkIterator {
|
pub struct IpNetworkIterator {
|
||||||
inner: IpNetworkIteratorInner,
|
inner: IpNetworkIteratorInner,
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user