socks5_client/error.rs
1// Set of libraries for privacy-preserving networking apps
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2023 by
6// Dr. Maxim Orlovsky <orlovsky@cyphernet.org>
7//
8// Copyright 2022-2023 Cyphernet DAO, Switzerland
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
23#[display(doc_comments)]
24#[repr(u8)]
25pub enum ServerError {
26 /// general SOCKS server failure
27 GeneralFailure = 1,
28 /// connection not allowed by ruleset
29 NotAllowed = 2,
30 /// network unreachable
31 NetworkUnreachable = 3,
32 /// host unreachable
33 HostUnreachable = 4,
34 /// connection refused
35 ConnectionRefused = 5,
36 /// TTL expired
37 TtlExpired = 6,
38 /// command not supported
39 CommandNotSupported = 7,
40 /// address kind not supported
41 AddressNotSupported = 8,
42 /// unknown error type
43 Unknown = 0xFF,
44}
45
46impl From<u8> for ServerError {
47 fn from(value: u8) -> Self {
48 const ALL: [ServerError; 9] = [
49 ServerError::GeneralFailure,
50 ServerError::NotAllowed,
51 ServerError::NetworkUnreachable,
52 ServerError::HostUnreachable,
53 ServerError::ConnectionRefused,
54 ServerError::TtlExpired,
55 ServerError::CommandNotSupported,
56 ServerError::AddressNotSupported,
57 ServerError::Unknown,
58 ];
59
60 for ty in ALL {
61 if ty as u8 == value {
62 return ty;
63 }
64 }
65
66 unreachable!()
67 }
68}