A library to work with CIDRs in rust

Overview

ipnetwork

This is a library to work with IPv4 and IPv6 CIDRs in Rust

Build Status Merit Badge

Run Clippy by doing

rustup component add clippy
cargo clippy

Installation

This crate works with Cargo. Assuming you have Rust and Cargo installed, simply check out the source and run tests:

git clone https://github.com/achanda/ipnetwork
cd ipnetwork
cargo test

You can also add ipnetwork as a dependency to your project's Cargo.toml:

[dependencies]
ipnetwork = "*"
Comments
  • Serde serialize/deserialize

    Serde serialize/deserialize

    There's some confusion around serialization/deserialization, added some test cases which can serve as an example.

    If we wanted to change how we go from string/to json we can implement the Serialize and Deserialize trait. I can see how some people expect it to deserialize and serialize from a single string and not an object.

    Edit: In https://github.com/achanda/ipnetwork/issues/73, we now want to serialize/deserialize from a string, Serialize and Deserialize traits are implemented in this PR

    opened by sharksforarms 9
  • Should serde support be optional?

    Should serde support be optional?

    The serde support is not optional though someone might not need it. Serde (and especially serde-derive) is somewhat heavy dependency and it is quite common to have serde as a feature flag on many crates.

    Would it be OK if I add the support?

    opened by vorner 7
  • Serde support doesn't work as expected

    Serde support doesn't work as expected

    For example, if you get a JSON payload with network addresses like "10.0.0.0/24" or "fd00::/64" it won't be deserialised to IpNetwork. A quick look at the code confirms this. derive(Serialize, Deserialize) doesn't cut it here. Those traits will need to be implemented manually.

    bug 
    opened by rushmorem 7
  • Use `slice::iter` instead of `into_iter` to avoid future breakage

    Use `slice::iter` instead of `into_iter` to avoid future breakage

    an_array.into_iter() currently just works because of the autoref feature, which then calls <[T] as IntoIterator>::into_iter. But in the future, arrays will implement IntoIterator, too. In order to avoid problems in the future, the call is replaced by iter() which is shorter and more explicit.

    A crater run showed that your crate is affected by a potential future change. See https://github.com/rust-lang/rust/pull/65819 for more information.

    opened by LukasKalbertodt 4
  • fn size() on Ipv4Network returns an u64?

    fn size() on Ipv4Network returns an u64?

    https://docs.rs/ipnetwork/0.13.0/ipnetwork/struct.Ipv4Network.html#method.size

    Is it safe to change this method to return a u32 since IPv4 networks can never be bigger than 32 bits? Would you mind if I sent a PR that introduces a breaking change to this API? It would help me cut a cast out of my code.

    opened by stusmall 4
  • Error checking, add: size nth, fix mask bug

    Error checking, add: size nth, fix mask bug

    I combined three things in one PR here. Just tell me if you want me to split it up and/or if you don't want some of it.

    • fixed a bug in Ipv4Network::mask that resulted in a shift error when using /32 networks.
    • Added size and nth methods to Ipv4Network since I needed them. I hope you will find it useful.
    • Remade the public API so that any creation of any of the three network types return a Result<Ip*Network, IpNetworkError> instead of the network directly. The reason is that all of them allow inputs that can be invalid. This is a pretty large change so I'm not sure if you want it or not. If merged, this probably need a version bump to 0.7.0.
    opened by faern 4
  • Parsing from ip/mask string

    Parsing from ip/mask string

    Currently the Ipv4Network can be parsed from ip/prefix representation (eg. 192.168.2.1/24), but the older ip/mask (192.168.2.1/255.255.255.0) is rejected. Would it make sense to add such support too?

    opened by vorner 3
  • add From<IpAddr> for IpNetwork

    add From for IpNetwork

    This adds From traits so one can conveniently convert an IpAddr to an IpNetwork containing a single address without checking if it's an IPv4 or IPv6 address.

    opened by nrdmn 3
  • Split up growing library

    Split up growing library

    The main file, lib.rs is growing quite large. A lot of IPv4 and IPv6 stuff is more or less interleaved. I think this library would benefit from being split up into smaller files. Do you agree @achanda ?

    If so, how do we do this? Simplest solution would be to just create ipv4.rs and ipv6.rs and move the respective code (and tests) into those. How about the common stuff? Some stuff is shared between the two versions and the more code they can share the better. Just put it in a common.rs? lib.rs can just reexport everything so the public API is the same as today.

    I can start working on this, but I want to discuss what you think first.

    opened by faern 3
  • Add size and nth to Ipv4Network

    Add size and nth to Ipv4Network

    Possible to get the size of the network and the Nth host address from it.

    Designed slightly different than in my previous PR where I included three things in one. Now I regard a /32 net as having size 0 instead of 1 as in the previous version. This should be more correct.

    opened by faern 3
  • Add from_cidr to parse strs

    Add from_cidr to parse strs

    Work in progress, not ready to merge. Just want to get feedback at this stage.

    I need to parse CIDRs from strings, do you think that's a good addition to the library?

    opened by faern 3
  • Version 1.0

    Version 1.0

    As far as I can tell, the public API of this crate has stayed backwards compatible for quite a few versions already. This means that releasing a 1.0 version in the current form would allow to add new features (in 1.x) versions without sacrificing backwards compatibility.

    This, in turn, would make it easier for crates such as diesel and sqlx to keep an up to date ipnetwork dependency without having to release a new version themselves whenever a new ipnetwork version is released.

    Is there something preventing the release of a 1.0 version?

    opened by Razican 0
  • is_supernet_of does not work properly

    is_supernet_of does not work properly

    Hi

    use ipnetwork::Ipv4Network;
    let net: Ipv4Network = "10.1.0.0/16".parse().unwrap();
    let net2: Ipv4Network = "10.1.0.0/15".parse().unwrap();
    println!("{:?} {} {}", net2, net.is_supernet_of(net2),
        net.is_supernet_of(Ipv4Network::new(net2.network(), net2.prefix()).unwrap()));
    

    Output: Ipv4Network { addr: 10.1.0.0, prefix: 15 } true false

    So, is_supernet_of accidentally thinks that /16 is a supernet of /15 if network address is the same.

    PS: Prefix "X/Y" consists of network address X and bits Y. So, function prefix which return bits confuses any network engineer.

    opened by mbasunov 0
  • Ipv6Network::nth

    Ipv6Network::nth

    nth method is implemented for Ipv4Network but not for Ipv6Network. Is it due to some fundamental difference between IP4/IP6 neworks or just forgotten?

    opened by kriomant 0
  • [Change suggestion] Resetting the device bits to 0 when initiating new objects

    [Change suggestion] Resetting the device bits to 0 when initiating new objects

    When creating an IP network, the network address's device bits should be resets to zero and then save into the struct.

    For example, I believe the following two networks should be considered the same: 8.8.8.0/24 and 8.8.8.1/24. But when the resulting IpNetwork are different, and the to_string results are also different.

    opened by digizeph 0
