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
//! Error type for the RTMP ingest session engine.
//!
//! Structured [`thiserror`] errors covering the failure surface of the
//! sans-IO engine: handshake framing (§5.2), chunk stream parsing (§5.3),
//! message reassembly (§6), AMF0 decoding (`[AMF0]`), and session state-machine
//! violations (§7.2) — see `private/specs-verified/rtmp/rtmp-runtime/rtmp.md`.
use crate::amf0::Amf0Value;
/// Errors produced by the RTMP ingest session engine.
#[non_exhaustive]
#[derive(Debug, thiserror::Error, Clone, PartialEq)]
pub enum RtmpError {
/// Not enough input bytes were available to parse a complete structure
/// (handshake message, chunk header, or reassembled RTMP message).
#[error(transparent)]
BufferTooShort(#[from] broadcast_core::error::BufferTooShort),
/// The input did not conform to the expected wire layout (bad handshake
/// version, invalid chunk `fmt`/CSID encoding, a length that overruns
/// its buffer, or similar).
#[error("malformed {what}")]
#[non_exhaustive]
Malformed {
/// What was being parsed when the malformed data was found.
what: &'static str,
},
/// A Set Chunk Size announced a value this engine cannot adopt: zero, a
/// non-zero reserved top bit (§5.4.1), or above the caller's `ChunkLimits::max_chunk_size`.
#[error("chunk size {value} is outside the range this engine accepts")]
#[non_exhaustive]
ChunkSizeOutOfRange {
/// The announced value, as it was on the wire.
value: u32,
},
/// A Set Peer Bandwidth carried a limit type byte outside `0..=2`
/// (§5.4.5).
#[error("set peer bandwidth limit type {value} is not 0..=2")]
#[non_exhaustive]
InvalidLimitType {
/// The offending byte.
value: u8,
},
/// A User Control message carried an event type §6.7 does not define.
#[error("unsupported user control event type {value}")]
#[non_exhaustive]
UnsupportedUserControlEvent {
/// The offending event type.
value: u16,
},
/// An AMF0 value used a marker this engine does not decode (`[AMF0]`
/// §2.1's reserved, legacy and AMF3-switch markers).
#[error("unsupported AMF0 marker {marker:#04x}")]
#[non_exhaustive]
UnsupportedAmf0Marker {
/// The offending marker byte.
marker: u8,
},
/// AMF0 data nested containers deeper than the caller's
/// [`Amf0Limits::max_depth`](crate::amf0::Amf0Limits::max_depth).
#[error("AMF0 nesting depth exceeds {max}")]
#[non_exhaustive]
Amf0DepthExceeded {
/// The limit that was reached.
max: usize,
},
/// AMF0 data held more values than the caller's
/// [`Amf0Limits::max_values`](crate::amf0::Amf0Limits::max_values) — a wide container
/// (millions of one-byte nulls, or three-byte empty-key pairs) would
/// otherwise expand into millions of `Amf0Value`s from one message.
#[error("AMF0 value count exceeds {max}")]
#[non_exhaustive]
Amf0TooManyValues {
/// The limit that was reached.
max: usize,
},
/// A Command Message body was longer than the caller's
/// [`ServerLimits::max_command_len`](crate::server::ServerLimits::max_command_len).
#[error("command message is {len} bytes, above the {max}-byte limit")]
#[non_exhaustive]
CommandTooLong {
/// The body length that was rejected.
len: usize,
/// The limit that was reached.
max: usize,
},
/// An AMF0 String carried more than the 65535 bytes its 16-bit length
/// prefix can hold; AMF0 has no Long String variant to fall back on
/// inside these messages.
#[error("{len} bytes do not fit the AMF0 string length prefix")]
#[non_exhaustive]
Amf0StringTooLong {
/// The string's length.
len: usize,
},
/// The first value of a Command Message body was not an AMF0 String
/// (§7.1.1: the command name).
#[error("command name is not an AMF0 string: {}", displayed(.value))]
#[non_exhaustive]
CommandNameNotString {
/// The value that stood where the name belongs.
value: Amf0Value,
},
/// The second value of a Command Message body was not an AMF0 Number
/// (§7.1.1: the transaction id).
#[error("command transaction id is not an AMF0 number: {}", displayed(.value))]
#[non_exhaustive]
CommandTransactionIdNotNumber {
/// The value that stood where the transaction id belongs.
value: Amf0Value,
},
/// The handshake version byte was not [`RTMP_VERSION`](crate::handshake::RTMP_VERSION)
/// (§5.2.2).
#[error("handshake version {value} is not {expected}")]
#[non_exhaustive]
HandshakeVersion {
/// The version byte received.
value: u8,
/// The version this engine speaks.
expected: u8,
},
/// An AMF0 value was not valid UTF-8 where the format requires text
/// (`[AMF0]` §2.4/§2.5/§2.14).
#[error("invalid AMF0 {what}")]
#[non_exhaustive]
Amf0Utf8 {
/// What held the invalid text.
what: &'static str,
},
/// Reassembly held more distinct chunk streams than the per-connection
/// bound allows.
#[error("more than {max} concurrent chunk streams")]
#[non_exhaustive]
TooManyChunkStreams {
/// The bound that was reached.
max: usize,
},
/// The in-progress reassembly buffer grew past the per-connection
/// budget (#1108/RTMP-W1).
#[error("buffered input exceeds {max} bytes")]
#[non_exhaustive]
InputBudgetExceeded {
/// The budget, in bytes.
max: usize,
},
/// Assembling a message hit a chunk-stream state that cannot complete:
/// a continuation before any chunk, a header on a completed message, a
/// stream without a message type id, or a chunk that does not match its
/// message's declared length.
#[error("inconsistent chunk stream {chunk_stream_id}: {what}")]
#[non_exhaustive]
ChunkStreamState {
/// The chunk stream id whose state did not hold.
chunk_stream_id: u32,
/// What was inconsistent.
what: &'static str,
},
/// An operation was attempted, or a message arrived, that is not valid
/// in the session's current state (e.g. a command received before the
/// handshake completed, or `publish` before `createStream`). The server
/// maps a rejected `publish` to `NetStream.Publish.BadName`; a client
/// sees a rejected stream key as this variant after `publish`.
#[error("unexpected state: {what}")]
#[non_exhaustive]
UnexpectedState {
/// Description of the state violation.
what: &'static str,
},
/// A [`crate::chunk::Message`] was built with a `chunk_stream_id` outside
/// the basic header's encodable range (2..=65599, RTMP §5.3.1.1), so it
/// cannot be written.
#[error("chunk stream id {chunk_stream_id} is outside the encodable range 2..=65599")]
#[non_exhaustive]
InvalidChunkStreamId {
/// The offending chunk stream id.
chunk_stream_id: u32,
},
/// A length or count did not fit the wire field it is written to
/// (#1129): a serializer that narrowed a `usize` with `as u32` (then
/// packed it into a 24-bit field) would have silently wrapped and
/// emitted a misframed chunk stream; this is returned instead.
#[error(transparent)]
FieldOverflow(#[from] broadcast_core::error::FieldOverflow),
/// The output buffer passed to `serialize_into` was smaller than
/// `serialized_len()`.
#[error(transparent)]
OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),
/// The caller's [`broadcast_core::damage::DamagePolicy`] refused a report
/// the session was about to publish: the session ends with the same
/// report inside this error.
#[error(transparent)]
Damage(#[from] broadcast_core::damage::DamageError),
}
impl RtmpError {
/// Builds [`Self::Malformed`].
#[must_use]
pub fn malformed(what: &'static str) -> Self {
Self::Malformed { what }
}
}
/// Render one AMF0 value for an error message; the value's `Debug` form is
/// the only lossless rendering it has.
fn displayed(value: &Amf0Value) -> alloc::string::String {
alloc::format!("{value:?}")
}
impl RtmpError {
/// The error for an announced chunk size this engine cannot adopt.
pub(crate) const fn chunk_size_out_of_range(value: u32) -> Self {
RtmpError::ChunkSizeOutOfRange { value }
}
}