srt-runtime 0.10.0

SRT (Secure Reliable Transport, draft-sharabayko-srt-01) packet codecs + sans-IO HSv5 Caller-Listener and Rendezvous handshake state machines, an ARQ reliability engine, a TSBPD delivery scheduler, LiveCC packet pacing, FileCC window-based congestion control, optional (crypto feature) payload encryption, and an optional (tokio feature) async UDP socket adapter; no_std core.
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
//! The CIF-less / single-scalar-CIF control packets: Keep-Alive (§3.2.3),
//! Congestion Warning (§3.2.6), Shutdown (§3.2.7), ACKACK (§3.2.8), Message
//! Drop Request (§3.2.9), and Peer Error (§3.2.10).

use super::number::{AckNumber, MESSAGE_NUMBER_MASK, MessageNumber, SeqNo, SocketId, SrtTimestamp};
use super::{Error, Result, be32, put_be32};
use broadcast_common::error::BufferTooShort;

/// Peer error code for a file-system error — the only value
/// `draft-sharabayko-srt-01` §3.2.10 currently defines.
pub const PEER_ERROR_FILE_SYSTEM: u32 = 4000;

/// Keep-Alive control packet (§3.2.3, Figure 12). No CIF.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct KeepAlivePacket {
    /// Timestamp (§3).
    pub timestamp: SrtTimestamp,
    /// Destination Socket ID (§3).
    pub dest_socket_id: SocketId,
    /// `true` when the CIF carries libsrt's 4-byte zero pad; `false` for the
    /// empty CIF of draft-sharabayko-srt-01.
    pub libsrt_pad: bool,
}

/// Congestion Warning control packet (§3.2.6, Figure 15). Reserved for future
/// use; no CIF.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CongestionWarningPacket {
    /// Timestamp (§3).
    pub timestamp: SrtTimestamp,
    /// Destination Socket ID (§3).
    pub dest_socket_id: SocketId,
    /// `true` when the CIF carries libsrt's 4-byte zero pad; `false` for the
    /// empty CIF of draft-sharabayko-srt-01.
    pub libsrt_pad: bool,
}

/// Shutdown control packet (§3.2.7, Figure 16). No CIF.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ShutdownPacket {
    /// Timestamp (§3).
    pub timestamp: SrtTimestamp,
    /// Destination Socket ID (§3).
    pub dest_socket_id: SocketId,
    /// `true` when the CIF carries libsrt's 4-byte zero pad; `false` for the
    /// empty CIF of draft-sharabayko-srt-01.
    pub libsrt_pad: bool,
}

/// ACKACK control packet (§3.2.8, Figure 17). No CIF.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct AckAckPacket {
    /// Acknowledgement Number of the Full ACK being acknowledged (§3.2.8,
    /// Figure 17).
    pub ack_number: AckNumber,
    /// Timestamp (§3).
    pub timestamp: SrtTimestamp,
    /// Destination Socket ID (§3).
    pub dest_socket_id: SocketId,
    /// `true` when the CIF carries libsrt's 4-byte zero pad; `false` for the
    /// empty CIF of draft-sharabayko-srt-01.
    pub libsrt_pad: bool,
}

/// Message Drop Request control packet (§3.2.9, Figure 18).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DropReqPacket {
    /// The message number requested to be dropped (`0` if the sender no
    /// longer has the packets and cannot restore it).
    ///
    /// §3.2.9 (Figure 18) calls this field 32 bits wide, but it names a
    /// message, so it is bounded here by the 26-bit Message Number field of
    /// §3.1 it refers to — the only values the sender can ever produce.
    pub message_number: MessageNumber,
    /// Timestamp (§3).
    pub timestamp: SrtTimestamp,
    /// Destination Socket ID (§3).
    pub dest_socket_id: SocketId,
    /// First Packet Sequence Number of the range to drop.
    pub first_seq: SeqNo,
    /// Last Packet Sequence Number of the range to drop.
    pub last_seq: SeqNo,
}

impl DropReqPacket {
    pub(crate) fn parse_cif(
        message_number: u32,
        timestamp: SrtTimestamp,
        dest_socket_id: SocketId,
        cif: &[u8],
    ) -> Result<Self> {
        if cif.len() != 8 {
            return Err(Error::BufferTooShort(BufferTooShort {
                need: 8,
                have: cif.len(),
                what: "drop request CIF",
            }));
        }
        let message_number = MessageNumber::try_new(message_number).map_err(|_| {
            Error::FieldOverflow(broadcast_common::error::FieldOverflow {
                field: "Message Number",
                value: u64::from(message_number),
                max: u64::from(MESSAGE_NUMBER_MASK),
            })
        })?;
        Ok(DropReqPacket {
            message_number,
            timestamp,
            dest_socket_id,
            first_seq: SeqNo::from_wire_masked(be32(cif, 0)),
            last_seq: SeqNo::from_wire_masked(be32(cif, 4)),
        })
    }

