Search in sources :

Example 26 with DnsClient

use of io.vertx.core.dns.DnsClient in project vert.x by eclipse.

the class DNSTest method testReverseLookupIpv4.

@Test
public void testReverseLookupIpv4() throws Exception {
    String address = "10.0.0.1";
    final String ptr = "ptr.vertx.io";
    DnsClient dns = prepareDns(FakeDNSServer.testReverseLookup(ptr));
    dns.reverseLookup(address, ar -> {
        String result = ar.result();
        assertNotNull(result);
        assertEquals(ptr, result);
        testComplete();
    });
    await();
    dnsServer.stop();
}
Also used : DnsClient(io.vertx.core.dns.DnsClient) Test(org.junit.Test)

Example 27 with DnsClient

use of io.vertx.core.dns.DnsClient in project vert.x by eclipse.

the class DNSTest method testIllegalArguments.

@Test
public void testIllegalArguments() throws Exception {
    DnsClient dns = prepareDns(FakeDNSServer.testResolveAAAA("::1"));
    assertNullPointerException(() -> dns.lookup(null, ar -> {
    }));
    assertNullPointerException(() -> dns.lookup4(null, ar -> {
    }));
    assertNullPointerException(() -> dns.lookup6(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolveA(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolveAAAA(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolveCNAME(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolveMX(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolveTXT(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolvePTR(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolveNS(null, ar -> {
    }));
    assertNullPointerException(() -> dns.resolveSRV(null, ar -> {
    }));
    dnsServer.stop();
}
Also used : DnsNameResolverBuilder(io.netty.resolver.dns.DnsNameResolverBuilder) DnsException(io.vertx.core.dns.DnsException) DnsNameResolver(io.netty.resolver.dns.DnsNameResolver) ContextInternal(io.vertx.core.impl.ContextInternal) Test(org.junit.Test) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) InetSocketAddress(java.net.InetSocketAddress) SrvRecord(io.vertx.core.dns.SrvRecord) DnsClient(io.vertx.core.dns.DnsClient) InetAddress(java.net.InetAddress) List(java.util.List) DnsServerAddresses(io.netty.resolver.dns.DnsServerAddresses) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) DnsResponseCode(io.vertx.core.dns.DnsResponseCode) MxRecord(io.vertx.core.dns.MxRecord) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) Future(io.netty.util.concurrent.Future) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) DnsClient(io.vertx.core.dns.DnsClient) Test(org.junit.Test)

Example 28 with DnsClient

use of io.vertx.core.dns.DnsClient in project vert.x by eclipse.

the class DNSTest method testResolveAAAA.

@Test
public void testResolveAAAA() throws Exception {
    DnsClient dns = prepareDns(FakeDNSServer.testResolveAAAA("::1"));
    dns.resolveAAAA("vertx.io", ar -> {
        List<String> result = ar.result();
        assertNotNull(result);
        assertFalse(result.isEmpty());
        assertEquals(1, result.size());
        assertEquals("0:0:0:0:0:0:0:1", result.get(0));
        testComplete();
    });
    await();
    dnsServer.stop();
}
Also used : DnsClient(io.vertx.core.dns.DnsClient) Test(org.junit.Test)

Example 29 with DnsClient

use of io.vertx.core.dns.DnsClient in project vert.x by eclipse.

the class DNSTest method testLookup6.

@Test
public void testLookup6() throws Exception {
    DnsClient dns = prepareDns(FakeDNSServer.testLookup6());
    dns.lookup6("vertx.io", ar -> {
        String result = ar.result();
        assertNotNull(result);
        assertEquals("0:0:0:0:0:0:0:1", result);
        testComplete();
    });
    await();
    dnsServer.stop();
}
Also used : DnsClient(io.vertx.core.dns.DnsClient) Test(org.junit.Test)

Example 30 with DnsClient

use of io.vertx.core.dns.DnsClient in project vert.x by eclipse.

the class DnsClientImpl method reverseLookup.

@Override
public DnsClient reverseLookup(String address, Handler<AsyncResult<String>> handler) {
    //        An other option would be to change address to be of type InetAddress.
    try {
        InetAddress inetAddress = InetAddress.getByName(address);
        byte[] addr = inetAddress.getAddress();
        StringBuilder reverseName = new StringBuilder(64);
        if (inetAddress instanceof Inet4Address) {
            // reverse ipv4 address
            reverseName.append(addr[3] & 0xff).append(".").append(addr[2] & 0xff).append(".").append(addr[1] & 0xff).append(".").append(addr[0] & 0xff);
        } else {
            // It is an ipv 6 address time to reverse it
            for (int i = 0; i < 16; i++) {
                reverseName.append(HEX_TABLE[(addr[15 - i] & 0xf)]);
                reverseName.append(".");
                reverseName.append(HEX_TABLE[(addr[15 - i] >> 4) & 0xf]);
                if (i != 15) {
                    reverseName.append(".");
                }
            }
        }
        reverseName.append(".in-addr.arpa");
        return resolvePTR(reverseName.toString(), handler);
    } catch (UnknownHostException e) {
        // Should never happen as we work with ip addresses as input
        // anyway just in case notify the handler
        actualCtx.runOnContext((v) -> handler.handle(Future.failedFuture(e)));
    }
    return this;
}
Also used : DatagramDnsQueryEncoder(io.netty.handler.codec.dns.DatagramDnsQueryEncoder) DnsRecord(io.netty.handler.codec.dns.DnsRecord) DnsResponse(io.netty.handler.codec.dns.DnsResponse) DnsException(io.vertx.core.dns.DnsException) ChannelOption(io.netty.channel.ChannelOption) ContextImpl(io.vertx.core.impl.ContextImpl) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) SrvRecord(io.vertx.core.dns.SrvRecord) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DatagramChannel(io.netty.channel.socket.DatagramChannel) DnsSection(io.netty.handler.codec.dns.DnsSection) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DnsResponseCode(io.vertx.core.dns.DnsResponseCode) AsyncResult(io.vertx.core.AsyncResult) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) DatagramDnsQuery(io.netty.handler.codec.dns.DatagramDnsQuery) PartialPooledByteBufAllocator(io.vertx.core.net.impl.PartialPooledByteBufAllocator) ChannelPipeline(io.netty.channel.ChannelPipeline) Future(io.vertx.core.Future) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) RecordDecoder(io.vertx.core.dns.impl.decoder.RecordDecoder) ChannelFuture(io.netty.channel.ChannelFuture) DnsClient(io.vertx.core.dns.DnsClient) Objects(java.util.Objects) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DatagramDnsResponseDecoder(io.netty.handler.codec.dns.DatagramDnsResponseDecoder) MxRecord(io.vertx.core.dns.MxRecord) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress)

Aggregations

DnsClient (io.vertx.core.dns.DnsClient)30 Test (org.junit.Test)16 DnsException (io.vertx.core.dns.DnsException)4 MxRecord (io.vertx.core.dns.MxRecord)4 SrvRecord (io.vertx.core.dns.SrvRecord)4 DnsResponseCode (io.vertx.core.dns.DnsResponseCode)3 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 List (java.util.List)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelOption (io.netty.channel.ChannelOption)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 DatagramChannel (io.netty.channel.socket.DatagramChannel)1 DatagramDnsQuery (io.netty.handler.codec.dns.DatagramDnsQuery)1