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:com.ethlo.geodata.GeodataServiceImpl.java

@Override
public GeoLocation findByIp(String ip) {
    if (!InetAddresses.isInetAddress(ip)) {
        return null;
    }//from  w w  w .  j a  v  a  2s  .  com

    final InetAddress address = InetAddresses.forString(ip);
    final boolean isLocalAddress = address.isLoopbackAddress() || address.isAnyLocalAddress();
    if (isLocalAddress) {
        return null;
    }

    final long ipLong = UnsignedInteger.fromIntBits(InetAddresses.coerceToInteger(InetAddresses.forString(ip)))
            .longValue();

    final Long id = ipRanges.get(ipLong);
    return id != null ? findById(id) : null;
}

From source file:org.graphwalker.Util.java

protected static InetAddress getInternetAddr(final String nic) {
    // Find the real network interface
    NetworkInterface iface = null;
    InetAddress ia = null;
    boolean foundNIC = false;
    try {/*  w  w w . java2  s .  c o m*/
        for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces
                .hasMoreElements() && foundNIC == false;) {
            iface = ifaces.nextElement();
            Util.logger.debug("Interface: " + iface.getDisplayName());
            for (Enumeration<InetAddress> ips = iface.getInetAddresses(); ips.hasMoreElements()
                    && foundNIC == false;) {
                ia = ips.nextElement();
                Util.logger.debug(ia.getCanonicalHostName() + " " + ia.getHostAddress());
                if (!ia.isLoopbackAddress()) {
                    Util.logger.debug("  Not a loopback address...");
                    if (!ia.getHostAddress().contains(":") && nic.equals(iface.getDisplayName())) {
                        Util.logger.debug("  Host address does not contain ':'");
                        Util.logger.debug("  Interface: " + iface.getName()
                                + " seems to be InternetInterface. I'll take it...");
                        foundNIC = true;
                    }
                }
            }
        }
    } catch (SocketException e) {
        Util.logger.error(e.getMessage());
    } finally {
        if (!foundNIC && nic != null) {
            Util.logger.error("Could not bind to network interface: " + nic);
            throw new RuntimeException("Could not bind to network interface: " + nic);
        } else if (!foundNIC) {
            Util.logger.error("Could not bind to any network interface");
            throw new RuntimeException("Could not bind to any network interface: ");
        }
    }
    return ia;
}

From source file:de.sjka.logstash.osgi.internal.LogstashSender.java

@SuppressWarnings("unchecked")
private void addIps(JSONObject values) {
    List<String> ip4s = new ArrayList<>();
    List<String> ip6s = new ArrayList<>();
    String ip = "unknown";
    try {//from   ww w. j  av  a  2s  .  c o  m
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress address = inetAddresses.nextElement();
                if (address instanceof Inet4Address) {
                    ip4s.add(address.getHostAddress() + "_" + address.getHostName());
                    if (!address.isLinkLocalAddress() && !address.isAnyLocalAddress()
                            && !address.isLoopbackAddress()) {
                        ip = address.getHostAddress();
                    }
                }
                if (address instanceof Inet6Address) {
                    ip6s.add(address.getHostAddress() + "_" + address.getHostName());
                }
            }
        }
        ip4s.add("LOC_" + InetAddress.getLocalHost().getHostAddress() + "_"
                + InetAddress.getLocalHost().getHostName());
        if (!ip4s.isEmpty()) {
            values.put("ip", ip);
            values.put("ip4s", ip4s);
        }
        if (!ip6s.isEmpty()) {
            values.put("ip6s", ip6s);
        }
    } catch (UnknownHostException | SocketException e) {
        values.put("ip", "offline_" + e.getMessage());
    }
}

From source file:org.alfresco.repo.security.authentication.ntlm.NTLMAuthenticationProvider.java

/**
 * Use the local server as the authentication server
 * //from   ww  w .  j  a  va  2  s .co  m
 * @param useLocal String
 */
