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    *   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.internal.StringUtil;
19  import io.netty.util.internal.UnstableApi;
20  
21  import java.net.IDN;
22  
23  import static io.netty.util.internal.ObjectUtil.checkNotNull;
24  
25  /**
26   * A skeletal implementation of {@link DnsRecord}.
27   */
28  @UnstableApi
29  public abstract class AbstractDnsRecord implements DnsRecord {
30  
31      private final String name;
32      private final DnsRecordType type;
33      private final short dnsClass;
34      private final long timeToLive;
35      private int hashCode;
36  
37      /**
38       * Creates a new {@link #CLASS_IN IN-class} record.
39       *
40       * @param name the domain name
41       * @param type the type of the record
42       * @param timeToLive the TTL value of the record
43       */
44      protected AbstractDnsRecord(String name, DnsRecordType type, long timeToLive) {
45          this(name, type, CLASS_IN, timeToLive);
46      }
47  
48      /**
49       * Creates a new record.
50       *
51       * @param name the domain name
52       * @param type the type of the record
53       * @param dnsClass the class of the record, usually one of the following:
54       *                 <ul>
55       *                     <li>{@link #CLASS_IN}</li>
56       *                     <li>{@link #CLASS_CSNET}</li>
57       *                     <li>{@link #CLASS_CHAOS}</li>
58       *                     <li>{@link #CLASS_HESIOD}</li>
59       *                     <li>{@link #CLASS_NONE}</li>
60       *                     <li>{@link #CLASS_ANY}</li>
61       *                 </ul>
62       * @param timeToLive the TTL value of the record
63       */
64      protected AbstractDnsRecord(String name, DnsRecordType type, int dnsClass, long timeToLive) {
65          if (timeToLive < 0) {
66              throw new IllegalArgumentException("timeToLive: " + timeToLive + " (expected: >= 0)");
67          }
68          // Convert to ASCII which will also check that the length is not too big.
69          // See:
70          //   - https://github.com/netty/netty/issues/4937
71          //   - https://github.com/netty/netty/issues/4935
72          this.name = appendTrailingDot(IDN.toASCII(checkNotNull(name, "name")));
73          this.type = checkNotNull(type, "type");
74          this.dnsClass = (short) dnsClass;
75          this.timeToLive = timeToLive;
76      }
77  
78      private static String appendTrailingDot(String name) {
79          if (name.length() > 0 && name.charAt(name.length() - 1) != '.') {
80              return name + '.';
81          }
82          return name;
83      }
84  
85      @Override
86      public String name() {
87          return name;
88      }
89  
90      @Override
91      public DnsRecordType type() {
92          return type;
93      }
94  
95      @Override
96      public int dnsClass() {
97          return dnsClass & 0xFFFF;
98      }
99  
100     @Override
101     public long timeToLive() {
102         return timeToLive;
103     }
104 
105     @Override
106     public boolean equals(Object obj) {
107         if (this == obj) {
108             return true;
109         }
110 
111         if (!(obj instanceof DnsRecord)) {
112             return false;
113         }
114 
115         final DnsRecord that = (DnsRecord) obj;
116         final int hashCode = this.hashCode;
117         if (hashCode != 0 && hashCode != that.hashCode()) {
118             return false;
119         }
120 
121         return type().intValue() == that.type().intValue() &&
122                dnsClass() == that.dnsClass() &&
123                name().equals(that.name());
124     }
125 
126     @Override
127     public int hashCode() {
128         final int hashCode = this.hashCode;
129         if (hashCode != 0) {
130             return hashCode;
131         }
132 
133         return this.hashCode = name.hashCode() * 31 + type().intValue() * 31 + dnsClass();
134     }
135 
136     @Override
137     public String toString() {
138         StringBuilder buf = new StringBuilder(64);
139 
140         buf.append(StringUtil.simpleClassName(this))
141            .append('(')
142            .append(name())
143            .append(' ')
144            .append(timeToLive())
145            .append(' ');
146 
147         DnsMessageUtil.appendRecordClass(buf, dnsClass())
148                       .append(' ')
149                       .append(type().name())
150                       .append(')');
151 
152         return buf.toString();
153     }
154 }