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 java.net.UnknownHostException;
19  import java.util.List;
20  
21  import io.netty.channel.EventLoop;
22  import io.netty.handler.codec.dns.DnsQuestion;
23  import io.netty.handler.codec.dns.DnsRecord;
24  import io.netty.handler.codec.dns.DnsRecordType;
25  import io.netty.util.ReferenceCountUtil;
26  import io.netty.util.concurrent.Promise;
27  
28  final class DnsRecordResolveContext extends DnsResolveContext<DnsRecord> {
29  
30      DnsRecordResolveContext(DnsNameResolver parent, Promise<?> originalPromise, DnsQuestion question,
31                              DnsRecord[] additionals, DnsServerAddressStream nameServerAddrs, int allowedQueries) {
32          this(parent, originalPromise, question.name(), question.dnsClass(),
33               new DnsRecordType[] { question.type() },
34               additionals, nameServerAddrs, allowedQueries);
35      }
36  
37      private DnsRecordResolveContext(DnsNameResolver parent, Promise<?> originalPromise, String hostname,
38                                      int dnsClass, DnsRecordType[] expectedTypes,
39                                      DnsRecord[] additionals,
40                                      DnsServerAddressStream nameServerAddrs,
41                                      int allowedQueries) {
42          super(parent, originalPromise, hostname, dnsClass, expectedTypes, additionals, nameServerAddrs, allowedQueries);
43      }
44  
45      @Override
46      DnsResolveContext<DnsRecord> newResolverContext(DnsNameResolver parent, Promise<?> originalPromise,
47                                                      String hostname,
48                                                      int dnsClass, DnsRecordType[] expectedTypes,
49                                                      DnsRecord[] additionals,
50                                                      DnsServerAddressStream nameServerAddrs,
51                                                      int allowedQueries) {
52          return new DnsRecordResolveContext(parent, originalPromise, hostname, dnsClass,
53                                             expectedTypes, additionals, nameServerAddrs, allowedQueries);
54      }
55  
56      @Override
57      DnsRecord convertRecord(DnsRecord record, String hostname, DnsRecord[] additionals, EventLoop eventLoop) {
58          return ReferenceCountUtil.retain(record);
59      }
60  
61      @Override
62      List<DnsRecord> filterResults(List<DnsRecord> unfiltered) {
63          return unfiltered;
64      }
65  
66      @Override
67      boolean isCompleteEarly(DnsRecord resolved) {
68          return false;
69      }
70  
71      @Override
72      boolean isDuplicateAllowed() {
73          return true;
74      }
75  
76      @Override
77      void cache(String hostname, DnsRecord[] additionals, DnsRecord result, DnsRecord convertedResult) {
78          // Do not cache.
79          // XXX: When we implement cache, we would need to retain the reference count of the result record.
80      }
81  
82      @Override
83      void cache(String hostname, DnsRecord[] additionals, UnknownHostException cause) {
84          // Do not cache.
85          // XXX: When we implement cache, we would need to retain the reference count of the result record.
86      }
87  
88      @Override
89      DnsCnameCache cnameCache() {
90          // We don't use a cache here at all as we also don't cache if we end up using the DnsRecordResolverContext.
91          return NoopDnsCnameCache.INSTANCE;
92      }
93  }