Skip to content

Commit

Permalink
proto: expose PlainHeader and PartialDecode to the public
Browse files Browse the repository at this point in the history
  • Loading branch information
LAN Xingcan committed May 17, 2024
1 parent 4142b8b commit f3fe143
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
3 changes: 3 additions & 0 deletions quinn-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ mod cid_queue;
#[doc(hidden)]
pub mod coding;
mod constant_time;

mod packet;
pub use packet::{LongType, PacketDecodeError, PartialDecode, PlainHeader, PlainInitialHeader};

mod range_set;
#[cfg(all(test, feature = "rustls"))]
mod tests;
Expand Down
39 changes: 32 additions & 7 deletions quinn-proto/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ impl PartialDecode {
}
}

pub(crate) fn dst_cid(&self) -> &ConnectionId {
/// The destination connection ID of the packet
pub fn dst_cid(&self) -> &ConnectionId {
self.plain_header.dst_cid()
}

Expand Down Expand Up @@ -508,8 +509,10 @@ impl PartialEncode {
}
}

/// Plain header of a packet
#[derive(Clone, Debug)]
pub(crate) enum PlainHeader {
#[allow(missing_docs)]
pub enum PlainHeader {
Initial(PlainInitialHeader),
Long {
ty: LongType,
Expand All @@ -519,7 +522,9 @@ pub(crate) enum PlainHeader {
version: u32,
},
Retry {
/// Destination Connection ID
dst_cid: ConnectionId,
/// Source Connection ID
src_cid: ConnectionId,
version: u32,
},
Expand All @@ -535,14 +540,15 @@ pub(crate) enum PlainHeader {
}

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

fn dst_cid(&self) -> &ConnectionId {
/// The destination Connection ID of the packet.
pub fn dst_cid(&self) -> &ConnectionId {
use self::PlainHeader::*;
match self {
Initial(header) => &header.dst_cid,
Expand Down Expand Up @@ -655,15 +661,27 @@ impl PlainHeader {
}
}

/// A Plain QUIC Header
#[derive(Clone, Debug)]
pub(crate) struct PlainInitialHeader {
pub 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,
}

impl PlainInitialHeader {
/// The destination Connection ID of the packet.
pub fn dst_cid(&self) -> &ConnectionId {
&self.dst_cid
}
/// The source Connection ID of the packet.
pub fn src_cid(&self) -> &ConnectionId {
&self.src_cid
}
}

#[derive(Clone, Debug)]
pub(crate) struct InitialHeader {
pub(crate) dst_cid: ConnectionId,
Expand Down Expand Up @@ -812,20 +830,27 @@ impl From<LongHeaderType> for u8 {

/// Long packet types with uniform header structure
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum LongType {
pub enum LongType {
/// Long header type for Handshake packets
Handshake,
/// Long header type for 0-RTT packets
ZeroRtt,
}

#[allow(unreachable_pub)] // fuzzing only
#[derive(Debug, Error, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Packet decode error
pub enum PacketDecodeError {
/// The packet uses a QUIC version that is not supported
#[error("unsupported version {version:x}")]
UnsupportedVersion {
/// The source connection ID
src_cid: ConnectionId,
/// The destination connection ID
dst_cid: ConnectionId,
/// The version that was not supported
version: u32,
},
/// The header of the packet is invalid
#[error("invalid header: {0}")]
InvalidHeader(&'static str),
}
Expand Down

0 comments on commit f3fe143

Please sign in to comment.