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
thynson committed May 18, 2024
1 parent ce39cf5 commit e9ecf73
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 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
44 changes: 31 additions & 13 deletions quinn-proto/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::{
/// across QUIC versions), which gives us the destination CID and allows us
/// to inspect the version and packet type (which depends on the version).
/// This information allows us to fully decode and decrypt the packet.
#[allow(unreachable_pub)] // fuzzing only
#[cfg_attr(test, derive(Clone))]
#[derive(Debug)]
pub struct PartialDecode {
Expand Down Expand Up @@ -100,7 +99,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 @@ -490,8 +490,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 @@ -501,7 +503,9 @@ pub(crate) enum PlainHeader {
version: u32,
},
Retry {
/// Destination Connection ID
dst_cid: ConnectionId,
/// Source Connection ID
src_cid: ConnectionId,
version: u32,
},
Expand All @@ -517,14 +521,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 @@ -622,13 +627,19 @@ impl PlainHeader {
}
}

/// A Plain QUIC Header
#[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,
pub struct PlainInitialHeader {
/// Destination Connection ID
pub dst_cid: ConnectionId,
/// Source Connection ID
pub src_cid: ConnectionId,
/// The position of a token in the packet buffer
pub token_pos: Range<usize>,
/// Length of the packet payload
pub len: u64,
/// QUIC version
pub version: u32,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -779,20 +790,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 e9ecf73

Please sign in to comment.