qualia_core_db/net/peer/connectivity/
path.rs1use super::state::{ConnError, ConnSlot, ConnState};
8
9pub 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
19pub 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
41pub 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
46pub fn resolve_role_conflict(local_tie: u64, remote_tie: u64) -> bool {
48 local_tie > remote_tie
49}
50
51pub 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}