Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yield protocol violation for packets without frames #1693

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,7 @@ impl Connection {
return Ok(());
}
State::Closed(_) => {
for result in frame::Iter::new(packet.payload.freeze()) {
for result in frame::Iter::new(packet.payload.freeze())? {
let frame = match result {
Ok(frame) => frame,
Err(err) => {
Expand Down Expand Up @@ -2441,7 +2441,7 @@ impl Connection {
debug_assert_ne!(packet.header.space(), SpaceId::Data);
let payload_len = packet.payload.len();
let mut ack_eliciting = false;
for result in frame::Iter::new(packet.payload.freeze()) {
for result in frame::Iter::new(packet.payload.freeze())? {
let frame = result?;
let span = match frame {
Frame::Padding => continue,
Expand Down Expand Up @@ -2499,7 +2499,7 @@ impl Connection {
let mut close = None;
let payload_len = payload.len();
let mut ack_eliciting = false;
for result in frame::Iter::new(payload) {
for result in frame::Iter::new(payload)? {
let frame = result?;
let span = match frame {
Frame::Padding => continue,
Expand Down
60 changes: 35 additions & 25 deletions quinn-proto/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,35 +526,21 @@ pub(crate) struct Iter {
last_ty: Option<Type>,
}

enum IterErr {
UnexpectedEnd,
InvalidFrameId,
Malformed,
}

impl IterErr {
fn reason(&self) -> &'static str {
use self::IterErr::*;
match *self {
UnexpectedEnd => "unexpected end",
InvalidFrameId => "invalid frame ID",
Malformed => "malformed",
impl Iter {
pub(crate) fn new(payload: Bytes) -> Result<Self, TransportError> {
if payload.is_empty() {
// "An endpoint MUST treat receipt of a packet containing no frames as a
// connection error of type PROTOCOL_VIOLATION."
// https://www.rfc-editor.org/rfc/rfc9000.html#name-frames-and-frame-types
return Err(TransportError::PROTOCOL_VIOLATION(
"packet payload is empty",
));
}
}
}

impl From<UnexpectedEnd> for IterErr {
fn from(_: UnexpectedEnd) -> Self {
Self::UnexpectedEnd
}
}

impl Iter {
pub(crate) fn new(payload: Bytes) -> Self {
Self {
Ok(Self {
bytes: io::Cursor::new(payload),
last_ty: None,
}
})
}

fn take_len(&mut self) -> Result<Bytes, UnexpectedEnd> {
Expand Down Expand Up @@ -777,6 +763,29 @@ fn scan_ack_blocks(buf: &mut io::Cursor<Bytes>, largest: u64, n: usize) -> Resul
Ok(())
}

enum IterErr {
UnexpectedEnd,
InvalidFrameId,
Malformed,
}

impl IterErr {
fn reason(&self) -> &'static str {
use self::IterErr::*;
match *self {
UnexpectedEnd => "unexpected end",
InvalidFrameId => "invalid frame ID",
Malformed => "malformed",
}
}
}

impl From<UnexpectedEnd> for IterErr {
fn from(_: UnexpectedEnd) -> Self {
Self::UnexpectedEnd
}
}

#[derive(Debug, Clone)]
pub struct AckIter<'a> {
largest: u64,
Expand Down Expand Up @@ -923,6 +932,7 @@ mod test {

fn frames(buf: Vec<u8>) -> Vec<Frame> {
Iter::new(Bytes::from(buf))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
}
Expand Down
1 change: 1 addition & 0 deletions quinn-proto/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,7 @@ fn single_ack_eliciting_packet_triggers_ack_after_delay() {
// The ACK delay is properly calculated
assert_eq!(pair.client.captured_packets.len(), 1);
let mut frames = frame::Iter::new(pair.client.captured_packets.remove(0).into())
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(frames.len(), 1);
Expand Down
5 changes: 1 addition & 4 deletions quinn-proto/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,7 @@ impl TestEndpoint {
self.captured_packets.extend(packet);
}

self.conn_events
.entry(ch)
.or_insert_with(VecDeque::new)
.push_back(event);
self.conn_events.entry(ch).or_default().push_back(event);
}
DatagramEvent::Response(transmit) => {
self.outbound.extend(split_transmit(transmit));
Expand Down
2 changes: 1 addition & 1 deletion quinn/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ impl Connection {
///
/// The dynamic type returned is determined by the configured
/// [`Session`](proto::crypto::Session). For the default `rustls` session, the return value can
/// be [`downcast`](Box::downcast) to a <code>Vec<[rustls::Certificate](rustls::Certificate)></code>
/// be [`downcast`](Box::downcast) to a <code>Vec<[rustls::Certificate]></code>
pub fn peer_identity(&self) -> Option<Box<dyn Any>> {
self.0
.state
Expand Down