Example usage for java.net InetAddress isLoopbackAddress

List of usage examples for java.net InetAddress isLoopbackAddress

Introduction

In this page you can find the example usage for java.net InetAddress isLoopbackAddress.

Prototype

public boolean isLoopbackAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a loopback address.

Usage

From source file:org.ofbiz.passport.util.PassportUtil.java

public static String getEnvPrefixByHost(HttpServletRequest request) {
    String prefix = "test";
    try {//from w w w.  j  a v a2  s .co m
        InetAddress[] addresses = InetAddress.getAllByName(request.getServerName());
        for (InetAddress address : addresses) {
            if (address.isAnyLocalAddress() || address.isLinkLocalAddress() || address.isLoopbackAddress()) {
                return prefix;
            }
        }
        prefix = "live";
    } catch (UnknownHostException e) {
        Debug.logError(e.getMessage(), module);
    }
    return prefix;
}

From source file:com.yahoo.gondola.container.Utils.java

/**
 * Check address is on the server//  w  w  w .  ja  va  2  s .  com
 */
public static boolean isMyAddress(InetAddress address) {
    // Check if the address is a valid special local or loop back
    if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
        return true;
    }

    // Check if the address is defined on any interface
    try {
        return NetworkInterface.getByInetAddress(address) != null;
    } catch (SocketException e) {
        return false;
    }
}

From source file:com.groupon.odo.proxylib.Utils.java

/**
 * This function returns the first external IP address encountered
 *
 * @return IP address or null/*from   ww  w.jav a  2s . co m*/
 * @throws Exception
 */
public static String getPublicIPAddress() throws Exception {
    final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";

    String ipAddr = null;
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();

            // Pick the first non loop back address
            if ((!i.isLoopbackAddress() && i.isSiteLocalAddress()) || i.getHostAddress().matches(IPV4_REGEX)) {
                ipAddr = i.getHostAddress();
                break;
            }
        }
        if (ipAddr != null) {
            break;
        }
    }

    return ipAddr;
}

From source file:org.eclipse.orion.server.authentication.formpersona.PersonaHelper.java

private static boolean isLoopback(InetAddress addr) {
    try {// ww  w .j a va 2 s  .  c  o m
        if (addr.isLoopbackAddress())
            return true;
        return NetworkInterface.getByInetAddress(addr) != null;
    } catch (SocketException e) {
        return false;
    }
}

From source file:net.grinder.util.NetworkUtil.java

private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6)
        throws SocketException {
    Enumeration<?> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = (NetworkInterface) en.nextElement();
        if (!i.isUp()) {
            continue;
        }//from   w  w  w  . j av  a  2 s  .  c  om
        for (Enumeration<?> en2 = i.getInetAddresses(); en2.hasMoreElements();) {
            InetAddress addr = (InetAddress) en2.nextElement();
            if (!addr.isLoopbackAddress()) {
                if (addr instanceof Inet4Address) {
                    if (preferIPv6) {
                        continue;
                    }
                    return addr;
                }
                if (addr instanceof Inet6Address) {
                    if (preferIpv4) {
                        continue;
                    }
                    return addr;
                }
            }
        }
    }
    return null;
}

From source file:totalcross.android.ConnectionManager4A.java

public static String getLocalIpAddress() {
    String ipv4 = null;/*from w w w  .  j  ava 2  s. c  o  m*/
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                // for getting IPV4 format
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress()))
                    return ipv4;
            }
        }
    } catch (Exception ex) {
        AndroidUtils.debug("IP Address" + ex.toString());
    }
    return null;
}

From source file:eu.eubrazilcc.lvl.core.util.NetworkingUtils.java

/**
 * Gets the first public IP address of the host. If no public address are found, one of the private
 * IPs are randomly selected. Otherwise, it returns {@code localhost}.
 * @return the first public IP address of the host.
 *///from   ww  w  .jav  a2 s. c  o  m
