From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2210) id DAFB4389041C; Mon, 26 Apr 2021 13:20:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DAFB4389041C Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Ken Brown To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: connect: set connect state for DGRAM sockets X-Act-Checkin: newlib-cygwin X-Git-Author: Ken Brown X-Git-Refname: refs/heads/master X-Git-Oldrev: ae6e6c3526e11ec09a7e6d23a7e40b4494a0e51e X-Git-Newrev: 2be07f75548a9111ebf9b5cb4bb1040c3dc4f4f0 Message-Id: <20210426132018.DAFB4389041C@sourceware.org> Date: Mon, 26 Apr 2021 13:20:18 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 26 Apr 2021 13:20:19 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=2be07f75548a9111ebf9b5cb4bb1040c3dc4f4f0 commit 2be07f75548a9111ebf9b5cb4bb1040c3dc4f4f0 Author: Ken Brown Date: Fri Apr 23 14:40:07 2021 -0400 Cygwin: connect: set connect state for DGRAM sockets When connect is called on a DGRAM socket, the call to Winsock's connect can immediately return successfully rather than failing with WSAEWOULDBLOCK. Set the connect state to "connected" in this case. Previously the connect state remained "connect_pending" after the successful connection. Diff: --- winsup/cygwin/fhandler_socket_inet.cc | 19 +++++++++++-------- winsup/cygwin/fhandler_socket_local.cc | 19 +++++++++++-------- winsup/cygwin/release/3.2.1 | 5 ++++- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/winsup/cygwin/fhandler_socket_inet.cc b/winsup/cygwin/fhandler_socket_inet.cc index 4ecb31a27..f6bb8c503 100644 --- a/winsup/cygwin/fhandler_socket_inet.cc +++ b/winsup/cygwin/fhandler_socket_inet.cc @@ -785,11 +785,13 @@ fhandler_socket_inet::connect (const struct sockaddr *name, int namelen) if (get_inet_addr_inet (name, namelen, &sst, &namelen) == SOCKET_ERROR) return SOCKET_ERROR; - /* Initialize connect state to "connect_pending". State is ultimately set - to "connected" or "connect_failed" in wait_for_events when the FD_CONNECT - event occurs. Note that the underlying OS sockets are always non-blocking - and a successfully initiated non-blocking Winsock connect always returns - WSAEWOULDBLOCK. Thus it's safe to rely on event handling. + /* Initialize connect state to "connect_pending". In the SOCK_STREAM + case, the state is ultimately set to "connected" or "connect_failed" in + wait_for_events when the FD_CONNECT event occurs. Note that the + underlying OS sockets are always non-blocking in this case and a + successfully initiated non-blocking Winsock connect always returns + WSAEWOULDBLOCK. Thus it's safe to rely on event handling. For DGRAM + sockets, however, connect can return immediately. Check for either unconnected or connect_failed since in both cases it's allowed to retry connecting the socket. It's also ok (albeit ugly) to @@ -801,7 +803,9 @@ fhandler_socket_inet::connect (const struct sockaddr *name, int namelen) connect_state (connect_pending); int res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen); - if (!is_nonblocking () + if (!res) + connect_state (connected); + else if (!is_nonblocking () && res == SOCKET_ERROR && WSAGetLastError () == WSAEWOULDBLOCK) res = wait_for_events (FD_CONNECT | FD_CLOSE, 0); @@ -824,8 +828,7 @@ fhandler_socket_inet::connect (const struct sockaddr *name, int namelen) Convert to POSIX/Linux compliant EISCONN. */ else if (err == WSAEINVAL && connect_state () == listener) WSASetLastError (WSAEISCONN); - /* Any other error except WSAEALREADY during connect_pending means the - connect failed. */ + /* Any other error except WSAEALREADY means the connect failed. */ else if (connect_state () == connect_pending && err != WSAEALREADY) connect_state (connect_failed); set_winsock_errno (); diff --git a/winsup/cygwin/fhandler_socket_local.cc b/winsup/cygwin/fhandler_socket_local.cc index ad7dd0a98..1c8d48b58 100644 --- a/winsup/cygwin/fhandler_socket_local.cc +++ b/winsup/cygwin/fhandler_socket_local.cc @@ -914,11 +914,13 @@ fhandler_socket_local::connect (const struct sockaddr *name, int namelen) if (get_socket_type () == SOCK_STREAM) af_local_set_cred (); - /* Initialize connect state to "connect_pending". State is ultimately set - to "connected" or "connect_failed" in wait_for_events when the FD_CONNECT - event occurs. Note that the underlying OS sockets are always non-blocking - and a successfully initiated non-blocking Winsock connect always returns - WSAEWOULDBLOCK. Thus it's safe to rely on event handling. + /* Initialize connect state to "connect_pending". In the SOCK_STREAM + case, the state is ultimately set to "connected" or "connect_failed" in + wait_for_events when the FD_CONNECT event occurs. Note that the + underlying OS sockets are always non-blocking in this case and a + successfully initiated non-blocking Winsock connect always returns + WSAEWOULDBLOCK. Thus it's safe to rely on event handling. For DGRAM + sockets, however, connect can return immediately. Check for either unconnected or connect_failed since in both cases it's allowed to retry connecting the socket. It's also ok (albeit ugly) to @@ -930,7 +932,9 @@ fhandler_socket_local::connect (const struct sockaddr *name, int namelen) connect_state (connect_pending); int res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen); - if (!is_nonblocking () + if (!res) + connect_state (connected); + else if (!is_nonblocking () && res == SOCKET_ERROR && WSAGetLastError () == WSAEWOULDBLOCK) res = wait_for_events (FD_CONNECT | FD_CLOSE, 0); @@ -953,8 +957,7 @@ fhandler_socket_local::connect (const struct sockaddr *name, int namelen) Convert to POSIX/Linux compliant EISCONN. */ else if (err == WSAEINVAL && connect_state () == listener) WSASetLastError (WSAEISCONN); - /* Any other error except WSAEALREADY during connect_pending means the - connect failed. */ + /* Any other error except WSAEALREADY means the connect failed. */ else if (connect_state () == connect_pending && err != WSAEALREADY) connect_state (connect_failed); set_winsock_errno (); diff --git a/winsup/cygwin/release/3.2.1 b/winsup/cygwin/release/3.2.1 index c1ada5d50..7662c7114 100644 --- a/winsup/cygwin/release/3.2.1 +++ b/winsup/cygwin/release/3.2.1 @@ -12,7 +12,7 @@ Bug Fixes - Fix values returned by select(2) for shutdown sockets. Addresses: https://cygwin.com/pipermail/cygwin-developers/2021-April/012092.html -- Introduce a new hypotl(3) function not suffering unnecesswry overflows. +- Introduce a new hypotl(3) function not suffering unnecessary overflows. Addresses: https://cygwin.com/pipermail/cygwin/2021-April/248302.html - Fix path handling for paths spanning native symlinks. @@ -25,3 +25,6 @@ Bug Fixes - Handle two race conditions in pseudo console usage. Addresses: https://cygwin.com/pipermail/cygwin/2021-April/248292.html + +- Fix a bug in recognizing a successful completion of connect(2) on a + datagram socket.