Example usage for java.net NetworkInterface getInetAddresses

List of usage examples for java.net NetworkInterface getInetAddresses

Introduction

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

Prototype

public Enumeration<InetAddress> getInetAddresses() 

Source Link

Document

Get an Enumeration with all or a subset of the InetAddresses bound to this network interface.

Usage

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

/**
 * Add all addresses associated with the given nif in the given subnet to the given list.
 *//*w w w .j  a  v  a 2  s  .  co m*/
private static void addMatchingAddrs(NetworkInterface nif, SubnetInfo subnetInfo, List<InetAddress> addrs) {
    Enumeration<InetAddress> ifAddrs = nif.getInetAddresses();
    while (ifAddrs.hasMoreElements()) {
        InetAddress ifAddr = ifAddrs.nextElement();
        if (subnetInfo.isInRange(ifAddr.getHostAddress())) {
            addrs.add(ifAddr);
        }
    }
}

From source file:ws.argo.DemoWebClient.Browser.BrowserController.java

private static String getHostIPAddressFromString(String propIPAddress, String niName) {
    String hostIPAddr = propIPAddress;

    if (propIPAddress.equals("")) {
        // If the listenerIPAddress is blank, then try to get the ip address of
        // the interface

        try {//ww  w. java  2s . com
            NetworkInterface ni = null;
            if (niName != null)
                ni = NetworkInterface.getByName(niName);
            if (ni == null) {
                LOGGER.info("Network Interface name not specified or incorrect.  Defaulting to localhost");
                hostIPAddr = InetAddress.getLocalHost().getHostAddress();
            } else {

                if (!ni.isLoopback()) {

                    Enumeration<InetAddress> ee = ni.getInetAddresses();
                    while (ee.hasMoreElements()) {
                        InetAddress i = (InetAddress) ee.nextElement();
                        if (i instanceof Inet4Address) {
                            hostIPAddr = i.getHostAddress();
                            break; // get the first one an get out of the loop
                        }
                    }
                }
            }

        } catch (SocketException e) {
            LOGGER.warn("A socket exception occurred.");
        } catch (UnknownHostException e) {
            LOGGER.warn("Error finding Network Interface", e);
        }

    }

    return hostIPAddr;
}

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 {/*from  w w w  .ja 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:adams.core.net.InternetHelper.java

/**
 * Returns the IP address determined from the network interfaces (using
 * the IP address of the one with a proper host name).
 *
 * @return       the IP address/*w  w  w  .  jav  a  2  s .  com*/
 */
public static synchronized String getIPFromNetworkInterface() {
    String result;
    List<String> list;
    Enumeration<NetworkInterface> enmI;
    NetworkInterface intf;
    Enumeration<InetAddress> enmA;
    InetAddress addr;
    boolean found;

    result = null;

    if (m_IPNetworkInterface == null) {
        list = new ArrayList<>();
        found = false;
        try {
            enmI = NetworkInterface.getNetworkInterfaces();
            while (enmI.hasMoreElements()) {
                intf = enmI.nextElement();
                // skip non-active ones
                if (!intf.isUp())
                    continue;
                enmA = intf.getInetAddresses();
                while (enmA.hasMoreElements()) {
                    addr = enmA.nextElement();
                    list.add(addr.getHostAddress());
                    if (addr.getHostName().indexOf(':') == -1) {
                        result = addr.getHostAddress();
                        found = true;
                        break;
                    }
                }
                if (found)
                    break;
            }
        } catch (Exception e) {
            // ignored
        }

        if (result == null) {
            if (list.size() > 0)
                result = list.get(0);
            else
                result = "<unknown>";
        }

        m_IPNetworkInterface = result;
    } else {
        result = m_IPNetworkInterface;
    }

    return result;
}

From source file:org.speechforge.zanzibar.sip.SipServer.java

public static InetAddress getLocalHost() throws SocketException, UnknownHostException {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface networkInterface = networkInterfaces.nextElement();
        if (!networkInterface.isLoopback()) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            if (inetAddresses.hasMoreElements())
                return inetAddresses.nextElement();
        }/*from   w w  w . j a  va 2 s .  c o m*/
    }
    return InetAddress.getLocalHost();
}

From source file:adams.core.net.InternetHelper.java

/**
 * Returns the host name determined from the network interfaces.
 *
 * @return       the host name, null if none found
 *///ww w.  j a  v  a 2  s  .  co m
public static synchronized String getHostnameFromNetworkInterface() {
    String result;
    List<String> list;
    Enumeration<NetworkInterface> enmI;
    NetworkInterface intf;
    Enumeration<InetAddress> enmA;
    InetAddress addr;
    boolean found;

    result = null;

    if (m_HostnameNetworkInterface == null) {
        list = new ArrayList<>();
        found = false;
        try {
            enmI = NetworkInterface.getNetworkInterfaces();
            while (enmI.hasMoreElements()) {
                intf = enmI.nextElement();
                // skip non-active ones
                if (!intf.isUp())
                    continue;
                enmA = intf.getInetAddresses();
                while (enmA.hasMoreElements()) {
                    addr = enmA.nextElement();
                    list.add(addr.getHostName());
                    if (addr.getHostName().indexOf(':') == -1) {
                        result = addr.getHostName();
                        found = true;
                        break;
                    }
                }
                if (found)
                    break;
            }
        } catch (Exception e) {
            // ignored
        }

        if (result == null) {
            if (list.size() > 0)
                result = list.get(0);
            else
                result = "<unknown>";
        }

        m_HostnameNetworkInterface = result;
    } else {
        result = m_HostnameNetworkInterface;
    }

    return result;
}

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

protected static String getLocalIpAddress() {
    try {//from w w w.  j a v a 2s. co m
        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:org.apache.sshd.common.util.net.SshdSocketAddress.java

/**
 * @return a {@link List} of local network addresses which are not multicast
 * or localhost sorted according to {@link #BY_HOST_ADDRESS}
 *///w  ww .  ja v a 2 s .  co  m
public static List<InetAddress> getExternalNetwork4Addresses() {
    List<InetAddress> addresses = new ArrayList<>();
    try {
        for (Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); (nets != null)
                && nets.hasMoreElements();) {
            NetworkInterface netint = nets.nextElement();
            /* TODO - uncomment when 1.5 compatibility no longer required
            if (!netint.isUp()) {
            continue;    // ignore non-running interfaces
            }
            */

            for (Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); (inetAddresses != null)
                    && inetAddresses.hasMoreElements();) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (isValidHostAddress(inetAddress)) {
                    addresses.add(inetAddress);
                }
            }
        }
    } catch (SocketException e) {
        // swallow
    }

    if (GenericUtils.size(addresses) > 1) {
        Collections.sort(addresses, BY_HOST_ADDRESS);
    }

    return addresses;
}

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

public static List<InetAddress> getAllIPv6Addrs() {
    if (allIPv6Addrs == null) {
        allIPv6Addrs = new ArrayList<InetAddress>();
        try {/*from w  w  w  . j a  v a2 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 Inet6Address) {
                            allIPv6Addrs.add(ip);
                        }
                    }
                }
            } else {
                log.error("No network interfaces found!");
            }
        } catch (IOException ex) {
            log.error("Failed to get IPv6 addresses: " + ex);
        }
    }
    return allIPv6Addrs;
}

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

public static List<InetAddress> getAllIPv4Addrs() {
    if (allIPv4Addrs == null) {
        allIPv4Addrs = new ArrayList<InetAddress>();
        try {// w w  w  . j av  a 2 s . com
            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;
}