Skip to main content

srt_runtime/packet/
control.rs

1//! SRT control packet dispatch — `draft-sharabayko-srt-01` §3.2, Figure 4,
2//! Table 1.
3//!
4//! ```text
5//! word0  1|         Control Type (15)   |          Subtype (16)         |
6//! word1                   Type-specific Information (32)
7//! word2                          Timestamp (32)
8//! word3                    Destination Socket ID (32)
9//! rest                              CIF (variable, per Control Type)
10//! ```
11//!
12//! `Subtype` is `0x0` for every Control Type in Table 1 except
13//! `User-Defined Type` (`0x7FFF`), where it carries `SRT_CMD_KMREQ` /
14//! `SRT_CMD_KMRSP` (Table 5) when the CIF is a Key Material message (§3.2.2).
15//! `Subtype` and the `Type-specific Information` word (where a given packet
16//! type does not use it) are validated as reserved-must-be-zero and are not
17//! stored in the typed per-type structs — see the crate root's reserved-bit
18//! policy.
19
20use super::ack::AckPacket;
21use super::handshake::HandshakePacket;
22use super::misc::{
23    AckAckPacket, CongestionWarningPacket, DropReqPacket, KeepAlivePacket, PeerErrorPacket,
24    ShutdownPacket,
25};
26use super::nak::NakPacket;
27use super::number::{AckNumber, SocketId, SrtTimestamp};
28use super::{Error, Result, SRT_HEADER_LEN, be32, put_be32};
29use broadcast_common::error::ReservedBits;
30use broadcast_common::len::{need_in, need_out};
31
32/// `Control Type` wire values (`draft-sharabayko-srt-01` §3.2, Table 1).
33/// Crate-private: they back [`ControlType::from_bits`]/[`ControlType::to_bits`],
34/// and [`ControlType`] is how a caller names a control type.
35pub(crate) const CONTROL_TYPE_HANDSHAKE: u16 = 0x0000;
36/// Keep-Alive (§3.2.3).
37pub(crate) const CONTROL_TYPE_KEEPALIVE: u16 = 0x0001;
38/// ACK (§3.2.4).
39pub(crate) const CONTROL_TYPE_ACK: u16 = 0x0002;
40/// NAK / Loss Report (§3.2.5).
41pub(crate) const CONTROL_TYPE_NAK: u16 = 0x0003;
42/// Congestion Warning (§3.2.6).
43pub(crate) const CONTROL_TYPE_CONGESTION_WARNING: u16 = 0x0004;
44/// Shutdown (§3.2.7).
45pub(crate) const CONTROL_TYPE_SHUTDOWN: u16 = 0x0005;
46/// ACKACK (§3.2.8).
47pub(crate) const CONTROL_TYPE_ACKACK: u16 = 0x0006;
48/// Message Drop Request (§3.2.9).
49pub(crate) const CONTROL_TYPE_DROPREQ: u16 = 0x0007;
50/// Peer Error (§3.2.10).
51pub(crate) const CONTROL_TYPE_PEERERROR: u16 = 0x0008;
52/// User-Defined Type (reserved value; also carries the Key Material message
53/// per §3.2.2 via `Subtype` `SRT_CMD_KMREQ`/`SRT_CMD_KMRSP`).
54pub(crate) const CONTROL_TYPE_USER_DEFINED: u16 = 0x7FFF;
55
56/// The `Control Type` field (`draft-sharabayko-srt-01` §3.2, Table 1) — 15
57/// bits.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59#[cfg_attr(feature = "serde", derive(serde::Serialize))]
60#[non_exhaustive]
61pub enum ControlType {
62    /// `0x0000` — Handshake (§3.2.1).
63    Handshake,
64    /// `0x0001` — Keep-Alive (§3.2.3).
65    KeepAlive,
66    /// `0x0002` — ACK (§3.2.4).
67    Ack,
68    /// `0x0003` — NAK / Loss Report (§3.2.5).
69    Nak,
70    /// `0x0004` — Congestion Warning (§3.2.6).
71    CongestionWarning,
72    /// `0x0005` — Shutdown (§3.2.7).
73    Shutdown,
74    /// `0x0006` — ACKACK (§3.2.8).
75    AckAck,
76    /// `0x0007` — Message Drop Request (§3.2.9).
77    DropReq,
78    /// `0x0008` — Peer Error (§3.2.10).
79    PeerError,
80    /// `0x7FFF` — User-Defined Type (also the Key Material control-packet
81    /// delivery form, §3.2.2).
82    UserDefined,
83    /// A Control Type value Table 1 does not define.
84    Reserved(u16),
85}
86
87impl ControlType {
88    /// Decode a 15-bit `Control Type` value.
89    pub fn from_bits(v: u16) -> Self {
90        match v {
91            CONTROL_TYPE_HANDSHAKE => ControlType::Handshake,
92            CONTROL_TYPE_KEEPALIVE => ControlType::KeepAlive,
93            CONTROL_TYPE_ACK => ControlType::Ack,
94            CONTROL_TYPE_NAK => ControlType::Nak,
95            CONTROL_TYPE_CONGESTION_WARNING => ControlType::CongestionWarning,
96            CONTROL_TYPE_SHUTDOWN => ControlType::Shutdown,
97            CONTROL_TYPE_ACKACK => ControlType::AckAck,
98            CONTROL_TYPE_DROPREQ => ControlType::DropReq,
99            CONTROL_TYPE_PEERERROR => ControlType::PeerError,
100            CONTROL_TYPE_USER_DEFINED => ControlType::UserDefined,
101            other => ControlType::Reserved(other),
102        }
103    }
104
105    /// The wire value.
106    pub fn to_bits(self) -> u16 {
107        match self {
108            ControlType::Handshake => CONTROL_TYPE_HANDSHAKE,
109            ControlType::KeepAlive => CONTROL_TYPE_KEEPALIVE,
110            ControlType::Ack => CONTROL_TYPE_ACK,
111            ControlType::Nak => CONTROL_TYPE_NAK,
112            ControlType::CongestionWarning => CONTROL_TYPE_CONGESTION_WARNING,
113            ControlType::Shutdown => CONTROL_TYPE_SHUTDOWN,
114            ControlType::AckAck => CONTROL_TYPE_ACKACK,
115            ControlType::DropReq => CONTROL_TYPE_DROPREQ,
116            ControlType::PeerError => CONTROL_TYPE_PEERERROR,
117            ControlType::UserDefined => CONTROL_TYPE_USER_DEFINED,
118            ControlType::Reserved(v) => v,
119        }
120    }
121
122    /// Spec label.
123    pub fn name(&self) -> &'static str {
124        match self {
125            ControlType::Handshake => "handshake",
126            ControlType::KeepAlive => "keep-alive",
127            ControlType::Ack => "ACK",
128            ControlType::Nak => "NAK",
129            ControlType::CongestionWarning => "congestion warning",
130            ControlType::Shutdown => "shutdown",
131            ControlType::AckAck => "ACKACK",
132            ControlType::DropReq => "message drop request",
133            ControlType::PeerError => "peer error",
134            ControlType::UserDefined => "user-defined",
135            ControlType::Reserved(_) => "reserved",
136        }
137    }
138}
139
140broadcast_common::impl_spec_display!(ControlType, Reserved);
141
142/// A Control Type = User-Defined (`0x7FFF`) packet, or a Control Type not
143/// defined by Table 1. The CIF is genuinely opaque here — the only defined
144/// use is the Key Material message (§3.2.2) via `Subtype`
145/// `SRT_CMD_KMREQ`/`SRT_CMD_KMRSP` — see [`Self::as_key_material`].
146#[derive(Debug, Clone, PartialEq)]
147#[cfg_attr(feature = "serde", derive(serde::Serialize))]
148pub struct UserDefinedPacket<'a> {
149    /// The raw 15-bit Control Type value.
150    pub control_type: u16,
151    /// The 16-bit Subtype — `SRT_CMD_KMREQ`/`SRT_CMD_KMRSP` when the CIF is
152    /// Key Material (Table 5), otherwise vendor-defined.
153    pub subtype: u16,
154    /// The header `Type-specific Information` word.
155    pub type_specific_info: u32,
156    /// Timestamp (§3).
157    pub timestamp: SrtTimestamp,
158    /// Destination Socket ID (§3).
159    pub dest_socket_id: SocketId,
160    /// The raw CIF bytes.
161    pub cif: &'a [u8],
162}
163
164impl<'a> UserDefinedPacket<'a> {
165    /// Attempt to decode [`Self::cif`] as a Key Material message (§3.2.2).
166    /// Valid regardless of [`Self::subtype`] — callers that care should check
167    /// `subtype` against `SRT_CMD_KMREQ`/`SRT_CMD_KMRSP` (Table 5) first.
168    pub fn as_key_material(&self) -> Result<super::KeyMaterial<'a>> {
169        super::KeyMaterial::parse(self.cif)
170    }
171}
172
173/// An SRT control packet (`draft-sharabayko-srt-01` §3.2).
174#[derive(Debug, Clone, PartialEq)]
175#[cfg_attr(feature = "serde", derive(serde::Serialize))]
176#[non_exhaustive]
177pub enum ControlPacket<'a> {
178    /// Handshake (§3.2.1).
179    Handshake(HandshakePacket<'a>),
180    /// Keep-Alive (§3.2.3).
181    KeepAlive(KeepAlivePacket),
182    /// ACK (§3.2.4).
183    Ack(AckPacket),
184    /// NAK / Loss Report (§3.2.5).
185    Nak(NakPacket),
186    /// Congestion Warning (§3.2.6).
187    CongestionWarning(CongestionWarningPacket),
188    /// Shutdown (§3.2.7).
189    Shutdown(ShutdownPacket),
190    /// ACKACK (§3.2.8).
191    AckAck(AckAckPacket),
192    /// Message Drop Request (§3.2.9).
193    DropReq(DropReqPacket),
194    /// Peer Error (§3.2.10).
195    PeerError(PeerErrorPacket),
196    /// User-Defined Type, or a Control Type Table 1 does not define.
197    UserDefined(UserDefinedPacket<'a>),
198}
199
200fn check_reserved_u16(what: &'static str, v: u16) -> Result<()> {
201    if v != 0 {
202        return Err(Error::ReservedBits(ReservedBits {
203            field: what,
204            expected: 0,
205            found: u64::from(v),
206        }));
207    }
208    Ok(())
209}
210
211fn check_reserved_u32(what: &'static str, v: u32) -> Result<()> {
212    if v != 0 {
213        return Err(Error::ReservedBits(ReservedBits {
214            field: what,
215            expected: 0,
216            found: u64::from(v),
217        }));
218    }
219    Ok(())
220}
221
222/// Bytes libsrt appends to a control packet type whose CIF the spec documents
223/// as absent (Keep-Alive §3.2.3, Congestion Warning §3.2.6, Shutdown §3.2.7,
224/// ACKACK §3.2.8, Peer Error §3.2.10). libsrt's `CPacket::pack` always calls
225/// `m_PacketVector[PV_DATA].set(&m_extra_pad, 4)` for these types — its own
226/// comment says why: "control info field should be none but writev does not
227/// allow this" (`packet.cpp`, `CPacket::pack`, e.g. the `UMSG_KEEPALIVE` arm)
228/// — and `m_extra_pad` is a zero-initialized member, so the 4 bytes are
229/// always zero on the wire. Confirmed against a real `srt-live-transmit`
230/// 1.5.5 KEEPALIVE/ACKACK capture (`../fixtures/srt/libsrt-*.bin`).
231const LIBSRT_CIF_PAD_LEN: usize = 4;
232
233/// Accept either the pure-spec empty CIF or libsrt's 4-byte zero pad (see
234/// [`LIBSRT_CIF_PAD_LEN`]); reject anything else. A non-zero 4-byte value is
235/// rejected the same way a defined reserved field is (`ReservedFieldNotZero`)
236/// — nothing defines those bits, so a peer setting them to something else is
237/// not a shape this crate recognizes. Returns `true` if the 4-byte pad was
238/// present, `false` for the empty CIF.
239fn check_no_cif_or_libsrt_pad(what: &'static str, cif: &[u8]) -> Result<bool> {
240    match cif.len() {
241        0 => Ok(false),
242        LIBSRT_CIF_PAD_LEN => {
243            let value = be32(cif, 0);
244            if value != 0 {
245                return Err(Error::ReservedBits(ReservedBits {
246                    field: what,
247                    expected: 0,
248                    found: u64::from(value),
249                }));
250            }
251            Ok(true)
252        }
253        extra => Err(Error::UnexpectedTrailingBytes { what, extra }),
254    }
255}
256
257impl<'a> ControlPacket<'a> {
258    /// Parse a control packet from `bytes` (the full SRT packet).
259    ///
260    /// # Errors
261    /// [`Error::BufferTooShort`] if shorter than the 16-byte header;
262    /// [`Error::WrongPacketKind`] if the `F` bit is clear (this is a data
263    /// packet); type-specific errors from the dispatched CIF parser.
264    pub fn parse(bytes: &'a [u8]) -> Result<Self> {
265        need_in(bytes, SRT_HEADER_LEN, "SRT control packet header")?;
266        let word0 = be32(bytes, 0);
267        if word0 & super::F_BIT == 0 {
268            return Err(Error::WrongPacketKind {
269                expected: "control packet (F=1)",
270            });
271        }
272        let control_type_bits = ((word0 >> 16) & 0x7FFF) as u16;
273        let subtype = (word0 & 0xFFFF) as u16;
274        let type_specific_info = be32(bytes, 4);
275        let timestamp = SrtTimestamp::from_wire(be32(bytes, 8));
276        let dest_socket_id = SocketId::from_wire(be32(bytes, 12));
277        let cif = &bytes[SRT_HEADER_LEN..];
278        let control_type = ControlType::from_bits(control_type_bits);
279
280        Ok(match control_type {
281            ControlType::Handshake => {
282                check_reserved_u16("Subtype", subtype)?;
283                check_reserved_u32("Type-specific Information", type_specific_info)?;
284                ControlPacket::Handshake(HandshakePacket::parse_cif(
285                    timestamp,
286                    dest_socket_id,
287                    cif,
288                )?)
289            }
290            ControlType::KeepAlive => {
291                check_reserved_u16("Subtype", subtype)?;
292                check_reserved_u32("Type-specific Information", type_specific_info)?;
293                let libsrt_pad = check_no_cif_or_libsrt_pad("keep-alive CIF", cif)?;
294                ControlPacket::KeepAlive(KeepAlivePacket {
295                    timestamp,
296                    dest_socket_id,
297                    libsrt_pad,
298                })
299            }
300            ControlType::Ack => {
301                check_reserved_u16("Subtype", subtype)?;
302                ControlPacket::Ack(AckPacket::parse_cif(
303                    type_specific_info,
304                    timestamp,
305                    dest_socket_id,
306                    cif,
307                )?)
308            }
309            ControlType::Nak => {
310                check_reserved_u16("Subtype", subtype)?;
311                check_reserved_u32("Type-specific Information", type_specific_info)?;
312                ControlPacket::Nak(NakPacket::parse_cif(timestamp, dest_socket_id, cif)?)
313            }
314            ControlType::CongestionWarning => {
315                check_reserved_u16("Subtype", subtype)?;
316                check_reserved_u32("Type-specific Information", type_specific_info)?;
317                let libsrt_pad = check_no_cif_or_libsrt_pad("congestion warning CIF", cif)?;
318                ControlPacket::CongestionWarning(CongestionWarningPacket {
319                    timestamp,
320                    dest_socket_id,
321                    libsrt_pad,
322                })
323            }
324            ControlType::Shutdown => {
325                check_reserved_u16("Subtype", subtype)?;
326                check_reserved_u32("Type-specific Information", type_specific_info)?;
327                let libsrt_pad = check_no_cif_or_libsrt_pad("shutdown CIF", cif)?;
328                ControlPacket::Shutdown(ShutdownPacket {
329                    timestamp,
330                    dest_socket_id,
331                    libsrt_pad,
332                })
333            }
334            ControlType::AckAck => {
335                check_reserved_u16("Subtype", subtype)?;
336                let libsrt_pad = check_no_cif_or_libsrt_pad("ACKACK CIF", cif)?;
337                ControlPacket::AckAck(AckAckPacket {
338                    ack_number: AckNumber::from_wire(type_specific_info),
339                    timestamp,
340                    dest_socket_id,
341                    libsrt_pad,
342                })
343            }
344            ControlType::DropReq => {
345                check_reserved_u16("Subtype", subtype)?;
346                ControlPacket::DropReq(DropReqPacket::parse_cif(
347                    type_specific_info,
348                    timestamp,
349                    dest_socket_id,
350                    cif,
351                )?)
352            }
353            ControlType::PeerError => {
354                check_reserved_u16("Subtype", subtype)?;
355                let libsrt_pad = check_no_cif_or_libsrt_pad("peer error CIF", cif)?;
356                ControlPacket::PeerError(PeerErrorPacket {
357                    error_code: type_specific_info,
358                    timestamp,
359                    dest_socket_id,
360                    libsrt_pad,
361                })
362            }
363            ControlType::UserDefined | ControlType::Reserved(_) => {
364                ControlPacket::UserDefined(UserDefinedPacket {
365                    control_type: control_type_bits,
366                    subtype,
367                    type_specific_info,
368                    timestamp,
369                    dest_socket_id,
370                    cif,
371                })
372            }
373        })
374    }
375
376    /// The `Control Type` this packet carries.
377    pub fn control_type(&self) -> ControlType {
378        match self {
379            ControlPacket::Handshake(_) => ControlType::Handshake,
380            ControlPacket::KeepAlive(_) => ControlType::KeepAlive,
381            ControlPacket::Ack(_) => ControlType::Ack,
382            ControlPacket::Nak(_) => ControlType::Nak,
383            ControlPacket::CongestionWarning(_) => ControlType::CongestionWarning,
384            ControlPacket::Shutdown(_) => ControlType::Shutdown,
385            ControlPacket::AckAck(_) => ControlType::AckAck,
386            ControlPacket::DropReq(_) => ControlType::DropReq,
387            ControlPacket::PeerError(_) => ControlType::PeerError,
388            ControlPacket::UserDefined(u) => ControlType::from_bits(u.control_type),
389        }
390    }
391
392    fn subtype(&self) -> u16 {
393        match self {
394            ControlPacket::UserDefined(u) => u.subtype,
395            _ => 0,
396        }
397    }
398
399    fn word1(&self) -> Result<u32> {
400        Ok(match self {
401            ControlPacket::Handshake(_)
402            | ControlPacket::KeepAlive(_)
403            | ControlPacket::Nak(_)
404            | ControlPacket::CongestionWarning(_)
405            | ControlPacket::Shutdown(_) => 0,
406            // An ACK's and an ACKACK's Acknowledgement Number fill their
407            // whole `Type-specific Information` word (§3.2.4, §3.2.8): every
408            // `u32` is a legal value, so nothing is bounded here (it was, to
409            // 31 bits, which made serialize refuse a packet parse accepted).
410            ControlPacket::Ack(a) => a.ack_number.value(),
411            ControlPacket::AckAck(a) => a.ack_number.value(),
412            ControlPacket::DropReq(d) => d.message_number.value(),
413            ControlPacket::PeerError(p) => p.error_code,
414            ControlPacket::UserDefined(u) => u.type_specific_info,
415        })
416    }
417
418    fn timestamp(&self) -> SrtTimestamp {
419        match self {
420            ControlPacket::Handshake(h) => h.timestamp,
421            ControlPacket::KeepAlive(k) => k.timestamp,
422            ControlPacket::Ack(a) => a.timestamp,
423            ControlPacket::Nak(n) => n.timestamp,
424            ControlPacket::CongestionWarning(c) => c.timestamp,
425            ControlPacket::Shutdown(s) => s.timestamp,
426            ControlPacket::AckAck(a) => a.timestamp,
427            ControlPacket::DropReq(d) => d.timestamp,
428            ControlPacket::PeerError(p) => p.timestamp,
429            ControlPacket::UserDefined(u) => u.timestamp,
430        }
431    }
432
433    fn dest_socket_id(&self) -> SocketId {
434        match self {
435            ControlPacket::Handshake(h) => h.dest_socket_id,
436            ControlPacket::KeepAlive(k) => k.dest_socket_id,
437            ControlPacket::Ack(a) => a.dest_socket_id,
438            ControlPacket::Nak(n) => n.dest_socket_id,
439            ControlPacket::CongestionWarning(c) => c.dest_socket_id,
440            ControlPacket::Shutdown(s) => s.dest_socket_id,
441            ControlPacket::AckAck(a) => a.dest_socket_id,
442            ControlPacket::DropReq(d) => d.dest_socket_id,
443            ControlPacket::PeerError(p) => p.dest_socket_id,
444            ControlPacket::UserDefined(u) => u.dest_socket_id,
445        }
446    }
447
448    fn cif_len(&self) -> usize {
449        match self {
450            ControlPacket::Handshake(h) => h.cif_len(),
451            // Emit libsrt's 4-byte zero pad (see `LIBSRT_CIF_PAD_LEN`) when
452            // requested, or empty CIF otherwise, so packets round-trip
453            // byte-identically regardless of which spec-legal shape they parsed.
454            ControlPacket::KeepAlive(k) => {
455                if k.libsrt_pad {
456                    LIBSRT_CIF_PAD_LEN
457                } else {
458                    0
459                }
460            }
461            ControlPacket::CongestionWarning(c) => {
462                if c.libsrt_pad {
463                    LIBSRT_CIF_PAD_LEN
464                } else {
465                    0
466                }
467            }
468            ControlPacket::Shutdown(s) => {
469                if s.libsrt_pad {
470                    LIBSRT_CIF_PAD_LEN
471                } else {
472                    0
473                }
474            }
475            ControlPacket::AckAck(a) => {
476                if a.libsrt_pad {
477                    LIBSRT_CIF_PAD_LEN
478                } else {
479                    0
480                }
481            }
482            ControlPacket::PeerError(p) => {
483                if p.libsrt_pad {
484                    LIBSRT_CIF_PAD_LEN
485                } else {
486                    0
487                }
488            }
489            ControlPacket::Ack(a) => a.cif_len(),
490            ControlPacket::Nak(n) => n.cif_len(),
491            ControlPacket::DropReq(d) => d.cif_len(),
492            ControlPacket::UserDefined(u) => u.cif.len(),
493        }
494    }
495
496    /// Number of bytes [`Self::serialize_into`] will write.
497    pub fn serialized_len(&self) -> usize {
498        SRT_HEADER_LEN + self.cif_len()
499    }
500
501    /// Serialize this control packet into `buf`.
502    pub fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
503        let len = self.serialized_len();
504        need_out(buf, len)?;
505        let control_type_bits = self.control_type().to_bits();
506        broadcast_common::len::fit_bits(u64::from(control_type_bits), 15, "Control Type")?;
507        let word0 = super::F_BIT | (u32::from(control_type_bits) << 16) | u32::from(self.subtype());
508        put_be32(buf, 0, word0);
509        put_be32(buf, 4, self.word1()?);
510        put_be32(buf, 8, self.timestamp().value());
511        put_be32(buf, 12, self.dest_socket_id().value());
512        let cif = &mut buf[SRT_HEADER_LEN..len];
513        match self {
514            ControlPacket::Handshake(h) => {
515                h.write_cif(cif)?;
516            }
517            ControlPacket::KeepAlive(_)
518            | ControlPacket::CongestionWarning(_)
519            | ControlPacket::Shutdown(_)
520            | ControlPacket::AckAck(_)
521            | ControlPacket::PeerError(_) => {
522                cif.fill(0);
523            }
524            ControlPacket::Ack(a) => {
525                a.write_cif(cif)?;
526            }
527            ControlPacket::Nak(n) => {
528                n.write_cif(cif);
529            }
530            ControlPacket::DropReq(d) => {
531                d.write_cif(cif)?;
532            }
533            ControlPacket::UserDefined(u) => {
534                cif.copy_from_slice(u.cif);
535            }
536        }
537        Ok(len)
538    }
539}
540
541impl<'a> broadcast_common::Parse<'a> for ControlPacket<'a> {
542    type Error = crate::Error;
543
544    fn parse(bytes: &'a [u8]) -> core::result::Result<Self, Self::Error> {
545        ControlPacket::parse(bytes)
546    }
547}
548
549impl broadcast_common::Serialize for ControlPacket<'_> {
550    type Error = crate::Error;
551
552    fn serialized_len(&self) -> usize {
553        ControlPacket::serialized_len(self)
554    }
555
556    fn serialize_into(&self, buf: &mut [u8]) -> core::result::Result<usize, Self::Error> {
557        ControlPacket::serialize_into(self, buf)
558    }
559}
560
561#[cfg(test)]
562mod tests {
563    use super::*;
564
565    #[test]
566    fn every_control_type_round_trips_its_table_1_wire_value() {
567        for (ty, value) in [
568            (ControlType::Handshake, 0x0000u16),
569            (ControlType::KeepAlive, 0x0001),
570            (ControlType::Ack, 0x0002),
571            (ControlType::Nak, 0x0003),
572            (ControlType::CongestionWarning, 0x0004),
573            (ControlType::Shutdown, 0x0005),
574            (ControlType::AckAck, 0x0006),
575            (ControlType::DropReq, 0x0007),
576            (ControlType::PeerError, 0x0008),
577            (ControlType::UserDefined, 0x7FFF),
578        ] {
579            assert_eq!(ty.to_bits(), value, "{}", ty.name());
580            assert_eq!(ControlType::from_bits(value), ty);
581        }
582    }
583
584    #[test]
585    fn a_control_type_off_table_1_is_reserved_and_keeps_its_value() {
586        // 0x0009..=0x7FFE are not in Table 1; the enum, not a raw constant,
587        // is the public way to name and recover them.
588        for value in [0x0009u16, 0x0010, 0x7FFE] {
589            assert_eq!(ControlType::from_bits(value), ControlType::Reserved(value));
590            assert_eq!(ControlType::from_bits(value).to_bits(), value);
591        }
592        assert_eq!(ControlType::Reserved(0x0010).name(), "reserved");
593    }
594}