Skip to main content

hyperlane_plugin_ip_filter/
enum.rs

1use super::*;
2
3/// Canonicalized IPv4 or IPv6 CIDR network block.
4#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5pub enum IpNet {
6    /// IPv4 network represented as `(masked_network, bitmask, prefix_len)`.
7    V4 {
8        /// Masked IPv4 network bits in host byte order.
9        network: u32,
10        /// Contiguous high-bit prefix mask.
11        mask: u32,
12        /// Prefix length in `0..=32`.
13        prefix_len: u8,
14    },
15    /// IPv6 network represented as `(masked_network, bitmask, prefix_len)`.
16    V6 {
17        /// Masked IPv6 network bits in host byte order.
18        network: u128,
19        /// Contiguous high-bit prefix mask.
20        mask: u128,
21        /// Prefix length in `0..=128`.
22        prefix_len: u8,
23    },
24}
25
26/// Enforcement outcome produced by [`CompiledIpFilter::evaluate`].
27#[derive(Clone, Copy, Debug, Eq, PartialEq)]
28pub enum IpFilterAction {
29    /// Client IP passed all allowlist and denylist checks.
30    Allow,
31    /// Client IP was blocked by denylist or missing from non-empty allowlist.
32    Deny,
33    /// Request path matched the configured skip list.
34    Exempt,
35}
36
37/// Errors produced when parsing CIDR strings or compiling filter policies.
38#[derive(Clone, Debug, Eq, PartialEq)]
39pub enum IpFilterError {
40    /// Input string was empty after trimming whitespace.
41    EmptyCidr,
42    /// Address portion could not be parsed as IPv4 or IPv6.
43    InvalidIp(String),
44    /// Prefix length portion could not be parsed as an unsigned integer.
45    InvalidPrefix(String),
46    /// Prefix length exceeded 32 (IPv4) or 128 (IPv6).
47    PrefixOutOfRange(u8),
48}
49
50/// Compact inline-or-radix IPv4 CIDR lookup index inside [`CidrTable`].
51#[derive(Clone, Debug)]
52pub(crate) enum V4CidrIndex {
53    /// No IPv4 CIDR entries.
54    Empty,
55    /// Covers all IPv4 addresses (`0.0.0.0/0` or merged equivalent).
56    Any,
57    /// Up to [`CIDR_INLINE_LIMIT`] sorted disjoint intervals stored inline without heap indirection.
58    Inline {
59        /// Number of valid intervals in `items`.
60        len: u8,
61        /// Inline sorted disjoint `(start, end)` intervals.
62        items: [(u32, u32); CIDR_INLINE_LIMIT],
63    },
64    /// Large table indexed by 256-bit `/8` full-block bitmap and CSR `/8` radix buckets.
65    Radix(Box<V4CidrCsr>),
66}
67
68/// Compact inline-or-radix IPv6 CIDR lookup index inside [`CidrTable`].
69#[derive(Clone, Debug)]
70pub(crate) enum V6CidrIndex {
71    /// No IPv6 CIDR entries.
72    Empty,
73    /// Covers all IPv6 addresses (`::/0` or merged equivalent).
74    Any,
75    /// Up to [`CIDR_INLINE_LIMIT`] sorted disjoint intervals stored inline without heap indirection.
76    Inline {
77        /// Number of valid intervals in `items`.
78        len: u8,
79        /// Inline sorted disjoint `(start, end)` intervals.
80        items: [(u128, u128); CIDR_INLINE_LIMIT],
81    },
82    /// Large table indexed by 256-bit `/8` full-block bitmap and CSR `/8` radix buckets.
83    Radix(Box<V6CidrCsr>),
84}