msf-ice 0.2.3

Interactive Connectivity Establishment (ICE) for Rust.
Documentation
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use std::{
    net::SocketAddr,
    pin::Pin,
    task::{Context, Poll, Waker},
    time::{Duration, Instant},
};

use bytes::Bytes;
use futures::{ready, FutureExt};
use msf_stun as stun;
use tokio::time::Sleep;

use crate::{
    candidate::{CandidateKind, CandidatePair, LocalCandidate, RemoteCandidate},
    AgentRole,
};

const RTO: u64 = 500;
const RM: u64 = 16;
const RC: u32 = 7;

/// Single connectivity check.
pub struct Check {
    pair: CandidatePair,
    state: CheckState,
    task: Option<Waker>,
    nominated: bool,
}

impl Check {
    /// Create a new connectivity check.
    pub fn new(pair: CandidatePair, nominated: bool) -> Self {
        Self {
            pair,
            state: CheckState::Frozen,
            task: None,
            nominated,
        }
    }

    /// Get the current check transaction (if any).
    fn transaction(&self) -> Option<&CheckTransaction> {
        if let CheckState::InProgress(t) = &self.state {
            Some(t)
        } else {
            None
        }
    }

    /// Get the current transaction ID (if any).
    pub fn transaction_id(&self) -> Option<TransactionId> {
        self.transaction().map(|t| t.id())
    }

    /// Check if the underlying candidate pair has been nominated.
    pub fn is_nominated(&self) -> bool {
        self.nominated
    }

    /// Get the local candidate from the underlying candidate pair.
    pub fn local_candidate(&self) -> &LocalCandidate {
        self.pair.local()
    }

    /// Get the remote candidate from the underlying candidate pair.
    pub fn remote_candidate(&self) -> &RemoteCandidate {
        self.pair.remote()
    }

    /// Get the underlying candidate pair.
    pub fn candidate_pair(&self) -> &CandidatePair {
        &self.pair
    }

    /// Get the corresponding component ID.
    pub fn component(&self) -> u8 {
        self.pair.component()
    }

    /// Get priority of the underlying candidate pair.
    pub fn priority(&self, local_role: AgentRole) -> u64 {
        self.pair.priority(local_role)
    }

    /// Get foundation of the underlying candidate pair.
    pub fn foundation(&self) -> &str {
        self.pair.foundation()
    }

    /// Check if the current state is "frozen".
    pub fn is_frozen(&self) -> bool {
        matches!(self.state, CheckState::Frozen)
    }

    /// Check if the current state is "waiting".
    pub fn is_waiting(&self) -> bool {
        matches!(self.state, CheckState::Waiting)
    }

    /// Check if the current state is "success".
    pub fn is_success(&self) -> bool {
        matches!(self.state, CheckState::Succeeded)
    }

    /// Check if the current state is "success", "failed" or "cancelled".
    pub fn is_done(&self) -> bool {
        matches!(
            self.state,
            CheckState::Succeeded | CheckState::Cancelled | CheckState::Failed
        )
    }

    /// Unfreeze the check.
    ///
    /// The check must be in the "frozen" state.
    pub fn unfreeze(&mut self) {
        debug_assert!(self.is_frozen());

        self.state = CheckState::Waiting;
    }

    /// Trigger the check.
    ///
    /// Regardless of the current state, the check will be switched to the
    /// "waiting" state.
    pub fn trigger(&mut self) {
        self.state = CheckState::Waiting;
    }

    /// Finish the check ASAP.
    ///
    /// No more retransmissions will be generated. The check will be cancelled
    /// if it hasn't been scheduled yet.
    pub fn finish(&mut self) {
        match &mut self.state {
            CheckState::Frozen | CheckState::Waiting => self.state = CheckState::Cancelled,
            CheckState::InProgress(t) => t.finish(),
            _ => (),
        }

        if let Some(task) = self.task.take() {
            task.wake();
        }
    }