Releases(v0.20.0)
Owner
Abhishek Chanda
FooBarBaz
Abhishek Chanda
The gRPC library for Rust built on C Core library and futures

gRPC-rs gRPC-rs is a Rust wrapper of gRPC Core. gRPC is a high performance, open source universal RPC framework that puts mobile and HTTP/2 first. Sta

TiKV Project 1.6k Jan 7, 2023
A µTP (Micro/uTorrent Transport Library) library implemented in Rust

rust-utp A Micro Transport Protocol library implemented in Rust. API documentation Overview The Micro Transport Protocol is a reliable transport proto

Ricardo Martins 134 Dec 11, 2022
Nanomsg library for Rust

Nanomsg Documentation Nanomsg is a modern messaging library that is the successor to ZeroMQ, written in C by Martin Sustrik and colleagues. The nanoms

Daniel Fagnan 371 Nov 18, 2022
A Constrained Application Protocol(CoAP) library implemented in Rust.

coap-rs A fast and stable Constrained Application Protocol(CoAP) library implemented in Rust. Features: CoAP core protocol RFC 7252 CoAP Observe optio

Covertness 170 Dec 19, 2022
Backroll is a pure Rust implementation of GGPO rollback networking library.

backroll-rs Backroll is a pure Rust implementation of GGPO rollback networking library. Development Status This is still in an untested alpha stage. A

