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:nfinity.FindPeer.java

public String ScanNetwork() {
    try {//from  ww w  . j  a v a 2s .c o  m
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
            for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                if (!inetAddress.getClass().toString().equals("class java.net.Inet6Address")) {
                    if (!inetAddress.isLoopbackAddress()) {
                        try {
                            SubnetUtils utils = new SubnetUtils(inetAddress.getHostAddress() + "/"
                                    + netint.getInterfaceAddresses().get(1).getNetworkPrefixLength());
                            String[] allIps = utils.getInfo().getAllAddresses();
                            return ConnectIP(allIps, inetAddress);
                        } catch (IllegalArgumentException ex) {
                            int prefix = NetworkInterface.getByInetAddress(inetAddress).getInterfaceAddresses()
                                    .get(0).getNetworkPrefixLength();
                            if (!inetAddress.isLoopbackAddress()) {
                                System.out
                                        .println("IP: " + inetAddress.getHostAddress() + " Prefix: " + prefix);
                                SubnetUtils utils = new SubnetUtils(
                                        inetAddress.getHostAddress() + "/" + prefix);
                                String[] allIps = utils.getInfo().getAllAddresses();
                                return ConnectIP(allIps, inetAddress);
                            }
                        }
                    }
                }
            }
        }
    } catch (SocketException ex) {
        System.out.println("Connection failed " + ex.getMessage());
    }
    return "Peer not found";
}

From source file:org.fusesource.mop.commands.AbstractContainerBase.java

protected String getHostName() throws Exception {
    String hostname = "localhost";
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface inet = interfaces.nextElement();
        Enumeration<InetAddress> addresses = inet.getInetAddresses();
        if (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (address.isLoopbackAddress()) {
                InetAddress localAddress = InetAddress.getByName(address.getHostName());
                hostname = localAddress.getHostName();
                break;
            }/*from   www.ja va  2 s .  c  om*/
        }
    }
    return hostname;
}

From source file:Comman.Tool.java

public InetAddress getCurrentIp() {
    try {/*w ww.  ja  v  a 2s  . c o  m*/
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement();
            Enumeration<InetAddress> nias = ni.getInetAddresses();
            while (nias.hasMoreElements()) {
                InetAddress ia = (InetAddress) nias.nextElement();
                if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                    return ia;
                }
            }
        }
    } catch (SocketException e) {
    }
    return null;
}

From source file:com.jagornet.dhcp.client.GenerateTestConfig.java