    /// Cancel the check.
    ///
    /// Unless the check is done, it will be switched to the "cancelled" state.
    pub fn cancel(&mut self) {
        if self.is_done() {
            return;
        }

        self.state = CheckState::Cancelled;

        if let Some(task) = self.task.take() {
            task.wake();
        }
    }

    /// Schedule the check.
    ///
    /// The check must be either in the "frozen" state or in the "waiting"
    /// state. It will be switched to the "in-progress" state and a new check
    /// transaction will be created.
    pub fn schedule(
        &mut self,
        username: &str,
        password: &str,
        agent_role: AgentRole,
        tie_breaker: u64,
    ) {
        debug_assert!(matches!(
            self.state,
            CheckState::Frozen | CheckState::Waiting
        ));

        let transaction_id = rand::random();

        // request priority must be equal to priority of a peer-reflexive
        // candidate that could be a result of this transaction
        let local = self.pair.local();
        let remote = self.pair.remote();

        let addr = local.addr();

        let priority = LocalCandidate::calculate_priority(
            remote.component(),
            CandidateKind::PeerReflexive,
            addr,
        );

        let mut builder = stun::MessageBuilder::binding_request(transaction_id);

        builder
            .username(username)
            .priority(priority)
            .message_integrity(password.as_bytes())
            .fingerprint(true);

        if agent_role == AgentRole::Controlling {
            if self.nominated {
                builder.use_candidate(true);
            }

            builder.ice_controlling(tie_breaker);
        } else {
            builder.ice_controlled(tie_breaker);
        }

        let msg = CheckMessage {
            local_addr: local.base(),
            remote_addr: remote.addr(),
            component: remote.component(),
            data: builder.build(),
        };

        let transaction = CheckTransaction::new(agent_role, transaction_id, msg);

        self.state = CheckState::InProgress(Box::new(transaction));

        if let Some(task) = self.task.take() {
            task.wake();
        }
    }

    /// Process a given STUN response.
    ///
    /// The check must be in the "in-progress" state and the current
    /// transaction ID must match the transaction ID in the STUN response.
    pub fn process_stun_response(
        &mut self,
        local_addr: SocketAddr,
        remote_addr: SocketAddr,
        response: &stun::Message,
    ) -> Result<(), CheckError> {
        let transaction = self.transaction().unwrap();

        debug_assert_eq!(transaction.id(), response.transaction_id());

        let res = if let Some(err) = response.attributes().get_error_code() {
            if err.code() == 487 {
                Err(CheckError::RoleConflict(transaction.agent_role()))
            } else {
                Err(CheckError::Failed)
            }
        } else {
            // check that the local and remote addresses are symmetric
            let local_candidate = self.pair.local();
            let remote_candidate = self.pair.remote();

            if local_addr == local_candidate.base() && remote_addr == remote_candidate.addr() {
                Ok(())
            } else {
                Err(CheckError::Failed)
            }
        };

        self.state = match &res {
            Ok(_) => CheckState::Succeeded,
            Err(CheckError::RoleConflict(_)) => CheckState::Waiting,
            Err(_) => CheckState::Failed,
        };

        res
    }

    /// Poll the check.
    ///
    /// Polling the check will yield connectivity check STUN messages or `None`
    /// if the check is done.
    pub fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Option<CheckMessage>> {
        match &mut self.state {
            CheckState::Frozen | CheckState::Waiting => {
                let task = cx.waker();

                self.task = Some(task.clone());

                Poll::Pending
            }
            CheckState::InProgress(t) => {
                if let Some(msg) = ready!(t.poll(cx)) {
                    Poll::Ready(Some(msg))
                } else {
                    // connectivity check timeout
                    self.state = CheckState::Failed;

                    Poll::Ready(None)
                }
            }
            _ => Poll::Ready(None),
        }
    }

