1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Which STUN / TURN servers each ICE generation of each transport gathers from.
//!
//! W3C webrtc-pc, "set the configuration": "Set the ICE Agent's ICE servers list to
//! iceServers. As defined in [RFC9429], if a new list of servers replaces the ICE Agent's
//! existing ICE servers list, no action will be taken until the next gathering phase. If a
//! script wants this to happen immediately, it should do an ICE restart." RFC 9429 section
//! 4.1.18 says the same. So the list `set_configuration` gives is the one a generation that
//! begins afterwards (an ICE restart, a new transport) gathers from; a generation already
//! gathering keeps the list it began with.
use crate::peer_connection::handler::transport_set::TransportId;
use ice::url::Url;
/// The latest list, and the list each transport's current generation took.
#[derive(Default)]
pub(crate) struct ServersInForce {
latest: Vec<Url>,
taken: Vec<(TransportId, String, Vec<Url>)>,
/// The generation each transport had before its current one, with its list: a rolled-back
/// local ICE restart brings it back ([`Self::revert`]).
previous: Vec<(TransportId, String, Vec<Url>)>,
}
impl ServersInForce {
pub(crate) fn new(urls: Vec<Url>) -> Self {
Self {
latest: urls,
taken: vec![],
previous: vec![],
}
}
/// The configuration's new list: generations that begin from now on take it.
pub(crate) fn set(&mut self, urls: Vec<Url>) {
self.latest = urls;
}
/// The list the generation `ufrag` of `transport` gathers from, and whether it took it
/// just now (its first call: a new gathering phase).
pub(crate) fn for_generation(
&mut self,
transport: TransportId,
ufrag: &str,
) -> (Vec<Url>, bool) {
match self.taken.iter_mut().find(|(t, _, _)| *t == transport) {
Some((_, u, list)) if u == ufrag => (list.clone(), false),
Some((_, u, list)) => {
let before = (
transport,
std::mem::replace(u, ufrag.to_owned()),
std::mem::replace(list, self.latest.clone()),
);
let list = list.clone();
self.previous.retain(|(t, _, _)| *t != transport);
self.previous.push(before);
(list, true)
}
None => {
self.taken
.push((transport, ufrag.to_owned(), self.latest.clone()));
(self.latest.clone(), true)
}
}
}
/// RFC 9429 section 5.7: the local offer that began generation `abandoned` of `transport`
/// was rolled back, and `restored` is the generation again: it gathers from the list it
/// took, not from the one the abandoned generation took.
pub(crate) fn revert(&mut self, transport: TransportId, abandoned: &str, restored: &str) {
let Some(at) = self
.previous
.iter()
.position(|(t, u, _)| *t == transport && u == restored)
else {
return;
};
if let Some(entry) = self
.taken
.iter_mut()
.find(|(t, u, _)| *t == transport && u == abandoned)
{
let (_, u, list) = self.previous.remove(at);
entry.1 = u;
entry.2 = list;
}
}
/// The list `transport`'s current generation took, if it took one.
pub(crate) fn taken_by(&self, transport: TransportId) -> Option<&[Url]> {
self.taken
.iter()
.find(|(t, _, _)| *t == transport)
.map(|(_, _, list)| list.as_slice())
}
/// Forgets what `transport`'s generations took: its next gathering takes the latest list
/// as a first one (a rolled-back first local offer, RFC 9429 section 5.7).
pub(crate) fn forget(&mut self, transport: TransportId) {
self.taken.retain(|(t, _, _)| *t != transport);
self.previous.retain(|(t, _, _)| *t != transport);
}
/// Whether any generation could gather from a server: the latest list or one in use.
pub(crate) fn any(&self) -> bool {
!self.latest.is_empty() || self.taken.iter().any(|(_, _, l)| !l.is_empty())
}
/// Forgets the transports not in `ids`.
pub(crate) fn retain_transports(&mut self, ids: &[TransportId]) {
self.taken.retain(|(t, _, _)| ids.contains(t));
self.previous.retain(|(t, _, _)| ids.contains(t));
}
}
/// The same server, credentials included (a TURN server given new credentials is a new
/// allocation).
pub(crate) fn same_url(a: &Url, b: &Url) -> bool {
a.scheme == b.scheme
&& a.proto == b.proto
&& a.host.eq_ignore_ascii_case(&b.host)
&& a.port == b.port
&& a.username == b.username
&& a.password == b.password
}