rtci-datachannel 0.2.1

WebRTC data channels and the Data Channel Establishment Protocol (RFC 8831, RFC 8832), sans-I/O
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use super::*;
use crate::message::message_type::MessageType;
use sansio::Protocol;
use shared::error::Result;

fn create_new_association_pair() -> Result<(usize, usize)> {
    Ok((0, 0))
}

fn close_association_pair(_client: usize, _server: usize) {}

//use std::io::Write;

// pr_ordered_unordered_test is in sctp_link_test.rs.

#[test]
fn test_data_channel_channel_type_reliable_ordered() -> Result<()> {
    let mut sbuf = vec![0u8; 1000];

    let (a0, a1) = create_new_association_pair()?;

    let cfg = DataChannelConfig {
        channel_type: ChannelType::Reliable,
        reliability_parameter: 123,
        label: "data".to_string(),
        ..Default::default()
    };

    let mut dc0 = DataChannel::dial(cfg.clone(), a0, 100)?;

    let msg = dc0.poll_write().ok_or(Error::ErrAssociationNotExisted)?;
    let mut dc1 = DataChannel::accept(
        DataChannelConfig::default(),
        a1,
        msg.stream_id,
        PayloadProtocolIdentifier::Dcep,
        &msg.payload,
    )?;

    assert_eq!(dc0.config, cfg, "local config should match");
    assert_eq!(dc1.config, cfg, "remote config should match");

    sbuf[0..4].copy_from_slice(&1u32.to_be_bytes());
    let data_channel_message =
        DataChannel::get_data_channel_message(true, BytesMut::from(&sbuf[0..4]));
    dc0.handle_write(data_channel_message)?;
    assert_eq!(dc0.bytes_sent(), 4, "data length should match");

    sbuf[0..4].copy_from_slice(&2u32.to_be_bytes());
    let data_channel_message =
        DataChannel::get_data_channel_message(false, BytesMut::from(&sbuf[0..4]));
    dc0.handle_write(data_channel_message)?;
    assert_eq!(dc0.bytes_sent(), 8, "data length should match");

    let msg = dc0.poll_write().ok_or(Error::ErrAssociationNotExisted)?;
    dc1.handle_read(msg)?;
    assert_eq!(dc1.bytes_received(), 4, "data length should match");
    let data_channel_message = dc1.poll_read().ok_or(Error::ErrAssociationNotExisted)?;
    assert!(
        data_channel_message.ppi == PayloadProtocolIdentifier::String
            || data_channel_message.ppi == PayloadProtocolIdentifier::StringEmpty
    );
    assert_eq!(
        4,
        data_channel_message.payload.len(),
        "data length should match"
    );
    assert_eq!(
        1,
        u32::from_be_bytes([
            data_channel_message.payload[0],
            data_channel_message.payload[1],
            data_channel_message.payload[2],
            data_channel_message.payload[3]
        ]),
        "data should match"
    );

    let msg = dc0.poll_write().ok_or(Error::ErrAssociationNotExisted)?;
    dc1.handle_read(msg)?;
    assert_eq!(dc1.bytes_received(), 8, "data length should match");
    let data_channel_message = dc1.poll_read().ok_or(Error::ErrAssociationNotExisted)?;
    assert!(
        !(data_channel_message.ppi == PayloadProtocolIdentifier::String
            || data_channel_message.ppi == PayloadProtocolIdentifier::StringEmpty)
    );
    assert_eq!(
        4,
        data_channel_message.payload.len(),
        "data length should match"
    );
    assert_eq!(
        2,
        u32::from_be_bytes([
            data_channel_message.payload[0],
            data_channel_message.payload[1],
            data_channel_message.payload[2],
            data_channel_message.payload[3]
        ]),
        "data should match"
    );

    dc0.close()?;
    dc1.close()?;

    close_association_pair(a0, a1);

    Ok(())
}

