From f3fe143bb00b3b0733a595c8f8a0ce4fabed85f5 Mon Sep 17 00:00:00 2001 From: LAN Xingcan Date: Fri, 17 May 2024 18:39:19 +0800 Subject: [PATCH] proto: expose PlainHeader and PartialDecode to the public --- quinn-proto/src/lib.rs | 3 +++ quinn-proto/src/packet.rs | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/quinn-proto/src/lib.rs b/quinn-proto/src/lib.rs index 4aa082de41..9849c3d7f4 100644 --- a/quinn-proto/src/lib.rs +++ b/quinn-proto/src/lib.rs @@ -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; diff --git a/quinn-proto/src/packet.rs b/quinn-proto/src/packet.rs index 62dfa1a9ac..5ab1b383f3 100644 --- a/quinn-proto/src/packet.rs +++ b/quinn-proto/src/packet.rs @@ -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() } @@ -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, @@ -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, }, @@ -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, @@ -655,8 +661,9 @@ 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, @@ -664,6 +671,17 @@ pub(crate) struct PlainInitialHeader { 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, @@ -812,20 +830,27 @@ impl From 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), }