hyperlane-plugin-ip-filter 23.0.0

A zero-allocation IP allowlist/denylist and anti-spoofing client IP extraction plugin for the Hyperlane framework with /8 radix-indexed CIDR lookup, IPv4-mapped IPv6 canonicalization, RFC 7239 Forwarded/X-Forwarded-For right-to-left proxy peeling, and prebuilt 403 wire responses.
Documentation
use super::*;

/// Canonicalized IPv4 or IPv6 CIDR network block.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum IpNet {
    /// IPv4 network represented as `(masked_network, bitmask, prefix_len)`.
    V4 {
        /// Masked IPv4 network bits in host byte order.
        network: u32,
        /// Contiguous high-bit prefix mask.
        mask: u32,
        /// Prefix length in `0..=32`.
        prefix_len: u8,
    },
    /// IPv6 network represented as `(masked_network, bitmask, prefix_len)`.
    V6 {
        /// Masked IPv6 network bits in host byte order.
        network: u128,
        /// Contiguous high-bit prefix mask.
        mask: u128,
        /// Prefix length in `0..=128`.
        prefix_len: u8,
    },
}

/// Enforcement outcome produced by [`CompiledIpFilter::evaluate`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IpFilterAction {
    /// Client IP passed all allowlist and denylist checks.
    Allow,
    /// Client IP was blocked by denylist or missing from non-empty allowlist.
    Deny,
    /// Request path matched the configured skip list.
    Exempt,
}

/// Errors produced when parsing CIDR strings or compiling filter policies.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum IpFilterError {
    /// Input string was empty after trimming whitespace.
    EmptyCidr,
    /// Address portion could not be parsed as IPv4 or IPv6.
    InvalidIp(String),
    /// Prefix length portion could not be parsed as an unsigned integer.
    InvalidPrefix(String),
    /// Prefix length exceeded 32 (IPv4) or 128 (IPv6).
    PrefixOutOfRange(u8),
}

/// Compact inline-or-radix IPv4 CIDR lookup index inside [`CidrTable`].
#[derive(Clone, Debug)]
pub(crate) enum V4CidrIndex {
    /// No IPv4 CIDR entries.
    Empty,
    /// Covers all IPv4 addresses (`0.0.0.0/0` or merged equivalent).
    Any,
    /// Up to [`CIDR_INLINE_LIMIT`] sorted disjoint intervals stored inline without heap indirection.
    Inline {
        /// Number of valid intervals in `items`.
        len: u8,
        /// Inline sorted disjoint `(start, end)` intervals.
        items: [(u32, u32); CIDR_INLINE_LIMIT],
    },
    /// Large table indexed by 256-bit `/8` full-block bitmap and CSR `/8` radix buckets.
    Radix(Box<V4CidrCsr>),
}

/// Compact inline-or-radix IPv6 CIDR lookup index inside [`CidrTable`].
#[derive(Clone, Debug)]
pub(crate) enum V6CidrIndex {
    /// No IPv6 CIDR entries.
    Empty,
    /// Covers all IPv6 addresses (`::/0` or merged equivalent).
    Any,
    /// Up to [`CIDR_INLINE_LIMIT`] sorted disjoint intervals stored inline without heap indirection.
    Inline {
        /// Number of valid intervals in `items`.
        len: u8,
        /// Inline sorted disjoint `(start, end)` intervals.
        items: [(u128, u128); CIDR_INLINE_LIMIT],
    },
    /// Large table indexed by 256-bit `/8` full-block bitmap and CSR `/8` radix buckets.
    Radix(Box<V6CidrCsr>),
}