fetch-ccip 0.3.2

CLI application to retrieve the latest address allocation file provided by the RIR and output the IPv4/v6 address blocks corresponding to the specified country code in a text file.
Documentation
/// IPv4 の範囲をサブネットに分割する際に、
/// CIDR ブロックをどのサイズで切るか決めるためのユーティリティ。

/// 範囲[current, end]の中で取れる最大の CIDR ブロックサイズを返す。
/// 例: currentが192.168.0.0(=0xc0a80000), endが192.168.0.255(=0xc0a800ff)なら/24など
pub fn largest_ipv4_block(current: u32, end: u32) -> u8 {
    let tz = current.trailing_zeros();
    let span = (end - current + 1).ilog2_sub1();
    let max_block = tz.min(span);
    (32 - max_block) as u8
}

/// u32用のヘルパートレイト。
/// RIRが出力するIPv4範囲の計算に利用する。
pub trait ILog2Sub1 {
    fn ilog2_sub1(&self) -> u32;
}

impl ILog2Sub1 for u32 {
    fn ilog2_sub1(&self) -> u32 {
        if *self == 0 {
            0
        } else {
            // 2のべき乗の範囲を求めるためのヘルパー
            31 - self.leading_zeros()
        }
    }
}