View Javadoc
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 static io.netty.util.internal.ObjectUtil.checkNotNull;
19  
20  /**
21   * The DNS {@code RCODE}, as defined in <a href="https://tools.ietf.org/html/rfc2929">RFC2929</a>.
22   */
23  public class DnsResponseCode implements Comparable<DnsResponseCode> {
24  
25      /**
26       * The 'NoError' DNS RCODE (0), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
27       */
28      public static final DnsResponseCode NOERROR = new DnsResponseCode(0, "NoError");
29  
30      /**
31       * The 'FormErr' DNS RCODE (1), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
32       */
33      public static final DnsResponseCode FORMERR = new DnsResponseCode(1, "FormErr");
34  
35      /**
36       * The 'ServFail' DNS RCODE (2), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
37       */
38      public static final DnsResponseCode SERVFAIL = new DnsResponseCode(2, "ServFail");
39  
40      /**
41       * The 'NXDomain' DNS RCODE (3), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
42       */
43      public static final DnsResponseCode NXDOMAIN = new DnsResponseCode(3, "NXDomain");
44  
45      /**
46       * The 'NotImp' DNS RCODE (4), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
47       */
48      public static final DnsResponseCode NOTIMP = new DnsResponseCode(4, "NotImp");
49  
50      /**
51       * The 'Refused' DNS RCODE (5), as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
52       */
53      public static final DnsResponseCode REFUSED = new DnsResponseCode(5, "Refused");
54  
55      /**
56       * The 'YXDomain' DNS RCODE (6), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
57       */
58      public static final DnsResponseCode YXDOMAIN = new DnsResponseCode(6, "YXDomain");
59  
60      /**
61       * The 'YXRRSet' DNS RCODE (7), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
62       */
63      public static final DnsResponseCode YXRRSET = new DnsResponseCode(7, "YXRRSet");
64  
65      /**
66       * The 'NXRRSet' DNS RCODE (8), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
67       */
68      public static final DnsResponseCode NXRRSET = new DnsResponseCode(8, "NXRRSet");
69  
70      /**
71       * The 'NotAuth' DNS RCODE (9), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
72       */
73      public static final DnsResponseCode NOTAUTH = new DnsResponseCode(9, "NotAuth");
74  
75      /**
76       * The 'NotZone' DNS RCODE (10), as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
77       */
78      public static final DnsResponseCode NOTZONE = new DnsResponseCode(10, "NotZone");
79  
80      /**
81       * The 'BADVERS' or 'BADSIG' DNS RCODE (16), as defined in <a href="https://tools.ietf.org/html/rfc2671">RFC2671</a>
82       * and <a href="https://tools.ietf.org/html/rfc2845">RFC2845</a>.
83       */
84      public static final DnsResponseCode BADVERS_OR_BADSIG = new DnsResponseCode(16, "BADVERS_OR_BADSIG");
85  
86      /**
87       * The 'BADKEY' DNS RCODE (17), as defined in <a href="https://tools.ietf.org/html/rfc2845">RFC2845</a>.
88       */
89      public static final DnsResponseCode BADKEY = new DnsResponseCode(17, "BADKEY");
90  
91      /**
92       * The 'BADTIME' DNS RCODE (18), as defined in <a href="https://tools.ietf.org/html/rfc2845">RFC2845</a>.
93       */
94      public static final DnsResponseCode BADTIME = new DnsResponseCode(18, "BADTIME");
95  
96      /**
97       * The 'BADMODE' DNS RCODE (19), as defined in <a href="https://tools.ietf.org/html/rfc2930">RFC2930</a>.
98       */
99      public static final DnsResponseCode BADMODE = new DnsResponseCode(19, "BADMODE");
100 
101     /**
102      * The 'BADNAME' DNS RCODE (20), as defined in <a href="https://tools.ietf.org/html/rfc2930">RFC2930</a>.
103      */
104     public static final DnsResponseCode BADNAME = new DnsResponseCode(20, "BADNAME");
105 
106     /**
107      * The 'BADALG' DNS RCODE (21), as defined in <a href="https://tools.ietf.org/html/rfc2930">RFC2930</a>.
108      */
109     public static final DnsResponseCode BADALG = new DnsResponseCode(21, "BADALG");
110 
111     /**
112      * Returns the {@link DnsResponseCode} that corresponds with the given {@code responseCode}.
113      *
114      * @param responseCode the DNS RCODE
115      *
116      * @return the corresponding {@link DnsResponseCode}
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      * Returns the error code for this {@link DnsResponseCode}.
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      * Equality of {@link DnsResponseCode} only depends on {@link #intValue()}.
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      * Returns a formatted error message for this {@link DnsResponseCode}.
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 }