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.hippoecm.repository.FormAuth.java

public static Session login(HttpServletRequest request, SimpleCredentials credentials,
        HippoRepository repository) {//from w  ww. ja  v a 2  s.  co m
    Session hippoSession;
    try {
        if (credentials.getUserID() == null || credentials.getUserID().length() == 0) {
            hippoSession = repository.login();
        } else {
            hippoSession = repository.login(credentials);
        }
        if (((HippoSession) hippoSession).getUser().isSystemUser()) {
            final InetAddress address = InetAddress.getByName(request.getRemoteHost());
            if (!address.isAnyLocalAddress() && !address.isLoopbackAddress()) {
                throw new LoginException();
            }
        }
        return hippoSession;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.talend.commons.utils.network.NetworkUtil.java

public static boolean isSelfAddress(String addr) {
    if (addr == null || addr.isEmpty()) {
        return false; // ?
    }//from  w w  w . j  a  va  2  s . c  o m

    try {
        final InetAddress sourceAddress = InetAddress.getByName(addr);
        if (sourceAddress.isLoopbackAddress()) {
            // final String hostAddress = sourceAddress.getHostAddress();
            // // if addr is localhost, will be 127.0.0.1 also
            // if (hostAddress.equals("127.0.0.1") || hostAddress.equals("localhost") ) {
            return true;
            // }
        } else {
            // check all ip configs
            InetAddress curAddr = null;
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> address = ni.getInetAddresses();
                while (address.hasMoreElements()) {
                    curAddr = address.nextElement();
                    if (addr.equals(curAddr.getHostAddress())) {
                        return true;
                    }
                }
            }
        }

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.seadee.degree.service.NetworkStateReceiver.java

public static String getLocalIpAddress(Context context) {
    final String IPTAG = "getLocalIpAddress";
    String nullAddress = "0.0.0.0";
    try {/*from   w ww  .ja  va  2 s. co  m*/
        if (SettingVarible.networkstate != SettingVarible.NETWORKSTATE.NONETWORK) {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    String ip_address = inetAddress.getHostAddress();
                    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address))
                        return ip_address;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(IPTAG, ex.toString());
    }
    return nullAddress;
}

From source file:org.droidupnp.Main.java

private static InetAddress getLocalIpAdressFromIntf(String intfName) {
    try {//from  w ww. j  a  va 2 s  .c  o m
        NetworkInterface intf = NetworkInterface.getByName(intfName);
        if (intf.isUp()) {
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)
                    return inetAddress;
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "Unable to get ip adress for interface " + intfName);
    }
    return null;
}

From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java

private static String detectLoadBalancerIpAddress() {
    Enumeration<NetworkInterface> networkInterfaces = null;
    try {//from   w  w  w  .  j  a  v a 2  s .c om
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("Unable to obtain network interfaces!", e);
        return null;
    }

    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        boolean isLoopback = false;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException e) {
            logger.debug("Unable to identify if a network interface is a loopback or not");
            continue;
        }

        if (isLoopback) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (!inetAddress.isLoopbackAddress() && Inet4Address.class.isInstance(inetAddress)) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    }

    return null;
}

From source file:com.barchart.udt.AppServer.java

/**
 * Many Linux systems typically return 127.0.0.1 as the localhost address
 * instead of the address assigned on the local network. It has to do with
 * how localhost is defined in /etc/hosts. This method creates a quick
 * UDP socket and gets the local address for the socket on Linux systems
 * to get around the problem. This can also happen on OSX in newer
 * versions of the OS./*from  www.ja  v a2  s. co m*/
 * 
 * @return The local network address in a cross-platform manner.
 * @throws UnknownHostException If the host is considered unknown for 
 * any reason.
 */
private static InetAddress getLocalHost() throws UnknownHostException {
    final InetAddress is = InetAddress.getLocalHost();
    if (!is.isLoopbackAddress()) {
        return is;
    }

    return getLocalHostViaUdp();
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * Get IP address from first non-localhost interface
 * /* www.j  ava 2 s  .  co  m*/
 * @param ipv4 true=return ipv4, false=return ipv6
 * @return address or empty string
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6
                                                            // port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:org.kuali.rice.core.api.util.RiceUtilities.java

/** * @return the current environment's IP address, taking into account the Internet connection to any of the available
*         machine's Network interfaces. Examples of the outputs can be in octatos or in IPV6 format.
*
*         fec0:0:0:9:213:e8ff:fef1:b717%4 siteLocal: true isLoopback: false isIPV6: true
*         ============================================ 130.212.150.216 <<<<<<<<<<<------------- This is the one we
*         want to grab so that we can. siteLocal: false address the DSP on the network. isLoopback: false isIPV6:
*         false ==> lo ============================================ 0:0:0:0:0:0:0:1%1 siteLocal: false isLoopback:
*         true isIPV6: true ============================================ 127.0.0.1 siteLocal: false isLoopback:
*         true isIPV6: false/* www  .j ava  2  s.co  m*/
*/
private static String getCurrentEnvironmentNetworkIp() {
    Enumeration<NetworkInterface> netInterfaces = null;
    try {
        netInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Somehow we have a socket error...", e);
        return "127.0.0.1";
    }

    while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = netInterfaces.nextElement();
        Enumeration<InetAddress> address = ni.getInetAddresses();
        while (address.hasMoreElements()) {
            InetAddress addr = address.nextElement();
            if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress()
                    && !(addr.getHostAddress().indexOf(":") > -1)) {
                return addr.getHostAddress();
            }
        }
    }
    try {
        return InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        return "127.0.0.1";
    }
}

From source file:stargate.commons.utils.IPUtils.java

public static Collection<String> getIPAddress() {
    if (!cached_ip_addr.isEmpty()) {
        return Collections.unmodifiableCollection(cached_ip_addr);
    } else {//from  w w  w. j a  v  a2s  . 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();
                        cached_ip_addr.add(hostAddress);
                    }
                }
            }
        } catch (SocketException ex) {
            LOG.error("Exception occurred while scanning local interfaces", ex);
        }

        return Collections.unmodifiableCollection(cached_ip_addr);
    }
}

From source file:org.apache.ftpserver.test.TestUtil.java

public static InetAddress findNonLocalhostIp() throws Exception {
    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        Enumeration<InetAddress> ips = nif.getInetAddresses();

        while (ips.hasMoreElements()) {
            InetAddress ip = ips.nextElement();
            if (ip instanceof java.net.Inet4Address && !ip.isLoopbackAddress()) {
                return ip;
            } else {
                // IPv6 not tested
            }//from  w  w  w. j  av  a  2 s. co m
        }
    }

    return null;
}