Search in sources :

Example 6 with DnsRecordType

use of io.netty.handler.codec.dns.DnsRecordType 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 DnsRecordType

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

the class DnsNameResolverContext method buildAliasMap.

private static Map<String, String> buildAliasMap(DnsResponse response) {
    final int answerCount = response.count(DnsSection.ANSWER);
    Map<String, String> cnames = null;
    for (int i = 0; i < answerCount; i++) {
        final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
        final DnsRecordType type = r.type();
        if (type != DnsRecordType.CNAME) {
            continue;
        }
        if (!(r instanceof DnsRawRecord)) {
            continue;
        }
        final ByteBuf recordContent = ((ByteBufHolder) r).content();
        final String domainName = decodeDomainName(recordContent);
        if (domainName == null) {
            continue;
        }
        if (cnames == null) {
            cnames = new HashMap<String, String>();
        }
        cnames.put(r.name().toLowerCase(Locale.US), domainName.toLowerCase(Locale.US));
    }
    return cnames != null ? cnames : Collections.<String, String>emptyMap();
}
Also used : DnsRawRecord(io.netty.handler.codec.dns.DnsRawRecord) ByteBufHolder(io.netty.buffer.ByteBufHolder) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) DnsRecord(io.netty.handler.codec.dns.DnsRecord) ByteBuf(io.netty.buffer.ByteBuf)

Example 8 with DnsRecordType

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

the class RecordDecoder method decode.

/**
 * Decodes a resource record and returns the result.
 *
 * @param record
 * @return the decoded resource record
 */
@SuppressWarnings("unchecked")
public static <T> T decode(DnsRecord record) {
    DnsRecordType type = record.type();
    Function<DnsRecord, ?> decoder = decoders.get(type);
    if (decoder == null) {
        throw new IllegalStateException("Unsupported resource record type [id: " + type + "].");
    }
    T result = null;
    try {
        result = (T) decoder.apply(record);
    } catch (Exception e) {
        log.error(e.getMessage(), e.getCause());
    }
    return result;
}
Also used : DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) DnsRecord(io.netty.handler.codec.dns.DnsRecord) DecoderException(io.netty.handler.codec.DecoderException) UnknownHostException(java.net.UnknownHostException)

Example 9 with DnsRecordType

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

the class DnsResolveContext method onResponse.

private void onResponse(final DnsServerAddressStream nameServerAddrStream, final int nameServerAddrStreamIndex, final DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope, final DnsQueryLifecycleObserver queryLifecycleObserver, Promise<List<T>> promise) {
    try {
        final DnsResponse res = envelope.content();
        final DnsResponseCode code = res.code();
        if (code == DnsResponseCode.NOERROR) {
            if (handleRedirect(question, envelope, queryLifecycleObserver, promise)) {
                // Was a redirect so return here as everything else is handled in handleRedirect(...)
                return;
            }
            final DnsRecordType type = question.type();
            if (type == DnsRecordType.CNAME) {
                onResponseCNAME(question, buildAliasMap(envelope.content(), cnameCache(), parent.executor()), queryLifecycleObserver, promise);
                return;
            }
            for (DnsRecordType expectedType : expectedTypes) {
                if (type == expectedType) {
                    onExpectedResponse(question, envelope, queryLifecycleObserver, promise);
                    return;
                }
            }
            queryLifecycleObserver.queryFailed(UNRECOGNIZED_TYPE_QUERY_FAILED_EXCEPTION);
            return;
        }
        // Retry with the next server if the server did not tell us that the domain does not exist.
        if (code != DnsResponseCode.NXDOMAIN) {
            query(nameServerAddrStream, nameServerAddrStreamIndex + 1, question, queryLifecycleObserver.queryNoAnswer(code), true, promise, null);
        } else {
            queryLifecycleObserver.queryFailed(NXDOMAIN_QUERY_FAILED_EXCEPTION);
            // ....
            if (!res.isAuthoritativeAnswer()) {
                query(nameServerAddrStream, nameServerAddrStreamIndex + 1, question, newDnsQueryLifecycleObserver(question), true, promise, null);
            }
        }
    } finally {
        ReferenceCountUtil.safeRelease(envelope);
    }
}
Also used : DnsResponse(io.netty.handler.codec.dns.DnsResponse) DnsResponseCode(io.netty.handler.codec.dns.DnsResponseCode) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType)

Aggregations

DnsRecordType (io.netty.handler.codec.dns.DnsRecordType)9 DnsRecord (io.netty.handler.codec.dns.DnsRecord)7 DnsResponse (io.netty.handler.codec.dns.DnsResponse)5 ByteBuf (io.netty.buffer.ByteBuf)3 ByteBufHolder (io.netty.buffer.ByteBufHolder)2 DnsRawRecord (io.netty.handler.codec.dns.DnsRawRecord)2 DnsResponseCode (io.netty.handler.codec.dns.DnsResponseCode)2 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 DecoderException (io.netty.handler.codec.DecoderException)1 DatagramDnsQuery (io.netty.handler.codec.dns.DatagramDnsQuery)1 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)1 DefaultDnsRawRecord (io.netty.handler.codec.dns.DefaultDnsRawRecord)1 DnsException (io.vertx.core.dns.DnsException)1 DnsResponseCode (io.vertx.core.dns.DnsResponseCode)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1