Use hashed connection IDs by default

This commit is contained in:
Benjamin Saunders
2024-03-30 11:42:25 -07:00
parent abdff8061e
commit bbf68c51ab
2 changed files with 20 additions and 14 deletions
+3 -5
View File
@@ -6,7 +6,7 @@ use thiserror::Error;
use rand::RngCore;
use crate::{
cid_generator::{ConnectionIdGenerator, RandomConnectionIdGenerator},
cid_generator::{ConnectionIdGenerator, HashedConnectionIdGenerator},
congestion,
crypto::{self, HandshakeTokenKey, HmacKey},
VarInt, VarIntBoundsExceeded, DEFAULT_SUPPORTED_VERSIONS, INITIAL_MTU, MAX_UDP_PAYLOAD,
@@ -620,7 +620,7 @@ impl EndpointConfig {
/// Create a default config with a particular `reset_key`
pub fn new(reset_key: Arc<dyn HmacKey>) -> Self {
let cid_factory: fn() -> Box<dyn ConnectionIdGenerator> =
|| Box::<RandomConnectionIdGenerator>::default();
|| Box::<HashedConnectionIdGenerator>::default();
Self {
reset_key,
max_udp_payload_size: (1500u32 - 28).into(), // Ethernet MTU minus IP + UDP headers
@@ -638,9 +638,7 @@ impl EndpointConfig {
/// connections involving that `Endpoint`. A custom CID generator allows applications to embed
/// information in local connection IDs, e.g. to support stateless packet-level load balancers.
///
/// `EndpointConfig::new()` applies a default random CID generator factory. This functions
/// accepts any customized CID generator to reset CID generator factory that implements
/// the `ConnectionIdGenerator` trait.
/// Defaults to [`HashedConnectionIdGenerator`].
pub fn cid_generator<F: Fn() -> Box<dyn ConnectionIdGenerator> + Send + Sync + 'static>(
&mut self,
factory: F,
+17 -9
View File
@@ -173,12 +173,15 @@ fn stateless_retry() {
#[test]
fn server_stateless_reset() {
let _guard = subscribe();
let mut reset_key = vec![0; 64];
let mut key_material = vec![0; 64];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut reset_key);
let reset_key = hmac::Key::new(hmac::HMAC_SHA256, &reset_key);
rng.fill_bytes(&mut key_material);
let reset_key = hmac::Key::new(hmac::HMAC_SHA256, &key_material);
rng.fill_bytes(&mut key_material);
let endpoint_config = Arc::new(EndpointConfig::new(Arc::new(reset_key)));
let mut endpoint_config = EndpointConfig::new(Arc::new(reset_key));
endpoint_config.cid_generator(move || Box::new(HashedConnectionIdGenerator::from_key(0)));
let endpoint_config = Arc::new(endpoint_config);
let mut pair = Pair::new(endpoint_config.clone(), server_config());
let (client_ch, _) = pair.connect();
@@ -200,12 +203,15 @@ fn server_stateless_reset() {
#[test]
fn client_stateless_reset() {
let _guard = subscribe();
let mut reset_key = vec![0; 64];
let mut key_material = vec![0; 64];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut reset_key);
let reset_key = hmac::Key::new(hmac::HMAC_SHA256, &reset_key);
rng.fill_bytes(&mut key_material);
let reset_key = hmac::Key::new(hmac::HMAC_SHA256, &key_material);
rng.fill_bytes(&mut key_material);
let endpoint_config = Arc::new(EndpointConfig::new(Arc::new(reset_key)));
let mut endpoint_config = EndpointConfig::new(Arc::new(reset_key));
endpoint_config.cid_generator(move || Box::new(HashedConnectionIdGenerator::from_key(0)));
let endpoint_config = Arc::new(endpoint_config);
let mut pair = Pair::new(endpoint_config.clone(), server_config());
let (_, server_ch) = pair.connect();
@@ -232,7 +238,9 @@ fn client_stateless_reset() {
fn stateless_reset_limit() {
let _guard = subscribe();
let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 42);
let endpoint_config = Arc::new(EndpointConfig::default());
let mut endpoint_config = EndpointConfig::default();
endpoint_config.cid_generator(move || Box::new(RandomConnectionIdGenerator::new(8)));
let endpoint_config = Arc::new(endpoint_config);
let mut endpoint = Endpoint::new(
endpoint_config.clone(),
Some(Arc::new(server_config())),