socks5_proto/
reply.rs

1/// SOCKS5 reply
2#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
3pub enum Reply {
4    Succeeded,
5    GeneralFailure,
6    ConnectionNotAllowed,
7    NetworkUnreachable,
8    HostUnreachable,
9    ConnectionRefused,
10    TtlExpired,
11    CommandNotSupported,
12    AddressTypeNotSupported,
13}
14
15impl Reply {
16    const SUCCEEDED: u8 = 0x00;
17    const GENERAL_FAILURE: u8 = 0x01;
18    const CONNECTION_NOT_ALLOWED: u8 = 0x02;
19    const NETWORK_UNREACHABLE: u8 = 0x03;
20    const HOST_UNREACHABLE: u8 = 0x04;
21    const CONNECTION_REFUSED: u8 = 0x05;
22    const TTL_EXPIRED: u8 = 0x06;
23    const COMMAND_NOT_SUPPORTED: u8 = 0x07;
24    const ADDRESS_TYPE_NOT_SUPPORTED: u8 = 0x08;
25}
26
27impl TryFrom<u8> for Reply {
28    type Error = u8;
29
30    fn try_from(code: u8) -> Result<Self, Self::Error> {
31        match code {
32            Self::SUCCEEDED => Ok(Self::Succeeded),
33            Self::GENERAL_FAILURE => Ok(Self::GeneralFailure),
34            Self::CONNECTION_NOT_ALLOWED => Ok(Self::ConnectionNotAllowed),
35            Self::NETWORK_UNREACHABLE => Ok(Self::NetworkUnreachable),
36            Self::HOST_UNREACHABLE => Ok(Self::HostUnreachable),
37            Self::CONNECTION_REFUSED => Ok(Self::ConnectionRefused),
38            Self::TTL_EXPIRED => Ok(Self::TtlExpired),
39            Self::COMMAND_NOT_SUPPORTED => Ok(Self::CommandNotSupported),
40            Self::ADDRESS_TYPE_NOT_SUPPORTED => Ok(Self::AddressTypeNotSupported),
41            code => Err(code),
42        }
43    }
44}
45
46impl From<Reply> for u8 {
47    fn from(reply: Reply) -> Self {
48        match reply {
49            Reply::Succeeded => Reply::SUCCEEDED,
50            Reply::GeneralFailure => Reply::GENERAL_FAILURE,
51            Reply::ConnectionNotAllowed => Reply::CONNECTION_NOT_ALLOWED,
52            Reply::NetworkUnreachable => Reply::NETWORK_UNREACHABLE,
53            Reply::HostUnreachable => Reply::HOST_UNREACHABLE,
54            Reply::ConnectionRefused => Reply::CONNECTION_REFUSED,
55            Reply::TtlExpired => Reply::TTL_EXPIRED,
56            Reply::CommandNotSupported => Reply::COMMAND_NOT_SUPPORTED,
57            Reply::AddressTypeNotSupported => Reply::ADDRESS_TYPE_NOT_SUPPORTED,
58        }
59    }
60}