1use std::{sync::Arc, time::Duration};
3use thiserror::Error;
4use tor_cell::relaycell::{msg::EndReason, StreamId};
5use tor_error::{Bug, ErrorKind, HasKind};
6use tor_linkspec::RelayIdType;
7
8#[derive(Error, Debug, Clone)]
14#[non_exhaustive]
15pub enum Error {
16 #[error("Unable to parse {object}")]
19 BytesErr {
20 object: &'static str,
22 #[source]
24 err: tor_bytes::Error,
25 },
26 #[error("IO error on channel with peer")]
29 ChanIoErr(#[source] Arc<std::io::Error>),
30 #[error("IO error while handshaking with peer")]
32 HandshakeIoErr(#[source] Arc<std::io::Error>),
33 #[error("Unable to generate or encode {object}")]
35 CellEncodeErr {
36 object: &'static str,
38 #[source]
40 err: tor_cell::Error,
41 },
42 #[error("Error while parsing {object}")]
44 CellDecodeErr {
45 object: &'static str,
47 #[source]
49 err: tor_cell::Error,
50 },
51 #[error("Problem while encoding {object}")]
57 EncodeErr {
58 object: &'static str,
60 #[source]
62 err: tor_bytes::EncodeError,
63 },
64 #[error("Problem with certificate on handshake")]
67 HandshakeCertErr(#[source] tor_cert::CertError),
68 #[error("Tried to extract too many bytes from a KDF")]
70 InvalidKDFOutputLength,
71 #[error("Tried to encrypt a cell for a nonexistent hop")]
73 NoSuchHop,
74 #[error("Bad relay cell authentication")]
77 BadCellAuth,
78 #[error("Circuit-extension handshake authentication failed")]
81 BadCircHandshakeAuth,
82 #[error("Handshake protocol violation: {0}")]
84 HandshakeProto(String),
85 #[error("Handshake failed due to expired certificates (possible clock skew)")]
90 HandshakeCertsExpired {
91 expired_by: Duration,
93 },
94 #[error("Channel protocol violation: {0}")]
97 ChanProto(String),
98 #[error("Circuit protocol violation: {0}")]
100 CircProto(String),
101 #[error("Channel closed")]
104 ChannelClosed(#[from] ChannelClosed),
105 #[error("Circuit closed")]
108 CircuitClosed,
109 #[error("Too many entries in map: can't allocate ID")]
111 IdRangeFull,
112 #[error("Stream ID {0} is already in use")]
114 IdUnavailable(StreamId),
115 #[error("Received a cell with a stream ID of zero")]
117 StreamIdZero,
118 #[error("Circuit extension refused: {0}")]
121 CircRefused(&'static str),
122 #[error("Invalid stream target address")]
124 BadStreamAddress,
125 #[error("Received an END cell with reason {0}")]
127 EndReceived(EndReason),
128 #[error("Stream not connected")]
130 NotConnected,
131 #[error("Stream protocol violation: {0}")]
133 StreamProto(String),
134
135 #[error("Peer identity mismatch: {0}")]
137 ChanMismatch(String),
138 #[error("Programming error")]
140 Bug(#[from] tor_error::Bug),
141 #[error("Remote resolve failed")]
143 ResolveError(#[source] ResolveError),
144 #[error("Relay has no {0} identity")]
147 MissingId(RelayIdType),
148 #[error("memory quota error")]
150 Memquota(#[from] tor_memquota::Error),
151}
152
153#[derive(Error, Debug, Clone)]
155#[error("Channel closed")]
156pub struct ChannelClosed;
157
158impl HasKind for ChannelClosed {
159 fn kind(&self) -> ErrorKind {
160 ErrorKind::CircuitCollapse
161 }
162}
163
164#[derive(Error, Debug, Clone)]
166#[non_exhaustive]
167pub enum ResolveError {
168 #[error("Received retriable transient error")]
170 Transient,
171 #[error("Received non-retriable error")]
173 Nontransient,
174 #[error("Received unrecognized result")]
176 Unrecognized,
177}
178
179impl Error {
180 pub(crate) fn from_cell_enc(err: tor_cell::Error, object: &'static str) -> Error {
183 Error::CellEncodeErr { object, err }
184 }
185
186 pub(crate) fn from_cell_dec(err: tor_cell::Error, object: &'static str) -> Error {
189 match err {
190 tor_cell::Error::ChanProto(msg) => Error::ChanProto(msg),
191 _ => Error::CellDecodeErr { err, object },
192 }
193 }
194
195 pub(crate) fn from_bytes_err(err: tor_bytes::Error, object: &'static str) -> Error {
198 Error::BytesErr { err, object }
199 }
200
201 pub(crate) fn from_bytes_enc(err: tor_bytes::EncodeError, object: &'static str) -> Error {
204 Error::EncodeErr { err, object }
205 }
206}
207
208impl From<Error> for std::io::Error {
209 fn from(err: Error) -> std::io::Error {
210 use std::io::ErrorKind;
211 use Error::*;
212 let kind = match err {
213 ChanIoErr(e) | HandshakeIoErr(e) => match Arc::try_unwrap(e) {
214 Ok(e) => return e,
215 Err(arc) => return std::io::Error::new(arc.kind(), arc),
216 },
217
218 InvalidKDFOutputLength | NoSuchHop | BadStreamAddress => ErrorKind::InvalidInput,
219
220 NotConnected => ErrorKind::NotConnected,
221
222 EndReceived(end_reason) => end_reason.into(),
223
224 CircuitClosed => ErrorKind::ConnectionReset,
225
226 Memquota { .. } => ErrorKind::OutOfMemory,
227
228 BytesErr { .. }
229 | BadCellAuth
230 | BadCircHandshakeAuth
231 | HandshakeProto(_)
232 | HandshakeCertErr(_)
233 | ChanProto(_)
234 | HandshakeCertsExpired { .. }
235 | ChannelClosed(_)
236 | CircProto(_)
237 | CellDecodeErr { .. }
238 | CellEncodeErr { .. }
239 | EncodeErr { .. }
240 | ChanMismatch(_)
241 | StreamProto(_)
242 | MissingId(_)
243 | IdUnavailable(_)
244 | StreamIdZero => ErrorKind::InvalidData,
245
246 Bug(ref e) if e.kind() == tor_error::ErrorKind::BadApiUsage => ErrorKind::InvalidData,
247
248 IdRangeFull | CircRefused(_) | ResolveError(_) | Bug(_) => ErrorKind::Other,
249 };
250 std::io::Error::new(kind, err)
251 }
252}
253
254impl HasKind for Error {
255 fn kind(&self) -> ErrorKind {
256 use tor_bytes::Error as BytesError;
257 use Error as E;
258 use ErrorKind as EK;
259 match self {
260 E::BytesErr {
261 err: BytesError::Bug(e),
262 ..
263 } => e.kind(),
264 E::BytesErr { .. } => EK::TorProtocolViolation,
265 E::ChanIoErr(_) => EK::LocalNetworkError,
266 E::HandshakeIoErr(_) => EK::TorAccessFailed,
267 E::HandshakeCertErr(_) => EK::TorProtocolViolation,
268 E::CellEncodeErr { err, .. } => err.kind(),
269 E::CellDecodeErr { err, .. } => err.kind(),
270 E::EncodeErr { .. } => EK::BadApiUsage,
271 E::InvalidKDFOutputLength => EK::Internal,
272 E::NoSuchHop => EK::BadApiUsage,
273 E::BadCellAuth => EK::TorProtocolViolation,
274 E::BadCircHandshakeAuth => EK::TorProtocolViolation,
275 E::HandshakeProto(_) => EK::TorAccessFailed,
276 E::HandshakeCertsExpired { .. } => EK::ClockSkew,
277 E::ChanProto(_) => EK::TorProtocolViolation,
278 E::CircProto(_) => EK::TorProtocolViolation,
279 E::ChannelClosed(e) => e.kind(),
280 E::CircuitClosed => EK::CircuitCollapse,
281 E::IdRangeFull => EK::BadApiUsage,
282 E::CircRefused(_) => EK::CircuitRefused,
283 E::BadStreamAddress => EK::BadApiUsage,
284 E::EndReceived(reason) => reason.kind(),
285 E::NotConnected => EK::BadApiUsage,
286 E::StreamProto(_) => EK::TorProtocolViolation,
287 E::ChanMismatch(_) => EK::RelayIdMismatch,
288 E::ResolveError(ResolveError::Nontransient) => EK::RemoteHostNotFound,
289 E::ResolveError(ResolveError::Transient) => EK::RemoteHostResolutionFailed,
290 E::ResolveError(ResolveError::Unrecognized) => EK::RemoteHostResolutionFailed,
291 E::MissingId(_) => EK::BadApiUsage,
292 E::IdUnavailable(_) => EK::BadApiUsage,
293 E::StreamIdZero => EK::BadApiUsage,
294 E::Memquota(err) => err.kind(),
295 E::Bug(e) => e.kind(),
296 }
297 }
298}
299
300#[derive(Debug)]
303pub(crate) enum ReactorError {
304 Err(Error),
306 Shutdown,
308}
309
310impl From<Error> for ReactorError {
311 fn from(e: Error) -> ReactorError {
312 ReactorError::Err(e)
313 }
314}
315
316impl From<ChannelClosed> for ReactorError {
317 fn from(e: ChannelClosed) -> ReactorError {
318 ReactorError::Err(e.into())
319 }
320}
321
322impl From<Bug> for ReactorError {
323 fn from(e: Bug) -> ReactorError {
324 ReactorError::Err(e.into())
325 }
326}
327
328#[cfg(test)]
329impl ReactorError {
330 pub(crate) fn unwrap_err(self) -> Error {
332 match self {
333 ReactorError::Shutdown => panic!(),
334 ReactorError::Err(e) => e,
335 }
336 }
337}
338
339#[derive(Debug)]
341#[cfg(feature = "conflux")]
342#[allow(unused)] pub(crate) enum ConfluxHandshakeError {
344 Timeout,
346 Link(Error),
348 ChannelClosed,
350}