Search in sources :

Example 1 with StructAddrinfo

use of libcore.io.StructAddrinfo in project robovm by robovm.

the class InetAddress method parseNumericAddressNoThrow.

private static InetAddress parseNumericAddressNoThrow(String address) {
    // Accept IPv6 addresses (only) in square brackets for compatibility.
    if (address.startsWith("[") && address.endsWith("]") && address.indexOf(':') != -1) {
        address = address.substring(1, address.length() - 1);
    }
    StructAddrinfo hints = new StructAddrinfo();
    hints.ai_flags = AI_NUMERICHOST;
    InetAddress[] addresses = null;
    try {
        addresses = Libcore.os.getaddrinfo(address, hints);
    } catch (GaiException ignored) {
    }
    return (addresses != null) ? addresses[0] : null;
}
Also used : StructAddrinfo(libcore.io.StructAddrinfo) GaiException(libcore.io.GaiException)

Example 2 with StructAddrinfo

use of libcore.io.StructAddrinfo in project XobotOS by xamarin.

the class InetAddress method parseNumericAddressNoThrow.

private static InetAddress parseNumericAddressNoThrow(String address) {
    // Accept IPv6 addresses (only) in square brackets for compatibility.
    if (address.startsWith("[") && address.endsWith("]") && address.indexOf(':') != -1) {
        address = address.substring(1, address.length() - 1);
    }
    StructAddrinfo hints = new StructAddrinfo();
    hints.ai_flags = AI_NUMERICHOST;
    InetAddress[] addresses = null;
    try {
        addresses = Libcore.os.getaddrinfo(address, hints);
    } catch (GaiException ignored) {
    }
    return (addresses != null) ? addresses[0] : null;
}
Also used : StructAddrinfo(libcore.io.StructAddrinfo) GaiException(libcore.io.GaiException)

Example 3 with StructAddrinfo

use of libcore.io.StructAddrinfo in project j2objc by google.

the class InetAddress method parseNumericAddressNoThrow.

private static InetAddress parseNumericAddressNoThrow(String address) {
    // Accept IPv6 addresses (only) in square brackets for compatibility.
    if (address.startsWith("[") && address.endsWith("]") && address.indexOf(':') != -1) {
        address = address.substring(1, address.length() - 1);
    }
    StructAddrinfo hints = new StructAddrinfo();
    hints.ai_flags = AI_NUMERICHOST;
    InetAddress[] addresses = null;
    try {
        addresses = NetworkOs.getaddrinfo(address, hints);
    } catch (GaiException ignored) {
    }
    return (addresses != null) ? addresses[0] : null;
}
Also used : StructAddrinfo(libcore.io.StructAddrinfo) GaiException(libcore.io.GaiException)

Example 4 with StructAddrinfo

use of libcore.io.StructAddrinfo in project j2objc by google.

the class InetAddress method lookupHostByName.

/**
     * Resolves a hostname to its IP addresses using a cache.
     *
     * @param host the hostname to resolve.
     * @return the IP addresses of the host.
     */
private static InetAddress[] lookupHostByName(String host) throws UnknownHostException {
    BlockGuard.getThreadPolicy().onNetwork();
    // Do we have a result cached?
    Object cachedResult = addressCache.get(host);
    if (cachedResult != null) {
        if (cachedResult instanceof InetAddress[]) {
            // A cached positive result.
            return (InetAddress[]) cachedResult;
        } else {
            // A cached negative result.
            throw new UnknownHostException((String) cachedResult);
        }
    }
    try {
        StructAddrinfo hints = new StructAddrinfo();
        hints.ai_flags = AI_ADDRCONFIG;
        hints.ai_family = AF_UNSPEC;
        // If we don't specify a socket type, every address will appear twice, once
        // for SOCK_STREAM and one for SOCK_DGRAM. Since we do not return the family
        // anyway, just pick one.
        hints.ai_socktype = SOCK_STREAM;
        InetAddress[] addresses = NetworkOs.getaddrinfo(host, hints);
        // TODO: should getaddrinfo set the hostname of the InetAddresses it returns?
        for (InetAddress address : addresses) {
            address.hostName = host;
        }
        addressCache.put(host, addresses);
        return addresses;
    } catch (GaiException gaiException) {
        // http://code.google.com/p/android/issues/detail?id=15722
        if (gaiException.getCause() instanceof ErrnoException) {
            if (((ErrnoException) gaiException.getCause()).errno == EACCES) {
                throw new SecurityException("Permission denied (missing INTERNET permission?)", gaiException);
            }
        }
        // Otherwise, throw an UnknownHostException.
        String detailMessage = "Unable to resolve host \"" + host + "\": " + NetworkOs.gai_strerror(gaiException.error);
        addressCache.putUnknownHost(host, detailMessage);
        throw gaiException.rethrowAsUnknownHostException(detailMessage);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) StructAddrinfo(libcore.io.StructAddrinfo) GaiException(libcore.io.GaiException)

Example 5 with StructAddrinfo

use of libcore.io.StructAddrinfo in project robovm by robovm.

the class InetAddress method lookupHostByName.

/**
     * Resolves a hostname to its IP addresses using a cache.
     *
     * @param host the hostname to resolve.
     * @return the IP addresses of the host.
     */
private static InetAddress[] lookupHostByName(String host) throws UnknownHostException {
    BlockGuard.getThreadPolicy().onNetwork();
    // Do we have a result cached?
    Object cachedResult = addressCache.get(host);
    if (cachedResult != null) {
        if (cachedResult instanceof InetAddress[]) {
            // A cached positive result.
            return (InetAddress[]) cachedResult;
        } else {
            // A cached negative result.
            throw new UnknownHostException((String) cachedResult);
        }
    }
    try {
        StructAddrinfo hints = new StructAddrinfo();
        hints.ai_flags = AI_ADDRCONFIG;
        hints.ai_family = AF_UNSPEC;
        // If we don't specify a socket type, every address will appear twice, once
        // for SOCK_STREAM and one for SOCK_DGRAM. Since we do not return the family
        // anyway, just pick one.
        hints.ai_socktype = SOCK_STREAM;
        InetAddress[] addresses = Libcore.os.getaddrinfo(host, hints);
        // TODO: should getaddrinfo set the hostname of the InetAddresses it returns?
        for (InetAddress address : addresses) {
            address.hostName = host;
        }
        addressCache.put(host, addresses);
        return addresses;
    } catch (GaiException gaiException) {
        // http://code.google.com/p/android/issues/detail?id=15722
        if (gaiException.getCause() instanceof ErrnoException) {
            if (((ErrnoException) gaiException.getCause()).errno == EACCES) {
                throw new SecurityException("Permission denied (missing INTERNET permission?)", gaiException);
            }
        }
        // Otherwise, throw an UnknownHostException.
        String detailMessage = "Unable to resolve host \"" + host + "\": " + Libcore.os.gai_strerror(gaiException.error);
        addressCache.putUnknownHost(host, detailMessage);
        throw gaiException.rethrowAsUnknownHostException(detailMessage);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) StructAddrinfo(libcore.io.StructAddrinfo) GaiException(libcore.io.GaiException)

Aggregations

GaiException (libcore.io.GaiException)6 StructAddrinfo (libcore.io.StructAddrinfo)6 ErrnoException (libcore.io.ErrnoException)2