/// Pre-negotiated (out-of-band) channels rely on the `SctpHandler` to suppress
/// the DCEP DataChannelOpen payload on the wire.  This test verifies the
/// `dial()` side of that contract: the outbound `DataChannelMessage` must carry
/// `negotiated = true` so the SCTP handler knows to open the stream without
/// sending the DCEP payload to the remote peer.
///
/// Note: the handler-side suppression is exercised by integration tests in the
/// `rtci` crate; this unit test only covers the flagging produced by `dial()`.
///
/// Regression test for <https://github.com/webrtc-rs/rtc/issues/61>.
#[test]
fn test_data_channel_negotiated_dial_flags_message() -> Result<()> {
    let (a0, a1) = create_new_association_pair()?;
    let stream_id = 42;

    let cfg = DataChannelConfig {
        channel_type: ChannelType::Reliable,
        negotiated: true,
        label: "negotiated-ch".to_string(),
        protocol: "test-proto".to_string(),
        ..Default::default()
    };

    let mut dc = DataChannel::dial(cfg.clone(), a0, stream_id)?;

    // dial() must still produce exactly one outbound DCEP message so the SCTP
    // handler can register the stream.
    let msg = dc.poll_write().ok_or(Error::ErrAssociationNotExisted)?;
    assert_eq!(msg.ppi, PayloadProtocolIdentifier::Dcep);
    assert_eq!(msg.stream_id, stream_id);

    // The message must be flagged as negotiated so the SCTP handler opens the
    // stream but does NOT send the DCEP payload to the peer.
    assert!(
        msg.negotiated,
        "negotiated DataChannelOpen must be marked as internal-only"
    );

    // No further outbound messages should be queued.
    assert!(
        dc.poll_write().is_none(),
        "negotiated dial should produce exactly one outbound message"
    );

    // Verify the config round-trips correctly.
    assert_eq!(dc.config(), &cfg);

    dc.close()?;
    close_association_pair(a0, a1);

    Ok(())
}

/// Regression test for <https://github.com/webrtc-rs/rtc/issues/61>.
///
/// An out-of-band *negotiated* channel used to emit no message from `dial`, so
/// the underlying SCTP stream was never opened and any later write failed with
/// "Stream not existed". `dial` must now emit a `DATA_CHANNEL_OPEN` (to open and
/// configure the local stream) that is flagged so the transport keeps it local
/// and does not perform the DCEP handshake with the peer.
#[test]
fn test_data_channel_negotiated_opens_stream_without_dcep_handshake() -> Result<()> {
    let (a0, _a1) = create_new_association_pair()?;

    let cfg = DataChannelConfig {
        channel_type: ChannelType::Reliable,
        label: "negotiated".to_string(),
        negotiated: true,
        ..Default::default()
    };

    let mut dc = DataChannel::dial(cfg, a0, 100)?;

    // A single DATA_CHANNEL_OPEN must be queued so the stream gets opened.
    let msg = dc.poll_write().ok_or(Error::ErrStreamNotExisted)?;
    assert_eq!(
        msg.ppi,
        PayloadProtocolIdentifier::Dcep,
        "negotiated dial should emit a DATA_CHANNEL_OPEN to open the stream"
    );
    assert!(
        msg.negotiated,
        "a negotiated channel's DATA_CHANNEL_OPEN must be flagged internal-only"
    );
    assert_eq!(
        Message::unmarshal(&mut &msg.payload[..])?.message_type(),
        MessageType::DataChannelOpen,
        "the queued message should be a DATA_CHANNEL_OPEN"
    );
    assert!(
        dc.poll_write().is_none(),
        "no further DCEP messages expected for a negotiated channel"
    );
    assert!(
        dc.is_handshake_complete(),
        "an out-of-band negotiated channel is open immediately (no DCEP handshake)"
    );

    Ok(())
}