Hourai Teahouse 273 Dec 28, 2022
A Rust library for parsing the SOME/IP network protocol (without payload interpretation).

someip_parse A Rust library for parsing the SOME/IP network protocol (without payload interpretation). Usage Add the following to your Cargo.toml: [de

Julian Schmid 18 Oct 31, 2022
A library for easily creating WebRTC data channel connections in Rust

Cyberdeck A library for easily creating WebRTC data channel connections in Rust.

RICHΛRD ΛNΛYΛ 34 Nov 10, 2022
Modrinth API is a simple library for using Modrinth's API in Rust projects

Ferinth is a simple library for using the Modrinth API in Rust projects. It uses reqwest as its HTTP(S) client and deserialises responses to typed structs using serde.

null 20 Dec 8, 2022
An LV2 host library for Rust.

Livi A library for hosting LV2 plugins. Note: This is a work in progress and has not yet been full tested. Supported LV2 Features LV2 has a simple cor

Will 11 Nov 8, 2022
A Rust compiler plugin and support library to annotate overflow behavior

overflower This project contains a compiler plugin and supporting library to allow the programmer to annotate their code to declare how integer overfl

null 104 Nov 28, 2022
Dav-server-rs - Rust WebDAV server library. A fork of the webdav-handler crate.

dav-server-rs A fork of the webdav-handler-rs project. Generic async HTTP/Webdav handler Webdav (RFC4918) is defined as HTTP (GET/HEAD/PUT/DELETE) plu

messense 30 Dec 29, 2022
Peer-to-peer communications library for Rust based on QUIC protocol

qp2p Crate Documentation MaidSafe website SAFE Dev Forum SAFE Network Forum Overview This library provides an API to simplify common tasks when creati

MaidSafe 337 Dec 14, 2022
A BitTorrent V1 engine library for Rust (and currently Linux)

cratetorrent Cratetorrent is a Rust crate implementing the BitTorrent version 1 protocol. It can be used as a library and also provides a simple examp

null 401 Dec 28, 2022
Rust library that helps you change the domain of the link to another domain 🦀🔐

Rust library that helps you to change the domain of the link to another domain, the library helps with privacy. It can be used to change the domain of sites that do not care about privacy to another that does.

TheAwiteb 2 Mar 28, 2022
A generic Rust based Bigtable connection library implemented using gRPC

A generic Rust based Bigtable connection library refactored out the solana mono-repo so that can be shared for different applications.

Lijun Wang 3 Sep 25, 2022
A high performance/low-overhead OpenMetrics library for Rust

* * * EXPERIMENTAL * * * discreet-metrics A high-performance/low-overhead metrics library aiming to conform with OpenMetrics and to satisfy the follow

null 2 Sep 14, 2022
A library for writing type-safe Durable Objects in Rust.

do-proxy A library for writing type-safe Durable Objects (DOs) in Rust. With do-proxy you can: Easily write type-safe APIs for Durable Objects. Abstra

Fisher Darling 12 Dec 4, 2022
📡 Rust mDNS library designed with user interfaces in mind

?? Searchlight Searchlight is an mDNS server & client library designed to be simple, lightweight and easy to use, even if you just have basic knowledg

William 5 Jan 8, 2023
This is a UPnP client library for Rust.

UPnP Client This is a UPNP client library for Rust. Usage Add this to your Cargo.toml: [dependencies] upnp-client = "0.1" Example This example will pr

Tsiry Sandratraina 7 Feb 20, 2023