public final void setUseLocalServer(String useLocal) {
    // Check if the local server should be used for authentication

    if (Boolean.parseBoolean(useLocal) == true) {
        // Check if the passthru server list is already configured

        if (m_passthruServers.getTotalServerCount() > 0)
            throw new AlfrescoRuntimeException("Passthru server list already configured");

        try {
            // Get the list of local network addresses

            InetAddress[] localAddrs = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());

            // Build the list of local addresses

            if (localAddrs != null && localAddrs.length > 0) {
                StringBuilder addrStr = new StringBuilder();

                for (InetAddress curAddr : localAddrs) {
                    if (curAddr.isLoopbackAddress() == false) {
                        addrStr.append(curAddr.getHostAddress());
                        addrStr.append(",");
                    }
                }

                if (addrStr.length() > 0)
                    addrStr.setLength(addrStr.length() - 1);

                // Set the server list using the local address list

                m_passthruServers.setServerList(addrStr.toString());
            } else
                throw new AlfrescoRuntimeException("No local server address(es)");
        } catch (UnknownHostException ex) {
            throw new AlfrescoRuntimeException("Failed to get local address list");
        }
    }
}

From source file:com.shreymalhotra.crazyflieserver.MainActivity.java

public String getLocalIpAddress() {
    try {/*from   www. j  a  v  a 2 s  .c  om*/
        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() &&
                // !inetAddress.isLinkLocalAddress() &&
                // inetAddress.isSiteLocalAddress() ) {
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    String ipAddr = inetAddress.getHostAddress();
                    return ipAddr;
                }
            }
        }
    } catch (SocketException ex) {
        Log.d("CrazyFlieServer", ex.toString());
    }
    return null;
}

From source file:com.mirth.connect.connectors.mllp.MllpMessageReceiver.java

protected ServerSocket createServerSocket(URI uri) throws IOException {
    String host = uri.getHost();/* w w w. j a va 2 s  .  c  o  m*/
    int backlog = connector.getBacklog();
    if (host == null || host.length() == 0) {
        host = "localhost";
    }
    InetAddress inetAddress = InetAddress.getByName(host);
    if (inetAddress.equals(InetAddress.getLocalHost()) || inetAddress.isLoopbackAddress()
            || host.trim().equals("localhost")) {
        return new ServerSocket(uri.getPort(), backlog);
    } else {
        return new ServerSocket(uri.getPort(), backlog, inetAddress);
    }
}

From source file:com.momathink.common.tools.ToolMonitor.java

/**
 * ?ip/*  ww  w . jav a  2 s. c o  m*/
 * @return  : "111.111.111.111"
 */
private String getRealIp() {
    String netip = null;// IP
    try {
        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        boolean finded = false;// ?IP
        while (netInterfaces.hasMoreElements() && !finded) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                ip = address.nextElement();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {// IP
                    netip = ip.getHostAddress();
                    finded = true;
                    break;
                }
            }
        }
    } catch (Exception e) {
    }
    return netip;
}

From source file:org.opendaylight.netvirt.openstack.netvirt.impl.BridgeConfigurationManagerImpl.java

private String getLocalControllerHostIpAddress() {
    String ipaddress = null;/*  w w w. j  a v  a 2  s.  c o m*/
    try {
        for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces
                .hasMoreElements();) {
            NetworkInterface iface = ifaces.nextElement();

            for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
                InetAddress inetAddr = inetAddrs.nextElement();
                if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
                    ipaddress = inetAddr.getHostAddress();
                    break;
                }
            }
        }
    } catch (Exception e) {
        LOG.warn("Exception while fetching local host ip address ", e);
    }
    return ipaddress;
}

From source file:uk.org.openseizuredetector.MainActivity.java

/** get the ip address of the phone.
 * Based on http://stackoverflow.com/questions/11015912/how-do-i-get-ip-address-in-ipv4-format
 *//*from  ww w .  j  a va  2 s  . c o  m*/
public String getLocalIpAddress() {
    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();
                //Log.v(TAG,"ip1--:" + inetAddress);
                //Log.v(TAG,"ip2--:" + inetAddress.getHostAddress());

                // for getting IPV4 format
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {

                    String ip = inetAddress.getHostAddress().toString();
                    //Log.v(TAG,"ip---::" + ip);
                    return ip;
                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}