1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
40
41
42
43
44
45 protected AbstractDnsRecord(String name, DnsRecordType type, long timeToLive) {
46 this(name, type, CLASS_IN, timeToLive);
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 protected AbstractDnsRecord(String name, DnsRecordType type, int dnsClass, long timeToLive) {
66 checkPositiveOrZero(timeToLive, "timeToLive");
67
68
69
70
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
81
82
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 }