Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8346204

Socket.connect specified to throw UHE for unresolved address is problematic for SOCKS V5 proxy

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P2 P2
    • 24, 25
    • core-libs
    • None
    • behavioral
    • minimal
    • Hide
      There should be no compatibility risk associated with this particular change, we're fixing a regression introduced by JDK-8343791.
      Show
      There should be no compatibility risk associated with this particular change, we're fixing a regression introduced by JDK-8343791 .
    • Java API
    • SE

      Summary

      Socket::connect should succeed with an unresolved address if the socket is using a proxy and the proxy can resolve the address.

      Problem

      The check for the unresolved address in Socket::connect was too early preventing the implementation to resolve the address if a proxy was used. The check is now delegated to the implementation. The behavior remains the same when an UnknownHostException is thrown: both the socket and the socket implementation are closed.

      Solution

      Delegate the unresolved address check to the socket implementation. The API documentation is clarified with respect to socket close and UnknownHostException.

      Specification

      In src/java.base/share/classes/java/net/Socket.java

           /**
            * Connects this socket to the server.
            *
      -     * <p> If the endpoint is an unresolved {@link InetSocketAddress}, or the
      -     * connection cannot be established, then the socket is closed, and an
      -     * {@link IOException} is thrown.
      +     * <p> If the connection cannot be established, then the socket is closed,
      +     * and an {@link IOException} is thrown.
            *
            * <p> This method is {@linkplain Thread#interrupt() interruptible} in the
            * following circumstances:
            * <ol>
            *   <li> The socket is {@linkplain SocketChannel#socket() associated} with
            *        a {@link SocketChannel SocketChannel}.
            *        In that case, interrupting a thread establishing a connection will
            *        close the underlying channel and cause this method to throw
            *        {@link ClosedByInterruptException} with the interrupt status set.
            *   <li> The socket uses the system-default socket implementation and a
            *        {@linkplain Thread#isVirtual() virtual thread} is establishing a
            *        connection. In that case, interrupting the virtual thread will
            *        cause it to wakeup and close the socket. This method will then throw
            *        {@code SocketException} with the interrupt status set.
            * </ol>
            *
            * @param   endpoint the {@code SocketAddress}
            * @throws  IOException if an error occurs during the connection, the socket
            *          is already connected or the socket is closed
      -     * @throws  UnknownHostException if the endpoint is an unresolved
      -     *          {@link InetSocketAddress}
      +     * @throws  UnknownHostException if the connection could not be established
      +     *          because the endpoint is an unresolved {@link InetSocketAddress}
            * @throws  java.nio.channels.IllegalBlockingModeException
            *          if this socket has an associated channel,
            *          and the channel is in non-blocking mode
            * @throws  IllegalArgumentException if endpoint is null or is a
            *          SocketAddress subclass not supported by this socket
            * @since 1.4
            */
           public void connect(SocketAddress endpoint) throws IOException {
      
      
      
      
           /**
            * Connects this socket to the server with a specified timeout value.
            * A timeout of zero is interpreted as an infinite timeout. The connection
            * will then block until established or an error occurs.
            *
      -     * <p> If the endpoint is an unresolved {@link InetSocketAddress}, the
      -     * connection cannot be established, or the timeout expires before the
      -     * connection is established, then the socket is closed, and an
      +     * <p> If the connection cannot be established, or the timeout expires
      +     * before the connection is established, then the socket is closed, and an
            * {@link IOException} is thrown.
            *
            * <p> This method is {@linkplain Thread#interrupt() interruptible} in the
            * following circumstances:
            * <ol>
            *   <li> The socket is {@linkplain SocketChannel#socket() associated} with
            *        a {@link SocketChannel SocketChannel}.
            *        In that case, interrupting a thread establishing a connection will
            *        close the underlying channel and cause this method to throw
            *        {@link ClosedByInterruptException} with the interrupt status set.
            *   <li> The socket uses the system-default socket implementation and a
            *        {@linkplain Thread#isVirtual() virtual thread} is establishing a
            *        connection. In that case, interrupting the virtual thread will
            *        cause it to wakeup and close the socket. This method will then throw
            *        {@code SocketException} with the interrupt status set.
            * </ol>
            *
            * @param   endpoint the {@code SocketAddress}
            * @param   timeout  the timeout value to be used in milliseconds.
            * @throws  IOException if an error occurs during the connection, the socket
            *          is already connected or the socket is closed
            * @throws  SocketTimeoutException if timeout expires before connecting
      -     * @throws  UnknownHostException if the endpoint is an unresolved
      -     *          {@link InetSocketAddress}
      +     * @throws  UnknownHostException if the connection could not be established
      +     *          because the endpoint is an unresolved {@link InetSocketAddress}
            * @throws  java.nio.channels.IllegalBlockingModeException
            *          if this socket has an associated channel,
            *          and the channel is in non-blocking mode
            * @throws  IllegalArgumentException if endpoint is null or is a
            *          SocketAddress subclass not supported by this socket, or
            *          if {@code timeout} is negative
            * @since 1.4
            */
           public void connect(SocketAddress endpoint, int timeout) throws IOException {

            vyazici Volkan Yazici
            webbuggrp Webbug Group
            Alan Bateman, Daniel Fuchs
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: