View Javadoc
1   /*
2    * Copyright 2013 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    *   http://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  /**
19   * Represents the possible response codes a server may send after receiving a
20   * query. A response code of 0 indicates no error.
21   */
22  public final class DnsResponseCode implements Comparable<DnsResponseCode> {
23  
24      /**
25       * ID 0, no error
26       */
27      public static final DnsResponseCode NOERROR = new DnsResponseCode(0, "no error");
28  
29      /**
30       * ID 1, format error
31       */
32      public static final DnsResponseCode FORMERROR = new DnsResponseCode(1, "format error");
33  
34      /**
35       * ID 2, server failure
36       */
37      public static final DnsResponseCode SERVFAIL = new DnsResponseCode(2, "server failure");
38  
39      /**
40       * ID 3, name error
41       */
42      public static final DnsResponseCode NXDOMAIN = new DnsResponseCode(3, "name error");
43  
44      /**
45       * ID 4, not implemented
46       */
47      public static final DnsResponseCode NOTIMPL = new DnsResponseCode(4, "not implemented");
48  
49      /**
50       * ID 5, operation refused
51       */
52      public static final DnsResponseCode REFUSED = new DnsResponseCode(5, "operation refused");
53  
54      /**
55       * ID 6, domain name should not exist
56       */
57      public static final DnsResponseCode YXDOMAIN = new DnsResponseCode(6, "domain name should not exist");
58  
59      /**
60       * ID 7, resource record set should not exist
61       */
62      public static final DnsResponseCode YXRRSET = new DnsResponseCode(7, "resource record set should not exist");
63  
64      /**
65       * ID 8, rrset does not exist
66       */
67      public static final DnsResponseCode NXRRSET = new DnsResponseCode(8, "rrset does not exist");
68  
69      /**
70       * ID 9, not authoritative for zone
71       */
72      public static final DnsResponseCode NOTAUTH = new DnsResponseCode(9, "not authoritative for zone");
73  
74      /**
75       * ID 10, name not in zone
76       */
77      public static final DnsResponseCode NOTZONE = new DnsResponseCode(10, "name not in zone");
78  
79      /**
80       * ID 11, bad extension mechanism for version
81       */
82      public static final DnsResponseCode BADVERS = new DnsResponseCode(11, "bad extension mechanism for version");
83  
84      /**
85       * ID 12, bad signature
86       */
87      public static final DnsResponseCode BADSIG = new DnsResponseCode(12, "bad signature");
88  
89      /**
90       * ID 13, bad key
91       */
92      public static final DnsResponseCode BADKEY = new DnsResponseCode(13, "bad key");
93  
94      /**
95       * ID 14, bad timestamp
96       */
97      public static final DnsResponseCode BADTIME = new DnsResponseCode(14, "bad timestamp");
98  
99      private final int errorCode;
100     private final String message;
101 
102     /**
103      * Returns the {@link DnsResponseCode} that corresponds with the given
104      * {@code responseCode}.
105      *
106      * @param responseCode
107      *            the error code's id
108      * @return corresponding {@link DnsResponseCode} or {@code null} if none can be found.
109      */
110     public static DnsResponseCode valueOf(int responseCode) {
111         switch (responseCode) {
112             case 0:
113                 return NOERROR;
114             case 1:
115                 return FORMERROR;
116             case 2:
117                 return SERVFAIL;
118             case 3:
119                 return NXDOMAIN;
120             case 4:
121                 return NOTIMPL;
122             case 5:
123                 return REFUSED;
124             case 6:
125                 return YXDOMAIN;
126             case 7:
127                 return YXRRSET;
128             case 8:
129                 return NXRRSET;
130             case 9:
131                 return NOTAUTH;
132             case 10:
133                 return NOTZONE;
134             case 11:
135                 return BADVERS;
136             case 12:
137                 return BADSIG;
138             case 13:
139                 return BADKEY;
140             case 14:
141                 return BADTIME;
142             default:
143                 return new DnsResponseCode(responseCode, null);
144         }
145     }
146 
147     public DnsResponseCode(int errorCode, String message) {
148         this.errorCode = errorCode;
149         this.message = message;
150     }
151 
152     /**
153      * Returns the error code for this {@link DnsResponseCode}.
154      */
155     public int code() {
156         return errorCode;
157     }
158 
159     @Override
160     public int compareTo(DnsResponseCode o) {
161         return code() - o.code();
162     }
163 
164     @Override
165     public int hashCode() {
166         return code();
167     }
168 
169     /**
170      * Equality of {@link DnsResponseCode} only depends on {@link #code()}.
171      */
172     @Override
173     public boolean equals(Object o) {
174         if (!(o instanceof DnsResponseCode)) {
175             return false;
176         }
177 
178         return code() == ((DnsResponseCode) o).code();
179     }
180 
181     /**
182      * Returns a formatted error message for this {@link DnsResponseCode}.
183      */
184     @Override
185     public String toString() {
186         if (message == null) {
187             return "DnsResponseCode(" + errorCode + ')';
188         }
189         return "DnsResponseCode(" + errorCode + ", " + message + ')';
190     }
191 }