Example usage for io.netty.util NetUtil bytesToIpAddress

List of usage examples for io.netty.util NetUtil bytesToIpAddress

Introduction

In this page you can find the example usage for io.netty.util NetUtil bytesToIpAddress.

Prototype

public static String bytesToIpAddress(byte[] bytes, int offset, int length) 

Source Link

Document

Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.

Usage

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;
        }/*  w w  w.  jav  a  2 s. c o  m*/

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