Example usage for io.netty.handler.codec.dns DnsRecordType AAAA

List of usage examples for io.netty.handler.codec.dns DnsRecordType AAAA

Introduction

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

Prototype

DnsRecordType AAAA

To view the source code for io.netty.handler.codec.dns DnsRecordType AAAA.

Click Source Link

Document

IPv6 address record RFC 3596 Returns a 128-bit IPv6 address, most commonly used to map hostnames to an IP address of the host.

Usage

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroup.java

License:Apache License

private static List<DnsQuestion> newQuestions(String hostname,
        @Nullable ResolvedAddressTypes resolvedAddressTypes) {

    if (resolvedAddressTypes == null) {
        if (NetUtil.isIpV4StackPreferred()) {
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_ONLY;
        } else {/*w  w w . j a  va 2s. c om*/
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_PREFERRED;
        }
    }

    final ImmutableList.Builder<DnsQuestion> builder = ImmutableList.builder();
    switch (resolvedAddressTypes) {
    case IPV4_ONLY:
    case IPV4_PREFERRED:
    case IPV6_PREFERRED:
        builder.add(new DefaultDnsQuestion(hostname, DnsRecordType.A));
        break;
    }
    switch (resolvedAddressTypes) {
    case IPV6_ONLY:
    case IPV4_PREFERRED:
    case IPV6_PREFERRED:
        builder.add(new DefaultDnsQuestion(hostname, DnsRecordType.AAAA));
        break;
    }
    return builder.build();
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroup.java

License:Apache License

@Override
ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception {
    final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder();
    final boolean hasLoopbackARecords = records.stream().filter(r -> r instanceof DnsRawRecord)
            .map(DnsRawRecord.class::cast).anyMatch(
                    r -> r.type() == DnsRecordType.A && r.content().getByte(r.content().readerIndex()) == 127);

    for (DnsRecord r : records) {
        if (!(r instanceof DnsRawRecord)) {
            continue;
        }/*from  w  ww  .jav  a2  s  .  c om*/

        final DnsRecordType type = r.type();
        final ByteBuf content = ((ByteBufHolder) r).content();
        final int contentLen = content.readableBytes();

        // Skip invalid records.
        if (type == DnsRecordType.A) {
            if (contentLen != 4) {
                warnInvalidRecord(DnsRecordType.A, content);
                continue;
            }
        } else if (type == DnsRecordType.AAAA) {
            if (contentLen != 16) {
                warnInvalidRecord(DnsRecordType.AAAA, content);
                continue;
            }
        } else {
            continue;
        }

        // Convert the content into an IP address and then into an endpoint.
        final String ipAddr;
        final byte[] addrBytes = new byte[contentLen];
        content.getBytes(content.readerIndex(), addrBytes);

        if (contentLen == 16) {
            // Convert some IPv6 addresses into IPv4 addresses to remove duplicate endpoints.
            if (addrBytes[0] == 0x00 && addrBytes[1] == 0x00 && addrBytes[2] == 0x00 && addrBytes[3] == 0x00
                    && addrBytes[4] == 0x00 && addrBytes[5] == 0x00 && addrBytes[6] == 0x00
                    && addrBytes[7] == 0x00 && addrBytes[8] == 0x00 && addrBytes[9] == 0x00) {

                if (addrBytes[10] == 0x00 && addrBytes[11] == 0x00) {
                    if (addrBytes[12] == 0x00 && addrBytes[13] == 0x00 && addrBytes[14] == 0x00
                            && addrBytes[15] == 0x01) {
                        // Loopback address (::1)
                        if (hasLoopbackARecords) {
                            // Contains an IPv4 loopback address already; skip.
                            continue;
                        } else {
                            ipAddr = "::1";
                        }
                    } else {
                        // IPv4-compatible address.
                        ipAddr = NetUtil.bytesToIpAddress(addrBytes, 12, 4);
                    }
                } else if (addrBytes[10] == -1 && addrBytes[11] == -1) {
                    // IPv4-mapped address.
                    ipAddr = NetUtil.bytesToIpAddress(addrBytes, 12, 4);
                } else {
                    ipAddr = NetUtil.bytesToIpAddress(addrBytes);
                }
            } else {
                ipAddr = NetUtil.bytesToIpAddress(addrBytes);
            }
        } else {
            ipAddr = NetUtil.bytesToIpAddress(addrBytes);
        }

        final Endpoint endpoint = port != 0 ? Endpoint.of(hostname, port) : Endpoint.of(hostname);
        builder.add(endpoint.withIpAddr(ipAddr));
    }

    final ImmutableSortedSet<Endpoint> endpoints = builder.build();
    if (logger().isDebugEnabled()) {
        logger().debug("{} Resolved: {} (TTL: {})", logPrefix(),
                endpoints.stream().map(Endpoint::ipAddr).collect(Collectors.joining(", ")), ttl);
    }

    return endpoints;
}

From source file:io.vertx.core.dns.impl.DnsClientImpl.java

License:Open Source License

@Override
public DnsClient lookup6(String name, Handler<AsyncResult<String>> handler) {
    lookupSingle(name, handler, DnsRecordType.AAAA);
    return this;
}

From source file:io.vertx.core.dns.impl.DnsClientImpl.java

License:Open Source License

@Override
public DnsClient lookup(String name, Handler<AsyncResult<String>> handler) {
    lookupSingle(name, handler, DnsRecordType.A, DnsRecordType.AAAA);
    return this;
}

From source file:io.vertx.core.dns.impl.DnsClientImpl.java

License:Open Source License

@Override
public DnsClient resolveAAAA(String name, Handler<AsyncResult<List<String>>> handler) {
    lookupList(name, handler, DnsRecordType.AAAA);
    return this;
}

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

License:Apache License

void resolve() {
    InetSocketAddress nameServerAddrToTry = nameServerAddrs.next();
    for (InternetProtocolFamily f : resolveAddressTypes) {
        final DnsRecordType type;
        switch (f) {
        case IPv4:
            type = DnsRecordType.A;/*w ww  . ja  v  a2 s. c o  m*/
            break;
        case IPv6:
            type = DnsRecordType.AAAA;
            break;
        default:
            throw new Error();
        }

        query(nameServerAddrToTry, new DefaultDnsQuestion(hostname, type));
    }
}

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

License:Apache License

void onResponse(final DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {
    try {//w  ww.  ja v  a2 s  . c  o  m
        final DnsResponse res = envelope.content();
        final DnsResponseCode code = res.code();
        if (code == DnsResponseCode.NOERROR) {
            final DnsRecordType type = question.type();
            if (type == DnsRecordType.A || type == DnsRecordType.AAAA) {
                onResponseAorAAAA(type, question, envelope);
            } else if (type == DnsRecordType.CNAME) {
                onResponseCNAME(question, envelope);
            }
            return;
        }

        if (traceEnabled) {
            addTrace(envelope.sender(), "response code: " + code + " with " + res.count(DnsSection.ANSWER)
                    + " answer(s) and " + res.count(DnsSection.AUTHORITY) + " authority resource(s)");
        }

        // Retry with the next server if the server did not tell us that the domain does not exist.
        if (code != DnsResponseCode.NXDOMAIN) {
            query(nameServerAddrs.next(), question);
        }
    } finally {
        ReferenceCountUtil.safeRelease(envelope);
    }
}

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 ww  w . j  a v a2s  .com*/

        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);
    }
}

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

License:Apache License

private void followCname(InetSocketAddress nameServerAddr, String name, String cname) {

    if (traceEnabled) {
        if (trace == null) {
            trace = new StringBuilder(128);
        }/* w  ww  . j a va 2s.c  o  m*/

        trace.append(StringUtil.NEWLINE);
        trace.append("\tfrom ");
        trace.append(nameServerAddr);
        trace.append(": ");
        trace.append(name);
        trace.append(" CNAME ");
        trace.append(cname);
    }

    final InetSocketAddress nextAddr = nameServerAddrs.next();
    query(nextAddr, new DefaultDnsQuestion(cname, DnsRecordType.A));
    query(nextAddr, new DefaultDnsQuestion(cname, DnsRecordType.AAAA));
}