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_core::error::BufferTooShort),
15 /// The output buffer passed to `serialize_into` was too small.
16 #[error(transparent)]
17 OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),
18 /// A field value did not fit in its wire bit-width.
19 #[error(transparent)]
20 FieldOverflow(#[from] broadcast_core::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 #[non_exhaustive]
25 InvalidAckLength {
26 /// The CIF length actually found, in bytes.
27 len: usize,
28 },
29 /// A NAK loss-list (`draft-sharabayko-srt-01` Appendix A) was not a whole
30 /// number of 4-byte entries, or a range entry's second word had its top
31 /// bit set (which would make it a nested range).
32 #[error("invalid NAK loss list: {reason}")]
33 #[non_exhaustive]
34 InvalidLossList {
35 /// Why the loss list is invalid.
36 reason: &'static str,
37 },
38 /// A Handshake Extension block's declared length (in 4-byte units)
39 /// overran the remaining CIF bytes (`draft-sharabayko-srt-01` §3.2.1).
40 #[error("handshake extension length {declared} * 4 bytes overruns {remaining} remaining")]
41 #[non_exhaustive]
42 ExtensionOverrun {
43 /// The declared `Extension Length` (in 4-byte blocks).
44 declared: u16,
45 /// The bytes actually remaining in the CIF.
46 remaining: usize,
47 },
48 /// The Stream ID extension contents were not valid UTF-8 after undoing the
49 /// 32-bit little-endian word storage (`draft-sharabayko-srt-01` §3.2.1.3).
50 #[error("invalid Stream ID extension UTF-8")]
51 InvalidStreamIdUtf8,
52 /// A Key Material message (`draft-sharabayko-srt-01` §3.2.2) fixed-value
53 /// field did not carry its mandated value.
54 #[error("invalid key material {field}: {reason}")]
55 #[non_exhaustive]
56 InvalidKeyMaterial {
57 /// The offending field.
58 field: &'static str,
59 /// Why it is invalid.
60 reason: &'static str,
61 },
62 /// A type-specific `parse` was called on bytes whose `F` bit (or Control
63 /// Type) indicated the other packet kind.
64 #[error("wrong packet kind: expected {expected}")]
65 #[non_exhaustive]
66 WrongPacketKind {
67 /// What the caller expected to parse.
68 expected: &'static str,
69 },
70 /// A field documented as reserved / must-be-zero (`draft-sharabayko-srt-01`
71 /// §3.2, e.g. `Subtype` on a defined control type, or the header
72 /// `Type-specific Information` word where the packet type does not use
73 /// it) carried a non-zero value.
74 #[error(transparent)]
75 ReservedBits(#[from] broadcast_core::error::ReservedBits),
76 /// A general structural constraint (not a bit-width overflow) was
77 /// violated — e.g. a length that must be a whole number of 4-byte words.
78 #[error(transparent)]
79 InvalidField(#[from] broadcast_core::error::InvalidField),
80 /// A Control Information Field documented as absent (Keep-Alive,
81 /// Congestion Warning, Shutdown, ACKACK, Peer Error — §3.2) carried extra
82 /// bytes.
83 #[error("{what} has {extra} unexpected trailing byte(s)")]
84 #[non_exhaustive]
85 UnexpectedTrailingBytes {
86 /// What was being parsed.
87 what: &'static str,
88 /// How many bytes were left over.
89 extra: usize,
90 },
91 /// The handshake state machine (`crate::caller`/`crate::listener`) was
92 /// fed a control packet that is not a Handshake packet
93 /// (`draft-sharabayko-srt-01` §3.2.1) — only Handshake packets
94 /// participate in the exchange modeled there.
95 #[error("expected a Handshake control packet, got {actual}")]
96 #[non_exhaustive]
97 UnexpectedControlPacket {
98 /// The `Control Type` label of the packet actually fed.
99 actual: &'static str,
100 },
101 /// A handshake state-machine call happened in a state where the draft's
102 /// flow (`draft-sharabayko-srt-01` §4.3.1) does not expect it — a driver
103 /// bug (e.g. calling `start()` twice, or `feed()` after `Connected`),
104 /// not a peer protocol failure.
105 #[error("handshake call not valid in state {state}: {reason}")]
106 #[non_exhaustive]
107 HandshakeOutOfSequence {
108 /// The state the handshake was in.
109 state: &'static str,
110 /// Why the call is not valid there.
111 reason: &'static str,
112 },
113 /// The peer refused the handshake, with the reason it gave
114 /// (`draft-sharabayko-srt-01` §4.3, Table 7) — a wrong passphrase
115 /// ([`crate::handshake_sm::RejectionReason::BadSecret`]) is distinguishable from a refused
116 /// stream or a version mismatch.
117 #[error("handshake rejected: {0}")]
118 Rejected(crate::handshake_sm::RejectionReason),
119 /// The handshake got no (complete) answer within its time or
120 /// retransmission budget.
121 #[error("handshake timed out ({stage})")]
122 #[non_exhaustive]
123 HandshakeTimedOut {
124 /// Which budget ran out.
125 stage: &'static str,
126 },
127 /// A handshake step failed; `source` is the underlying parse / engine
128 /// error and `stage` names where it happened.
129 #[error("handshake failed during {stage}: {source}")]
130 #[non_exhaustive]
131 Handshake {
132 /// Where in the handshake the failure happened.
133 stage: &'static str,
134 /// The underlying error.
135 #[source]
136 source: alloc::boxed::Box<Error>,
137 },
138 /// A caller-supplied configuration value cannot work: refused when the
139 /// configuration is built or first used, never repaired.
140 #[error("invalid configuration {field}: {rule}")]
141 #[non_exhaustive]
142 InvalidConfig {
143 /// The offending field.
144 field: &'static str,
145 /// The rule it breaks.
146 rule: &'static str,
147 },
148 /// The caller's [`broadcast_core::damage::DamagePolicy`] refused a
149 /// damage report: the connection ends with the same report inside this
150 /// error (`crate::io::SrtSocket::recv_event`).
151 #[error(transparent)]
152 Damage(#[from] broadcast_core::damage::DamageError),
153 /// A real-socket I/O failure surfaced by [`crate::io`]'s tokio adapter
154 /// (feature `tokio`, which implies `std`). Carries the OS
155 /// [`std::io::ErrorKind`] — so e.g. a bind failure (`AddrInUse`) is
156 /// distinguishable from a mid-connection reset (`ConnectionReset`) — plus
157 /// a fixed `context` naming the failing call site (`"bind"`, `"connect"`,
158 /// `"recv"`, `"send"`, …). `std::io::Error` itself is not
159 /// `Clone`/`PartialEq`/`Eq` (this enum derives all three), so only its
160 /// `kind()` is kept rather than the full error.
161 #[cfg(feature = "std")]
162 #[error("io error during {context}: {kind:?}")]
163 #[non_exhaustive]
164 Io {
165 /// The `std::io::Error::kind()` of the underlying OS error.
166 kind: std::io::ErrorKind,
167 /// Which call site failed (e.g. `"bind"`, `"connect"`, `"recv"`,
168 /// `"send"`).
169 context: &'static str,
170 },
171}
172
173impl Error {
174 /// Builds [`Self::InvalidConfig`].
175 #[must_use]
176 pub const fn invalid_config(field: &'static str, rule: &'static str) -> Self {
177 Self::InvalidConfig { field, rule }
178 }
179 /// Builds [`Self::HandshakeTimedOut`].
180 #[must_use]
181 pub fn handshake_timed_out(stage: &'static str) -> Self {
182 Self::HandshakeTimedOut { stage }
183 }
184 /// Builds [`Self::Io`].
185 #[cfg(feature = "std")]
186 #[must_use]
187 pub fn io(kind: std::io::ErrorKind, context: &'static str) -> Self {
188 Self::Io { kind, context }
189 }
190}