1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.socksx.v5;
18
19 import io.netty.util.internal.ObjectUtil;
20
21
22
23
24 public class Socks5CommandStatus implements Comparable<Socks5CommandStatus> {
25
26 public static final Socks5CommandStatus SUCCESS = new Socks5CommandStatus(0x00, "SUCCESS");
27 public static final Socks5CommandStatus FAILURE = new Socks5CommandStatus(0x01, "FAILURE");
28 public static final Socks5CommandStatus FORBIDDEN = new Socks5CommandStatus(0x02, "FORBIDDEN");
29 public static final Socks5CommandStatus NETWORK_UNREACHABLE = new Socks5CommandStatus(0x03, "NETWORK_UNREACHABLE");
30 public static final Socks5CommandStatus HOST_UNREACHABLE = new Socks5CommandStatus(0x04, "HOST_UNREACHABLE");
31 public static final Socks5CommandStatus CONNECTION_REFUSED = new Socks5CommandStatus(0x05, "CONNECTION_REFUSED");
32 public static final Socks5CommandStatus TTL_EXPIRED = new Socks5CommandStatus(0x06, "TTL_EXPIRED");
33 public static final Socks5CommandStatus COMMAND_UNSUPPORTED = new Socks5CommandStatus(0x07, "COMMAND_UNSUPPORTED");
34 public static final Socks5CommandStatus ADDRESS_UNSUPPORTED = new Socks5CommandStatus(0x08, "ADDRESS_UNSUPPORTED");
35
36 public static Socks5CommandStatus valueOf(byte b) {
37 switch (b) {
38 case 0x00:
39 return SUCCESS;
40 case 0x01:
41 return FAILURE;
42 case 0x02:
43 return FORBIDDEN;
44 case 0x03:
45 return NETWORK_UNREACHABLE;
46 case 0x04:
47 return HOST_UNREACHABLE;
48 case 0x05:
49 return CONNECTION_REFUSED;
50 case 0x06:
51 return TTL_EXPIRED;
52 case 0x07:
53 return COMMAND_UNSUPPORTED;
54 case 0x08:
55 return ADDRESS_UNSUPPORTED;
56 }
57
58 return new Socks5CommandStatus(b);
59 }
60
61 private final byte byteValue;
62 private final String name;
63 private String text;
64
65 public Socks5CommandStatus(int byteValue) {
66 this(byteValue, "UNKNOWN");
67 }
68
69 public Socks5CommandStatus(int byteValue, String name) {
70 this.name = ObjectUtil.checkNotNull(name, "name");
71 this.byteValue = (byte) byteValue;
72 }
73
74 public byte byteValue() {
75 return byteValue;
76 }
77
78 public boolean isSuccess() {
79 return byteValue == 0;
80 }
81
82 @Override
83 public int hashCode() {
84 return byteValue;
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (!(obj instanceof Socks5CommandStatus)) {
90 return false;
91 }
92
93 return byteValue == ((Socks5CommandStatus) obj).byteValue;
94 }
95
96 @Override
97 public int compareTo(Socks5CommandStatus o) {
98 return byteValue - o.byteValue;
99 }
100
101 @Override
102 public String toString() {
103 String text = this.text;
104 if (text == null) {
105 this.text = text = name + '(' + (byteValue & 0xFF) + ')';
106 }
107 return text;
108 }
109 }