Skip to main content

UnknownHostException

java.net.UnknownHostException

UnknownHostException is described in the javadoc comments as:

Thrown to indicate that the IP address of a host could not be determined.
author: Jonathan Payne version: 1.15, 12/19/03 since: JDK1.0

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

java.net.InetAddress.getAllByName(String, InetAddress)

private static InetAddress[] getAllByName(String host, InetAddress reqAddr) throws UnknownHostException {
    if (host == null || host.length() == 0) {
        InetAddress[] ret = new InetAddress[1];
        ret[0] = impl.loopbackAddress();
        return ret;
    }
    boolean ipv6Expected = false;
    if (host.charAt(0) == '[') {
        if (host.length() > 2 && host.charAt(host.length() - 1) == ']') {
            host = host.substring(1, host.length() - 1);
            ipv6Expected = true;
        } else {
            throw new UnknownHostException(host);
        }
    }
    if (Character.digit(host.charAt(0), 16) != -1 || (host.charAt(0) == ':')) {
        byte[] addr = null;
        int numericZone = -1;
        String ifname = null;
        addr = IPAddressUtil.textToNumericFormatV4(host);
        if (addr == null) {
            int pos;
            if ((pos = host.indexOf('%')) != -1) {
                numericZone = checkNumericZone(host);
                if (numericZone == -1) {
                    ifname = host.substring(pos + 1);
                }
            }
            addr = IPAddressUtil.textToNumericFormatV6(host);
        } else if (ipv6Expected) {
            throw new UnknownHostException('[' + host + ']');
        }
        InetAddress[] ret = new InetAddress[1];
        if (addr != null) {
            if (addr.length == Inet4Address.INADDRSZ) {
                ret[0] = new Inet4Address(null, addr);
            } else {
                if (ifname != null) {
                    ret[0] = new Inet6Address(null, addr, ifname);
                } else {
                    ret[0] = new Inet6Address(null, addr, numericZone);
                }
            }
            return ret;
        }
    } else if (ipv6Expected) {
        throw new UnknownHostException('[' + host + ']');
    }
    return getAllByName0(host, reqAddr, true);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.InetAddress.getAllByName0(String, InetAddress, boolean)

private static InetAddress[] getAllByName0(String host, InetAddress reqAddr, boolean check) throws UnknownHostException {
    Object obj = null;
    Object objcopy = null;
    if (check) {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkConnect(host, -1);
        }
    }
    obj = getCachedAddress(host);
    if (obj == null) {
        try {
            obj = getAddressFromNameService(host, reqAddr);
        } catch (UnknownHostException uhe) {
            throw new UnknownHostException(host + ': ' + uhe.getMessage());
        }
    }
    if (obj == unknown_array) throw new UnknownHostException(host);
    objcopy = ((InetAddress[]) obj).clone();
    return (InetAddress[]) objcopy;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.PlainSocketImpl.connect(SocketAddress, int)

/**
     * Creates a socket and connects it to the specified address on
     * the specified port.
     * @param address the address
     * @param timeout the timeout value in milliseconds, or zero for no timeout.
     * @throws IOException if connection fails
     * @throws  IllegalArgumentException if address is null or is a
     *          SocketAddress subclass not supported by this socket
     * @since 1.4
     */
protected void connect(SocketAddress address, int timeout) throws IOException {
    if (address == null || !(address instanceof InetSocketAddress)) throw new IllegalArgumentException('unsupported address type');
    InetSocketAddress addr = (InetSocketAddress) address;
    if (addr.isUnresolved()) throw new UnknownHostException(addr.getHostName());
    this.port = addr.getPort();
    this.address = addr.getAddress();
    try {
        connectToAddress(this.address, port, timeout);
        return;
    } catch (IOException e) {
        close();
        throw e;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.SocketPermission.getIP()

/**
     * get IP addresses. Sets invalid to true if we can't get them.
     */
void getIP() throws UnknownHostException {
    if (addresses != null || wildcard || invalid) return;
    try {
        String host;
        if (getName().charAt(0) == '[') {
            host = getName().substring(1, getName().indexOf(']'));
        } else {
            int i = getName().indexOf(':');
            if (i == -1) host = getName(); else {
                host = getName().substring(0, i);
            }
        }
        addresses = new InetAddress[] { InetAddress.getAllByName0(host, false)[0] };
    } catch (UnknownHostException uhe) {
        invalid = true;
        throw uhe;
    } catch (IndexOutOfBoundsException iobe) {
        invalid = true;
        throw new UnknownHostException(getName());
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.SocksSocketImpl.connect(SocketAddress, int)

/**
     * Connects the Socks Socket to the specified endpoint. It will first
     * connect to the SOCKS proxy and negotiate the access. If the proxy
     * grants the connections, then the connect is successful and all
     * further traffic will go to the 'real' endpoint.
     * @param endpoint the <code>SocketAddress</code> to connect to.
     * @param timeout  the timeout value in milliseconds
     * @throws IOException if the connection can't be established.
     * @throws SecurityException if there is a security manager and it
     *    doesn't allow the connection
     * @throws  IllegalArgumentException if endpoint is null or a
     *          SocketAddress subclass not supported by this socket
     */
protected void connect(SocketAddress endpoint, int timeout) throws IOException {
    SecurityManager security = System.getSecurityManager();
    if (endpoint == null || !(endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException('Unsupported address type');
    InetSocketAddress epoint = (InetSocketAddress) endpoint;
    if (security != null) {
        if (epoint.isUnresolved()) security.checkConnect(epoint.getHostName(), epoint.getPort()); else security.checkConnect(epoint.getAddress().getHostAddress(), epoint.getPort());
    }
    if (server == null) {
        ProxySelector sel = (ProxySelector) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

            public Object run() {
                return ProxySelector.getDefault();
            }
        });
        if (sel == null) {
            super.connect(epoint, timeout);
            return;
        }
        URI uri = null;
        String host = epoint.getHostString();
        if (epoint.getAddress() instanceof Inet6Address && (!host.startsWith('[')) && (host.indexOf(':') >= 0)) {
            host = '[' + host + ']';
        }
        try {
            uri = new URI('socket://' + ParseUtil.encodePath(host) + ':' + epoint.getPort());
        } catch (URISyntaxException e) {
            assert false : e;
        }
        Proxy p = null;
        IOException savedExc = null;
        java.util.Iterator<Proxy> iProxy = null;
        iProxy = sel.select(uri).iterator();
        if (iProxy == null || !(iProxy.hasNext())) {
            super.connect(epoint, timeout);
            return;
        }
        while (iProxy.hasNext()) {
            p = iProxy.next();
            if (p == null || p == Proxy.NO_PROXY) {
                super.connect(epoint, timeout);
                return;
            }
            if (p.type() != Proxy.Type.SOCKS) throw new SocketException('Unknown proxy type : ' + p.type());
            if (!(p.address() instanceof InetSocketAddress)) throw new SocketException('Unknow address type for proxy: ' + p);
            server = ((InetSocketAddress) p.address()).getHostString();
            port = ((InetSocketAddress) p.address()).getPort();
            try {
                privilegedConnect(server, port, timeout);
                break;
            } catch (IOException e) {
                sel.connectFailed(uri, p.address(), e);
                server = null;
                port = -1;
                savedExc = e;
            }
        }
        if (server == null) {
            throw new SocketException('Can't connect to SOCKS proxy:' + savedExc.getMessage());
        }
    } else {
        try {
            privilegedConnect(server, port, timeout);
        } catch (IOException e) {
            throw new SocketException(e.getMessage());
        }
    }
    BufferedOutputStream out = new BufferedOutputStream(cmdOut, 512);
    InputStream in = cmdIn;
    if (useV4) {
        if (epoint.isUnresolved()) throw new UnknownHostException(epoint.toString());
        connectV4(in, out, epoint);
        return;
    }
    out.write(PROTO_VERS);
    out.write(2);
    out.write(NO_AUTH);
    out.write(USER_PASSW);
    out.flush();
    byte[] data = new byte[2];
    int i = readSocksReply(in, data);
    if (i != 2 || ((int) data[0]) != PROTO_VERS) {
        if (epoint.isUnresolved()) throw new UnknownHostException(epoint.toString());
        connectV4(in, out, epoint);
        return;
    }
    if (((int) data[1]) == NO_METHODS) throw new SocketException('SOCKS : No acceptable methods');
    if (!authenticate(data[1], in, out)) {
        throw new SocketException('SOCKS : authentication failed');
    }
    out.write(PROTO_VERS);
    out.write(CONNECT);
    out.write(0);
    if (epoint.isUnresolved()) {
        out.write(DOMAIN_NAME);
        out.write(epoint.getHostName().length());
        try {
            out.write(epoint.getHostName().getBytes('ISO-8859-1'));
        } catch (java.io.UnsupportedEncodingException uee) {
            assert false;
        }
        out.write((epoint.getPort() >> 8) & 0xff);
        out.write((epoint.getPort() >> 0) & 0xff);
    } else if (epoint.getAddress() instanceof Inet6Address) {
        out.write(IPV6);
        out.write(epoint.getAddress().getAddress());
        out.write((epoint.getPort() >> 8) & 0xff);
        out.write((epoint.getPort() >> 0) & 0xff);
    } else {
        out.write(IPV4);
        out.write(epoint.getAddress().getAddress());
        out.write((epoint.getPort() >> 8) & 0xff);
        out.write((epoint.getPort() >> 0) & 0xff);
    }
    out.flush();
    data = new byte[4];
    i = readSocksReply(in, data);
    if (i != 4) throw new SocketException('Reply from SOCKS server has bad length');
    SocketException ex = null;
    int nport, len;
    byte[] addr;
    switch(data[1]) {
        case REQUEST_OK:
            switch(data[3]) {
                case IPV4:
                    addr = new byte[4];
                    i = readSocksReply(in, addr);
                    if (i != 4) throw new SocketException('Reply from SOCKS server badly formatted');
                    data = new byte[2];
                    i = readSocksReply(in, data);
                    if (i != 2) throw new SocketException('Reply from SOCKS server badly formatted');
                    nport = ((int) data[0] & 0xff) << 8;
                    nport += ((int) data[1] & 0xff);
                    break;
                case DOMAIN_NAME:
                    len = data[1];
                    byte[] host = new byte[len];
                    i = readSocksReply(in, host);
                    if (i != len) throw new SocketException('Reply from SOCKS server badly formatted');
                    data = new byte[2];
                    i = readSocksReply(in, data);
                    if (i != 2) throw new SocketException('Reply from SOCKS server badly formatted');
                    nport = ((int) data[0] & 0xff) << 8;
                    nport += ((int) data[1] & 0xff);
                    break;
                case IPV6:
                    len = data[1];
                    addr = new byte[len];
                    i = readSocksReply(in, addr);
                    if (i != len) throw new SocketException('Reply from SOCKS server badly formatted');
                    data = new byte[2];
                    i = readSocksReply(in, data);
                    if (i != 2) throw new SocketException('Reply from SOCKS server badly formatted');
                    nport = ((int) data[0] & 0xff) << 8;
                    nport += ((int) data[1] & 0xff);
                    break;
                default:
                    ex = new SocketException('Reply from SOCKS server contains wrong code');
                    break;
            }
            break;
        case GENERAL_FAILURE:
            ex = new SocketException('SOCKS server general failure');
            break;
        case NOT_ALLOWED:
            ex = new SocketException('SOCKS: Connection not allowed by ruleset');
            break;
        case NET_UNREACHABLE:
            ex = new SocketException('SOCKS: Network unreachable');
            break;
        case HOST_UNREACHABLE:
            ex = new SocketException('SOCKS: Host unreachable');
            break;
        case CONN_REFUSED:
            ex = new SocketException('SOCKS: Connection refused');
            break;
        case TTL_EXPIRED:
            ex = new SocketException('SOCKS: TTL expired');
            break;
        case CMD_NOT_SUPPORTED:
            ex = new SocketException('SOCKS: Command not supported');
            break;
        case ADDR_TYPE_NOT_SUP:
            ex = new SocketException('SOCKS: address type not supported');
            break;
    }
    if (ex != null) {
        in.close();
        out.close();
        throw ex;
    }
    external_address = epoint;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.InetAddress.getLocalHost()

/**
     * Returns the local host.
     * If there is a security manager, its
     * <code>checkConnect</code> method is called
     * with the local host name and <code>-1</code> 
     * as its arguments to see if the operation is allowed. 
     * If the operation is not allowed, an InetAddress representing
     * the loopback address is returned.
     * @return     the IP address of the local host.
     * @exception  UnknownHostException  if no IP address for the
     *               <code>host</code> could be found.
     * @see SecurityManager#checkConnect
     */
public static InetAddress getLocalHost() throws UnknownHostException {
    SecurityManager security = System.getSecurityManager();
    try {
        String local = impl.getLocalHostName();
        if (security != null) {
            security.checkConnect(local, -1);
        }
        InetAddress[] localAddrs;
        try {
            localAddrs = (InetAddress[]) InetAddress.getAddressFromNameService(local, null);
        } catch (UnknownHostException uhe) {
            throw new UnknownHostException(local + ': ' + uhe.getMessage());
        }
        return localAddrs[0];
    } catch (java.lang.SecurityException e) {
        return impl.loopbackAddress();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.Inet6Address.deriveNumericScope(String)

private int deriveNumericScope(String ifname) throws UnknownHostException {
    Enumeration en;
    try {
        en = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        throw new UnknownHostException('could not enumerate local network interfaces');
    }
    while (en.hasMoreElements()) {
        NetworkInterface ifc = (NetworkInterface) en.nextElement();
        if (ifc.getName().equals(ifname)) {
            Enumeration addresses = ifc.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = (InetAddress) addresses.nextElement();
                if (!(address instanceof Inet6Address)) {
                    continue;
                }
                Inet6Address ia6_addr = (Inet6Address) address;
                if (!differentLocalAddressTypes(ia6_addr)) {
                    continue;
                }
                return ia6_addr.scope_id;
            }
        }
    }
    throw new UnknownHostException('No matching address found for interface : ' + ifname);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.Inet6Address.initstr(String, byte, String)

private void initstr(String hostName, byte addr[], String ifname) throws UnknownHostException {
    try {
        NetworkInterface nif = NetworkInterface.getByName(ifname);
        if (nif == null) {
            throw new UnknownHostException('no such interface ' + ifname);
        }
        initif(hostName, addr, nif);
    } catch (SocketException e) {
        throw new UnknownHostException('SocketException thrown' + ifname);
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.Inet6Address.getByAddress(String, byte[], NetworkInterface)

/**
     * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
     * except that the IPv6 scope_id is set to the value corresponding to the given interface 
     * for the address type specified in <code>addr</code>. 
     * The call will fail with an UnknownHostException if the given interface does not have a numeric
     * scope_id assigned for the given address type (eg. link-local or site-local).
     * See <a href='Inet6Address.html#scoped'>here</a> for a description of IPv6
     * scoped addresses.
     * @param host the specified host
     * @param addr the raw IP address in network byte order
     * @param nif an interface this address must be associated with.
     * @return  an Inet6Address object created from the raw IP address.
     * @exception  UnknownHostException  if IP address is of illegal length, or if the interface
     *  does not have a numeric scope_id assigned for the given address type.
     * @since 1.5
     */
public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif) 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 == Inet6Address.INADDRSZ) {
            return new Inet6Address(host, addr, nif);
        }
    }
    throw new UnknownHostException('addr is of illegal length');
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.Inet6Address.getByAddress(String, byte[], int)

/**
     * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
     * except that the IPv6 scope_id is set to the given numeric value.
     * The scope_id is not checked to determine if it corresponds to any interface on the system.
     * See <a href='Inet6Address.html#scoped'>here</a> for a description of IPv6
     * scoped addresses.
     * @param host the specified host
     * @param addr the raw IP address in network byte order
     * @param scope_id the numeric scope_id for the address.
     * @return  an Inet6Address object created from the raw IP address.
     * @exception  UnknownHostException  if IP address is of illegal length.
     * @since 1.5
     */
public static Inet6Address getByAddress(String host, byte[] addr, int scope_id) 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 == Inet6Address.INADDRSZ) {
            return new Inet6Address(host, addr, scope_id);
        }
    }
    throw new UnknownHostException('addr is of illegal length');
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.InetAddress.getByAddress(String, byte[])

/**
     * Create an InetAddress based on the provided host name and IP address
     * No name service is checked for the validity of the address. 
     *  The host name can either be a machine name, such as
     * '<code>java.sun.com</code>', or a textual representation of its IP
     * address.
     *  No validity checking is done on the host name either.
     *  If addr specifies an IPv4 address an instance of Inet4Address 
     * will be returned; otherwise, an instance of Inet6Address 
     * will be returned.
     *  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');
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.net.Inet6Address.deriveNumericScope(NetworkInterface)

private int deriveNumericScope(NetworkInterface ifc) throws UnknownHostException {
    Enumeration addresses = ifc.getInetAddresses();
    while (addresses.hasMoreElements()) {
        InetAddress address = (InetAddress) addresses.nextElement();
        if (!(address instanceof Inet6Address)) {
            continue;
        }
        Inet6Address ia6_addr = (Inet6Address) address;
        if (!differentLocalAddressTypes(ia6_addr)) {
            continue;
        }
        return ia6_addr.scope_id;
    }
    throw new UnknownHostException('no scope_id found');
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

Comments

Popular posts from this blog

NullPointerException

java.lang.NullPointerException NullPointerException is described in the javadoc comments as: Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object. Taking the length of null as if it were an array. Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value. Applications should throw instances of this class to indicate other illegal uses of the null object. author: unascribed version: 1.19, 12/19/03 since: JDK1.0 Where is this exception thrown? Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown. The message ' java.lang.NullPointerException: ' is thrown within the method: com.sun.corba.se.impl.interceptors.ClientRequestInfoImpl.get_r

Connection refused: No available router to destination

This is a simple symptom-cause-solution blog entry only. I hope these blogs will help fellow administrators. Symptom The following exception occurs in WebLogic server logs. Most likely to occur during WebLogic server start-up, but similar exceptions may occur at other times. java.net.ConnectException: t3://myserver:8000: Destination unreachable; nested exception is: java.net.ConnectException: Connection refused: connect; No available router to destination] at weblogic.jndi.internal.ExceptionTranslator.toNamingException(ExceptionTranslator.java:49) at weblogic.jndi.WLInitialContextFactoryDelegate.toNamingException(WLInitialContextFactoryDelegate.java:773) at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:363) at weblogic.jndi.Environment.getContext(Environment.java:307) at weblogic.jndi.Environment.getContext(Environment.java:277) Cause This message (Connection refused: connect; No available

Recovering WebLogic Passwords

In one of my previous articles ( here ) I explained that the SerializedSystemIni.dat file in WebLogic contains the key used to encrypt and decrypt passwords. If you're not currently keeping this file secure I suggest you do, as with it someone can (to name a few things): Decrypt the WebLogic admin username and password from boot.properties. Recover database passwords, if JDBC Connection pools are configured, from config.xml. Recover the keystore passwords from config.xml and obtain SSL certificates stored in the jks keystores. Essentially, they can do whatever they want, so if you don't know who can read your SerializedSystemIni.dat files, look... now. In this article I will show how easy it is for this file to be used to recover lost passwords via a simple WLST script. The Script The script I use to decrypt passwords is incredibly short, and it works with WebLogic 8, 9 and 10 (probably for version 7 too). To use it, just create a new file called decryptpwd.py and paste the fol