Skip to main content

zond_engine/core/models/
ip.rs

1// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
2//
3// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4// If a copy of the MPL was not distributed with this file, You can obtain one at
5// https://mozilla.org/MPL/2.0/.
6
7use std::net::{IpAddr, Ipv6Addr};
8
9pub mod range;
10pub mod set;
11
12#[derive(Debug, Default)]
13pub enum Ipv6AddressType {
14    GlobalUnicast,
15    UniqueLocal,
16    LinkLocal,
17    Loopback,
18    #[default]
19    Unspecified,
20}
21
22pub fn get_ipv6_type(ipv6_addr: &Ipv6Addr) -> Ipv6AddressType {
23    match true {
24        _ if is_global_unicast(ipv6_addr) => Ipv6AddressType::GlobalUnicast,
25        _ if ipv6_addr.is_unique_local() => Ipv6AddressType::UniqueLocal,
26        _ if ipv6_addr.is_unicast_link_local() => Ipv6AddressType::LinkLocal,
27        _ if ipv6_addr.is_loopback() => Ipv6AddressType::Loopback,
28        _ => Ipv6AddressType::Unspecified,
29    }
30}
31
32pub fn is_global_unicast(ipv6_addr: &Ipv6Addr) -> bool {
33    let first_byte = ipv6_addr.octets()[0];
34    (0x20..=0x3F).contains(&first_byte)
35}
36
37pub fn is_private(ip_addr: &IpAddr) -> bool {
38    match ip_addr {
39        IpAddr::V4(ipv4) => ipv4.is_private(),
40        IpAddr::V6(ipv6) => ipv6.is_unicast_link_local() || ipv6.is_unique_local(),
41    }
42}