2018-03-29 22:01:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.resolver.dns;
|
|
|
|
|
|
|
|
import static io.netty.resolver.dns.DnsAddressDecoder.decodeAddress;
|
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import io.netty.channel.EventLoop;
|
|
|
|
import io.netty.handler.codec.dns.DnsRecord;
|
|
|
|
import io.netty.handler.codec.dns.DnsRecordType;
|
2018-08-10 08:53:59 +02:00
|
|
|
import io.netty.util.concurrent.Promise;
|
2018-03-29 22:01:25 +02:00
|
|
|
|
|
|
|
final class DnsAddressResolveContext extends DnsResolveContext<InetAddress> {
|
|
|
|
|
|
|
|
private final DnsCache resolveCache;
|
2018-08-22 17:49:22 +02:00
|
|
|
private final AuthoritativeDnsServerCache authoritativeDnsServerCache;
|
2018-03-29 22:01:25 +02:00
|
|
|
|
|
|
|
DnsAddressResolveContext(DnsNameResolver parent, String hostname, DnsRecord[] additionals,
|
2018-08-22 17:49:22 +02:00
|
|
|
DnsServerAddressStream nameServerAddrs, DnsCache resolveCache,
|
|
|
|
AuthoritativeDnsServerCache authoritativeDnsServerCache) {
|
2018-03-29 22:01:25 +02:00
|
|
|
super(parent, hostname, DnsRecord.CLASS_IN, parent.resolveRecordTypes(), additionals, nameServerAddrs);
|
|
|
|
this.resolveCache = resolveCache;
|
2018-08-22 17:49:22 +02:00
|
|
|
this.authoritativeDnsServerCache = authoritativeDnsServerCache;
|
2018-03-29 22:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
DnsResolveContext<InetAddress> newResolverContext(DnsNameResolver parent, String hostname,
|
|
|
|
int dnsClass, DnsRecordType[] expectedTypes,
|
|
|
|
DnsRecord[] additionals,
|
|
|
|
DnsServerAddressStream nameServerAddrs) {
|
2018-08-22 17:49:22 +02:00
|
|
|
return new DnsAddressResolveContext(parent, hostname, additionals, nameServerAddrs, resolveCache,
|
|
|
|
authoritativeDnsServerCache);
|
2018-03-29 22:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
InetAddress convertRecord(DnsRecord record, String hostname, DnsRecord[] additionals, EventLoop eventLoop) {
|
|
|
|
return decodeAddress(record, hostname, parent.isDecodeIdn());
|
|
|
|
}
|
|
|
|
|
2018-04-19 10:52:22 +02:00
|
|
|
@Override
|
|
|
|
List<InetAddress> filterResults(List<InetAddress> unfiltered) {
|
2019-04-15 10:19:54 +02:00
|
|
|
unfiltered.sort(PreferredAddressTypeComparator.comparator(parent.preferredAddressType()));
|
|
|
|
return unfiltered;
|
2018-04-19 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 22:01:25 +02:00
|
|
|
@Override
|
|
|
|
void cache(String hostname, DnsRecord[] additionals,
|
|
|
|
DnsRecord result, InetAddress convertedResult) {
|
|
|
|
resolveCache.cache(hostname, additionals, convertedResult, result.timeToLive(), parent.ch.eventLoop());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void cache(String hostname, DnsRecord[] additionals, UnknownHostException cause) {
|
|
|
|
resolveCache.cache(hostname, additionals, cause, parent.ch.eventLoop());
|
|
|
|
}
|
2018-08-10 08:53:59 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
void doSearchDomainQuery(String hostname, Promise<List<InetAddress>> nextPromise) {
|
|
|
|
// Query the cache for the hostname first and only do a query if we could not find it in the cache.
|
2018-08-22 17:49:22 +02:00
|
|
|
if (!DnsNameResolver.doResolveAllCached(
|
|
|
|
hostname, additionals, nextPromise, resolveCache, parent.resolvedInternetProtocolFamiliesUnsafe())) {
|
2018-08-10 08:53:59 +02:00
|
|
|
super.doSearchDomainQuery(hostname, nextPromise);
|
|
|
|
}
|
|
|
|
}
|
2018-08-22 17:49:22 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
DnsCache resolveCache() {
|
|
|
|
return resolveCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
AuthoritativeDnsServerCache authoritativeDnsServerCache() {
|
|
|
|
return authoritativeDnsServerCache;
|
|
|
|
}
|
2018-03-29 22:01:25 +02:00
|
|
|
}
|