查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.dns;
17  
18  import io.netty.util.internal.UnstableApi;
19  
20  import static io.netty.util.internal.ObjectUtil.checkNotNull;
21  
22  /**
23   * The DNS {@code RCODE}, as defined in <a href="https://tools.ietf.org/html/rfc2929">RFC2929</a>.
24   */
25  @UnstableApi
26  public class DnsResponseCode implements Comparable<DnsResponseCode> {
27  
28      /**
29       * The 'NoError' DNS RCODE (0), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
30       */
31      public static final DnsResponseCode NOERROR = new DnsResponseCode(0, "NoError");
32  
33      /**
34       * The 'FormErr' DNS RCODE (1), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
35       */
36      public static final DnsResponseCode FORMERR = new DnsResponseCode(1, "FormErr");
37  
38      /**
39       * The 'ServFail' DNS RCODE (2), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
40       */
41      public static final DnsResponseCode SERVFAIL = new DnsResponseCode(2, "ServFail");
42  
43      /**
44       * The 'NXDomain' DNS RCODE (3), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
45       */
46      public static final DnsResponseCode NXDOMAIN = new DnsResponseCode(3, "NXDomain");
47  
48      /**
49       * The 'NotImp' DNS RCODE (4), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
50       */
51      public static final DnsResponseCode NOTIMP = new DnsResponseCode(4, "NotImp");
52  
53      /**
54       * The 'Refused' DNS RCODE (5), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
55       */
56      public static final DnsResponseCode REFUSED = new DnsResponseCode(5, "Refused");
57  
58      /**
59       * The 'YXDomain' DNS RCODE (6), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
60       */
61      public static final DnsResponseCode YXDOMAIN = new DnsResponseCode(6, "YXDomain");
62  
63      /**
64       * The 'YXRRSet' DNS RCODE (7), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
65       */
66      public static final DnsResponseCode YXRRSET = new DnsResponseCode(7, "YXRRSet");
67  
68      /**
69       * The 'NXRRSet' DNS RCODE (8), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
70       */
71      public static final DnsResponseCode NXRRSET = new DnsResponseCode(8, "NXRRSet");
72  
73      /**
74       * The 'NotAuth' DNS RCODE (9), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
75       */
76      public static final DnsResponseCode NOTAUTH = new DnsResponseCode(9, "NotAuth");
77  
78      /**
79       * The 'NotZone' DNS RCODE (10), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
80       */
81      public static final DnsResponseCode NOTZONE = new DnsResponseCode(10, "NotZone");
82  
83      /**
84       * The 'BADVERS' or 'BADSIG' DNS RCODE (16), as defined in <a href="https://tools.ietf.org/html/rfc2671">RFC2671</a>
85       * and <a href="https://tools.ietf.org/html/rfc2845">RFC2845</a>.
86       */
87      public static final DnsResponseCode BADVERS_OR_BADSIG = new DnsResponseCode(16, "BADVERS_OR_BADSIG");
88  
89      /**
90       * The 'BADKEY' DNS RCODE (17), as defined in <a href="https://tools.ietf.org/html/rfc2845">RFC2845</a>.
91       */
92      public static final DnsResponseCode BADKEY = new DnsResponseCode(17, "BADKEY");
93  
94      /**
95       * The 'BADTIME' DNS RCODE (18), as defined in <a href="https://tools.ietf.org/html/rfc2845">RFC2845</a>.
96       */
97      public static final DnsResponseCode BADTIME = new DnsResponseCode(18, "BADTIME");
98  
99      /**
100      * The 'BADMODE' DNS RCODE (19), as defined in <a href="https://tools.ietf.org/html/rfc2930">RFC2930</a>.
101      */
102     public static final DnsResponseCode BADMODE = new DnsResponseCode(19, "BADMODE");
103 
104     /**
105      * The 'BADNAME' DNS RCODE (20), as defined in <a href="https://tools.ietf.org/html/rfc2930">RFC2930</a>.
106      */
107     public static final DnsResponseCode BADNAME = new DnsResponseCode(20, "BADNAME");
108 
109     /**
110      * The 'BADALG' DNS RCODE (21), as defined in <a href="https://tools.ietf.org/html/rfc2930">RFC2930</a>.
111      */
112     public static final DnsResponseCode BADALG = new DnsResponseCode(21, "BADALG");
113 
114     /**
115      * Returns the {@link DnsResponseCode} that corresponds with the given {@code responseCode}.
116      *
117      * @param responseCode the DNS RCODE
118      *
119      * @return the corresponding {@link DnsResponseCode}
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 = checkNotNull(name, "name");
177     }
178 
179     /**
180      * Returns the error code for this {@link DnsResponseCode}.
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      * Equality of {@link DnsResponseCode} only depends on {@link #intValue()}.
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      * Returns a formatted error message for this {@link DnsResponseCode}.
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 }