cmake: fix support for UnixSockets feature on Win32

Move the definition of sockaddr_un struct from config-win32.h to
curl_setup.h, so that it could be shared by all build systems.

Add ADDRESS_FAMILY typedef for old mingw, now old mingw can also use
unix sockets.

Also fix the build of tests/server/sws.c on Win32 when USE_UNIX_SOCKETS
is defined.

Closes #7034
This commit is contained in:
Li Xinwei 2021-06-04 15:03:30 +08:00 committed by Daniel Stenberg
parent 62be096085
commit 30e491e5c9
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
5 changed files with 30 additions and 26 deletions

View File

@ -807,7 +807,11 @@ endif()
option(ENABLE_UNIX_SOCKETS "Define if you want Unix domain sockets support" ON)
if(ENABLE_UNIX_SOCKETS)
include(CheckStructHasMember)
check_struct_has_member("struct sockaddr_un" sun_path "sys/un.h" USE_UNIX_SOCKETS)
if(WIN32)
set(USE_UNIX_SOCKETS ON)
else()
check_struct_has_member("struct sockaddr_un" sun_path "sys/un.h" USE_UNIX_SOCKETS)
endif()
else()
unset(USE_UNIX_SOCKETS CACHE)
endif()

View File

@ -678,22 +678,8 @@ Vista
#define USE_WIN32_CRYPTO
#endif
/* On MinGW the ADDRESS_FAMILY typedef was committed alongside LUP_SECURE,
so we use it to check for the presence of the typedef. */
#include <ws2tcpip.h>
#if !defined(__MINGW32__) || defined(LUP_SECURE)
/* Define to use Unix sockets. */
#define USE_UNIX_SOCKETS
#if !defined(UNIX_PATH_MAX)
/* Replicating logic present in afunix.h of newer Windows 10 SDK versions */
# define UNIX_PATH_MAX 108
/* !checksrc! disable TYPEDEFSTRUCT 1 */
typedef struct sockaddr_un {
ADDRESS_FAMILY sun_family;
char sun_path[UNIX_PATH_MAX];
} SOCKADDR_UN, *PSOCKADDR_UN;
#endif
#endif
/* ---------------------------------------------------------------- */
/* ADDITIONAL DEFINITIONS */

View File

@ -50,12 +50,6 @@
# define in_addr_t unsigned long
#endif
#if defined(USE_UNIX_SOCKETS) && defined(WINAPI_FAMILY) && \
(WINAPI_FAMILY == WINAPI_FAMILY_APP)
/* Required for sockaddr_un type */
# include <afunix.h>
#endif
#include <stddef.h>
#include "curl_addrinfo.h"

View File

@ -824,4 +824,20 @@ int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf,
#define ENABLE_QUIC
#endif
#if defined(USE_UNIX_SOCKETS) && defined(WIN32)
# if defined(__MINGW32__) && !defined(LUP_SECURE)
typedef u_short ADDRESS_FAMILY; /* Classic mingw, 11y+ old mingw-w64 */
# endif
# if !defined(UNIX_PATH_MAX)
/* Replicating logic present in afunix.h
(distributed with newer Windows 10 SDK versions only) */
# define UNIX_PATH_MAX 108
/* !checksrc! disable TYPEDEFSTRUCT 1 */
typedef struct sockaddr_un {
ADDRESS_FAMILY sun_family;
char sun_path[UNIX_PATH_MAX];
} SOCKADDR_UN, *PSOCKADDR_UN;
# endif
#endif
#endif /* HEADER_CURL_SETUP_H */

View File

@ -2072,9 +2072,9 @@ int main(int argc, char *argv[])
strncpy(me.sau.sun_path, unix_socket, sizeof(me.sau.sun_path) - 1);
rc = bind(sock, &me.sa, sizeof(me.sau));
if(0 != rc && errno == EADDRINUSE) {
struct stat statbuf;
struct_stat statbuf;
/* socket already exists. Perhaps it is stale? */
int unixfd = socket(AF_UNIX, SOCK_STREAM, 0);
curl_socket_t unixfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(CURL_SOCKET_BAD == unixfd) {
error = SOCKERRNO;
logmsg("Error binding socket, failed to create socket at %s: (%d) %s",
@ -2084,15 +2084,19 @@ int main(int argc, char *argv[])
/* check whether the server is alive */
rc = connect(unixfd, &me.sa, sizeof(me.sau));
error = errno;
close(unixfd);
sclose(unixfd);
if(ECONNREFUSED != error) {
logmsg("Error binding socket, failed to connect to %s: (%d) %s",
unix_socket, error, strerror(error));
goto sws_cleanup;
}
/* socket server is not alive, now check if it was actually a socket.
* Systems which have Unix sockets will also have lstat */
/* socket server is not alive, now check if it was actually a socket. */
#ifdef WIN32
/* Windows does not have lstat function. */
rc = curlx_win32_stat(unix_socket, &statbuf);
#else
rc = lstat(unix_socket, &statbuf);
#endif
if(0 != rc) {
logmsg("Error binding socket, failed to stat %s: (%d) %s",
unix_socket, errno, strerror(errno));