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 import io.netty.util.internal.UnstableApi;
21
22 import java.net.IDN;
23
24 import static io.netty.util.internal.ObjectUtil.checkNotNull;
25 import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
26
27
28
29
30 @UnstableApi
31 public abstract class AbstractDnsRecord implements DnsRecord {
32
33 private final String name;
34 private final DnsRecordType type;
35 private final short dnsClass;
36 private final long timeToLive;
37 private int hashCode;
38
39
40
41
42
43
44
45
46 protected AbstractDnsRecord(String name, DnsRecordType type, long timeToLive) {
47 this(name, type, CLASS_IN, timeToLive);
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 protected AbstractDnsRecord(String name, DnsRecordType type, int dnsClass, long timeToLive) {
67 checkPositiveOrZero(timeToLive, "timeToLive");
68
69
70
71
72 this.name = appendTrailingDot(IDNtoASCII(name));
73 this.type = checkNotNull(type, "type");
74 this.dnsClass = (short) dnsClass;
75 this.timeToLive = timeToLive;
76 }
77
78 private static String IDNtoASCII(String name) {
79 checkNotNull(name, "name");
80 if (PlatformDependent.isAndroid() && DefaultDnsRecordDecoder.ROOT.equals(name)) {
81
82
83
84 return name;
85 }
86 return IDN.toASCII(name);
87 }
88
89 private static String appendTrailingDot(String name) {
90 if (name.length() > 0 && name.charAt(name.length() - 1) != '.') {
91 return name + '.';
92 }
93 return name;
94 }
95
96 @Override
97 public String name() {
98 return name;
99 }
100
101 @Override
102 public DnsRecordType type() {
103 return type;
104 }
105
106 @Override
107 public int dnsClass() {
108 return dnsClass & 0xFFFF;
109 }
110
111 @Override
112 public long timeToLive() {
113 return timeToLive;
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121
122 if (!(obj instanceof DnsRecord)) {
123 return false;
124 }
125
126 final DnsRecord that = (DnsRecord) obj;
127 final int hashCode = this.hashCode;
128 if (hashCode != 0 && hashCode != that.hashCode()) {
129 return false;
130 }
131
132 return type().intValue() == that.type().intValue() &&
133 dnsClass() == that.dnsClass() &&
134 name().equals(that.name());
135 }
136
137 @Override
138 public int hashCode() {
139 final int hashCode = this.hashCode;
140 if (hashCode != 0) {
141 return hashCode;
142 }
143
144 return this.hashCode = name.hashCode() * 31 + type().intValue() * 31 + dnsClass();
145 }
146
147 @Override
148 public String toString() {
149 StringBuilder buf = new StringBuilder(64);
150
151 buf.append(StringUtil.simpleClassName(this))
152 .append('(')
153 .append(name())
154 .append(' ')
155 .append(timeToLive())
156 .append(' ');
157
158 DnsMessageUtil.appendRecordClass(buf, dnsClass())
159 .append(' ')
160 .append(type().name())
161 .append(')');
162
163 return buf.toString();
164 }
165 }