Example usage for java.net InetAddress isAnyLocalAddress

List of usage examples for java.net InetAddress isAnyLocalAddress

Introduction

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

Prototype

public boolean isAnyLocalAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a wildcard address.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress address = InetAddress.getByName("web.mit.edu");
    System.out.println("Name: " + address.getHostName());
    System.out.println("Addr: " + address.getHostAddress());
    System.out.println(address.isAnyLocalAddress());
}

From source file:Main.java

/**
 * checks if the provided address is a global-scope ipv6 unicast address
 *///from   w  ww . j  a va 2  s.  c o  m
public static boolean isGlobalAddressV6(InetAddress addr) {
    return addr instanceof Inet6Address && !addr.isAnyLocalAddress() && !addr.isLinkLocalAddress()
            && !addr.isLoopbackAddress() && !addr.isMulticastAddress() && !addr.isSiteLocalAddress()
            && !((Inet6Address) addr).isIPv4CompatibleAddress();
}

From source file:Main.java

private static int scoreAddress(InetAddress addr) {
    if (addr.isAnyLocalAddress()) {
        return 0;
    }/*from ww  w. j a  v  a2s.c om*/
    if (addr.isMulticastAddress()) {
        return 1;
    }
    if (addr.isLinkLocalAddress()) {
        return 2;
    }
    if (addr.isSiteLocalAddress()) {
        return 3;
    }

    return 4;
}

From source file:org.apache.metron.enrichment.adapters.maxmind.MaxMindDbUtilities.java

/**
 * Determines if an address isn't eligible for getting appropriate results from the underlying database.
 * @param ipStr The IP String/*w ww  . j  a v  a 2 s . com*/
 * @param addr The addr to be tested
 * @return True if ineligible, false otherwise
 */
public static boolean isIneligibleAddress(String ipStr, InetAddress addr) {
    return addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
            || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(ipStr);
}

From source file:org.apache.hadoop.util.NodeUtils.java

public static String addressToString(InetSocketAddress addr) {
    InetAddress address = addr.getAddress();
    if (address.isAnyLocalAddress()) {
        //it's local, so patch it to whatever we think we are
        return cachedHostAddress;
    } else {//from   ww  w .ja v a 2 s.com
        return addr.getAddress().getHostAddress();
    }
}

From source file:de.forsthaus.backend.service.impl.IpToCountryServiceImpl.java

/**
 * Converts an ip-address to a long value.<br>
 * /*from   w w  w  .j a  v a  2s  . co  m*/
 * @param address
 * @return
 */
private static long inetAddressToLong(InetAddress address) {
    if (address.isAnyLocalAddress())
        return 0l;

    final byte[] bs;
    if (address instanceof Inet4Address) {
        bs = address.getAddress();
    } else if (address instanceof Inet6Address) {
        if (((Inet6Address) address).isIPv4CompatibleAddress()) {
            // take the last 4 digits
            bs = ArrayUtils.subarray(address.getAddress(), 12, 16);
        } else {
            throw new RuntimeException("IPv6 not supported!");
        }
    } else {
        throw new RuntimeException();
    }

    return bs[0] * 16777216l + bs[1] * 65536 + bs[2] * 256 + bs[3];
}

From source file:org.springframework.cloud.dataflow.yarn.streamappmaster.EmbeddedAppmasterTrackService.java

private static String getDefaultAddress() {
    Enumeration<NetworkInterface> nets;
    try {/*from   www.  ja v a2s  .  c  o  m*/
        nets = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        return null;
    }
    NetworkInterface netinf;
    while (nets.hasMoreElements()) {
        netinf = nets.nextElement();
        boolean skip = false;
        try {
            skip = netinf.isPointToPoint();
        } catch (SocketException e) {
            skip = true;
        }
        if (skip) {
            continue;
        }
        Enumeration<InetAddress> addresses = netinf.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!address.isAnyLocalAddress() && !address.isMulticastAddress()
                    && !(address instanceof Inet6Address)) {
                return address.getHostAddress();
            }
        }
    }
    return null;
}

From source file:eu.eubrazilcc.lvl.core.util.NetworkingUtils.java

/**
 * Gets the first public IP address of the host. If no public address are found, one of the private
 * IPs are randomly selected. Otherwise, it returns {@code localhost}.
 * @return the first public IP address of the host.
 *///  ww  w  . j  av a  2s  .c o m
public static final String getInet4Address() {
    String inet4Address = null;
    final List<String> localAddresses = new ArrayList<String>();
    try {
        final Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
        if (networks != null) {
            final List<NetworkInterface> ifs = Collections.list(networks);
            for (int i = 0; i < ifs.size() && inet4Address == null; i++) {
                final Enumeration<InetAddress> inetAddresses = ifs.get(i).getInetAddresses();
                if (inetAddresses != null) {
                    final List<InetAddress> addresses = Collections.list(inetAddresses);
                    for (int j = 0; j < addresses.size() && inet4Address == null; j++) {
                        final InetAddress address = addresses.get(j);
                        if (address instanceof Inet4Address && !address.isAnyLocalAddress()
                                && !address.isLinkLocalAddress() && !address.isLoopbackAddress()
                                && StringUtils.isNotBlank(address.getHostAddress())) {
                            final String hostAddress = address.getHostAddress().trim();
                            if (!hostAddress.startsWith("10.") && !hostAddress.startsWith("172.16.")
                                    && !hostAddress.startsWith("192.168.")) {
                                inet4Address = hostAddress;
                            } else {
                                localAddresses.add(hostAddress);
                            }
                            LOGGER.trace(
                                    "IP found - Name: " + address.getHostName() + ", Addr: " + hostAddress);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Failed to discover public IP address for this host", e);
    }
    return (StringUtils.isNotBlank(inet4Address) ? inet4Address
            : (!localAddresses.isEmpty() ? localAddresses.get(new Random().nextInt(localAddresses.size()))
                    : "localhost")).trim();
}

From source file:org.ofbiz.passport.util.PassportUtil.java

public static String getEnvPrefixByHost(HttpServletRequest request) {
    String prefix = "test";
    try {/*  w w  w .j  a  v  a 2 s.  c  om*/
        InetAddress[] addresses = InetAddress.getAllByName(request.getServerName());
        for (InetAddress address : addresses) {
            if (address.isAnyLocalAddress() || address.isLinkLocalAddress() || address.isLoopbackAddress()) {
                return prefix;
            }
        }
        prefix = "live";
    } catch (UnknownHostException e) {
        Debug.logError(e.getMessage(), module);
    }
    return prefix;
}

From source file:org.encuestame.core.util.InternetUtils.java

/**
 * //from  w ww . j  ava 2  s .  c  om
 * @param addr
 * @return
 */
public static boolean isThisMyIpAddress(InetAddress addr) {
    // Check if the address is a valid special local or loop back
    if (addr.isAnyLocalAddress() || addr.isLoopbackAddress())
        return true;

    // Check if the address is defined on any interface
    try {
        return NetworkInterface.getByInetAddress(addr) != null;
    } catch (SocketException e) {
        return false;
    }
}