| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::fmt::{self, Display, Formatter}; |
| 8 |
|
| 9 |
use neqo_common::qwarn; |
| 10 |
use neqo_crypto::Error as CryptoError; |
| 11 |
|
| 12 |
mod ackrate; |
| 13 |
mod addr_valid; |
| 14 |
mod cc; |
| 15 |
mod cid; |
| 16 |
mod connection; |
| 17 |
mod crypto; |
| 18 |
pub mod ecn; |
| 19 |
mod events; |
| 20 |
mod fc; |
| 21 |
#[cfg(fuzzing)] |
| 22 |
pub mod frame; |
| 23 |
#[cfg(not(fuzzing))] |
| 24 |
mod frame; |
| 25 |
mod pace; |
| 26 |
#[cfg(any(fuzzing, feature = "bench"))] |
| 27 |
pub mod packet; |
| 28 |
#[cfg(not(any(fuzzing, feature = "bench")))] |
| 29 |
mod packet; |
| 30 |
mod path; |
| 31 |
mod pmtud; |
| 32 |
mod qlog; |
| 33 |
mod quic_datagrams; |
| 34 |
#[cfg(feature = "bench")] |
| 35 |
pub mod recovery; |
| 36 |
#[cfg(not(feature = "bench"))] |
| 37 |
mod recovery; |
| 38 |
#[cfg(feature = "bench")] |
| 39 |
pub mod recv_stream; |
| 40 |
#[cfg(not(feature = "bench"))] |
| 41 |
mod recv_stream; |
| 42 |
mod rtt; |
| 43 |
#[cfg(feature = "bench")] |
| 44 |
pub mod send_stream; |
| 45 |
#[cfg(not(feature = "bench"))] |
| 46 |
mod send_stream; |
| 47 |
mod sender; |
| 48 |
pub mod server; |
| 49 |
mod sni; |
| 50 |
mod stats; |
| 51 |
pub mod stream_id; |
| 52 |
pub mod streams; |
| 53 |
pub mod tparams; |
| 54 |
mod tracking; |
| 55 |
pub mod version; |
| 56 |
|
| 57 |
pub use self::{ |
| 58 |
cc::CongestionControlAlgorithm, |
| 59 |
cid::{ |
| 60 |
ConnectionId, ConnectionIdDecoder, ConnectionIdGenerator, ConnectionIdRef, |
| 61 |
EmptyConnectionIdGenerator, RandomConnectionIdGenerator, |
| 62 |
}, |
| 63 |
connection::{ |
| 64 |
params::{ConnectionParameters, ACK_RATIO_SCALE}, |
| 65 |
Connection, Output, State, ZeroRttState, |
| 66 |
}, |
| 67 |
events::{ConnectionEvent, ConnectionEvents}, |
| 68 |
frame::CloseError, |
| 69 |
packet::MIN_INITIAL_PACKET_SIZE, |
| 70 |
pmtud::Pmtud, |
| 71 |
quic_datagrams::DatagramTracking, |
| 72 |
recv_stream::{RecvStreamStats, INITIAL_RECV_WINDOW_SIZE}, |
| 73 |
send_stream::SendStreamStats, |
| 74 |
sni::find_sni, |
| 75 |
stats::Stats, |
| 76 |
stream_id::{StreamId, StreamType}, |
| 77 |
version::Version, |
| 78 |
}; |
| 79 |
|
| 80 |
pub type TransportError = u64; |
| 81 |
const ERROR_APPLICATION_CLOSE: TransportError = 12; |
| 82 |
const ERROR_CRYPTO_BUFFER_EXCEEDED: TransportError = 13; |
| 83 |
const ERROR_AEAD_LIMIT_REACHED: TransportError = 15; |
| 84 |
|
| 85 |
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq)] |
| 86 |
pub enum Error { |
| 87 |
NoError, |
| 88 |
// Each time this error is returned a different parameter is supplied. |
| 89 |
// This will be used to distinguish each occurrence of this error. |
| 90 |
InternalError, |
| 91 |
ConnectionRefused, |
| 92 |
FlowControlError, |
| 93 |
StreamLimitError, |
| 94 |
StreamStateError, |
| 95 |
FinalSizeError, |
| 96 |
FrameEncodingError, |
| 97 |
TransportParameterError, |
| 98 |
ProtocolViolation, |
| 99 |
InvalidToken, |
| 100 |
ApplicationError, |
| 101 |
CryptoBufferExceeded, |
| 102 |
CryptoError(CryptoError), |
| 103 |
QlogError, |
| 104 |
CryptoAlert(u8), |
| 105 |
EchRetry(Vec<u8>), |
| 106 |
|
| 107 |
// All internal errors from here. Please keep these sorted. |
| 108 |
AckedUnsentPacket, |
| 109 |
ConnectionIdLimitExceeded, |
| 110 |
ConnectionIdsExhausted, |
| 111 |
ConnectionState, |
| 112 |
DecryptError, |
| 113 |
DisabledVersion, |
| 114 |
IdleTimeout, |
| 115 |
IntegerOverflow, |
| 116 |
InvalidInput, |
| 117 |
InvalidMigration, |
| 118 |
InvalidPacket, |
| 119 |
InvalidResumptionToken, |
| 120 |
InvalidRetry, |
| 121 |
InvalidStreamId, |
| 122 |
KeysDiscarded(crypto::Epoch), |
| 123 |
/// Packet protection keys are exhausted. |
| 124 |
/// Also used when too many key updates have happened. |
| 125 |
KeysExhausted, |
| 126 |
/// Packet protection keys aren't available yet for the identified space. |
| 127 |
KeysPending(crypto::Epoch), |
| 128 |
/// An attempt to update keys can be blocked if |
| 129 |
/// a packet sent with the current keys hasn't been acknowledged. |
| 130 |
KeyUpdateBlocked, |
| 131 |
NoAvailablePath, |
| 132 |
NoMoreData, |
| 133 |
NotAvailable, |
| 134 |
NotConnected, |
| 135 |
PacketNumberOverlap, |
| 136 |
PeerApplicationError(AppError), |
| 137 |
PeerError(TransportError), |
| 138 |
StatelessReset, |
| 139 |
TooMuchData, |
| 140 |
UnexpectedMessage, |
| 141 |
UnknownConnectionId, |
| 142 |
UnknownFrameType, |
| 143 |
VersionNegotiation, |
| 144 |
WrongRole, |
| 145 |
UnknownTransportParameter, |
| 146 |
} |
| 147 |
|
| 148 |
impl Error { |
| 149 |
#[must_use] |
| 150 |
pub fn code(&self) -> TransportError { |
| 151 |
match self { |
| 152 |
Self::NoError |
| 153 |
| Self::IdleTimeout |
| 154 |
| Self::PeerError(_) |
| 155 |
| Self::PeerApplicationError(_) => 0, |
| 156 |
Self::ConnectionRefused => 2, |
| 157 |
Self::FlowControlError => 3, |
| 158 |
Self::StreamLimitError => 4, |
| 159 |
Self::StreamStateError => 5, |
| 160 |
Self::FinalSizeError => 6, |
| 161 |
Self::FrameEncodingError => 7, |
| 162 |
Self::TransportParameterError => 8, |
| 163 |
Self::ProtocolViolation => 10, |
| 164 |
Self::InvalidToken => 11, |
| 165 |
Self::KeysExhausted => ERROR_AEAD_LIMIT_REACHED, |
| 166 |
Self::ApplicationError => ERROR_APPLICATION_CLOSE, |
| 167 |
Self::NoAvailablePath => 16, |
| 168 |
Self::CryptoBufferExceeded => ERROR_CRYPTO_BUFFER_EXCEEDED, |
| 169 |
Self::CryptoAlert(a) => 0x100 + u64::from(*a), |
| 170 |
// As we have a special error code for ECH fallbacks, we lose the alert. |
| 171 |
// Send the server "ech_required" directly. |
| 172 |
Self::EchRetry(_) => 0x100 + 121, |
| 173 |
Self::VersionNegotiation => 0x53f8, |
| 174 |
// All the rest are internal errors. |
| 175 |
_ => 1, |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
impl From<CryptoError> for Error { |
| 181 |
fn from(err: CryptoError) -> Self { |
| 182 |
qwarn!("Crypto operation failed {err:?}"); |
| 183 |
match err { |
| 184 |
CryptoError::EchRetry(config) => Self::EchRetry(config), |
| 185 |
_ => Self::CryptoError(err), |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
impl From<::qlog::Error> for Error { |
| 191 |
fn from(_err: ::qlog::Error) -> Self { |
| 192 |
Self::QlogError |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
impl From<std::num::TryFromIntError> for Error { |
| 197 |
fn from(_: std::num::TryFromIntError) -> Self { |
| 198 |
Self::IntegerOverflow |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
impl ::std::error::Error for Error { |
| 203 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 204 |
match self { |
| 205 |
Self::CryptoError(e) => Some(e), |
| 206 |
_ => None, |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
impl Display for Error { |
| 212 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 213 |
write!(f, "Transport error: {self:?}") |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
pub type AppError = u64; |
| 218 |
|
| 219 |
#[deprecated(note = "use `CloseReason` instead")] |
| 220 |
pub type ConnectionError = CloseReason; |
| 221 |
|
| 222 |
/// Reason why a connection closed. |
| 223 |
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq)] |
| 224 |
pub enum CloseReason { |
| 225 |
Transport(Error), |
| 226 |
Application(AppError), |
| 227 |
} |
| 228 |
|
| 229 |
impl CloseReason { |
| 230 |
#[must_use] |
| 231 |
pub const fn app_code(&self) -> Option<AppError> { |
| 232 |
match self { |
| 233 |
Self::Application(e) => Some(*e), |
| 234 |
Self::Transport(_) => None, |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/// Checks enclosed error for [`Error::NoError`] and |
| 239 |
/// [`CloseReason::Application(0)`]. |
| 240 |
#[must_use] |
| 241 |
pub const fn is_error(&self) -> bool { |
| 242 |
!matches!(self, Self::Transport(Error::NoError) | Self::Application(0),) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
impl From<CloseError> for CloseReason { |
| 247 |
fn from(err: CloseError) -> Self { |
| 248 |
match err { |
| 249 |
CloseError::Transport(c) => Self::Transport(Error::PeerError(c)), |
| 250 |
CloseError::Application(c) => Self::Application(c), |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
pub type Res<T> = Result<T, Error>; |