Search in sources :

Example 41 with NetworkInterface

use of java.net.NetworkInterface in project ButterRemote-Android by se-bastiaan.

the class NetworkUtils method getMACAddress.

/**
     * Returns MAC address of the given interface name.
     *
     * @param interfaceName eth0, wlan0 or NULL=use first interface
     * @return mac address or empty string
     */
public static String getMACAddress(String interfaceName) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                return "";
            StringBuilder buf = new StringBuilder();
            for (int idx = 0; idx < mac.length; idx++) buf.append(String.format("%02X:", mac[idx]));
            if (buf.length() > 0)
                buf.deleteCharAt(buf.length() - 1);
            return buf.toString();
        }
    } catch (Exception ex) {
    }
    // for now eat exceptions
    return "";
/*try {
            // this is so Linux hack
            return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
        } catch (IOException ex) {
            return null;
        }*/
}
Also used : NetworkInterface(java.net.NetworkInterface)

Example 42 with NetworkInterface

use of java.net.NetworkInterface in project platform_frameworks_base by android.

the class WifiDisplayController method getInterfaceAddress.

private static Inet4Address getInterfaceAddress(WifiP2pGroup info) {
    NetworkInterface iface;
    try {
        iface = NetworkInterface.getByName(info.getInterface());
    } catch (SocketException ex) {
        Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface(), ex);
        return null;
    }
    Enumeration<InetAddress> addrs = iface.getInetAddresses();
    while (addrs.hasMoreElements()) {
        InetAddress addr = addrs.nextElement();
        if (addr instanceof Inet4Address) {
            return (Inet4Address) addr;
        }
    }
    Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface() + " because it had no IPv4 addresses.");
    return null;
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 43 with NetworkInterface

use of java.net.NetworkInterface in project otter by alibaba.

the class AddressUtils method getHostAddress.

public static InetAddress getHostAddress() {
    InetAddress localAddress = null;
    try {
        localAddress = InetAddress.getLocalHost();
        if (isValidHostAddress(localAddress)) {
            return localAddress;
        }
    } catch (Throwable e) {
        logger.warn("Failed to retriving local host ip address, try scan network card ip address. cause: " + e.getMessage());
    }
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        if (interfaces != null) {
            while (interfaces.hasMoreElements()) {
                try {
                    NetworkInterface network = interfaces.nextElement();
                    Enumeration<InetAddress> addresses = network.getInetAddresses();
                    if (addresses != null) {
                        while (addresses.hasMoreElements()) {
                            try {
                                InetAddress address = addresses.nextElement();
                                if (isValidHostAddress(address)) {
                                    return address;
                                }
                            } catch (Throwable e) {
                                logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
                            }
                        }
                    }
                } catch (Throwable e) {
                    logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
                }
            }
        }
    } catch (Throwable e) {
        logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
    }
    logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
    return localAddress;
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 44 with NetworkInterface

use of java.net.NetworkInterface in project otter by alibaba.

the class AddressUtils method isHostIp.

/**
     * 判断该ip是否为本机ip,一台机器可能同时有多个IP
     * 
     * @param ip
     * @return
     */
public static boolean isHostIp(String ip) {
    InetAddress localAddress = null;
    try {
        localAddress = InetAddress.getLocalHost();
        if (isValidHostAddress(localAddress) && (localAddress.getHostAddress().equals(ip) || localAddress.getHostName().equals(ip))) {
            return true;
        }
    } catch (Throwable e) {
        logger.warn("Failed to retriving local host ip address, try scan network card ip address. cause: " + e.getMessage());
    }
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        if (interfaces != null) {
            while (interfaces.hasMoreElements()) {
                try {
                    NetworkInterface network = interfaces.nextElement();
                    Enumeration<InetAddress> addresses = network.getInetAddresses();
                    if (addresses != null) {
                        while (addresses.hasMoreElements()) {
                            try {
                                InetAddress address = addresses.nextElement();
                                if (isValidHostAddress(address) && address.getHostAddress().equals(ip)) {
                                    return true;
                                }
                            } catch (Throwable e) {
                                logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
                            }
                        }
                    }
                } catch (Throwable e) {
                    logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
                }
            }
        }
    } catch (Throwable e) {
        logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
    }
    return false;
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 45 with NetworkInterface

use of java.net.NetworkInterface in project cassandra by apache.

the class DatabaseDescriptorTest method selectSuitableInterface.

/*
     * Server only accepts interfaces by name if they have a single address
     * OS X seems to always have an ipv4 and ipv6 address on all interfaces which means some tests fail
     * if not checked for and skipped
     */
@BeforeClass
public static void selectSuitableInterface() throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface intf = interfaces.nextElement();
        System.out.println("Evaluating " + intf.getName());
        if (intf.isLoopback()) {
            suitableInterface = intf;
            boolean hasIPv4 = false;
            boolean hasIPv6 = false;
            Enumeration<InetAddress> addresses = suitableInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                if (addresses.nextElement() instanceof Inet6Address)
                    hasIPv6 = true;
                else
                    hasIPv4 = true;
            }
            hasIPv4andIPv6 = hasIPv4 && hasIPv6;
            return;
        }
    }
}
Also used : NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) BeforeClass(org.junit.BeforeClass)

Aggregations

NetworkInterface (java.net.NetworkInterface)225 InetAddress (java.net.InetAddress)178 SocketException (java.net.SocketException)112 ArrayList (java.util.ArrayList)42 Inet4Address (java.net.Inet4Address)39 UnknownHostException (java.net.UnknownHostException)36 InterfaceAddress (java.net.InterfaceAddress)32 Inet6Address (java.net.Inet6Address)29 IOException (java.io.IOException)25 Enumeration (java.util.Enumeration)9 InetSocketAddress (java.net.InetSocketAddress)7 HashSet (java.util.HashSet)7 LinkedHashSet (java.util.LinkedHashSet)7 Command (com.android.server.NativeDaemonConnector.Command)6 Test (org.junit.Test)6 DatagramSocket (java.net.DatagramSocket)5 File (java.io.File)4 MulticastSocket (java.net.MulticastSocket)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3