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:org.hippoecm.repository.FormAuth.java

public static Session login(HttpServletRequest request, SimpleCredentials credentials,
        HippoRepository repository) {/*  w  ww. ja  v a2s .  c  o  m*/
    Session hippoSession;
    try {
        if (credentials.getUserID() == null || credentials.getUserID().length() == 0) {
            hippoSession = repository.login();
        } else {
            hippoSession = repository.login(credentials);
        }
        if (((HippoSession) hippoSession).getUser().isSystemUser()) {
            final InetAddress address = InetAddress.getByName(request.getRemoteHost());
            if (!address.isAnyLocalAddress() && !address.isLoopbackAddress()) {
                throw new LoginException();
            }
        }
        return hippoSession;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.yahoo.gondola.container.Utils.java

/**
 * Check address is on the server/*from ww  w  .  j av a  2  s . c o m*/
 */
public static boolean isMyAddress(InetAddress address) {
    // Check if the address is a valid special local or loop back
    if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
        return true;
    }

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

From source file:utils.Config.java

/**
 * @return the default IP address/*from www. ja  v a2  s  .  com*/
 * @throws SocketException
 * @throws UnknownHostException
 */
private static InetAddress getDefaultAddress() throws SocketException, UnknownHostException {
    Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
    while (n.hasMoreElements()) {
        Enumeration<InetAddress> a = n.nextElement().getInetAddresses();
        while (a.hasMoreElements()) {
            InetAddress address = a.nextElement();
            if (!address.isAnyLocalAddress() && (address instanceof Inet4Address)) {
                return address;
            }
        }
    }
    return InetAddress.getLocalHost();
}

From source file:com.offbynull.portmapper.common.NetworkUtils.java

/**
 * Attempts to put together a list of gateway addresses using pre-set values and running OS-specific processes.
 * @return a list of possible addresses for gateway device
 * @throws InterruptedException if interrupted
 *//*from  w w  w. ja  v  a  2  s .c o  m*/
public static Set<InetAddress> getPotentialGatewayAddresses() throws InterruptedException {
    // Ask OS for gateway address
    String netstatOutput = "";
    try {
        netstatOutput = ProcessUtils.runProcessAndDumpOutput(5000L, "netstat", "-rn");
    } catch (IOException ioe) { // NOPMD
        // do nothing
        if (Thread.currentThread().isInterrupted()) {
            throw new InterruptedException();
        }
    }
    LinkedHashSet<String> strAddresses = new LinkedHashSet<>(RegexUtils.findAllIpv4Addresses(netstatOutput));

    // Push in defaults
    strAddresses.addAll(PRESET_IPV4_GATEWAY_ADDRESSES);

    LinkedHashSet<InetAddress> addresses = new LinkedHashSet<>();
    for (String strAddress : strAddresses) {
        try {
            InetAddress addr = InetAddress.getByName(strAddress);
            if (!addr.isAnyLocalAddress()) {
                addresses.add(addr);
            }
        } catch (UnknownHostException uhe) { // NOPMD
            // do nothing
        }
    }

    return addresses;
}

From source file:com.offbynull.portmapper.common.NetworkUtils.java

/**
 * Get IP addresses for all interfaces on this machine that are IPv4.
 * @return IPv4 addresses assigned to this machine
 * @throws IOException if there's an error
 * @throws NullPointerException if any argument is {@code null}
 *//*www  .  j  a  v  a2  s  .  c  om*/
public static Set<InetAddress> getAllLocalIpv4Addresses() throws IOException {
    Set<InetAddress> ret = new HashSet<>();

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
        while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface
            InetAddress addr = addrs.nextElement();

            if (addr instanceof Inet4Address && !addr.isAnyLocalAddress()) {
                ret.add(addr);
            }
        }
    }

    return ret;
}

From source file:com.offbynull.portmapper.common.NetworkUtils.java

/**
 * Get IP addresses for all interfaces on this machine that are IPv6.
 * @return IPv6 addresses assigned to this machine
 * @throws IOException if there's an error
 * @throws NullPointerException if any argument is {@code null}
 *//*  w  ww  .j a v  a  2  s . co  m*/
public static Set<InetAddress> getAllLocalIpv6Addresses() throws IOException {
    Set<InetAddress> ret = new HashSet<>();

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
        while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface
            InetAddress addr = addrs.nextElement();

            if (addr instanceof Inet6Address && !addr.isAnyLocalAddress()) {
                ret.add(addr);
            }
        }
    }

    return ret;
}

From source file:org.apache.hadoop.mapreduce.v2.MiniMRYarnCluster.java

public static String getResolvedMRHistoryWebAppURLWithoutScheme(Configuration conf, boolean isSSLEnabled) {
    InetSocketAddress address = null;
    if (isSSLEnabled) {
        address = conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
                JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
                JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_PORT);
    } else {//ww w  .  j a v  a 2  s  .  com
        address = conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS,
                JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS, JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_PORT);
    }
    address = NetUtils.getConnectAddress(address);
    StringBuffer sb = new StringBuffer();
    InetAddress resolved = address.getAddress();
    if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) {
        String lh = address.getHostName();
        try {
            lh = InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException e) {
            //Ignore and fallback.
        }
        sb.append(lh);
    } else {
        sb.append(address.getHostName());
    }
    sb.append(":").append(address.getPort());
    return sb.toString();
}

From source file:Main.java

public static String filterIP(final InetAddress inetAddress) {
    try {/*ww  w  . j ava  2s .c o  m*/
        final String ipVersion;
        if (inetAddress instanceof Inet4Address)
            ipVersion = "ipv4";
        else if (inetAddress instanceof Inet6Address)
            ipVersion = "ipv6";
        else
            ipVersion = "ipv?";

        if (inetAddress.isAnyLocalAddress())
            return "wildcard";
        if (inetAddress.isSiteLocalAddress())
            return "site_local_" + ipVersion;
        if (inetAddress.isLinkLocalAddress())
            return "link_local_" + ipVersion;
        if (inetAddress.isLoopbackAddress())
            return "loopback_" + ipVersion;
        return inetAddress.getHostAddress();
    } catch (final IllegalArgumentException e) {
        return "illegal_ip";
    }
}

From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java

private static String detectPublicIpAddress() {
    Enumeration<NetworkInterface> networkInterfaces = null;
    try {/*from  w  w w.  j  av a 2 s.co m*/
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("Unable to obtain network interfaces!", e);
        return null;
    }

    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        boolean isLoopback = false;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException e) {
            logger.debug("Unable to identify if a network interface is a loopback or not");
        }

        if (!isLoopback) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (Inet4Address.class.isInstance(inetAddress)) {
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()
                            && !inetAddress.isLinkLocalAddress() && !inetAddress.isSiteLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    }

    return null;
}

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

/**
 * Checking for local ip addresses, e.g.
 * <p/>/*from  w  w  w. j  a v  a  2  s .c o m*/
 * <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();
}