1
//! Declare an error type for tor_socksproto
2
use std::borrow::Cow;
3

            
4
use thiserror::Error;
5

            
6
use tor_error::{ErrorKind, HasKind};
7

            
8
/// An error that occurs while negotiating a SOCKS handshake.
9
#[derive(Clone, Error, Debug)]
10
#[non_exhaustive]
11
pub enum Error {
12
    /// The SOCKS client didn't implement SOCKS correctly.
13
    ///
14
    /// (Or, more likely, we didn't account for its behavior.)
15
    #[error("SOCKS protocol syntax violation")]
16
    Syntax,
17

            
18
    /// Failed to decode a SOCKS message.
19
    #[error("Error decoding SOCKS message")]
20
    Decode(#[from] tor_bytes::Error),
21

            
22
    /// The SOCKS client declared a SOCKS version number that isn't
23
    /// one we support.
24
    ///
25
    /// In all likelihood, this is somebody trying to use the port for
26
    /// some protocol other than SOCKS.
27
    //
28
    // Note: This is not the appropriate error for cases where the
29
    // client has changed its declared version
30
    // between the initial message and some other message:
31
    // those cases should just be `Syntax`.
32
    #[error("Unrecognized SOCKS protocol version {0}")]
33
    BadProtocol(u8),
34

            
35
    /// The SOCKS client tried to use a SOCKS feature that we don't
36
    /// support at all.
37
    #[error("SOCKS feature ({0}) not implemented")]
38
    NotImplemented(Cow<'static, str>),
39

            
40
    /// Tried to progress the SOCKS handshake when it was already
41
    /// finished.  This is a programming error.
42
    #[error("SOCKS handshake was finished; no need to call this again")]
43
    AlreadyFinished(tor_error::Bug),
44

            
45
    /// The SOCKS proxy refused our authentication.
46
    #[error("SOCKS Authentication failed")]
47
    AuthRejected,
48

            
49
    /// During the protocol exchange, we needed to handle a handshake bigger than our buffer
50
    #[error("SOCKS protocol message size limit {limit} exceeded")]
51
    MessageTooLong {
52
        /// The limit in bytes
53
        limit: usize,
54
    },
55

            
56
    /// Peer closed connection during SOCKS handshake
57
    #[error("peer closed connection during SOCKS handshake")]
58
    UnexpectedEof,
59

            
60
    /// The peer sent payload data too early
61
    ///
62
    /// The peer sent data after its part of the protocol exchange,
63
    /// without waiting for our side of it to complete,
64
    /// in circumstances where we consider that a protocol violation by the peer.
65
    ///
66
    /// Returned only by
67
    /// [`Finished::into_output_forbid_pipelining`](crate::Finished::into_output_forbid_pipelining).
68
    #[error("SOCKS peer inappropriately pipelined (optimistically sent) payload data")]
69
    ForbiddenPipelining,
70

            
71
    /// The program (perhaps this module, perhaps Arti, perhaps the caller) is buggy
72
    #[error("Bug while handling SOCKS handshake")]
73
    Bug(#[from] tor_error::Bug),
74
}
75

            
76
// Note: at present, tor-socksproto isn't used in any settings where ErrorKind
77
// is used.  This is provided for future-proofing, since someday we'll want to
78
// have SOCKS protocol support internally as well as in the `arti` proxy.
79
impl HasKind for Error {
80
    fn kind(&self) -> ErrorKind {
81
        use Error as E;
82
        use ErrorKind as EK;
83
        match self {
84
            E::Decode(tor_bytes::Error::Incomplete { .. }) => {
85
                // This variant should always get converted before a user can
86
                // see it.
87
                EK::Internal
88
            }
89
            E::Syntax | E::Decode(_) | E::BadProtocol(_) => EK::LocalProtocolViolation,
90
            E::NotImplemented(_) => EK::NotImplemented,
91
            E::AuthRejected => EK::LocalProtocolViolation,
92
            E::UnexpectedEof => EK::LocalProtocolViolation,
93
            E::ForbiddenPipelining => EK::LocalProtocolViolation,
94
            E::MessageTooLong { .. } => EK::Internal, // We should select a buffer big enough!
95
            E::AlreadyFinished(e) => e.kind(),
96
            E::Bug(e) => e.kind(),
97
        }
98
    }
99
}