fire_scope/parse_ipv4.rs
1/// IPv4 の範囲をサブネットに分割する際に、
2/// CIDR ブロックをどのサイズで切るか決めるためのユーティリティ。
3
4/// 範囲[current, end]の中で取れる最大の CIDR ブロックサイズを返す。
5pub fn largest_ipv4_block(current: u32, end: u32) -> u8 {
6 let tz = current.trailing_zeros();
7 let span = (end - current + 1).ilog2_sub1();
8 let max_block = tz.min(span);
9 (32 - max_block) as u8
10}
11
12/// u32用のヘルパートレイト。
13/// RIRが出力するIPv4範囲の計算に利用する。
14pub trait ILog2Sub1 {
15 fn ilog2_sub1(&self) -> u32;
16}
17
18impl ILog2Sub1 for u32 {
19 fn ilog2_sub1(&self) -> u32 {
20 if *self == 0 {
21 0
22 } else {
23 31 - self.leading_zeros()
24 }
25 }
26}