private boolean parseOptions(String args[]) {
    try {/*from  w  ww .j a  v a 2  s .c o  m*/
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("?")) {
            return false;
        }
        if (cmd.hasOption("f")) {
            filename = cmd.getOptionValue("f");
        } else {
            filename = "dhcpserver-test-config.xml";
        }
        if (cmd.hasOption("i")) {
            networkInterface = NetworkInterface.getByName(cmd.getOptionValue("i"));
        } else {
            networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
        }

        Enumeration<InetAddress> ipAddrs = networkInterface.getInetAddresses();
        while (ipAddrs.hasMoreElements()) {
            InetAddress ipAddr = ipAddrs.nextElement();
            if ((ipAddr instanceof Inet4Address) && !ipAddr.isLinkLocalAddress()
                    && !ipAddr.isLoopbackAddress()) {
                ipv4Address = ipAddr;
                return true;
            }
        }
        System.err.println("No IPv4 address found for interface: " + networkInterface.getName());
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

From source file:com.opensoc.enrichment.adapters.geo.GeoMysqlAdapter.java

@SuppressWarnings("unchecked")
@Override//from w  w w  .  ja va 2s .c o  m
public JSONObject enrich(String metadata) {

    ResultSet resultSet = null;

    try {

        _LOG.trace("[OpenSOC] Received metadata: " + metadata);

        InetAddress addr = InetAddress.getByName(metadata);

        if (addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
                || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(metadata)) {
            _LOG.trace("[OpenSOC] Not a remote IP: " + metadata);
            _LOG.trace("[OpenSOC] Returning enrichment: " + "{}");

            return new JSONObject();
        }

        _LOG.trace("[OpenSOC] Is a valid remote IP: " + metadata);

        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String locid_query = "select IPTOLOCID(\"" + metadata + "\") as ANS";
        resultSet = statement.executeQuery(locid_query);

        if (resultSet == null)
            throw new Exception(
                    "Invalid result set for metadata: " + metadata + ". Query run was: " + locid_query);

        resultSet.last();
        int size = resultSet.getRow();

        if (size == 0)
            throw new Exception("No result returned for: " + metadata + ". Query run was: " + locid_query);

        resultSet.beforeFirst();
        resultSet.next();

        String locid = null;
        locid = resultSet.getString("ANS");

        if (locid == null)
            throw new Exception("Invalid location id for: " + metadata + ". Query run was: " + locid_query);

        String geo_query = "select * from location where locID = " + locid + ";";
        resultSet = statement.executeQuery(geo_query);

        if (resultSet == null)
            throw new Exception("Invalid result set for metadata and locid: " + metadata + ", " + locid
                    + ". Query run was: " + geo_query);

        resultSet.last();
        size = resultSet.getRow();

        if (size == 0)
            throw new Exception("No result id returned for metadata and locid: " + metadata + ", " + locid
                    + ". Query run was: " + geo_query);

        resultSet.beforeFirst();
        resultSet.next();

        JSONObject jo = new JSONObject();
        jo.put("locID", resultSet.getString("locID"));
        jo.put("country", resultSet.getString("country"));
        jo.put("city", resultSet.getString("city"));
        jo.put("postalCode", resultSet.getString("postalCode"));
        jo.put("latitude", resultSet.getString("latitude"));
        jo.put("longitude", resultSet.getString("longitude"));
        jo.put("dmaCode", resultSet.getString("dmaCode"));
        jo.put("locID", resultSet.getString("locID"));

        jo.put("location_point", jo.get("longitude") + "," + jo.get("latitude"));

        _LOG.debug("Returning enrichment: " + jo);

        return jo;

    } catch (Exception e) {
        e.printStackTrace();
        _LOG.error("Enrichment failure: " + e);
        return new JSONObject();
    }
}

From source file:org.apache.metron.enrichment.adapters.geo.GeoMysqlAdapter.java

@SuppressWarnings("unchecked")
@Override/*www . ja v  a2  s . c om*/
public JSONObject enrich(String metadata) {

    ResultSet resultSet = null;

    try {

        _LOG.trace("[Metron] Received metadata: " + metadata);

        InetAddress addr = InetAddress.getByName(metadata);

        if (addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
                || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(metadata)) {
            _LOG.trace("[Metron] Not a remote IP: " + metadata);
            _LOG.trace("[Metron] Returning enrichment: " + "{}");

            return new JSONObject();
        }

        _LOG.trace("[Metron] Is a valid remote IP: " + metadata);

        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String locid_query = "select IPTOLOCID(\"" + metadata + "\") as ANS";
        resultSet = statement.executeQuery(locid_query);

        if (resultSet == null)
            throw new Exception(
                    "Invalid result set for metadata: " + metadata + ". Query run was: " + locid_query);

        resultSet.last();
        int size = resultSet.getRow();

        if (size == 0)
            throw new Exception("No result returned for: " + metadata + ". Query run was: " + locid_query);

        resultSet.beforeFirst();
        resultSet.next();

        String locid = null;
        locid = resultSet.getString("ANS");

        if (locid == null)
            throw new Exception("Invalid location id for: " + metadata + ". Query run was: " + locid_query);

        String geo_query = "select * from location where locID = " + locid + ";";
        resultSet = statement.executeQuery(geo_query);

        if (resultSet == null)
            throw new Exception("Invalid result set for metadata and locid: " + metadata + ", " + locid
                    + ". Query run was: " + geo_query);

        resultSet.last();
        size = resultSet.getRow();

        if (size == 0)
            throw new Exception("No result id returned for metadata and locid: " + metadata + ", " + locid
                    + ". Query run was: " + geo_query);

        resultSet.beforeFirst();
        resultSet.next();

        JSONObject jo = new JSONObject();
        jo.put("locID", resultSet.getString("locID"));
        jo.put("country", resultSet.getString("country"));
        jo.put("city", resultSet.getString("city"));
        jo.put("postalCode", resultSet.getString("postalCode"));
        jo.put("latitude", resultSet.getString("latitude"));
        jo.put("longitude", resultSet.getString("longitude"));
        jo.put("dmaCode", resultSet.getString("dmaCode"));
        jo.put("locID", resultSet.getString("locID"));

        jo.put("location_point", jo.get("longitude") + "," + jo.get("latitude"));

        _LOG.debug("Returning enrichment: " + jo);

        return jo;

    } catch (Exception e) {
        e.printStackTrace();
        _LOG.error("Enrichment failure: " + e);
        return new JSONObject();
    }
}

From source file:com.xperia64.cosi.CosiActivity.java

public String getLocalIpAddress() {
    try {//from   w  w w .  ja  v  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() && inetAddress instanceof Inet4Address) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:nu.nethome.home.items.net.H2DatabaseTCPServer.java

public String getExternalIPAddress() {
    String result = null;//from   w  ww .  j a v a 2 s  .com
    Enumeration<NetworkInterface> interfaces2 = null;
    try {
        interfaces2 = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.severe("Can't get network interfaces: " + e.getMessage());
        return "";
    }
    if (interfaces2 != null) {
        while (interfaces2.hasMoreElements() && StringUtils.isEmpty(result)) {
            NetworkInterface i = interfaces2.nextElement();
            Enumeration<InetAddress> addresses2 = i.getInetAddresses();
            while (addresses2.hasMoreElements() && (result == null || result.isEmpty())) {
                InetAddress address = addresses2.nextElement();
                if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
                    result = address.getHostAddress();
                }
            }
        }
    }
    return result;
}

From source file:org.apache.geode.internal.net.SocketCreator.java

/**
 * returns a set of the non-loopback InetAddresses for this machine
 *//*from w  w w  .  ja v  a 2s  . c om*/
public static Set<InetAddress> getMyAddresses() {
    Set<InetAddress> result = new HashSet<InetAddress>();
    Set<InetAddress> locals = new HashSet<InetAddress>();
    Enumeration<NetworkInterface> interfaces;
    try {
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        throw new IllegalArgumentException(
                LocalizedStrings.StartupMessage_UNABLE_TO_EXAMINE_NETWORK_INTERFACES.toLocalizedString(), e);
    }
    while (interfaces.hasMoreElements()) {
        NetworkInterface face = interfaces.nextElement();
        boolean faceIsUp = false;
        try {
            faceIsUp = face.isUp();
        } catch (SocketException e) {
            InternalDistributedSystem ids = InternalDistributedSystem.getAnyInstance();
            if (ids != null) {
                logger.info("Failed to check if network interface is up. Skipping {}", face, e);
            }
        }
        if (faceIsUp) {
            Enumeration<InetAddress> addrs = face.getInetAddresses();
            while (addrs.hasMoreElements()) {
                InetAddress addr = addrs.nextElement();
                if (addr.isLoopbackAddress() || addr.isAnyLocalAddress()
                        || (!useLinkLocalAddresses && addr.isLinkLocalAddress())) {
                    locals.add(addr);
                } else {
                    result.add(addr);
                }
            } // while
        }
    } // while
      // fix for bug #42427 - allow product to run on a standalone box by using
      // local addresses if there are no non-local addresses available
    if (result.size() == 0) {
        return locals;
    } else {
        return result;
    }
}

From source file:org.peercast.core.PeerCastServiceController.java

private String getIpAddress() {
    Enumeration<NetworkInterface> netIFs;
    try {/*from   w w  w  .j  av  a 2  s .c  o  m*/
        netIFs = NetworkInterface.getNetworkInterfaces();
        while (netIFs.hasMoreElements()) {
            NetworkInterface netIF = netIFs.nextElement();
            Enumeration<InetAddress> ipAddrs = netIF.getInetAddresses();
            while (ipAddrs.hasMoreElements()) {
                InetAddress ip = ipAddrs.nextElement();
                if (!ip.isLoopbackAddress() && !ip.isLinkLocalAddress() && ip.isSiteLocalAddress()) {
                    String ipStr = ip.getHostAddress().toString();
                    Log.d(TAG, "IP: " + ipStr);
                    return ipStr;
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}