Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto: make initial destination cid configurable #1897

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions quinn-proto/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::{
cid_generator::{ConnectionIdGenerator, HashedConnectionIdGenerator},
congestion,
crypto::{self, HandshakeTokenKey, HmacKey},
VarInt, VarIntBoundsExceeded, DEFAULT_SUPPORTED_VERSIONS, INITIAL_MTU, MAX_UDP_PAYLOAD,
shared::ConnectionId,
RandomConnectionIdGenerator, VarInt, VarIntBoundsExceeded, DEFAULT_SUPPORTED_VERSIONS,
INITIAL_MTU, MAX_CID_SIZE, MAX_UDP_PAYLOAD,
};

/// Parameters governing the core QUIC state machine
Expand Down Expand Up @@ -963,6 +965,9 @@ pub struct ClientConfig {
/// Cryptographic configuration to use
pub(crate) crypto: Arc<dyn crypto::ClientConfig>,

/// Provider that populates the destination connection ID of Initial Packets
pub(crate) initial_dst_cid_provider: Arc<dyn Fn() -> ConnectionId + Send + Sync>,

/// QUIC protocol version to use
pub(crate) version: u32,
}
Expand All @@ -973,10 +978,29 @@ impl ClientConfig {
Self {
transport: Default::default(),
crypto,
initial_dst_cid_provider: Arc::new(|| {
RandomConnectionIdGenerator::new(MAX_CID_SIZE).generate_cid()
}),
version: 1,
}
}

/// Configure how to populate the destination CID of the initial packet when attempting to
/// establish a new connection.
///
/// By default, it's populated with random bytes with reasonable length, so unless you have
/// a good reason, you do not need to change it.
///
/// When prefer to override the default, please note that the generated connection ID MUST be
/// at least 8 bytes long and unpredictable, as per section 7.2 of RFC 9000.
pub fn initial_dst_cid_provider(
&mut self,
initial_dst_cid_provider: Arc<dyn Fn() -> ConnectionId + Send + Sync>,
) -> &mut Self {
self.initial_dst_cid_provider = initial_dst_cid_provider;
self
}

/// Set a custom [`TransportConfig`]
pub fn transport_config(&mut self, transport: Arc<TransportConfig>) -> &mut Self {
self.transport = transport;
Expand Down Expand Up @@ -1016,7 +1040,7 @@ impl fmt::Debug for ClientConfig {
.field("transport", &self.transport)
.field("crypto", &"ClientConfig { elided }")
.field("version", &self.version)
.finish()
.finish_non_exhaustive()
}
}

Expand Down
4 changes: 2 additions & 2 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use thiserror::Error;
use tracing::{debug, error, trace, warn};

use crate::{
cid_generator::{ConnectionIdGenerator, RandomConnectionIdGenerator},
cid_generator::ConnectionIdGenerator,
coding::BufMutExt,
config::{ClientConfig, EndpointConfig, ServerConfig},
connection::{Connection, ConnectionError},
Expand Down Expand Up @@ -395,7 +395,7 @@ impl Endpoint {
return Err(ConnectError::UnsupportedVersion);
}

let remote_id = RandomConnectionIdGenerator::new(MAX_CID_SIZE).generate_cid();
let remote_id = (config.initial_dst_cid_provider)();
trace!(initial_dcid = %remote_id);

let ch = ConnectionHandle(self.connections.vacant_key());
Expand Down