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