#subnet #address #cidr #networking

subnet-utils

A Rust subnet utility library

1 unstable release

0.1.0 Dec 2, 2024

#1415 in Network programming

Download history 132/week @ 2024-12-02 11/week @ 2024-12-09

143 downloads per month

MIT license

11KB
114 lines

subnet-utils

Simple IP subnet utilities

Usage

To use subnet-utils, first add this to your Cargo.toml:

[dependencies]
subnet-utils = "0.1"

Examples

Check if subnet contains an address.

use std::net::{IpAddr, Ipv4Addr};
use subnet_utils::addr_in_subnet;

let res = addr_in_subnet(&IpAddr::V4(Ipv4Addr::new(192, 168, 182, 1)), "192.168.182.0/24").unwrap();
assert!(res);

Check if any subnet contains an address.

use std::net::{IpAddr, Ipv4Addr};
use subnet_utils::addr_in_any_subnet;

let subnets = vec!["192.168.181.0/24", "192.168.182.0/24"];
let res = addr_in_any_subnet(&IpAddr::V4(Ipv4Addr::new(192, 168, 182, 1)), &subnets).unwrap();
assert!(res);

Check if all subnets contain an address.

use std::net::{IpAddr, Ipv4Addr};
use subnet_utils::addr_in_all_subnets;

let subnets = vec!["192.168.182.0/24", "192.168.182.1/32"];
let res = addr_in_all_subnets(&IpAddr::V4(Ipv4Addr::new(192, 168, 182, 1)), &subnets).unwrap();
assert!(res);

Check if any subnet contains any address.

use std::net::{IpAddr, Ipv4Addr};
use subnet_utils::any_addr_in_any_subnet;

let addrs = vec![IpAddr::V4(Ipv4Addr::new(192, 168, 182, 1)), IpAddr::V4(Ipv4Addr::new(192, 168, 182, 2))];
let subnets = vec!["192.168.181.0/24", "192.168.182.2/32"];
let res = any_addr_in_any_subnet(&addrs, &subnets).unwrap();
assert!(res);

License

Copyright © 2024 s0s

This software is released under:

Please see the license file (LICENSE.md) for more information.

Dependencies

~71KB