Example usage for java.net InetAddress isMulticastAddress

List of usage examples for java.net InetAddress isMulticastAddress

Introduction

In this page you can find the example usage for java.net InetAddress isMulticastAddress.

Prototype

public boolean isMulticastAddress() 

Source Link

Document

Utility routine to check if the InetAddress is an IP multicast address.

Usage

From source file:org.dd4t.core.util.HttpUtils.java

/**
 * Checking for local ip addresses, e.g.
 * <p/>// ww  w  .j av a 2 s.  c om
 * <pre>
 *     10.x.x.x
 *     172.[16-31].x.x
 *     192.168.x.x
 *     127.0.0.1
 * </pre>
 */
private static boolean isLocalDomainAddress(final String ipAddress) throws UnknownHostException {
    final InetAddress inetAddress = InetAddress.getByName(ipAddress);
    return inetAddress.isAnyLocalAddress() || inetAddress.isLinkLocalAddress()
            || inetAddress.isMulticastAddress() || inetAddress.isSiteLocalAddress();
}

From source file:com.cloud.utils.UriUtils.java

public static Pair<String, Integer> validateUrl(String format, String url) throws IllegalArgumentException {
    try {//from  w  w  w.j a  v a  2s  . co  m
        URI uri = new URI(url);
        if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http")
                && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) {
            throw new IllegalArgumentException("Unsupported scheme for url: " + url);
        }
        int port = uri.getPort();
        if (!(port == 80 || port == 8080 || port == 443 || port == -1)) {
            throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed");
        }

        if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) {
            port = 443;
        } else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) {
            port = 80;
        }

        String host = uri.getHost();
        try {
            InetAddress hostAddr = InetAddress.getByName(host);
            if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress()
                    || hostAddr.isMulticastAddress()) {
                throw new IllegalArgumentException("Illegal host specified in url");
            }
            if (hostAddr instanceof Inet6Address) {
                throw new IllegalArgumentException(
                        "IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")");
            }
        } catch (UnknownHostException uhe) {
            throw new IllegalArgumentException("Unable to resolve " + host);
        }

        // verify format
        if (format != null) {
            String uripath = uri.getPath();
            checkFormat(format, uripath);
        }
        return new Pair<String, Integer>(host, port);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid URL: " + url);
    }
}

From source file:org.apache.sshd.common.util.net.SshdSocketAddress.java

/**
 * @param addr The {@link InetAddress} to be verified
 * @return <P><code>true</code> if the address is:</P></BR>
 * <UL>//from w  w  w .j  ava2  s . com
 *         <LI>Not <code>null</code></LI>
 *         <LI>An {@link Inet4Address}</LI>
 *         <LI>Not link local</LI>
 *         <LI>Not a multicast</LI>
 *         <LI>Not a loopback</LI>
 * </UL>
 * @see InetAddress#isLinkLocalAddress()
 * @see InetAddress#isMulticastAddress()
 * @see InetAddress#isMulticastAddress()
 */
public static boolean isValidHostAddress(InetAddress addr) {
    if (addr == null) {
        return false;
    }

    if (addr.isLinkLocalAddress()) {
        return false;
    }

    if (addr.isMulticastAddress()) {
        return false;
    }

    if (!(addr instanceof Inet4Address)) {
        return false; // TODO add support for IPv6 - see SSHD-746
    }

    return !isLoopback(addr);

}

From source file:org.apache.hadoop.hbase.regionserver.TestRegionServerHostname.java

@Test(timeout = 120000)
public void testRegionServerHostname() throws Exception {
    final int NUM_MASTERS = 1;
    final int NUM_RS = 1;
    Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces();

    while (netInterfaceList.hasMoreElements()) {
        NetworkInterface ni = netInterfaceList.nextElement();
        Enumeration<InetAddress> addrList = ni.getInetAddresses();
        // iterate through host addresses and use each as hostname
        while (addrList.hasMoreElements()) {
            InetAddress addr = addrList.nextElement();
            if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) {
                continue;
            }//from   ww w . j  ava  2s . c o  m
            String hostName = addr.getHostName();
            LOG.info("Found " + hostName + " on " + ni);

            TEST_UTIL.getConfiguration().set(HRegionServer.MASTER_HOSTNAME_KEY, hostName);
            TEST_UTIL.getConfiguration().set(HRegionServer.RS_HOSTNAME_KEY, hostName);
            TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
            try {
                ZooKeeperWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
                List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.rsZNode);
                // there would be NUM_RS+1 children - one for the master
                assertTrue(servers.size() == NUM_RS + 1);
                for (String server : servers) {
                    assertTrue("From zookeeper: " + server + " hostname: " + hostName,
                            server.startsWith(hostName.toLowerCase() + ","));
                }
                zkw.close();
            } finally {
                TEST_UTIL.shutdownMiniCluster();
            }
        }
    }
}

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

private boolean isIneligibleAddress(String ipStr, InetAddress addr) {
    return addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
            || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(ipStr);
}

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

@SuppressWarnings("unchecked")
@Override/*w ww.java2s  .c o  m*/
public JSONObject enrich(CacheKey value) {
    JSONObject enriched = new JSONObject();
    if (!resetConnectionIfNecessary()) {
        _LOG.error(
                "Enrichment failure, cannot maintain a connection to JDBC.  Please check connection.  In the meantime, I'm not enriching.");
        return enriched;
    }
    try {
        InetAddress addr = InetAddress.getByName(value.getValue(String.class));
        if (addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
                || addr.isMulticastAddress()
                || !ipvalidator.isValidInet4Address(value.getValue(String.class))) {
            return new JSONObject();
        }
        String locidQuery = "select IPTOLOCID(\"" + value.getValue() + "\") as ANS";
        ResultSet resultSet = statement.executeQuery(locidQuery);
        String locid = null;
        if (resultSet.next()) {
            locid = resultSet.getString("ANS");
        }
        resultSet.close();
        if (locid == null)
            return new JSONObject();
        String geoQuery = "select * from location where locID = " + locid;
        resultSet = statement.executeQuery(geoQuery);
        if (resultSet.next()) {
            enriched.put("locID", resultSet.getString("locID"));
            enriched.put("country", resultSet.getString("country"));
            enriched.put("city", resultSet.getString("city"));
            enriched.put("postalCode", resultSet.getString("postalCode"));
            enriched.put("latitude", resultSet.getString("latitude"));
            enriched.put("longitude", resultSet.getString("longitude"));
            enriched.put("dmaCode", resultSet.getString("dmaCode"));
            enriched.put("location_point", enriched.get("latitude") + "," + enriched.get("longitude"));
        }
        resultSet.close();
    } catch (Exception e) {
        _LOG.error("Enrichment failure: " + e.getMessage(), e);
        return new JSONObject();
    }
    return enriched;
}

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 source file:com.almende.arum.EventPusher.java

private String getHostAddress() throws SocketException {
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        if (!n.isLoopback() && n.isUp() && !n.isVirtual()) {

            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                if (i instanceof Inet4Address && !i.isLinkLocalAddress() && !i.isMulticastAddress()) {
                    return i.getHostAddress().trim();
                }/*from   w w  w  .j  a  v  a2 s  .  c  om*/
            }
        }
    }
    return null;
}

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

@SuppressWarnings("unchecked")
@Override//from   www  .ja  va  2  s.c  om
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/*w ww  . ja  v a2  s  . c o m*/
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();
    }
}