use std::{sync::Arc, time::Duration};
use thiserror::Error;
use tor_cell::relaycell::msg::EndReason;
use tor_error::{ErrorKind, HasKind};
use tor_linkspec::RelayIdType;
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("Unable to parse {object}")]
BytesErr {
object: &'static str,
#[source]
err: tor_bytes::Error,
},
#[error("IO error on channel with peer")]
ChanIoErr(#[source] Arc<std::io::Error>),
#[error("IO error while handshaking with peer")]
HandshakeIoErr(#[source] Arc<std::io::Error>),
#[error("Unable to generate or encode {object}")]
CellEncodeErr {
object: &'static str,
#[source]
err: tor_cell::Error,
},
#[error("Error while parsing {object}")]
CellDecodeErr {
object: &'static str,
#[source]
err: tor_cell::Error,
},
#[error("Problem while encoding {object}")]
EncodeErr {
object: &'static str,
#[source]
err: tor_bytes::EncodeError,
},
#[error("Problem with certificate on handshake")]
HandshakeCertErr(#[source] tor_cert::CertError),
#[error("Tried to extract too many bytes from a KDF")]
InvalidKDFOutputLength,
#[error("Tried to encrypt a cell for a nonexistent hop")]
NoSuchHop,
#[error("Bad relay cell authentication")]
BadCellAuth,
#[error("Circuit-extension handshake authentication failed")]
BadCircHandshakeAuth,
#[error("Handshake protocol violation: {0}")]
HandshakeProto(String),
#[error("Handshake failed due to expired certificates (possible clock skew)")]
HandshakeCertsExpired {
expired_by: Duration,
},
#[error("Channel protocol violation: {0}")]
ChanProto(String),
#[error("Circuit protocol violation: {0}")]
CircProto(String),
#[error("Channel closed")]
ChannelClosed(#[from] ChannelClosed),
#[error("Circuit closed")]
CircuitClosed,
#[error("Too many entries in map: can't allocate ID")]
IdRangeFull,
#[error("Circuit extension refused: {0}")]
CircRefused(&'static str),
#[error("Invalid stream target address")]
BadStreamAddress,
#[error("Received an END cell with reason {0}")]
EndReceived(EndReason),
#[error("Stream not connected")]
NotConnected,
#[error("Stream protocol violation: {0}")]
StreamProto(String),
#[error("Peer identity mismatch: {0}")]
ChanMismatch(String),
#[error("Programming error")]
Bug(#[from] tor_error::Bug),
#[error("Remote resolve failed")]
ResolveError(#[source] ResolveError),
#[error("Relay has no {0} identity")]
MissingId(RelayIdType),
}
#[derive(Error, Debug, Clone)]
#[error("Channel closed")]
pub struct ChannelClosed;
impl HasKind for ChannelClosed {
fn kind(&self) -> ErrorKind {
ErrorKind::CircuitCollapse
}
}
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum ResolveError {
#[error("Received retriable transient error")]
Transient,
#[error("Received non-retriable error")]
Nontransient,
#[error("Received unrecognized result")]
Unrecognized,
}
impl Error {
pub(crate) fn from_cell_enc(err: tor_cell::Error, object: &'static str) -> Error {
Error::CellEncodeErr { object, err }
}
pub(crate) fn from_cell_dec(err: tor_cell::Error, object: &'static str) -> Error {
match err {
tor_cell::Error::ChanProto(msg) => Error::ChanProto(msg),
_ => Error::CellDecodeErr { err, object },
}
}
pub(crate) fn from_bytes_err(err: tor_bytes::Error, object: &'static str) -> Error {
Error::BytesErr { err, object }
}
pub(crate) fn from_bytes_enc(err: tor_bytes::EncodeError, object: &'static str) -> Error {
Error::EncodeErr { err, object }
}
}
impl From<Error> for std::io::Error {
fn from(err: Error) -> std::io::Error {
use std::io::ErrorKind;
use Error::*;
let kind = match err {
ChanIoErr(e) | HandshakeIoErr(e) => match Arc::try_unwrap(e) {
Ok(e) => return e,
Err(arc) => return std::io::Error::new(arc.kind(), arc),
},
InvalidKDFOutputLength | NoSuchHop | BadStreamAddress => ErrorKind::InvalidInput,
NotConnected => ErrorKind::NotConnected,
EndReceived(end_reason) => end_reason.into(),
CircuitClosed => ErrorKind::ConnectionReset,
BytesErr { .. }
| BadCellAuth
| BadCircHandshakeAuth
| HandshakeProto(_)
| HandshakeCertErr(_)
| ChanProto(_)
| HandshakeCertsExpired { .. }
| ChannelClosed(_)
| CircProto(_)
| CellDecodeErr { .. }
| CellEncodeErr { .. }
| EncodeErr { .. }
| ChanMismatch(_)
| StreamProto(_)
| MissingId(_) => ErrorKind::InvalidData,
Bug(ref e) if e.kind() == tor_error::ErrorKind::BadApiUsage => ErrorKind::InvalidData,
IdRangeFull | CircRefused(_) | ResolveError(_) | Bug(_) => ErrorKind::Other,
};
std::io::Error::new(kind, err)
}
}
impl HasKind for Error {
fn kind(&self) -> ErrorKind {
use tor_bytes::Error as BytesError;
use Error as E;
use ErrorKind as EK;
match self {
E::BytesErr {
err: BytesError::Bug(e),
..
} => e.kind(),
E::BytesErr { .. } => EK::TorProtocolViolation,
E::ChanIoErr(_) => EK::LocalNetworkError,
E::HandshakeIoErr(_) => EK::TorAccessFailed,
E::HandshakeCertErr(_) => EK::TorProtocolViolation,
E::CellEncodeErr { err, .. } => err.kind(),
E::CellDecodeErr { err, .. } => err.kind(),
E::EncodeErr { .. } => EK::BadApiUsage,
E::InvalidKDFOutputLength => EK::Internal,
E::NoSuchHop => EK::BadApiUsage,
E::BadCellAuth => EK::TorProtocolViolation,
E::BadCircHandshakeAuth => EK::TorProtocolViolation,
E::HandshakeProto(_) => EK::TorAccessFailed,
E::HandshakeCertsExpired { .. } => EK::ClockSkew,
E::ChanProto(_) => EK::TorProtocolViolation,
E::CircProto(_) => EK::TorProtocolViolation,
E::ChannelClosed(e) => e.kind(),
E::CircuitClosed => EK::CircuitCollapse,
E::IdRangeFull => EK::BadApiUsage,
E::CircRefused(_) => EK::CircuitRefused,
E::BadStreamAddress => EK::BadApiUsage,
E::EndReceived(reason) => reason.kind(),
E::NotConnected => EK::BadApiUsage,
E::StreamProto(_) => EK::TorProtocolViolation,
E::ChanMismatch(_) => EK::RelayIdMismatch,
E::ResolveError(ResolveError::Nontransient) => EK::RemoteHostNotFound,
E::ResolveError(ResolveError::Transient) => EK::RemoteHostResolutionFailed,
E::ResolveError(ResolveError::Unrecognized) => EK::RemoteHostResolutionFailed,
E::MissingId(_) => EK::BadApiUsage,
E::Bug(e) => e.kind(),
}
}
}
#[derive(Debug)]
pub(crate) enum ReactorError {
Err(Error),
Shutdown,
}
impl From<Error> for ReactorError {
fn from(e: Error) -> ReactorError {
ReactorError::Err(e)
}
}
impl From<ChannelClosed> for ReactorError {
fn from(e: ChannelClosed) -> ReactorError {
ReactorError::Err(e.into())
}
}
#[cfg(test)]
impl ReactorError {
pub(crate) fn unwrap_err(self) -> Error {
match self {
ReactorError::Shutdown => panic!(),
ReactorError::Err(e) => e,
}
}
}