Skip to main content

qualia_core_db/net/peer/connectivity/
path.rs

1//! Generation-safe send-path activation.
2//!
3//! An old authenticated WireGuard packet is not, by itself, proof that a newly
4//! observed source is a safe current bidirectional route. Relayed traffic keeps
5//! circuit provenance; the relay IP is not a direct peer candidate.
6
7use super::state::{ConnError, ConnSlot, ConnState};
8
9/// ICE consent-to-send timers (RFC 7675). Scheduler objectives, not STUN wire values.
10pub const CONSENT_INTERVAL_MS: u64 = 5_000;
11pub const CONSENT_TIMEOUT_MS: u64 = 30_000;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct ObservedSource {
15    pub generation: u32,
16    pub relay_circuit: bool,
17}
18
19/// Fresh challenge required before changing the send path.
20pub fn may_activate_send_path(
21    slot: &ConnSlot,
22    observed: ObservedSource,
23    fresh_challenge_ok: bool,
24) -> Result<(), ConnError> {
25    if observed.generation != slot.generation {
26        return Err(ConnError::StaleGeneration);
27    }
28    if !fresh_challenge_ok {
29        return Err(ConnError::PolicyDenied);
30    }
31    match slot.state {
32        ConnState::Establishing
33        | ConnState::CarrierReady
34        | ConnState::SessionReady
35        | ConnState::Upgrading
36        | ConnState::Degraded => Ok(()),
37        _ => Err(ConnError::IllegalTransition),
38    }
39}
40
41/// Consent expires if the last successful check is older than [`CONSENT_TIMEOUT_MS`].
42pub const fn consent_fresh(last_ok_ms: u64, now_ms: u64) -> bool {
43    now_ms.saturating_sub(last_ok_ms) < CONSENT_TIMEOUT_MS
44}
45
46/// RFC 8445: the larger tie-breaker remains controlling.
47pub fn resolve_role_conflict(local_tie: u64, remote_tie: u64) -> bool {
48    local_tie > remote_tie
49}
50
51/// Relay IP / circuit is not a host or srflx candidate.
52pub const fn relay_ip_is_not_direct_candidate() -> bool {
53    true
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::net::peer::connectivity::state::ConnSlot;
60
61    #[test]
62    fn old_wg_packet_without_challenge_is_denied() {
63        let mut slot = ConnSlot::new(1);
64        slot.transition(1, ConnState::Establishing).unwrap();
65        slot.transition(1, ConnState::CarrierReady).unwrap();
66        let obs = ObservedSource {
67            generation: 1,
68            relay_circuit: false,
69        };
70        assert_eq!(
71            may_activate_send_path(&slot, obs, false),
72            Err(ConnError::PolicyDenied)
73        );
74        assert!(may_activate_send_path(&slot, obs, true).is_ok());
75        slot.restart();
76        assert_eq!(
77            may_activate_send_path(&slot, obs, true),
78            Err(ConnError::StaleGeneration)
79        );
80        assert!(consent_fresh(0, CONSENT_TIMEOUT_MS - 1));
81        assert!(!consent_fresh(0, CONSENT_TIMEOUT_MS));
82        assert!(resolve_role_conflict(9, 3));
83        assert!(!resolve_role_conflict(3, 9));
84        assert!(relay_ip_is_not_direct_candidate());
85    }
86}