Skip invalid hostnames when construct default dns servers to use.

Motivation:

When the hostname portion can not be extracted we should just skip the server as otherwise we will produce and exception when trying to create the InetSocketAddress.

This was happing when trying to run the test-suite on a system and using java7:

java.lang.IllegalArgumentException: hostname can't be null
	at java.net.InetSocketAddress.checkHost(InetSocketAddress.java:149)
	at java.net.InetSocketAddress.<init>(InetSocketAddress.java:216)
	at io.netty.util.internal.SocketUtils$10.run(SocketUtils.java:171)
	at io.netty.util.internal.SocketUtils$10.run(SocketUtils.java:168)
	at java.security.AccessController.doPrivileged(Native Method)
	at io.netty.util.internal.SocketUtils.socketAddress(SocketUtils.java:168)
	at io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider.<clinit>(DefaultDnsServerAddressStreamProvider.java:74)
	at io.netty.resolver.dns.DnsServerAddressesTest.testDefaultAddresses(DnsServerAddressesTest.java:39)

Modifications:

Skip if hostname can not be extracted.

Result:

No more java.lang.ExceptionInInitializerError.
This commit is contained in:
Norman Maurer 2017-07-24 12:03:25 +02:00
parent 486f962252
commit 34fdc7a33e

View File

@ -66,14 +66,25 @@ public final class DefaultDnsServerAddressStreamProvider implements DnsServerAdd
try { try {
DirContext ctx = new InitialDirContext(env); DirContext ctx = new InitialDirContext(env);
String dnsUrls = (String) ctx.getEnvironment().get("java.naming.provider.url"); String dnsUrls = (String) ctx.getEnvironment().get("java.naming.provider.url");
String[] servers = dnsUrls.split(" "); // Only try if not empty as otherwise we will produce an exception
for (String server : servers) { if (dnsUrls != null && !dnsUrls.isEmpty()) {
try { String[] servers = dnsUrls.split(" ");
URI uri = new URI(server); for (String server : servers) {
int port = uri.getPort(); try {
defaultNameServers.add(SocketUtils.socketAddress(uri.getHost(), port == -1 ? DNS_PORT : port)); URI uri = new URI(server);
} catch (URISyntaxException e) { String host = new URI(server).getHost();
logger.debug("Skipping a malformed nameserver URI: {}", server, e);
if (host == null || host.isEmpty()) {
logger.debug(
"Skipping a nameserver URI as host portion could not be extracted: {}", server);
// If the host portion can not be parsed we should just skip this entry.
continue;
}
int port = uri.getPort();
defaultNameServers.add(SocketUtils.socketAddress(uri.getHost(), port == -1 ? DNS_PORT : port));
} catch (URISyntaxException e) {
logger.debug("Skipping a malformed nameserver URI: {}", server, e);
}
} }
} }
} catch (NamingException ignore) { } catch (NamingException ignore) {