类 io.netty.handler.codec.dns.DnsRawRecord 源码实例Demo

下面列出了怎么用 io.netty.handler.codec.dns.DnsRawRecord 的API类实例代码及写法,或者点击链接到github查看源代码。


private InetAddress parseAddress(DnsRecord r, String name) {
    if (!(r instanceof DnsRawRecord)) {
        return null;
    }
    final ByteBuf content = ((ByteBufHolder) r).content();
    final int contentLen = content.readableBytes();
    if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
        return null;
    }

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

    try {
        return InetAddress.getByAddress(
                parent.isDecodeIdn() ? IDN.toUnicode(name) : name, addrBytes);
    } catch (UnknownHostException e) {
        // Should never reach here.
        throw new Error(e);
    }
}
 

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>(min(8, answerCount));
        }

        cnames.put(r.name().toLowerCase(Locale.US), domainName.toLowerCase(Locale.US));
    }

    return cnames != null? cnames : Collections.<String, String>emptyMap();
}
 

void add(DnsRecord r) {
    if (r.type() != DnsRecordType.NS || !(r instanceof DnsRawRecord)) {
        return;
    }

    // Only include servers that serve the correct domain.
    if (questionName.length() <  r.name().length()) {
        return;
    }

    String recordName = r.name().toLowerCase(Locale.US);

    int dots = 0;
    for (int a = recordName.length() - 1, b = questionName.length() - 1; a >= 0; a--, b--) {
        char c = recordName.charAt(a);
        if (questionName.charAt(b) != c) {
            return;
        }
        if (c == '.') {
            dots++;
        }
    }

    if (head != null && head.dots > dots) {
        // We already have a closer match so ignore this one, no need to parse the domainName etc.
        return;
    }

    final ByteBuf recordContent = ((ByteBufHolder) r).content();
    final String domainName = decodeDomainName(recordContent);
    if (domainName == null) {
        // Could not be parsed, ignore.
        return;
    }

    // We are only interested in preserving the nameservers which are the closest to our qName, so ensure
    // we drop servers that have a smaller dots count.
    if (head == null || head.dots < dots) {
        count = 1;
        head = new AuthoritativeNameServer(dots, recordName, domainName);
    } else if (head.dots == dots) {
        AuthoritativeNameServer serverName = head;
        while (serverName.next != null) {
            serverName = serverName.next;
        }
        serverName.next = new AuthoritativeNameServer(dots, recordName, domainName);
        count++;
    }
}
 
源代码4 项目: netty-4.1.22   文件: DnsNameResolver.java

private static void validateAdditional(DnsRecord record, boolean validateType) {
    checkNotNull(record, "record");
    if (validateType && record instanceof DnsRawRecord) {
        throw new IllegalArgumentException("DnsRawRecord implementations not allowed: " + record);
    }
}
 
源代码5 项目: armeria   文件: DnsTextEndpointGroup.java

@Override
ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception {
    final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder();
    for (DnsRecord r : records) {
        if (!(r instanceof DnsRawRecord) || r.type() != DnsRecordType.TXT) {
            continue;
        }

        final ByteBuf content = ((ByteBufHolder) r).content();
        if (!content.isReadable()) { // Missing length octet
            warnInvalidRecord(DnsRecordType.TXT, content);
            continue;
        }

        content.markReaderIndex();
        final int txtLen = content.readUnsignedByte();
        if (txtLen == 0) { // Empty content
            continue;
        }

        if (content.readableBytes() != txtLen) { // Mismatching number of octets
            content.resetReaderIndex();
            warnInvalidRecord(DnsRecordType.TXT, content);
            continue;
        }

        final byte[] txt = new byte[txtLen];
        content.readBytes(txt);

        final Endpoint endpoint;
        try {
            endpoint = mapping.apply(txt);
        } catch (Exception e) {
            content.resetReaderIndex();
            warnInvalidRecord(DnsRecordType.TXT, content);
            continue;
        }

        if (endpoint != null) {
            builder.add(endpoint);
        }
    }

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

    return endpoints;
}
 
源代码6 项目: armeria   文件: DnsServiceEndpointGroup.java

@Override
ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception {
    final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder();
    for (DnsRecord r : records) {
        if (!(r instanceof DnsRawRecord) || r.type() != DnsRecordType.SRV) {
            continue;
        }

        final ByteBuf content = ((ByteBufHolder) r).content();
        if (content.readableBytes() <= 6) { // Too few bytes
            warnInvalidRecord(DnsRecordType.SRV, content);
            continue;
        }

        content.markReaderIndex();
        content.skipBytes(2);  // priority unused
        final int weight = content.readUnsignedShort();
        final int port = content.readUnsignedShort();

        final Endpoint endpoint;
        try {
            final String target = stripTrailingDot(DefaultDnsRecordDecoder.decodeName(content));
            endpoint = port > 0 ? Endpoint.of(target, port) : Endpoint.of(target);
        } catch (Exception e) {
            content.resetReaderIndex();
            warnInvalidRecord(DnsRecordType.SRV, content);
            continue;
        }

        builder.add(endpoint.withWeight(weight));
    }

    final ImmutableSortedSet<Endpoint> endpoints = builder.build();
    if (logger().isDebugEnabled()) {
        logger().debug("{} Resolved: {} (TTL: {})",
                       logPrefix(),
                       endpoints.stream()
                                .map(e -> e.authority() + '/' + e.weight())
                                .collect(Collectors.joining(", ")),
                       ttl);
    }

    return endpoints;
}
 
 类所在包
 同包方法