rtci 0.2.1

A sans-I/O WebRTC implementation: peer connections with ICE, DTLS-SRTP, SCTP data channels, RTP/RTCP and JSEP negotiation
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Relayed TCP candidates (RFC 6544 section 5.5 over TURN-TCP, RFC 6062), run by the engine
//! for its `turn:...?transport=tcp` and `turns:` servers.
//!
//! A link with `relay_tcp` asks for a TCP allocation (REQUESTED-TRANSPORT TCP) on a control
//! connection of its own and announces its relayed address twice: as a passive TCP candidate,
//! and on port 9 as an active one (RFC 6544 section 4.5). Each peer connection through the
//! allocation is a data connection to the server: the engine opens it for a ConnectionAttempt
//! the server indicates (a peer reached the passive candidate; RFC 6062 section 4.4), or after
//! a Connect for a check the agent sends from the active candidate (section 4.3), binds it with
//! ConnectionBind and then carries the peer's byte stream over it, framed with RFC 4571 as on
//! any ICE-TCP connection. Permissions for the peers' addresses are created as the agent learns
//! remote candidates, since the server refuses a connection from an address without one
//! (RFC 6062 section 5.3).

use super::*;
use crate::peer_connection::streams::TURN_DATA;
use shared::tcp_framing::TcpFrameDecoder;
use turn::client::data_connection::{DataConnection, DataConnectionEvent};
use turn::client::{AllocateOptions, RelayTransport};
use turn::proto::connid::ConnectionId;

/// Datagrams the agent sends from the active relayed candidate while its data connection is
/// being made.
const MAX_QUEUED: usize = 32;

/// The port of an active TCP candidate (RFC 6544 section 4.5).
const ACTIVE_PORT: u16 = 9;

/// One peer's TCP connection through a TCP allocation.
pub(super) struct DataConn {
    /// The data connection to the server, once asked for.
    stream: Option<StreamId>,
    peer: SocketAddr,
    /// Made for a check from the active candidate (Connect), not for a ConnectionAttempt.
    active: bool,
    connection_id: Option<ConnectionId>,
    conn: Option<DataConnection>,
    decoder: TcpFrameDecoder,
    /// ICE datagrams waiting for the bind.
    queued: Vec<Vec<u8>>,
}

impl DataConn {
    fn new(peer: SocketAddr, active: bool, connection_id: Option<ConnectionId>) -> Self {
        Self {
            stream: None,
            peer,
            active,
            connection_id,
            conn: None,
            decoder: TcpFrameDecoder::new(),
            queued: vec![],
        }
    }

    /// Its data connection to the server, once asked for.
    pub(super) fn stream(&self) -> Option<StreamId> {
        self.stream
    }
}

/// The allocation options of a TCP allocation.
pub(super) fn tcp_allocation() -> AllocateOptions {
    AllocateOptions {
        transport: RelayTransport::Tcp,
        ..Default::default()
    }
}

impl TurnLink {
    /// The address the agent knows its side of `conn` by: the passive candidate's (the
    /// relayed address) or the active one's (its address on port 9).
    fn agent_local(&self, conn: &DataConn) -> Option<SocketAddr> {
        let relay = self.relay?;
        Some(if conn.active {
            SocketAddr::new(relay.ip(), ACTIVE_PORT)
        } else {
            relay
        })
    }
}

impl RTCPeerConnection {
    /// The TCP-allocation link whose relayed candidate `local` is: `(index, active)`.
    fn tcp_relay_of(&self, local: SocketAddr) -> Option<(usize, bool)> {
        self.streams
            .turn
            .links
            .iter()
            .position(|l| {
                l.relay_tcp
                    && l.relay.is_some_and(|r| {
                        r == local || (local.port() == ACTIVE_PORT && r.ip() == local.ip())
                    })
            })
            .map(|i| {
                let relay = self.streams.turn.links[i].relay;
                (i, relay != Some(local))
            })
    }

