Home / Class/ NativeInetAddress Class — netty Architecture

NativeInetAddress Class — netty Architecture

Architecture documentation for the NativeInetAddress class in NativeInetAddress.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3["NativeInetAddress"]
  721d8659_747c_0877_a046_e18927af5ebb["NativeInetAddress.java"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|defined in| 721d8659_747c_0877_a046_e18927af5ebb
  55503a66_5d9d_ab86_5bbb_fe252cf0d737["NativeInetAddress()"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|method| 55503a66_5d9d_ab86_5bbb_fe252cf0d737
  8d634805_2b62_972a_755c_f1eabad6946c["address()"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|method| 8d634805_2b62_972a_755c_f1eabad6946c
  bb3895d3_e587_930b_341d_658282ea404b["scopeId()"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|method| bb3895d3_e587_930b_341d_658282ea404b
  b00d13a5_4f19_23f6_c395_dd77ba52a972["ipv4MappedIpv6Address()"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|method| b00d13a5_4f19_23f6_c395_dd77ba52a972
  2467e30f_264f_e807_046a_286539b8d1a9["copyIpv4MappedIpv6Address()"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|method| 2467e30f_264f_e807_046a_286539b8d1a9
  cedb0830_08b2_c585_5975_b4a9bdbeae9e["InetSocketAddress()"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|method| cedb0830_08b2_c585_5975_b4a9bdbeae9e
  b912c6cd_787e_6c61_c70e_24982dc7bfb8["decodeInt()"]
  8c187a6a_d8a7_5d2b_d6bb_c850cff3c2c3 -->|method| b912c6cd_787e_6c61_c70e_24982dc7bfb8

Relationship Graph

Source Code

transport-native-unix-common/src/main/java/io/netty/channel/unix/NativeInetAddress.java lines 26–117

public final class NativeInetAddress {
    private static final byte[] IPV4_MAPPED_IPV6_PREFIX = {
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff };
    final byte[] address;
    final int scopeId;

    public static NativeInetAddress newInstance(InetAddress addr) {
        byte[] bytes = addr.getAddress();
        if (addr instanceof Inet6Address) {
            return new NativeInetAddress(bytes, ((Inet6Address) addr).getScopeId());
        } else {
            // convert to ipv4 mapped ipv6 address;
            return new NativeInetAddress(ipv4MappedIpv6Address(bytes));
        }
    }

    public NativeInetAddress(byte[] address, int scopeId) {
        this.address = address;
        this.scopeId = scopeId;
    }

    public NativeInetAddress(byte[] address) {
        this(address, 0);
    }

    public byte[] address() {
        return address;
    }

    public int scopeId() {
        return scopeId;
    }

    public static byte[] ipv4MappedIpv6Address(byte[] ipv4) {
        byte[] address = new byte[16];
        copyIpv4MappedIpv6Address(ipv4, address);
        return address;
    }

    public static void copyIpv4MappedIpv6Address(byte[] ipv4, byte[] ipv6) {
        System.arraycopy(IPV4_MAPPED_IPV6_PREFIX, 0, ipv6, 0, IPV4_MAPPED_IPV6_PREFIX.length);
        System.arraycopy(ipv4, 0, ipv6, 12, ipv4.length);
    }

    public static InetSocketAddress address(byte[] addr, int offset, int len) {
        // The last 4 bytes are always the port
        final int port = decodeInt(addr, offset + len - 4);
        final InetAddress address;
        try {
            switch (len) {
                // 8 bytes:
                // - 4  == ipaddress
                // - 4  == port
                case 8:
                    byte[] ipv4 = new byte[4];
                    System.arraycopy(addr, offset, ipv4, 0, 4);
                    address = InetAddress.getByAddress(ipv4);
                    break;

                // 24 bytes:
                // - 16  == ipaddress
                // - 4   == scopeId
                // - 4   == port
                case 24:
                    byte[] ipv6 = new byte[16];
                    System.arraycopy(addr, offset, ipv6, 0, 16);
                    int scopeId = decodeInt(addr, offset + len  - 8);
                    // Only include the scopeId if its either non 0 or if this is a link-local address
                    // as scopeId is only supported with it:
                    // See also https://man7.org/linux/man-pages/man7/ipv6.7.html
                    if (scopeId != 0 || (ipv6[0] == (byte) 0xfe && ipv6[1] == (byte) 0x80)) {
                        address = Inet6Address.getByAddress(null, ipv6, scopeId);
                    } else {
                        address = InetAddress.getByAddress(null, ipv6);
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported length: " + len + " (allowed: 8 or 24)");
            }
            return new InetSocketAddress(address, port);
        } catch (UnknownHostException e) {

Frequently Asked Questions

What is the NativeInetAddress class?
NativeInetAddress is a class in the netty codebase, defined in transport-native-unix-common/src/main/java/io/netty/channel/unix/NativeInetAddress.java.
Where is NativeInetAddress defined?
NativeInetAddress is defined in transport-native-unix-common/src/main/java/io/netty/channel/unix/NativeInetAddress.java at line 26.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free