DnsNameResolver.resolveAll(DnsQuestion) should not try to filter duplicates (#9141)
Motivation: https://github.com/netty/netty/pull/9021 did apply some changes to filter out duplicates InetAddress when calling resolveAll(...) to mimic JDK behaviour. Unfortunally this also introduced a regression as we should not filter duplicates when the user explicit calls resolveAll(DnsQuestion). Modifications: - Only filter duplicates if resolveAll(String) is used - Add unit test Result: Fixes regressions introduces by https://github.com/netty/netty/pull/9021
This commit is contained in:
parent
d3a13a0d6a
commit
debc1cbc0a
@ -67,6 +67,12 @@ final class DnsAddressResolveContext extends DnsResolveContext<InetAddress> {
|
||||
return completeEarlyIfPossible && parent.preferredAddressType().addressType() == resolved.getClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isDuplicateAllowed() {
|
||||
// We don't want include duplicates to mimic JDK behaviour.
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
void cache(String hostname, DnsRecord[] additionals,
|
||||
DnsRecord result, InetAddress convertedResult) {
|
||||
|
@ -63,6 +63,11 @@ final class DnsRecordResolveContext extends DnsResolveContext<DnsRecord> {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isDuplicateAllowed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
void cache(String hostname, DnsRecord[] additionals, DnsRecord result, DnsRecord convertedResult) {
|
||||
// Do not cache.
|
||||
|
@ -159,6 +159,12 @@ abstract class DnsResolveContext<T> {
|
||||
|
||||
abstract boolean isCompleteEarly(T resolved);
|
||||
|
||||
/**
|
||||
* Returns {@code true} if we should allow duplicates in the result or {@code false} if no duplicates should
|
||||
* be included.
|
||||
*/
|
||||
abstract boolean isDuplicateAllowed();
|
||||
|
||||
/**
|
||||
* Caches a successful resolution.
|
||||
*/
|
||||
@ -699,7 +705,7 @@ abstract class DnsResolveContext<T> {
|
||||
if (finalResult == null) {
|
||||
finalResult = new ArrayList<>(8);
|
||||
finalResult.add(converted);
|
||||
} else if (!finalResult.contains(converted)) {
|
||||
} else if (isDuplicateAllowed() || !finalResult.contains(converted)) {
|
||||
finalResult.add(converted);
|
||||
} else {
|
||||
shouldRelease = true;
|
||||
|
@ -2567,6 +2567,48 @@ public class DnsNameResolverTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncludeDuplicates() throws IOException {
|
||||
final String name = "netty.io";
|
||||
final String ipv4Addr = "1.2.3.4";
|
||||
TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {
|
||||
@Override
|
||||
public Set<ResourceRecord> getRecords(QuestionRecord question) {
|
||||
Set<ResourceRecord> records = new LinkedHashSet<ResourceRecord>(2);
|
||||
String qName = question.getDomainName().toLowerCase();
|
||||
records.add(new TestDnsServer.TestResourceRecord(qName,
|
||||
RecordType.A, Collections.<String, Object>singletonMap(
|
||||
DnsAttribute.IP_ADDRESS.toLowerCase(), ipv4Addr)));
|
||||
records.add(new TestDnsServer.TestResourceRecord(qName,
|
||||
RecordType.A, Collections.<String, Object>singletonMap(
|
||||
DnsAttribute.IP_ADDRESS.toLowerCase(), ipv4Addr)));
|
||||
return records;
|
||||
}
|
||||
});
|
||||
dnsServer2.start();
|
||||
DnsNameResolver resolver = null;
|
||||
try {
|
||||
DnsNameResolverBuilder builder = newResolver()
|
||||
.recursionDesired(true)
|
||||
.maxQueriesPerResolve(16)
|
||||
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
|
||||
builder.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY);
|
||||
|
||||
resolver = builder.build();
|
||||
List<DnsRecord> resolvedAddresses = resolver.resolveAll(new DefaultDnsQuestion(name, A))
|
||||
.syncUninterruptibly().getNow();
|
||||
assertEquals(2, resolvedAddresses.size());
|
||||
for (DnsRecord record: resolvedAddresses) {
|
||||
ReferenceCountUtil.release(record);
|
||||
}
|
||||
} finally {
|
||||
dnsServer2.stop();
|
||||
if (resolver != null) {
|
||||
resolver.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDropAAAA() throws IOException {
|
||||
String host = "somehost.netty.io";
|
||||
|
Loading…
Reference in New Issue
Block a user