View Javadoc
1   /*
2    * Copyright 2014 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  import io.netty.util.collection.IntObjectHashMap;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * Represents a DNS record type.
24   */
25  public final class DnsType implements Comparable<DnsType> {
26  
27      /**
28       * Address record RFC 1035 Returns a 32-bit IPv4 address, most commonly used
29       * to map hostnames to an IP address of the host, but also used for DNSBLs,
30       * storing subnet masks in RFC 1101, etc.
31       */
32      public static final DnsType A = new DnsType(0x0001, "A");
33  
34      /**
35       * Name server record RFC 1035 Delegates a DNS zone to use the given
36       * authoritative name servers
37       */
38      public static final DnsType NS = new DnsType(0x0002, "NS");
39  
40      /**
41       * Canonical name record RFC 1035 Alias of one name to another: the DNS
42       * lookup will continue by retrying the lookup with the new name.
43       */
44      public static final DnsType CNAME = new DnsType(0x0005, "CNAME");
45  
46      /**
47       * Start of [a zone of] authority record RFC 1035 and RFC 2308 Specifies
48       * authoritative information about a DNS zone, including the primary name
49       * server, the email of the domain administrator, the domain serial number,
50       * and several timers relating to refreshing the zone.
51       */
52      public static final DnsType SOA = new DnsType(0x0006, "SOA");
53  
54      /**
55       * Pointer record RFC 1035 Pointer to a canonical name. Unlike a CNAME, DNS
56       * processing does NOT proceed, just the name is returned. The most common
57       * use is for implementing reverse DNS lookups, but other uses include such
58       * things as DNS-SD.
59       */
60      public static final DnsType PTR = new DnsType(0x000c, "PTR");
61  
62      /**
63       * Mail exchange record RFC 1035 Maps a domain name to a list of message
64       * transfer agents for that domain.
65       */
66      public static final DnsType MX = new DnsType(0x000f, "MX");
67  
68      /**
69       * Text record RFC 1035 Originally for arbitrary human-readable text in a
70       * DNS record. Since the early 1990s, however, this record more often
71       * carries machine-readable data, such as specified by RFC 1464,
72       * opportunistic encryption, Sender Policy Framework, DKIM, DMARC DNS-SD,
73       * etc.
74       */
75      public static final DnsType TXT = new DnsType(0x0010, "TXT");
76  
77      /**
78       * Responsible person record RFC 1183 Information about the responsible
79       * person(s) for the domain. Usually an email address with the @ replaced by
80       * a .
81       */
82      public static final DnsType RP = new DnsType(0x0011, "RP");
83  
84      /**
85       * AFS database record RFC 1183 Location of database servers of an AFS cell.
86       * This record is commonly used by AFS clients to contact AFS cells outside
87       * their local domain. A subtype of this record is used by the obsolete
88       * DCE/DFS file system.
89       */
90      public static final DnsType AFSDB = new DnsType(0x0012, "AFSDB");
91  
92      /**
93       * Signature record RFC 2535 Signature record used in SIG(0) (RFC 2931) and
94       * TKEY (RFC 2930). RFC 3755 designated RRSIG as the replacement for SIG for
95       * use within DNSSEC.
96       */
97      public static final DnsType SIG = new DnsType(0x0018, "SIG");
98  
99      /**
100      * key record RFC 2535 and RFC 2930 Used only for SIG(0) (RFC 2931) and TKEY
101      * (RFC 2930). RFC 3445 eliminated their use for application keys and
102      * limited their use to DNSSEC. RFC 3755 designates DNSKEY as the
103      * replacement within DNSSEC. RFC 4025 designates IPSECKEY as the
104      * replacement for use with IPsec.
105      */
106     public static final DnsType KEY = new DnsType(0x0019, "KEY");
107 
108     /**
109      * IPv6 address record RFC 3596 Returns a 128-bit IPv6 address, most
110      * commonly used to map hostnames to an IP address of the host.
111      */
112     public static final DnsType AAAA = new DnsType(0x001c, "AAAA");
113 
114     /**
115      * Location record RFC 1876 Specifies a geographical location associated
116      * with a domain name.
117      */
118     public static final DnsType LOC = new DnsType(0x001d, "LOC");
119 
120     /**
121      * Service locator RFC 2782 Generalized service location record, used for
122      * newer protocols instead of creating protocol-specific records such as MX.
123      */
124     public static final DnsType SRV = new DnsType(0x0021, "SRV");
125 
126     /**
127      * Naming Authority Pointer record RFC 3403 Allows regular expression based
128      * rewriting of domain names which can then be used as URIs, further domain
129      * names to lookups, etc.
130      */
131     public static final DnsType NAPTR = new DnsType(0x0023, "NAPTR");
132 
133     /**
134      * Key eXchanger record RFC 2230 Used with some cryptographic systems (not
135      * including DNSSEC) to identify a key management agent for the associated
136      * domain-name. Note that this has nothing to do with DNS Security. It is
137      * Informational status, rather than being on the IETF standards-track. It
138      * has always had limited deployment, but is still in use.
139      */
140     public static final DnsType KX = new DnsType(0x0024, "KX");
141 
142     /**
143      * Certificate record RFC 4398 Stores PKIX, SPKI, PGP, etc.
144      */
145     public static final DnsType CERT = new DnsType(0x0025, "CERT");
146 
147     /**
148      * Delegation name record RFC 2672 DNAME creates an alias for a name and all
149      * its subnames, unlike CNAME, which aliases only the exact name in its
150      * label. Like the CNAME record, the DNS lookup will continue by retrying
151      * the lookup with the new name.
152      */
153     public static final DnsType DNAME = new DnsType(0x0027, "DNAME");
154 
155     /**
156      * Option record RFC 2671 This is a pseudo DNS record type needed to support
157      * EDNS.
158      */
159     public static final DnsType OPT = new DnsType(0x0029, "OPT");
160 
161     /**
162      * Address Prefix List record RFC 3123 Specify lists of address ranges, e.g.
163      * in CIDR format, for various address families. Experimental.
164      */
165     public static final DnsType APL = new DnsType(0x002a, "APL");
166 
167     /**
168      * Delegation signer record RFC 4034 The record used to identify the DNSSEC
169      * signing key of a delegated zone.
170      */
171     public static final DnsType DS = new DnsType(0x002b, "DS");
172 
173     /**
174      * SSH Public Key Fingerprint record RFC 4255 Resource record for publishing
175      * SSH public host key fingerprints in the DNS System, in order to aid in
176      * verifying the authenticity of the host. RFC 6594 defines ECC SSH keys and
177      * SHA-256 hashes. See the IANA SSHFP RR parameters registry for details.
178      */
179     public static final DnsType SSHFP = new DnsType(0x002c, "SSHFP");
180 
181     /**
182      * IPsec Key record RFC 4025 Key record that can be used with IPsec.
183      */
184     public static final DnsType IPSECKEY = new DnsType(0x002d, "IPSECKEY");
185 
186     /**
187      * DNSSEC signature record RFC 4034 Signature for a DNSSEC-secured record
188      * set. Uses the same format as the SIG record.
189      */
190     public static final DnsType RRSIG = new DnsType(0x002e, "RRSIG");
191 
192     /**
193      * Next-Secure record RFC 4034 Part of DNSSEC, used to prove a name does not
194      * exist. Uses the same format as the (obsolete) NXT record.
195      */
196     public static final DnsType NSEC = new DnsType(0x002f, "NSEC");
197 
198     /**
199      * DNS Key record RFC 4034 The key record used in DNSSEC. Uses the same
200      * format as the KEY record.
201      */
202     public static final DnsType DNSKEY = new DnsType(0x0030, "DNSKEY");
203 
204     /**
205      * DHCP identifier record RFC 4701 Used in conjunction with the FQDN option
206      * to DHCP.
207      */
208     public static final DnsType DHCID = new DnsType(0x0031, "DHCID");
209 
210     /**
211      * NSEC record version 3 RFC 5155 An extension to DNSSEC that allows proof
212      * of nonexistence for a name without permitting zonewalking.
213      */
214     public static final DnsType NSEC3 = new DnsType(0x0032, "NSEC3");
215 
216     /**
217      * NSEC3 parameters record RFC 5155 Parameter record for use with NSEC3.
218      */
219     public static final DnsType NSEC3PARAM = new DnsType(0x0033, "NSEC3PARAM");
220 
221     /**
222      * TLSA certificate association record RFC 6698 A record for DNS-based
223      * Authentication of Named Entities (DANE). RFC 6698 defines The TLSA DNS
224      * resource record is used to associate a TLS server certificate or public
225      * key with the domain name where the record is found, thus forming a 'TLSA
226      * certificate association'.
227      */
228     public static final DnsType TLSA = new DnsType(0x0034, "TLSA");
229 
230     /**
231      * Host Identity Protocol record RFC 5205 Method of separating the end-point
232      * identifier and locator roles of IP addresses.
233      */
234     public static final DnsType HIP = new DnsType(0x0037, "HIP");
235 
236     /**
237      * Sender Policy Framework record RFC 4408 Specified as part of the SPF
238      * protocol as an alternative to of storing SPF data in TXT records. Uses
239      * the same format as the earlier TXT record.
240      */
241     public static final DnsType SPF = new DnsType(0x0063, "SPF");
242 
243     /**
244      * Secret key record RFC 2930 A method of providing keying material to be
245      * used with TSIG that is encrypted under the public key in an accompanying
246      * KEY RR..
247      */
248     public static final DnsType TKEY = new DnsType(0x00f9, "TKEY");
249 
250     /**
251      * Transaction Signature record RFC 2845 Can be used to authenticate dynamic
252      * updates as coming from an approved client, or to authenticate responses
253      * as coming from an approved recursive name server similar to DNSSEC.
254      */
255     public static final DnsType TSIG = new DnsType(0x00fa, "TSIG");
256 
257     /**
258      * Incremental Zone Transfer record RFC 1996 Requests a zone transfer of the
259      * given zone but only differences from a previous serial number. This
260      * request may be ignored and a full (AXFR) sent in response if the
261      * authoritative server is unable to fulfill the request due to
262      * configuration or lack of required deltas.
263      */
264     public static final DnsType IXFR = new DnsType(0x00fb, "IXFR");
265 
266     /**
267      * Authoritative Zone Transfer record RFC 1035 Transfer entire zone file
268      * from the master name server to secondary name servers.
269      */
270     public static final DnsType AXFR = new DnsType(0x00fc, "AXFR");
271 
272     /**
273      * All cached records RFC 1035 Returns all records of all types known to the
274      * name server. If the name server does not have any information on the
275      * name, the request will be forwarded on. The records returned may not be
276      * complete. For example, if there is both an A and an MX for a name, but
277      * the name server has only the A record cached, only the A record will be
278      * returned. Sometimes referred to as ANY, for example in Windows nslookup
279      * and Wireshark.
280      */
281     public static final DnsType ANY = new DnsType(0x00ff, "ANY");
282 
283     /**
284      * Certification Authority Authorization record RFC 6844 CA pinning,
285      * constraining acceptable CAs for a host/domain.
286      */
287     public static final DnsType CAA = new DnsType(0x0101, "CAA");
288 
289     /**
290      * DNSSEC Trust Authorities record N/A Part of a deployment proposal for
291      * DNSSEC without a signed DNS root. See the IANA database and Weiler Spec
292      * for details. Uses the same format as the DS record.
293      */
294     public static final DnsType TA = new DnsType(0x8000, "TA");
295 
296     /**
297      * DNSSEC Lookaside Validation record RFC 4431 For publishing DNSSEC trust
298      * anchors outside of the DNS delegation chain. Uses the same format as the
299      * DS record. RFC 5074 describes a way of using these records.
300      */
301     public static final DnsType DLV = new DnsType(0x8001, "DLV");
302 
303     private static final Map<String, DnsType> BY_NAME = new HashMap<String, DnsType>();
304     private static final IntObjectHashMap<DnsType> BY_TYPE = new IntObjectHashMap<DnsType>();
305     private static final String EXPECTED;
306 
307     static {
308         DnsType[] all = {
309                 A, NS, CNAME, SOA, PTR, MX, TXT, RP, AFSDB, SIG, KEY, AAAA, LOC, SRV, NAPTR, KX, CERT, DNAME, OPT, APL,
310                 DS, SSHFP, IPSECKEY, RRSIG, NSEC, DNSKEY, DHCID, NSEC3, NSEC3PARAM, TLSA, HIP, SPF, TKEY, TSIG, IXFR,
311                 AXFR, ANY, CAA, TA, DLV
312         };
313 
314         StringBuilder expected = new StringBuilder(512);
315         expected.append(" (expected: ");
316 
317         for (DnsType type: all) {
318             BY_NAME.put(type.name(), type);
319             BY_TYPE.put(type.intValue(), type);
320             expected.append(type.name());
321             expected.append('(');
322             expected.append(type.intValue());
323             expected.append("), ");
324         }
325 
326         expected.setLength(expected.length() - 2);
327         expected.append(')');
328         EXPECTED = expected.toString();
329     }
330 
331     public static DnsType valueOf(int intValue) {
332         DnsType result = BY_TYPE.get(intValue);
333         if (result == null) {
334             return new DnsType(intValue, "UNKNOWN");
335         }
336         return result;
337     }
338 
339     public static DnsType valueOf(String name) {
340         DnsType result = BY_NAME.get(name);
341         if (result == null) {
342             throw new IllegalArgumentException("name: " + name + EXPECTED);
343         }
344         return result;
345     }
346 
347     /**
348      * Returns a new instance.
349      */
350     public static DnsType valueOf(int intValue, String name) {
351         return new DnsType(intValue, name);
352     }
353 
354     private final int intValue;
355     private final String name;
356 
357     private DnsType(int intValue, String name) {
358         if ((intValue & 0xffff) != intValue) {
359             throw new IllegalArgumentException("intValue: " + intValue + " (expected: 0 ~ 65535)");
360         }
361         this.intValue = intValue;
362         this.name = name;
363     }
364 
365     /**
366      * Returns the name of this type, as seen in bind config files
367      */
368     public String name() {
369         return name;
370     }
371 
372     /**
373      * Returns the value of this DnsType as it appears in DNS protocol
374      */
375     public int intValue() {
376         return intValue;
377     }
378 
379     @Override
380     public int hashCode() {
381         return intValue;
382     }
383 
384     @Override
385     public boolean equals(Object o) {
386         return o instanceof DnsType && ((DnsType) o).intValue == intValue;
387     }
388 
389     @Override
390     public int compareTo(DnsType o) {
391         return intValue() - o.intValue();
392     }
393 
394     @Override
395     public String toString() {
396         return name;
397     }
398 }