CIDR Block
A Rust library for CIDR block manipulation that provides utilities to parse, validate, and manipulate CIDR notation and get individual IP addresses.
Features
- Calculate the number of IP addresses in IPv4 and IPv6 CIDR blocks
- Generate the next IP address within a CIDR block with wrapping behavior
- Support for both IPv4 and IPv6 address families
- Trait-based extensions for IP address types
- Error handling for invalid CIDR blocks and prefix lengths
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Or use the cargo command:
Usage Examples
IPv4 Examples
Calculate the number of IP addresses in a CIDR block
use v4_num_ips;
Get the next IP address in a CIDR block
use next_ip_v4;
use Ipv4Addr;
Using the trait-based API
use IPExt;
use Ipv4Addr;
IPv6 Examples
Calculate the number of IP addresses in a CIDR block
use v6_num_ips;
Get the next IP address in a CIDR block
use next_ip_v6;
use Ipv6Addr;
Using the trait-based API
use IPExt;
use Ipv6Addr;
API Reference
IPv4 Functions
v4_num_ips(block: &str) -> Result<u32, Error>
Calculates the number of IP addresses in an IPv4 CIDR block.
- Parameters:
block: A string slice that represents the IPv4 CIDR block (e.g., "192.168.0.0/24").
- Returns:
Ok(u32): The number of IP addresses in the block.Err(Error): If the block format is invalid or if the prefix length is out of range.
next_ip_v4(ip: Ipv4Addr, prefix_length: u8, n: u32) -> Result<Ipv4Addr, Error>
Calculates the next IPv4 address within the same CIDR block, wrapping around if necessary.
- Parameters:
ip: AnIpv4Addrthat represents the starting IPv4 address.prefix_length: The prefix length of the CIDR block (e.g., 24 for a /24 network).n: The number of addresses to skip.
- Returns:
Ok(Ipv4Addr): The next IPv4 address within the CIDR block.Err(Error): If the prefix length is invalid (greater than 32).
IPv6 Functions
v6_num_ips(block: &str) -> Result<u128, Error>
Calculates the number of IP addresses in an IPv6 CIDR block.
- Parameters:
block: A string slice that represents the IPv6 CIDR block (e.g., "2001:db8::/64").
- Returns:
Ok(u128): The number of IP addresses in the block.Err(Error): If the block format is invalid or if the prefix length is out of range.
next_ip_v6(ip: Ipv6Addr, prefix_length: u8, n: u128) -> Result<Ipv6Addr, Error>
Calculates the next IPv6 address within the same CIDR block, wrapping around if necessary.
- Parameters:
ip: AnIpv6Addrthat represents the starting IPv6 address.prefix_length: The prefix length of the CIDR block (e.g., 64 for a /64 network).n: The number of addresses to skip.
- Returns:
Ok(Ipv6Addr): The next IPv6 address within the CIDR block.Err(Error): If the prefix length is invalid (greater than 128).
Trait-based API
IPExt Trait
The IPExt trait provides a unified interface for both IPv4 and IPv6 address types.
This trait is implemented for both Ipv4Addr and Ipv6Addr types, allowing for a more ergonomic API when working with different IP address families.
Error Types
Error Enum
- InvalidBlock: Returned when the CIDR block format is invalid.
- InvalidPrefixLength: Returned when the prefix length is out of range (greater than 32 for IPv4 or greater than 128 for IPv6).
License
This project is licensed under the MIT License - see the LICENSE file for details.