Skip to main content

net_wire/quic/frame/
control.rs

1//! RFC 9000 scalar control frame views.
2
3use super::super::varint::QuicVarInt;
4use super::parse::{QuicFrameField, QuicFrameParseError};
5
6const MAXIMUM_STREAMS_LIMIT: u64 = 1 << 60;
7
8/// Identifies whether a QUIC stream is bidirectional or unidirectional.
9#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
10pub enum QuicStreamDirection {
11    /// A stream carrying data in both directions.
12    Bidirectional,
13    /// A stream carrying data in one direction.
14    Unidirectional,
15}
16
17/// A checked borrowed RFC 9000 MAX_STREAMS frame view.
18#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19pub struct QuicMaxStreamsFrame<'a> {
20    bytes: &'a [u8],
21    frame_type: QuicVarInt<'a>,
22    direction: QuicStreamDirection,
23    maximum_streams: QuicVarInt<'a>,
24}
25
26impl<'a> QuicMaxStreamsFrame<'a> {
27    /// Returns the exact encoded frame bytes.
28    pub const fn as_bytes(self) -> &'a [u8] {
29        self.bytes
30    }
31
32    /// Returns the exact encoded frame-type variable integer.
33    pub const fn frame_type(self) -> QuicVarInt<'a> {
34        self.frame_type
35    }
36
37    /// Returns the stream direction selected by the frame type.
38    pub const fn direction(self) -> QuicStreamDirection {
39        self.direction
40    }
41
42    /// Returns the exact encoded maximum-streams variable integer.
43    pub const fn maximum_streams(self) -> QuicVarInt<'a> {
44        self.maximum_streams
45    }
46}
47
48/// A checked borrowed RFC 9000 STREAMS_BLOCKED frame view.
49#[derive(Clone, Copy, Debug, Eq, PartialEq)]
50pub struct QuicStreamsBlockedFrame<'a> {
51    bytes: &'a [u8],
52    frame_type: QuicVarInt<'a>,
53    direction: QuicStreamDirection,
54    maximum_streams: QuicVarInt<'a>,
55}
56
57impl<'a> QuicStreamsBlockedFrame<'a> {
58    /// Returns the exact encoded frame bytes.
59    pub const fn as_bytes(self) -> &'a [u8] {
60        self.bytes
61    }
62
63    /// Returns the exact encoded frame-type variable integer.
64    pub const fn frame_type(self) -> QuicVarInt<'a> {
65        self.frame_type
66    }
67
68    /// Returns the stream direction selected by the frame type.
69    pub const fn direction(self) -> QuicStreamDirection {
70        self.direction
71    }
72
73    /// Returns the exact encoded maximum-streams variable integer.
74    pub const fn maximum_streams(self) -> QuicVarInt<'a> {
75        self.maximum_streams
76    }
77}
78
79/// A checked borrowed RFC 9000 MAX_DATA frame view.
80#[derive(Clone, Copy, Debug, Eq, PartialEq)]
81pub struct QuicMaxDataFrame<'a> {
82    bytes: &'a [u8],
83    frame_type: QuicVarInt<'a>,
84    maximum_data: QuicVarInt<'a>,
85}
86
87impl<'a> QuicMaxDataFrame<'a> {
88    /// Returns the exact encoded frame bytes.
89    pub const fn as_bytes(self) -> &'a [u8] {
90        self.bytes
91    }
92    /// Returns the exact encoded frame-type variable integer.
93    pub const fn frame_type(self) -> QuicVarInt<'a> {
94        self.frame_type
95    }
96    /// Returns the exact encoded maximum-data variable integer.
97    pub const fn maximum_data(self) -> QuicVarInt<'a> {
98        self.maximum_data
99    }
100}
101
102/// A checked borrowed RFC 9000 MAX_STREAM_DATA frame view.
103#[derive(Clone, Copy, Debug, Eq, PartialEq)]
104pub struct QuicMaxStreamDataFrame<'a> {
105    bytes: &'a [u8],
106    frame_type: QuicVarInt<'a>,
107    stream_id: QuicVarInt<'a>,
108    maximum_stream_data: QuicVarInt<'a>,
109}
110
111impl<'a> QuicMaxStreamDataFrame<'a> {
112    /// Returns the exact encoded frame bytes.
113    pub const fn as_bytes(self) -> &'a [u8] {
114        self.bytes
115    }
116    /// Returns the exact encoded frame-type variable integer.
117    pub const fn frame_type(self) -> QuicVarInt<'a> {
118        self.frame_type
119    }
120    /// Returns the exact encoded stream-ID variable integer.
121    pub const fn stream_id(self) -> QuicVarInt<'a> {
122        self.stream_id
123    }
124    /// Returns the exact encoded maximum-stream-data variable integer.
125    pub const fn maximum_stream_data(self) -> QuicVarInt<'a> {
126        self.maximum_stream_data
127    }
128}
129
130/// A checked borrowed RFC 9000 DATA_BLOCKED frame view.
131#[derive(Clone, Copy, Debug, Eq, PartialEq)]
132pub struct QuicDataBlockedFrame<'a> {
133    bytes: &'a [u8],
134    frame_type: QuicVarInt<'a>,
135    maximum_data: QuicVarInt<'a>,
136}
137
138impl<'a> QuicDataBlockedFrame<'a> {
139    /// Returns the exact encoded frame bytes.
140    pub const fn as_bytes(self) -> &'a [u8] {
141        self.bytes
142    }
143    /// Returns the exact encoded frame-type variable integer.
144    pub const fn frame_type(self) -> QuicVarInt<'a> {
145        self.frame_type
146    }
147    /// Returns the exact encoded maximum-data variable integer.
148    pub const fn maximum_data(self) -> QuicVarInt<'a> {
149        self.maximum_data
150    }
151}
152
153/// A checked borrowed RFC 9000 STREAM_DATA_BLOCKED frame view.
154#[derive(Clone, Copy, Debug, Eq, PartialEq)]
155pub struct QuicStreamDataBlockedFrame<'a> {
156    bytes: &'a [u8],
157    frame_type: QuicVarInt<'a>,
158    stream_id: QuicVarInt<'a>,
159    maximum_stream_data: QuicVarInt<'a>,
160}
161
162impl<'a> QuicStreamDataBlockedFrame<'a> {
163    /// Returns the exact encoded frame bytes.
164    pub const fn as_bytes(self) -> &'a [u8] {
165        self.bytes
166    }
167    /// Returns the exact encoded frame-type variable integer.
168    pub const fn frame_type(self) -> QuicVarInt<'a> {
169        self.frame_type
170    }
171    /// Returns the exact encoded stream-ID variable integer.
172    pub const fn stream_id(self) -> QuicVarInt<'a> {
173        self.stream_id
174    }
175    /// Returns the exact encoded maximum-stream-data variable integer.
176    pub const fn maximum_stream_data(self) -> QuicVarInt<'a> {
177        self.maximum_stream_data
178    }
179}
180
181pub(super) fn parse_max_data<'a>(
182    bytes: &'a [u8],
183    frame_type: QuicVarInt<'a>,
184    offset: usize,
185) -> Result<(QuicMaxDataFrame<'a>, &'a [u8]), QuicFrameParseError> {
186    let input = &bytes[frame_type.byte_len()..];
187    let (maximum_data, suffix) = parse_field(
188        input,
189        offset + frame_type.byte_len(),
190        QuicFrameField::MaximumData,
191    )?;
192    let frame_len = bytes.len() - suffix.len();
193    Ok((
194        QuicMaxDataFrame {
195            bytes: &bytes[..frame_len],
196            frame_type,
197            maximum_data,
198        },
199        suffix,
200    ))
201}
202
203pub(super) fn parse_max_stream_data<'a>(
204    bytes: &'a [u8],
205    frame_type: QuicVarInt<'a>,
206    offset: usize,
207) -> Result<(QuicMaxStreamDataFrame<'a>, &'a [u8]), QuicFrameParseError> {
208    let input = &bytes[frame_type.byte_len()..];
209    let (stream_id, input) = parse_field(
210        input,
211        offset + frame_type.byte_len(),
212        QuicFrameField::StreamId,
213    )?;
214    let (maximum_stream_data, suffix) = parse_field(
215        input,
216        offset + bytes.len() - input.len(),
217        QuicFrameField::MaximumStreamData,
218    )?;
219    let frame_len = bytes.len() - suffix.len();
220    Ok((
221        QuicMaxStreamDataFrame {
222            bytes: &bytes[..frame_len],
223            frame_type,
224            stream_id,
225            maximum_stream_data,
226        },
227        suffix,
228    ))
229}
230
231pub(super) fn parse_data_blocked<'a>(
232    bytes: &'a [u8],
233    frame_type: QuicVarInt<'a>,
234    offset: usize,
235) -> Result<(QuicDataBlockedFrame<'a>, &'a [u8]), QuicFrameParseError> {
236    let input = &bytes[frame_type.byte_len()..];
237    let (maximum_data, suffix) = parse_field(
238        input,
239        offset + frame_type.byte_len(),
240        QuicFrameField::MaximumData,
241    )?;
242    let frame_len = bytes.len() - suffix.len();
243    Ok((
244        QuicDataBlockedFrame {
245            bytes: &bytes[..frame_len],
246            frame_type,
247            maximum_data,
248        },
249        suffix,
250    ))
251}
252
253pub(super) fn parse_max_streams<'a>(
254    bytes: &'a [u8],
255    frame_type: QuicVarInt<'a>,
256    offset: usize,
257    direction: QuicStreamDirection,
258) -> Result<(QuicMaxStreamsFrame<'a>, &'a [u8]), QuicFrameParseError> {
259    let (maximum_streams, suffix) = parse_maximum_streams(bytes, frame_type, offset)?;
260    let frame_len = bytes.len() - suffix.len();
261    Ok((
262        QuicMaxStreamsFrame {
263            bytes: &bytes[..frame_len],
264            frame_type,
265            direction,
266            maximum_streams,
267        },
268        suffix,
269    ))
270}
271
272pub(super) fn parse_streams_blocked<'a>(
273    bytes: &'a [u8],
274    frame_type: QuicVarInt<'a>,
275    offset: usize,
276    direction: QuicStreamDirection,
277) -> Result<(QuicStreamsBlockedFrame<'a>, &'a [u8]), QuicFrameParseError> {
278    let (maximum_streams, suffix) = parse_maximum_streams(bytes, frame_type, offset)?;
279    let frame_len = bytes.len() - suffix.len();
280    Ok((
281        QuicStreamsBlockedFrame {
282            bytes: &bytes[..frame_len],
283            frame_type,
284            direction,
285            maximum_streams,
286        },
287        suffix,
288    ))
289}
290
291pub(super) fn parse_stream_data_blocked<'a>(
292    bytes: &'a [u8],
293    frame_type: QuicVarInt<'a>,
294    offset: usize,
295) -> Result<(QuicStreamDataBlockedFrame<'a>, &'a [u8]), QuicFrameParseError> {
296    let input = &bytes[frame_type.byte_len()..];
297    let (stream_id, input) = parse_field(
298        input,
299        offset + frame_type.byte_len(),
300        QuicFrameField::StreamId,
301    )?;
302    let (maximum_stream_data, suffix) = parse_field(
303        input,
304        offset + bytes.len() - input.len(),
305        QuicFrameField::MaximumStreamData,
306    )?;
307    let frame_len = bytes.len() - suffix.len();
308    Ok((
309        QuicStreamDataBlockedFrame {
310            bytes: &bytes[..frame_len],
311            frame_type,
312            stream_id,
313            maximum_stream_data,
314        },
315        suffix,
316    ))
317}
318
319/// A checked borrowed RFC 9000 RESET_STREAM frame view.
320#[derive(Clone, Copy, Debug, Eq, PartialEq)]
321pub struct QuicResetStreamFrame<'a> {
322    bytes: &'a [u8],
323    frame_type: QuicVarInt<'a>,
324    stream_id: QuicVarInt<'a>,
325    application_error_code: QuicVarInt<'a>,
326    final_size: QuicVarInt<'a>,
327}
328
329impl<'a> QuicResetStreamFrame<'a> {
330    /// Returns the exact encoded frame bytes.
331    pub const fn as_bytes(self) -> &'a [u8] {
332        self.bytes
333    }
334
335    /// Returns the exact encoded frame-type variable integer.
336    pub const fn frame_type(self) -> QuicVarInt<'a> {
337        self.frame_type
338    }
339
340    /// Returns the exact encoded stream-ID variable integer.
341    pub const fn stream_id(self) -> QuicVarInt<'a> {
342        self.stream_id
343    }
344
345    /// Returns the exact encoded application-error-code variable integer.
346    pub const fn application_error_code(self) -> QuicVarInt<'a> {
347        self.application_error_code
348    }
349
350    /// Returns the exact encoded final-size variable integer.
351    pub const fn final_size(self) -> QuicVarInt<'a> {
352        self.final_size
353    }
354}
355
356/// A checked borrowed RFC 9000 STOP_SENDING frame view.
357#[derive(Clone, Copy, Debug, Eq, PartialEq)]
358pub struct QuicStopSendingFrame<'a> {
359    bytes: &'a [u8],
360    frame_type: QuicVarInt<'a>,
361    stream_id: QuicVarInt<'a>,
362    application_error_code: QuicVarInt<'a>,
363}
364
365impl<'a> QuicStopSendingFrame<'a> {
366    /// Returns the exact encoded frame bytes.
367    pub const fn as_bytes(self) -> &'a [u8] {
368        self.bytes
369    }
370
371    /// Returns the exact encoded frame-type variable integer.
372    pub const fn frame_type(self) -> QuicVarInt<'a> {
373        self.frame_type
374    }
375
376    /// Returns the exact encoded stream-ID variable integer.
377    pub const fn stream_id(self) -> QuicVarInt<'a> {
378        self.stream_id
379    }
380
381    /// Returns the exact encoded application-error-code variable integer.
382    pub const fn application_error_code(self) -> QuicVarInt<'a> {
383        self.application_error_code
384    }
385}
386
387pub(super) fn parse_reset_stream<'a>(
388    bytes: &'a [u8],
389    frame_type: QuicVarInt<'a>,
390    offset: usize,
391) -> Result<(QuicResetStreamFrame<'a>, &'a [u8]), QuicFrameParseError> {
392    let input = &bytes[frame_type.byte_len()..];
393    let (stream_id, input) = parse_field(
394        input,
395        offset + frame_type.byte_len(),
396        QuicFrameField::StreamId,
397    )?;
398    let (application_error_code, input) = parse_field(
399        input,
400        offset + bytes.len() - input.len(),
401        QuicFrameField::ApplicationErrorCode,
402    )?;
403    let (final_size, suffix) = parse_field(
404        input,
405        offset + bytes.len() - input.len(),
406        QuicFrameField::FinalSize,
407    )?;
408    let frame_len = bytes.len() - suffix.len();
409    Ok((
410        QuicResetStreamFrame {
411            bytes: &bytes[..frame_len],
412            frame_type,
413            stream_id,
414            application_error_code,
415            final_size,
416        },
417        suffix,
418    ))
419}
420
421pub(super) fn parse_stop_sending<'a>(
422    bytes: &'a [u8],
423    frame_type: QuicVarInt<'a>,
424    offset: usize,
425) -> Result<(QuicStopSendingFrame<'a>, &'a [u8]), QuicFrameParseError> {
426    let input = &bytes[frame_type.byte_len()..];
427    let (stream_id, input) = parse_field(
428        input,
429        offset + frame_type.byte_len(),
430        QuicFrameField::StreamId,
431    )?;
432    let (application_error_code, suffix) = parse_field(
433        input,
434        offset + bytes.len() - input.len(),
435        QuicFrameField::ApplicationErrorCode,
436    )?;
437    let frame_len = bytes.len() - suffix.len();
438    Ok((
439        QuicStopSendingFrame {
440            bytes: &bytes[..frame_len],
441            frame_type,
442            stream_id,
443            application_error_code,
444        },
445        suffix,
446    ))
447}
448
449fn parse_field<'a>(
450    input: &'a [u8],
451    offset: usize,
452    field: QuicFrameField,
453) -> Result<(QuicVarInt<'a>, &'a [u8]), QuicFrameParseError> {
454    let value = QuicVarInt::parse(input).map_err(|error| QuicFrameParseError::Field {
455        field,
456        offset,
457        error,
458    })?;
459    Ok((value, &input[value.byte_len()..]))
460}
461
462fn parse_maximum_streams<'a>(
463    bytes: &'a [u8],
464    frame_type: QuicVarInt<'a>,
465    offset: usize,
466) -> Result<(QuicVarInt<'a>, &'a [u8]), QuicFrameParseError> {
467    let field_offset = offset + frame_type.byte_len();
468    let (maximum_streams, suffix) = parse_field(
469        &bytes[frame_type.byte_len()..],
470        field_offset,
471        QuicFrameField::MaximumStreams,
472    )?;
473    if maximum_streams.value() > MAXIMUM_STREAMS_LIMIT {
474        return Err(QuicFrameParseError::FieldValueOutOfRange {
475            field: QuicFrameField::MaximumStreams,
476            offset: field_offset,
477            value: maximum_streams.value(),
478            maximum: MAXIMUM_STREAMS_LIMIT,
479        });
480    }
481    Ok((maximum_streams, suffix))
482}