-
-
Notifications
You must be signed in to change notification settings - Fork 400
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: Connection side enum #2084
Open
gretchenfrage
wants to merge
2
commits into
quinn-rs:main
Choose a base branch
from
gretchenfrage:connection-side-enum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−49
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,6 @@ use timer::{Timer, TimerTable}; | |
/// events or timeouts with different instants must not be interleaved. | ||
pub struct Connection { | ||
endpoint_config: Arc<EndpointConfig>, | ||
server_config: Option<Arc<ServerConfig>>, | ||
config: Arc<TransportConfig>, | ||
rng: StdRng, | ||
crypto: Box<dyn crypto::Session>, | ||
|
@@ -145,7 +144,7 @@ pub struct Connection { | |
allow_mtud: bool, | ||
prev_path: Option<(ConnectionId, PathData)>, | ||
state: State, | ||
side: Side, | ||
side: ConnectionSide, | ||
/// Whether or not 0-RTT was enabled during the handshake. Does not imply acceptance. | ||
zero_rtt_enabled: bool, | ||
/// Set if 0-RTT is supported, then cleared when no longer needed. | ||
|
@@ -191,9 +190,6 @@ pub struct Connection { | |
authentication_failures: u64, | ||
/// Why the connection was lost, if it has been | ||
error: Option<ConnectionError>, | ||
/// Sent in every outgoing Initial packet. Always empty for servers and after Initial keys are | ||
/// discarded. | ||
retry_token: Bytes, | ||
/// Identifies Data-space packet numbers to skip. Not used in earlier spaces. | ||
packet_number_filter: PacketNumberFilter, | ||
|
||
|
@@ -242,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>, | ||
|
@@ -256,13 +250,12 @@ impl Connection { | |
version: u32, | ||
allow_mtud: bool, | ||
rng_seed: [u8; 32], | ||
path_validated: bool, | ||
side_args: SideArgs, | ||
) -> Self { | ||
let side = if server_config.is_some() { | ||
Side::Server | ||
} else { | ||
Side::Client | ||
}; | ||
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)), | ||
..PacketSpace::new(now) | ||
|
@@ -275,7 +268,6 @@ impl Connection { | |
let mut rng = StdRng::from_seed(rng_seed); | ||
let mut this = Self { | ||
endpoint_config, | ||
server_config, | ||
crypto, | ||
handshake_cid: loc_cid, | ||
rem_handshake_cid: rem_cid, | ||
|
@@ -289,8 +281,8 @@ impl Connection { | |
allow_mtud, | ||
local_ip, | ||
prev_path: None, | ||
side, | ||
state, | ||
side: connection_side, | ||
zero_rtt_enabled: false, | ||
zero_rtt_crypto: None, | ||
key_phase: false, | ||
|
@@ -323,7 +315,6 @@ impl Connection { | |
timers: TimerTable::default(), | ||
authentication_failures: 0, | ||
error: None, | ||
retry_token: Bytes::new(), | ||
#[cfg(test)] | ||
packet_number_filter: match config.deterministic_packet_numbers { | ||
false => PacketNumberFilter::new(&mut rng), | ||
|
@@ -420,7 +411,7 @@ impl Connection { | |
/// Provide control over streams | ||
#[must_use] | ||
pub fn recv_stream(&mut self, id: StreamId) -> RecvStream<'_> { | ||
assert!(id.dir() == Dir::Bi || id.initiator() != self.side); | ||
assert!(id.dir() == Dir::Bi || id.initiator() != self.side.side()); | ||
RecvStream { | ||
id, | ||
state: &mut self.streams, | ||
|
@@ -431,7 +422,7 @@ impl Connection { | |
/// Provide control over streams | ||
#[must_use] | ||
pub fn send_stream(&mut self, id: StreamId) -> SendStream<'_> { | ||
assert!(id.dir() == Dir::Bi || id.initiator() == self.side); | ||
assert!(id.dir() == Dir::Bi || id.initiator() == self.side.side()); | ||
SendStream { | ||
id, | ||
state: &mut self.streams, | ||
|
@@ -1075,9 +1066,7 @@ impl Connection { | |
// If this packet could initiate a migration and we're a client or a server that | ||
// forbids migration, drop the datagram. This could be relaxed to heuristically | ||
// permit NAT-rebinding-like migration. | ||
if remote != self.path.remote | ||
&& self.server_config.as_ref().map_or(true, |x| !x.migration) | ||
{ | ||
if remote != self.path.remote && !self.side.remote_may_migrate() { | ||
trace!("discarding packet from unrecognized peer {}", remote); | ||
return; | ||
} | ||
|
@@ -1297,7 +1286,7 @@ impl Connection { | |
|
||
/// Look up whether we're the client or server of this Connection | ||
pub fn side(&self) -> Side { | ||
self.side | ||
self.side.side() | ||
} | ||
|
||
/// The latest socket address for this connection's peer | ||
|
@@ -2101,7 +2090,9 @@ impl Connection { | |
trace!("discarding {:?} keys", space_id); | ||
if space_id == SpaceId::Initial { | ||
// No longer needed | ||
self.retry_token = Bytes::new(); | ||
if let ConnectionSide::Client { token, .. } = &mut self.side { | ||
*token = Bytes::new(); | ||
} | ||
} | ||
let space = &mut self.spaces[space_id]; | ||
space.crypto = None; | ||
|
@@ -2398,7 +2389,7 @@ impl Connection { | |
|
||
self.discard_space(now, SpaceId::Initial); // Make sure we clean up after any retransmitted Initials | ||
self.spaces[SpaceId::Initial] = PacketSpace { | ||
crypto: Some(self.crypto.initial_keys(&rem_cid, self.side)), | ||
crypto: Some(self.crypto.initial_keys(&rem_cid, self.side.side())), | ||
next_packet_number: self.spaces[SpaceId::Initial].next_packet_number, | ||
crypto_offset: client_hello.len() as u64, | ||
..PacketSpace::new(now) | ||
|
@@ -2420,7 +2411,10 @@ impl Connection { | |
self.streams.retransmit_all_for_0rtt(); | ||
|
||
let token_len = packet.payload.len() - 16; | ||
self.retry_token = packet.payload.freeze().split_to(token_len); | ||
let ConnectionSide::Client { ref mut token, .. } = self.side else { | ||
unreachable!("we already short-circuited if we're server"); | ||
}; | ||
*token = packet.payload.freeze().split_to(token_len); | ||
self.state = State::Handshake(state::Handshake { | ||
expected_token: Bytes::new(), | ||
rem_cid_set: false, | ||
|
@@ -2745,7 +2739,7 @@ impl Connection { | |
debug!(offset, "peer claims to be blocked at connection level"); | ||
} | ||
Frame::StreamDataBlocked { id, offset } => { | ||
if id.initiator() == self.side && id.dir() == Dir::Uni { | ||
if id.initiator() == self.side.side() && id.dir() == Dir::Uni { | ||
debug!("got STREAM_DATA_BLOCKED on send-only {}", id); | ||
return Err(TransportError::STREAM_STATE_ERROR( | ||
"STREAM_DATA_BLOCKED on send-only stream", | ||
|
@@ -2768,7 +2762,7 @@ impl Connection { | |
); | ||
} | ||
Frame::StopSending(frame::StopSending { id, error_code }) => { | ||
if id.initiator() != self.side { | ||
if id.initiator() != self.side.side() { | ||
if id.dir() == Dir::Uni { | ||
debug!("got STOP_SENDING on recv-only {}", id); | ||
return Err(TransportError::STREAM_STATE_ERROR( | ||
|
@@ -2938,11 +2932,11 @@ impl Connection { | |
&& !is_probing_packet | ||
&& number == self.spaces[SpaceId::Data].rx_packet | ||
{ | ||
let ConnectionSide::Server { ref server_config } = self.side else { | ||
panic!("packets from unknown remote should be dropped by clients"); | ||
}; | ||
debug_assert!( | ||
self.server_config | ||
.as_ref() | ||
.expect("packets from unknown remote should be dropped by clients") | ||
.migration, | ||
server_config.migration, | ||
"migration-initiating packets should have been dropped immediately" | ||
); | ||
self.migrate(now, remote); | ||
|
@@ -3618,6 +3612,89 @@ impl fmt::Debug for Connection { | |
} | ||
} | ||
|
||
/// Fields of `Connection` specific to it being client-side or server-side | ||
enum ConnectionSide { | ||
Client { | ||
/// Sent in every outgoing Initial packet. Always empty after Initial keys are discarded | ||
token: Bytes, | ||
}, | ||
Server { | ||
server_config: Arc<ServerConfig>, | ||
}, | ||
} | ||
|
||
impl ConnectionSide { | ||
fn side(&self) -> Side { | ||
match *self { | ||
Self::Client { .. } => Side::Client, | ||
Self::Server { .. } => Side::Server, | ||
} | ||
} | ||
|
||
fn is_client(&self) -> bool { | ||
self.side().is_client() | ||
} | ||
|
||
fn is_server(&self) -> bool { | ||
self.side().is_server() | ||
} | ||
|
||
fn remote_may_migrate(&self) -> bool { | ||
match self { | ||
Self::Server { server_config } => server_config.migration, | ||
Self::Client { .. } => false, | ||
} | ||
} | ||
} | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ordering nit: let's order this last. |
||
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ordering nit: let's have
remote_may_migrate()
first, then orderis_client()
andis_server()
beforeside()
(since they depend on it).