net-wire 0.1.0

Allocation-free no_std wire views and codecs for network protocols.
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! RFC 9000 scalar control frame views.

use super::super::varint::QuicVarInt;
use super::parse::{QuicFrameField, QuicFrameParseError};

const MAXIMUM_STREAMS_LIMIT: u64 = 1 << 60;

/// Identifies whether a QUIC stream is bidirectional or unidirectional.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum QuicStreamDirection {
    /// A stream carrying data in both directions.
    Bidirectional,
    /// A stream carrying data in one direction.
    Unidirectional,
}

/// A checked borrowed RFC 9000 MAX_STREAMS frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicMaxStreamsFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    direction: QuicStreamDirection,
    maximum_streams: QuicVarInt<'a>,
}

impl<'a> QuicMaxStreamsFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }

    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }

    /// Returns the stream direction selected by the frame type.
    pub const fn direction(self) -> QuicStreamDirection {
        self.direction
    }

    /// Returns the exact encoded maximum-streams variable integer.
    pub const fn maximum_streams(self) -> QuicVarInt<'a> {
        self.maximum_streams
    }
}

/// A checked borrowed RFC 9000 STREAMS_BLOCKED frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicStreamsBlockedFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    direction: QuicStreamDirection,
    maximum_streams: QuicVarInt<'a>,
}

impl<'a> QuicStreamsBlockedFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }

    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }

    /// Returns the stream direction selected by the frame type.
    pub const fn direction(self) -> QuicStreamDirection {
        self.direction
    }

    /// Returns the exact encoded maximum-streams variable integer.
    pub const fn maximum_streams(self) -> QuicVarInt<'a> {
        self.maximum_streams
    }
}

/// A checked borrowed RFC 9000 MAX_DATA frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicMaxDataFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    maximum_data: QuicVarInt<'a>,
}

impl<'a> QuicMaxDataFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }
    /// Returns the exact encoded maximum-data variable integer.
    pub const fn maximum_data(self) -> QuicVarInt<'a> {
        self.maximum_data
    }
}

/// A checked borrowed RFC 9000 MAX_STREAM_DATA frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicMaxStreamDataFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    stream_id: QuicVarInt<'a>,
    maximum_stream_data: QuicVarInt<'a>,
}

impl<'a> QuicMaxStreamDataFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }
    /// Returns the exact encoded stream-ID variable integer.
    pub const fn stream_id(self) -> QuicVarInt<'a> {
        self.stream_id
    }
    /// Returns the exact encoded maximum-stream-data variable integer.
    pub const fn maximum_stream_data(self) -> QuicVarInt<'a> {
        self.maximum_stream_data
    }
}

/// A checked borrowed RFC 9000 DATA_BLOCKED frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicDataBlockedFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    maximum_data: QuicVarInt<'a>,
}

impl<'a> QuicDataBlockedFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }
    /// Returns the exact encoded maximum-data variable integer.
    pub const fn maximum_data(self) -> QuicVarInt<'a> {
        self.maximum_data
    }
}

/// A checked borrowed RFC 9000 STREAM_DATA_BLOCKED frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicStreamDataBlockedFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    stream_id: QuicVarInt<'a>,
    maximum_stream_data: QuicVarInt<'a>,
}

impl<'a> QuicStreamDataBlockedFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }
    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }
    /// Returns the exact encoded stream-ID variable integer.
    pub const fn stream_id(self) -> QuicVarInt<'a> {
        self.stream_id
    }
    /// Returns the exact encoded maximum-stream-data variable integer.
    pub const fn maximum_stream_data(self) -> QuicVarInt<'a> {
        self.maximum_stream_data
    }
}

pub(super) fn parse_max_data<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
) -> Result<(QuicMaxDataFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let input = &bytes[frame_type.byte_len()..];
    let (maximum_data, suffix) = parse_field(
        input,
        offset + frame_type.byte_len(),
        QuicFrameField::MaximumData,
    )?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicMaxDataFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            maximum_data,
        },
        suffix,
    ))
}

pub(super) fn parse_max_stream_data<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
) -> Result<(QuicMaxStreamDataFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let input = &bytes[frame_type.byte_len()..];
    let (stream_id, input) = parse_field(
        input,
        offset + frame_type.byte_len(),
        QuicFrameField::StreamId,
    )?;
    let (maximum_stream_data, suffix) = parse_field(
        input,
        offset + bytes.len() - input.len(),
        QuicFrameField::MaximumStreamData,
    )?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicMaxStreamDataFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            stream_id,
            maximum_stream_data,
        },
        suffix,
    ))
}

pub(super) fn parse_data_blocked<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
) -> Result<(QuicDataBlockedFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let input = &bytes[frame_type.byte_len()..];
    let (maximum_data, suffix) = parse_field(
        input,
        offset + frame_type.byte_len(),
        QuicFrameField::MaximumData,
    )?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicDataBlockedFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            maximum_data,
        },
        suffix,
    ))
}

pub(super) fn parse_max_streams<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
    direction: QuicStreamDirection,
) -> Result<(QuicMaxStreamsFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let (maximum_streams, suffix) = parse_maximum_streams(bytes, frame_type, offset)?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicMaxStreamsFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            direction,
            maximum_streams,
        },
        suffix,
    ))
}

pub(super) fn parse_streams_blocked<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
    direction: QuicStreamDirection,
) -> Result<(QuicStreamsBlockedFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let (maximum_streams, suffix) = parse_maximum_streams(bytes, frame_type, offset)?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicStreamsBlockedFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            direction,
            maximum_streams,
        },
        suffix,
    ))
}

pub(super) fn parse_stream_data_blocked<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
) -> Result<(QuicStreamDataBlockedFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let input = &bytes[frame_type.byte_len()..];
    let (stream_id, input) = parse_field(
        input,
        offset + frame_type.byte_len(),
        QuicFrameField::StreamId,
    )?;
    let (maximum_stream_data, suffix) = parse_field(
        input,
        offset + bytes.len() - input.len(),
        QuicFrameField::MaximumStreamData,
    )?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicStreamDataBlockedFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            stream_id,
            maximum_stream_data,
        },
        suffix,
    ))
}

/// A checked borrowed RFC 9000 RESET_STREAM frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicResetStreamFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    stream_id: QuicVarInt<'a>,
    application_error_code: QuicVarInt<'a>,
    final_size: QuicVarInt<'a>,
}

impl<'a> QuicResetStreamFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }

    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }

    /// Returns the exact encoded stream-ID variable integer.
    pub const fn stream_id(self) -> QuicVarInt<'a> {
        self.stream_id
    }

    /// Returns the exact encoded application-error-code variable integer.
    pub const fn application_error_code(self) -> QuicVarInt<'a> {
        self.application_error_code
    }

    /// Returns the exact encoded final-size variable integer.
    pub const fn final_size(self) -> QuicVarInt<'a> {
        self.final_size
    }
}

/// A checked borrowed RFC 9000 STOP_SENDING frame view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuicStopSendingFrame<'a> {
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    stream_id: QuicVarInt<'a>,
    application_error_code: QuicVarInt<'a>,
}

impl<'a> QuicStopSendingFrame<'a> {
    /// Returns the exact encoded frame bytes.
    pub const fn as_bytes(self) -> &'a [u8] {
        self.bytes
    }

    /// Returns the exact encoded frame-type variable integer.
    pub const fn frame_type(self) -> QuicVarInt<'a> {
        self.frame_type
    }

    /// Returns the exact encoded stream-ID variable integer.
    pub const fn stream_id(self) -> QuicVarInt<'a> {
        self.stream_id
    }

    /// Returns the exact encoded application-error-code variable integer.
    pub const fn application_error_code(self) -> QuicVarInt<'a> {
        self.application_error_code
    }
}

pub(super) fn parse_reset_stream<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
) -> Result<(QuicResetStreamFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let input = &bytes[frame_type.byte_len()..];
    let (stream_id, input) = parse_field(
        input,
        offset + frame_type.byte_len(),
        QuicFrameField::StreamId,
    )?;
    let (application_error_code, input) = parse_field(
        input,
        offset + bytes.len() - input.len(),
        QuicFrameField::ApplicationErrorCode,
    )?;
    let (final_size, suffix) = parse_field(
        input,
        offset + bytes.len() - input.len(),
        QuicFrameField::FinalSize,
    )?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicResetStreamFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            stream_id,
            application_error_code,
            final_size,
        },
        suffix,
    ))
}

pub(super) fn parse_stop_sending<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
) -> Result<(QuicStopSendingFrame<'a>, &'a [u8]), QuicFrameParseError> {
    let input = &bytes[frame_type.byte_len()..];
    let (stream_id, input) = parse_field(
        input,
        offset + frame_type.byte_len(),
        QuicFrameField::StreamId,
    )?;
    let (application_error_code, suffix) = parse_field(
        input,
        offset + bytes.len() - input.len(),
        QuicFrameField::ApplicationErrorCode,
    )?;
    let frame_len = bytes.len() - suffix.len();
    Ok((
        QuicStopSendingFrame {
            bytes: &bytes[..frame_len],
            frame_type,
            stream_id,
            application_error_code,
        },
        suffix,
    ))
}

fn parse_field<'a>(
    input: &'a [u8],
    offset: usize,
    field: QuicFrameField,
) -> Result<(QuicVarInt<'a>, &'a [u8]), QuicFrameParseError> {
    let value = QuicVarInt::parse(input).map_err(|error| QuicFrameParseError::Field {
        field,
        offset,
        error,
    })?;
    Ok((value, &input[value.byte_len()..]))
}

fn parse_maximum_streams<'a>(
    bytes: &'a [u8],
    frame_type: QuicVarInt<'a>,
    offset: usize,
) -> Result<(QuicVarInt<'a>, &'a [u8]), QuicFrameParseError> {
    let field_offset = offset + frame_type.byte_len();
    let (maximum_streams, suffix) = parse_field(
        &bytes[frame_type.byte_len()..],
        field_offset,
        QuicFrameField::MaximumStreams,
    )?;
    if maximum_streams.value() > MAXIMUM_STREAMS_LIMIT {
        return Err(QuicFrameParseError::FieldValueOutOfRange {
            field: QuicFrameField::MaximumStreams,
            offset: field_offset,
            value: maximum_streams.value(),
            maximum: MAXIMUM_STREAMS_LIMIT,
        });
    }
    Ok((maximum_streams, suffix))
}