socks5_proto/
error.rs

1//! Error types for the SOCKS5 protocol
2
3use crate::{handshake::Method, Command, Reply};
4use std::io::{Error as IoError, ErrorKind};
5use thiserror::Error;
6
7/// Errors may occured during protocol header parsing
8///
9/// Since the process of parsing the protocol header follows certain steps, some sub-types contain other previously parsed data for better error reporting.
10#[derive(Debug, Error)]
11pub enum ProtocolError {
12    #[error("Unsupported SOCKS version {version:#04x}")]
13    ProtocolVersion { version: u8 },
14
15    #[error("No acceptable handshake method")]
16    NoAcceptableHandshakeMethod {
17        version: u8,
18        chosen_method: Method,
19        methods: Vec<Method>,
20    },
21
22    #[error("Unsupported command {command:#04x}")]
23    InvalidCommand { version: u8, command: u8 },
24
25    #[error("Unsupported reply {reply:#04x}")]
26    InvalidReply { version: u8, reply: u8 },
27
28    #[error("Unsupported address type in request {address_type:#04x}")]
29    InvalidAddressTypeInRequest {
30        version: u8,
31        command: Command,
32        address_type: u8,
33    },
34
35    #[error("Unsupported address type in response {address_type:#04x}")]
36    InvalidAddressTypeInResponse {
37        version: u8,
38        reply: Reply,
39        address_type: u8,
40    },
41
42    #[error("Unsupported address type in UDP Header {address_type:#04x}")]
43    InvalidAddressTypeInUdpHeader { frag: u8, address_type: u8 },
44}
45
46impl From<ProtocolError> for IoError {
47    fn from(err: ProtocolError) -> Self {
48        IoError::new(ErrorKind::Other, err)
49    }
50}
51
52/// Converging error types
53#[derive(Debug, Error)]
54pub enum Error {
55    #[error(transparent)]
56    Protocol(#[from] ProtocolError),
57    #[error(transparent)]
58    Io(#[from] IoError),
59}
60
61impl From<Error> for IoError {
62    fn from(err: Error) -> Self {
63        match err {
64            Error::Io(err) => err,
65            err => IoError::new(ErrorKind::Other, err),
66        }
67    }
68}