1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver.dns;
17
18 import io.netty.channel.EventLoop;
19
20 import java.net.InetSocketAddress;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
24 import java.util.concurrent.ConcurrentMap;
25
26 import static io.netty.util.internal.ObjectUtil.checkNotNull;
27 import static io.netty.util.internal.ObjectUtil.checkPositive;
28 import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
29
30
31
32
33 public class DefaultAuthoritativeDnsServerCache implements AuthoritativeDnsServerCache {
34
35 private final int minTtl;
36 private final int maxTtl;
37 private final Comparator<InetSocketAddress> comparator;
38 private final Cache<InetSocketAddress> resolveCache = new Cache<InetSocketAddress>() {
39 @Override
40 protected boolean shouldReplaceAll(InetSocketAddress entry) {
41 return false;
42 }
43
44 @Override
45 protected boolean equals(InetSocketAddress entry, InetSocketAddress otherEntry) {
46 return entry.getHostString().equalsIgnoreCase(otherEntry.getHostString());
47 }
48
49 @Override
50 protected void sortEntries(String hostname, List<InetSocketAddress> entries) {
51 if (comparator != null) {
52 Collections.sort(entries, comparator);
53 }
54 }
55 };
56
57
58
59
60 public DefaultAuthoritativeDnsServerCache() {
61 this(0, Cache.MAX_SUPPORTED_TTL_SECS, null);
62 }
63
64
65
66
67
68
69
70
71
72 public DefaultAuthoritativeDnsServerCache(int minTtl, int maxTtl, Comparator<InetSocketAddress> comparator) {
73 this.minTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositiveOrZero(minTtl, "minTtl"));
74 this.maxTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositive(maxTtl, "maxTtl"));
75 if (minTtl > maxTtl) {
76 throw new IllegalArgumentException(
77 "minTtl: " + minTtl + ", maxTtl: " + maxTtl + " (expected: 0 <= minTtl <= maxTtl)");
78 }
79 this.comparator = comparator;
80 }
81
82 @SuppressWarnings("unchecked")
83 @Override
84 public DnsServerAddressStream get(String hostname) {
85 checkNotNull(hostname, "hostname");
86
87 List<? extends InetSocketAddress> addresses = resolveCache.get(hostname);
88 if (addresses == null || addresses.isEmpty()) {
89 return null;
90 }
91 return new SequentialDnsServerAddressStream(addresses, 0);
92 }
93
94 @Override
95 public void cache(String hostname, InetSocketAddress address, long originalTtl, EventLoop loop) {
96 checkNotNull(hostname, "hostname");
97 checkNotNull(address, "address");
98 checkNotNull(loop, "loop");
99
100 if (address.getHostString() == null) {
101
102
103 return;
104 }
105
106 resolveCache.cache(hostname, address, Math.max(minTtl, (int) Math.min(maxTtl, originalTtl)), loop);
107 }
108
109 @Override
110 public void clear() {
111 resolveCache.clear();
112 }
113
114 @Override
115 public boolean clear(String hostname) {
116 return resolveCache.clear(checkNotNull(hostname, "hostname"));
117 }
118
119 @Override
120 public String toString() {
121 return "DefaultAuthoritativeDnsServerCache(minTtl=" + minTtl + ", maxTtl=" + maxTtl + ", cached nameservers=" +
122 resolveCache.size() + ')';
123 }
124
125
126 int minTtl() {
127 return minTtl;
128 }
129
130
131 int maxTtl() {
132 return maxTtl;
133 }
134 }