DnsResolver.resolve(...) fails when ipaddress is used.

Motivation:

DnsResolver.resolve(...) fails when an InetSocketAddress is used that was constructed of an ipaddress string.

Modifications:

Don't try to lookup when the InetSocketAddress was constructed via an ipaddress.

Result:

DnsResolver.resolve(...) works in all cases.
This commit is contained in:
Norman Maurer 2015-07-10 22:00:30 +02:00
parent 74dd7f85ca
commit d1344345bb
2 changed files with 22 additions and 4 deletions

View File

@ -38,6 +38,7 @@ import io.netty.handler.codec.dns.DnsResponse;
import io.netty.handler.codec.dns.DnsResponseCode; import io.netty.handler.codec.dns.DnsResponseCode;
import io.netty.resolver.NameResolver; import io.netty.resolver.NameResolver;
import io.netty.resolver.SimpleNameResolver; import io.netty.resolver.SimpleNameResolver;
import io.netty.util.NetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
@ -50,6 +51,7 @@ import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.IDN; import java.net.IDN;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
@ -638,12 +640,18 @@ public class DnsNameResolver extends SimpleNameResolver<InetSocketAddress> {
@Override @Override
protected void doResolve(InetSocketAddress unresolvedAddress, Promise<InetSocketAddress> promise) throws Exception { protected void doResolve(InetSocketAddress unresolvedAddress, Promise<InetSocketAddress> promise) throws Exception {
byte[] bytes = NetUtil.createByteArrayFromIpAddressString(unresolvedAddress.getHostName());
if (bytes == null) {
final String hostname = IDN.toASCII(hostname(unresolvedAddress)); final String hostname = IDN.toASCII(hostname(unresolvedAddress));
final int port = unresolvedAddress.getPort(); final int port = unresolvedAddress.getPort();
final DnsNameResolverContext ctx = new DnsNameResolverContext(this, hostname, port, promise); final DnsNameResolverContext ctx = new DnsNameResolverContext(this, hostname, port, promise);
ctx.resolve(); ctx.resolve();
} else {
// The unresolvedAddress was created via a String that contains an ipaddress.
promise.setSuccess(new InetSocketAddress(InetAddress.getByAddress(bytes), unresolvedAddress.getPort()));
}
} }
private static String hostname(InetSocketAddress addr) { private static String hostname(InetSocketAddress addr) {

View File

@ -418,6 +418,16 @@ public class DnsNameResolverTest {
} }
} }
@Test
public void testResolveIp() {
InetSocketAddress unresolved =
InetSocketAddress.createUnresolved("10.0.0.1", ThreadLocalRandom.current().nextInt(65536));
InetSocketAddress address = resolver.resolve(unresolved).syncUninterruptibly().getNow();
assertEquals("10.0.0.1", address.getHostName());
}
private static void resolve( private static void resolve(
Map<InetSocketAddress, Future<InetSocketAddress>> futures, String hostname) { Map<InetSocketAddress, Future<InetSocketAddress>> futures, String hostname) {
InetSocketAddress unresolved = InetSocketAddress unresolved =