    /// Takes a TCP datagram the agent sends from a relayed TCP candidate (and returns
    /// `None`), onto the data connection to its peer; returns anything else.
    pub(crate) fn turn_tcp_write(&mut self, msg: TaggedBytesMut) -> Option<TaggedBytesMut> {
        if msg.transport.transport_protocol != TransportProtocol::TCP {
            return Some(msg);
        }
        let Some((index, active)) = self.tcp_relay_of(msg.transport.local_addr) else {
            return Some(msg);
        };
        let now = msg.now.max(self.pipeline_context.now);
        let peer = msg.transport.peer_addr;
        let framed = match Framing::Rfc4571.encode(&msg.message) {
            Ok(framed) => framed,
            Err(err) => {
                debug!("relayed TCP write to {peer}: {err}");
                return None;
            }
        };
        let link = &mut self.streams.turn.links[index];
        if let Some(conn) = link
            .data
            .iter_mut()
            .find(|c| c.peer == peer && c.active == active)
        {
            match conn.conn.as_mut() {
                Some(c) if c.is_bound() => {
                    if let Err(err) = c.send(now, &framed) {
                        debug!("relayed TCP write to {peer}: {err}");
                    }
                }
                _ if conn.queued.len() < MAX_QUEUED => conn.queued.push(framed),
                _ => {}
            }
        } else if active {
            // RFC 6062 section 4.3: Connect, after the permission the server checks.
            if let (Some(relay), Some(client)) = (link.relay, link.client.as_mut())
                && let Ok(mut r) = client.relay(relay)
            {
                if link.permitted.insert(peer.ip()) {
                    let _ = r.create_permission(now, peer);
                }
                match r.connect(now, peer) {
                    Ok(_) => {
                        let mut conn = DataConn::new(peer, true, None);
                        conn.queued.push(framed);
                        link.data.push(conn);
                    }
                    Err(err) => debug!("TURN Connect to {peer}: {err}"),
                }
            }
        }
        // A passive candidate only answers on connections the server announced.
        self.pump_turn_data(now, index);
        self.pump_turn(now);
        None
    }

    /// RFC 6062 section 4.3 / 4.4: the server has a peer connection for `peer` (a Connect
    /// succeeded, or a peer connected to the relayed address): a data connection is opened to
    /// the server for it.
    pub(super) fn turn_tcp_peer_ready(
        &mut self,
        index: usize,
        peer: SocketAddr,
        connection_id: ConnectionId,
        attempt: bool,
    ) {
        // A peer the embedder's address policy refuses gets no data connection: its
        // connection to the relayed address is never bound (RFC 6062 section 4.4).
        if attempt && !self.address_allowed(peer, ice::AddressPurpose::InboundDatagramSource) {
            debug!("TURN TCP connection from {peer}: the address policy refuses it, not bound");
            return;
        }
        let link = &self.streams.turn.links[index];
        let Some(server) = link.server else {
            return;
        };
        let layer: Option<Box<dyn StreamLayer>> = if link.tls() {
            match self
                .streams
                .turn
                .tls_config
                .clone()
                .map(|config| TlsLayer::new(config, &link.url.host))
            {
                Some(Ok(layer)) => Some(Box::new(layer)),
                _ => return,
            }
        } else {
            None
        };
        let link = &mut self.streams.turn.links[index];
        let conn = if attempt {
            link.data
                .push(DataConn::new(peer, false, Some(connection_id)));
            link.data.last_mut()
        } else {
            let found = link
                .data
                .iter_mut()
                .find(|c| c.active && c.peer == peer && c.connection_id.is_none());
            if let Some(c) = found {
                c.connection_id = Some(connection_id);
                Some(c)
            } else {
                None
            }
        };
        let Some(conn) = conn else {
            return;
        };
        match self.streams.table.connect(
            any_of_family(server),
            server,
            Framing::Raw,
            TURN_DATA,
            false,
            layer,
        ) {
            Ok(id) => conn.stream = Some(id),
            Err(err) => debug!("TURN data connection for {peer}: {err}"),
        }
    }

    fn data_conn_of_stream(&self, id: StreamId) -> Option<(usize, usize)> {
        self.streams
            .turn
            .links
            .iter()
            .enumerate()
            .find_map(|(i, l)| {
                l.data
                    .iter()
                    .position(|c| c.stream == Some(id))
                    .map(|j| (i, j))
            })
    }

    /// A data connection to the server is up: the ConnectionBind goes out on it.
    pub(crate) fn turn_data_connected(&mut self, now: Instant, id: StreamId, local: SocketAddr) {
        let Some((index, j)) = self.data_conn_of_stream(id) else {
            return;
        };
        let link = &mut self.streams.turn.links[index];
        let (Some(relay), Some(client)) = (link.relay, link.client.as_mut()) else {
            return;
        };
        let Some(connection_id) = link.data[j].connection_id else {
            return;
        };
        match client
            .relay(relay)
            .and_then(|mut r| r.data_connection(now, connection_id, local))
        {
            Ok(conn) => link.data[j].conn = Some(conn),
            Err(err) => {
                debug!("TURN data connection: {err}");
                self.streams.table.close(id);
                self.streams.turn.links[index].data.remove(j);
                return;
            }
        }
        self.pump_turn_data(now, index);
    }

