Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

In this page you can find the example usage for java.net NetworkInterface getNetworkInterfaces.

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:ca.psiphon.PsiphonTunnel.java

private static PrivateAddress selectPrivateAddress() throws Exception {
    // Select one of 10.0.0.1, 172.16.0.1, or 192.168.0.1 depending on
    // which private address range isn't in use.

    Map<String, PrivateAddress> candidates = new HashMap<String, PrivateAddress>();
    candidates.put("10", new PrivateAddress("10.0.0.1", "10.0.0.0", 8, "10.0.0.2"));
    candidates.put("172", new PrivateAddress("172.16.0.1", "172.16.0.0", 12, "172.16.0.2"));
    candidates.put("192", new PrivateAddress("192.168.0.1", "192.168.0.0", 16, "192.168.0.2"));
    candidates.put("169", new PrivateAddress("169.254.1.1", "169.254.1.0", 24, "169.254.1.2"));

    List<NetworkInterface> netInterfaces;
    try {/*w w  w .  j  a  v  a2 s  .  c  o  m*/
        netInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
    } catch (SocketException e) {
        throw new Exception("selectPrivateAddress failed", e);
    }

    for (NetworkInterface netInterface : netInterfaces) {
        for (InetAddress inetAddress : Collections.list(netInterface.getInetAddresses())) {
            String ipAddress = inetAddress.getHostAddress();
            if (InetAddressUtils.isIPv4Address(ipAddress)) {
                if (ipAddress.startsWith("10.")) {
                    candidates.remove("10");
                } else if (ipAddress.length() >= 6 && ipAddress.substring(0, 6).compareTo("172.16") >= 0
                        && ipAddress.substring(0, 6).compareTo("172.31") <= 0) {
                    candidates.remove("172");
                } else if (ipAddress.startsWith("192.168")) {
                    candidates.remove("192");
                }
            }
        }
    }

    if (candidates.size() > 0) {
        return candidates.values().iterator().next();
    }

    throw new Exception("no private address available");
}

From source file:com.taobao.adfs.util.Utilities.java

public static List<InetAddress> getInetAddressList() throws IOException {
    List<InetAddress> addressList = new ArrayList<InetAddress>();
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
        NetworkInterface netInterface = netInterfaces.nextElement();
        Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
            InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress != null && inetAddress instanceof Inet4Address)
                addressList.add(inetAddress);
        }/*from w  ww . j a  v  a 2  s  .  c om*/
    }

    List<InetAddress> newAddressList = getWanInetAddressList(addressList);
    newAddressList.addAll(getLanInetAddressList(addressList));
    newAddressList.addAll(getLoopbackInetAddressList(addressList));
    newAddressList.addAll(getLinklocalInetAddressList(addressList));

    return newAddressList;
}

From source file:com.example.zoetablet.BasicFragmentActivity.java

protected static String getLocalIpAddress() {
    try {//  w ww  .j a v a2  s.  com
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("Tablet", ex.toString());
    }
    return null;
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Return an InetAddress for each interface that matches the given subnet specified using CIDR notation.
 *
 * @param subnet              subnet specified using CIDR notation
 * @param returnSubinterfaces whether to return IPs associated with subinterfaces
 *
 * @throws IllegalArgumentException if subnet is invalid
 *///from  w  w  w .  j av a 2  s .  c om
public static List<InetAddress> getIPs(String subnet, boolean returnSubinterfaces) {
    List<InetAddress> addrs = new ArrayList<InetAddress>();
    SubnetInfo subnetInfo = new SubnetUtils(subnet).getInfo();
    Enumeration<NetworkInterface> nifs;

    try {
        nifs = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Unable to get host interfaces", e);
        return addrs;
    }

    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        // NB: adding addresses even if the nif is not up
        addMatchingAddrs(nif, subnetInfo, addrs);

        if (!returnSubinterfaces) {
            continue;
        }
        Enumeration<NetworkInterface> subNifs = nif.getSubInterfaces();
        while (subNifs.hasMoreElements()) {
            addMatchingAddrs(subNifs.nextElement(), subnetInfo, addrs);
        }
    }
    return addrs;
}

From source file:com.lfv.yada.net.client.ClientNetworkManager.java

