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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use serde::{Deserialize, Serialize};
use std::fmt;
use std::time::Duration;
/// Represent the ICE candidate pair state.
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum CandidatePairState {
#[default]
#[serde(rename = "unspecified")]
Unspecified = 0,
/// Means a check has not been performed for this pair.
#[serde(rename = "waiting")]
Waiting = 1,
/// Means a check has been sent for this pair, but the transaction is in progress.
#[serde(rename = "in-progress")]
InProgress = 2,
/// Means a check for this pair was already done and failed, either never producing any response
/// or producing an unrecoverable failure response.
#[serde(rename = "failed")]
Failed = 3,
/// Means a check for this pair was already done and produced a successful result.
#[serde(rename = "succeeded")]
Succeeded = 4,
}
impl From<u8> for CandidatePairState {
fn from(v: u8) -> Self {
match v {
1 => Self::Waiting,
2 => Self::InProgress,
3 => Self::Failed,
4 => Self::Succeeded,
_ => Self::Unspecified,
}
}
}
impl fmt::Display for CandidatePairState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match *self {
Self::Waiting => "waiting",
Self::InProgress => "in-progress",
Self::Failed => "failed",
Self::Succeeded => "succeeded",
Self::Unspecified => "unspecified",
};
write!(f, "{s}")
}
}
/// Represents a combination of a local and remote candidate.
#[derive(Clone, Copy)]
pub struct CandidatePair {
pub local_index: usize,
pub remote_index: usize,
pub local_priority: u32,
pub remote_priority: u32,
pub(crate) ice_role_controlling: bool,
pub(crate) binding_request_count: u16,
pub(crate) state: CandidatePairState,
pub(crate) nominated: bool,
// STUN transaction stats
/// Total number of STUN connectivity check requests sent (not including retransmissions).
pub(crate) requests_sent: u64,
/// Total number of STUN connectivity check requests received.
pub(crate) requests_received: u64,
/// Total number of STUN connectivity check responses sent.
pub(crate) responses_sent: u64,
/// Total number of STUN connectivity check responses received.
pub(crate) responses_received: u64,
/// Total number of consent freshness requests sent.
pub(crate) consent_requests_sent: u64,
// RTT tracking
/// Sum of all round trip time measurements.
pub(crate) total_round_trip_time: Duration,
/// Latest round trip time measured.
pub(crate) current_round_trip_time: Duration,
}
impl fmt::Debug for CandidatePair {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"prio {} (local, prio {}) {} <-> {} (remote, prio {})",
self.priority(),
self.local_priority,
self.local_index,
self.remote_index,
self.remote_priority,
)
}
}
impl fmt::Display for CandidatePair {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"prio {} (local, prio {}) {} <-> {} (remote, prio {})",
self.priority(),
self.local_priority,
self.local_index,
self.remote_index,
self.remote_priority,
)
}
}
impl PartialEq for CandidatePair {
fn eq(&self, other: &Self) -> bool {
self.local_index == other.local_index && self.remote_index == other.remote_index
}
}
impl CandidatePair {
#[must_use]
pub fn new(
local_index: usize,
remote_index: usize,
local_priority: u32,
remote_priority: u32,
ice_role_controlling: bool,
) -> Self {
Self {
local_index,
remote_index,
local_priority,
remote_priority,
ice_role_controlling,
state: CandidatePairState::Waiting,
binding_request_count: 0,
nominated: false,
// STUN transaction stats
requests_sent: 0,
requests_received: 0,
responses_sent: 0,
responses_received: 0,
consent_requests_sent: 0,
// RTT tracking
total_round_trip_time: Duration::ZERO,
current_round_trip_time: Duration::ZERO,
}
}
/// RFC 5245 - 5.7.2. Computing Pair Priority and Ordering Pairs
/// Let G be the priority for the candidate provided by the controlling
/// agent. Let D be the priority for the candidate provided by the
/// controlled agent.
/// pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0)
pub fn priority(&self) -> u64 {
let (g, d) = if self.ice_role_controlling {
(self.local_priority, self.remote_priority)
} else {
(self.remote_priority, self.local_priority)
};
// 1<<32 overflows uint32; and if both g && d are
// maxUint32, this result would overflow uint64
((1 << 32_u64) - 1) * u64::from(std::cmp::min(g, d))
+ 2 * u64::from(std::cmp::max(g, d))
+ u64::from(g > d)
}
/// Called when a STUN binding request is sent.
pub fn on_request_sent(&mut self) {
self.requests_sent += 1;
}
/// Called when a STUN binding request is received.
pub fn on_request_received(&mut self) {
self.requests_received += 1;
}
/// Called when a STUN binding success response is sent.
pub fn on_response_sent(&mut self) {
self.responses_sent += 1;
}
/// Called when a STUN binding success response is received.
/// Also updates RTT measurements.
pub fn on_response_received(&mut self, rtt: Duration) {
self.responses_received += 1;
self.current_round_trip_time = rtt;
self.total_round_trip_time += rtt;
}
/// Called when a consent freshness request is sent (keepalive).
pub fn on_consent_request_sent(&mut self) {
self.consent_requests_sent += 1;
}
}