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