#cidr #ipv6 #ipv4 #ip #networking

cidr-utils

本库提供了处理IPv4 CIDR和IPv6 CIDR的函数。

31个版本

0.6.1 2023年11月29日
0.5.11 2023年9月8日
0.5.10 2022年12月31日
0.5.9 2022年11月2日
0.3.5 2019年7月28日

#64 in 网络编程

Download history 17197/week @ 2024-03-14 19752/week @ 2024-03-21 19476/week @ 2024-03-28 20554/week @ 2024-04-04 19567/week @ 2024-04-11 17876/week @ 2024-04-18 17364/week @ 2024-04-25 16684/week @ 2024-05-02 15180/week @ 2024-05-09 14173/week @ 2024-05-16 13380/week @ 2024-05-23 13457/week @ 2024-05-30 11922/week @ 2024-06-06 12870/week @ 2024-06-13 11674/week @ 2024-06-20 11595/week @ 2024-06-27

50,147 每月下载量
27 crate(22个直接)中使用

MIT 许可证

46KB
1K SLoC

CIDR Utils

CI

本库提供了处理IPv4 CIDR和IPv6 CIDR的函数。

示例

将子网合并为超网。

use std::str::FromStr;

use cidr::Ipv4Cidr;
use cidr_utils::Ipv4CidrSize;
use cidr_utils::combiner::Ipv4CidrCombiner;

let mut combiner = Ipv4CidrCombiner::new();

combiner.push(Ipv4Cidr::from_str("192.168.51.100").unwrap());

assert_eq!(1, combiner.len());
assert_eq!("192.168.51.100".to_string(), combiner[0].to_string());

combiner.push(Ipv4Cidr::from_str("192.168.51.101").unwrap());

assert_eq!(1, combiner.len());
assert_eq!("192.168.51.100/31".to_string(), combiner[0].to_string());

combiner.push(Ipv4Cidr::from_str("192.168.51.102").unwrap());

assert_eq!(2, combiner.len());
assert_eq!("192.168.51.100/31".to_string(), combiner[0].to_string());
assert_eq!("192.168.51.102".to_string(), combiner[1].to_string());

combiner.push(Ipv4Cidr::from_str("192.168.51.103").unwrap());

assert_eq!(1, combiner.len());
assert_eq!("192.168.51.100/30".to_string(), combiner[0].to_string());

assert_eq!(true, combiner.contains(&[192, 168, 51, 102].into()));
assert_eq!(false, combiner.contains(&[192, 168, 51, 105].into()));

assert_eq!(4, combiner.size());

将网络分割为子网。

use std::str::FromStr;

use cidr::Ipv4Cidr;
use cidr_utils::Ipv4CidrSize;
use cidr_utils::separator::Ipv4CidrSeparator;

let cidr = Ipv4Cidr::from_str("192.168.56.0/24").unwrap();

let result = Ipv4CidrSeparator::divide_by(&cidr, 4).unwrap();

assert_eq!(4, result.len());
assert_eq!(64, result[0].size());
assert_eq!(64, result[1].size());
assert_eq!(64, result[2].size());
assert_eq!(64, result[3].size());

assert_eq!("[192.168.56.0/26]".to_string(), result[0].to_string());
assert_eq!("[192.168.56.64/26]".to_string(), result[1].to_string());
assert_eq!("[192.168.56.128/26]".to_string(), result[2].to_string());
assert_eq!("[192.168.56.192/26]".to_string(), result[3].to_string());

let result = Ipv4CidrSeparator::divide_by(&cidr, 5).unwrap();

assert_eq!(5, result.len());
assert_eq!(51, result[0].size());
assert_eq!(51, result[1].size());
assert_eq!(51, result[2].size());
assert_eq!(51, result[3].size());
assert_eq!(52, result[4].size());

assert_eq!("[192.168.56.0/27, 192.168.56.32/28, 192.168.56.48/31, 192.168.56.50/32]".to_string(), result[0].to_string());
assert_eq!("[192.168.56.51/32, 192.168.56.52/30, 192.168.56.56/29, 192.168.56.64/27, 192.168.56.96/30, 192.168.56.100/31]".to_string(), result[1].to_string());
assert_eq!("[192.168.56.102/31, 192.168.56.104/29, 192.168.56.112/28, 192.168.56.128/28, 192.168.56.144/29, 192.168.56.152/32]".to_string(), result[2].to_string());
assert_eq!("[192.168.56.153/32, 192.168.56.154/31, 192.168.56.156/30, 192.168.56.160/27, 192.168.56.192/29, 192.168.56.200/30]".to_string(), result[3].to_string());
assert_eq!("[192.168.56.204/30, 192.168.56.208/28, 192.168.56.224/27]".to_string(), result[4].to_string());

let result = Ipv4CidrSeparator::sub_networks(&cidr, 26).unwrap();

assert_eq!(4, result.len());
assert_eq!(64, result[0].size());
assert_eq!(64, result[1].size());
assert_eq!(64, result[2].size());
assert_eq!(64, result[3].size());

assert_eq!("192.168.56.0/26".to_string(), result[0].to_string());
assert_eq!("192.168.56.64/26".to_string(), result[1].to_string());
assert_eq!("192.168.56.128/26".to_string(), result[2].to_string());
assert_eq!("192.168.56.192/26".to_string(), result[3].to_string());

Crates.io

https://crates.io/crates/cidr-utils

文档

https://docs.rs/cidr-utils

许可证

MIT

依赖项

~670KB
~17K SLoC