public static final String getInet4Address() {
    String inet4Address = null;
    final List<String> localAddresses = new ArrayList<String>();
    try {
        final Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
        if (networks != null) {
            final List<NetworkInterface> ifs = Collections.list(networks);
            for (int i = 0; i < ifs.size() && inet4Address == null; i++) {
                final Enumeration<InetAddress> inetAddresses = ifs.get(i).getInetAddresses();
                if (inetAddresses != null) {
                    final List<InetAddress> addresses = Collections.list(inetAddresses);
                    for (int j = 0; j < addresses.size() && inet4Address == null; j++) {
                        final InetAddress address = addresses.get(j);
                        if (address instanceof Inet4Address && !address.isAnyLocalAddress()
                                && !address.isLinkLocalAddress() && !address.isLoopbackAddress()
                                && StringUtils.isNotBlank(address.getHostAddress())) {
                            final String hostAddress = address.getHostAddress().trim();
                            if (!hostAddress.startsWith("10.") && !hostAddress.startsWith("172.16.")
                                    && !hostAddress.startsWith("192.168.")) {
                                inet4Address = hostAddress;
                            } else {
                                localAddresses.add(hostAddress);
                            }
                            LOGGER.trace(
                                    "IP found - Name: " + address.getHostName() + ", Addr: " + hostAddress);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Failed to discover public IP address for this host", e);
    }
    return (StringUtils.isNotBlank(inet4Address) ? inet4Address
            : (!localAddresses.isEmpty() ? localAddresses.get(new Random().nextInt(localAddresses.size()))
                    : "localhost")).trim();
}

From source file:Main.java

public static void printAddressDetails(String host) throws Exception {
    System.out.println("Host '" + host + "' details starts...");
    InetAddress addr = InetAddress.getByName(host);
    System.out.println("Host  IP  Address: " + addr.getHostAddress());
    System.out.println("Canonical  Host  Name: " + addr.getCanonicalHostName());

    int timeOutinMillis = 10000;
    System.out.println("isReachable(): " + addr.isReachable(timeOutinMillis));
    System.out.println("isLoopbackAddress(): " + addr.isLoopbackAddress());

}

From source file:org.springframework.cloud.etcd.discovery.EtcdDiscoveryProperties.java

public static InetAddress getIpAddress() {
    try {/*from  w  w  w.ja va 2s. c  om*/
        for (Enumeration<NetworkInterface> enumNic = NetworkInterface.getNetworkInterfaces(); enumNic
                .hasMoreElements();) {
            NetworkInterface ifc = enumNic.nextElement();
            if (ifc.isUp()) {
                for (Enumeration<InetAddress> enumAddr = ifc.getInetAddresses(); enumAddr.hasMoreElements();) {
                    InetAddress address = enumAddr.nextElement();
                    if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
                        return address;
                    }
                }
            }
        }
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        ReflectionUtils.rethrowRuntimeException(e);
        return null;
    } catch (IOException e) {
        log.warn("Unable to find non-loopback address", e);
        return null;
    }
}

From source file:hsyndicate.utils.IPUtils.java

public static Collection<String> getIPAddress() {
    if (!cachedIPAddr.isEmpty()) {
        return Collections.unmodifiableCollection(cachedIPAddr);
    } else {//from   ww w  .j a  v  a 2 s. c om
        try {
            Enumeration e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                NetworkInterface n = (NetworkInterface) e.nextElement();
                Enumeration ee = n.getInetAddresses();
                while (ee.hasMoreElements()) {
                    InetAddress i = (InetAddress) ee.nextElement();
                    if (!i.isLoopbackAddress()) {
                        String hostAddress = i.getHostAddress();
                        cachedIPAddr.add(hostAddress);
                    }
                }
            }
        } catch (SocketException ex) {
            LOG.error("Exception occurred while scanning local interfaces", ex);
        }

        return Collections.unmodifiableCollection(cachedIPAddr);
    }
}