Home / Class/ MacAddressUtil Class — netty Architecture

MacAddressUtil Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  a4685b43_b51c_914d_456a_1efa65aff7a6["MacAddressUtil"]
  5b771475_3e26_448a_6463_fce27e6785f3["MacAddressUtil.java"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|defined in| 5b771475_3e26_448a_6463_fce27e6785f3
  076ea31f_ecd7_dd77_e9b3_a6aaa2aded58["bestAvailableMac()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| 076ea31f_ecd7_dd77_e9b3_a6aaa2aded58
  ae9e0cc2_d1a5_209d_7506_53283cf656bf["defaultMachineId()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| ae9e0cc2_d1a5_209d_7506_53283cf656bf
  305a02f9_0c0f_c367_f9d6_4e47f4b9319e["parseMAC()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| 305a02f9_0c0f_c367_f9d6_4e47f4b9319e
  5362bac4_444d_c013_8aa9_3e9edb8ab4ee["validateMacSeparator()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| 5362bac4_444d_c013_8aa9_3e9edb8ab4ee
  e95c566c_64f8_1d1a_596e_d78a1850042e["String()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| e95c566c_64f8_1d1a_596e_d78a1850042e
  bce5fa35_f827_f8d9_1f74_97f9adbfbbef["compareAddresses()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| bce5fa35_f827_f8d9_1f74_97f9adbfbbef
  9305d4fa_a1fb_1272_769e_bfae8c4ad4c4["scoreAddress()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| 9305d4fa_a1fb_1272_769e_bfae8c4ad4c4
  cdad9b36_adc8_7fd7_d7fa_89c43f9acebf["MacAddressUtil()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6 -->|method| cdad9b36_adc8_7fd7_d7fa_89c43f9acebf

Relationship Graph

Source Code

common/src/main/java/io/netty/util/internal/MacAddressUtil.java lines 35–270

public final class MacAddressUtil {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(MacAddressUtil.class);

    private static final int EUI64_MAC_ADDRESS_LENGTH = 8;
    private static final int EUI48_MAC_ADDRESS_LENGTH = 6;

    /**
     * Obtains the best MAC address found on local network interfaces.
     * Generally speaking, an active network interface used on public
     * networks is better than a local network interface.
     *
     * @return byte array containing a MAC. null if no MAC can be found.
     */
    public static byte[] bestAvailableMac() {
        // Find the best MAC address available.
        byte[] bestMacAddr = EMPTY_BYTES;
        InetAddress bestInetAddr = NetUtil.LOCALHOST4;

        // Retrieve the list of available network interfaces.
        Map<NetworkInterface, InetAddress> ifaces = new LinkedHashMap<NetworkInterface, InetAddress>();
        for (NetworkInterface iface: NetUtil.NETWORK_INTERFACES) {
            // Use the interface with proper INET addresses only.
            Enumeration<InetAddress> addrs = SocketUtils.addressesFromNetworkInterface(iface);
            if (addrs.hasMoreElements()) {
                InetAddress a = addrs.nextElement();
                if (!a.isLoopbackAddress()) {
                    ifaces.put(iface, a);
                }
            }
        }

        for (Entry<NetworkInterface, InetAddress> entry: ifaces.entrySet()) {
            NetworkInterface iface = entry.getKey();
            InetAddress inetAddr = entry.getValue();
            if (iface.isVirtual()) {
                continue;
            }

            byte[] macAddr;
            try {
                macAddr = SocketUtils.hardwareAddressFromNetworkInterface(iface);
            } catch (SocketException e) {
                logger.debug("Failed to get the hardware address of a network interface: {}", iface, e);
                continue;
            }

            boolean replace = false;
            int res = compareAddresses(bestMacAddr, macAddr);
            if (res < 0) {
                // Found a better MAC address.
                replace = true;
            } else if (res == 0) {
                // Two MAC addresses are of pretty much same quality.
                res = compareAddresses(bestInetAddr, inetAddr);
                if (res < 0) {
                    // Found a MAC address with better INET address.
                    replace = true;
                } else if (res == 0) {
                    // Cannot tell the difference.  Choose the longer one.
                    if (bestMacAddr.length < macAddr.length) {
                        replace = true;
                    }
                }
            }

            if (replace) {
                bestMacAddr = macAddr;
                bestInetAddr = inetAddr;
            }
        }

        if (bestMacAddr == EMPTY_BYTES) {
            return null;
        }

        if (bestMacAddr.length == EUI48_MAC_ADDRESS_LENGTH) { // EUI-48 - convert to EUI-64
            byte[] newAddr = new byte[EUI64_MAC_ADDRESS_LENGTH];
            System.arraycopy(bestMacAddr, 0, newAddr, 0, 3);
            newAddr[3] = (byte) 0xFF;
            newAddr[4] = (byte) 0xFE;
            System.arraycopy(bestMacAddr, 3, newAddr, 5, 3);

Frequently Asked Questions

What is the MacAddressUtil class?
MacAddressUtil is a class in the netty codebase, defined in common/src/main/java/io/netty/util/internal/MacAddressUtil.java.
Where is MacAddressUtil defined?
MacAddressUtil is defined in common/src/main/java/io/netty/util/internal/MacAddressUtil.java at line 35.

Analyze Your Own Codebase

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

Try Supermodel Free