Home / Function/ bestAvailableMac() — netty Function Reference

bestAvailableMac() — netty Function Reference

Architecture documentation for the bestAvailableMac() function in MacAddressUtil.java from the netty codebase.

Function java CommonUtil Internal calls 1 called by 1

Entity Profile

Dependency Diagram

graph TD
  076ea31f_ecd7_dd77_e9b3_a6aaa2aded58["bestAvailableMac()"]
  a4685b43_b51c_914d_456a_1efa65aff7a6["MacAddressUtil"]
  076ea31f_ecd7_dd77_e9b3_a6aaa2aded58 -->|defined in| a4685b43_b51c_914d_456a_1efa65aff7a6
  ae9e0cc2_d1a5_209d_7506_53283cf656bf["defaultMachineId()"]
  ae9e0cc2_d1a5_209d_7506_53283cf656bf -->|calls| 076ea31f_ecd7_dd77_e9b3_a6aaa2aded58
  bce5fa35_f827_f8d9_1f74_97f9adbfbbef["compareAddresses()"]
  076ea31f_ecd7_dd77_e9b3_a6aaa2aded58 -->|calls| bce5fa35_f827_f8d9_1f74_97f9adbfbbef
  style 076ea31f_ecd7_dd77_e9b3_a6aaa2aded58 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

common/src/main/java/io/netty/util/internal/MacAddressUtil.java lines 48–123

    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);
            bestMacAddr = newAddr;
        } else {
            // Unknown
            bestMacAddr = Arrays.copyOf(bestMacAddr, EUI64_MAC_ADDRESS_LENGTH);
        }

        return bestMacAddr;
    }

Domain

Subdomains

Called By

Frequently Asked Questions

What does bestAvailableMac() do?
bestAvailableMac() is a function in the netty codebase, defined in common/src/main/java/io/netty/util/internal/MacAddressUtil.java.
Where is bestAvailableMac() defined?
bestAvailableMac() is defined in common/src/main/java/io/netty/util/internal/MacAddressUtil.java at line 48.
What does bestAvailableMac() call?
bestAvailableMac() calls 1 function(s): compareAddresses.
What calls bestAvailableMac()?
bestAvailableMac() is called by 1 function(s): defaultMachineId.

Analyze Your Own Codebase

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

Try Supermodel Free