Skip to main content

srt_runtime/
error.rs

1//! Error type for SRT packet parsing and serialization.
2//!
3//! Spec grounding: `draft-sharabayko-srt-01` §3 (Packet Structure).
4
5/// Result alias for SRT packet parsing/serialization.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// An SRT packet parse / serialize error.
9#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// Input shorter than required.
13    #[error(transparent)]
14    BufferTooShort(#[from] broadcast_common::error::BufferTooShort),
15    /// The output buffer passed to `serialize_into` was too small.
16    #[error(transparent)]
17    OutputBufferTooSmall(#[from] broadcast_common::error::OutputBufferTooSmall),
18    /// A field value did not fit in its wire bit-width.
19    #[error(transparent)]
20    FieldOverflow(#[from] broadcast_common::error::FieldOverflow),
21    /// The Control Information Field length did not match any known ACK
22    /// variant (Full / Small / Light — `draft-sharabayko-srt-01` §3.2.4).
23    #[error("ACK CIF length {len} does not match Full (28), Small (16), or Light (4)")]
24    InvalidAckLength {
25        /// The CIF length actually found, in bytes.
26        len: usize,
27    },
28    /// A NAK loss-list (`draft-sharabayko-srt-01` Appendix A) was not a whole
29    /// number of 4-byte entries, or a range entry's second word had its top
30    /// bit set (which would make it a nested range).
31    #[error("invalid NAK loss list: {reason}")]
32    InvalidLossList {
33        /// Why the loss list is invalid.
34        reason: &'static str,
35    },
36    /// A Handshake Extension block's declared length (in 4-byte units)
37    /// overran the remaining CIF bytes (`draft-sharabayko-srt-01` §3.2.1).
38    #[error("handshake extension length {declared} * 4 bytes overruns {remaining} remaining")]
39    ExtensionOverrun {
40        /// The declared `Extension Length` (in 4-byte blocks).
41        declared: u16,
42        /// The bytes actually remaining in the CIF.
43        remaining: usize,
44    },
45    /// The Stream ID extension contents were not valid UTF-8 after undoing the
46    /// 32-bit little-endian word storage (`draft-sharabayko-srt-01` §3.2.1.3).
47    #[error("invalid Stream ID extension UTF-8")]
48    InvalidStreamIdUtf8,
49    /// A Key Material message (`draft-sharabayko-srt-01` §3.2.2) fixed-value
50    /// field did not carry its mandated value.
51    #[error("invalid key material {field}: {reason}")]
52    InvalidKeyMaterial {
53        /// The offending field.
54        field: &'static str,
55        /// Why it is invalid.
56        reason: &'static str,
57    },
58    /// A type-specific `parse` was called on bytes whose `F` bit (or Control
59    /// Type) indicated the other packet kind.
60    #[error("wrong packet kind: expected {expected}")]
61    WrongPacketKind {
62        /// What the caller expected to parse.
63        expected: &'static str,
64    },
65    /// A field documented as reserved / must-be-zero (`draft-sharabayko-srt-01`
66    /// §3.2, e.g. `Subtype` on a defined control type, or the header
67    /// `Type-specific Information` word where the packet type does not use
68    /// it) carried a non-zero value.
69    #[error(transparent)]
70    ReservedBits(#[from] broadcast_common::error::ReservedBits),
71    /// A general structural constraint (not a bit-width overflow) was
72    /// violated — e.g. a length that must be a whole number of 4-byte words.
73    #[error(transparent)]
74    InvalidField(#[from] broadcast_common::error::InvalidField),
75    /// A Control Information Field documented as absent (Keep-Alive,
76    /// Congestion Warning, Shutdown, ACKACK, Peer Error — §3.2) carried extra
77    /// bytes.
78    #[error("{what} has {extra} unexpected trailing byte(s)")]
79    UnexpectedTrailingBytes {
80        /// What was being parsed.
81        what: &'static str,
82        /// How many bytes were left over.
83        extra: usize,
84    },
85    /// The handshake state machine (`crate::caller`/`crate::listener`) was
86    /// fed a control packet that is not a Handshake packet
87    /// (`draft-sharabayko-srt-01` §3.2.1) — only Handshake packets
88    /// participate in the exchange modeled there.
89    #[error("expected a Handshake control packet, got {actual}")]
90    UnexpectedControlPacket {
91        /// The `Control Type` label of the packet actually fed.
92        actual: &'static str,
93    },
94    /// A handshake state-machine call happened in a state where the draft's
95    /// flow (`draft-sharabayko-srt-01` §4.3.1) does not expect it — a driver
96    /// bug (e.g. calling `start()` twice, or `feed()` after `Connected`),
97    /// not a peer protocol failure.
98    #[error("handshake call not valid in state {state}: {reason}")]
99    HandshakeOutOfSequence {
100        /// The state the handshake was in.
101        state: &'static str,
102        /// Why the call is not valid there.
103        reason: &'static str,
104    },
105    /// The peer refused the handshake, with the reason it gave
106    /// (`draft-sharabayko-srt-01` §4.3, Table 7) — a wrong passphrase
107    /// ([`crate::handshake_sm::RejectionReason::BadSecret`]) is distinguishable from a refused
108    /// stream or a version mismatch.
109    #[error("handshake rejected: {0}")]
110    Rejected(crate::handshake_sm::RejectionReason),
111    /// The handshake got no (complete) answer within its time or
112    /// retransmission budget.
113    #[error("handshake timed out ({stage})")]
114    HandshakeTimedOut {
115        /// Which budget ran out.
116        stage: &'static str,
117    },
118    /// A handshake step failed; `source` is the underlying parse / engine
119    /// error and `stage` names where it happened.
120    #[error("handshake failed during {stage}: {source}")]
121    Handshake {
122        /// Where in the handshake the failure happened.
123        stage: &'static str,
124        /// The underlying error.
125        #[source]
126        source: alloc::boxed::Box<Error>,
127    },
128    /// A real-socket I/O failure surfaced by [`crate::io`]'s tokio adapter
129    /// (feature `tokio`, which implies `std`). Carries the OS
130    /// [`std::io::ErrorKind`] — so e.g. a bind failure (`AddrInUse`) is
131    /// distinguishable from a mid-connection reset (`ConnectionReset`) — plus
132    /// a fixed `context` naming the failing call site (`"bind"`, `"connect"`,
133    /// `"recv"`, `"send"`, …). `std::io::Error` itself is not
134    /// `Clone`/`PartialEq`/`Eq` (this enum derives all three), so only its
135    /// `kind()` is kept rather than the full error.
136    #[cfg(feature = "std")]
137    #[error("io error during {context}: {kind:?}")]
138    Io {
139        /// The `std::io::Error::kind()` of the underlying OS error.
140        kind: std::io::ErrorKind,
141        /// Which call site failed (e.g. `"bind"`, `"connect"`, `"recv"`,
142        /// `"send"`).
143        context: &'static str,
144    },
145}