use std::net::Ipv4Addr;
use uuid::Uuid;
pub fn direct_cgnat() -> ipnet::Ipv4Net {
ipnet::Ipv4Net::new(Ipv4Addr::new(100, 64, 0, 0), 10).expect("static CGNAT")
}
pub use self::direct_cgnat as direct_cgnat_net;
const HOST_BITS: u32 = 22;
const HOST_MASK: u32 = (1 << HOST_BITS) - 1;
pub fn derive_ipv4(endpoint_id_hex: &str, collision_index: u8) -> Ipv4Addr {
let mut hasher = blake3::Hasher::new();
hasher.update(endpoint_id_hex.as_bytes());
hasher.update(&[collision_index]);
let hash = hasher.finalize();
let bytes = hash.as_bytes();
let host = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) & HOST_MASK;
let host = if host == 0 {
1
} else if host == HOST_MASK {
HOST_MASK - 1
} else {
host
};
let base: u32 = u32::from(Ipv4Addr::new(100, 64, 0, 0));
Ipv4Addr::from(base | host)
}
pub fn network_id_from_topic(topic_hash_hex: &str) -> Uuid {
let raw = hex::decode(topic_hash_hex).unwrap_or_else(|_| topic_hash_hex.as_bytes().to_vec());
let hash = blake3::hash(&raw);
let b = hash.as_bytes();
Uuid::from_bytes([
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13],
b[14], b[15],
])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn derives_within_cgnat() {
let ip = derive_ipv4(
"aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899",
0,
);
assert!(direct_cgnat().contains(&ip));
assert!(!ip.is_loopback());
assert!(!ip.is_unspecified());
}
#[test]
fn stable_for_same_key() {
let hex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
assert_eq!(derive_ipv4(hex, 0), derive_ipv4(hex, 0));
}
#[test]
fn collision_index_changes_ip() {
let hex = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210";
assert_ne!(derive_ipv4(hex, 0), derive_ipv4(hex, 1));
}
#[test]
fn not_in_rfc1918_10() {
let ip = derive_ipv4(
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
0,
);
let ten: ipnet::Ipv4Net = "10.0.0.0/8".parse().unwrap();
assert!(!ten.contains(&ip));
}
#[test]
fn network_id_stable() {
let t = "aa".repeat(32);
assert_eq!(network_id_from_topic(&t), network_id_from_topic(&t));
}
}