View Javadoc
1   /*
2    * Copyright 2018 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.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.Promise;
30  
31  final class DnsAddressResolveContext extends DnsResolveContext<InetAddress> {
32  
33      private final DnsCache resolveCache;
34      private final AuthoritativeDnsServerCache authoritativeDnsServerCache;
35      private final boolean completeEarlyIfPossible;
36  
37      DnsAddressResolveContext(DnsNameResolver parent, Channel channel,
38                               Promise<?> originalPromise, String hostname, DnsRecord[] additionals,
39                               DnsServerAddressStream nameServerAddrs, int allowedQueries, DnsCache resolveCache,
40                               AuthoritativeDnsServerCache authoritativeDnsServerCache,
41                               boolean completeEarlyIfPossible) {
42          super(parent, channel, originalPromise, hostname, DnsRecord.CLASS_IN,
43                parent.resolveRecordTypes(), additionals, nameServerAddrs, allowedQueries);
44          this.resolveCache = resolveCache;
45          this.authoritativeDnsServerCache = authoritativeDnsServerCache;
46          this.completeEarlyIfPossible = completeEarlyIfPossible;
47      }
48  
49      @Override
50      DnsResolveContext<InetAddress> newResolverContext(DnsNameResolver parent, Channel channel,
51                                                        Promise<?> originalPromise,
52                                                        String hostname,
53                                                        int dnsClass, DnsRecordType[] expectedTypes,
54                                                        DnsRecord[] additionals,
55                                                        DnsServerAddressStream nameServerAddrs, int allowedQueries) {
56          return new DnsAddressResolveContext(parent, channel, originalPromise, hostname, additionals,
57                  nameServerAddrs, allowedQueries, resolveCache, authoritativeDnsServerCache, completeEarlyIfPossible);
58      }
59  
60      @Override
61      InetAddress convertRecord(DnsRecord record, String hostname, DnsRecord[] additionals, EventLoop eventLoop) {
62          return decodeAddress(record, hostname, parent.isDecodeIdn());
63      }
64  
65      @Override
66      List<InetAddress> filterResults(List<InetAddress> unfiltered) {
67          Collections.sort(unfiltered, PreferredAddressTypeComparator.comparator(parent.preferredAddressType()));
68          return unfiltered;
69      }
70  
71      @Override
72      boolean isCompleteEarly(InetAddress resolved) {
73          return completeEarlyIfPossible && parent.preferredAddressType().addressType() == resolved.getClass();
74      }
75  
76      @Override
77      boolean isDuplicateAllowed() {
78          // We don't want include duplicates to mimic JDK behaviour.
79          return false;
80      }
81  
82      @Override
83      void cache(String hostname, DnsRecord[] additionals,
84                 DnsRecord result, InetAddress convertedResult) {
85          resolveCache.cache(hostname, additionals, convertedResult, result.timeToLive(),
86                  channel().eventLoop());
87      }
88  
89      @Override
90      void cache(String hostname, DnsRecord[] additionals, UnknownHostException cause) {
91          resolveCache.cache(hostname, additionals, cause, channel().eventLoop());
92      }
93  
94      @Override
95      void doSearchDomainQuery(String hostname, Promise<List<InetAddress>> nextPromise) {
96          // Query the cache for the hostname first and only do a query if we could not find it in the cache.
97          if (!DnsNameResolver.doResolveAllCached(hostname, additionals, nextPromise, resolveCache,
98                  parent.searchDomains(), parent.ndots(), parent.resolvedInternetProtocolFamiliesUnsafe())) {
99              super.doSearchDomainQuery(hostname, nextPromise);
100         }
101     }
102 
103     @Override
104     DnsCache resolveCache() {
105         return resolveCache;
106     }
107 
108     @Override
109     AuthoritativeDnsServerCache authoritativeDnsServerCache() {
110         return authoritativeDnsServerCache;
111     }
112 }