/// Non-negotiated (in-band) channels must send a regular DCEP DataChannelOpen
/// that can be accepted by the peer.
#[test]
fn test_data_channel_non_negotiated_sends_dcep() -> Result<()> {
    let (a0, a1) = create_new_association_pair()?;

    let cfg = DataChannelConfig {
        channel_type: ChannelType::Reliable,
        negotiated: false,
        label: "in-band".to_string(),
        ..Default::default()
    };

    let mut dc0 = DataChannel::dial(cfg.clone(), a0, 100)?;

    let msg = dc0.poll_write().ok_or(Error::ErrAssociationNotExisted)?;
    assert_eq!(msg.ppi, PayloadProtocolIdentifier::Dcep);
    // In-band channel: negotiated flag must be false so the SCTP handler sends
    // the DCEP payload over the wire.
    assert!(
        !msg.negotiated,
        "in-band DataChannelOpen must NOT be marked as internal-only"
    );

    // The peer should be able to accept this DCEP message.
    let mut dc1 = DataChannel::accept(
        DataChannelConfig::default(),
        a1,
        msg.stream_id,
        PayloadProtocolIdentifier::Dcep,
        &msg.payload,
    )?;
    assert_eq!(dc1.config(), &cfg, "remote config should match");

    dc0.close()?;
    dc1.close()?;
    close_association_pair(a0, a1);

    Ok(())
}

/// A non-negotiated (in-band) channel emits a `DATA_CHANNEL_OPEN` that IS sent
/// over the wire, so it must not be flagged as negotiated.
#[test]
fn test_data_channel_in_band_open_is_transmitted() -> Result<()> {
    let (a0, _a1) = create_new_association_pair()?;

    let cfg = DataChannelConfig {
        channel_type: ChannelType::Reliable,
        label: "in-band".to_string(),
        negotiated: false,
        ..Default::default()
    };

    let mut dc = DataChannel::dial(cfg, a0, 100)?;
    let msg = dc.poll_write().ok_or(Error::ErrStreamNotExisted)?;
    assert_eq!(msg.ppi, PayloadProtocolIdentifier::Dcep);
    assert!(
        !msg.negotiated,
        "an in-band DATA_CHANNEL_OPEN must be transmitted to the peer"
    );
    assert!(
        !dc.is_handshake_complete(),
        "an in-band channel is not open until the peer's DATA_CHANNEL_ACK arrives"
    );

    Ok(())
}

/// An in-band channel becomes open the moment its `DATA_CHANNEL_ACK` is processed.
#[test]
fn test_data_channel_in_band_open_on_data_channel_ack() -> Result<()> {
    let (a0, _a1) = create_new_association_pair()?;

    let cfg = DataChannelConfig {
        channel_type: ChannelType::Reliable,
        label: "in-band".to_string(),
        negotiated: false,
        ..Default::default()
    };

    let mut dc = DataChannel::dial(cfg, a0, 100)?;
    assert!(!dc.is_handshake_complete());

    // The dialed channel is a Protocol<DataChannelMessage, ...>; feed it the peer's ACK.
    let ack_bytes = Message::DataChannelAck(DataChannelAck {}).marshal()?;
    dc.handle_read(DataChannelMessage {
        association_handle: a0,
        stream_id: 100,
        ppi: PayloadProtocolIdentifier::Dcep,
        payload: BytesMut::from(&ack_bytes[..]),
        negotiated: false,
    })?;

    assert!(
        dc.is_handshake_complete(),
        "receiving DATA_CHANNEL_ACK must mark the local channel as open"
    );

    Ok(())
}

/// A remote channel is open from `accept()`: the peer's `DATA_CHANNEL_OPEN` has
/// been processed and the ACK is written out, completing the handshake.
#[test]
fn test_data_channel_accept_marks_remote_channel_open() -> Result<()> {
    let (a0, _a1) = create_new_association_pair()?;

    let open_bytes = Message::DataChannelOpen(DataChannelOpen {
        channel_type: ChannelType::Reliable,
        priority: CHANNEL_PRIORITY_NORMAL,
        reliability_parameter: 0,
        label: b"remote".to_vec(),
        protocol: Vec::new(),
    })
    .marshal()?;

    let dc = DataChannel::accept(
        DataChannelConfig::default(),
        a0,
        100,
        PayloadProtocolIdentifier::Dcep,
        &open_bytes[..],
    )?;

    assert!(
        dc.is_handshake_complete(),
        "accept() processing the peer's DATA_CHANNEL_OPEN must mark the remote channel as open"
    );

    Ok(())
}

