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.geoserver.security.file.LockFile.java

/**
 * Write some info into the lock file /*  ww w. ja  v a2s  .co  m*/
 * hostname, ip, user and lock file path
 * 
 * @param lockFile
 * @throws IOException
 */
protected void writeLockFileContent(File lockFile) throws IOException {

    FileOutputStream out = new FileOutputStream(lockFile);
    Properties props = new Properties();

    try {
        props.store(out, "Locking info");

        String hostname = "UNKNOWN";
        String ip = "UNKNOWN";

        // find some network info
        try {
            hostname = InetAddress.getLocalHost().getHostName();
            InetAddress addrs[] = InetAddress.getAllByName(hostname);
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress())
                    ip = addr.getHostAddress();
            }
        } catch (UnknownHostException ex) {
        }

        props.put("hostname", hostname);
        props.put("ip", ip);
        props.put("location", lockFile.getCanonicalPath());

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        props.put("principal", auth == null ? "UNKNOWN" : auth.getName());

        props.store(out, "Locking info");
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.doxu.g2.gwc.crawler.CrawlThread.java

private boolean isAddressBlocked(InetAddress ip) {
    return ip.isLinkLocalAddress() || ip.isLoopbackAddress() || ip.isSiteLocalAddress()
            || ip.isMulticastAddress();//from w w w  .j ava2  s.c  om
}

From source file:org.smartfrog.test.unit.net.BasicNetworkingTest.java

/**
 * @throws UnknownHostException network problems
 *///from  w  w  w .  j  a  va  2  s  .c  o m
public void test127dot0dot0dot1() throws UnknownHostException {
    InetAddress addr;
    addr = InetAddress.getByName("127.0.0.1");
    logAddr("127.0.0.1", addr);
    assertTrue(addr.isLoopbackAddress());
}

From source file:to.sven.androidrccar.host.communication.impl.SocketConnector.java

/**
 * Returns the first non-local IPv4 address of the device. 
 * @return IPv4 address as String or unknown, if no address is found.
 *//*from  ww w .j av a2s  .  co m*/
