1use std::{
10 convert::TryFrom,
11 net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6},
12};
13
14use bytes::{Buf, BufMut};
15use rand::{Rng as _, RngCore, seq::SliceRandom as _};
16use thiserror::Error;
17
18use crate::{
19 LOC_CID_COUNT, MAX_CID_SIZE, MAX_STREAM_COUNT, RESET_TOKEN_SIZE, ResetToken, Side,
20 TIMER_GRANULARITY, TransportError, VarInt,
21 cid_generator::ConnectionIdGenerator,
22 cid_queue::CidQueue,
23 coding::{BufExt, BufMutExt, UnexpectedEnd},
24 config::{EndpointConfig, ServerConfig, TransportConfig},
25 shared::ConnectionId,
26};
27
28macro_rules! apply_params {
34 ($macro:ident) => {
35 $macro! {
36 max_idle_timeout(MaxIdleTimeout) = 0,
39 max_udp_payload_size(MaxUdpPayloadSize) = 65527,
41
42 initial_max_data(InitialMaxData) = 0,
44 initial_max_stream_data_bidi_local(InitialMaxStreamDataBidiLocal) = 0,
46 initial_max_stream_data_bidi_remote(InitialMaxStreamDataBidiRemote) = 0,
48 initial_max_stream_data_uni(InitialMaxStreamDataUni) = 0,
50
51 initial_max_streams_bidi(InitialMaxStreamsBidi) = 0,
53 initial_max_streams_uni(InitialMaxStreamsUni) = 0,
55
56 ack_delay_exponent(AckDelayExponent) = 3,
58 max_ack_delay(MaxAckDelay) = 25,
61 active_connection_id_limit(ActiveConnectionIdLimit) = 2,
63 }
64 };
65}
66
67macro_rules! make_struct {
68 {$($(#[$doc:meta])* $name:ident ($id:ident) = $default:expr,)*} => {
69 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
71 pub struct TransportParameters {
72 $($(#[$doc])* pub(crate) $name : VarInt,)*
73
74 pub(crate) disable_active_migration: bool,
76 pub(crate) max_datagram_frame_size: Option<VarInt>,
78 pub(crate) initial_src_cid: Option<ConnectionId>,
81 pub(crate) grease_quic_bit: bool,
84
85 pub(crate) min_ack_delay: Option<VarInt>,
91
92 pub(crate) original_dst_cid: Option<ConnectionId>,
96 pub(crate) retry_src_cid: Option<ConnectionId>,
99 pub(crate) stateless_reset_token: Option<ResetToken>,
101 pub(crate) preferred_address: Option<PreferredAddress>,
103 pub(crate) grease_transport_parameter: Option<ReservedTransportParameter>,
107
108 pub(crate) write_order: Option<[u8; TransportParameterId::SUPPORTED.len()]>,
113 }
114
115 impl TransportParameters {
119 pub(crate) fn default() -> Self {
121 Self {
122 $($name: VarInt::from_u32($default),)*
123
124 disable_active_migration: false,
125 max_datagram_frame_size: None,
126 initial_src_cid: None,
127 grease_quic_bit: false,
128 min_ack_delay: None,
129
130 original_dst_cid: None,
131 retry_src_cid: None,
132 stateless_reset_token: None,
133 preferred_address: None,
134 grease_transport_parameter: None,
135 write_order: None,
136 }
137 }
138 }
139 }
140}
141
142apply_params!(make_struct);
143
144impl TransportParameters {
145 pub(crate) fn new(
146 config: &TransportConfig,
147 endpoint_config: &EndpointConfig,
148 cid_gen: &dyn ConnectionIdGenerator,
149 initial_src_cid: ConnectionId,
150 server_config: Option<&ServerConfig>,
151 rng: &mut impl RngCore,
152 ) -> Self {
153 Self {
154 initial_src_cid: Some(initial_src_cid),
155 initial_max_streams_bidi: config.max_concurrent_bidi_streams,
156 initial_max_streams_uni: config.max_concurrent_uni_streams,
157 initial_max_data: config.receive_window,
158 initial_max_stream_data_bidi_local: config.stream_receive_window,
159 initial_max_stream_data_bidi_remote: config.stream_receive_window,
160 initial_max_stream_data_uni: config.stream_receive_window,
161 max_udp_payload_size: endpoint_config.max_udp_payload_size,
162 max_idle_timeout: config.max_idle_timeout.unwrap_or(VarInt(0)),
163 disable_active_migration: server_config.is_some_and(|c| !c.migration),
164 active_connection_id_limit: if cid_gen.cid_len() == 0 {
165 2 } else {
167 CidQueue::LEN as u32
168 }
169 .into(),
170 max_datagram_frame_size: config
171 .datagram_receive_buffer_size
172 .map(|x| (x.min(u16::MAX.into()) as u16).into()),
173 grease_quic_bit: endpoint_config.grease_quic_bit,
174 min_ack_delay: Some(
175 VarInt::from_u64(u64::try_from(TIMER_GRANULARITY.as_micros()).unwrap()).unwrap(),
176 ),
177 grease_transport_parameter: Some(ReservedTransportParameter::random(rng)),
178 write_order: Some({
179 let mut order = std::array::from_fn(|i| i as u8);
180 order.shuffle(rng);
181 order
182 }),
183 ..Self::default()
184 }
185 }
186
187 pub(crate) fn validate_resumption_from(&self, cached: &Self) -> Result<(), TransportError> {
190 if cached.active_connection_id_limit > self.active_connection_id_limit
191 || cached.initial_max_data > self.initial_max_data
192 || cached.initial_max_stream_data_bidi_local > self.initial_max_stream_data_bidi_local
193 || cached.initial_max_stream_data_bidi_remote > self.initial_max_stream_data_bidi_remote
194 || cached.initial_max_stream_data_uni > self.initial_max_stream_data_uni
195 || cached.initial_max_streams_bidi > self.initial_max_streams_bidi
196 || cached.initial_max_streams_uni > self.initial_max_streams_uni
197 || cached.max_datagram_frame_size > self.max_datagram_frame_size
198 || cached.grease_quic_bit && !self.grease_quic_bit
199 {
200 return Err(TransportError::PROTOCOL_VIOLATION(
201 "0-RTT accepted with incompatible transport parameters",
202 ));
203 }
204 Ok(())
205 }
206
207 pub(crate) fn issue_cids_limit(&self) -> u64 {
212 self.active_connection_id_limit.0.min(LOC_CID_COUNT)
213 }
214}
215
216#[derive(Debug, Copy, Clone, Eq, PartialEq)]
220pub(crate) struct PreferredAddress {
221 pub(crate) address_v4: Option<SocketAddrV4>,
222 pub(crate) address_v6: Option<SocketAddrV6>,
223 pub(crate) connection_id: ConnectionId,
224 pub(crate) stateless_reset_token: ResetToken,
225}
226
227impl PreferredAddress {
228 fn wire_size(&self) -> u16 {
229 4 + 2 + 16 + 2 + 1 + self.connection_id.len() as u16 + 16
230 }
231
232 fn write<W: BufMut>(&self, w: &mut W) {
233 w.write(self.address_v4.map_or(Ipv4Addr::UNSPECIFIED, |x| *x.ip()));
234 w.write::<u16>(self.address_v4.map_or(0, |x| x.port()));
235 w.write(self.address_v6.map_or(Ipv6Addr::UNSPECIFIED, |x| *x.ip()));
236 w.write::<u16>(self.address_v6.map_or(0, |x| x.port()));
237 w.write::<u8>(self.connection_id.len() as u8);
238 w.put_slice(&self.connection_id);
239 w.put_slice(&self.stateless_reset_token);
240 }
241
242 fn read<R: Buf>(r: &mut R) -> Result<Self, Error> {
243 let ip_v4 = r.get::<Ipv4Addr>()?;
244 let port_v4 = r.get::<u16>()?;
245 let ip_v6 = r.get::<Ipv6Addr>()?;
246 let port_v6 = r.get::<u16>()?;
247 let cid_len = r.get::<u8>()?;
248 if r.remaining() < cid_len as usize || cid_len > MAX_CID_SIZE as u8 {
249 return Err(Error::Malformed);
250 }
251 let mut stage = [0; MAX_CID_SIZE];
252 r.copy_to_slice(&mut stage[0..cid_len as usize]);
253 let cid = ConnectionId::new(&stage[0..cid_len as usize]);
254 if r.remaining() < 16 {
255 return Err(Error::Malformed);
256 }
257 let mut token = [0; RESET_TOKEN_SIZE];
258 r.copy_to_slice(&mut token);
259 let address_v4 = if ip_v4.is_unspecified() && port_v4 == 0 {
260 None
261 } else {
262 Some(SocketAddrV4::new(ip_v4, port_v4))
263 };
264 let address_v6 = if ip_v6.is_unspecified() && port_v6 == 0 {
265 None
266 } else {
267 Some(SocketAddrV6::new(ip_v6, port_v6, 0, 0))
268 };
269 if address_v4.is_none() && address_v6.is_none() {
270 return Err(Error::IllegalValue);
271 }
272 Ok(Self {
273 address_v4,
274 address_v6,
275 connection_id: cid,
276 stateless_reset_token: token.into(),
277 })
278 }
279}
280
281#[derive(Debug, Copy, Clone, Eq, PartialEq, Error)]
283pub enum Error {
284 #[error("parameter had illegal value")]
286 IllegalValue,
287 #[error("parameters were malformed")]
289 Malformed,
290}
291
292impl From<Error> for TransportError {
293 fn from(e: Error) -> Self {
294 match e {
295 Error::IllegalValue => Self::TRANSPORT_PARAMETER_ERROR("illegal value"),
296 Error::Malformed => Self::TRANSPORT_PARAMETER_ERROR("malformed"),
297 }
298 }
299}
300
301impl From<UnexpectedEnd> for Error {
302 fn from(_: UnexpectedEnd) -> Self {
303 Self::Malformed
304 }
305}
306
307impl TransportParameters {
308 pub fn write<W: BufMut>(&self, w: &mut W) {
310 for idx in self
311 .write_order
312 .as_ref()
313 .unwrap_or(&std::array::from_fn(|i| i as u8))
314 {
315 let id = TransportParameterId::SUPPORTED[*idx as usize];
316 match id {
317 TransportParameterId::ReservedTransportParameter => {
318 if let Some(param) = self.grease_transport_parameter {
319 param.write(w);
320 }
321 }
322 TransportParameterId::StatelessResetToken => {
323 if let Some(ref x) = self.stateless_reset_token {
324 w.write_var(id as u64);
325 w.write_var(16);
326 w.put_slice(x);
327 }
328 }
329 TransportParameterId::DisableActiveMigration => {
330 if self.disable_active_migration {
331 w.write_var(id as u64);
332 w.write_var(0);
333 }
334 }
335 TransportParameterId::MaxDatagramFrameSize => {
336 if let Some(x) = self.max_datagram_frame_size {
337 w.write_var(id as u64);
338 w.write_var(x.size() as u64);
339 w.write(x);
340 }
341 }
342 TransportParameterId::PreferredAddress => {
343 if let Some(ref x) = self.preferred_address {
344 w.write_var(id as u64);
345 w.write_var(x.wire_size() as u64);
346 x.write(w);
347 }
348 }
349 TransportParameterId::OriginalDestinationConnectionId => {
350 if let Some(ref cid) = self.original_dst_cid {
351 w.write_var(id as u64);
352 w.write_var(cid.len() as u64);
353 w.put_slice(cid);
354 }
355 }
356 TransportParameterId::InitialSourceConnectionId => {
357 if let Some(ref cid) = self.initial_src_cid {
358 w.write_var(id as u64);
359 w.write_var(cid.len() as u64);
360 w.put_slice(cid);
361 }
362 }
363 TransportParameterId::RetrySourceConnectionId => {
364 if let Some(ref cid) = self.retry_src_cid {
365 w.write_var(id as u64);
366 w.write_var(cid.len() as u64);
367 w.put_slice(cid);
368 }
369 }
370 TransportParameterId::GreaseQuicBit => {
371 if self.grease_quic_bit {
372 w.write_var(id as u64);
373 w.write_var(0);
374 }
375 }
376 TransportParameterId::MinAckDelayDraft07 => {
377 if let Some(x) = self.min_ack_delay {
378 w.write_var(id as u64);
379 w.write_var(x.size() as u64);
380 w.write(x);
381 }
382 }
383 id => {
384 macro_rules! write_params {
385 {$($(#[$doc:meta])* $name:ident ($id:ident) = $default:expr,)*} => {
386 match id {
387 $(TransportParameterId::$id => {
388 if self.$name.0 != $default {
389 w.write_var(id as u64);
390 w.write(VarInt::try_from(self.$name.size()).unwrap());
391 w.write(self.$name);
392 }
393 })*,
394 _ => {
395 unimplemented!("Missing implementation of write for transport parameter with code {id:?}");
396 }
397 }
398 }
399 }
400 apply_params!(write_params);
401 }
402 }
403 }
404 }
405
406 pub fn read<R: Buf>(side: Side, r: &mut R) -> Result<Self, Error> {
408 let mut params = Self::default();
410
411 macro_rules! param_state {
413 {$($(#[$doc:meta])* $name:ident ($id:ident) = $default:expr,)*} => {{
414 struct ParamState {
415 $($name: bool,)*
416 }
417
418 ParamState {
419 $($name: false,)*
420 }
421 }}
422 }
423 let mut got = apply_params!(param_state);
424
425 while r.has_remaining() {
426 let id = r.get_var()?;
427 let len = r.get_var()?;
428 if (r.remaining() as u64) < len {
429 return Err(Error::Malformed);
430 }
431 let len = len as usize;
432 let Ok(id) = TransportParameterId::try_from(id) else {
433 r.advance(len);
435 continue;
436 };
437
438 match id {
439 TransportParameterId::OriginalDestinationConnectionId => {
440 decode_cid(len, &mut params.original_dst_cid, r)?
441 }
442 TransportParameterId::StatelessResetToken => {
443 if len != 16 || params.stateless_reset_token.is_some() {
444 return Err(Error::Malformed);
445 }
446 let mut tok = [0; RESET_TOKEN_SIZE];
447 r.copy_to_slice(&mut tok);
448 params.stateless_reset_token = Some(tok.into());
449 }
450 TransportParameterId::DisableActiveMigration => {
451 if len != 0 || params.disable_active_migration {
452 return Err(Error::Malformed);
453 }
454 params.disable_active_migration = true;
455 }
456 TransportParameterId::PreferredAddress => {
457 if params.preferred_address.is_some() {
458 return Err(Error::Malformed);
459 }
460 params.preferred_address = Some(PreferredAddress::read(&mut r.take(len))?);
461 }
462 TransportParameterId::InitialSourceConnectionId => {
463 decode_cid(len, &mut params.initial_src_cid, r)?
464 }
465 TransportParameterId::RetrySourceConnectionId => {
466 decode_cid(len, &mut params.retry_src_cid, r)?
467 }
468 TransportParameterId::MaxDatagramFrameSize => {
469 if len > 8 || params.max_datagram_frame_size.is_some() {
470 return Err(Error::Malformed);
471 }
472 params.max_datagram_frame_size = Some(r.get().unwrap());
473 }
474 TransportParameterId::GreaseQuicBit => match len {
475 0 => params.grease_quic_bit = true,
476 _ => return Err(Error::Malformed),
477 },
478 TransportParameterId::MinAckDelayDraft07 => {
479 params.min_ack_delay = Some(r.get().unwrap())
480 }
481 _ => {
482 macro_rules! parse {
483 {$($(#[$doc:meta])* $name:ident ($id:ident) = $default:expr,)*} => {
484 match id {
485 $(TransportParameterId::$id => {
486 let value = r.get::<VarInt>()?;
487 if len != value.size() || got.$name { return Err(Error::Malformed); }
488 params.$name = value.into();
489 got.$name = true;
490 })*
491 _ => r.advance(len),
492 }
493 }
494 }
495 apply_params!(parse);
496 }
497 }
498 }
499
500 if params.ack_delay_exponent.0 > 20
504 || params.max_ack_delay.0 >= 1 << 14
506 || params.active_connection_id_limit.0 < 2
508 || params.max_udp_payload_size.0 < 1200
510 || params.initial_max_streams_bidi.0 > MAX_STREAM_COUNT
512 || params.initial_max_streams_uni.0 > MAX_STREAM_COUNT
513 || params.min_ack_delay.is_some_and(|min_ack_delay| {
515 min_ack_delay.0 > params.max_ack_delay.0 * 1_000
517 })
518 || (side.is_server()
520 && (params.original_dst_cid.is_some()
521 || params.preferred_address.is_some()
522 || params.retry_src_cid.is_some()
523 || params.stateless_reset_token.is_some()))
524 || params
526 .preferred_address.is_some_and(|x| x.connection_id.is_empty())
527 {
528 return Err(Error::IllegalValue);
529 }
530
531 Ok(params)
532 }
533}
534
535#[derive(Debug, Copy, Clone, Eq, PartialEq)]
544pub(crate) struct ReservedTransportParameter {
545 id: VarInt,
547
548 payload: [u8; Self::MAX_PAYLOAD_LEN],
550
551 payload_len: usize,
553}
554
555impl ReservedTransportParameter {
556 fn random(rng: &mut impl RngCore) -> Self {
562 let id = Self::generate_reserved_id(rng);
563
564 let payload_len = rng.random_range(0..Self::MAX_PAYLOAD_LEN);
565
566 let payload = {
567 let mut slice = [0u8; Self::MAX_PAYLOAD_LEN];
568 rng.fill_bytes(&mut slice[..payload_len]);
569 slice
570 };
571
572 Self {
573 id,
574 payload,
575 payload_len,
576 }
577 }
578
579 fn write(&self, w: &mut impl BufMut) {
580 w.write_var(self.id.0);
581 w.write_var(self.payload_len as u64);
582 w.put_slice(&self.payload[..self.payload_len]);
583 }
584
585 fn generate_reserved_id(rng: &mut impl RngCore) -> VarInt {
590 let id = {
591 let rand = rng.random_range(0u64..(1 << 62) - 27);
592 let n = rand / 31;
593 31 * n + 27
594 };
595 debug_assert!(
596 id % 31 == 27,
597 "generated id does not have the form of 31 * N + 27"
598 );
599 VarInt::from_u64(id).expect(
600 "generated id does fit into range of allowed transport parameter IDs: [0; 2^62)",
601 )
602 }
603
604 const MAX_PAYLOAD_LEN: usize = 16;
608}
609
610#[repr(u64)]
611#[derive(Debug, Clone, Copy, PartialEq, Eq)]
612pub(crate) enum TransportParameterId {
613 OriginalDestinationConnectionId = 0x00,
615 MaxIdleTimeout = 0x01,
616 StatelessResetToken = 0x02,
617 MaxUdpPayloadSize = 0x03,
618 InitialMaxData = 0x04,
619 InitialMaxStreamDataBidiLocal = 0x05,
620 InitialMaxStreamDataBidiRemote = 0x06,
621 InitialMaxStreamDataUni = 0x07,
622 InitialMaxStreamsBidi = 0x08,
623 InitialMaxStreamsUni = 0x09,
624 AckDelayExponent = 0x0A,
625 MaxAckDelay = 0x0B,
626 DisableActiveMigration = 0x0C,
627 PreferredAddress = 0x0D,
628 ActiveConnectionIdLimit = 0x0E,
629 InitialSourceConnectionId = 0x0F,
630 RetrySourceConnectionId = 0x10,
631
632 ReservedTransportParameter = 0x1B,
634
635 MaxDatagramFrameSize = 0x20,
637
638 GreaseQuicBit = 0x2AB2,
640
641 MinAckDelayDraft07 = 0xFF04DE1B,
643}
644
645impl TransportParameterId {
646 const SUPPORTED: [Self; 21] = [
648 Self::MaxIdleTimeout,
649 Self::MaxUdpPayloadSize,
650 Self::InitialMaxData,
651 Self::InitialMaxStreamDataBidiLocal,
652 Self::InitialMaxStreamDataBidiRemote,
653 Self::InitialMaxStreamDataUni,
654 Self::InitialMaxStreamsBidi,
655 Self::InitialMaxStreamsUni,
656 Self::AckDelayExponent,
657 Self::MaxAckDelay,
658 Self::ActiveConnectionIdLimit,
659 Self::ReservedTransportParameter,
660 Self::StatelessResetToken,
661 Self::DisableActiveMigration,
662 Self::MaxDatagramFrameSize,
663 Self::PreferredAddress,
664 Self::OriginalDestinationConnectionId,
665 Self::InitialSourceConnectionId,
666 Self::RetrySourceConnectionId,
667 Self::GreaseQuicBit,
668 Self::MinAckDelayDraft07,
669 ];
670}
671
672impl std::cmp::PartialEq<u64> for TransportParameterId {
673 fn eq(&self, other: &u64) -> bool {
674 *other == (*self as u64)
675 }
676}
677
678impl TryFrom<u64> for TransportParameterId {
679 type Error = ();
680
681 fn try_from(value: u64) -> Result<Self, Self::Error> {
682 let param = match value {
683 id if Self::MaxIdleTimeout == id => Self::MaxIdleTimeout,
684 id if Self::MaxUdpPayloadSize == id => Self::MaxUdpPayloadSize,
685 id if Self::InitialMaxData == id => Self::InitialMaxData,
686 id if Self::InitialMaxStreamDataBidiLocal == id => Self::InitialMaxStreamDataBidiLocal,
687 id if Self::InitialMaxStreamDataBidiRemote == id => {
688 Self::InitialMaxStreamDataBidiRemote
689 }
690 id if Self::InitialMaxStreamDataUni == id => Self::InitialMaxStreamDataUni,
691 id if Self::InitialMaxStreamsBidi == id => Self::InitialMaxStreamsBidi,
692 id if Self::InitialMaxStreamsUni == id => Self::InitialMaxStreamsUni,
693 id if Self::AckDelayExponent == id => Self::AckDelayExponent,
694 id if Self::MaxAckDelay == id => Self::MaxAckDelay,
695 id if Self::ActiveConnectionIdLimit == id => Self::ActiveConnectionIdLimit,
696 id if Self::ReservedTransportParameter == id => Self::ReservedTransportParameter,
697 id if Self::StatelessResetToken == id => Self::StatelessResetToken,
698 id if Self::DisableActiveMigration == id => Self::DisableActiveMigration,
699 id if Self::MaxDatagramFrameSize == id => Self::MaxDatagramFrameSize,
700 id if Self::PreferredAddress == id => Self::PreferredAddress,
701 id if Self::OriginalDestinationConnectionId == id => {
702 Self::OriginalDestinationConnectionId
703 }
704 id if Self::InitialSourceConnectionId == id => Self::InitialSourceConnectionId,
705 id if Self::RetrySourceConnectionId == id => Self::RetrySourceConnectionId,
706 id if Self::GreaseQuicBit == id => Self::GreaseQuicBit,
707 id if Self::MinAckDelayDraft07 == id => Self::MinAckDelayDraft07,
708 _ => return Err(()),
709 };
710 Ok(param)
711 }
712}
713
714fn decode_cid(len: usize, value: &mut Option<ConnectionId>, r: &mut impl Buf) -> Result<(), Error> {
715 if len > MAX_CID_SIZE || value.is_some() || r.remaining() < len {
716 return Err(Error::Malformed);
717 }
718
719 *value = Some(ConnectionId::from_buf(r, len));
720 Ok(())
721}
722
723#[cfg(test)]
724mod test {
725 use super::*;
726
727 #[test]
728 fn coding() {
729 let mut buf = Vec::new();
730 let params = TransportParameters {
731 initial_src_cid: Some(ConnectionId::new(&[])),
732 original_dst_cid: Some(ConnectionId::new(&[])),
733 initial_max_streams_bidi: 16u32.into(),
734 initial_max_streams_uni: 16u32.into(),
735 ack_delay_exponent: 2u32.into(),
736 max_udp_payload_size: 1200u32.into(),
737 preferred_address: Some(PreferredAddress {
738 address_v4: Some(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 42)),
739 address_v6: Some(SocketAddrV6::new(Ipv6Addr::LOCALHOST, 24, 0, 0)),
740 connection_id: ConnectionId::new(&[0x42]),
741 stateless_reset_token: [0xab; RESET_TOKEN_SIZE].into(),
742 }),
743 grease_quic_bit: true,
744 min_ack_delay: Some(2_000u32.into()),
745 ..TransportParameters::default()
746 };
747 params.write(&mut buf);
748 assert_eq!(
749 TransportParameters::read(Side::Client, &mut buf.as_slice()).unwrap(),
750 params
751 );
752 }
753
754 #[test]
755 fn reserved_transport_parameter_generate_reserved_id() {
756 use rand::rngs::mock::StepRng;
757 let mut rngs = [
758 StepRng::new(0, 1),
759 StepRng::new(1, 1),
760 StepRng::new(27, 1),
761 StepRng::new(31, 1),
762 StepRng::new(u32::MAX as u64, 1),
763 StepRng::new(u32::MAX as u64 - 1, 1),
764 StepRng::new(u32::MAX as u64 + 1, 1),
765 StepRng::new(u32::MAX as u64 - 27, 1),
766 StepRng::new(u32::MAX as u64 + 27, 1),
767 StepRng::new(u32::MAX as u64 - 31, 1),
768 StepRng::new(u32::MAX as u64 + 31, 1),
769 StepRng::new(u64::MAX, 1),
770 StepRng::new(u64::MAX - 1, 1),
771 StepRng::new(u64::MAX - 27, 1),
772 StepRng::new(u64::MAX - 31, 1),
773 StepRng::new(1 << 62, 1),
774 StepRng::new((1 << 62) - 1, 1),
775 StepRng::new((1 << 62) + 1, 1),
776 StepRng::new((1 << 62) - 27, 1),
777 StepRng::new((1 << 62) + 27, 1),
778 StepRng::new((1 << 62) - 31, 1),
779 StepRng::new((1 << 62) + 31, 1),
780 ];
781 for rng in &mut rngs {
782 let id = ReservedTransportParameter::generate_reserved_id(rng);
783 assert!(id.0 % 31 == 27)
784 }
785 }
786
787 #[test]
788 fn reserved_transport_parameter_ignored_when_read() {
789 let mut buf = Vec::new();
790 let reserved_parameter = ReservedTransportParameter::random(&mut rand::rng());
791 assert!(reserved_parameter.payload_len < ReservedTransportParameter::MAX_PAYLOAD_LEN);
792 assert!(reserved_parameter.id.0 % 31 == 27);
793
794 reserved_parameter.write(&mut buf);
795 assert!(!buf.is_empty());
796 let read_params = TransportParameters::read(Side::Server, &mut buf.as_slice()).unwrap();
797 assert_eq!(read_params, TransportParameters::default());
798 }
799
800 #[test]
801 fn read_semantic_validation() {
802 #[allow(clippy::type_complexity)]
803 let illegal_params_builders: Vec<Box<dyn FnMut(&mut TransportParameters)>> = vec![
804 Box::new(|t| {
805 let min_ack_delay = t.max_ack_delay.0 * 1_000 + 1;
807 t.min_ack_delay = Some(VarInt::from_u64(min_ack_delay).unwrap())
808 }),
809 Box::new(|t| {
810 t.preferred_address = Some(PreferredAddress {
813 address_v4: Some(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 42)),
814 address_v6: None,
815 connection_id: ConnectionId::new(&[]),
816 stateless_reset_token: [0xab; RESET_TOKEN_SIZE].into(),
817 })
818 }),
819 ];
820
821 for mut builder in illegal_params_builders {
822 let mut buf = Vec::new();
823 let mut params = TransportParameters::default();
824 builder(&mut params);
825 params.write(&mut buf);
826
827 assert_eq!(
828 TransportParameters::read(Side::Server, &mut buf.as_slice()),
829 Err(Error::IllegalValue)
830 );
831 }
832 }
833
834 #[test]
835 fn resumption_params_validation() {
836 let high_limit = TransportParameters {
837 initial_max_streams_uni: 32u32.into(),
838 ..TransportParameters::default()
839 };
840 let low_limit = TransportParameters {
841 initial_max_streams_uni: 16u32.into(),
842 ..TransportParameters::default()
843 };
844 high_limit.validate_resumption_from(&low_limit).unwrap();
845 low_limit.validate_resumption_from(&high_limit).unwrap_err();
846 }
847}