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.netty5.resolver.dns;
17  
18  import io.netty5.channel.EventLoop;
19  import io.netty5.handler.codec.dns.DnsQuestion;
20  import io.netty5.handler.codec.dns.DnsRecord;
21  import io.netty5.handler.codec.dns.DnsRecordType;
22  import io.netty5.util.concurrent.Promise;
23  
24  import java.net.UnknownHostException;
25  import java.util.List;
26  
27  final class DnsRecordResolveContext extends DnsResolveContext<DnsRecord> {
28  
29      DnsRecordResolveContext(DnsNameResolver parent, Promise<?> originalPromise, DnsQuestion question,
30                              DnsRecord[] additionals, DnsServerAddressStream nameServerAddrs, int allowedQueries) {
31          this(parent, originalPromise, question.name(), question.dnsClass(),
32               new DnsRecordType[] { question.type() },
33               additionals, nameServerAddrs, allowedQueries);
34      }
35  
36      private DnsRecordResolveContext(DnsNameResolver parent, Promise<?> originalPromise, String hostname,
37                                      int dnsClass, DnsRecordType[] expectedTypes,
38                                      DnsRecord[] additionals,
39                                      DnsServerAddressStream nameServerAddrs,
40                                      int allowedQueries) {
41          super(parent, originalPromise, hostname, dnsClass, expectedTypes, additionals, nameServerAddrs, allowedQueries);
42      }
43  
44      @Override
45      DnsResolveContext<DnsRecord> newResolverContext(DnsNameResolver parent, Promise<?> originalPromise,
46                                                      String hostname,
47                                                      int dnsClass, DnsRecordType[] expectedTypes,
48                                                      DnsRecord[] additionals,
49                                                      DnsServerAddressStream nameServerAddrs,
50                                                      int allowedQueries) {
51          return new DnsRecordResolveContext(parent, originalPromise, hostname, dnsClass,
52                                             expectedTypes, additionals, nameServerAddrs, allowedQueries);
53      }
54  
55      @Override
56      DnsRecord convertRecord(DnsRecord record, String hostname, DnsRecord[] additionals, EventLoop eventLoop) {
57          // The given record is a shared object, but we need to give back a record that has its own life time.
58          return record.copy();
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  }