Skip to content

Commit

Permalink
quinn-rs#2057: Option to disable grease random transport parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mstyura committed Nov 20, 2024
1 parent f5fa2de commit abeff45
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
21 changes: 21 additions & 0 deletions quinn-proto/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub struct TransportConfig {
pub(crate) congestion_controller_factory: Arc<dyn congestion::ControllerFactory + Send + Sync>,

pub(crate) enable_segmentation_offload: bool,

pub(crate) enable_grease_random_transport_parameter: bool,
}

impl TransportConfig {
Expand Down Expand Up @@ -334,6 +336,18 @@ impl TransportConfig {
self.enable_segmentation_offload = enabled;
self
}

/// Specify if grease random transport parameter need to be included into transport parameters
/// during connection negotiation
///
/// Default to `true`
///
/// Will cause quinn to include single grease transport parameter
/// with ID in form of 37 * N + 21 with random payload.
pub fn enable_grease_random_transport_parameter(&mut self, enabled: bool) -> &mut Self {
self.enable_grease_random_transport_parameter = enabled;
self
}
}

impl Default for TransportConfig {
Expand Down Expand Up @@ -374,6 +388,8 @@ impl Default for TransportConfig {
congestion_controller_factory: Arc::new(congestion::CubicConfig::default()),

enable_segmentation_offload: true,

enable_grease_random_transport_parameter: true,
}
}
}
Expand Down Expand Up @@ -405,6 +421,7 @@ impl fmt::Debug for TransportConfig {
deterministic_packet_numbers: _,
congestion_controller_factory: _,
enable_segmentation_offload,
enable_grease_random_transport_parameter,
} = self;
fmt.debug_struct("TransportConfig")
.field("max_concurrent_bidi_streams", max_concurrent_bidi_streams)
Expand Down Expand Up @@ -432,6 +449,10 @@ impl fmt::Debug for TransportConfig {
.field("datagram_send_buffer_size", datagram_send_buffer_size)
.field("congestion_controller_factory", &"[ opaque ]")
.field("enable_segmentation_offload", enable_segmentation_offload)
.field(
"enable_grease_random_transport_parameter",
enable_grease_random_transport_parameter,
)
.finish()
}
}
Expand Down
20 changes: 17 additions & 3 deletions quinn-proto/src/transport_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ macro_rules! make_struct {
pub(crate) stateless_reset_token: Option<ResetToken>,
/// The server's preferred address for communication after handshake completion
pub(crate) preferred_address: Option<PreferredAddress>,
/// Indicator if single grease transport parameter will be sent over wire in case of
/// client or any number of grease transport parameters received in case of server.
pub(crate) grease_parameter_present: bool,
}

// We deliberately don't implement the `Default` trait, since that would be public, and
Expand All @@ -121,6 +124,7 @@ macro_rules! make_struct {
retry_src_cid: None,
stateless_reset_token: None,
preferred_address: None,
grease_parameter_present: false,
}
}
}
Expand Down Expand Up @@ -161,6 +165,7 @@ impl TransportParameters {
min_ack_delay: Some(
VarInt::from_u64(u64::try_from(TIMER_GRANULARITY.as_micros()).unwrap()).unwrap(),
),
grease_parameter_present: config.enable_grease_random_transport_parameter,
..Self::default()
}
}
Expand Down Expand Up @@ -301,7 +306,9 @@ impl TransportParameters {
}
apply_params!(write_params);

write_random_grease_reserved_parameter(w, &mut rand::thread_rng());
if self.grease_parameter_present {
write_random_grease_reserved_parameter(w, &mut rand::thread_rng());
}

if let Some(ref x) = self.stateless_reset_token {
w.write_var(0x02);
Expand Down Expand Up @@ -422,7 +429,12 @@ impl TransportParameters {
params.$name = value.into();
got.$name = true;
})*
_ => r.advance(len as usize),
id => {
if id % 31 == 27 {
params.grease_parameter_present = true;
}
r.advance(len as usize)
},
}
}
}
Expand Down Expand Up @@ -564,9 +576,11 @@ mod test {
for rng in &mut rngs {
let mut buf = Vec::new();
write_random_grease_reserved_parameter(&mut buf, rng);
let mut expected_params = TransportParameters::default();
expected_params.grease_parameter_present = true;
assert_eq!(
TransportParameters::read(Side::Client, &mut buf.as_slice()).unwrap(),
TransportParameters::default(),
expected_params,
);
}
}
Expand Down

0 comments on commit abeff45

Please sign in to comment.