1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.dns;
17
18 import static io.netty.util.internal.ObjectUtil.checkNotNull;
19
20
21
22
23 public class DnsResponseCode implements Comparable<DnsResponseCode> {
24
25
26
27
28 public static final DnsResponseCode NOERROR = new DnsResponseCode(0, "NoError");
29
30
31
32
33 public static final DnsResponseCode FORMERR = new DnsResponseCode(1, "FormErr");
34
35
36
37
38 public static final DnsResponseCode SERVFAIL = new DnsResponseCode(2, "ServFail");
39
40
41
42
43 public static final DnsResponseCode NXDOMAIN = new DnsResponseCode(3, "NXDomain");
44
45
46
47
48 public static final DnsResponseCode NOTIMP = new DnsResponseCode(4, "NotImp");
49
50
51
52
53 public static final DnsResponseCode REFUSED = new DnsResponseCode(5, "Refused");
54
55
56
57
58 public static final DnsResponseCode YXDOMAIN = new DnsResponseCode(6, "YXDomain");
59
60
61
62
63 public static final DnsResponseCode YXRRSET = new DnsResponseCode(7, "YXRRSet");
64
65
66
67
68 public static final DnsResponseCode NXRRSET = new DnsResponseCode(8, "NXRRSet");
69
70
71
72
73 public static final DnsResponseCode NOTAUTH = new DnsResponseCode(9, "NotAuth");
74
75
76
77
78 public static final DnsResponseCode NOTZONE = new DnsResponseCode(10, "NotZone");
79
80
81
82
83
84 public static final DnsResponseCode BADVERS_OR_BADSIG = new DnsResponseCode(16, "BADVERS_OR_BADSIG");
85
86
87
88
89 public static final DnsResponseCode BADKEY = new DnsResponseCode(17, "BADKEY");
90
91
92
93
94 public static final DnsResponseCode BADTIME = new DnsResponseCode(18, "BADTIME");
95
96
97
98
99 public static final DnsResponseCode BADMODE = new DnsResponseCode(19, "BADMODE");
100
101
102
103
104 public static final DnsResponseCode BADNAME = new DnsResponseCode(20, "BADNAME");
105
106
107
108
109 public static final DnsResponseCode BADALG = new DnsResponseCode(21, "BADALG");
110
111
112
113
114
115
116
117
118 public static DnsResponseCode valueOf(int responseCode) {
119 switch (responseCode) {
120 case 0:
121 return NOERROR;
122 case 1:
123 return FORMERR;
124 case 2:
125 return SERVFAIL;
126 case 3:
127 return NXDOMAIN;
128 case 4:
129 return NOTIMP;
130 case 5:
131 return REFUSED;
132 case 6:
133 return YXDOMAIN;
134 case 7:
135 return YXRRSET;
136 case 8:
137 return NXRRSET;
138 case 9:
139 return NOTAUTH;
140 case 10:
141 return NOTZONE;
142 case 16:
143 return BADVERS_OR_BADSIG;
144 case 17:
145 return BADKEY;
146 case 18:
147 return BADTIME;
148 case 19:
149 return BADMODE;
150 case 20:
151 return BADNAME;
152 case 21:
153 return BADALG;
154 default:
155 return new DnsResponseCode(responseCode);
156 }
157 }
158
159 private final int code;
160 private final String name;
161 private String text;
162
163 private DnsResponseCode(int code) {
164 this(code, "UNKNOWN");
165 }
166
167 public DnsResponseCode(int code, String name) {
168 if (code < 0 || code > 65535) {
169 throw new IllegalArgumentException("code: " + code + " (expected: 0 ~ 65535)");
170 }
171
172 this.code = code;
173 this.name = checkNotNull(name, "name");
174 }
175
176
177
178
179 public int intValue() {
180 return code;
181 }
182
183 @Override
184 public int compareTo(DnsResponseCode o) {
185 return intValue() - o.intValue();
186 }
187
188 @Override
189 public int hashCode() {
190 return intValue();
191 }
192
193
194
195
196 @Override
197 public boolean equals(Object o) {
198 if (!(o instanceof DnsResponseCode)) {
199 return false;
200 }
201
202 return intValue() == ((DnsResponseCode) o).intValue();
203 }
204
205
206
207
208 @Override
209 public String toString() {
210 String text = this.text;
211 if (text == null) {
212 this.text = text = name + '(' + intValue() + ')';
213 }
214 return text;
215 }
216 }