Skip to main content

qualia_core_db/net/peer/connectivity/
policy.rs

1//! Disclosure and path-selection policy. Applied *before* any probe.
2
3/// Whether the operation may disclose a direct locator to a counterpart.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Disclosure {
6    /// Counterpart may learn an IP locator. Direct ICE gathering is allowed.
7    DirectPermitted,
8    /// No direct probes, candidate export, LAN broadcast, or public STUN.
9    ApprovedRelaysOnly,
10    /// Independently operated hops plus a reviewed metadata construction.
11    QualifiedMultiHop,
12    /// Local authorised routes and sealed store-and-forward only.
13    Isolated,
14}
15
16/// Connectivity profile selected before sockets open.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct PathPolicy {
19    pub disclosure: Disclosure,
20    pub ipv6_preferred: bool,
21    pub ipv4_prompt_ms: u16,
22    pub relay_immediate: bool,
23    pub setup_deadline_ms: u32,
24    pub first_direct_round_ms: u32,
25}
26
27impl PathPolicy {
28    pub const ORDINARY: Self = Self {
29        disclosure: Disclosure::DirectPermitted,
30        ipv6_preferred: true,
31        ipv4_prompt_ms: 250,
32        relay_immediate: true,
33        setup_deadline_ms: 10_000,
34        first_direct_round_ms: 3_000,
35    };
36
37    pub const RELAY_ONLY: Self = Self {
38        disclosure: Disclosure::ApprovedRelaysOnly,
39        ipv6_preferred: true,
40        ipv4_prompt_ms: 250,
41        relay_immediate: true,
42        setup_deadline_ms: 10_000,
43        first_direct_round_ms: 3_000,
44    };
45
46    pub const ISOLATED: Self = Self {
47        disclosure: Disclosure::Isolated,
48        ipv6_preferred: true,
49        ipv4_prompt_ms: 250,
50        relay_immediate: false,
51        setup_deadline_ms: 10_000,
52        first_direct_round_ms: 3_000,
53    };
54
55    /// Direct host/srflx gathering is forbidden.
56    pub const fn allows_direct_probes(self) -> bool {
57        matches!(self.disclosure, Disclosure::DirectPermitted)
58    }
59
60    /// Public STUN toward third-party servers is forbidden.
61    pub const fn allows_public_stun(self) -> bool {
62        self.allows_direct_probes()
63    }
64
65    /// `PathPolicy::allows_relay`.
66    pub const fn allows_relay(self) -> bool {
67        !matches!(self.disclosure, Disclosure::Isolated)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn relay_only_forbids_direct_and_stun() {
77        assert!(!PathPolicy::RELAY_ONLY.allows_direct_probes());
78        assert!(!PathPolicy::RELAY_ONLY.allows_public_stun());
79        assert!(PathPolicy::RELAY_ONLY.allows_relay());
80    }
81
82    #[test]
83    fn isolated_forbids_external_discovery() {
84        assert!(!PathPolicy::ISOLATED.allows_direct_probes());
85        assert!(!PathPolicy::ISOLATED.allows_relay());
86    }
87}