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.springframework.cloud.commons.util.InetUtils.java

public InetAddress findFirstNonLoopbackAddress() {
    InetAddress result = null;/*from  w w  w.j  av  a  2s. c o  m*/
    try {
        int lowest = Integer.MAX_VALUE;
        for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics
                .hasMoreElements();) {
            NetworkInterface ifc = nics.nextElement();
            if (ifc.isUp()) {
                log.trace("Testing interface: " + ifc.getDisplayName());
                if (ifc.getIndex() < lowest || result == null) {
                    lowest = ifc.getIndex();
                } else if (result != null) {
                    continue;
                }

                // @formatter:off
                if (!ignoreInterface(ifc.getDisplayName())) {
                    for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
                        InetAddress address = addrs.nextElement();
                        if (address instanceof Inet4Address && !address.isLoopbackAddress()
                                && !ignoreAddress(address)) {
                            log.trace("Found non-loopback interface: " + ifc.getDisplayName());
                            result = address;
                        }
                    }
                }
                // @formatter:on
            }
        }
    } catch (IOException ex) {
        log.error("Cannot get first non-loopback address", ex);
    }

    if (result != null) {
        return result;
    }

    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        log.warn("Unable to retrieve localhost");
    }

    return null;
}

From source file:org.wso2.bam.integration.tests.archive.CassandraDataArchiveTestCase.java

private InetAddress getLocalHostAddress() throws SocketException {
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress addr = addresses.nextElement();
            if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                return addr;
            }//from   w  w  w.ja  v a  2s .  co m
        }
    }

    return null;
}

From source file:com.bluetooth.activities.WiFiControl.java

/**
 * This method gets an IPv4 address for the user to enter in the browser.
 * //from   w w w .  j a  v  a 2 s .c o  m
 * @return The IP
 */
private 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();
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.openhab.io.net.http.SecureHttpContext.java

protected boolean isExternalIp(String remoteAddr) throws UnknownHostException {
    InetAddress remoteIp = InetAddress.getByName(remoteAddr);
    if (remoteIp.isLoopbackAddress()) {
        // by definition: the loopback address is NOT external!
        return false;
    }/*w w w .  j  av  a  2s  .c  o  m*/

    boolean isExternal = true;
    for (IpAddressMatcher ipAddressMatcher : ipAddressMatchers) {
        if (ipAddressMatcher.matches(remoteAddr)) {
            isExternal = false;
            break;
        }
    }
    logger.trace("http request is originated by '{}' which is identified as '{}'", remoteAddr,
            isExternal ? "external" : "internal");
    return isExternal;
}

From source file:com.groupon.odo.bmp.ProxyServer.java

public void setLocalHost(InetAddress localHost) throws SocketException {
    if (localHost.isAnyLocalAddress() || localHost.isLoopbackAddress()
            || NetworkInterface.getByInetAddress(localHost) != null) {
        this.localHost = localHost;
    } else {/*w w w.j  av  a2  s.  co m*/
        throw new IllegalArgumentException("Must be address of a local adapter");
    }
}

From source file:com.singularityeye.eyetrack.MapsActivity.java

private String getHostAddress() {
    try {/*from w  ww  . j a  v a  2  s. co  m*/
        // iterate over interfaces
        for (Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
                .getNetworkInterfaces(); networkInterfaces.hasMoreElements();) {
            NetworkInterface nextInterface = networkInterfaces.nextElement();
            // iterate over ip addresses (note: one interface could have more than one ip address)
            for (Enumeration<InetAddress> ipAddresses = nextInterface.getInetAddresses(); ipAddresses
                    .hasMoreElements();) {
                InetAddress nextIpAddress = ipAddresses.nextElement();
                // get an IPv4 address
                if (!nextIpAddress.isLoopbackAddress() && nextIpAddress instanceof Inet4Address) {
                    return nextIpAddress.getHostAddress(); // get first ip address
                }
            }
        }
    } catch (SocketException e) {
        Log.e("ERROR", "GetHostAddressException: " + e.getMessage());
    }
    return null;
}

From source file:org.mobisocial.corral.ContentCorral.java

