Skip to main content

rtci_ice/agent/
restart_undo.rs

1//! An ICE restart that can be taken back: the local offer that asked for it was rolled back
2//! (RFC 9429 section 5.7).
3
4use super::*;
5
6/// What [`Agent::apply_restart`] replaced, kept by [`Agent::apply_restart_revertible`] until
7/// the restart is committed ([`Agent::commit_restart`]) or taken back
8/// ([`Agent::revert_restart`]).
9#[derive(Clone)]
10pub(crate) struct RestartUndo {
11    connection_state: ConnectionState,
12    local_credentials: Credentials,
13    remote_credentials: Option<Credentials>,
14    local_candidates: Vec<Candidate>,
15    remote_candidates: Vec<Candidate>,
16    candidate_pairs: Vec<CandidatePair>,
17    nominated_pair: Option<usize>,
18    selected_pair: Option<usize>,
19    previous_media_path: Option<MediaPath>,
20    previous_generation: Option<PreviousGeneration>,
21    pending_binding_requests: Vec<BindingRequest>,
22    early_requests: Vec<EarlyRequest>,
23    triggered_queue: VecDeque<(usize, usize)>,
24    remote_end_of_candidates: bool,
25    local_gathering_complete: bool,
26    lite_update_raised: bool,
27    next_check_at: Instant,
28    next_consent_at: Option<Instant>,
29    consent_unanswered_since: Option<Instant>,
30    checking_duration: Instant,
31    last_checking_time: Instant,
32    tie_breaker: u64,
33    ice_generation: u64,
34    mdns_known_public: KnownPublicAddresses,
35}
36
37impl Agent {
38    /// [`Agent::apply_restart`], remembering the session it replaces so that
39    /// [`Agent::revert_restart`] can bring it back: the restart belongs to a local offer that
40    /// may still be rolled back (RFC 9429 section 5.7: "any resources allocated by the
41    /// abandoned local description are discarded"). A second restart before the first is
42    /// committed keeps the session the first one replaced.
43    pub fn apply_restart_revertible(
44        &mut self,
45        now: Instant,
46        keep_local_candidates: bool,
47    ) -> Result<()> {
48        let undo = match self.restart_undo.take() {
49            Some(undo) => undo,
50            None => Box::new(RestartUndo {
51                connection_state: self.connection_state,
52                local_credentials: self.ufrag_pwd.local_credentials.clone(),
53                remote_credentials: self.ufrag_pwd.remote_credentials.clone(),
54                local_candidates: self.local_candidates.clone(),
55                remote_candidates: self.remote_candidates.clone(),
56                candidate_pairs: self.candidate_pairs.clone(),
57                nominated_pair: self.nominated_pair,
58                selected_pair: self.selected_pair,
59                previous_media_path: self.previous_media_path,
60                previous_generation: self.previous_generation.clone(),
61                pending_binding_requests: self.pending_binding_requests.clone(),
62                early_requests: self.early_requests.clone(),
63                triggered_queue: self.triggered_queue.clone(),
64                remote_end_of_candidates: self.remote_end_of_candidates,
65                local_gathering_complete: self.local_gathering_complete,
66                lite_update_raised: self.lite_update_raised,
67                next_check_at: self.next_check_at,
68                next_consent_at: self.next_consent_at,
69                consent_unanswered_since: self.consent_unanswered_since,
70                checking_duration: self.checking_duration,
71                last_checking_time: self.last_checking_time,
72                tie_breaker: self.tie_breaker,
73                ice_generation: self.ice_generation,
74                mdns_known_public: self.mdns_known_public.clone(),
75            }),
76        };
77        self.apply_restart(now, keep_local_candidates)?;
78        self.restart_undo = Some(undo);
79        Ok(())
80    }
81
82    /// Whether a restart applied by [`Agent::apply_restart_revertible`] can still be taken
83    /// back.
84    pub fn has_revertible_restart(&self) -> bool {
85        self.restart_undo.is_some()
86    }
87
88    /// Keeps the restart [`Agent::apply_restart_revertible`] applied: the answer to its offer
89    /// arrived. A no-op without one.
90    pub fn commit_restart(&mut self) {
91        self.restart_undo = None;
92    }
93
94    /// Takes back the restart [`Agent::apply_restart_revertible`] applied: the local offer was
95    /// rolled back, so the agent goes on with the session it had (RFC 9429 section 5.7),
96    /// its credentials, candidates, pairs and selected pair, and what the new generation
97    /// gathered or learned is dropped. Consent on the selected pair carries over what the
98    /// previous-generation checks of the meantime established (RFC 7675). Returns whether there
99    /// was a restart to take back; a closed agent stays closed.
100    pub fn revert_restart(&mut self, now: Instant) -> bool {
101        let Some(undo) = self.restart_undo.take() else {
102            return false;
103        };
104        if self.connection_state == ConnectionState::Closed {
105            return false;
106        }
107        let undo = *undo;
108        // The consent the old path kept in the meantime.
109        let kept_consent = self
110            .previous_generation
111            .as_ref()
112            .map(|previous| (previous.last_consent, previous.next_consent_at));
113
114        self.announce_all_pairs_removed();
115        self.ufrag_pwd.local_credentials = undo.local_credentials;
116        self.ufrag_pwd.remote_credentials = undo.remote_credentials;
117        self.ufrag_pwd.pending_local_credentials = None;
118        self.local_candidates = undo.local_candidates;
119        // The snapshot was taken while the restart was staged: what was gathered for it
120        // belongs to no generation now.
121        for c in &mut self.local_candidates {
122            c.restart_generation = false;
123        }
124        self.remote_candidates = undo.remote_candidates;
125        self.candidate_pairs = undo.candidate_pairs;
126        self.nominated_pair = undo.nominated_pair;
127        self.selected_pair = undo.selected_pair;
128        self.previous_media_path = undo.previous_media_path;
129        self.previous_generation = undo.previous_generation;
130        self.pending_binding_requests = undo.pending_binding_requests;
131        self.early_requests = undo.early_requests;
132        self.triggered_queue = undo.triggered_queue;
133        self.remote_end_of_candidates = undo.remote_end_of_candidates;
134        // The generation taken back is gone: what its gathering showed public with it.
135        self.ice_generation = undo.ice_generation;
136        self.mdns_known_public = undo.mdns_known_public;
137        self.local_gathering_complete = undo.local_gathering_complete;
138        self.lite_update_raised = undo.lite_update_raised;
139        self.next_check_at = undo.next_check_at.max(now);
140        self.next_consent_at = undo.next_consent_at;
141        self.consent_unanswered_since = undo.consent_unanswered_since;
142        self.checking_duration = undo.checking_duration;
143        self.last_checking_time = undo.last_checking_time;
144        self.tie_breaker = undo.tie_breaker;
145
146        if let (Some(pair_index), Some((last_consent, next_consent_at))) =
147            (self.selected_pair, kept_consent)
148        {
149            let remote_index = self.candidate_pairs[pair_index].remote_index;
150            let remote = &mut self.remote_candidates[remote_index];
151            if self.lite {
152                if remote.last_authenticated.is_none_or(|t| t < last_consent) {
153                    remote.last_authenticated = Some(last_consent);
154                }
155            } else if remote.last_consent.is_none_or(|t| t < last_consent) {
156                remote.last_consent = Some(last_consent);
157            }
158            self.next_consent_at = Some(next_consent_at);
159        }
160
161        for index in 0..self.candidate_pairs.len() {
162            self.announce_pair(index, true);
163        }
164        if let Some(pair_index) = self.selected_pair {
165            let pair = self.candidate_pairs[pair_index];
166            self.event_outs.push_back(TaggedEvent {
167                now,
168                event: Event::SelectedCandidatePairChange(
169                    Box::new(self.local_candidates[pair.local_index].clone()),
170                    Box::new(self.remote_candidates[pair.remote_index].clone()),
171                ),
172            });
173        }
174        self.update_connection_state(Some(now), undo.connection_state);
175        true
176    }
177}