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