public static String getLocalIpAddress() {
    try {/*  w ww .  j av  a  2 s.c o  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()) {
                    // not ready for IPv6, apparently.
                    if (!inetAddress.getHostAddress().contains(":")) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
    } catch (SocketException ex) {

    }
    return null;
}

From source file:org.apache.jmeter.JMeter.java

private static void waitForSignals(final List<JMeterEngine> engines, DatagramSocket socket) {
    byte[] buf = new byte[80];
    System.out.println(/*www.j a v  a 2  s  .  c om*/
            "Waiting for possible Shutdown/StopTestNow/Heapdump message on port " + socket.getLocalPort());
    DatagramPacket request = new DatagramPacket(buf, buf.length);
    try {
        while (true) {
            socket.receive(request);
            InetAddress address = request.getAddress();
            // Only accept commands from the local host
            if (address.isLoopbackAddress()) {
                String command = new String(request.getData(), request.getOffset(), request.getLength(),
                        "ASCII");
                System.out.println("Command: " + command + " received from " + address);
                log.info("Command: " + command + " received from " + address);
                if (command.equals("StopTestNow")) {
                    for (JMeterEngine engine : engines) {
                        engine.stopTest(true);
                    }
                } else if (command.equals("Shutdown")) {
                    for (JMeterEngine engine : engines) {
                        engine.stopTest(false);
                    }
                } else if (command.equals("HeapDump")) {
                    HeapDumper.dumpHeap();
                } else {
                    System.out.println("Command: " + command + " not recognised ");
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        socket.close();
    }
}

From source file:net.sf.janos.control.SonosController.java

/**
 * Broadcasts a search packet on all network interfaces. This method should
 * really be a part of the Discovery class, but that's not my code...
 * /*  w w w .  j  a v a  2s  .co m*/
 * @param searchTarget
 * @throws IOException 
 */
public void sendSearchPacket(String searchTarget) throws IOException {
    for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
        NetworkInterface intf = (NetworkInterface) e.nextElement();
        for (Enumeration<InetAddress> adrs = intf.getInetAddresses(); adrs.hasMoreElements();) {
            InetAddress adr = (InetAddress) adrs.nextElement();
            if (adr instanceof Inet4Address && !adr.isLoopbackAddress()) {
                Discovery.sendSearchMessage(adr, Discovery.DEFAULT_TTL, Discovery.DEFAULT_MX, searchTarget);
            }
        }
    }

}

From source file:com.orangelabs.rcs.platform.network.AndroidNetworkFactory.java

/**
 * Returns the local IP address of a given network interface
 * /*ww w  .j a  va2  s .c  o  m*/
 * @param dnsEntry remote address to find an according local socket address
  * @param type the type of the network interface, should be either
  *        {@link android.net.ConnectivityManager#TYPE_WIFI} or {@link android.net.ConnectivityManager#TYPE_MOBILE}
 * @return Address
 */
// Changed by Deutsche Telekom
public String getLocalIpAddress(DnsResolvedFields dnsEntry, int type) {
    String ipAddress = null;
    try {
        // What kind of remote address (P-CSCF) are we trying to reach?
        boolean isIpv4 = InetAddressUtils.isIPv4Address(dnsEntry.ipAddress);

        // check all available interfaces
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); (en != null)
                && en.hasMoreElements();) {
            NetworkInterface netIntf = (NetworkInterface) en.nextElement();
            for (Enumeration<InetAddress> addr = netIntf.getInetAddresses(); addr.hasMoreElements();) {
                InetAddress inetAddress = addr.nextElement();
                ipAddress = IpAddressUtils.extractHostAddress(inetAddress.getHostAddress());
                // if IP address version doesn't match to remote address
                // version then skip
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
                        && (InetAddressUtils.isIPv4Address(ipAddress) == isIpv4)) {
                    String intfName = netIntf.getDisplayName().toLowerCase();
                    // some devices do list several interfaces though only
                    // one is active
                    if (((type == ConnectivityManager.TYPE_WIFI) && intfName.startsWith("wlan"))
                            || ((type == ConnectivityManager.TYPE_MOBILE) && !intfName.startsWith("wlan"))) {
                        return ipAddress;
                    }
                }
            }
        }
    } catch (Exception e) {
        if (logger.isActivated()) {
            logger.error("getLocalIpAddress failed with ", e);
        }
    }
    return ipAddress;
}