/// Closing an open channel clears the handshake-complete flag.
#[test]
fn test_data_channel_close_resets_handshake_complete_flag() -> Result<()> {
    let (a0, _a1) = create_new_association_pair()?;

    let cfg = DataChannelConfig {
        channel_type: ChannelType::Reliable,
        label: "negotiated".to_string(),
        negotiated: true,
        ..Default::default()
    };

    let mut dc = DataChannel::dial(cfg, a0, 100)?;
    assert!(
        dc.is_handshake_complete(),
        "an out-of-band negotiated channel is open immediately (no DCEP handshake)"
    );

    dc.close()?;
    assert!(
        !dc.is_handshake_complete(),
        "closing an open channel must clear the handshake-complete flag"
    );

    Ok(())
}

// test_data_channel_channel_type_reliable_unordered, the four partial-reliability tests,
// test_data_channel_buffered_amount, test_stats and test_poll_data_channel ran over a tokio
// bridge upstream; they are in sctp_link_test.rs, over two real SCTP associations.

/// DCEP types 0x00 (threshold) and 0x01 (close) are internal to this stack and never sent on
/// the wire. From the peer they are refused, whether they would open a channel or arrive on
/// one, and they cause no write.
#[test]
fn test_internal_dcep_types_from_the_peer_are_refused() -> Result<()> {
    let (a0, _a1) = create_new_association_pair()?;
    let internal: [&[u8]; 3] = [&[0x00, 0x00, 0x00, 0x00, 0x10, 0x00], &[0x00], &[0x01]];
    for payload in internal {
        assert!(
            DataChannel::accept(
                DataChannelConfig::default(),
                a0,
                100,
                PayloadProtocolIdentifier::Dcep,
                payload,
            )
            .is_err(),
            "accept() took {payload:?}"
        );

        let mut dc = DataChannel::dial(
            DataChannelConfig {
                label: "in-band".to_string(),
                ..Default::default()
            },
            a0,
            100,
        )?;
        while dc.poll_write().is_some() {}
        assert!(
            dc.handle_read(DataChannelMessage {
                association_handle: a0,
                stream_id: 100,
                ppi: PayloadProtocolIdentifier::Dcep,
                payload: BytesMut::from(payload),
                negotiated: false,
            })
            .is_err(),
            "an open channel took {payload:?}"
        );
        assert!(dc.poll_write().is_none());
        assert!(dc.poll_read().is_none());
        assert!(!dc.is_handshake_complete());
    }
    Ok(())
}

/// Fuzz regression (`data_channel_handle_dcep`, minimised input `2f00f9ff79005b00`): a one-byte
/// DCEP payload `0x00` on a new stream, or on an open channel, panicked the receiver.
#[test]
fn test_one_byte_dcep_payload_is_an_error() {
    assert!(
        DataChannel::accept(
            DataChannelConfig::default(),
            0,
            1,
            PayloadProtocolIdentifier::Dcep,
            &[0x00]
        )
        .is_err()
    );

    let mut dc = DataChannel::accept(
        DataChannelConfig::default(),
        0,
        1,
        PayloadProtocolIdentifier::Dcep,
        &Message::DataChannelOpen(DataChannelOpen {
            channel_type: ChannelType::Reliable,
            priority: 0,
            reliability_parameter: 0,
            label: b"chat".to_vec(),
            protocol: vec![],
        })
        .marshal()
        .unwrap(),
    )
    .unwrap();
    let msg = DataChannelMessage {
        stream_id: 1,
        ppi: PayloadProtocolIdentifier::Dcep,
        payload: BytesMut::from(&[0x00][..]),
        ..Default::default()
    };
    assert!(dc.handle_read(msg).is_err());
}

