Skip to content

Commit

Permalink
Factor initial packet plain header fields out into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Mar 26, 2024
1 parent 95ac70d commit 36950c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
29 changes: 15 additions & 14 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,28 @@ impl Endpoint {
}
};

if let Some(version) = first_decode.initial_version() {
if let Some(header) = first_decode.initial_header() {
if datagram_len < MIN_INITIAL_SIZE as usize {
debug!("ignoring short initial for connection {}", dst_cid);
return None;
}

let crypto = match server_config
.crypto
.initial_keys(version, dst_cid, Side::Server)
{
Ok(keys) => keys,
Err(UnsupportedVersion) => {
// This probably indicates that the user set supported_versions incorrectly in
// `EndpointConfig`.
debug!(
let crypto =
match server_config
.crypto
.initial_keys(header.version, dst_cid, Side::Server)
{
Ok(keys) => keys,
Err(UnsupportedVersion) => {
// This probably indicates that the user set supported_versions incorrectly in
// `EndpointConfig`.
debug!(
"ignoring initial packet version {:#x} unsupported by cryptographic layer",
version
header.version
);
return None;
}
};
return None;
}
};
return match first_decode.finish(Some(&*crypto.header.remote)) {
Ok(packet) => {
self.handle_first_packet(now, addresses, ecn, packet, remaining, &crypto, buf)
Expand Down
43 changes: 25 additions & 18 deletions quinn-proto/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ impl PartialDecode {
self.buf.get_ref()
}

pub(crate) fn initial_version(&self) -> Option<u32> {
match self.plain_header {
PlainHeader::Initial { version, .. } => Some(version),
_ => None,
}
pub(crate) fn initial_header(&self) -> Option<&PlainInitialHeader> {
self.plain_header.as_initial()
}

pub(crate) fn has_long_header(&self) -> bool {
Expand Down Expand Up @@ -119,13 +116,13 @@ impl PartialDecode {
mut buf,
} = self;

if let Initial {
if let Initial(PlainInitialHeader {
dst_cid,
src_cid,
token_pos,
version,
..
} = plain_header
}) = plain_header
{
let number = Self::decrypt_header(&mut buf, header_crypto.unwrap())?;
let header_len = buf.position() as usize;
Expand Down Expand Up @@ -481,13 +478,7 @@ impl PartialEncode {

#[derive(Clone, Debug)]
pub(crate) enum PlainHeader {
Initial {
dst_cid: ConnectionId,
src_cid: ConnectionId,
token_pos: Range<usize>,
len: u64,
version: u32,
},
Initial(PlainInitialHeader),
Long {
ty: LongType,
dst_cid: ConnectionId,
Expand All @@ -512,10 +503,17 @@ pub(crate) enum PlainHeader {
}

impl PlainHeader {
pub(crate) fn as_initial(&self) -> Option<&PlainInitialHeader> {
match self {
Self::Initial(x) => Some(x),
_ => None,
}
}

fn dst_cid(&self) -> &ConnectionId {
use self::PlainHeader::*;
match self {
Initial { dst_cid, .. } => dst_cid,
Initial(header) => &header.dst_cid,
Long { dst_cid, .. } => dst_cid,
Retry { dst_cid, .. } => dst_cid,
Short { dst_cid, .. } => dst_cid,
Expand All @@ -526,7 +524,7 @@ impl PlainHeader {
fn payload_len(&self) -> Option<u64> {
use self::PlainHeader::*;
match self {
Initial { len, .. } | Long { len, .. } => Some(*len),
Initial(PlainInitialHeader { len, .. }) | Long { len, .. } => Some(*len),
_ => None,
}
}
Expand Down Expand Up @@ -587,13 +585,13 @@ impl PlainHeader {
buf.advance(token_len);

let len = buf.get_var()?;
Ok(Self::Initial {
Ok(Self::Initial(PlainInitialHeader {
dst_cid,
src_cid,
token_pos: token_start..token_start + token_len,
len,
version,
})
}))
}
LongHeaderType::Retry => Ok(Self::Retry {
dst_cid,
Expand All @@ -612,6 +610,15 @@ impl PlainHeader {
}
}

#[derive(Clone, Debug)]
pub(crate) struct PlainInitialHeader {
pub(crate) dst_cid: ConnectionId,
pub(crate) src_cid: ConnectionId,
pub(crate) token_pos: Range<usize>,
pub(crate) len: u64,
pub(crate) version: u32,
}

// An encoded packet number
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum PacketNumber {
Expand Down

0 comments on commit 36950c6

Please sign in to comment.