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: Pass SocketAddr by value #2107

Merged
merged 1 commit into from
Dec 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
4 changes: 2 additions & 2 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ impl Connection {
// Send an off-path PATH_RESPONSE. Prioritized over on-path data to ensure that path
// validation can occur while the link is saturated.
if space_id == SpaceId::Data && num_datagrams == 1 {
if let Some((token, remote)) = self.path_responses.pop_off_path(&self.path.remote) {
if let Some((token, remote)) = self.path_responses.pop_off_path(self.path.remote) {
// `unwrap` guaranteed to succeed because `builder_storage` was populated just
// above.
let mut builder = builder_storage.take().unwrap();
Expand Down Expand Up @@ -3127,7 +3127,7 @@ impl Connection {

// PATH_RESPONSE
if buf.len() + 9 < max_size && space_id == SpaceId::Data {
if let Some(token) = self.path_responses.pop_on_path(&self.path.remote) {
if let Some(token) = self.path_responses.pop_on_path(self.path.remote) {
sent.non_retransmits = true;
sent.requires_padding = true;
trace!("PATH_RESPONSE {:08x}", token);
Expand Down
8 changes: 4 additions & 4 deletions quinn-proto/src/connection/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ impl PathResponses {
}
}

pub(crate) fn pop_off_path(&mut self, remote: &SocketAddr) -> Option<(u64, SocketAddr)> {
pub(crate) fn pop_off_path(&mut self, remote: SocketAddr) -> Option<(u64, SocketAddr)> {
let response = *self.pending.last()?;
if response.remote == *remote {
if response.remote == remote {
// We don't bother searching further because we expect that the on-path response will
// get drained in the immediate future by a call to `pop_on_path`
return None;
Expand All @@ -272,9 +272,9 @@ impl PathResponses {
Some((response.token, response.remote))
}

pub(crate) fn pop_on_path(&mut self, remote: &SocketAddr) -> Option<u64> {
pub(crate) fn pop_on_path(&mut self, remote: SocketAddr) -> Option<u64> {
let response = *self.pending.last()?;
if response.remote != *remote {
if response.remote != remote {
// We don't bother searching further because we expect that the off-path response will
// get drained in the immediate future by a call to `pop_off_path`
return None;
Expand Down
2 changes: 1 addition & 1 deletion quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ impl Endpoint {
}
.encode(
&*server_config.token_key,
&incoming.addresses.remote,
incoming.addresses.remote,
&loc_cid,
);

Expand Down
16 changes: 8 additions & 8 deletions quinn-proto/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl IncomingToken {

let result = RetryToken::from_bytes(
&*server_config.token_key,
&remote_address,
remote_address,
&header.dst_cid,
&header.token,
);
Expand Down Expand Up @@ -77,7 +77,7 @@ impl RetryToken {
pub(crate) fn encode(
&self,
key: &dyn HandshakeTokenKey,
address: &SocketAddr,
address: SocketAddr,
retry_src_cid: &ConnectionId,
) -> Vec<u8> {
let aead_key = key.aead_from_hkdf(retry_src_cid);
Expand All @@ -99,7 +99,7 @@ impl RetryToken {

fn from_bytes(
key: &dyn HandshakeTokenKey,
address: &SocketAddr,
address: SocketAddr,
retry_src_cid: &ConnectionId,
raw_token_bytes: &[u8],
) -> Result<Self, ValidationError> {
Expand All @@ -109,7 +109,7 @@ impl RetryToken {
let data = aead_key.open(&mut sealed_token, &[])?;
let mut reader = io::Cursor::new(data);
let token_addr = decode_addr(&mut reader).ok_or(ValidationError::Unusable)?;
if token_addr != *address {
if token_addr != address {
return Err(ValidationError::InvalidRetry);
}
let orig_dst_cid =
Expand All @@ -127,7 +127,7 @@ impl RetryToken {
}
}

fn encode_addr(buf: &mut Vec<u8>, address: &SocketAddr) {
fn encode_addr(buf: &mut Vec<u8>, address: SocketAddr) {
match address.ip() {
IpAddr::V4(x) => {
buf.put_u8(0);
Expand Down Expand Up @@ -262,9 +262,9 @@ mod test {
orig_dst_cid: RandomConnectionIdGenerator::new(MAX_CID_SIZE).generate_cid(),
issued: UNIX_EPOCH + Duration::new(42, 0), // Fractional seconds would be lost
};
let encoded = token.encode(&prk, &addr, &retry_src_cid);
let encoded = token.encode(&prk, addr, &retry_src_cid);

let decoded = RetryToken::from_bytes(&prk, &addr, &retry_src_cid, &encoded)
let decoded = RetryToken::from_bytes(&prk, addr, &retry_src_cid, &encoded)
.expect("token didn't validate");
assert_eq!(token.orig_dst_cid, decoded.orig_dst_cid);
assert_eq!(token.issued, decoded.issued);
Expand Down Expand Up @@ -295,6 +295,6 @@ mod test {
invalid_token.put_slice(&random_data);

// Assert: garbage sealed data returns err
assert!(RetryToken::from_bytes(&prk, &addr, &retry_src_cid, &invalid_token).is_err());
assert!(RetryToken::from_bytes(&prk, addr, &retry_src_cid, &invalid_token).is_err());
}
}
Loading