Skip to main content

rtmp_runtime/
error.rs

1//! Error type for the RTMP ingest session engine.
2//!
3//! Structured [`thiserror`] errors covering the failure surface of the
4//! sans-IO engine: handshake framing (§5.2), chunk stream parsing (§5.3),
5//! message reassembly (§6), AMF0 decoding (`[AMF0]`), and session state-machine
6//! violations (§7.2) — see `private/specs-verified/rtmp/rtmp-runtime/rtmp.md`.
7
8use crate::amf0::Amf0Value;
9
10/// Errors produced by the RTMP ingest session engine.
11#[non_exhaustive]
12#[derive(Debug, thiserror::Error, Clone, PartialEq)]
13pub enum RtmpError {
14    /// Not enough input bytes were available to parse a complete structure
15    /// (handshake message, chunk header, or reassembled RTMP message).
16    #[error(transparent)]
17    BufferTooShort(#[from] broadcast_common::error::BufferTooShort),
18
19    /// The input did not conform to the expected wire layout (bad handshake
20    /// version, invalid chunk `fmt`/CSID encoding, a length that overruns
21    /// its buffer, or similar).
22    #[error("malformed {what}")]
23    Malformed {
24        /// What was being parsed when the malformed data was found.
25        what: &'static str,
26    },
27
28    /// A Set Chunk Size announced a value this engine cannot adopt: zero, a
29    /// non-zero reserved top bit (§5.4.1), or above `MAX_CHUNK_SIZE`.
30    #[error("chunk size {value} is outside the range this engine accepts")]
31    ChunkSizeOutOfRange {
32        /// The announced value, as it was on the wire.
33        value: u32,
34    },
35
36    /// A Set Peer Bandwidth carried a limit type byte outside `0..=2`
37    /// (§5.4.5).
38    #[error("set peer bandwidth limit type {value} is not 0..=2")]
39    InvalidLimitType {
40        /// The offending byte.
41        value: u8,
42    },
43
44    /// A User Control message carried an event type §6.7 does not define.
45    #[error("unsupported user control event type {value}")]
46    UnsupportedUserControlEvent {
47        /// The offending event type.
48        value: u16,
49    },
50
51    /// An AMF0 value used a marker this engine does not decode (`[AMF0]`
52    /// §2.1's reserved, legacy and AMF3-switch markers).
53    #[error("unsupported AMF0 marker {marker:#04x}")]
54    UnsupportedAmf0Marker {
55        /// The offending marker byte.
56        marker: u8,
57    },
58
59    /// AMF0 data nested containers deeper than
60    /// [`MAX_AMF0_DEPTH`](crate::amf0::MAX_AMF0_DEPTH).
61    #[error("AMF0 nesting depth exceeds {max}")]
62    Amf0DepthExceeded {
63        /// The limit that was reached.
64        max: usize,
65    },
66
67    /// AMF0 data held more values than
68    /// [`MAX_AMF0_VALUES`](crate::amf0::MAX_AMF0_VALUES) — a wide container
69    /// (millions of one-byte nulls, or three-byte empty-key pairs) would
70    /// otherwise expand into millions of `Amf0Value`s from one message.
71    #[error("AMF0 value count exceeds {max}")]
72    Amf0TooManyValues {
73        /// The limit that was reached.
74        max: usize,
75    },
76
77    /// A Command Message body was longer than
78    /// [`MAX_COMMAND_LEN`](crate::server::MAX_COMMAND_LEN).
79    #[error("command message is {len} bytes, above the {max}-byte limit")]
80    CommandTooLong {
81        /// The body length that was rejected.
82        len: usize,
83        /// The limit that was reached.
84        max: usize,
85    },
86
87    /// An AMF0 String carried more than the 65535 bytes its 16-bit length
88    /// prefix can hold; AMF0 has no Long String variant to fall back on
89    /// inside these messages.
90    #[error("{len} bytes do not fit the AMF0 string length prefix")]
91    Amf0StringTooLong {
92        /// The string's length.
93        len: usize,
94    },
95
96    /// The first value of a Command Message body was not an AMF0 String
97    /// (§7.1.1: the command name).
98    #[error("command name is not an AMF0 string: {}", displayed(.value))]
99    CommandNameNotString {
100        /// The value that stood where the name belongs.
101        value: Amf0Value,
102    },
103
104    /// The second value of a Command Message body was not an AMF0 Number
105    /// (§7.1.1: the transaction id).
106    #[error("command transaction id is not an AMF0 number: {}", displayed(.value))]
107    CommandTransactionIdNotNumber {
108        /// The value that stood where the transaction id belongs.
109        value: Amf0Value,
110    },
111
112    /// The handshake version byte was not [`RTMP_VERSION`](crate::handshake::RTMP_VERSION)
113    /// (§5.2.2).
114    #[error("handshake version {value} is not {expected}")]
115    HandshakeVersion {
116        /// The version byte received.
117        value: u8,
118        /// The version this engine speaks.
119        expected: u8,
120    },
121
122    /// An AMF0 Date carried a non-zero reserved time-zone field (`[AMF0]`
123    /// §2.13: it MUST be `0x0000`).
124    #[error("AMF0 date reserved time zone is {value}, must be 0")]
125    Amf0DateTimeZone {
126        /// The offending field.
127        value: u16,
128    },
129
130    /// An AMF0 value was not valid UTF-8 where the format requires text
131    /// (`[AMF0]` §2.4/§2.5/§2.14).
132    #[error("invalid AMF0 {what}")]
133    Amf0Utf8 {
134        /// What held the invalid text.
135        what: &'static str,
136    },
137
138    /// Reassembly held more distinct chunk streams than the per-connection
139    /// bound allows.
140    #[error("more than {max} concurrent chunk streams")]
141    TooManyChunkStreams {
142        /// The bound that was reached.
143        max: usize,
144    },
145
146    /// The in-progress reassembly buffer grew past the per-connection
147    /// budget (#1108/RTMP-W1).
148    #[error("buffered input exceeds {max} bytes")]
149    InputBudgetExceeded {
150        /// The budget, in bytes.
151        max: usize,
152    },
153
154    /// Assembling a message hit a chunk-stream state that cannot complete:
155    /// a continuation before any chunk, a header on a completed message, a
156    /// stream without a message type id, or a chunk that does not match its
157    /// message's declared length.
158    #[error("inconsistent chunk stream {chunk_stream_id}: {what}")]
159    ChunkStreamState {
160        /// The chunk stream id whose state did not hold.
161        chunk_stream_id: u32,
162        /// What was inconsistent.
163        what: &'static str,
164    },
165
166    /// An operation was attempted, or a message arrived, that is not valid
167    /// in the session's current state (e.g. a command received before the
168    /// handshake completed, or `publish` before `createStream`). The server
169    /// maps a rejected `publish` to `NetStream.Publish.BadName`; a client
170    /// sees a rejected stream key as this variant after `publish`.
171    #[error("unexpected state: {what}")]
172    UnexpectedState {
173        /// Description of the state violation.
174        what: &'static str,
175    },
176
177    /// A [`crate::chunk::Message`] was built with a `chunk_stream_id` outside
178    /// the basic header's encodable range (2..=65599, RTMP §5.3.1.1), so it
179    /// cannot be written.
180    #[error("chunk stream id {chunk_stream_id} is outside the encodable range 2..=65599")]
181    InvalidChunkStreamId {
182        /// The offending chunk stream id.
183        chunk_stream_id: u32,
184    },
185
186    /// A length or count did not fit the wire field it is written to
187    /// (#1129): a serializer that narrowed a `usize` with `as u32` (then
188    /// packed it into a 24-bit field) would have silently wrapped and
189    /// emitted a misframed chunk stream; this is returned instead.
190    #[error(transparent)]
191    FieldOverflow(#[from] broadcast_common::error::FieldOverflow),
192
193    /// The output buffer passed to `serialize_into` was smaller than
194    /// `serialized_len()`.
195    #[error(transparent)]
196    OutputBufferTooSmall(#[from] broadcast_common::error::OutputBufferTooSmall),
197}
198
199/// Render one AMF0 value for an error message; the value's `Debug` form is
200/// the only lossless rendering it has.
201fn displayed(value: &Amf0Value) -> alloc::string::String {
202    alloc::format!("{value:?}")
203}
204
205impl RtmpError {
206    /// The error for an announced chunk size this engine cannot adopt.
207    pub(crate) const fn chunk_size_out_of_range(value: u32) -> Self {
208        RtmpError::ChunkSizeOutOfRange { value }
209    }
210}