private InetAddress getLocalHostFix() throws SocketException {
    // Hack for getting the localhost inetaddress on linux systems
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface networkInterface = (NetworkInterface) e.nextElement();
        Enumeration e2 = networkInterface.getInetAddresses();
        while (e2.hasMoreElements()) {
            InetAddress ip = (InetAddress) e2.nextElement();
            if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                return ip;
            }//w w  w .  j  a v  a 2s .  co m
        }
    }
    return null;
}

From source file:it.isislab.sof.core.engine.hadoop.sshclient.connection.SofManager.java

/**
 * //  w ww .  ja v  a  2  s .  co  m
 * @return a unique filename. If there is execption, it will be return MD5(System.currentTimeMillis)
 */
public static String getMexID() {
    InetAddress addr;
    try {
        addr = InetAddress.getLocalHost();
        Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
        while (networks.hasMoreElements()) {
            NetworkInterface network = networks.nextElement();
            byte[] mac = network.getHardwareAddress();

            if (mac != null) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                }
                return DigestUtils.md5Hex(sb.toString() + (System.currentTimeMillis() + ""));
            }
        }
        return DigestUtils.md5Hex(System.currentTimeMillis() + "");
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return DigestUtils.md5Hex(System.currentTimeMillis() + "");
    } catch (SocketException e) {
        e.printStackTrace();
        return DigestUtils.md5Hex(System.currentTimeMillis() + "");
    }
}

From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java

public static List<InetAddress> getAllIPv4Addrs() {
    if (allIPv4Addrs == null) {
        allIPv4Addrs = new ArrayList<InetAddress>();
        try {//  w  ww. ja  v  a 2  s.c o m
            Enumeration<NetworkInterface> localInterfaces = NetworkInterface.getNetworkInterfaces();
            if (localInterfaces != null) {
                while (localInterfaces.hasMoreElements()) {
                    NetworkInterface netIf = localInterfaces.nextElement();
                    Enumeration<InetAddress> ifAddrs = netIf.getInetAddresses();
                    while (ifAddrs.hasMoreElements()) {
                        InetAddress ip = ifAddrs.nextElement();
                        if (ip instanceof Inet4Address) {
                            allIPv4Addrs.add(ip);
                        }
                    }
                }
            } else {
                log.error("No network interfaces found!");
            }
        } catch (IOException ex) {
            log.error("Failed to get IPv4 addresses: " + ex);
        }
    }
    return allIPv4Addrs;
}

From source file:net.spfbl.core.Core.java

private static boolean hasInterface(String netInterface) {
    try {//  ww  w.j a va2 s.  c o  m
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            if (netInterface.equals(netint.getName())) {
                return true;
            }
        }
        return false;
    } catch (SocketException ex) {
        return false;
    }
}

From source file:com.rockagen.commons.util.CommUtil.java

/**
 * Returns the hardware address (usually MAC) of the interface if it
 * has one and if it can be accessed given the current privileges.
 *
 * @return a char array containing the first address or <code>null</code> if
 * the address doesn't exist or is not accessible.
 * @throws SocketException if an I/O error occurs.
 * @since 1.6/*  w w w  .j a va2  s. c om*/
 */
public static String[] getMacAddrs() throws SocketException {
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    StringBuilder buf = new StringBuilder();
    while (e.hasMoreElements()) {
        NetworkInterface network = e.nextElement();
        if (network != null) {
            byte[] nmac = network.getHardwareAddress();
            if (nmac != null) {
                buf.append(hexdump(nmac));
                buf.append("#");
            }
        }
    }
    return buf.toString().split("#");
}

From source file:org.wso2.carbon.appmanager.integration.ui.APPManagerIntegrationTest.java

protected String getNetworkIPAddress() {
    String networkIpAddress = null;
    try {/*from w ww .  ja v a2s  .  co m*/
        String localhost = InetAddress.getLocalHost().getHostAddress();
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) e.nextElement();
            if (ni.isLoopback())
                continue;
            if (ni.isPointToPoint())
                continue;
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = (InetAddress) addresses.nextElement();
                if (address instanceof Inet4Address) {
                    String ip = address.getHostAddress();
                    if (!ip.equals(localhost)) {
                        networkIpAddress = ip;
                    }

                }
            }
        }
    } catch (UnknownHostException e) {
        log.error("Error occurred due to an unknown host.", e);
    } catch (SocketException e) {
        log.error("Error occurred with Socket connections", e);
    }
    return networkIpAddress;
}