    /// Bytes read on a data connection: the ConnectionBind response, then the peer's stream.
    pub(crate) fn turn_data_messages(&mut self, now: Instant, id: StreamId, chunks: Vec<Vec<u8>>) {
        let Some((index, j)) = self.data_conn_of_stream(id) else {
            return;
        };
        let Some(info) = self.streams.table.info(id) else {
            return;
        };
        let transport = self.streams.turn.links[index].transport;
        let mut packets = vec![];
        let mut failed = false;
        {
            let link = &mut self.streams.turn.links[index];
            let agent_local = link.agent_local(&link.data[j]);
            let conn = &mut link.data[j];
            if let Some(c) = conn.conn.as_mut() {
                for chunk in chunks {
                    if let Err(err) = c.handle_read(TaggedBytesMut {
                        now,
                        transport: tcp_context(info.local, info.remote),
                        message: BytesMut::from(&chunk[..]),
                    }) {
                        debug!("TURN data connection: {err}");
                    }
                }
                while let Some(event) = c.poll_event() {
                    match event {
                        DataConnectionEvent::Bound => {
                            for framed in std::mem::take(&mut conn.queued) {
                                let _ = c.send(now, &framed);
                            }
                        }
                        DataConnectionEvent::BindError(err) => {
                            debug!("ConnectionBind for {}: {err:?}", conn.peer);
                            failed = true;
                        }
                        _ => {}
                    }
                }
                while let Some(bytes) = c.poll_read() {
                    if conn.decoder.push(&bytes).is_err() {
                        failed = true;
                        break;
                    }
                    while let Some(packet) = conn.decoder.next_packet() {
                        packets.push(packet);
                    }
                }
            }
            if let Some(local) = agent_local {
                for packet in packets {
                    self.pipeline_context.observe(now);
                    let peer = self.streams.turn.links[index].data[j].peer;
                    self.read_on_transport(
                        transport,
                        TaggedBytesMut {
                            now,
                            transport: tcp_context(local, peer),
                            message: BytesMut::from(&packet[..]),
                        },
                    );
                }
            }
        }
        if failed {
            self.streams.table.reset(id);
            self.turn_data_ended(now, id);
            return;
        }
        self.pump_turn_data(now, index);
    }

    /// A data connection could not be made or has ended: the peer's connection is gone, and
    /// the checks waiting on it fail (RFC 6544 section 7.1).
    pub(crate) fn turn_data_ended(&mut self, now: Instant, id: StreamId) {
        let Some((index, j)) = self.data_conn_of_stream(id) else {
            return;
        };
        let link = &mut self.streams.turn.links[index];
        let conn = link.data.remove(j);
        let (transport, local) = (link.transport, link.agent_local(&conn));
        if let Some(local) = local {
            self.with_transport(transport, |pc| {
                pc.ice_transport_mut().agent.handle_transport_error(
                    now,
                    tcp_context(local, conn.peer),
                    ice::TransportError::ConnectionFailed,
                )
            });
        }
    }

    /// Moves what the data connections of link `index` have to send onto their streams.
    pub(super) fn pump_turn_data(&mut self, _now: Instant, index: usize) {
        let link = &mut self.streams.turn.links[index];
        for conn in link.data.iter_mut() {
            let (Some(id), Some(c)) = (conn.stream, conn.conn.as_mut()) else {
                continue;
            };
            while let Some(t) = c.poll_write() {
                let _ = self.streams.table.send(id, &t.message);
            }
        }
    }

    /// A Connect for `peer` failed: its queued checks fail with it (RFC 6544 section 7.1).
    pub(super) fn turn_tcp_connect_failed(&mut self, now: Instant, index: usize, peer: SocketAddr) {
        let link = &mut self.streams.turn.links[index];
        let Some(j) = link
            .data
            .iter()
            .position(|c| c.active && c.peer == peer && c.stream.is_none())
        else {
            return;
        };
        let conn = link.data.remove(j);
        if let Some(local) = link.agent_local(&conn) {
            let transport = link.transport;
            self.with_transport(transport, |pc| {
                pc.ice_transport_mut().agent.handle_transport_error(
                    now,
                    tcp_context(local, conn.peer),
                    ice::TransportError::ConnectionFailed,
                )
            });
        }
    }

