sun.net.util.IPAddressUtil Java Examples

The following examples show how to use sun.net.util.IPAddressUtil. 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: InetAddress.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #2
Source File: InetAddress.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #3
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 #4
Source File: InetAddress.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #5
Source File: InetAddress.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #6
Source File: InetAddress.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #7
Source File: InetAddress.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #8
Source File: InetAddress.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static InetAddress getByAddress(String host, byte[] addr, int scopeId)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr, scopeId);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #9
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 #10
Source File: Utilities.java    From Bytecoder with Apache License 2.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 (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
                 SSLLogger.fine(hostname + "\" " +
                    "is not a legal HostName for  server name indication");
            }
        }
    }

    return sniHostName;
}
 
Example #11
Source File: AbstractPlainSocketImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a socket and connects it to the specified address on
 * the specified port.
 * @param address the address
 * @param port the specified port
 */
protected void connect(InetAddress address, int port) throws IOException {
    // recording this.address as supplied by caller before calling connect
    this.address = address;
    this.port = port;
    if (address.isLinkLocalAddress()) {
        address = IPAddressUtil.toScopedAddress(address);
    }

    try {
        connectToAddress(address, port, timeout);
        isConnected = true;
        return;
    } catch (IOException e) {
        // everything failed
        close();
        throw e;
    }
}
 
Example #12
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 #13
Source File: InetAddress.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code www.example.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @throws     UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && !host.isEmpty() && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #14
Source File: InetAddress.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #15
Source File: InetAddress.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #16
Source File: InetAddress.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #17
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 #18
Source File: InetAddress.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #19
Source File: InetAddress.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Creates an InetAddress based on the provided host name and IP address.
 * No name service is checked for the validity of the address.
 *
 * <p> The host name can either be a machine name, such as
 * "{@code java.sun.com}", or a textual representation of its IP
 * address.
 * <p> No validity checking is done on the host name either.
 *
 * <p> If addr specifies an IPv4 address an instance of Inet4Address
 * will be returned; otherwise, an instance of Inet6Address
 * will be returned.
 *
 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
 * must be 16 bytes long
 *
 * @param host the specified host
 * @param addr the raw IP address in network byte order
 * @return  an InetAddress object created from the raw IP address.
 * @exception  UnknownHostException  if IP address is of illegal length
 * @since 1.4
 */
public static InetAddress getByAddress(String host, byte[] addr)
    throws UnknownHostException {
    if (host != null && host.length() > 0 && host.charAt(0) == '[') {
        if (host.charAt(host.length()-1) == ']') {
            host = host.substring(1, host.length() -1);
        }
    }
    if (addr != null) {
        if (addr.length == Inet4Address.INADDRSZ) {
            return new Inet4Address(host, addr);
        } else if (addr.length == Inet6Address.INADDRSZ) {
            byte[] newAddr
                = IPAddressUtil.convertFromIPv4MappedAddress(addr);
            if (newAddr != null) {
                return new Inet4Address(host, newAddr);
            } else {
                return new Inet6Address(host, addr);
            }
        }
    }
    throw new UnknownHostException("addr is of illegal length");
}
 
Example #20
Source File: AbstractPlainSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a socket and connects it to the specified port on
 * the specified host.
 * @param host the specified host
 * @param port the specified port
 */
protected void connect(String host, int port)
    throws UnknownHostException, IOException
{
    boolean connected = false;
    try {
        InetAddress address = InetAddress.getByName(host);
        // recording this.address as supplied by caller before calling connect
        this.address = address;
        this.port = port;
        if (address.isLinkLocalAddress()) {
            address = IPAddressUtil.toScopedAddress(address);
        }

        connectToAddress(address, port, timeout);
        connected = true;
    } finally {
        if (!connected) {
            try {
                close();
            } catch (IOException ioe) {
                /* Do nothing. If connect threw an exception then
                   it will be passed up the call stack */
            }
        }
        isConnected = connected;
    }
}
 
Example #21
Source File: DNSNameService.java    From jdk8u-jdk 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 #22
Source File: Net.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
    throws IOException
{
    if (remote.isLinkLocalAddress()) {
        remote = IPAddressUtil.toScopedAddress(remote);
    }
    boolean preferIPv6 = isIPv6Available() &&
        (family != StandardProtocolFamily.INET);
    return connect0(preferIPv6, fd, remote, remotePort);
}
 
Example #23
Source File: AbstractPlainSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Binds the socket to the specified address of the specified local port.
 * @param address the address
 * @param lport the port
 */
protected synchronized void bind(InetAddress address, int lport)
    throws IOException
{
   synchronized (fdLock) {
        if (!closePending && !isBound) {
            NetHooks.beforeTcpBind(fd, address, lport);
        }
    }
    if (address.isLinkLocalAddress()) {
        address = IPAddressUtil.toScopedAddress(address);
    }
    socketBind(address, lport);
    isBound = true;
}
 
Example #24
Source File: ResolvingEnsembleProviderTest.java    From curator-extensions with Apache License 2.0 5 votes vote down vote up
private ResolverOngoingStubbing thenResolveTo(String... addresses) throws Exception {
    InetAddress[] result = new InetAddress[addresses.length];

    for (int i = 0; i < addresses.length; ++i) {
        result[i] = InetAddress.getByAddress(IPAddressUtil.textToNumericFormatV4(addresses[i]));
    }

    _stub =_stub.thenReturn(result);

    return this;
}
 
Example #25
Source File: HttpURLConnection.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static URL checkURL(URL u) throws IOException {
    if (u != null) {
        if (u.toExternalForm().indexOf('\n') > -1) {
            throw new MalformedURLException("Illegal character in URL");
        }
    }
    String s = IPAddressUtil.checkAuthority(u);
    if (s != null) {
        throw new MalformedURLException(s);
    }
    return u;
}
 
Example #26
Source File: DNSNameService.java    From jdk8u-dev-jdk 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 #27
Source File: HttpsURLConnectionImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static URL checkURL(URL u) throws IOException {
    if (u != null) {
        if (u.toExternalForm().indexOf('\n') > -1) {
            throw new MalformedURLException("Illegal character in URL");
        }
    }
    String s = IPAddressUtil.checkAuthority(u);
    if (s != null) {
        throw new MalformedURLException(s);
    }
    return u;
}
 
Example #28
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 #29
Source File: SocksCmdRequestDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testCmdRequestDecoderIPv6() {
    String[] hosts = {SocksCommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))};
    int[] ports = {1, 32769, 65535};
    for (SocksCmdType cmdType : SocksCmdType.values()) {
        for (String host : hosts) {
            for (int port : ports) {
                testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.IPv6, host, port);
            }
        }
    }
}
 
Example #30
Source File: InetAddress.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private byte [] createAddressByteArray(String addrStr) {
    byte[] addrArray;
    // check if IPV4 address - most likely
    addrArray = IPAddressUtil.textToNumericFormatV4(addrStr);
    if (addrArray == null) {
        addrArray = IPAddressUtil.textToNumericFormatV6(addrStr);
    }
    return addrArray;
}