Skip to content

Commit

Permalink
proto: unify PartialDecode constructor
Browse files Browse the repository at this point in the history
Remove the origin `PartialDecode::new` and rename
`PartialDeocde::new_with_cid_parser` to `PartialDeocde::new`.

This commit also expose `FixedLengthConnectionIdParser` to public
for to make the fuzzer be able to compile.

Since `PartialDeocde::new` was a private function, it's not
a breaking change.
  • Loading branch information
thynson committed May 17, 2024
1 parent 652d357 commit 6d4504a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 35 deletions.
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ extern crate proto;
use libfuzzer_sys::fuzz_target;
use proto::{
fuzzing::{PacketParams, PartialDecode},
DEFAULT_SUPPORTED_VERSIONS,
FixedLengthConnectionIdParser, DEFAULT_SUPPORTED_VERSIONS,
};

fuzz_target!(|data: PacketParams| {
let len = data.buf.len();
let supported_versions = DEFAULT_SUPPORTED_VERSIONS.to_vec();
if let Ok(decoded) = PartialDecode::new(
data.buf,
data.local_cid_len,
&FixedLengthConnectionIdParser::new(data.local_cid_len),
&supported_versions,
data.grease_quic_bit,
) {
Expand Down
4 changes: 2 additions & 2 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
range_set::ArrayRangeSet,
shared::{
ConnectionEvent, ConnectionEventInner, ConnectionId, DatagramConnectionEvent, EcnCodepoint,
EndpointEvent, EndpointEventInner,
EndpointEvent, EndpointEventInner, FixedLengthConnectionIdParser,
},
token::ResetToken,
transport_parameters::TransportParameters,
Expand Down Expand Up @@ -2120,7 +2120,7 @@ impl Connection {
while let Some(data) = remaining {
match PartialDecode::new(
data,
self.local_cid_state.cid_len(),
&FixedLengthConnectionIdParser::new(self.local_cid_state.cid_len()),
&[self.version],
self.endpoint_config.grease_quic_bit,
) {
Expand Down
4 changes: 2 additions & 2 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
},
shared::{
ConnectionEvent, ConnectionEventInner, ConnectionId, DatagramConnectionEvent, EcnCodepoint,
EndpointEvent, EndpointEventInner, IssuedCid,
EndpointEvent, EndpointEventInner, FixedLengthConnectionIdParser, IssuedCid,
},
token::TokenDecodeError,
transport_parameters::{PreferredAddress, TransportParameters},
Expand Down Expand Up @@ -144,7 +144,7 @@ impl Endpoint {
let datagram_len = data.len();
let (first_decode, remaining) = match PartialDecode::new(
data,
self.local_cid_generator.cid_len(),
&FixedLengthConnectionIdParser::new(self.local_cid_generator.cid_len()),
&self.config.supported_versions,
self.config.grease_quic_bit,
) {
Expand Down
1 change: 1 addition & 0 deletions quinn-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub use crate::endpoint::{
mod shared;
pub use crate::shared::{
ConnectionEvent, ConnectionId, ConnectionIdParser, EcnCodepoint, EndpointEvent,
FixedLengthConnectionIdParser,
};

mod transport_error;
Expand Down
40 changes: 13 additions & 27 deletions quinn-proto/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use thiserror::Error;
use crate::{
coding::{self, BufExt, BufMutExt},
crypto,
shared::{ConnectionIdParser, FixedLengthConnectionIdParser},
shared::ConnectionIdParser,
ConnectionId,
};

Expand All @@ -31,35 +31,16 @@ pub struct PartialDecode {

#[allow(clippy::len_without_is_empty)]
impl PartialDecode {
/// Create an instance of [`PartialDecode`], expecting a fixed-length local connection id
/// Create an instance of [`PartialDecode`]
pub fn new(
bytes: BytesMut,
local_cid_len: usize,
supported_versions: &[u32],
grease_quic_bit: bool,
) -> Result<(Self, Option<BytesMut>), PacketDecodeError> {
Self::new_with_cid_parser(
bytes,
&FixedLengthConnectionIdParser::new(local_cid_len),
supported_versions,
grease_quic_bit,
)
}

/// Create an instance of [`PartialDecode`], with given [`ConnectionIdParser`].
pub fn new_with_cid_parser(
bytes: BytesMut,
cid_parser: &impl ConnectionIdParser,
supported_versions: &[u32],
grease_quic_bit: bool,
) -> Result<(Self, Option<BytesMut>), PacketDecodeError> {
let mut buf = io::Cursor::new(bytes);
let plain_header = PlainHeader::decode(
&mut buf,
cid_parser,
supported_versions,
grease_quic_bit,
)?;
let plain_header =
PlainHeader::decode(&mut buf, cid_parser, supported_versions, grease_quic_bit)?;
let dgram_len = buf.get_ref().len();
let packet_len = plain_header
.payload_len()
Expand Down Expand Up @@ -873,7 +854,7 @@ impl SpaceId {
mod tests {
use super::*;
#[cfg(feature = "rustls")]
use crate::DEFAULT_SUPPORTED_VERSIONS;
use crate::{shared::FixedLengthConnectionIdParser, DEFAULT_SUPPORTED_VERSIONS};
use hex_literal::hex;
use std::io;

Expand Down Expand Up @@ -954,9 +935,14 @@ mod tests {

let server = initial_keys(Version::V1, &dcid, Side::Server, &suite);
let supported_versions = DEFAULT_SUPPORTED_VERSIONS.to_vec();
let decode = PartialDecode::new(buf.as_slice().into(), 0, &supported_versions, false)
.unwrap()
.0;
let decode = PartialDecode::new(
buf.as_slice().into(),
&FixedLengthConnectionIdParser::new(0),
&supported_versions,
false,
)
.unwrap()
.0;
let mut packet = decode.finish(Some(&*server.header.remote)).unwrap();
assert_eq!(
packet.header_data[..],
Expand Down
4 changes: 2 additions & 2 deletions quinn-proto/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use crate::{
ResetToken, MAX_CID_SIZE,
};

pub(crate) struct FixedLengthConnectionIdParser {
pub struct FixedLengthConnectionIdParser {
expected_len: usize,
}

impl FixedLengthConnectionIdParser {
pub(crate) fn new(expected_len: usize) -> Self {
pub fn new(expected_len: usize) -> Self {
Self { expected_len }
}
}
Expand Down

0 comments on commit 6d4504a

Please sign in to comment.