Example usage for io.netty.handler.codec.dns DnsRecord timeToLive

List of usage examples for io.netty.handler.codec.dns DnsRecord timeToLive

Introduction

In this page you can find the example usage for io.netty.handler.codec.dns DnsRecord timeToLive.

Prototype

long timeToLive();

Source Link

Document

Returns the time to live after reading for this resource record.

Usage

From source file:io.vertx.core.dns.impl.fix.DnsNameResolverContext.java

License:Apache License

private void onResponseAorAAAA(DnsRecordType qType, DnsQuestion question,
        AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {

    // We often get a bunch of CNAMES as well when we asked for A/AAAA.
    final DnsResponse response = envelope.content();
    final Map<String, String> cnames = buildAliasMap(response);
    final int answerCount = response.count(DnsSection.ANSWER);

    boolean found = false;
    for (int i = 0; i < answerCount; i++) {
        final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
        final DnsRecordType type = r.type();
        if (type != DnsRecordType.A && type != DnsRecordType.AAAA) {
            continue;
        }/*from w ww. j  a  v  a2 s  .c o  m*/

        final String qName = question.name().toLowerCase(Locale.US);
        final String rName = r.name().toLowerCase(Locale.US);

        // Make sure the record is for the questioned domain.
        if (!rName.equals(qName)) {
            // Even if the record's name is not exactly same, it might be an alias defined in the CNAME records.
            String resolved = qName;
            do {
                resolved = cnames.get(resolved);
                if (rName.equals(resolved)) {
                    break;
                }
            } while (resolved != null);

            if (resolved == null) {
                continue;
            }
        }

        if (!(r instanceof DnsRawRecord)) {
            continue;
        }

        final ByteBuf content = ((ByteBufHolder) r).content();
        final int contentLen = content.readableBytes();
        if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
            continue;
        }

        final byte[] addrBytes = new byte[contentLen];
        content.getBytes(content.readerIndex(), addrBytes);

        final InetAddress resolved;
        try {
            resolved = InetAddress.getByAddress(hostname, addrBytes);
        } catch (UnknownHostException e) {
            // Should never reach here.
            throw new Error(e);
        }

        if (resolvedEntries == null) {
            resolvedEntries = new ArrayList<DnsCacheEntry>(8);
        }

        final DnsCacheEntry e = new DnsCacheEntry(hostname, resolved);
        resolveCache.cache(hostname, resolved, r.timeToLive(), parent.ch.eventLoop());
        resolvedEntries.add(e);
        found = true;

        // Note that we do not break from the loop here, so we decode/cache all A/AAAA records.
    }

    if (found) {
        return;
    }

    if (traceEnabled) {
        addTrace(envelope.sender(), "no matching " + qType + " record found");
    }

    // We aked for A/AAAA but we got only CNAME.
    if (!cnames.isEmpty()) {
        onResponseCNAME(question, envelope, cnames, false);
    }
}