private String getLocalIpAddress() {
    try {
        for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            for (InetAddress address : Collections.list(iface.getInetAddresses())) {
                if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(address.getHostAddress())) {
                    return address.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return dc.getContext().getString(android.R.string.unknownName);
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

public String readIp4ApAddress() {
    try {/*from  www  . j a va2s  .co m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            if (intf.getName().contains("wlan")) {
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) {
                        mLog.debug("found AP address [" + inetAddress.getHostAddress() + "]");
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        mLog.debug("failed to read ip address in access point mode");
    }
    return null;
}

From source file:com.meltmedia.cadmium.cli.AbstractAuthorizedOnly.java

/**
 * Converts the passed in siteUrl to be https if not already or not local.
 * //from w  w  w  .  java  2  s . co  m
 * @param siteUrl The url of a cadmium site.
 * @return The passed in siteUrl or the same url but converted to https.
 */
protected String getSecureBaseUrl(String siteUrl) {
    if (!siteUrl.startsWith("http://") && !siteUrl.startsWith("https://")) {
        siteUrl = "http://" + siteUrl;
    }
    Matcher urlMatcher = URL_PATTERN.matcher(siteUrl);
    if (urlMatcher.matches()) {
        logger.debug("Url matches [{}]", siteUrl);
        boolean local = false;
        try {
            logger.debug("Checking if host [{}] is local", urlMatcher.group(1));
            InetAddress hostAddr = InetAddress.getByName(urlMatcher.group(1));
            local = hostAddr.isLoopbackAddress() || hostAddr.isSiteLocalAddress();
            logger.debug("IpAddress [{}] local: {}", hostAddr.getHostAddress(), local);
        } catch (UnknownHostException e) {
            logger.warn("Hostname not found [" + siteUrl + "]", e);
        }
        if (!local) {
            return siteUrl.replaceFirst("http://", "https://");
        }
    }
    return siteUrl;
}

From source file:com.hg.development.apps.messagenotifier_v1.Utils.Utility.java

/**
 *
 * @return adresse IP v4 actuelle du tlphone.
 * @throws SocketException/*from  w ww .ja  v  a2  s  .  c o m*/
 */
private String getLocalIpAddress() throws SocketException {
    String ipv4 = null;
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

        if (interfaces.size() > 0) {
            for (NetworkInterface ni : interfaces) {
                List<InetAddress> ialist = Collections.list(ni.getInetAddresses());

                if (ialist.size() > 0) {
                    for (InetAddress address : ialist) {
                        if (!address.isLoopbackAddress()
                                && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress()))
                            ;
                    }
                }

            }
        }

    } catch (SocketException ex) {
        throw ex;
    } finally {
        return ipv4;
    }
}

From source file:io.cloudslang.content.utilities.services.osdetector.LocalOsDetectorService.java

private boolean isLocalAddress(String host) {
    InetAddress address;
    try {/*  w w  w . ja  va2 s. co  m*/
        address = getByName(host);
    } catch (UnknownHostException ignored) {
        return false;
    }

    if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
        return true;
    }

    try {
        return getByInetAddress(address) != null;
    } catch (SocketException ignored) {
        return false;
    }
}

From source file:org.openhab.io.hueemulation.internal.HueEmulationUpnpServer.java

@Override
public void run() {
    MulticastSocket recvSocket = null;
    // since jupnp shares port 1900, lets use a different port to send UDP packets on just to be safe.
    DatagramSocket sendSocket = null;
    byte[] buf = new byte[1000];
    DatagramPacket recv = new DatagramPacket(buf, buf.length);
    while (running) {
        try {//www.j av a 2s .  co m
            if (discoveryIp != null && discoveryIp.trim().length() > 0) {
                address = InetAddress.getByName(discoveryIp);
            } else {
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                while (interfaces.hasMoreElements()) {
                    NetworkInterface ni = interfaces.nextElement();
                    Enumeration<InetAddress> addresses = ni.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress addr = addresses.nextElement();
                        if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                            address = addr;
                            break;
                        }
                    }
                }
            }
            InetSocketAddress socketAddr = new InetSocketAddress(MULTI_ADDR, UPNP_PORT_RECV);
            recvSocket = new MulticastSocket(UPNP_PORT_RECV);
            recvSocket.joinGroup(socketAddr, NetworkInterface.getByInetAddress(address));
            sendSocket = new DatagramSocket();
            while (running) {
                recvSocket.receive(recv);
                if (recv.getLength() > 0) {
                    String data = new String(recv.getData());
                    logger.trace("Got SSDP Discovery packet from {}:{}", recv.getAddress().getHostAddress(),
                            recv.getPort());
                    if (data.startsWith("M-SEARCH")) {
                        String msg = String.format(discoString, "http://" + address.getHostAddress().toString()
                                + ":" + System.getProperty("org.osgi.service.http.port") + discoPath, usn);
                        DatagramPacket response = new DatagramPacket(msg.getBytes(), msg.length(),
                                recv.getAddress(), recv.getPort());
                        try {
                            logger.trace("Sending to {} : {}", recv.getAddress().getHostAddress(), msg);
                            sendSocket.send(response);
                        } catch (IOException e) {
                            logger.error("Could not send UPNP response", e);
                        }
                    }
                }
            }
        } catch (SocketException e) {
            logger.error("Socket error with UPNP server", e);
        } catch (IOException e) {
            logger.error("IO Error with UPNP server", e);
        } finally {
            IOUtils.closeQuietly(recvSocket);
            IOUtils.closeQuietly(sendSocket);
            if (running) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}

From source file:eu.musesproject.client.contextmonitoring.sensors.DeviceProtectionSensor.java

public String getIPAddress(boolean useIPv4) {
    try {/*from   ww  w .  ja  va  2  s  .  co  m*/
        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('%');
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}