Skip to content

Commit

Permalink
proto: Make Connection externally use SideArgs
Browse files Browse the repository at this point in the history
Server/client-specific args to proto::Connection::new are now passed
through a new SideArgs enum.
  • Loading branch information
gretchenfrage committed Dec 15, 2024
1 parent ed22361 commit 2d21680
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 29 deletions.
64 changes: 52 additions & 12 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,10 @@ pub struct Connection {
impl Connection {
pub(crate) fn new(
endpoint_config: Arc<EndpointConfig>,
server_config: Option<Arc<ServerConfig>>,
config: Arc<TransportConfig>,
init_cid: ConnectionId,
loc_cid: ConnectionId,
rem_cid: ConnectionId,
pref_addr_cid: Option<ConnectionId>,
remote: SocketAddr,
local_ip: Option<IpAddr>,
crypto: Box<dyn crypto::Session>,
Expand All @@ -252,17 +250,11 @@ impl Connection {
version: u32,
allow_mtud: bool,
rng_seed: [u8; 32],
path_validated: bool,
side_args: SideArgs,
) -> Self {
let connection_side = if let Some(server_config) = server_config.clone() {
ConnectionSide::Server { server_config }
} else {
assert!(pref_addr_cid.is_none());
assert!(path_validated);
ConnectionSide::Client {
token: Bytes::new(),
}
};
let pref_addr_cid = side_args.pref_addr_cid();
let path_validated = side_args.path_validated();
let connection_side = ConnectionSide::from(side_args);
let side = connection_side.side();
let initial_space = PacketSpace {
crypto: Some(crypto.initial_keys(&init_cid, side)),
Expand Down Expand Up @@ -3655,6 +3647,54 @@ impl ConnectionSide {
}
}

impl From<SideArgs> for ConnectionSide {
fn from(side: SideArgs) -> Self {
match side {
SideArgs::Client => Self::Client {
token: Bytes::new(),
},
SideArgs::Server {
server_config,
pref_addr_cid: _,
path_validated: _,
} => Self::Server { server_config },
}
}
}

/// Parameters to `Connection::new` specific to it being client-side or server-side
pub(crate) enum SideArgs {
Client,
Server {
server_config: Arc<ServerConfig>,
pref_addr_cid: Option<ConnectionId>,
path_validated: bool,
},
}

impl SideArgs {
pub(crate) fn side(&self) -> Side {
match *self {
Self::Client { .. } => Side::Client,
Self::Server { .. } => Side::Server,
}
}

pub(crate) fn pref_addr_cid(&self) -> Option<ConnectionId> {
match *self {
Self::Client { .. } => None,
Self::Server { pref_addr_cid, .. } => pref_addr_cid,
}
}

pub(crate) fn path_validated(&self) -> bool {
match *self {
Self::Client { .. } => true,
Self::Server { path_validated, .. } => path_validated,
}
}
}

/// Reasons why a connection might be lost
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ConnectionError {
Expand Down
28 changes: 11 additions & 17 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
cid_generator::ConnectionIdGenerator,
coding::BufMutExt,
config::{ClientConfig, EndpointConfig, ServerConfig},
connection::{Connection, ConnectionError},
connection::{Connection, ConnectionError, SideArgs},
crypto::{self, Keys, UnsupportedVersion},
frame,
packet::{
Expand Down Expand Up @@ -423,16 +423,14 @@ impl Endpoint {
remote_id,
loc_cid,
remote_id,
None,
FourTuple {
remote,
local_ip: None,
},
now,
tls,
None,
config.transport,
true,
SideArgs::Client,
);
Ok((ch, conn))
}
Expand Down Expand Up @@ -660,13 +658,15 @@ impl Endpoint {
dst_cid,
loc_cid,
src_cid,
pref_addr_cid,
incoming.addresses,
incoming.received_at,
tls,
Some(server_config),
transport_config,
remote_address_validated,
SideArgs::Server {
server_config,
pref_addr_cid,
path_validated: remote_address_validated,
},
);
self.index.insert_initial(dst_cid, ch);

Expand Down Expand Up @@ -829,28 +829,22 @@ impl Endpoint {
init_cid: ConnectionId,
loc_cid: ConnectionId,
rem_cid: ConnectionId,
pref_addr_cid: Option<ConnectionId>,
addresses: FourTuple,
now: Instant,
tls: Box<dyn crypto::Session>,
server_config: Option<Arc<ServerConfig>>,
transport_config: Arc<TransportConfig>,
path_validated: bool,
side_args: SideArgs,
) -> Connection {
let mut rng_seed = [0; 32];
self.rng.fill_bytes(&mut rng_seed);
let side = match server_config.is_some() {
true => Side::Server,
false => Side::Client,
};
let side = side_args.side();
let pref_addr_cid = side_args.pref_addr_cid();
let conn = Connection::new(
self.config.clone(),
server_config,
transport_config,
init_cid,
loc_cid,
rem_cid,
pref_addr_cid,
addresses.remote,
addresses.local_ip,
tls,
Expand All @@ -859,7 +853,7 @@ impl Endpoint {
version,
self.allow_mtud,
rng_seed,
path_validated,
side_args,
);

let mut cids_issued = 0;
Expand Down

0 comments on commit 2d21680

Please sign in to comment.