    /// Update the current check pushing the state machine forward if possible.
    pub fn update(&mut self, other: Check) {
        if !self.nominated && other.nominated {
            *self = other;
        } else if self.nominated == other.nominated {
            match (&self.state, &other.state) {
                (CheckState::Frozen, _) => *self = other,
                (CheckState::Waiting, CheckState::InProgress(_)) => *self = other,
                (_, CheckState::Succeeded) => *self = other,
                (_, CheckState::Failed) => *self = other,
                (_, CheckState::Cancelled) => *self = other,
                _ => (),
            }
        }
    }
}

/// Connectivity check state.
enum CheckState {
    Frozen,
    Waiting,
    InProgress(Box<CheckTransaction>),
    Succeeded,
    Failed,
    Cancelled,
}

/// Connectivity check transaction ID.
pub type TransactionId = [u8; 12];

/// Connectivity check transaction.
struct CheckTransaction {
    id: TransactionId,
    timeout: Pin<Box<Sleep>>,
    next_timeout: Duration,
    last_timeout: Duration,
    remaining_attempts: u32,
    message: CheckMessage,
    agent_role: AgentRole,
    last_attempt: Option<Instant>,
    task: Option<Waker>,
}

impl CheckTransaction {
    /// Create a new connectivity check transaction.
    fn new(agent_role: AgentRole, id: TransactionId, message: CheckMessage) -> Self {
        Self {
            id,
            timeout: Box::pin(tokio::time::sleep(Duration::from_millis(0))),
            next_timeout: Duration::from_millis(RTO),
            last_timeout: Duration::from_millis(RTO * RM),
            remaining_attempts: RC,
            message,
            agent_role,
            last_attempt: None,
            task: None,
        }
    }

    /// Get the transaction ID.
    fn id(&self) -> TransactionId {
        self.id
    }

    /// Get the agent role used by the agent when the transaction was created.
    fn agent_role(&self) -> AgentRole {
        self.agent_role
    }

    /// Finish the transaction without generating any more connection attempts.
    fn finish(&mut self) {
        if let Some(last_attempt) = self.last_attempt {
            let deadline = std::cmp::min(
                last_attempt + self.last_timeout,
                Instant::now() + Duration::from_millis(1_000),
            );

            self.timeout.as_mut().reset(deadline.into());

            self.remaining_attempts = 0;
        } else {
            self.remaining_attempts = 1;
        }

        if let Some(task) = self.task.take() {
            task.wake();
        }
    }

    /// Poll the transaction.
    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Option<CheckMessage>> {
        let mut res = Poll::Pending;

        loop {
            let poll = self.timeout.poll_unpin(cx);

            if poll.is_pending() {
                let task = cx.waker();

                // save the task handle if necessary
                if res.is_pending() {
                    self.task = Some(task.clone());
                }

                return res;
            }

            let timeout = if self.remaining_attempts == 0 {
                return Poll::Ready(None);
            } else if self.remaining_attempts == 1 {
                self.last_timeout
            } else {
                self.next_timeout
            };

            let now = Instant::now();

            self.last_attempt = Some(now);

            let deadline = now + timeout;

            self.timeout.as_mut().reset(deadline.into());

            res = Poll::Ready(Some(self.message.clone()));

            self.remaining_attempts -= 1;
            self.next_timeout *= 2;
        }
    }
}

/// Connectivity check STUN message.
#[derive(Clone)]
pub struct CheckMessage {
    local_addr: SocketAddr,
    remote_addr: SocketAddr,
    component: u8,
    data: Bytes,
}

impl CheckMessage {
    /// Get the local address from which the connectivity check must be sent.
    pub fn local_addr(&self) -> SocketAddr {
        self.local_addr
    }

    /// Get the target address.
    pub fn remote_addr(&self) -> SocketAddr {
        self.remote_addr
    }

    /// Get the component ID.
    pub fn component(&self) -> u8 {
        self.component
    }

    /// Take the STUN message.
    pub fn take_data(self) -> Bytes {
        self.data
    }
}

/// Connectivity check error.
#[derive(Copy, Clone)]
pub enum CheckError {
    Failed,
    RoleConflict(AgentRole),
    UnknownTransaction,
}