zond_engine/core/models/
ip.rs1use 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}