Skip to content

Commit 549fb25

Browse files
committed
Disable IPv6 address lookups when -Djava.net.preferIPv4Stack=true, close AsyncHttpClient#1336
Motivation: According to the Oracle documentation: > java.net.preferIPv4Stack (default: false) > > If IPv6 is available on the operating system, the underlying native > socket will be an IPv6 socket. This allows Java applications to connect > to, and accept connections from, both IPv4 and IPv6 hosts. > > If an application has a preference to only use IPv4 sockets, then this > property can be set to true. The implication is that the application > will not be able to communicate with IPv6 hosts. which means, if DnsNameResolver returns an IPv6 address, a user (or Netty) will not be able to connect to it. Modifications: - Move the code that retrieves java.net.prefer* properties from DnsNameResolver to NetUtil - Add NetUtil.isIpV6AddressesPreferred() - Revise the API documentation of NetUtil.isIpV*Preferred() - Set the default resolveAddressTypes to IPv4 only when NetUtil.isIpv4StackPreferred() returns true Result: - Fixes AsyncHttpClient#1336
1 parent 2fd2d86 commit 549fb25

File tree

2 files changed

+1111
-14
lines changed

2 files changed

+1111
-14
lines changed

netty-bp/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import io.netty.handler.codec.dns.DnsResponse;
3838
import io.netty.resolver.HostsFileEntriesResolver;
3939
import io.netty.resolver.InetNameResolver;
40-
import io.netty.util.NetUtil;
40+
import io.netty.util.NetUtil2;
4141
import io.netty.util.ReferenceCountUtil;
4242
import io.netty.util.concurrent.FastThreadLocal;
4343
import io.netty.util.concurrent.Future;
@@ -69,21 +69,24 @@ public class DnsNameResolver extends InetNameResolver {
6969
private static final String LOCALHOST = "localhost";
7070
private static final InetAddress LOCALHOST_ADDRESS;
7171

72-
static final InternetProtocolFamily[] DEFAULT_RESOLVE_ADDRESS_TYPES = new InternetProtocolFamily[2];
72+
static final InternetProtocolFamily[] DEFAULT_RESOLVE_ADDRESS_TYPES;
7373
static final String[] DEFAULT_SEACH_DOMAINS;
7474

7575
static {
76-
// Note that we did not use SystemPropertyUtil.getBoolean() here to emulate the behavior of JDK.
77-
if (Boolean.getBoolean("java.net.preferIPv6Addresses")) {
78-
DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv6;
79-
DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv4;
80-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST6;
81-
logger.debug("-Djava.net.preferIPv6Addresses: true");
76+
if (NetUtil2.isIpV4StackPreferred()) {
77+
DEFAULT_RESOLVE_ADDRESS_TYPES = new InternetProtocolFamily[] { InternetProtocolFamily.IPv4 };
78+
LOCALHOST_ADDRESS = NetUtil2.LOCALHOST4;
8279
} else {
83-
DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv4;
84-
DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv6;
85-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
86-
logger.debug("-Djava.net.preferIPv6Addresses: false");
80+
DEFAULT_RESOLVE_ADDRESS_TYPES = new InternetProtocolFamily[2];
81+
if (NetUtil2.isIpV6AddressesPreferred()) {
82+
DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv6;
83+
DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv4;
84+
LOCALHOST_ADDRESS = NetUtil2.LOCALHOST6;
85+
} else {
86+
DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv4;
87+
DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv6;
88+
LOCALHOST_ADDRESS = NetUtil2.LOCALHOST4;
89+
}
8790
}
8891
}
8992

@@ -343,7 +346,7 @@ protected void doResolve(String inetHost, Promise<InetAddress> promise) throws E
343346
protected void doResolve(String inetHost,
344347
Promise<InetAddress> promise,
345348
DnsCache resolveCache) throws Exception {
346-
final byte[] bytes = NetUtil.createByteArrayFromIpAddressString(inetHost);
349+
final byte[] bytes = NetUtil2.createByteArrayFromIpAddressString(inetHost);
347350
if (bytes != null) {
348351
// The inetHost is actually an ipaddress.
349352
promise.setSuccess(InetAddress.getByAddress(bytes));
@@ -461,7 +464,7 @@ protected void doResolveAll(String inetHost,
461464
Promise<List<InetAddress>> promise,
462465
DnsCache resolveCache) throws Exception {
463466

464-
final byte[] bytes = NetUtil.createByteArrayFromIpAddressString(inetHost);
467+
final byte[] bytes = NetUtil2.createByteArrayFromIpAddressString(inetHost);
465468
if (bytes != null) {
466469
// The unresolvedAddress was created via a String that contains an ipaddress.
467470
promise.setSuccess(Collections.singletonList(InetAddress.getByAddress(bytes)));

0 commit comments

Comments
 (0)