20 Commits

Author SHA1 Message Date
0b98eecf6a Merge pull request #134 from achanda/v0.17.0
Cut a new release
2020-07-13 11:47:44 +01:00
6c6bdb333d Cut a new release 2020-07-13 11:45:28 +01:00
eded5f13a9 Merge pull request #133 from jethrogb/patch-1
Update serde version
2020-07-13 11:39:35 +01:00
1d4665dfe6 Update serde version
This crate doesn't actually work with serde 0.8 or 0.9.
2020-07-13 12:01:48 +02:00
e80eab0c80 Merge pull request #131 from tshepang/patch-1
Update README.md
2020-06-06 14:09:43 +01:00
8f1c6b59bb Update README.md
nits
2020-06-06 14:48:15 +02:00
a9f4547c82 Merge pull request #128 from achanda/dual-license
Dual license this project
2020-02-26 16:35:36 +00:00
fa128680b5 Dual license this project
closes #127
2020-02-26 13:47:18 +00:00
620ec4d42c Merge pull request #123 from achanda/bench-speedup
Isolate the contains call in bench
2020-01-18 18:48:51 +00:00
b6397ca2bc Isolate the contains call in bench 2020-01-18 18:42:49 +00:00
c0257fc59d Merge pull request #122 from achanda/clippy-lints
Address all changes clippy pointed out
2020-01-18 17:59:43 +00:00
45fbf458b3 Address all changes clippy pointed out 2020-01-18 17:53:15 +00:00
ca1d332e71 Merge pull request #121 from achanda/clippy
Remove the clippy plugin
2020-01-18 17:47:04 +00:00
121820add8 Drop clippy from dependencies 2020-01-18 17:41:43 +00:00
65bd6724c2 Remove the clippy plugin
And update instructions to run clippy from cargo
2020-01-18 17:39:07 +00:00
41e0f8a8a3 Merge pull request #120 from achanda/forbid
Add a few deny lints
2020-01-18 11:16:59 +00:00
f23ea4d7d6 Add a few deny lints
And conform to the new ones
2020-01-18 11:03:37 +00:00
fc958f16dc Merge pull request #119 from thepacketgeek/master
Update README for IPv6 support
2020-01-17 20:12:23 +00:00
23b95c3b85 Update README for IPv6 support
IPv6 looks implemented, cleaning up the README comment
2020-01-17 11:22:52 -08:00
38460c0598 Merge pull request #117 from achanda/release-0.16
Uprev for a new release
2020-01-08 10:42:56 +00:00
9 changed files with 59 additions and 31 deletions

View File

@ -1,9 +1,9 @@
[package]
name = "ipnetwork"
version = "0.16.0" # When updating version, also modify html_root_url in the lib.rs
version = "0.17.0" # When updating version, also modify html_root_url in the lib.rs
authors = ["Abhishek Chanda <abhishek.becs@gmail.com>", "Linus Färnstrand <faern@faern.net>"]
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"
keywords = ["network", "ip", "address", "cidr"]
readme = "README.md"
@ -11,12 +11,11 @@ categories = ["network-programming", "parser-implementations"]
edition = "2018"
[dependencies]
clippy = {version = "0.0.302", optional = true}
serde = { version = ">=0.8.0, <2.0", optional = true }
serde = { version = "1", optional = true }
[dev-dependencies]
serde_json = "1.0"
serde_derive = ">=0.8.0, <2.0"
serde_derive = "1"
criterion = "0.3.0"
[badges]
@ -25,7 +24,6 @@ maintenance = { status = "passively-maintained" }
[features]
default = ["serde"]
dev = ["clippy"]
[[bench]]
name = "parse_bench"

25
LICENSE-MIT.md Normal file
View 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.

View File

@ -1,14 +1,14 @@
ipnetwork
===
This is a library to work with IPv4 and v6 CIDRs in rust
The IPv4 implementation is stable, IPv6 implementation is not done yet.
This is a library to work with IPv4 and IPv6 CIDRs in Rust
[![Build Status](https://travis-ci.org/achanda/ipnetwork.svg?branch=master)](https://travis-ci.org/achanda/ipnetwork)
[![Merit Badge](http://meritbadge.herokuapp.com/ipnetwork)](https://crates.io/crates/ipnetwork)
Run Clippy by doing
```
cargo test --features "dev"
rustup component add clippy
cargo clippy
```
### Installation
@ -20,7 +20,7 @@ cargo test
```
You can also add `ipnetwork` as a dependency to your project's `Cargo.toml`:
```
```toml
[dependencies]
ipnetwork = "*"
```

View File

@ -21,18 +21,18 @@ fn parse_ipv4_netmask_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| {
b.iter(|| {
let cidr = "74.125.227.0/25".parse::<Ipv4Network>().unwrap();
cidr.contains(Ipv4Addr::new(74, 125, 227, 4))
})
});
}
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| {
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))
})
});

View File

