Example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address

List of usage examples for org.apache.http.conn.util InetAddressUtils isIPv4Address

Introduction

In this page you can find the example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address.

Prototype

public static boolean isIPv4Address(final String input) 

Source Link

Document

Checks whether the parameter is a valid IPv4 address

Usage

From source file:ca.psiphon.PsiphonTunnel.java

private static PrivateAddress selectPrivateAddress() throws Exception {
    // Select one of 10.0.0.1, 172.16.0.1, or 192.168.0.1 depending on
    // which private address range isn't in use.

    Map<String, PrivateAddress> candidates = new HashMap<String, PrivateAddress>();
    candidates.put("10", new PrivateAddress("10.0.0.1", "10.0.0.0", 8, "10.0.0.2"));
    candidates.put("172", new PrivateAddress("172.16.0.1", "172.16.0.0", 12, "172.16.0.2"));
    candidates.put("192", new PrivateAddress("192.168.0.1", "192.168.0.0", 16, "192.168.0.2"));
    candidates.put("169", new PrivateAddress("169.254.1.1", "169.254.1.0", 24, "169.254.1.2"));

    List<NetworkInterface> netInterfaces;
    try {//from ww w .ja v  a 2 s. co  m
        netInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
    } catch (SocketException e) {
        throw new Exception("selectPrivateAddress failed", e);
    }

    for (NetworkInterface netInterface : netInterfaces) {
        for (InetAddress inetAddress : Collections.list(netInterface.getInetAddresses())) {
            String ipAddress = inetAddress.getHostAddress();
            if (InetAddressUtils.isIPv4Address(ipAddress)) {
                if (ipAddress.startsWith("10.")) {
                    candidates.remove("10");
                } else if (ipAddress.length() >= 6 && ipAddress.substring(0, 6).compareTo("172.16") >= 0
                        && ipAddress.substring(0, 6).compareTo("172.31") <= 0) {
                    candidates.remove("172");
                } else if (ipAddress.startsWith("192.168")) {
                    candidates.remove("192");
                }
            }
        }
    }

    if (candidates.size() > 0) {
        return candidates.values().iterator().next();
    }

    throw new Exception("no private address available");
}

From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.directory.Directory.java

/**
 * Is the requested destination a dir router?
 * @param sp {@link TCPStreamProperties} containing the destination infos
 * @return true if it is a directory server
 *///w  w w.  java2 s  .c  o m
public boolean isDirServer(final TCPStreamProperties sp) {
    if (sp.getHostname() != null && InetAddressUtils.isIPv4Address(sp.getHostname())) {
        String[] octets = sp.getHostname().split("\\.");
        byte[] ip = new byte[4];
        ip[0] = (byte) Integer.parseInt(octets[0]);
        ip[1] = (byte) Integer.parseInt(octets[1]);
        ip[2] = (byte) Integer.parseInt(octets[2]);
        ip[3] = (byte) Integer.parseInt(octets[3]);

        final Router router = getValidRouterByIpAddressAndDirPort(new TcpipNetAddress(ip, sp.getPort()));
        if (router != null && (router.isDirv2HSDir() || router.isDirv2V2dir())) {
            return true;
        }
    }
    return false;
}

From source file:org.deviceconnect.android.deviceplugin.host.HostDeviceService.java

/**
 * ?IP?.//from   w  ww  .  j  a  v a2s .  c  o m
 * 
 * @return ?IP
 */
public String getLocalIpAddress() {
    try {
        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()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {

                    String ipAddr = inetAddress.getHostAddress();
                    return ipAddr;
                }
            }
        }
    } catch (Exception e) {
        if (BuildConfig.DEBUG) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:net.pocketmine.server.HomeActivity.java

public static String getIPAddress(boolean useIPv4) {
    try {//  www .j av a2s . c om
        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(Locale.US);
                    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) {
    }
    return ha.getResources().getString(R.string.unknown);
}

From source file:org.apache.http.conn.ssl.AbstractVerifier.java

private static boolean isIPAddress(final String hostname) {
    return hostname != null
            && (InetAddressUtils.isIPv4Address(hostname) || InetAddressUtils.isIPv6Address(hostname));
}

From source file:org.apache.http.conn.ssl.DefaultHostnameVerifier.java

public final void verify(final String host, final X509Certificate cert) throws SSLException {
    final boolean ipv4 = InetAddressUtils.isIPv4Address(host);
    final boolean ipv6 = InetAddressUtils.isIPv6Address(host);
    final int subjectType = ipv4 || ipv6 ? IP_ADDRESS_TYPE : DNS_NAME_TYPE;
    final List<String> subjectAlts = extractSubjectAlts(cert, subjectType);
    if (subjectAlts != null && !subjectAlts.isEmpty()) {
        if (ipv4) {
            matchIPAddress(host, subjectAlts);
        } else if (ipv6) {
            matchIPv6Address(host, subjectAlts);
        } else {/*  ww w  .  jav a 2 s.c o m*/
            matchDNSName(host, subjectAlts, this.publicSuffixMatcher);
        }
    } else {
        // CN matching has been deprecated by rfc2818 and can be used
        // as fallback only when no subjectAlts are available
        final X500Principal subjectPrincipal = cert.getSubjectX500Principal();
        final String cn = extractCN(subjectPrincipal.getName(X500Principal.RFC2253));
        if (cn == null) {
            throw new SSLException("Certificate subject for <" + host + "> doesn't contain "
                    + "a common name and does not have alternative names");
        }
        matchCN(host, cn, this.publicSuffixMatcher);
    }
}