Search in sources :

Example 6 with DnsResponse

use of io.netty.handler.codec.dns.DnsResponse in project vert.x by eclipse.

the class DnsClientImpl method lookup.

@SuppressWarnings("unchecked")
private <T> void lookup(String name, Future<List<T>> result, DnsRecordType... types) {
    Objects.requireNonNull(name, "no null name accepted");
    bootstrap.connect(dnsServer).addListener(new RetryChannelFutureListener(result) {

        @Override
        public void onSuccess(ChannelFuture future) throws Exception {
            DatagramDnsQuery query = new DatagramDnsQuery(null, dnsServer, ThreadLocalRandom.current().nextInt());
            for (DnsRecordType type : types) {
                query.addRecord(DnsSection.QUESTION, new DefaultDnsQuestion(name, type, DnsRecord.CLASS_IN));
            }
            future.channel().writeAndFlush(query).addListener(new RetryChannelFutureListener(result) {

                @Override
                public void onSuccess(ChannelFuture future) throws Exception {
                    future.channel().pipeline().addLast(new SimpleChannelInboundHandler<DnsResponse>() {

                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, DnsResponse msg) throws Exception {
                            DnsResponseCode code = DnsResponseCode.valueOf(msg.code().intValue());
                            if (code == DnsResponseCode.NOERROR) {
                                int count = msg.count(DnsSection.ANSWER);
                                List<T> records = new ArrayList<>(count);
                                for (int idx = 0; idx < count; idx++) {
                                    DnsRecord a = msg.recordAt(DnsSection.ANSWER, idx);
                                    T record = RecordDecoder.decode(a);
                                    if (isRequestedType(a.type(), types)) {
                                        records.add(record);
                                    }
                                }
                                if (records.size() > 0 && (records.get(0) instanceof MxRecordImpl || records.get(0) instanceof SrvRecordImpl)) {
                                    Collections.sort((List) records);
                                }
                                setResult(result, records);
                            } else {
                                setFailure(result, new DnsException(code));
                            }
                            ctx.close();
                        }

                        private boolean isRequestedType(DnsRecordType dnsRecordType, DnsRecordType[] types) {
                            for (DnsRecordType t : types) {
                                if (t.equals(dnsRecordType)) {
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                            setFailure(result, cause);
                            ctx.close();
                        }
                    });
                }
            });
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DnsResponseCode(io.vertx.core.dns.DnsResponseCode) DnsException(io.vertx.core.dns.DnsException) UnknownHostException(java.net.UnknownHostException) DnsException(io.vertx.core.dns.DnsException) DatagramDnsQuery(io.netty.handler.codec.dns.DatagramDnsQuery) DnsResponse(io.netty.handler.codec.dns.DnsResponse) ArrayList(java.util.ArrayList) List(java.util.List) DnsRecord(io.netty.handler.codec.dns.DnsRecord) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Example 7 with DnsResponse

use of io.netty.handler.codec.dns.DnsResponse in project netty by netty.

the class DnsNameResolverContext method finishResolve.

private void finishResolve(Promise<T> promise) {
    if (!queriesInProgress.isEmpty()) {
        // If there are queries in progress, we should cancel it because we already finished the resolution.
        for (Iterator<Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> i = queriesInProgress.iterator(); i.hasNext(); ) {
            Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = i.next();
            i.remove();
            if (!f.cancel(false)) {
                f.addListener(RELEASE_RESPONSE);
            }
        }
    }
    if (resolvedEntries != null) {
        // Found at least one resolved address.
        for (InternetProtocolFamily f : resolvedInternetProtocolFamilies) {
            if (finishResolve(f.addressType(), resolvedEntries, promise)) {
                return;
            }
        }
    }
    // No resolved address found.
    final int tries = maxAllowedQueries - allowedQueries;
    final StringBuilder buf = new StringBuilder(64);
    buf.append("failed to resolve '");
    if (pristineHostname != null) {
        buf.append(pristineHostname);
    } else {
        buf.append(hostname);
    }
    buf.append('\'');
    if (tries > 1) {
        if (tries < maxAllowedQueries) {
            buf.append(" after ").append(tries).append(" queries ");
        } else {
            buf.append(". Exceeded max queries per resolve ").append(maxAllowedQueries).append(' ');
        }
    }
    if (trace != null) {
        buf.append(':').append(trace);
    }
    final UnknownHostException cause = new UnknownHostException(buf.toString());
    resolveCache.cache(hostname, additionals, cause, parent.ch.eventLoop());
    promise.tryFailure(cause);
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) DnsResponse(io.netty.handler.codec.dns.DnsResponse) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) Future(io.netty.util.concurrent.Future)

Aggregations

DnsResponse (io.netty.handler.codec.dns.DnsResponse)7 DnsRecord (io.netty.handler.codec.dns.DnsRecord)4 DnsRecordType (io.netty.handler.codec.dns.DnsRecordType)3 InetSocketAddress (java.net.InetSocketAddress)3 UnknownHostException (java.net.UnknownHostException)3 ArrayList (java.util.ArrayList)3 AddressedEnvelope (io.netty.channel.AddressedEnvelope)2 Future (io.netty.util.concurrent.Future)2 InetAddress (java.net.InetAddress)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufHolder (io.netty.buffer.ByteBufHolder)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 InternetProtocolFamily (io.netty.channel.socket.InternetProtocolFamily)1 DatagramDnsQuery (io.netty.handler.codec.dns.DatagramDnsQuery)1 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)1 DnsResponseCode (io.netty.handler.codec.dns.DnsResponseCode)1 DnsException (io.vertx.core.dns.DnsException)1 DnsResponseCode (io.vertx.core.dns.DnsResponseCode)1 LinkedHashMap (java.util.LinkedHashMap)1