1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-29 13:01:14 +03:00

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 f1e80d8d8c
Follow-up to 5644eea216

Closes #980
This commit is contained in:
Viktor Szakats
2023-04-18 08:19:34 +00:00
parent 01f3fbf01c
commit 191c4e8c71
7 changed files with 35 additions and 105 deletions

View File

@ -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

View File

@ -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 <winsock2.h>
#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 <sys/ioctl.h>
@ -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 <socket.h>
@ -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

View File

@ -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 <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
@ -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 <unistd.h>
#include <stropts.h>
@ -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 <winsock2.h>
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 <sys/ioctl.h>
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 <socket.h>
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()

View File

@ -59,9 +59,6 @@
/* Define to 1 if you have the <arpa/inet.h> 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 <inttypes.h> 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

View File

@ -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)

View File

@ -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

View File

@ -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
}