1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver.dns;
17
18 import static io.netty.resolver.dns.DnsAddressDecoder.decodeAddress;
19
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.util.Collections;
23 import java.util.List;
24
25 import io.netty.channel.Channel;
26 import io.netty.channel.EventLoop;
27 import io.netty.handler.codec.dns.DnsRecord;
28 import io.netty.handler.codec.dns.DnsRecordType;
29 import io.netty.util.concurrent.Future;
30 import io.netty.util.concurrent.Promise;
31
32 final class DnsAddressResolveContext extends DnsResolveContext<InetAddress> {
33
34 private final DnsCache resolveCache;
35 private final AuthoritativeDnsServerCache authoritativeDnsServerCache;
36 private final boolean completeEarlyIfPossible;
37
38 DnsAddressResolveContext(DnsNameResolver parent, Channel channel, Future<? extends Channel> channelReadyFuture,
39 Promise<?> originalPromise, String hostname, DnsRecord[] additionals,
40 DnsServerAddressStream nameServerAddrs, int allowedQueries, DnsCache resolveCache,
41 AuthoritativeDnsServerCache authoritativeDnsServerCache,
42 boolean completeEarlyIfPossible) {
43 super(parent, channel, channelReadyFuture, originalPromise, hostname, DnsRecord.CLASS_IN,
44 parent.resolveRecordTypes(), additionals, nameServerAddrs, allowedQueries);
45 this.resolveCache = resolveCache;
46 this.authoritativeDnsServerCache = authoritativeDnsServerCache;
47 this.completeEarlyIfPossible = completeEarlyIfPossible;
48 }
49
50 @Override
51 DnsResolveContext<InetAddress> newResolverContext(DnsNameResolver parent, Channel channel,
52 Future<? extends Channel> channelReadyFuture,
53 Promise<?> originalPromise,
54 String hostname,
55 int dnsClass, DnsRecordType[] expectedTypes,
56 DnsRecord[] additionals,
57 DnsServerAddressStream nameServerAddrs, int allowedQueries) {
58 return new DnsAddressResolveContext(parent, channel, channelReadyFuture, originalPromise, hostname, additionals,
59 nameServerAddrs, allowedQueries, resolveCache, authoritativeDnsServerCache, completeEarlyIfPossible);
60 }
61
62 @Override
63 InetAddress convertRecord(DnsRecord record, String hostname, DnsRecord[] additionals, EventLoop eventLoop) {
64 return decodeAddress(record, hostname, parent.isDecodeIdn());
65 }
66
67 @Override
68 List<InetAddress> filterResults(List<InetAddress> unfiltered) {
69 Collections.sort(unfiltered, PreferredAddressTypeComparator.comparator(parent.preferredAddressType()));
70 return unfiltered;
71 }
72
73 @Override
74 boolean isCompleteEarly(InetAddress resolved) {
75 return completeEarlyIfPossible && parent.preferredAddressType().addressType() == resolved.getClass();
76 }
77
78 @Override
79 boolean isDuplicateAllowed() {
80
81 return false;
82 }
83
84 @Override
85 void cache(String hostname, DnsRecord[] additionals,
86 DnsRecord result, InetAddress convertedResult) {
87 resolveCache.cache(hostname, additionals, convertedResult, result.timeToLive(), channel().eventLoop());
88 }
89
90 @Override
91 void cache(String hostname, DnsRecord[] additionals, UnknownHostException cause) {
92 resolveCache.cache(hostname, additionals, cause, channel().eventLoop());
93 }
94
95 @Override
96 void doSearchDomainQuery(String hostname, Promise<List<InetAddress>> nextPromise) {
97
98 if (!DnsNameResolver.doResolveAllCached(
99 hostname, additionals, nextPromise, resolveCache, parent.resolvedInternetProtocolFamiliesUnsafe())) {
100 super.doSearchDomainQuery(hostname, nextPromise);
101 }
102 }
103
104 @Override
105 DnsCache resolveCache() {
106 return resolveCache;
107 }
108
109 @Override
110 AuthoritativeDnsServerCache authoritativeDnsServerCache() {
111 return authoritativeDnsServerCache;
112 }
113 }