/// RFC 8832 section 6: a DATA_CHANNEL_OPEN "received on an already used stream" is not
/// acknowledged. The channel reports it as an error, on which its owner closes the channel;
/// it used to answer with a second DATA_CHANNEL_ACK and carry on.
#[test]
fn test_open_on_a_channel_in_use_is_refused_without_an_ack() -> Result<()> {
    let open = Message::DataChannelOpen(DataChannelOpen {
        channel_type: ChannelType::Reliable,
        priority: CHANNEL_PRIORITY_NORMAL,
        reliability_parameter: 0,
        label: b"again".to_vec(),
        protocol: Vec::new(),
    })
    .marshal()?;
    let open_on = |stream_id| DataChannelMessage {
        stream_id,
        ppi: PayloadProtocolIdentifier::Dcep,
        payload: BytesMut::from(&open[..]),
        ..Default::default()
    };

    // A channel the peer opened, one this side opened, and one negotiated out of band.
    let accepted = DataChannel::accept(
        DataChannelConfig::default(),
        0,
        1,
        PayloadProtocolIdentifier::Dcep,
        &open,
    )?;
    let dialed = DataChannel::dial(DataChannelConfig::default(), 0, 1)?;
    let negotiated = DataChannel::dial(
        DataChannelConfig {
            negotiated: true,
            ..Default::default()
        },
        0,
        1,
    )?;
    for mut dc in [accepted, dialed, negotiated] {
        while dc.poll_write().is_some() {}
        assert!(dc.handle_read(open_on(1)).is_err());
        assert!(dc.poll_write().is_none(), "no DATA_CHANNEL_ACK");
        assert_eq!(
            dc.messages_received(),
            0,
            "a refused message is not counted"
        );
    }
    Ok(())
}

/// RFC 8831 section 6.6: the PPID says how a message is to be read. One under a PPID that is
/// not a data channel's (52 and 54, the deprecated partial ones, included: all decode to
/// `Unknown`) is refused, where it used to be queued for the application as binary data.
#[test]
fn test_a_message_with_an_unsupported_ppid_is_refused() -> Result<()> {
    let mut dc = DataChannel::dial(
        DataChannelConfig {
            negotiated: true,
            ..Default::default()
        },
        0,
        1,
    )?;
    for wire_ppid in [0u32, 52, 54, 55, 58] {
        let ppi = PayloadProtocolIdentifier::from(wire_ppid);
        assert!(
            dc.handle_read(DataChannelMessage {
                stream_id: 1,
                ppi,
                payload: BytesMut::from(&b"fragment"[..]),
                ..Default::default()
            })
            .is_err(),
            "PPID {wire_ppid}"
        );
    }
    assert!(dc.poll_read().is_none());
    assert_eq!(dc.messages_received(), 0);
    assert_eq!(dc.bytes_received(), 0);

    // The four user PPIDs of RFC 8831 section 8 are delivered.
    for wire_ppid in [51u32, 53, 56, 57] {
        dc.handle_read(DataChannelMessage {
            stream_id: 1,
            ppi: PayloadProtocolIdentifier::from(wire_ppid),
            payload: BytesMut::from(&[0u8][..]),
            ..Default::default()
        })?;
        assert!(dc.poll_read().is_some(), "PPID {wire_ppid}");
    }
    Ok(())
}

/// RFC 8832 section 5.1: for reliable data channels the Reliability Parameter "MUST be set to
/// 0 on the sending side", and the OPEN this side sends carries the priority, label and
/// protocol the channel was created with.
#[test]
fn test_a_dialed_open_carries_the_channels_parameters() -> Result<()> {
    let (channel_type, reliability_parameter) =
        DataChannel::get_channel_type_and_reliability_parameter(false, None, None);
    assert_eq!(
        (channel_type, reliability_parameter),
        (ChannelType::ReliableUnordered, 0)
    );
    let mut dc = DataChannel::dial(
        DataChannelConfig {
            channel_type,
            reliability_parameter,
            priority: 1024,
            label: "label".to_owned(),
            protocol: "proto".to_owned(),
            negotiated: false,
        },
        0,
        2,
    )?;
    let open = dc.poll_write().expect("the OPEN");
    assert_eq!(open.ppi, PayloadProtocolIdentifier::Dcep);
    assert_eq!(open.stream_id, 2);
    assert_eq!(
        Message::unmarshal(&mut &open.payload[..])?,
        Message::DataChannelOpen(DataChannelOpen {
            channel_type: ChannelType::ReliableUnordered,
            priority: 1024,
            reliability_parameter: 0,
            label: b"label".to_vec(),
            protocol: b"proto".to_vec(),
        })
    );
    Ok(())
}