    pub(crate) fn cif_len(&self) -> usize {
        8
    }

    pub(crate) fn write_cif(&self, buf: &mut [u8]) -> Result<()> {
        put_be32(buf, 0, self.first_seq.value());
        put_be32(buf, 4, self.last_seq.value());
        Ok(())
    }
}

/// Peer Error control packet (§3.2.10, Figure 19). No CIF.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct PeerErrorPacket {
    /// Peer error code (see [`PEER_ERROR_FILE_SYSTEM`]).
    pub error_code: u32,
    /// Timestamp (§3).
    pub timestamp: SrtTimestamp,
    /// Destination Socket ID (§3).
    pub dest_socket_id: SocketId,
    /// `true` when the CIF carries libsrt's 4-byte zero pad; `false` for the
    /// empty CIF of draft-sharabayko-srt-01.
    pub libsrt_pad: bool,
}

#[cfg(test)]
mod tests {
    use alloc::vec::Vec;

    use super::super::control::ControlPacket;
    use super::super::data::MESSAGE_NUMBER_MASK as MESSAGE_NUMBER_MAX;
    use super::*;

    fn msg(n: u32) -> MessageNumber {
        MessageNumber::try_new(n).expect("fits 26 bits")
    }

    fn sn(n: u32) -> SeqNo {
        SeqNo::try_new(n).expect("fits 31 bits")
    }

    #[test]
    fn drop_req_round_trips() {
        let d = DropReqPacket {
            message_number: msg(7),
            timestamp: SrtTimestamp::from_wire(100),
            dest_socket_id: SocketId::from_wire(200),
            first_seq: sn(10),
            last_seq: sn(20),
        };
        let pkt = ControlPacket::DropReq(d);
        let mut buf = [0u8; 24];
        let n = pkt.serialize_into(&mut buf).unwrap();
        assert_eq!(n, 24);
        assert_eq!(&buf[4..8], &7u32.to_be_bytes()); // message number in word1
        assert_eq!(&buf[16..20], &10u32.to_be_bytes());
        assert_eq!(&buf[20..24], &20u32.to_be_bytes());
        let parsed = ControlPacket::parse(&buf).unwrap();
        assert_eq!(parsed, pkt);
    }

    /// §3.2.9's fields are 32-bit words on the wire, but the two sequence
    /// numbers share the data packet's 31-bit space (§3.1) and the Message
    /// Number is the 26-bit one: a peer's top-bit-set values are accepted
    /// (masked for the sequence numbers, since the typed `SeqNo` cannot hold
    /// them) and serialize back to the same masked bytes — never a
    /// serialize-time refusal of a packet parse accepted.
    #[test]
    fn drop_req_wide_wire_numbers_still_serialize() {
        let mut buf = alloc::vec![0u8; 24];
        ControlPacket::DropReq(DropReqPacket {
            message_number: msg(7),
            timestamp: SrtTimestamp::from_wire(100),
            dest_socket_id: SocketId::from_wire(200),
            first_seq: sn(10),
            last_seq: sn(20),
        })
        .serialize_into(&mut buf)
        .unwrap();
        buf[16..20].copy_from_slice(&0xFFFF_FFFFu32.to_be_bytes());
        buf[20..24].copy_from_slice(&0xFFFF_FFFEu32.to_be_bytes());
        let parsed = ControlPacket::parse(&buf).unwrap();
        let ControlPacket::DropReq(d) = parsed else {
            panic!("a drop request");
        };
        assert_eq!(d.first_seq, SeqNo::MAX);
        assert_eq!(d.last_seq, sn(0x7FFF_FFFE));
        let mut out = alloc::vec![0u8; parsed.serialized_len()];
        parsed.serialize_into(&mut out).unwrap();
        assert_eq!(&out[0..16], &buf[0..16]);
        assert_eq!(&out[16..20], &SeqNo::MAX.value().to_be_bytes());
        assert_eq!(&out[20..24], &0x7FFF_FFFEu32.to_be_bytes());
        assert_eq!(ControlPacket::parse(&out).unwrap(), parsed);
    }

