1use std::convert::TryInto;
28
29use crate::Error;
30use crate::Result;
31
32use crate::packet;
33use crate::ranges;
34use crate::stream;
35
36#[cfg(feature = "qlog")]
37use qlog::events::quic::AckedRanges;
38#[cfg(feature = "qlog")]
39use qlog::events::quic::ErrorSpace;
40#[cfg(feature = "qlog")]
41use qlog::events::quic::QuicFrame;
42#[cfg(feature = "qlog")]
43use qlog::events::quic::StreamType;
44
45pub const MAX_CRYPTO_OVERHEAD: usize = 8;
46pub const MAX_DGRAM_OVERHEAD: usize = 2;
47pub const MAX_STREAM_OVERHEAD: usize = 12;
48pub const MAX_STREAM_SIZE: u64 = 1 << 62;
49
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub struct EcnCounts {
52 ect0_count: u64,
53 ect1_count: u64,
54 ecn_ce_count: u64,
55}
56
57#[derive(Clone, PartialEq, Eq)]
58pub enum Frame {
59 Padding {
60 len: usize,
61 },
62
63 Ping {
64 mtu_probe: Option<usize>,
70 },
71
72 ACK {
73 ack_delay: u64,
74 ranges: ranges::RangeSet,
75 ecn_counts: Option<EcnCounts>,
76 },
77
78 ResetStream {
79 stream_id: u64,
80 error_code: u64,
81 final_size: u64,
82 },
83
84 StopSending {
85 stream_id: u64,
86 error_code: u64,
87 },
88
89 Crypto {
90 data: stream::RangeBuf,
91 },
92
93 CryptoHeader {
94 offset: u64,
95 length: usize,
96 },
97
98 NewToken {
99 token: Vec<u8>,
100 },
101
102 Stream {
103 stream_id: u64,
104 data: stream::RangeBuf,
105 },
106
107 StreamHeader {
108 stream_id: u64,
109 offset: u64,
110 length: usize,
111 fin: bool,
112 },
113
114 MaxData {
115 max: u64,
116 },
117
118 MaxStreamData {
119 stream_id: u64,
120 max: u64,
121 },
122
123 MaxStreamsBidi {
124 max: u64,
125 },
126
127 MaxStreamsUni {
128 max: u64,
129 },
130
131 DataBlocked {
132 limit: u64,
133 },
134
135 StreamDataBlocked {
136 stream_id: u64,
137 limit: u64,
138 },
139
140 StreamsBlockedBidi {
141 limit: u64,
142 },
143
144 StreamsBlockedUni {
145 limit: u64,
146 },
147
148 NewConnectionId {
149 seq_num: u64,
150 retire_prior_to: u64,
151 conn_id: Vec<u8>,
152 reset_token: [u8; 16],
153 },
154
155 RetireConnectionId {
156 seq_num: u64,
157 },
158
159 PathChallenge {
160 data: [u8; 8],
161 },
162
163 PathResponse {
164 data: [u8; 8],
165 },
166
167 ConnectionClose {
168 error_code: u64,
169 frame_type: u64,
170 reason: Vec<u8>,
171 },
172
173 ApplicationClose {
174 error_code: u64,
175 reason: Vec<u8>,
176 },
177
178 HandshakeDone,
179
180 Datagram {
181 data: Vec<u8>,
182 },
183
184 DatagramHeader {
185 length: usize,
186 },
187}
188
189impl Frame {
190 pub fn from_bytes(
191 b: &mut octets::Octets, pkt: packet::Type,
192 ) -> Result<Frame> {
193 let frame_type = b.get_varint()?;
194
195 let frame = match frame_type {
196 0x00 => {
197 let mut len = 1;
198
199 while b.peek_u8() == Ok(0x00) {
200 b.get_u8()?;
201
202 len += 1;
203 }
204
205 Frame::Padding { len }
206 },
207
208 0x01 => Frame::Ping { mtu_probe: None },
209
210 0x02..=0x03 => parse_ack_frame(frame_type, b)?,
211
212 0x04 => Frame::ResetStream {
213 stream_id: b.get_varint()?,
214 error_code: b.get_varint()?,
215 final_size: b.get_varint()?,
216 },
217
218 0x05 => Frame::StopSending {
219 stream_id: b.get_varint()?,
220 error_code: b.get_varint()?,
221 },
222
223 0x06 => {
224 let offset = b.get_varint()?;
225 let data = b.get_bytes_with_varint_length()?;
226 let data = stream::RangeBuf::from(data.as_ref(), offset, false);
227
228 Frame::Crypto { data }
229 },
230
231 0x07 => {
232 let len = b.get_varint()?;
233 if len == 0 {
234 return Err(Error::InvalidFrame);
235 }
236
237 Frame::NewToken {
238 token: b.get_bytes(len as usize)?.to_vec(),
239 }
240 },
241
242 0x08..=0x0f => parse_stream_frame(frame_type, b)?,
243
244 0x10 => Frame::MaxData {
245 max: b.get_varint()?,
246 },
247
248 0x11 => Frame::MaxStreamData {
249 stream_id: b.get_varint()?,
250 max: b.get_varint()?,
251 },
252
253 0x12 => Frame::MaxStreamsBidi {
254 max: b.get_varint()?,
255 },
256
257 0x13 => Frame::MaxStreamsUni {
258 max: b.get_varint()?,
259 },
260
261 0x14 => Frame::DataBlocked {
262 limit: b.get_varint()?,
263 },
264
265 0x15 => Frame::StreamDataBlocked {
266 stream_id: b.get_varint()?,
267 limit: b.get_varint()?,
268 },
269
270 0x16 => Frame::StreamsBlockedBidi {
271 limit: b.get_varint()?,
272 },
273
274 0x17 => Frame::StreamsBlockedUni {
275 limit: b.get_varint()?,
276 },
277
278 0x18 => {
279 let seq_num = b.get_varint()?;
280 let retire_prior_to = b.get_varint()?;
281 let conn_id_len = b.get_u8()?;
282
283 if !(1..=packet::MAX_CID_LEN).contains(&conn_id_len) {
284 return Err(Error::InvalidFrame);
285 }
286
287 Frame::NewConnectionId {
288 seq_num,
289 retire_prior_to,
290 conn_id: b.get_bytes(conn_id_len as usize)?.to_vec(),
291 reset_token: b
292 .get_bytes(16)?
293 .buf()
294 .try_into()
295 .map_err(|_| Error::BufferTooShort)?,
296 }
297 },
298
299 0x19 => Frame::RetireConnectionId {
300 seq_num: b.get_varint()?,
301 },
302
303 0x1a => Frame::PathChallenge {
304 data: b
305 .get_bytes(8)?
306 .buf()
307 .try_into()
308 .map_err(|_| Error::BufferTooShort)?,
309 },
310
311 0x1b => Frame::PathResponse {
312 data: b
313 .get_bytes(8)?
314 .buf()
315 .try_into()
316 .map_err(|_| Error::BufferTooShort)?,
317 },
318
319 0x1c => Frame::ConnectionClose {
320 error_code: b.get_varint()?,
321 frame_type: b.get_varint()?,
322 reason: b.get_bytes_with_varint_length()?.to_vec(),
323 },
324
325 0x1d => Frame::ApplicationClose {
326 error_code: b.get_varint()?,
327 reason: b.get_bytes_with_varint_length()?.to_vec(),
328 },
329
330 0x1e => Frame::HandshakeDone,
331
332 0x30 | 0x31 => parse_datagram_frame(frame_type, b)?,
333
334 _ => return Err(Error::InvalidFrame),
335 };
336
337 let allowed = match (pkt, &frame) {
338 (_, Frame::Padding { .. }) | (_, Frame::Ping { .. }) => true,
340
341 (packet::Type::ZeroRTT, Frame::ACK { .. }) => false,
344 (packet::Type::ZeroRTT, Frame::Crypto { .. }) => false,
345 (packet::Type::ZeroRTT, Frame::HandshakeDone) => false,
346 (packet::Type::ZeroRTT, Frame::NewToken { .. }) => false,
347 (packet::Type::ZeroRTT, Frame::PathResponse { .. }) => false,
348 (packet::Type::ZeroRTT, Frame::RetireConnectionId { .. }) => false,
349 (packet::Type::ZeroRTT, Frame::ConnectionClose { .. }) => false,
350
351 (_, Frame::ACK { .. }) => true,
354 (_, Frame::Crypto { .. }) => true,
355 (_, Frame::ConnectionClose { .. }) => true,
356
357 (packet::Type::Short, _) => true,
359 (packet::Type::ZeroRTT, _) => true,
360
361 (..) => false,
363 };
364
365 if !allowed {
366 return Err(Error::InvalidPacket);
367 }
368
369 Ok(frame)
370 }
371
372 pub fn to_bytes(&self, b: &mut octets::OctetsMut) -> Result<usize> {
373 let before = b.cap();
374
375 match self {
376 Frame::Padding { len } => {
377 let mut left = *len;
378
379 while left > 0 {
380 b.put_varint(0x00)?;
381
382 left -= 1;
383 }
384 },
385
386 Frame::Ping { .. } => {
387 b.put_varint(0x01)?;
388 },
389
390 Frame::ACK {
391 ack_delay,
392 ranges,
393 ecn_counts,
394 } => {
395 if ecn_counts.is_none() {
396 b.put_varint(0x02)?;
397 } else {
398 b.put_varint(0x03)?;
399 }
400
401 let mut it = ranges.iter().rev();
402
403 let first = it.next().unwrap();
404 let ack_block = (first.end - 1) - first.start;
405
406 b.put_varint(first.end - 1)?;
407 b.put_varint(*ack_delay)?;
408 b.put_varint(it.len() as u64)?;
409 b.put_varint(ack_block)?;
410
411 let mut smallest_ack = first.start;
412
413 for block in it {
414 let gap = smallest_ack - block.end - 1;
415 let ack_block = (block.end - 1) - block.start;
416
417 b.put_varint(gap)?;
418 b.put_varint(ack_block)?;
419
420 smallest_ack = block.start;
421 }
422
423 if let Some(ecn) = ecn_counts {
424 b.put_varint(ecn.ect0_count)?;
425 b.put_varint(ecn.ect1_count)?;
426 b.put_varint(ecn.ecn_ce_count)?;
427 }
428 },
429
430 Frame::ResetStream {
431 stream_id,
432 error_code,
433 final_size,
434 } => {
435 b.put_varint(0x04)?;
436
437 b.put_varint(*stream_id)?;
438 b.put_varint(*error_code)?;
439 b.put_varint(*final_size)?;
440 },
441
442 Frame::StopSending {
443 stream_id,
444 error_code,
445 } => {
446 b.put_varint(0x05)?;
447
448 b.put_varint(*stream_id)?;
449 b.put_varint(*error_code)?;
450 },
451
452 Frame::Crypto { data } => {
453 encode_crypto_header(data.off(), data.len() as u64, b)?;
454
455 b.put_bytes(data)?;
456 },
457
458 Frame::CryptoHeader { .. } => (),
459
460 Frame::NewToken { token } => {
461 b.put_varint(0x07)?;
462
463 b.put_varint(token.len() as u64)?;
464 b.put_bytes(token)?;
465 },
466
467 Frame::Stream { stream_id, data } => {
468 encode_stream_header(
469 *stream_id,
470 data.off(),
471 data.len() as u64,
472 data.fin(),
473 b,
474 )?;
475
476 b.put_bytes(data)?;
477 },
478
479 Frame::StreamHeader { .. } => (),
480
481 Frame::MaxData { max } => {
482 b.put_varint(0x10)?;
483
484 b.put_varint(*max)?;
485 },
486
487 Frame::MaxStreamData { stream_id, max } => {
488 b.put_varint(0x11)?;
489
490 b.put_varint(*stream_id)?;
491 b.put_varint(*max)?;
492 },
493
494 Frame::MaxStreamsBidi { max } => {
495 b.put_varint(0x12)?;
496
497 b.put_varint(*max)?;
498 },
499
500 Frame::MaxStreamsUni { max } => {
501 b.put_varint(0x13)?;
502
503 b.put_varint(*max)?;
504 },
505
506 Frame::DataBlocked { limit } => {
507 b.put_varint(0x14)?;
508
509 b.put_varint(*limit)?;
510 },
511
512 Frame::StreamDataBlocked { stream_id, limit } => {
513 b.put_varint(0x15)?;
514
515 b.put_varint(*stream_id)?;
516 b.put_varint(*limit)?;
517 },
518
519 Frame::StreamsBlockedBidi { limit } => {
520 b.put_varint(0x16)?;
521
522 b.put_varint(*limit)?;
523 },
524
525 Frame::StreamsBlockedUni { limit } => {
526 b.put_varint(0x17)?;
527
528 b.put_varint(*limit)?;
529 },
530
531 Frame::NewConnectionId {
532 seq_num,
533 retire_prior_to,
534 conn_id,
535 reset_token,
536 } => {
537 b.put_varint(0x18)?;
538
539 b.put_varint(*seq_num)?;
540 b.put_varint(*retire_prior_to)?;
541 b.put_u8(conn_id.len() as u8)?;
542 b.put_bytes(conn_id.as_ref())?;
543 b.put_bytes(reset_token.as_ref())?;
544 },
545
546 Frame::RetireConnectionId { seq_num } => {
547 b.put_varint(0x19)?;
548
549 b.put_varint(*seq_num)?;
550 },
551
552 Frame::PathChallenge { data } => {
553 b.put_varint(0x1a)?;
554
555 b.put_bytes(data.as_ref())?;
556 },
557
558 Frame::PathResponse { data } => {
559 b.put_varint(0x1b)?;
560
561 b.put_bytes(data.as_ref())?;
562 },
563
564 Frame::ConnectionClose {
565 error_code,
566 frame_type,
567 reason,
568 } => {
569 b.put_varint(0x1c)?;
570
571 b.put_varint(*error_code)?;
572 b.put_varint(*frame_type)?;
573 b.put_varint(reason.len() as u64)?;
574 b.put_bytes(reason.as_ref())?;
575 },
576
577 Frame::ApplicationClose { error_code, reason } => {
578 b.put_varint(0x1d)?;
579
580 b.put_varint(*error_code)?;
581 b.put_varint(reason.len() as u64)?;
582 b.put_bytes(reason.as_ref())?;
583 },
584
585 Frame::HandshakeDone => {
586 b.put_varint(0x1e)?;
587 },
588
589 Frame::Datagram { data } => {
590 encode_dgram_header(data.len() as u64, b)?;
591
592 b.put_bytes(data.as_ref())?;
593 },
594
595 Frame::DatagramHeader { .. } => (),
596 }
597
598 Ok(before - b.cap())
599 }
600
601 pub fn wire_len(&self) -> usize {
602 match self {
603 Frame::Padding { len } => *len,
604
605 Frame::Ping { .. } => 1,
606
607 Frame::ACK {
608 ack_delay,
609 ranges,
610 ecn_counts,
611 } => {
612 let mut it = ranges.iter().rev();
613
614 let first = it.next().unwrap();
615 let ack_block = (first.end - 1) - first.start;
616
617 let mut len = 1 + octets::varint_len(first.end - 1) + octets::varint_len(*ack_delay) + octets::varint_len(it.len() as u64) + octets::varint_len(ack_block); let mut smallest_ack = first.start;
624
625 for block in it {
626 let gap = smallest_ack - block.end - 1;
627 let ack_block = (block.end - 1) - block.start;
628
629 len += octets::varint_len(gap) + octets::varint_len(ack_block); smallest_ack = block.start;
633 }
634
635 if let Some(ecn) = ecn_counts {
636 len += octets::varint_len(ecn.ect0_count) +
637 octets::varint_len(ecn.ect1_count) +
638 octets::varint_len(ecn.ecn_ce_count);
639 }
640
641 len
642 },
643
644 Frame::ResetStream {
645 stream_id,
646 error_code,
647 final_size,
648 } => {
649 1 + octets::varint_len(*stream_id) + octets::varint_len(*error_code) + octets::varint_len(*final_size) },
654
655 Frame::StopSending {
656 stream_id,
657 error_code,
658 } => {
659 1 + octets::varint_len(*stream_id) + octets::varint_len(*error_code) },
663
664 Frame::Crypto { data } => {
665 1 + octets::varint_len(data.off()) + 2 + data.len() },
670
671 Frame::CryptoHeader { offset, length, .. } => {
672 1 + octets::varint_len(*offset) + 2 + length },
677
678 Frame::NewToken { token } => {
679 1 + octets::varint_len(token.len() as u64) + token.len() },
683
684 Frame::Stream { stream_id, data } => {
685 1 + octets::varint_len(*stream_id) + octets::varint_len(data.off()) + 2 + data.len() },
691
692 Frame::StreamHeader {
693 stream_id,
694 offset,
695 length,
696 ..
697 } => {
698 1 + octets::varint_len(*stream_id) + octets::varint_len(*offset) + 2 + length },
704
705 Frame::MaxData { max } => {
706 1 + octets::varint_len(*max) },
709
710 Frame::MaxStreamData { stream_id, max } => {
711 1 + octets::varint_len(*stream_id) + octets::varint_len(*max) },
715
716 Frame::MaxStreamsBidi { max } => {
717 1 + octets::varint_len(*max) },
720
721 Frame::MaxStreamsUni { max } => {
722 1 + octets::varint_len(*max) },
725
726 Frame::DataBlocked { limit } => {
727 1 + octets::varint_len(*limit) },
730
731 Frame::StreamDataBlocked { stream_id, limit } => {
732 1 + octets::varint_len(*stream_id) + octets::varint_len(*limit) },
736
737 Frame::StreamsBlockedBidi { limit } => {
738 1 + octets::varint_len(*limit) },
741
742 Frame::StreamsBlockedUni { limit } => {
743 1 + octets::varint_len(*limit) },
746
747 Frame::NewConnectionId {
748 seq_num,
749 retire_prior_to,
750 conn_id,
751 reset_token,
752 } => {
753 1 + octets::varint_len(*seq_num) + octets::varint_len(*retire_prior_to) + 1 + conn_id.len() + reset_token.len() },
760
761 Frame::RetireConnectionId { seq_num } => {
762 1 + octets::varint_len(*seq_num) },
765
766 Frame::PathChallenge { .. } => {
767 1 + 8 },
770
771 Frame::PathResponse { .. } => {
772 1 + 8 },
775
776 Frame::ConnectionClose {
777 frame_type,
778 error_code,
779 reason,
780 ..
781 } => {
782 1 + octets::varint_len(*error_code) + octets::varint_len(*frame_type) + octets::varint_len(reason.len() as u64) + reason.len() },
788
789 Frame::ApplicationClose { reason, error_code } => {
790 1 + octets::varint_len(*error_code) + octets::varint_len(reason.len() as u64) + reason.len() },
795
796 Frame::HandshakeDone => {
797 1 },
799
800 Frame::Datagram { data } => {
801 1 + 2 + data.len() },
805
806 Frame::DatagramHeader { length } => {
807 1 + 2 + *length },
811 }
812 }
813
814 pub fn ack_eliciting(&self) -> bool {
815 !matches!(
817 self,
818 Frame::Padding { .. } |
819 Frame::ACK { .. } |
820 Frame::ApplicationClose { .. } |
821 Frame::ConnectionClose { .. }
822 )
823 }
824
825 pub fn probing(&self) -> bool {
826 matches!(
827 self,
828 Frame::Padding { .. } |
829 Frame::NewConnectionId { .. } |
830 Frame::PathChallenge { .. } |
831 Frame::PathResponse { .. }
832 )
833 }
834
835 #[cfg(feature = "qlog")]
836 pub fn to_qlog(&self) -> QuicFrame {
837 match self {
838 Frame::Padding { len } => QuicFrame::Padding {
839 length: None,
840 payload_length: *len as u32,
841 },
842
843 Frame::Ping { .. } => QuicFrame::Ping {
844 length: None,
845 payload_length: None,
846 },
847
848 Frame::ACK {
849 ack_delay,
850 ranges,
851 ecn_counts,
852 } => {
853 let ack_ranges = AckedRanges::Double(
854 ranges.iter().map(|r| (r.start, r.end - 1)).collect(),
855 );
856
857 let (ect0, ect1, ce) = match ecn_counts {
858 Some(ecn) => (
859 Some(ecn.ect0_count),
860 Some(ecn.ect1_count),
861 Some(ecn.ecn_ce_count),
862 ),
863
864 None => (None, None, None),
865 };
866
867 QuicFrame::Ack {
868 ack_delay: Some(*ack_delay as f32 / 1000.0),
869 acked_ranges: Some(ack_ranges),
870 ect1,
871 ect0,
872 ce,
873 length: None,
874 payload_length: None,
875 }
876 },
877
878 Frame::ResetStream {
879 stream_id,
880 error_code,
881 final_size,
882 } => QuicFrame::ResetStream {
883 stream_id: *stream_id,
884 error_code: *error_code,
885 final_size: *final_size,
886 length: None,
887 payload_length: None,
888 },
889
890 Frame::StopSending {
891 stream_id,
892 error_code,
893 } => QuicFrame::StopSending {
894 stream_id: *stream_id,
895 error_code: *error_code,
896 length: None,
897 payload_length: None,
898 },
899
900 Frame::Crypto { data } => QuicFrame::Crypto {
901 offset: data.off(),
902 length: data.len() as u64,
903 },
904
905 Frame::CryptoHeader { offset, length } => QuicFrame::Crypto {
906 offset: *offset,
907 length: *length as u64,
908 },
909
910 Frame::NewToken { token } => QuicFrame::NewToken {
911 token: qlog::Token {
912 ty: Some(qlog::TokenType::Retry),
914 raw: Some(qlog::events::RawInfo {
915 data: qlog::HexSlice::maybe_string(Some(token)),
916 length: Some(token.len() as u64),
917 payload_length: None,
918 }),
919 details: None,
920 },
921 },
922
923 Frame::Stream { stream_id, data } => QuicFrame::Stream {
924 stream_id: *stream_id,
925 offset: data.off(),
926 length: data.len() as u64,
927 fin: data.fin().then_some(true),
928 raw: None,
929 },
930
931 Frame::StreamHeader {
932 stream_id,
933 offset,
934 length,
935 fin,
936 } => QuicFrame::Stream {
937 stream_id: *stream_id,
938 offset: *offset,
939 length: *length as u64,
940 fin: fin.then(|| true),
941 raw: None,
942 },
943
944 Frame::MaxData { max } => QuicFrame::MaxData { maximum: *max },
945
946 Frame::MaxStreamData { stream_id, max } => QuicFrame::MaxStreamData {
947 stream_id: *stream_id,
948 maximum: *max,
949 },
950
951 Frame::MaxStreamsBidi { max } => QuicFrame::MaxStreams {
952 stream_type: StreamType::Bidirectional,
953 maximum: *max,
954 },
955
956 Frame::MaxStreamsUni { max } => QuicFrame::MaxStreams {
957 stream_type: StreamType::Unidirectional,
958 maximum: *max,
959 },
960
961 Frame::DataBlocked { limit } =>
962 QuicFrame::DataBlocked { limit: *limit },
963
964 Frame::StreamDataBlocked { stream_id, limit } =>
965 QuicFrame::StreamDataBlocked {
966 stream_id: *stream_id,
967 limit: *limit,
968 },
969
970 Frame::StreamsBlockedBidi { limit } => QuicFrame::StreamsBlocked {
971 stream_type: StreamType::Bidirectional,
972 limit: *limit,
973 },
974
975 Frame::StreamsBlockedUni { limit } => QuicFrame::StreamsBlocked {
976 stream_type: StreamType::Unidirectional,
977 limit: *limit,
978 },
979
980 Frame::NewConnectionId {
981 seq_num,
982 retire_prior_to,
983 conn_id,
984 reset_token,
985 } => QuicFrame::NewConnectionId {
986 sequence_number: *seq_num as u32,
987 retire_prior_to: *retire_prior_to as u32,
988 connection_id_length: Some(conn_id.len() as u8),
989 connection_id: format!("{}", qlog::HexSlice::new(conn_id)),
990 stateless_reset_token: qlog::HexSlice::maybe_string(Some(
991 reset_token,
992 )),
993 },
994
995 Frame::RetireConnectionId { seq_num } =>
996 QuicFrame::RetireConnectionId {
997 sequence_number: *seq_num as u32,
998 },
999
1000 Frame::PathChallenge { .. } =>
1001 QuicFrame::PathChallenge { data: None },
1002
1003 Frame::PathResponse { .. } => QuicFrame::PathResponse { data: None },
1004
1005 Frame::ConnectionClose {
1006 error_code, reason, ..
1007 } => QuicFrame::ConnectionClose {
1008 error_space: Some(ErrorSpace::TransportError),
1009 error_code: Some(*error_code),
1010 error_code_value: None, reason: Some(String::from_utf8_lossy(reason).into_owned()),
1012 trigger_frame_type: None, },
1014
1015 Frame::ApplicationClose { error_code, reason } => {
1016 QuicFrame::ConnectionClose {
1017 error_space: Some(ErrorSpace::ApplicationError),
1018 error_code: Some(*error_code),
1019 error_code_value: None, reason: Some(String::from_utf8_lossy(reason).into_owned()),
1021 trigger_frame_type: None, }
1023 },
1024
1025 Frame::HandshakeDone => QuicFrame::HandshakeDone,
1026
1027 Frame::Datagram { data } => QuicFrame::Datagram {
1028 length: data.len() as u64,
1029 raw: None,
1030 },
1031
1032 Frame::DatagramHeader { length } => QuicFrame::Datagram {
1033 length: *length as u64,
1034 raw: None,
1035 },
1036 }
1037 }
1038}
1039
1040impl std::fmt::Debug for Frame {
1041 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1042 match self {
1043 Frame::Padding { len } => {
1044 write!(f, "PADDING len={len}")?;
1045 },
1046
1047 Frame::Ping { mtu_probe } => {
1048 write!(f, "PING mtu_probe={mtu_probe:?}")?;
1049 },
1050
1051 Frame::ACK {
1052 ack_delay,
1053 ranges,
1054 ecn_counts,
1055 } => {
1056 write!(
1057 f,
1058 "ACK delay={ack_delay} blocks={ranges:?} ecn_counts={ecn_counts:?}"
1059 )?;
1060 },
1061
1062 Frame::ResetStream {
1063 stream_id,
1064 error_code,
1065 final_size,
1066 } => {
1067 write!(
1068 f,
1069 "RESET_STREAM stream={stream_id} err={error_code:x} size={final_size}"
1070 )?;
1071 },
1072
1073 Frame::StopSending {
1074 stream_id,
1075 error_code,
1076 } => {
1077 write!(f, "STOP_SENDING stream={stream_id} err={error_code:x}")?;
1078 },
1079
1080 Frame::Crypto { data } => {
1081 write!(f, "CRYPTO off={} len={}", data.off(), data.len())?;
1082 },
1083
1084 Frame::CryptoHeader { offset, length } => {
1085 write!(f, "CRYPTO off={offset} len={length}")?;
1086 },
1087
1088 Frame::NewToken { token } => {
1089 write!(f, "NEW_TOKEN len={}", token.len())?;
1090 },
1091
1092 Frame::Stream { stream_id, data } => {
1093 write!(
1094 f,
1095 "STREAM id={} off={} len={} fin={}",
1096 stream_id,
1097 data.off(),
1098 data.len(),
1099 data.fin()
1100 )?;
1101 },
1102
1103 Frame::StreamHeader {
1104 stream_id,
1105 offset,
1106 length,
1107 fin,
1108 } => {
1109 write!(
1110 f,
1111 "STREAM id={stream_id} off={offset} len={length} fin={fin}"
1112 )?;
1113 },
1114
1115 Frame::MaxData { max } => {
1116 write!(f, "MAX_DATA max={max}")?;
1117 },
1118
1119 Frame::MaxStreamData { stream_id, max } => {
1120 write!(f, "MAX_STREAM_DATA stream={stream_id} max={max}")?;
1121 },
1122
1123 Frame::MaxStreamsBidi { max } => {
1124 write!(f, "MAX_STREAMS type=bidi max={max}")?;
1125 },
1126
1127 Frame::MaxStreamsUni { max } => {
1128 write!(f, "MAX_STREAMS type=uni max={max}")?;
1129 },
1130
1131 Frame::DataBlocked { limit } => {
1132 write!(f, "DATA_BLOCKED limit={limit}")?;
1133 },
1134
1135 Frame::StreamDataBlocked { stream_id, limit } => {
1136 write!(
1137 f,
1138 "STREAM_DATA_BLOCKED stream={stream_id} limit={limit}"
1139 )?;
1140 },
1141
1142 Frame::StreamsBlockedBidi { limit } => {
1143 write!(f, "STREAMS_BLOCKED type=bidi limit={limit}")?;
1144 },
1145
1146 Frame::StreamsBlockedUni { limit } => {
1147 write!(f, "STREAMS_BLOCKED type=uni limit={limit}")?;
1148 },
1149
1150 Frame::NewConnectionId {
1151 seq_num,
1152 retire_prior_to,
1153 conn_id,
1154 reset_token,
1155 } => {
1156 write!(
1157 f,
1158 "NEW_CONNECTION_ID seq_num={seq_num} retire_prior_to={retire_prior_to} conn_id={conn_id:02x?} reset_token={reset_token:02x?}",
1159 )?;
1160 },
1161
1162 Frame::RetireConnectionId { seq_num } => {
1163 write!(f, "RETIRE_CONNECTION_ID seq_num={seq_num}")?;
1164 },
1165
1166 Frame::PathChallenge { data } => {
1167 write!(f, "PATH_CHALLENGE data={data:02x?}")?;
1168 },
1169
1170 Frame::PathResponse { data } => {
1171 write!(f, "PATH_RESPONSE data={data:02x?}")?;
1172 },
1173
1174 Frame::ConnectionClose {
1175 error_code,
1176 frame_type,
1177 reason,
1178 } => {
1179 write!(
1180 f,
1181 "CONNECTION_CLOSE err={error_code:x} frame={frame_type:x} reason={reason:x?}"
1182 )?;
1183 },
1184
1185 Frame::ApplicationClose { error_code, reason } => {
1186 write!(
1187 f,
1188 "APPLICATION_CLOSE err={error_code:x} reason={reason:x?}"
1189 )?;
1190 },
1191
1192 Frame::HandshakeDone => {
1193 write!(f, "HANDSHAKE_DONE")?;
1194 },
1195
1196 Frame::Datagram { data } => {
1197 write!(f, "DATAGRAM len={}", data.len())?;
1198 },
1199
1200 Frame::DatagramHeader { length } => {
1201 write!(f, "DATAGRAM len={length}")?;
1202 },
1203 }
1204
1205 Ok(())
1206 }
1207}
1208
1209fn parse_ack_frame(ty: u64, b: &mut octets::Octets) -> Result<Frame> {
1210 let first = ty as u8;
1211
1212 let largest_ack = b.get_varint()?;
1213 let ack_delay = b.get_varint()?;
1214 let block_count = b.get_varint()?;
1215 let ack_block = b.get_varint()?;
1216
1217 if largest_ack < ack_block {
1218 return Err(Error::InvalidFrame);
1219 }
1220
1221 let mut smallest_ack = largest_ack - ack_block;
1222
1223 let mut ranges = ranges::RangeSet::default();
1224
1225 ranges.insert(smallest_ack..largest_ack + 1);
1226
1227 for _i in 0..block_count {
1228 let gap = b.get_varint()?;
1229
1230 if smallest_ack < 2 + gap {
1231 return Err(Error::InvalidFrame);
1232 }
1233
1234 let largest_ack = (smallest_ack - gap) - 2;
1235 let ack_block = b.get_varint()?;
1236
1237 if largest_ack < ack_block {
1238 return Err(Error::InvalidFrame);
1239 }
1240
1241 smallest_ack = largest_ack - ack_block;
1242
1243 ranges.insert(smallest_ack..largest_ack + 1);
1244 }
1245
1246 let ecn_counts = if first & 0x01 != 0 {
1247 let ecn = EcnCounts {
1248 ect0_count: b.get_varint()?,
1249 ect1_count: b.get_varint()?,
1250 ecn_ce_count: b.get_varint()?,
1251 };
1252
1253 Some(ecn)
1254 } else {
1255 None
1256 };
1257
1258 Ok(Frame::ACK {
1259 ack_delay,
1260 ranges,
1261 ecn_counts,
1262 })
1263}
1264
1265pub fn encode_crypto_header(
1266 offset: u64, length: u64, b: &mut octets::OctetsMut,
1267) -> Result<()> {
1268 b.put_varint(0x06)?;
1269
1270 b.put_varint(offset)?;
1271
1272 b.put_varint_with_len(length, 2)?;
1274
1275 Ok(())
1276}
1277
1278pub fn encode_stream_header(
1279 stream_id: u64, offset: u64, length: u64, fin: bool,
1280 b: &mut octets::OctetsMut,
1281) -> Result<()> {
1282 let mut ty: u8 = 0x08;
1283
1284 ty |= 0x04;
1286
1287 ty |= 0x02;
1289
1290 if fin {
1291 ty |= 0x01;
1292 }
1293
1294 b.put_varint(u64::from(ty))?;
1295
1296 b.put_varint(stream_id)?;
1297 b.put_varint(offset)?;
1298
1299 b.put_varint_with_len(length, 2)?;
1301
1302 Ok(())
1303}
1304
1305pub fn encode_dgram_header(length: u64, b: &mut octets::OctetsMut) -> Result<()> {
1306 let mut ty: u8 = 0x30;
1307
1308 ty |= 0x01;
1310
1311 b.put_varint(u64::from(ty))?;
1312
1313 b.put_varint_with_len(length, 2)?;
1315
1316 Ok(())
1317}
1318
1319fn parse_stream_frame(ty: u64, b: &mut octets::Octets) -> Result<Frame> {
1320 let first = ty as u8;
1321
1322 let stream_id = b.get_varint()?;
1323
1324 let offset = if first & 0x04 != 0 {
1325 b.get_varint()?
1326 } else {
1327 0
1328 };
1329
1330 let len = if first & 0x02 != 0 {
1331 b.get_varint()? as usize
1332 } else {
1333 b.cap()
1334 };
1335
1336 if offset + len as u64 >= MAX_STREAM_SIZE {
1337 return Err(Error::InvalidFrame);
1338 }
1339
1340 let fin = first & 0x01 != 0;
1341
1342 let data = b.get_bytes(len)?;
1343 let data = stream::RangeBuf::from(data.as_ref(), offset, fin);
1344
1345 Ok(Frame::Stream { stream_id, data })
1346}
1347
1348fn parse_datagram_frame(ty: u64, b: &mut octets::Octets) -> Result<Frame> {
1349 let first = ty as u8;
1350
1351 let len = if first & 0x01 != 0 {
1352 b.get_varint()? as usize
1353 } else {
1354 b.cap()
1355 };
1356
1357 let data = b.get_bytes(len)?;
1358
1359 Ok(Frame::Datagram {
1360 data: Vec::from(data.buf()),
1361 })
1362}
1363
1364#[cfg(test)]
1365mod tests {
1366 use super::*;
1367
1368 #[test]
1369 fn padding() {
1370 let mut d = [42; 128];
1371
1372 let frame = Frame::Padding { len: 128 };
1373
1374 let wire_len = {
1375 let mut b = octets::OctetsMut::with_slice(&mut d);
1376 frame.to_bytes(&mut b).unwrap()
1377 };
1378
1379 assert_eq!(wire_len, 128);
1380
1381 let mut b = octets::Octets::with_slice(&d);
1382 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1383
1384 let mut b = octets::Octets::with_slice(&d);
1385 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1386
1387 let mut b = octets::Octets::with_slice(&d);
1388 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1389
1390 let mut b = octets::Octets::with_slice(&d);
1391 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1392 }
1393
1394 #[test]
1395 fn ping() {
1396 let mut d = [42; 128];
1397
1398 let frame = Frame::Ping { mtu_probe: None };
1399
1400 let wire_len = {
1401 let mut b = octets::OctetsMut::with_slice(&mut d);
1402 frame.to_bytes(&mut b).unwrap()
1403 };
1404
1405 assert_eq!(wire_len, 1);
1406 assert_eq!(&d[..wire_len], [0x01_u8]);
1407
1408 let mut b = octets::Octets::with_slice(&d);
1409 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1410
1411 let mut b = octets::Octets::with_slice(&d);
1412 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1413
1414 let mut b = octets::Octets::with_slice(&d);
1415 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1416
1417 let mut b = octets::Octets::with_slice(&d);
1418 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1419 }
1420
1421 #[test]
1422 fn ack() {
1423 let mut d = [42; 128];
1424
1425 let mut ranges = ranges::RangeSet::default();
1426 ranges.insert(4..7);
1427 ranges.insert(9..12);
1428 ranges.insert(15..19);
1429 ranges.insert(3000..5000);
1430
1431 let frame = Frame::ACK {
1432 ack_delay: 874_656_534,
1433 ranges,
1434 ecn_counts: None,
1435 };
1436
1437 let wire_len = {
1438 let mut b = octets::OctetsMut::with_slice(&mut d);
1439 frame.to_bytes(&mut b).unwrap()
1440 };
1441
1442 assert_eq!(wire_len, 17);
1443
1444 let mut b = octets::Octets::with_slice(&d);
1445 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1446
1447 let mut b = octets::Octets::with_slice(&d);
1448 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1449
1450 let mut b = octets::Octets::with_slice(&d);
1451 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1452
1453 let mut b = octets::Octets::with_slice(&d);
1454 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1455 }
1456
1457 #[test]
1458 fn ack_ecn() {
1459 let mut d = [42; 128];
1460
1461 let mut ranges = ranges::RangeSet::default();
1462 ranges.insert(4..7);
1463 ranges.insert(9..12);
1464 ranges.insert(15..19);
1465 ranges.insert(3000..5000);
1466
1467 let ecn_counts = Some(EcnCounts {
1468 ect0_count: 100,
1469 ect1_count: 200,
1470 ecn_ce_count: 300,
1471 });
1472
1473 let frame = Frame::ACK {
1474 ack_delay: 874_656_534,
1475 ranges,
1476 ecn_counts,
1477 };
1478
1479 let wire_len = {
1480 let mut b = octets::OctetsMut::with_slice(&mut d);
1481 frame.to_bytes(&mut b).unwrap()
1482 };
1483
1484 assert_eq!(wire_len, 23);
1485
1486 let mut b = octets::Octets::with_slice(&d);
1487 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1488
1489 let mut b = octets::Octets::with_slice(&d);
1490 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1491
1492 let mut b = octets::Octets::with_slice(&d);
1493 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1494
1495 let mut b = octets::Octets::with_slice(&d);
1496 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1497 }
1498
1499 #[test]
1500 fn reset_stream() {
1501 let mut d = [42; 128];
1502
1503 let frame = Frame::ResetStream {
1504 stream_id: 123_213,
1505 error_code: 21_123_767,
1506 final_size: 21_123_767,
1507 };
1508
1509 let wire_len = {
1510 let mut b = octets::OctetsMut::with_slice(&mut d);
1511 frame.to_bytes(&mut b).unwrap()
1512 };
1513
1514 assert_eq!(wire_len, 13);
1515
1516 let mut b = octets::Octets::with_slice(&d);
1517 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1518
1519 let mut b = octets::Octets::with_slice(&d);
1520 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1521
1522 let mut b = octets::Octets::with_slice(&d);
1523 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1524
1525 let mut b = octets::Octets::with_slice(&d);
1526 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1527 }
1528
1529 #[test]
1530 fn stop_sending() {
1531 let mut d = [42; 128];
1532
1533 let frame = Frame::StopSending {
1534 stream_id: 123_213,
1535 error_code: 15_352,
1536 };
1537
1538 let wire_len = {
1539 let mut b = octets::OctetsMut::with_slice(&mut d);
1540 frame.to_bytes(&mut b).unwrap()
1541 };
1542
1543 assert_eq!(wire_len, 7);
1544
1545 let mut b = octets::Octets::with_slice(&d);
1546 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1547
1548 let mut b = octets::Octets::with_slice(&d);
1549 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1550
1551 let mut b = octets::Octets::with_slice(&d);
1552 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1553
1554 let mut b = octets::Octets::with_slice(&d);
1555 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1556 }
1557
1558 #[test]
1559 fn crypto() {
1560 let mut d = [42; 128];
1561
1562 let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1563
1564 let frame = Frame::Crypto {
1565 data: stream::RangeBuf::from(&data, 1230976, false),
1566 };
1567
1568 let wire_len = {
1569 let mut b = octets::OctetsMut::with_slice(&mut d);
1570 frame.to_bytes(&mut b).unwrap()
1571 };
1572
1573 assert_eq!(wire_len, 19);
1574
1575 let mut b = octets::Octets::with_slice(&d);
1576 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1577
1578 let mut b = octets::Octets::with_slice(&d);
1579 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1580
1581 let mut b = octets::Octets::with_slice(&d);
1582 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1583
1584 let mut b = octets::Octets::with_slice(&d);
1585 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1586 }
1587
1588 #[test]
1589 fn new_token() {
1590 let mut d = [42; 128];
1591
1592 let frame = Frame::NewToken {
1593 token: Vec::from("this is a token"),
1594 };
1595
1596 let wire_len = {
1597 let mut b = octets::OctetsMut::with_slice(&mut d);
1598 frame.to_bytes(&mut b).unwrap()
1599 };
1600
1601 assert_eq!(wire_len, 17);
1602
1603 let mut b = octets::Octets::with_slice(&d);
1604 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1605
1606 let mut b = octets::Octets::with_slice(&d);
1607 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1608
1609 let mut b = octets::Octets::with_slice(&d);
1610 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1611
1612 let mut b = octets::Octets::with_slice(&d);
1613 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1614 }
1615
1616 #[test]
1617 fn stream() {
1618 let mut d = [42; 128];
1619
1620 let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1621
1622 let frame = Frame::Stream {
1623 stream_id: 32,
1624 data: stream::RangeBuf::from(&data, 1230976, true),
1625 };
1626
1627 let wire_len = {
1628 let mut b = octets::OctetsMut::with_slice(&mut d);
1629 frame.to_bytes(&mut b).unwrap()
1630 };
1631
1632 assert_eq!(wire_len, 20);
1633
1634 let mut b = octets::Octets::with_slice(&d);
1635 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1636
1637 let mut b = octets::Octets::with_slice(&d);
1638 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1639
1640 let mut b = octets::Octets::with_slice(&d);
1641 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1642
1643 let mut b = octets::Octets::with_slice(&d);
1644 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1645 }
1646
1647 #[test]
1648 fn stream_too_big() {
1649 let mut d = [42; 128];
1650
1651 let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1652
1653 let frame = Frame::Stream {
1654 stream_id: 32,
1655 data: stream::RangeBuf::from(&data, MAX_STREAM_SIZE - 11, true),
1656 };
1657
1658 let wire_len = {
1659 let mut b = octets::OctetsMut::with_slice(&mut d);
1660 frame.to_bytes(&mut b).unwrap()
1661 };
1662
1663 assert_eq!(wire_len, 24);
1664
1665 let mut b = octets::Octets::with_slice(&d);
1666 assert_eq!(
1667 Frame::from_bytes(&mut b, packet::Type::Short),
1668 Err(Error::InvalidFrame)
1669 );
1670 }
1671
1672 #[test]
1673 fn max_data() {
1674 let mut d = [42; 128];
1675
1676 let frame = Frame::MaxData { max: 128_318_273 };
1677
1678 let wire_len = {
1679 let mut b = octets::OctetsMut::with_slice(&mut d);
1680 frame.to_bytes(&mut b).unwrap()
1681 };
1682
1683 assert_eq!(wire_len, 5);
1684
1685 let mut b = octets::Octets::with_slice(&d);
1686 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1687
1688 let mut b = octets::Octets::with_slice(&d);
1689 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1690
1691 let mut b = octets::Octets::with_slice(&d);
1692 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1693
1694 let mut b = octets::Octets::with_slice(&d);
1695 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1696 }
1697
1698 #[test]
1699 fn max_stream_data() {
1700 let mut d = [42; 128];
1701
1702 let frame = Frame::MaxStreamData {
1703 stream_id: 12_321,
1704 max: 128_318_273,
1705 };
1706
1707 let wire_len = {
1708 let mut b = octets::OctetsMut::with_slice(&mut d);
1709 frame.to_bytes(&mut b).unwrap()
1710 };
1711
1712 assert_eq!(wire_len, 7);
1713
1714 let mut b = octets::Octets::with_slice(&d);
1715 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1716
1717 let mut b = octets::Octets::with_slice(&d);
1718 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1719
1720 let mut b = octets::Octets::with_slice(&d);
1721 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1722
1723 let mut b = octets::Octets::with_slice(&d);
1724 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1725 }
1726
1727 #[test]
1728 fn max_streams_bidi() {
1729 let mut d = [42; 128];
1730
1731 let frame = Frame::MaxStreamsBidi { max: 128_318_273 };
1732
1733 let wire_len = {
1734 let mut b = octets::OctetsMut::with_slice(&mut d);
1735 frame.to_bytes(&mut b).unwrap()
1736 };
1737
1738 assert_eq!(wire_len, 5);
1739
1740 let mut b = octets::Octets::with_slice(&d);
1741 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1742
1743 let mut b = octets::Octets::with_slice(&d);
1744 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1745
1746 let mut b = octets::Octets::with_slice(&d);
1747 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1748
1749 let mut b = octets::Octets::with_slice(&d);
1750 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1751 }
1752
1753 #[test]
1754 fn max_streams_uni() {
1755 let mut d = [42; 128];
1756
1757 let frame = Frame::MaxStreamsUni { max: 128_318_273 };
1758
1759 let wire_len = {
1760 let mut b = octets::OctetsMut::with_slice(&mut d);
1761 frame.to_bytes(&mut b).unwrap()
1762 };
1763
1764 assert_eq!(wire_len, 5);
1765
1766 let mut b = octets::Octets::with_slice(&d);
1767 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1768
1769 let mut b = octets::Octets::with_slice(&d);
1770 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1771
1772 let mut b = octets::Octets::with_slice(&d);
1773 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1774
1775 let mut b = octets::Octets::with_slice(&d);
1776 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1777 }
1778
1779 #[test]
1780 fn data_blocked() {
1781 let mut d = [42; 128];
1782
1783 let frame = Frame::DataBlocked { limit: 128_318_273 };
1784
1785 let wire_len = {
1786 let mut b = octets::OctetsMut::with_slice(&mut d);
1787 frame.to_bytes(&mut b).unwrap()
1788 };
1789
1790 assert_eq!(wire_len, 5);
1791
1792 let mut b = octets::Octets::with_slice(&d);
1793 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1794
1795 let mut b = octets::Octets::with_slice(&d);
1796 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1797
1798 let mut b = octets::Octets::with_slice(&d);
1799 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1800
1801 let mut b = octets::Octets::with_slice(&d);
1802 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1803 }
1804
1805 #[test]
1806 fn stream_data_blocked() {
1807 let mut d = [42; 128];
1808
1809 let frame = Frame::StreamDataBlocked {
1810 stream_id: 12_321,
1811 limit: 128_318_273,
1812 };
1813
1814 let wire_len = {
1815 let mut b = octets::OctetsMut::with_slice(&mut d);
1816 frame.to_bytes(&mut b).unwrap()
1817 };
1818
1819 assert_eq!(wire_len, 7);
1820
1821 let mut b = octets::Octets::with_slice(&d);
1822 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1823
1824 let mut b = octets::Octets::with_slice(&d);
1825 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1826
1827 let mut b = octets::Octets::with_slice(&d);
1828 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1829
1830 let mut b = octets::Octets::with_slice(&d);
1831 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1832 }
1833
1834 #[test]
1835 fn streams_blocked_bidi() {
1836 let mut d = [42; 128];
1837
1838 let frame = Frame::StreamsBlockedBidi { limit: 128_318_273 };
1839
1840 let wire_len = {
1841 let mut b = octets::OctetsMut::with_slice(&mut d);
1842 frame.to_bytes(&mut b).unwrap()
1843 };
1844
1845 assert_eq!(wire_len, 5);
1846
1847 let mut b = octets::Octets::with_slice(&d);
1848 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1849
1850 let mut b = octets::Octets::with_slice(&d);
1851 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1852
1853 let mut b = octets::Octets::with_slice(&d);
1854 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1855
1856 let mut b = octets::Octets::with_slice(&d);
1857 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1858 }
1859
1860 #[test]
1861 fn streams_blocked_uni() {
1862 let mut d = [42; 128];
1863
1864 let frame = Frame::StreamsBlockedUni { limit: 128_318_273 };
1865
1866 let wire_len = {
1867 let mut b = octets::OctetsMut::with_slice(&mut d);
1868 frame.to_bytes(&mut b).unwrap()
1869 };
1870
1871 assert_eq!(wire_len, 5);
1872
1873 let mut b = octets::Octets::with_slice(&d);
1874 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1875
1876 let mut b = octets::Octets::with_slice(&d);
1877 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1878
1879 let mut b = octets::Octets::with_slice(&d);
1880 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1881
1882 let mut b = octets::Octets::with_slice(&d);
1883 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1884 }
1885
1886 #[test]
1887 fn new_connection_id() {
1888 let mut d = [42; 128];
1889
1890 let frame = Frame::NewConnectionId {
1891 seq_num: 123_213,
1892 retire_prior_to: 122_211,
1893 conn_id: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
1894 reset_token: [0x42; 16],
1895 };
1896
1897 let wire_len = {
1898 let mut b = octets::OctetsMut::with_slice(&mut d);
1899 frame.to_bytes(&mut b).unwrap()
1900 };
1901
1902 assert_eq!(wire_len, 41);
1903
1904 let mut b = octets::Octets::with_slice(&d);
1905 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1906
1907 let mut b = octets::Octets::with_slice(&d);
1908 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1909
1910 let mut b = octets::Octets::with_slice(&d);
1911 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1912
1913 let mut b = octets::Octets::with_slice(&d);
1914 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1915 }
1916
1917 #[test]
1918 fn retire_connection_id() {
1919 let mut d = [42; 128];
1920
1921 let frame = Frame::RetireConnectionId { seq_num: 123_213 };
1922
1923 let wire_len = {
1924 let mut b = octets::OctetsMut::with_slice(&mut d);
1925 frame.to_bytes(&mut b).unwrap()
1926 };
1927
1928 assert_eq!(wire_len, 5);
1929
1930 let mut b = octets::Octets::with_slice(&d);
1931 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1932
1933 let mut b = octets::Octets::with_slice(&d);
1934 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1935
1936 let mut b = octets::Octets::with_slice(&d);
1937 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1938
1939 let mut b = octets::Octets::with_slice(&d);
1940 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1941 }
1942
1943 #[test]
1944 fn path_challenge() {
1945 let mut d = [42; 128];
1946
1947 let frame = Frame::PathChallenge {
1948 data: [1, 2, 3, 4, 5, 6, 7, 8],
1949 };
1950
1951 let wire_len = {
1952 let mut b = octets::OctetsMut::with_slice(&mut d);
1953 frame.to_bytes(&mut b).unwrap()
1954 };
1955
1956 assert_eq!(wire_len, 9);
1957
1958 let mut b = octets::Octets::with_slice(&d);
1959 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1960
1961 let mut b = octets::Octets::with_slice(&d);
1962 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1963
1964 let mut b = octets::Octets::with_slice(&d);
1965 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1966
1967 let mut b = octets::Octets::with_slice(&d);
1968 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1969 }
1970
1971 #[test]
1972 fn path_response() {
1973 let mut d = [42; 128];
1974
1975 let frame = Frame::PathResponse {
1976 data: [1, 2, 3, 4, 5, 6, 7, 8],
1977 };
1978
1979 let wire_len = {
1980 let mut b = octets::OctetsMut::with_slice(&mut d);
1981 frame.to_bytes(&mut b).unwrap()
1982 };
1983
1984 assert_eq!(wire_len, 9);
1985
1986 let mut b = octets::Octets::with_slice(&d);
1987 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1988
1989 let mut b = octets::Octets::with_slice(&d);
1990 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1991
1992 let mut b = octets::Octets::with_slice(&d);
1993 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1994
1995 let mut b = octets::Octets::with_slice(&d);
1996 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1997 }
1998
1999 #[test]
2000 fn connection_close() {
2001 let mut d = [42; 128];
2002
2003 let frame = Frame::ConnectionClose {
2004 error_code: 0xbeef,
2005 frame_type: 523_423,
2006 reason: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
2007 };
2008
2009 let wire_len = {
2010 let mut b = octets::OctetsMut::with_slice(&mut d);
2011 frame.to_bytes(&mut b).unwrap()
2012 };
2013
2014 assert_eq!(wire_len, 22);
2015
2016 let mut b = octets::Octets::with_slice(&d);
2017 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
2018
2019 let mut b = octets::Octets::with_slice(&d);
2020 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
2021
2022 let mut b = octets::Octets::with_slice(&d);
2023 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
2024
2025 let mut b = octets::Octets::with_slice(&d);
2026 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
2027 }
2028
2029 #[test]
2030 fn application_close() {
2031 let mut d = [42; 128];
2032
2033 let frame = Frame::ApplicationClose {
2034 error_code: 0xbeef,
2035 reason: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
2036 };
2037
2038 let wire_len = {
2039 let mut b = octets::OctetsMut::with_slice(&mut d);
2040 frame.to_bytes(&mut b).unwrap()
2041 };
2042
2043 assert_eq!(wire_len, 18);
2044
2045 let mut b = octets::Octets::with_slice(&d);
2046 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
2047
2048 let mut b = octets::Octets::with_slice(&d);
2049 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
2050
2051 let mut b = octets::Octets::with_slice(&d);
2052 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
2053
2054 let mut b = octets::Octets::with_slice(&d);
2055 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
2056 }
2057
2058 #[test]
2059 fn handshake_done() {
2060 let mut d = [42; 128];
2061
2062 let frame = Frame::HandshakeDone;
2063
2064 let wire_len = {
2065 let mut b = octets::OctetsMut::with_slice(&mut d);
2066 frame.to_bytes(&mut b).unwrap()
2067 };
2068
2069 assert_eq!(wire_len, 1);
2070
2071 let mut b = octets::Octets::with_slice(&d);
2072 assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
2073
2074 let mut b = octets::Octets::with_slice(&d);
2075 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
2076
2077 let mut b = octets::Octets::with_slice(&d);
2078 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
2079
2080 let mut b = octets::Octets::with_slice(&d);
2081 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
2082 }
2083
2084 #[test]
2085 fn datagram() {
2086 let mut d = [42; 128];
2087
2088 let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
2089
2090 let frame = Frame::Datagram { data: data.clone() };
2091
2092 let wire_len = {
2093 let mut b = octets::OctetsMut::with_slice(&mut d);
2094 frame.to_bytes(&mut b).unwrap()
2095 };
2096
2097 assert_eq!(wire_len, 15);
2098
2099 let mut b = octets::Octets::with_slice(&d);
2100 assert_eq!(
2101 Frame::from_bytes(&mut b, packet::Type::Short),
2102 Ok(frame.clone())
2103 );
2104
2105 let mut b = octets::Octets::with_slice(&d);
2106 assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
2107
2108 let mut b = octets::Octets::with_slice(&d);
2109 assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
2110
2111 let mut b = octets::Octets::with_slice(&d);
2112 assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
2113
2114 let frame_data = match &frame {
2115 Frame::Datagram { data } => data.clone(),
2116
2117 _ => unreachable!(),
2118 };
2119
2120 assert_eq!(frame_data, data);
2121 }
2122}