ipnet

IpNet, Ipv4Net, and Ipv6Net types for working with IP network addresses (CIDR) in Rust.

Library
Cargo
v2.12.1
169stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
59/100Fair
Development Activity56
Maintenance36
Community64
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
80/100Excellent
Architecture80
Code Quality82
Innovation68
Learning Curve88

ipnet provides IpNet, Ipv4Net, and Ipv6Net types for representing and manipulating IP network addresses (CIDR prefixes) in Rust, built directly on top of the standard library’s IpAddr, Ipv4Addr, and Ipv6Addr types to stay API-consistent with them. It adds subnet and address-range iterators (Ipv4Subnets, Ipv6Subnets, Ipv4AddrRange, Ipv6AddrRange) plus extension traits giving Add/Sub/BitAnd/BitOr arithmetic on IP addresses that the standard library omits.

The crate only uses stable Rust features, targets no_std environments via an optional std feature, and supports optional serde serialization (with a heapless variant to avoid dynamic allocation) and schemars JSON Schema generation — making it a common low-level building block for networking, firewall, and infrastructure tooling written in Rust.

What You Get

  • IpNet, Ipv4Net, and Ipv6Net types for representing CIDR network prefixes, parseable from strings ("10.1.1.0/24".parse()) and usable in const contexts
  • Ipv4Subnets/Ipv6Subnets iterators for enumerating subnets within an address range, and Ipv4AddrRange/Ipv6AddrRange for iterating individual addresses
  • Extension traits adding Add, Sub, BitAnd, and BitOr operations to Ipv4Addr/Ipv6Addr, which the standard library doesn’t provide
  • Optional serde (with heapless no-alloc variant) and schemars (JSON Schema, v0.8 or v1) integration behind feature flags

Common Use Cases

  • Parsing and validating CIDR blocks in network configuration, firewall rules, or infrastructure-as-code tooling written in Rust
  • Enumerating all subnets or hosts within an IP range for allocation, scanning, or reporting tools
  • Serializing/deserializing network address ranges to/from JSON or config formats via the optional serde feature
  • Embedded or no_std networking code that needs CIDR arithmetic without pulling in the full standard library

Under The Hood

Architecture — the crate splits cleanly by concern: src/ipnet.rs (2,001 lines) defines the core IpNet/Ipv4Net/Ipv6Net types and their subnet/range iterator logic; src/parser.rs handles CIDR string parsing; src/mask.rs implements netmask/hostmask bit manipulation; src/ipext.rs (989 lines) adds the Add/Sub/BitAnd/BitOr extension traits onto std::net::Ipv4Addr/Ipv6Addr; and src/ipnet_serde.rs/ipnet_schemars_08.rs/ipnet_schemars_1.rs are optional, feature-gated integration shims kept isolated from the core so the zero-dependency default build stays lean. Tech Stack — pure Rust, 2018 edition, using only stable-toolchain features (no nightly-only APIs), with optional dependencies on serde (derive), schemars (v0.8 or v1, mutually exclusive), and heapless for allocation-free serialization; a std feature flag (on by default) gates the parts requiring the standard library, keeping a no_std path viable. Code Quality — the README states tests aim for thorough coverage across both dedicated test modules and doctests embedded in the public API’s documentation; the code is stable and mature (297 total commits, MSRV frozen at Rust 1.26+ since the 2.0 rewrite that replaced a custom 128-bit integer emulation with native u128 support). API Design — types deliberately mirror the standard library’s IpAddr/Ipv4Addr/Ipv6Addr naming and construction patterns (new(), from_str(), TryFrom), including const-compatible constructors (new_assert) added for compile-time network definitions, so Rust developers already familiar with std::net face almost no additional learning curve.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search