    /// A DROPREQ whose Message Number word is wider than the 26-bit Message
    /// Number it names is rejected at parse, so `parse` never yields a packet
    /// `serialize` would refuse (#2766's bound now sits at the parse edge).
    #[test]
    fn drop_req_rejects_an_over_wide_message_number_at_parse() {
        let mut buf = alloc::vec![0u8; 24];
        ControlPacket::DropReq(DropReqPacket {
            message_number: msg(MESSAGE_NUMBER_MAX),
            timestamp: SrtTimestamp::from_wire(100),
            dest_socket_id: SocketId::from_wire(200),
            first_seq: sn(10),
            last_seq: sn(20),
        })
        .serialize_into(&mut buf)
        .unwrap();
        assert!(ControlPacket::parse(&buf).is_ok());

        for wide in [MESSAGE_NUMBER_MAX + 1, 0x8E8E_8E8E, u32::MAX] {
            buf[4..8].copy_from_slice(&wide.to_be_bytes());
            assert_eq!(
                ControlPacket::parse(&buf).unwrap_err(),
                Error::FieldOverflow(broadcast_common::error::FieldOverflow {
                    field: "Message Number",
                    value: u64::from(wide),
                    max: u64::from(MESSAGE_NUMBER_MAX),
                })
            );
        }
    }

    /// An ACKACK echoes a Full ACK's Acknowledgement Number verbatim, and
    /// that field is a whole 32-bit word (§3.2.8, Figure 17) — a value above
    /// the 31-bit sequence space round-trips.
    #[test]
    fn ackack_carries_every_32_bit_ack_number() {
        for ack_number in [0, 1, 0x8000_0000, 0x8E8E_8E8E, u32::MAX] {
            let pkt = ControlPacket::AckAck(AckAckPacket {
                ack_number: AckNumber::from_wire(ack_number),
                timestamp: SrtTimestamp::from_wire(7),
                dest_socket_id: SocketId::from_wire(8),
                libsrt_pad: false,
            });
            let mut buf = alloc::vec![0u8; pkt.serialized_len()];
            pkt.serialize_into(&mut buf).unwrap();
            assert_eq!(&buf[4..8], &ack_number.to_be_bytes());
            assert_eq!(ControlPacket::parse(&buf).unwrap(), pkt);
        }
    }

    #[test]
    fn keepalive_congestion_shutdown_ackack_peererror_round_trip() {
        let cases: Vec<ControlPacket> = alloc::vec![
            ControlPacket::KeepAlive(KeepAlivePacket {
                timestamp: SrtTimestamp::from_wire(1),
                dest_socket_id: SocketId::from_wire(2),
                libsrt_pad: true,
            }),
            ControlPacket::CongestionWarning(CongestionWarningPacket {
                timestamp: SrtTimestamp::from_wire(3),
                dest_socket_id: SocketId::from_wire(4),
                libsrt_pad: true,
            }),
            ControlPacket::Shutdown(ShutdownPacket {
                timestamp: SrtTimestamp::from_wire(5),
                dest_socket_id: SocketId::from_wire(6),
                libsrt_pad: true,
            }),
            ControlPacket::AckAck(AckAckPacket {
                ack_number: AckNumber::from_wire(9),
                timestamp: SrtTimestamp::from_wire(7),
                dest_socket_id: SocketId::from_wire(8),
                libsrt_pad: true,
            }),
            ControlPacket::PeerError(PeerErrorPacket {
                error_code: PEER_ERROR_FILE_SYSTEM,
                timestamp: SrtTimestamp::from_wire(11),
                dest_socket_id: SocketId::from_wire(12),
                libsrt_pad: true,
            }),
        ];
        for pkt in cases {
            // 20 bytes when libsrt_pad is true: serializes with the 4-byte
            // zero pad (see `control::LIBSRT_CIF_PAD_LEN`).
            let mut buf = [0u8; 20];
            let n = pkt.serialize_into(&mut buf).unwrap();
            assert_eq!(n, 20);
            assert_eq!(&buf[16..20], &[0, 0, 0, 0]);
            let parsed = ControlPacket::parse(&buf).unwrap();
            assert_eq!(parsed, pkt);
        }
    }