    /// RFC 6062 section 5.3: a peer reaches the passive relayed candidate only with a
    /// permission for its address, so one is created for each remote TCP candidate address of
    /// the transport (of the relayed address's family) as the agent learns it.
    pub(crate) fn turn_tcp_permissions(&mut self, now: Instant) {
        for index in 0..self.streams.turn.links.len() {
            let link = &self.streams.turn.links[index];
            let (true, Some(relay), LinkState::Open(_)) = (link.relay_tcp, link.relay, link.state)
            else {
                continue;
            };
            let transport = link.transport;
            // The remote TCP candidates the agent holds, and those it does not keep (active
            // ones, RFC 6544 section 4.5) but that were signalled.
            let remote_description: Vec<IpAddr> = self
                .remote_description()
                .map(|d| d.sdp.clone())
                .unwrap_or_default()
                .lines()
                .filter_map(|l| l.strip_prefix("a=candidate:"))
                .filter_map(|v| ice::candidate::unmarshal_candidate(v).ok())
                .filter(|c| c.network_type().is_tcp())
                .filter(|c| {
                    c.address().parse::<IpAddr>().is_ok()
                        && self.address_allowed(c.addr(), ice::AddressPurpose::RemoteCandidate)
                })
                .filter_map(|c| c.address().parse::<IpAddr>().ok())
                .collect();
            let ips: Vec<IpAddr> = self
                .ice_transport_of(transport)
                .agent
                .get_remote_candidates()
                .iter()
                .filter(|c| c.network_type().is_tcp())
                .map(|c| c.addr().ip())
                .chain(
                    self.streams
                        .turn
                        .remote_tcp_ips
                        .iter()
                        .filter(|(t, _)| *t == transport)
                        .map(|(_, ip)| *ip),
                )
                .chain(remote_description)
                .filter(|ip| ip.is_ipv4() == relay.is_ipv4())
                .collect();
            let link = &mut self.streams.turn.links[index];
            let Some(client) = link.client.as_mut() else {
                continue;
            };
            for ip in ips {
                if link.permitted.insert(ip)
                    && let Ok(mut r) = client.relay(relay)
                {
                    let _ = r.create_permission(now, SocketAddr::new(ip, 0));
                }
            }
        }
    }

    /// Notes the address of a remote TCP candidate (`candidate_value`) of the active
    /// transport for the permissions of its TCP allocations.
    pub(crate) fn note_remote_tcp_candidate(&mut self, candidate_value: &str) {
        if !self.streams.turn.tcp_relay {
            return;
        }
        let Ok(c) = ice::candidate::unmarshal_candidate(candidate_value) else {
            return;
        };
        if !c.network_type().is_tcp() {
            return;
        }
        let Ok(ip) = c.address().parse::<IpAddr>() else {
            return;
        };
        if !self.address_allowed(c.addr(), ice::AddressPurpose::RemoteCandidate) {
            return;
        }
        let on = self.pipeline_context.transports.active_id();
        if !self.streams.turn.remote_tcp_ips.contains(&(on, ip)) {
            self.streams.turn.remote_tcp_ips.push((on, ip));
        }
    }

    /// The data connections' timers (the ConnectionBind's 30 s, RFC 6062 section 4.4).
    pub(super) fn turn_data_poll_timeout(&mut self) -> Option<Instant> {
        self.streams
            .turn
            .links
            .iter_mut()
            .flat_map(|l| l.data.iter_mut())
            .filter_map(|c| c.conn.as_mut().and_then(|c| c.poll_timeout()))
            .min()
    }

    /// Fires the data connections' timers.
    pub(super) fn turn_data_handle_timeout(&mut self, now: Instant) {
        let mut failed = vec![];
        for link in self.streams.turn.links.iter_mut() {
            for conn in link.data.iter_mut() {
                let (Some(id), Some(c)) = (conn.stream, conn.conn.as_mut()) else {
                    continue;
                };
                if c.poll_timeout().is_some_and(|t| t <= now) {
                    let _ = c.handle_timeout(now);
                }
                while let Some(event) = c.poll_event() {
                    if let DataConnectionEvent::BindError(err) = event {
                        debug!("ConnectionBind for {}: {err:?}", conn.peer);
                        failed.push(id);
                    }
                }
            }
        }
        for id in failed {
            self.streams.table.reset(id);
            self.turn_data_ended(now, id);
        }
    }
}