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