Codexportfolio intelligence

@philiprehberger/rs-ip-utils

IP address utilities — CIDR matching, subnet calculation, classification, and anonymization

Rustcrates.io

Capabilities

README

rs-ip-utils

CI Crates.io Last updated

IP address utilities — CIDR matching, subnet calculation, classification, and anonymization

Installation

[dependencies]
philiprehberger-ip-utils = "0.2.0"

Usage

use philiprehberger_ip_utils::{Cidr, IpClassify};
use std::net::IpAddr;

// CIDR matching
let cidr = Cidr::parse("192.168.1.0/24").unwrap();
let ip: IpAddr = "192.168.1.100".parse().unwrap();
assert!(cidr.contains(ip));

// Subnet info
assert_eq!(cidr.network_address(), "192.168.1.0".parse::<IpAddr>().unwrap());
assert_eq!(cidr.host_count(), 256);

IP classification

use philiprehberger_ip_utils::IpClassify;
use std::net::IpAddr;

let ip: IpAddr = "10.0.0.1".parse().unwrap();
assert!(ip.is_private());
assert!(!ip.is_global_ip());

let public: IpAddr = "8.8.8.8".parse().unwrap();
assert!(public.is_global_ip());

Anonymization

use philiprehberger_ip_utils::anonymize;

let ip: std::net::IpAddr = "192.168.1.100".parse().unwrap();
let anon = anonymize(ip, 8); // zero last 8 bits
assert_eq!(anon.to_string(), "192.168.1.0");

API

Function / TypeDescription
Cidr::parse(s)Parse CIDR notation
.contains(ip)Check if IP is in range
.network_address()First address in range
.broadcast_address()Last address (IPv4)
.host_count()Number of addresses
.subnet_mask()Subnet mask (IPv4)
.overlaps(other)Check if CIDRs overlap
IpClassify traitIP classification methods
.is_bogon()Check if address is a bogon/martian (non-globally-routable)
anonymize(ip, bits)Zero last N bits
aggregate(cidrs)Merge overlapping CIDRs

Development

cargo test
cargo clippy -- -D warnings

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT