1use crate::{types::*, Packet, PrimitiveValues};
2
3#[derive(Debug)]
4pub enum QuicPacket<'a> {
5 VersionNegotiation(VersionNegotiationPacket<'a>),
6 Initial(InitialPacket<'a>),
7 ZeroRtt(ZeroRttPacket<'a>),
8 Handshake(HandshakePacket<'a>),
9 Retry(RetryPacket<'a>),
10 OneRtt(OneRttPacket<'a>),
11}
12
13impl<'a> QuicPacket<'a> {
14 pub fn new(mut packet: &'a [u8]) -> Option<Vec<Self>> {
15 let mut packets = vec![];
16 while !packet.is_empty() {
17 if packet.len() < 5 {
18 return None;
19 }
20 let header_form = packet[0] & (1 << 7) > 0;
21 let fixed_bit = packet[0] & (1 << 6) > 0;
22 let quic = if fixed_bit && !header_form {
23 Self::OneRtt(OneRttPacket::new(packet)?)
24 } else {
25 if packet[1..5] == [0u8; 4] {
26 Self::VersionNegotiation(VersionNegotiationPacket::new(packet)?)
27 } else {
28 match (LongPacketType::new((packet[0] >> 4) & 0b11), fixed_bit) {
29 (LongPacketTypes::Initial, true) => {
30 Self::Initial(InitialPacket::new(packet)?)
31 }
32 (LongPacketTypes::ZeroRtt, true) => {
33 Self::ZeroRtt(ZeroRttPacket::new(packet)?)
34 }
35 (LongPacketTypes::Handshake, true) => {
36 Self::Handshake(HandshakePacket::new(packet)?)
37 }
38 (LongPacketTypes::Retry, true) => Self::Retry(RetryPacket::new(packet)?),
39 _ => return None,
40 }
41 }
42 };
43 let remaining = unsafe { std::mem::transmute(quic.remaining()) };
45 packets.push(quic);
46 packet = remaining;
47 }
48 Some(packets)
49 }
50
51 pub fn version(&self) -> Option<Version> {
52 match self {
53 Self::VersionNegotiation(_) => None,
54 Self::Initial(packet) => Some(packet.get_version()),
55 Self::ZeroRtt(packet) => Some(packet.get_version()),
56 Self::Handshake(packet) => Some(packet.get_version()),
57 Self::Retry(packet) => Some(packet.get_version()),
58 Self::OneRtt(_) => None,
59 }
60 }
61
62 pub fn token(&self) -> Option<Vec<u8>> {
63 match self {
64 Self::Initial(packet) => Some(packet.get_token()),
65 _ => None,
66 }
67 }
68
69 pub fn packet_number(&self) -> Option<u64> {
70 match self {
71 Self::VersionNegotiation(_) => None,
72 Self::Initial(packet) => Some(packet.get_packet_number_raw()),
73 Self::ZeroRtt(packet) => Some(packet.get_packet_number_raw()),
74 Self::Handshake(packet) => Some(packet.get_packet_number_raw()),
75 Self::Retry(_) => None,
76 Self::OneRtt(packet) => Some(packet.get_packet_number_raw()),
77 }
78 .map(packet_number)
79 }
80
81 pub fn dest_id(&self) -> Vec<u8> {
82 match self {
83 Self::VersionNegotiation(packet) => packet.get_dest_id(),
84 Self::Initial(packet) => packet.get_dest_id(),
85 Self::ZeroRtt(packet) => packet.get_dest_id(),
86 Self::Handshake(packet) => packet.get_dest_id(),
87 Self::Retry(packet) => packet.get_dest_id(),
88 Self::OneRtt(packet) => packet.get_dest_id(),
89 }
90 }
91
92 pub fn src_id(&self) -> Option<Vec<u8>> {
93 Some(match self {
94 Self::VersionNegotiation(packet) => packet.get_src_id(),
95 Self::Initial(packet) => packet.get_src_id(),
96 Self::ZeroRtt(packet) => packet.get_src_id(),
97 Self::Handshake(packet) => packet.get_src_id(),
98 Self::Retry(packet) => packet.get_src_id(),
99 Self::OneRtt(_) => return None,
100 })
101 }
102
103 pub fn packet(&self) -> &[u8] {
104 match self {
105 Self::VersionNegotiation(p) => p.packet(),
106 Self::Initial(p) => p.packet(),
107 Self::ZeroRtt(p) => p.packet(),
108 Self::Handshake(p) => p.packet(),
109 Self::Retry(p) => p.packet(),
110 Self::OneRtt(p) => p.packet(),
111 }
112 }
113
114 pub fn frames(&self) -> Option<&[u8]> {
115 match self {
116 Self::VersionNegotiation(_) => None,
117 Self::Initial(packet) => Some(packet.get_frames_raw()),
118 Self::ZeroRtt(packet) => Some(packet.get_frames_raw()),
119 Self::Handshake(packet) => Some(packet.get_frames_raw()),
120 Self::Retry(_) => None,
121 Self::OneRtt(packet) => Some(packet.payload()),
122 }
123 }
124
125 pub fn remaining(&self) -> &[u8] {
126 match self {
127 Self::VersionNegotiation(_) => &[],
128 Self::Initial(packet) => packet.get_remaining_raw(),
129 Self::ZeroRtt(packet) => packet.get_remaining_raw(),
130 Self::Handshake(packet) => packet.get_remaining_raw(),
131 Self::Retry(_) => &[],
132 Self::OneRtt(_) => &[],
133 }
134 }
135}
136
137impl<'a> std::fmt::Display for QuicPacket<'a> {
138 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
139 let ty = match self {
140 Self::VersionNegotiation(_) => "version-negotiation",
141 Self::Initial(_) => "initial",
142 Self::ZeroRtt(_) => "0rtt",
143 Self::Handshake(_) => "handshake",
144 Self::Retry(_) => "retry",
145 Self::OneRtt(_) => "1rtt",
146 };
147 writeln!(f, "packet-type: {}", ty)?;
148 if let Some(version) = self.version() {
149 writeln!(f, "quic-version: {}", version)?;
150 }
151 if let Some(token) = self.token() {
152 writeln!(f, "token: {:?}", token)?;
153 }
154 if let Some(pn) = self.packet_number() {
155 writeln!(f, "packet-number: {}", pn)?;
156 }
157 let dest_id = self.dest_id();
158 writeln!(f, "dest-id: {:?}", dest_id)?;
159 if let Some(src_id) = self.src_id() {
160 writeln!(f, "src-id: {:?}", src_id)?;
161 }
162 if let Some(payload) = self.frames() {
163 writeln!(f, "payload: {}", payload.len())?;
164 }
165 Ok(())
166 }
167}
168
169pub fn varint_length(rest: &[u8]) -> usize {
170 let prefix = rest[0] >> 6;
171 let length = 1 << prefix;
172 length
173}
174
175pub fn varint(bytes: &[u8]) -> usize {
176 let mut length = (bytes[0] & 0x3f) as u64;
177 for v in &bytes[1..] {
178 length = (length << 8) + *v as u64;
179 }
180 length as usize
181}
182
183pub fn packet_number(bytes: &[u8]) -> u64 {
184 let mut pn = [0; 8];
185 pn[(8 - bytes.len())..].copy_from_slice(bytes);
186 u64::from_be_bytes(pn)
187}
188
189pub fn n_varints(n: usize, mut bytes: &[u8]) -> usize {
190 let mut length = 0;
191 for _ in 0..n {
192 let prefix = bytes[0] >> 6;
193 let vlen = 1 << prefix;
194 length += vlen;
195 let (_, rest) = bytes.split_at(vlen);
196 bytes = rest;
197 }
198 length
199}
200
201#[derive(Debug, Packet)]
202pub struct VersionNegotiation {
203 #[construct_with(u1)]
204 header_form: HeaderForm,
205 unused: u7,
206 version: u32be,
207 dest_id_len: u8,
208 #[length = "dest_id_len"]
209 dest_id: Vec<u8>,
210 src_id_len: u8,
211 #[length = "src_id_len"]
212 src_id: Vec<u8>,
213 supported_versions: Vec<u8>,
214}
215
216#[derive(Debug, Packet)]
217pub struct Initial {
218 #[construct_with(u1)]
219 header_form: HeaderForm,
220 #[construct_with(u1)]
221 fixed_bit: FixedBit,
222 #[construct_with(u2)]
223 long_packet_type: LongPacketType,
224 reserved: u2,
225 packet_number_len: u2,
226 #[construct_with(u32be)]
227 version: Version,
228 dest_id_len: u8,
229 #[length = "dest_id_len"]
230 dest_id: Vec<u8>,
231 src_id_len: u8,
232 #[length = "src_id_len"]
233 src_id: Vec<u8>,
234 #[length = "varint_length(...)"]
235 token_length: Vec<u8>,
236 #[length = "varint(&token_length)"]
237 token: Vec<u8>,
238 #[length = "varint_length(...)"]
239 length: Vec<u8>,
240 #[length = "packet_number_len + 1"]
241 packet_number: Vec<u8>,
242 #[length = "varint(&length) - packet_number.len()"]
243 frames: Vec<u8>,
244 remaining: Vec<u8>,
245}
246
247#[derive(Debug, Packet)]
248pub struct ZeroRtt {
249 #[construct_with(u1)]
250 header_form: HeaderForm,
251 #[construct_with(u1)]
252 fixed_bit: FixedBit,
253 #[construct_with(u2)]
254 long_packet_type: LongPacketType,
255 reserved: u2,
256 packet_number_len: u2,
257 #[construct_with(u32be)]
258 version: Version,
259 dest_id_len: u8,
260 #[length = "dest_id_len"]
261 dest_id: Vec<u8>,
262 src_id_len: u8,
263 #[length = "src_id_len"]
264 src_id: Vec<u8>,
265 #[length = "varint_length(...)"]
266 length: Vec<u8>,
267 #[length = "packet_number_len + 1"]
268 packet_number: Vec<u8>,
269 #[length = "varint(&length) - packet_number.len()"]
270 frames: Vec<u8>,
271 remaining: Vec<u8>,
272}
273
274#[derive(Debug, Packet)]
275pub struct Handshake {
276 #[construct_with(u1)]
277 header_form: HeaderForm,
278 #[construct_with(u1)]
279 fixed_bit: FixedBit,
280 #[construct_with(u2)]
281 long_packet_type: LongPacketType,
282 reserved: u2,
283 packet_number_len: u2,
284 #[construct_with(u32be)]
285 version: Version,
286 dest_id_len: u8,
287 #[length = "dest_id_len"]
288 dest_id: Vec<u8>,
289 src_id_len: u8,
290 #[length = "src_id_len"]
291 src_id: Vec<u8>,
292 #[length = "varint_length(...)"]
293 length: Vec<u8>,
294 #[length = "packet_number_len + 1"]
295 packet_number: Vec<u8>,
296 #[length = "varint(&length) - packet_number.len()"]
297 frames: Vec<u8>,
298 remaining: Vec<u8>,
299}
300
301#[derive(Debug, Packet)]
302pub struct Retry {
303 #[construct_with(u1)]
304 header_form: HeaderForm,
305 #[construct_with(u1)]
306 fixed_bit: FixedBit,
307 #[construct_with(u2)]
308 long_packet_type: LongPacketType,
309 unused: u4,
310 #[construct_with(u32be)]
311 version: Version,
312 dest_id_len: u8,
313 #[length = "dest_id_len"]
314 dest_id: Vec<u8>,
315 src_id_len: u8,
316 #[length = "src_id_len"]
317 src_id: Vec<u8>,
318 retry_token: Vec<u8>,
319}
320
321#[derive(Debug, Packet)]
322pub struct OneRtt {
323 header_form: u1,
324 fixed_bit: u1,
325 spin_bit: u1,
326 reserved_bits: u2,
327 key_phase: u1,
328 packet_number_len: u2,
329 #[length = "8"] dest_id: Vec<u8>,
331 #[length = "packet_number_len + 1"]
332 packet_number: Vec<u8>,
333 #[payload]
334 payload: Vec<u8>,
335}
336
337#[allow(non_snake_case)]
338#[allow(non_upper_case_globals)]
339pub mod HeaderForms {
340 use super::HeaderForm;
341
342 pub const Short: HeaderForm = HeaderForm(0);
343 pub const Long: HeaderForm = HeaderForm(1);
344}
345
346#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
347pub struct HeaderForm(pub u8);
348
349impl HeaderForm {
350 pub fn new(val: u8) -> Self {
352 Self(val)
353 }
354}
355
356impl PrimitiveValues for HeaderForm {
357 type T = (u8,);
358 fn to_primitive_values(&self) -> Self::T {
359 (self.0,)
360 }
361}
362
363impl std::fmt::Display for HeaderForm {
364 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
365 let s = match self {
366 &HeaderForms::Short => "short",
367 &HeaderForms::Long => "long",
368 _ => "unknown",
369 };
370 write!(f, "{}", s)
371 }
372}
373
374#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
375pub struct FixedBit(pub u8);
376
377impl FixedBit {
378 pub fn new(val: u8) -> Self {
380 Self(val)
381 }
382}
383
384impl PrimitiveValues for FixedBit {
385 type T = (u8,);
386 fn to_primitive_values(&self) -> Self::T {
387 (self.0,)
388 }
389}
390
391impl std::fmt::Display for FixedBit {
392 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
393 let s = match self.0 {
394 1 => "1",
395 _ => "unknown",
396 };
397 write!(f, "{}", s)
398 }
399}
400
401#[allow(non_snake_case)]
402#[allow(non_upper_case_globals)]
403pub mod LongPacketTypes {
404 use super::LongPacketType;
405
406 pub const Initial: LongPacketType = LongPacketType(0);
407 pub const ZeroRtt: LongPacketType = LongPacketType(1);
408 pub const Handshake: LongPacketType = LongPacketType(2);
409 pub const Retry: LongPacketType = LongPacketType(3);
410}
411
412#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
413pub struct LongPacketType(pub u8);
414
415impl LongPacketType {
416 pub fn new(val: u8) -> Self {
418 Self(val)
419 }
420}
421
422impl PrimitiveValues for LongPacketType {
423 type T = (u8,);
424 fn to_primitive_values(&self) -> Self::T {
425 (self.0,)
426 }
427}
428
429impl std::fmt::Display for LongPacketType {
430 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
431 let s = match self {
432 &LongPacketTypes::Initial => "initial",
433 &LongPacketTypes::ZeroRtt => "0-rtt",
434 &LongPacketTypes::Handshake => "handshake",
435 &LongPacketTypes::Retry => "retry",
436 _ => "unknown",
437 };
438 write!(f, "{}", s)
439 }
440}
441
442#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
443pub struct Version(pub u32);
444
445impl Version {
446 pub fn new(val: u32) -> Self {
448 Self(val)
449 }
450}
451
452impl PrimitiveValues for Version {
453 type T = (u32be,);
454 fn to_primitive_values(&self) -> Self::T {
455 (self.0,)
456 }
457}
458
459impl std::fmt::Display for Version {
460 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
461 write!(f, "0x{:08x}", self.0)
462 }
463}
464
465#[derive(Debug)]
466pub enum Frame<'a> {
467 Padding(PaddingPacket<'a>, usize),
468 Ping(PingPacket<'a>),
469 Ack(AckPacket<'a>),
470 ResetStream(ResetStreamPacket<'a>),
471 StopSending(StopSendingPacket<'a>),
472 Crypto(CryptoPacket<'a>),
473 NewToken(NewTokenPacket<'a>),
474 Stream(StreamPacket<'a>),
475 MaxData(MaxDataPacket<'a>),
476 MaxStreamData(MaxStreamDataPacket<'a>),
477 MaxStreams(MaxStreamsPacket<'a>),
478 DataBlocked(DataBlockedPacket<'a>),
479 StreamDataBlocked(StreamDataBlockedPacket<'a>),
480 StreamsBlocked(StreamsBlockedPacket<'a>),
481 NewConnectionId(NewConnectionIdPacket<'a>),
482 RetireConnectionId(RetireConnectionIdPacket<'a>),
483 PathChallenge(PathChallengePacket<'a>),
484 PathResponse(PathResponsePacket<'a>),
485 ConnectionClose(ConnectionClosePacket<'a>),
486 HandshakeDone(HandshakeDonePacket<'a>),
487}
488
489impl<'a> Frame<'a> {
490 pub fn new(mut packet: &'a [u8]) -> Option<Vec<Self>> {
491 let mut frames = vec![];
492 while packet.len() > 0 {
493 let frame = match FrameType(packet[0]) {
494 FrameTypes::Padding => Self::Padding(PaddingPacket::new(packet)?, 1),
495 FrameTypes::Ping => Self::Ping(PingPacket::new(packet)?),
496 FrameTypes::Ack0 | FrameTypes::Ack1 => Self::Ack(AckPacket::new(packet)?),
497 FrameTypes::ResetStream => Self::ResetStream(ResetStreamPacket::new(packet)?),
498 FrameTypes::StopSending => Self::StopSending(StopSendingPacket::new(packet)?),
499 FrameTypes::Crypto => Self::Crypto(CryptoPacket::new(packet)?),
500 FrameTypes::NewToken => Self::NewToken(NewTokenPacket::new(packet)?),
501 x if (0x8..=0xf).contains(&x.0) => Self::Stream(StreamPacket::new(packet)?),
502 FrameTypes::MaxData => Self::MaxData(MaxDataPacket::new(packet)?),
503 FrameTypes::MaxStreamData => Self::MaxStreamData(MaxStreamDataPacket::new(packet)?),
504 FrameTypes::MaxStreams0 | FrameTypes::MaxStreams1 => {
505 Self::MaxStreams(MaxStreamsPacket::new(packet)?)
506 }
507 FrameTypes::DataBlocked => Self::DataBlocked(DataBlockedPacket::new(packet)?),
508 FrameTypes::StreamDataBlocked => {
509 Self::StreamDataBlocked(StreamDataBlockedPacket::new(packet)?)
510 }
511 FrameTypes::StreamsBlocked0 | FrameTypes::StreamsBlocked1 => {
512 Self::StreamsBlocked(StreamsBlockedPacket::new(packet)?)
513 }
514 FrameTypes::NewConnectionId => {
515 Self::NewConnectionId(NewConnectionIdPacket::new(packet)?)
516 }
517 FrameTypes::RetireConnectionId => {
518 Self::RetireConnectionId(RetireConnectionIdPacket::new(packet)?)
519 }
520 FrameTypes::PathChallenge => Self::PathChallenge(PathChallengePacket::new(packet)?),
521 FrameTypes::PathResponse => Self::PathResponse(PathResponsePacket::new(packet)?),
522 FrameTypes::ConnectionClose0 | FrameTypes::ConnectionClose1 => {
523 Self::ConnectionClose(ConnectionClosePacket::new(packet)?)
524 }
525 FrameTypes::HandshakeDone => Self::HandshakeDone(HandshakeDonePacket::new(packet)?),
526 _ => return None,
527 };
528 packet = unsafe { std::mem::transmute(frame.remaining()) };
530 match (frames.last_mut(), frame) {
531 (Some(Frame::Padding(_, x)), Frame::Padding(_, y)) => *x += y,
532 (_, frame) => frames.push(frame),
533 }
534 }
535 Some(frames)
536 }
537
538 fn ty(&self) -> FrameType {
539 match self {
540 Self::Padding(p, _) => p.get_ty(),
541 Self::Ping(p) => p.get_ty(),
542 Self::Ack(p) => p.get_ty(),
543 Self::ResetStream(p) => p.get_ty(),
544 Self::StopSending(p) => p.get_ty(),
545 Self::Crypto(p) => p.get_ty(),
546 Self::NewToken(p) => p.get_ty(),
547 Self::Stream(p) => p.get_ty(),
548 Self::MaxData(p) => p.get_ty(),
549 Self::MaxStreamData(p) => p.get_ty(),
550 Self::MaxStreams(p) => p.get_ty(),
551 Self::DataBlocked(p) => p.get_ty(),
552 Self::StreamDataBlocked(p) => p.get_ty(),
553 Self::StreamsBlocked(p) => p.get_ty(),
554 Self::NewConnectionId(p) => p.get_ty(),
555 Self::RetireConnectionId(p) => p.get_ty(),
556 Self::PathChallenge(p) => p.get_ty(),
557 Self::PathResponse(p) => p.get_ty(),
558 Self::ConnectionClose(p) => p.get_ty(),
559 Self::HandshakeDone(p) => p.get_ty(),
560 }
561 }
562
563 pub fn payload(&self) -> &[u8] {
564 match self {
565 Self::Padding(p, _) => p.payload(),
566 Self::Ping(p) => p.payload(),
567 Self::Ack(p) => p.payload(),
568 Self::ResetStream(p) => p.payload(),
569 Self::StopSending(p) => p.payload(),
570 Self::Crypto(p) => p.payload(),
571 Self::NewToken(p) => p.payload(),
572 Self::Stream(p) => p.payload(),
573 Self::MaxData(p) => p.payload(),
574 Self::MaxStreamData(p) => p.payload(),
575 Self::MaxStreams(p) => p.payload(),
576 Self::DataBlocked(p) => p.payload(),
577 Self::StreamDataBlocked(p) => p.payload(),
578 Self::StreamsBlocked(p) => p.payload(),
579 Self::NewConnectionId(p) => p.payload(),
580 Self::RetireConnectionId(p) => p.payload(),
581 Self::PathChallenge(p) => p.payload(),
582 Self::PathResponse(p) => p.payload(),
583 Self::ConnectionClose(p) => p.payload(),
584 Self::HandshakeDone(p) => p.payload(),
585 }
586 }
587
588 fn remaining(&self) -> &[u8] {
589 match self {
590 Self::Padding(p, _) => p.get_remaining_raw(),
591 Self::Ping(p) => p.get_remaining_raw(),
592 Self::Ack(p) => p.get_remaining_raw(),
593 Self::ResetStream(p) => p.get_remaining_raw(),
594 Self::StopSending(p) => p.get_remaining_raw(),
595 Self::Crypto(p) => p.get_remaining_raw(),
596 Self::NewToken(p) => p.get_remaining_raw(),
597 Self::Stream(p) => p.get_remaining_raw(),
598 Self::MaxData(p) => p.get_remaining_raw(),
599 Self::MaxStreamData(p) => p.get_remaining_raw(),
600 Self::MaxStreams(p) => p.get_remaining_raw(),
601 Self::DataBlocked(p) => p.get_remaining_raw(),
602 Self::StreamDataBlocked(p) => p.get_remaining_raw(),
603 Self::StreamsBlocked(p) => p.get_remaining_raw(),
604 Self::NewConnectionId(p) => p.get_remaining_raw(),
605 Self::RetireConnectionId(p) => p.get_remaining_raw(),
606 Self::PathChallenge(p) => p.get_remaining_raw(),
607 Self::PathResponse(p) => p.get_remaining_raw(),
608 Self::ConnectionClose(p) => p.get_remaining_raw(),
609 Self::HandshakeDone(p) => p.get_remaining_raw(),
610 }
611 }
612}
613
614impl<'a> std::fmt::Display for Frame<'a> {
615 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
616 write!(f, "{}", self.ty())
617 }
618}
619
620#[derive(Debug, Packet)]
621pub struct Padding {
622 #[construct_with(u8)]
623 ty: FrameType,
624 remaining: Vec<u8>,
625}
626
627#[derive(Debug, Packet)]
628pub struct Ping {
629 #[construct_with(u8)]
630 ty: FrameType,
631 remaining: Vec<u8>,
632}
633
634#[derive(Debug, Packet)]
635pub struct Ack {
636 #[construct_with(u8)]
637 ty: FrameType,
638 #[length = "varint_length(...)"]
639 largest_acknowledged: Vec<u8>,
640 #[length = "varint_length(...)"]
641 ack_delay: Vec<u8>,
642 #[length = "varint_length(...)"]
643 ack_range_count: Vec<u8>,
644 #[length = "varint_length(...)"]
645 first_ack_range: Vec<u8>,
646 #[length = "n_varints(varint(&ack_range_count), ...)"]
647 ack_range: Vec<u8>,
648 #[length = "n_varints(if ty.0 == 0x03 { 3 } else { 0 }, ...)"]
649 ecn_counts: Vec<u8>,
650 remaining: Vec<u8>,
651}
652
653#[derive(Debug, Packet)]
654pub struct ResetStream {
655 #[construct_with(u8)]
656 ty: FrameType,
657 #[length = "varint_length(...)"]
658 stream_id: Vec<u8>,
659 #[length = "varint_length(...)"]
660 application_protocol_error_code: Vec<u8>,
661 #[length = "varint_length(...)"]
662 final_size: Vec<u8>,
663 remaining: Vec<u8>,
664}
665
666#[derive(Debug, Packet)]
667pub struct StopSending {
668 #[construct_with(u8)]
669 ty: FrameType,
670 #[length = "varint_length(...)"]
671 stream_id: Vec<u8>,
672 #[length = "varint_length(...)"]
673 application_protocol_error_code: Vec<u8>,
674 remaining: Vec<u8>,
675}
676
677#[derive(Debug, Packet)]
678pub struct Crypto {
679 #[construct_with(u8)]
680 ty: FrameType,
681 #[length = "varint_length(...)"]
682 offset: Vec<u8>,
683 #[length = "varint_length(...)"]
684 length: Vec<u8>,
685 #[length = "varint(&length)"]
686 crypto_payload: Vec<u8>,
687 remaining: Vec<u8>,
688}
689
690#[derive(Debug, Packet)]
691pub struct NewToken {
692 #[construct_with(u8)]
693 ty: FrameType,
694 #[length = "varint_length(...)"]
695 token_length: Vec<u8>,
696 #[length = "varint(&token_length)"]
697 token: Vec<u8>,
698 remaining: Vec<u8>,
699}
700
701#[derive(Debug, Packet)]
702pub struct Stream {
703 #[construct_with(u8)]
704 ty: FrameType,
705 #[length = "varint_length(...)"]
706 stream_id: Vec<u8>,
707 #[length = "n_varints(if ty.0 & 0x04 > 0 { 1 } else { 0 }, ...)"]
708 offset: Vec<u8>,
709 #[length = "n_varints(if ty.0 & 0x02 > 0 { 1 } else { 0 }, ...)"]
710 length: Vec<u8>,
711 #[payload]
712 #[length = "if length.is_empty() { (...).len() } else { varint(&length) }"]
713 stream_data: Vec<u8>,
714 remaining: Vec<u8>,
715}
716
717#[derive(Debug, Packet)]
718pub struct MaxData {
719 #[construct_with(u8)]
720 ty: FrameType,
721 #[length = "varint_length(...)"]
722 max_data: Vec<u8>,
723 remaining: Vec<u8>,
724}
725
726#[derive(Debug, Packet)]
727pub struct MaxStreamData {
728 #[construct_with(u8)]
729 ty: FrameType,
730 #[length = "varint_length(...)"]
731 stream_id: Vec<u8>,
732 #[length = "varint_length(...)"]
733 max_stream_data: Vec<u8>,
734 remaining: Vec<u8>,
735}
736
737#[derive(Debug, Packet)]
738pub struct MaxStreams {
739 #[construct_with(u8)]
740 ty: FrameType,
741 #[length = "varint_length(...)"]
742 max_streams: Vec<u8>,
743 remaining: Vec<u8>,
744}
745
746#[derive(Debug, Packet)]
747pub struct DataBlocked {
748 #[construct_with(u8)]
749 ty: FrameType,
750 #[length = "varint_length(...)"]
751 max_data: Vec<u8>,
752 remaining: Vec<u8>,
753}
754
755#[derive(Debug, Packet)]
756pub struct StreamDataBlocked {
757 #[construct_with(u8)]
758 ty: FrameType,
759 #[length = "varint_length(...)"]
760 stream_id: Vec<u8>,
761 #[length = "varint_length(...)"]
762 max_stream_data: Vec<u8>,
763 remaining: Vec<u8>,
764}
765
766#[derive(Debug, Packet)]
767pub struct StreamsBlocked {
768 #[construct_with(u8)]
769 ty: FrameType,
770 #[length = "varint_length(...)"]
771 max_streams: Vec<u8>,
772 remaining: Vec<u8>,
773}
774
775#[derive(Debug, Packet)]
776pub struct NewConnectionId {
777 #[construct_with(u8)]
778 ty: FrameType,
779 #[length = "varint_length(...)"]
780 sequence_number: Vec<u8>,
781 #[length = "varint_length(...)"]
782 retire_prior_to: Vec<u8>,
783 length: u8,
784 #[length = "length"]
785 connection_id: Vec<u8>,
786 #[length = "16"]
787 stateless_reset_token: Vec<u8>,
788 remaining: Vec<u8>,
789}
790
791#[derive(Debug, Packet)]
792pub struct RetireConnectionId {
793 #[construct_with(u8)]
794 ty: FrameType,
795 #[length = "varint_length(...)"]
796 sequence_number: Vec<u8>,
797 remaining: Vec<u8>,
798}
799
800#[derive(Debug, Packet)]
801pub struct PathChallenge {
802 #[construct_with(u8)]
803 ty: FrameType,
804 #[length = "8"]
805 data: Vec<u8>,
806 remaining: Vec<u8>,
807}
808
809#[derive(Debug, Packet)]
810pub struct PathResponse {
811 #[construct_with(u8)]
812 ty: FrameType,
813 #[length = "8"]
814 data: Vec<u8>,
815 remaining: Vec<u8>,
816}
817
818#[derive(Debug, Packet)]
819pub struct ConnectionClose {
820 #[construct_with(u8)]
821 ty: FrameType,
822 #[length = "varint_length(...)"]
823 error_code: Vec<u8>,
824 #[length = "n_varints(if ty.0 == 0x1d { 0 } else { 1 }, ...)"]
825 frame_type: Vec<u8>,
826 #[length = "varint_length(...)"]
827 reason_phrase_length: Vec<u8>,
828 #[length = "varint(&reason_phrase_length)"]
829 reason_phrase: Vec<u8>,
830 remaining: Vec<u8>,
831}
832
833#[derive(Debug, Packet)]
834pub struct HandshakeDone {
835 #[construct_with(u8)]
836 ty: FrameType,
837 remaining: Vec<u8>,
838}
839
840#[allow(non_snake_case)]
841#[allow(non_upper_case_globals)]
842pub mod FrameTypes {
843 use super::FrameType;
844
845 pub const Padding: FrameType = FrameType(0x00);
846 pub const Ping: FrameType = FrameType(0x01);
847 pub const Ack0: FrameType = FrameType(0x02);
848 pub const Ack1: FrameType = FrameType(0x03);
849 pub const ResetStream: FrameType = FrameType(0x04);
850 pub const StopSending: FrameType = FrameType(0x05);
851 pub const Crypto: FrameType = FrameType(0x06);
852 pub const NewToken: FrameType = FrameType(0x07);
853 pub const Stream0: FrameType = FrameType(0x08);
854 pub const Stream1: FrameType = FrameType(0x09);
855 pub const Stream2: FrameType = FrameType(0x0a);
856 pub const Stream3: FrameType = FrameType(0x0b);
857 pub const Stream4: FrameType = FrameType(0x0c);
858 pub const Stream5: FrameType = FrameType(0x0d);
859 pub const Stream6: FrameType = FrameType(0x0e);
860 pub const Stream7: FrameType = FrameType(0x0f);
861 pub const MaxData: FrameType = FrameType(0x10);
862 pub const MaxStreamData: FrameType = FrameType(0x11);
863 pub const MaxStreams0: FrameType = FrameType(0x12);
864 pub const MaxStreams1: FrameType = FrameType(0x13);
865 pub const DataBlocked: FrameType = FrameType(0x14);
866 pub const StreamDataBlocked: FrameType = FrameType(0x15);
867 pub const StreamsBlocked0: FrameType = FrameType(0x16);
868 pub const StreamsBlocked1: FrameType = FrameType(0x17);
869 pub const NewConnectionId: FrameType = FrameType(0x18);
870 pub const RetireConnectionId: FrameType = FrameType(0x19);
871 pub const PathChallenge: FrameType = FrameType(0x1a);
872 pub const PathResponse: FrameType = FrameType(0x1b);
873 pub const ConnectionClose0: FrameType = FrameType(0x1c);
874 pub const ConnectionClose1: FrameType = FrameType(0x1d);
875 pub const HandshakeDone: FrameType = FrameType(0x1e);
876}
877
878#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
879pub struct FrameType(pub u8);
880
881impl FrameType {
882 pub fn new(val: u8) -> Self {
884 Self(val)
885 }
886}
887
888impl PrimitiveValues for FrameType {
889 type T = (u8,);
890 fn to_primitive_values(&self) -> Self::T {
891 (self.0,)
892 }
893}
894
895impl std::fmt::Display for FrameType {
896 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
897 let s = match self {
898 &FrameTypes::Padding => "padding",
899 &FrameTypes::Ping => "ping",
900 &FrameTypes::Ack0 | &FrameTypes::Ack1 => "ack",
901 &FrameTypes::ResetStream => "reset-stream",
902 &FrameTypes::StopSending => "stop-sending",
903 &FrameTypes::Crypto => "crypto",
904 &FrameTypes::NewToken => "new-token",
905 x if (0x8..=0xf).contains(&x.0) => "stream",
906 &FrameTypes::MaxData => "max-data",
907 &FrameTypes::MaxStreamData => "max-stream-data",
908 &FrameTypes::MaxStreams0 | &FrameTypes::MaxStreams1 => "max-streams",
909 &FrameTypes::DataBlocked => "data-blocked",
910 &FrameTypes::StreamDataBlocked => "stream-data-blocked",
911 &FrameTypes::StreamsBlocked0 | &FrameTypes::StreamsBlocked1 => "streams-blocked",
912 &FrameTypes::NewConnectionId => "new-connection-id",
913 &FrameTypes::RetireConnectionId => "retire-connection-id",
914 &FrameTypes::PathChallenge => "path-challenge",
915 &FrameTypes::PathResponse => "path-response",
916 &FrameTypes::ConnectionClose0 | &FrameTypes::ConnectionClose1 => "connection-close",
917 &FrameTypes::HandshakeDone => "handshake-done",
918 _ => "unknown",
919 };
920 write!(f, "{}", s)
921 }
922}