    /// A peer (e.g. real libsrt — see `tests/it/libsrt_fixtures.rs`) may also
    /// send these types with the pure-spec empty CIF instead of the pad.
    /// Both 16-byte (no-pad) and 20-byte (with-pad) wire shapes parse correctly.
    #[test]
    fn keepalive_congestion_shutdown_ackack_peererror_accept_empty_cif() {
        let cases: Vec<ControlPacket> = alloc::vec![
            ControlPacket::KeepAlive(KeepAlivePacket {
                timestamp: SrtTimestamp::from_wire(1),
                dest_socket_id: SocketId::from_wire(2),
                libsrt_pad: false,
            }),
            ControlPacket::CongestionWarning(CongestionWarningPacket {
                timestamp: SrtTimestamp::from_wire(3),
                dest_socket_id: SocketId::from_wire(4),
                libsrt_pad: false,
            }),
            ControlPacket::Shutdown(ShutdownPacket {
                timestamp: SrtTimestamp::from_wire(5),
                dest_socket_id: SocketId::from_wire(6),
                libsrt_pad: false,
            }),
            ControlPacket::AckAck(AckAckPacket {
                ack_number: AckNumber::from_wire(9),
                timestamp: SrtTimestamp::from_wire(7),
                dest_socket_id: SocketId::from_wire(8),
                libsrt_pad: false,
            }),
            ControlPacket::PeerError(PeerErrorPacket {
                error_code: PEER_ERROR_FILE_SYSTEM,
                timestamp: SrtTimestamp::from_wire(11),
                dest_socket_id: SocketId::from_wire(12),
                libsrt_pad: false,
            }),
        ];
        for pkt in cases {
            // Serialize with libsrt_pad: false -> 16 bytes
            let mut buf = [0u8; 16];
            let n = pkt.serialized_len();
            assert_eq!(n, 16, "16-byte packets with libsrt_pad: false");
            pkt.serialize_into(&mut buf).unwrap();

            // Parse the 16-byte form back
            let parsed = ControlPacket::parse(&buf).unwrap();
            assert_eq!(parsed, pkt);
        }
    }

    /// A 16-byte (pure-spec empty CIF) KEEPALIVE packet parses and
    /// round-trips byte-identically.
    #[test]
    fn sixteen_byte_keepalive_round_trip_byte_identical() {
        let pkt = ControlPacket::KeepAlive(KeepAlivePacket {
            timestamp: SrtTimestamp::from_wire(0x1234),
            dest_socket_id: SocketId::from_wire(0x5678),
            libsrt_pad: false,
        });
        let mut buf = [0u8; 16];
        let n = pkt.serialize_into(&mut buf).unwrap();
        assert_eq!(n, 16, "16-byte packet with libsrt_pad: false");

        let parsed = ControlPacket::parse(&buf).unwrap();
        assert_eq!(parsed, pkt);

        let mut reserialized = [0u8; 16];
        let n = parsed.serialize_into(&mut reserialized).unwrap();
        assert_eq!(n, 16);
        assert_eq!(&reserialized[..], &buf[..], "round-trip is byte-identical");
    }

    /// A 20-byte (libsrt's 4-byte zero pad) KEEPALIVE packet parses and
    /// round-trips byte-identically.
    #[test]
    fn twenty_byte_keepalive_round_trip_byte_identical() {
        let pkt = ControlPacket::KeepAlive(KeepAlivePacket {
            timestamp: SrtTimestamp::from_wire(0x1234),
            dest_socket_id: SocketId::from_wire(0x5678),
            libsrt_pad: true,
        });
        let mut buf = [0u8; 20];
        let n = pkt.serialize_into(&mut buf).unwrap();
        assert_eq!(n, 20, "20-byte packet with libsrt_pad: true");
        assert_eq!(&buf[16..20], &[0, 0, 0, 0], "CIF is 4-byte zero pad");

        let parsed = ControlPacket::parse(&buf).unwrap();
        assert_eq!(parsed, pkt);

        let mut reserialized = [0u8; 20];
        let n = parsed.serialize_into(&mut reserialized).unwrap();
        assert_eq!(n, 20);
        assert_eq!(&reserialized[..], &buf[..], "round-trip is byte-identical");
    }

    #[test]
    fn keepalive_rejects_trailing_bytes() {
        // Neither 0 (pure spec) nor 4 (libsrt's pad, §control::LIBSRT_CIF_PAD_LEN):
        // 1 stray byte must still be rejected.
        let mut buf = [0u8; 17];
        buf[0] = 0x80; // F=1
        buf[1] = 0x01; // control type = 1 (KEEPALIVE)
        assert!(matches!(
            ControlPacket::parse(&buf),
            Err(Error::UnexpectedTrailingBytes { .. })
        ));
    }

    #[test]
    fn keepalive_rejects_nonzero_pad() {
        let mut buf = [0u8; 20];
        buf[0] = 0x80; // F=1
        buf[1] = 0x01; // control type = 1 (KEEPALIVE)
        buf[19] = 0x01; // non-zero byte in the 4-byte pad
        assert!(matches!(
            ControlPacket::parse(&buf),
            Err(Error::ReservedBits(_))
        ));
    }
}