@ -36,17 +36,17 @@ pub fn cidr_parts(cidr: &str) -> Result<(&str, Option<&str>), IpNetworkError> {
let (ip, prefix) = cidr.split_at(sep);
// Error if cidr has multiple slashes
if prefix[1..].find('/').is_some() {
return Err(IpNetworkError::InvalidCidrFormat(format!(
Err(IpNetworkError::InvalidCidrFormat(format!(
"CIDR must contain a single '/': {}",
cidr
)));
)))
} else {
// Handle the case when cidr has exactly one slash
return Ok((ip, Some(&prefix[1..])));
Ok((ip, Some(&prefix[1..])))
}
} else {
// Handle the case when cidr does not have a slash
return Ok((cidr, None));
Ok((cidr, None))
}
}

View File

@ -61,7 +61,7 @@ impl Ipv4Network {
/// 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
/// addresses.
pub fn iter(&self) -> Ipv4NetworkIterator {
pub fn iter(self) -> Ipv4NetworkIterator {
let start = u32::from(self.network());
let end = start + (self.size() - 1);
Ipv4NetworkIterator {
@ -70,11 +70,11 @@ impl Ipv4Network {
}
}
pub fn ip(&self) -> Ipv4Addr {
pub fn ip(self) -> Ipv4Addr {
self.addr
}
pub fn prefix(&self) -> u8 {
pub fn prefix(self) -> u8 {
self.prefix
}
@ -109,7 +109,7 @@ impl Ipv4Network {
/// let net: Ipv4Network = "127.0.0.0/16".parse().unwrap();
/// 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 mask = !(0xffff_ffff as u64 >> prefix) as u32;
Ipv4Addr::from(mask)
@ -127,7 +127,7 @@ impl Ipv4Network {
/// let net: Ipv4Network = "10.1.9.32/16".parse().unwrap();
/// 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 ip = u32::from(self.addr) & mask;
Ipv4Addr::from(ip)
@ -145,7 +145,7 @@ impl Ipv4Network {
/// let net: Ipv4Network = "10.9.0.32/16".parse().unwrap();
/// 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 broadcast = u32::from(self.addr) | !mask;
Ipv4Addr::from(broadcast)
@ -163,7 +163,7 @@ impl Ipv4Network {
/// assert!(net.contains(Ipv4Addr::new(127, 0, 0, 70)));
/// assert!(!net.contains(Ipv4Addr::new(127, 0, 1, 70)));
/// ```
pub fn contains(&self, ip: Ipv4Addr) -> bool {
pub fn contains(self, ip: Ipv4Addr) -> bool {
let mask = !(0xffff_ffff as u64 >> self.prefix) as u32;
let net = u32::from(self.addr) & mask;
(u32::from(ip) & mask) == net
@ -183,7 +183,7 @@ impl Ipv4Network {
/// let tinynet: Ipv4Network = "0.0.0.0/32".parse().unwrap();
/// assert_eq!(tinynet.size(), 1);
/// ```
pub fn size(&self) -> u32 {
pub fn size(self) -> u32 {
let host_bits = u32::from(IPV4_BITS - self.prefix);
(2 as u32).pow(host_bits)
}
@ -205,7 +205,7 @@ impl Ipv4Network {
/// let net2: Ipv4Network = "10.0.0.0/16".parse().unwrap();
/// 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() {
let net = u32::from(self.network());
Some(Ipv4Addr::from(net + n))
@ -263,7 +263,7 @@ impl From<Ipv4Addr> for Ipv4Network {
}
}
#[derive(Clone, Debug)]
#[derive(Copy, Clone, Debug)]
pub struct Ipv4NetworkIterator {
next: Option<u32>,
end: u32,

View File

@ -72,7 +72,7 @@ impl Ipv6Network {
Ipv6NetworkIterator {
next: Some(start),
end: end,
end,
}
}
@ -228,7 +228,7 @@ impl From<Ipv6Addr> for Ipv6Network {
}
}
#[derive(Clone, Debug)]
#[derive(Copy, Clone, Debug)]
pub struct Ipv6NetworkIterator {
next: Option<u128>,
end: u128,

View File

@ -1,9 +1,13 @@
//! The `ipnetwork` crate provides a set of APIs to work with IP CIDRs in
//! Rust.
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
#![crate_type = "lib"]
#![doc(html_root_url = "https://docs.rs/ipnetwork/0.16.0")]
#![doc(html_root_url = "https://docs.rs/ipnetwork/0.17.0")]
#![deny(missing_copy_implementations,
missing_debug_implementations,
unsafe_code,
unused_extern_crates,
unused_import_braces)]
use std::{fmt, net::IpAddr, str::FromStr};
@ -325,6 +329,7 @@ enum IpNetworkIteratorInner {
V6(Ipv6NetworkIterator),
}
#[derive(Clone, Debug)]
pub struct IpNetworkIterator {
inner: IpNetworkIteratorInner,
}