Skip to content

Commit

Permalink
lib: Add TryFrom<u64> for WireErrorCode
Browse files Browse the repository at this point in the history
This is a fallible conversion, since a peer could send an error
code value that is unknown to quiche itself.
  • Loading branch information
LPardue committed May 29, 2024
1 parent 731510e commit 904dc0a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
55 changes: 55 additions & 0 deletions quiche/src/h3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,48 @@ pub enum WireErrorCode {
VersionFallback = 0x110,
}

/// Errors for conversions related to [WireErrorCode].
#[derive(Debug, Eq, PartialEq)]
pub enum FromWireConversionError {
/// The value was larger than the maximum encodable length.
TooBig,
/// The value was unknown by quiche, possibly a private extension or grease.
Unknown,
}

impl TryFrom<u64> for WireErrorCode {
type Error = FromWireConversionError;

fn try_from(value: u64) -> std::result::Result<Self, Self::Error> {
if value >= 1 << 62 {
return Err(FromWireConversionError::TooBig);
}

let res = match value {
0x100 => WireErrorCode::NoError,
0x101 => WireErrorCode::GeneralProtocolError,
0x102 => WireErrorCode::InternalError,
0x103 => WireErrorCode::StreamCreationError,
0x104 => WireErrorCode::ClosedCriticalStream,
0x105 => WireErrorCode::FrameUnexpected,
0x106 => WireErrorCode::FrameError,
0x107 => WireErrorCode::ExcessiveLoad,
0x108 => WireErrorCode::IdError,
0x109 => WireErrorCode::SettingsError,
0x10a => WireErrorCode::MissingSettings,
0x10b => WireErrorCode::RequestRejected,
0x10c => WireErrorCode::RequestCancelled,
0x10d => WireErrorCode::RequestIncomplete,
0x10e => WireErrorCode::MessageError,
0x10f => WireErrorCode::ConnectError,
0x110 => WireErrorCode::VersionFallback,
_ => return Err(FromWireConversionError::Unknown),
};

Ok(res)
}
}

impl Error {
fn to_wire(self) -> u64 {
match self {
Expand Down Expand Up @@ -6482,6 +6524,19 @@ mod tests {
assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
assert_eq!(s.poll_client(), Err(Error::Done));
}

#[test]
fn wire_error_convert() {
assert_eq!(WireErrorCode::try_from(0x100), Ok(WireErrorCode::NoError));
assert_eq!(
WireErrorCode::try_from(u64::MAX),
Err(FromWireConversionError::TooBig)
);
assert_eq!(
WireErrorCode::try_from(0),
Err(FromWireConversionError::Unknown)
);
}
}

#[cfg(feature = "ffi")]
Expand Down
63 changes: 63 additions & 0 deletions quiche/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ use qlog::events::RawInfo;
use stream::StreamPriorityKey;

use std::cmp;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::time;

Expand Down Expand Up @@ -643,6 +644,51 @@ pub enum WireErrorCode {
NoViablePath = 0x10,
}

/// Errors for conversions related to [WireErrorCode].
#[derive(Debug, Eq, PartialEq)]
pub enum FromWireConversionError {
/// The value was larger than the maximum encodable length.
TooBig,
/// The value was in the crypto error range.
CryptoRange,
/// The value was unknown by quiche, possibly a private extension or grease.
Unknown,
}

impl TryFrom<u64> for WireErrorCode {
type Error = FromWireConversionError;

fn try_from(value: u64) -> std::result::Result<Self, Self::Error> {
if value >= 1 << 62 {
return Err(FromWireConversionError::TooBig);
}

let res = match value {
0x0 => Self::NoError,
0x1 => Self::InternalError,
0x2 => Self::ConnectionRefused,
0x3 => Self::FlowControlError,
0x4 => Self::StreamLimitError,
0x5 => Self::StreamStateError,
0x6 => Self::FinalSizeError,
0x7 => Self::FrameEncodingError,
0x8 => Self::TransportParameterError,
0x9 => Self::ConnectionIdLimitError,
0xa => Self::ProtocolViolation,
0xb => Self::InvalidToken,
0xc => Self::ApplicationError,
0xd => Self::CryptoBufferExceeded,
0xe => Self::KeyUpdateError,
0xf => Self::AeadLimitReached,
0x10 => Self::NoViablePath,
0x100..=0x1ff => return Err(FromWireConversionError::CryptoRange),
_ => return Err(FromWireConversionError::Unknown),
};

Ok(res)
}
}

impl Error {
fn to_wire(self) -> u64 {
match self {
Expand Down Expand Up @@ -17334,6 +17380,23 @@ mod tests {
// Continue searching for PMTU
assert!(pmtu_param.get_probe_status());
}

#[test]
fn wire_error_convert() {
assert_eq!(WireErrorCode::try_from(0), Ok(WireErrorCode::NoError));
assert_eq!(
WireErrorCode::try_from(u64::MAX),
Err(FromWireConversionError::TooBig)
);
assert_eq!(
WireErrorCode::try_from(0x100),
Err(FromWireConversionError::CryptoRange)
);
assert_eq!(
WireErrorCode::try_from(0x200),
Err(FromWireConversionError::Unknown)
);
}
}

pub use crate::packet::ConnectionId;
Expand Down

0 comments on commit 904dc0a

Please sign in to comment.