Java Code Examples for sun.net.util.IPAddressUtil#isIPv4LiteralAddress()

The following examples show how to use sun.net.util.IPAddressUtil#isIPv4LiteralAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utilities.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example 2
Source File: Utilities.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example 3
Source File: Utilities.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example 4
Source File: Utilities.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example 5
Source File: Utilities.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example 6
Source File: Utilities.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example 7
Source File: Utilities.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts string hostname to {@code SNIHostName}.
 * <P>
 * Note that to check whether a hostname is a valid domain name, we cannot
 * use the hostname resolved from name services.  For virtual hosting,
 * multiple hostnames may be bound to the same IP address, so the hostname
 * resolved from name services is not always reliable.
 *
 * @param  hostname
 *         the raw hostname
 * @return an instance of {@link SNIHostName}, or null if the hostname does
 *         not look like a FQDN
 */
private static SNIHostName rawToSNIHostName(String hostname) {
    SNIHostName sniHostName = null;
    if (hostname != null && hostname.indexOf('.') > 0 &&
            !hostname.endsWith(".") &&
            !IPAddressUtil.isIPv4LiteralAddress(hostname) &&
            !IPAddressUtil.isIPv6LiteralAddress(hostname)) {

        try {
            sniHostName = new SNIHostName(hostname);
        } catch (IllegalArgumentException iae) {
            // don't bother to handle illegal host_name
            if (Debug.isOn("ssl")) {
                System.out.println(Thread.currentThread().getName() +
                    ", \"" + hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example 8
Source File: DNSNameService.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void appendIfLiteralAddress(String addr, StringBuffer sb) {
    if (IPAddressUtil.isIPv4LiteralAddress(addr)) {
        sb.append("dns://" + addr + " ");
    } else {
        if (IPAddressUtil.isIPv6LiteralAddress(addr)) {
            sb.append("dns://[" + addr + "] ");
        }
    }
}
 
Example 9
Source File: NetUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an address in a normalized format for Akka.
 * When an IPv6 address is specified, it normalizes the IPv6 address to avoid
 * complications with the exact URL match policy of Akka.
 * @param host The hostname, IPv4 or IPv6 address
 * @return host which will be normalized if it is an IPv6 address
 */
public static String unresolvedHostToNormalizedString(String host) {
	// Return loopback interface address if host is null
	// This represents the behavior of {@code InetAddress.getByName } and RFC 3330
	if (host == null) {
		host = InetAddress.getLoopbackAddress().getHostAddress();
	} else {
		host = host.trim().toLowerCase();
	}

	// normalize and valid address
	if (IPAddressUtil.isIPv6LiteralAddress(host)) {
		byte[] ipV6Address = IPAddressUtil.textToNumericFormatV6(host);
		host = getIPv6UrlRepresentation(ipV6Address);
	} else if (!IPAddressUtil.isIPv4LiteralAddress(host)) {
		try {
			// We don't allow these in hostnames
			Preconditions.checkArgument(!host.startsWith("."));
			Preconditions.checkArgument(!host.endsWith("."));
			Preconditions.checkArgument(!host.contains(":"));
		} catch (Exception e) {
			throw new IllegalConfigurationException("The configured hostname is not valid", e);
		}
	}

	return host;
}
 
Example 10
Source File: DNSNameService.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void appendIfLiteralAddress(String addr, StringBuffer sb) {
    if (IPAddressUtil.isIPv4LiteralAddress(addr)) {
        sb.append("dns://" + addr + " ");
    } else {
        if (IPAddressUtil.isIPv6LiteralAddress(addr)) {
            sb.append("dns://[" + addr + "] ");
        }
    }
}
 
Example 11
Source File: TaskManagerRunnerConfigurationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static TypeSafeMatcher<String> ipAddress() {
	return new TypeSafeMatcher<String>() {
		@Override
		protected boolean matchesSafely(String value) {
			return IPAddressUtil.isIPv4LiteralAddress(value) || IPAddressUtil.isIPv6LiteralAddress(value);
		}

		@Override
		public void describeTo(Description description) {
			description.appendText("Is an ip address.");
		}
	};
}
 
Example 12
Source File: DNSNameService.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void appendIfLiteralAddress(String addr, StringBuffer sb) {
    if (IPAddressUtil.isIPv4LiteralAddress(addr)) {
        sb.append("dns://" + addr + " ");
    } else {
        if (IPAddressUtil.isIPv6LiteralAddress(addr)) {
            sb.append("dns://[" + addr + "] ");
        }
    }
}
 
Example 13
Source File: DNSNameService.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void appendIfLiteralAddress(String addr, StringBuffer sb) {
    if (IPAddressUtil.isIPv4LiteralAddress(addr)) {
        sb.append("dns://" + addr + " ");
    } else {
        if (IPAddressUtil.isIPv6LiteralAddress(addr)) {
            sb.append("dns://[" + addr + "] ");
        }
    }
}
 
Example 14
Source File: DNSNameService.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public DNSNameService() throws Exception {

        // default domain
        String domain = AccessController.doPrivileged(
            new GetPropertyAction("sun.net.spi.nameservice.domain"));
        if (domain != null && domain.length() > 0) {
            domainList = new LinkedList<String>();
            domainList.add(domain);
        }

        // name servers
        String nameservers = AccessController.doPrivileged(
            new GetPropertyAction("sun.net.spi.nameservice.nameservers"));
        if (nameservers != null && nameservers.length() > 0) {
            nameProviderUrl = createProviderURL(nameservers);
            if (nameProviderUrl.length() == 0) {
                throw new RuntimeException("malformed nameservers property");
            }

        } else {

            // no property specified so check host DNS resolver configured
            // with at least one nameserver in dotted notation.
            //
            List<String> nsList = ResolverConfiguration.open().nameservers();
            if (nsList.isEmpty()) {
                throw new RuntimeException("no nameservers provided");
            }
            boolean found = false;
            for (String addr: nsList) {
                if (IPAddressUtil.isIPv4LiteralAddress(addr) ||
                    IPAddressUtil.isIPv6LiteralAddress(addr)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new RuntimeException("bad nameserver configuration");
            }
        }
    }
 
Example 15
Source File: DNSNameService.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public DNSNameService() throws Exception {

        // default domain
        String domain = AccessController.doPrivileged(
            new GetPropertyAction("sun.net.spi.nameservice.domain"));
        if (domain != null && domain.length() > 0) {
            domainList = new LinkedList<String>();
            domainList.add(domain);
        }

        // name servers
        String nameservers = AccessController.doPrivileged(
            new GetPropertyAction("sun.net.spi.nameservice.nameservers"));
        if (nameservers != null && nameservers.length() > 0) {
            nameProviderUrl = createProviderURL(nameservers);
            if (nameProviderUrl.length() == 0) {
                throw new RuntimeException("malformed nameservers property");
            }

        } else {

            // no property specified so check host DNS resolver configured
            // with at least one nameserver in dotted notation.
            //
            List<String> nsList = ResolverConfiguration.open().nameservers();
            if (nsList.isEmpty()) {
                throw new RuntimeException("no nameservers provided");
            }
            boolean found = false;
            for (String addr: nsList) {
                if (IPAddressUtil.isIPv4LiteralAddress(addr) ||
                    IPAddressUtil.isIPv6LiteralAddress(addr)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new RuntimeException("bad nameserver configuration");
            }
        }
    }
 
Example 16
Source File: HostnameChecker.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Test whether the given hostname looks like a literal IPv4 or IPv6
 * address. The hostname does not need to be a fully qualified name.
 *
 * This is not a strict check that performs full input validation.
 * That means if the method returns true, name need not be a correct
 * IP address, rather that it does not represent a valid DNS hostname.
 * Likewise for IP addresses when it returns false.
 */
private static boolean isIpAddress(String name) {
    if (IPAddressUtil.isIPv4LiteralAddress(name) ||
        IPAddressUtil.isIPv6LiteralAddress(name)) {
        return true;
    } else {
        return false;
    }
}
 
Example 17
Source File: HostnameChecker.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Test whether the given hostname looks like a literal IPv4 or IPv6
 * address. The hostname does not need to be a fully qualified name.
 *
 * This is not a strict check that performs full input validation.
 * That means if the method returns true, name need not be a correct
 * IP address, rather that it does not represent a valid DNS hostname.
 * Likewise for IP addresses when it returns false.
 */
private static boolean isIpAddress(String name) {
    if (IPAddressUtil.isIPv4LiteralAddress(name) ||
        IPAddressUtil.isIPv6LiteralAddress(name)) {
        return true;
    } else {
        return false;
    }
}
 
Example 18
Source File: HostnameChecker.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Test whether the given hostname looks like a literal IPv4 or IPv6
 * address. The hostname does not need to be a fully qualified name.
 *
 * This is not a strict check that performs full input validation.
 * That means if the method returns true, name need not be a correct
 * IP address, rather that it does not represent a valid DNS hostname.
 * Likewise for IP addresses when it returns false.
 */
private static boolean isIpAddress(String name) {
    if (IPAddressUtil.isIPv4LiteralAddress(name) ||
        IPAddressUtil.isIPv6LiteralAddress(name)) {
        return true;
    } else {
        return false;
    }
}
 
Example 19
Source File: HostnameChecker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Test whether the given hostname looks like a literal IPv4 or IPv6
 * address. The hostname does not need to be a fully qualified name.
 *
 * This is not a strict check that performs full input validation.
 * That means if the method returns true, name need not be a correct
 * IP address, rather that it does not represent a valid DNS hostname.
 * Likewise for IP addresses when it returns false.
 */
private static boolean isIpAddress(String name) {
    if (IPAddressUtil.isIPv4LiteralAddress(name) ||
        IPAddressUtil.isIPv6LiteralAddress(name)) {
        return true;
    } else {
        return false;
    }
}
 
Example 20
Source File: HostnameChecker.java    From openjsse with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Test whether the given hostname looks like a literal IPv4 or IPv6
 * address. The hostname does not need to be a fully qualified name.
 *
 * This is not a strict check that performs full input validation.
 * That means if the method returns true, name need not be a correct
 * IP address, rather that it does not represent a valid DNS hostname.
 * Likewise for IP addresses when it returns false.
 */
private static boolean isIpAddress(String name) {
    if (IPAddressUtil.isIPv4LiteralAddress(name) ||
        IPAddressUtil.isIPv6LiteralAddress(name)) {
        return true;
    } else {
        return false;
    }
}