From 191c4e8c71d4a3f41ea9db15ba581a35bf177762 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Tue, 18 Apr 2023 08:19:34 +0000 Subject: [PATCH] build: assume non-blocking I/O on Windows Drop checks from Windows builds and enable it based on `WIN32`. This saves detection time and also makes 3rd party builds simpler. Also: - delete `HAVE_DISABLED_NONBLOCKING`, that we used in build tools to explicitly disable an explicit `#error` in `session.c`. - replace existing `WSAEWOULDBLOCK` check for Windows support with `WIN32`. Cleaner with the same result. Follow-up to f1e80d8d8ce9570d81836da96ba02f4d4552a7b3 Follow-up to 5644eea2161b17f7c16e18f3a10465ebb217ca1f Closes #980 --- CMakeLists.txt | 10 +++-- acinclude.m4 | 30 +------------ cmake/CheckNonblockingSocketSupport.cmake | 51 +++++------------------ os400/libssh2_config.h | 6 --- src/libssh2_config_cmake.h.in | 2 - src/libssh2_setup.h | 1 - src/session.c | 40 ++++++++---------- 7 files changed, 35 insertions(+), 105 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3ff5988..2a28c9be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -188,10 +188,12 @@ endif() # Non-blocking socket support tests. Use a separate, yet unset variable # for the socket libraries to not link against the other configured # dependencies which might not have been built yet. -cmake_push_check_state() -set(CMAKE_REQUIRED_LIBRARIES ${SOCKET_LIBRARIES}) -check_nonblocking_socket_support() -cmake_pop_check_state() +if(NOT WIN32) + cmake_push_check_state() + set(CMAKE_REQUIRED_LIBRARIES ${SOCKET_LIBRARIES}) + check_nonblocking_socket_support() + cmake_pop_check_state() +endif() ## Cryptography backend choice diff --git a/acinclude.m4 b/acinclude.m4 index 9f771371..0a3a37da 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -601,28 +601,6 @@ AC_DEFINE(HAVE_FIONBIO, 1, [use FIONBIO for non-blocking sockets]) dnl FIONBIO test was also bad dnl the code was bad, try a different program now, test 3 - AC_TRY_COMPILE([ -/* headers for ioctlsocket test (Windows) */ -#undef inline -#ifdef HAVE_WINDOWS_H -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include -#endif -],[ -/* ioctlsocket source code */ - SOCKET sd; - unsigned long flags = 0; - sd = socket(0, 0, 0); - ioctlsocket(sd, FIONBIO, &flags); -],[ -dnl ioctlsocket test was good -nonblock="ioctlsocket" -AC_DEFINE(HAVE_IOCTLSOCKET, 1, [use ioctlsocket() for non-blocking sockets]) -],[ -dnl ioctlsocket did not compile!, go to test 4 - AC_TRY_LINK([ /* headers for IoctlSocket test (Amiga?) */ #include @@ -635,7 +613,7 @@ dnl ioctlsocket test was good nonblock="IoctlSocket" AC_DEFINE(HAVE_IOCTLSOCKET_CASE, 1, [use Ioctlsocket() for non-blocking sockets]) ],[ -dnl Ioctlsocket did not compile, do test 5! +dnl Ioctlsocket did not compile, do test 4! AC_TRY_COMPILE([ /* headers for SO_NONBLOCK test (BeOS) */ #include @@ -649,12 +627,8 @@ dnl the SO_NONBLOCK test was good nonblock="SO_NONBLOCK" AC_DEFINE(HAVE_SO_NONBLOCK, 1, [use SO_NONBLOCK for non-blocking sockets]) ],[ -dnl test 5 did not compile! +dnl test 4 did not compile! nonblock="nada" -AC_DEFINE(HAVE_DISABLED_NONBLOCKING, 1, [disabled non-blocking sockets]) -]) -dnl end of fifth test - ]) dnl end of forth test diff --git a/cmake/CheckNonblockingSocketSupport.cmake b/cmake/CheckNonblockingSocketSupport.cmake index 12e2bd64..6e02cccd 100644 --- a/cmake/CheckNonblockingSocketSupport.cmake +++ b/cmake/CheckNonblockingSocketSupport.cmake @@ -1,6 +1,6 @@ include(CheckCSourceCompiles) -# - check_nonblocking_socket_support() +# check_nonblocking_socket_support() # # Check for how to set a socket to non-blocking state. There seems to exist # four known different ways, with the one used almost everywhere being POSIX @@ -11,10 +11,8 @@ include(CheckCSourceCompiles) # method (if any): # HAVE_O_NONBLOCK # HAVE_FIONBIO -# HAVE_IOCTLSOCKET # HAVE_IOCTLSOCKET_CASE # HAVE_SO_NONBLOCK -# HAVE_DISABLED_NONBLOCKING # # The following variables may be set before calling this macro to # modify the way the check is run: @@ -27,8 +25,7 @@ include(CheckCSourceCompiles) macro(check_nonblocking_socket_support) # There are two known platforms (AIX 3.x and SunOS 4.1.x) where the # O_NONBLOCK define is found but does not work. - if(NOT WIN32) - check_c_source_compiles(" + check_c_source_compiles(" #include #include #include @@ -53,12 +50,10 @@ int main(void) int socket = 0; (void)fcntl(socket, F_SETFL, O_NONBLOCK); }" - HAVE_O_NONBLOCK) - endif() + HAVE_O_NONBLOCK) if(NOT HAVE_O_NONBLOCK) - if(NOT WIN32) - check_c_source_compiles("/* FIONBIO test (old-style unix) */ + check_c_source_compiles("/* FIONBIO test (old-style unix) */ #include #include @@ -68,30 +63,10 @@ int main(void) int flags = 0; (void)ioctl(socket, FIONBIO, &flags); }" - HAVE_FIONBIO) - endif() + HAVE_FIONBIO) if(NOT HAVE_FIONBIO) - if(WIN32) - check_c_source_compiles("/* ioctlsocket test (Windows) */ -#undef inline -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include - -int main(void) -{ - SOCKET sd = socket(0, 0, 0); - unsigned long flags = 0; - (void)ioctlsocket(sd, FIONBIO, &flags); -}" - HAVE_IOCTLSOCKET) - endif() - - if(NOT HAVE_IOCTLSOCKET) - check_c_source_compiles("/* IoctlSocket test (Amiga?) */ + check_c_source_compiles("/* IoctlSocket test (Amiga?) */ #include int main(void) @@ -99,10 +74,10 @@ int main(void) int socket = 0; (void)IoctlSocket(socket, FIONBIO, (long)1); }" - HAVE_IOCTLSOCKET_CASE) + HAVE_IOCTLSOCKET_CASE) - if(NOT HAVE_IOCTLSOCKET_CASE) - check_c_source_compiles("/* SO_NONBLOCK test (BeOS) */ + if(NOT HAVE_IOCTLSOCKET_CASE) + check_c_source_compiles("/* SO_NONBLOCK test (BeOS) */ #include int main(void) @@ -111,13 +86,7 @@ int main(void) int socket = 0; (void)setsockopt(socket, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b)); }" - HAVE_SO_NONBLOCK) - - if(NOT HAVE_SO_NONBLOCK) - # No non-blocking socket method found - set(HAVE_DISABLED_NONBLOCKING 1) - endif() - endif() + HAVE_SO_NONBLOCK) endif() endif() endif() diff --git a/os400/libssh2_config.h b/os400/libssh2_config.h index f3bf725d..99a6bcf7 100644 --- a/os400/libssh2_config.h +++ b/os400/libssh2_config.h @@ -59,9 +59,6 @@ /* Define to 1 if you have the header file. */ #define HAVE_ARPA_INET_H 1 -/* disabled non-blocking sockets */ -#undef HAVE_DISABLED_NONBLOCKING - /* use FIONBIO for non-blocking sockets */ #undef HAVE_FIONBIO @@ -71,9 +68,6 @@ /* Define to 1 if you have the header file. */ #define HAVE_INTTYPES_H 1 -/* use ioctlsocket() for non-blocking sockets */ -#undef HAVE_IOCTLSOCKET - /* use Ioctlsocket() for non-blocking sockets */ #undef HAVE_IOCTLSOCKET_CASE diff --git a/src/libssh2_config_cmake.h.in b/src/libssh2_config_cmake.h.in index 89784b4f..58dfa6b8 100644 --- a/src/libssh2_config_cmake.h.in +++ b/src/libssh2_config_cmake.h.in @@ -68,10 +68,8 @@ /* Socket non-blocking support */ #cmakedefine HAVE_O_NONBLOCK #cmakedefine HAVE_FIONBIO -#cmakedefine HAVE_IOCTLSOCKET #cmakedefine HAVE_IOCTLSOCKET_CASE #cmakedefine HAVE_SO_NONBLOCK -#cmakedefine HAVE_DISABLED_NONBLOCKING /* attribute to export symbol */ #if defined(LIBSSH2_EXPORTS) && defined(LIBSSH2_LIBRARY) diff --git a/src/libssh2_setup.h b/src/libssh2_setup.h index a110d619..48ebe20a 100644 --- a/src/libssh2_setup.h +++ b/src/libssh2_setup.h @@ -22,7 +22,6 @@ /* Hand-crafted configuration for platforms which lack config tool. */ #elif defined(WIN32) -#define HAVE_IOCTLSOCKET #define HAVE_SELECT #define HAVE_SNPRINTF diff --git a/src/session.c b/src/session.c index 6998ed1e..3613d522 100644 --- a/src/session.c +++ b/src/session.c @@ -308,12 +308,6 @@ session_nonblock(libssh2_socket_t sockfd, /* operate on this */ flags = nonblock; return ioctl(sockfd, FIONBIO, &flags); -#elif defined(HAVE_IOCTLSOCKET) - /* Windows */ - unsigned long flags; - flags = nonblock; - - return ioctlsocket(sockfd, FIONBIO, &flags); #elif defined(HAVE_IOCTLSOCKET_CASE) /* presumably for Amiga */ return IoctlSocket(sockfd, FIONBIO, (long) nonblock); @@ -321,12 +315,15 @@ session_nonblock(libssh2_socket_t sockfd, /* operate on this */ /* BeOS */ long b = nonblock ? 1 : 0; return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b)); -#elif defined(HAVE_DISABLED_NONBLOCKING) +#elif defined(WIN32) + unsigned long flags; + + flags = nonblock; + return ioctlsocket(sockfd, FIONBIO, &flags); +#else (void)sockfd; (void)nonblock; return 0; /* returns success */ -#else -#error "no non-blocking method was found/used/set" #endif } @@ -347,17 +344,6 @@ get_socket_nonblocking(libssh2_socket_t sockfd) return 1; } return (flags & O_NONBLOCK); -#elif defined(WSAEWOULDBLOCK) - /* Windows */ - unsigned int option_value; - socklen_t option_len = sizeof(option_value); - - if(getsockopt - (sockfd, SOL_SOCKET, SO_ERROR, (void *) &option_value, &option_len)) { - /* Assume blocking on error */ - return 1; - } - return (int) option_value; #elif defined(HAVE_SO_NONBLOCK) /* BeOS */ long b; @@ -382,11 +368,19 @@ get_socket_nonblocking(libssh2_socket_t sockfd) return 1; } return 0; -#elif defined(HAVE_DISABLED_NONBLOCKING) +#elif defined(WIN32) + unsigned int option_value; + socklen_t option_len = sizeof(option_value); + + if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, + (void *) &option_value, &option_len)) { + /* Assume blocking on error */ + return 1; + } + return (int) option_value; +#else (void)sockfd; return 1; /* returns blocking */ -#else -#error "no non-blocking method was found/used/get" #endif }