From 08091d02e525158f4831fd87c4c9231d87d4b88b Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Wed, 9 Apr 2025 10:51:55 -0300 Subject: [PATCH] allow other stack than WinSock in Windows --- mongoose.h | 41 ++++++++++++++++++++++++++++++++--------- src/arch_win32.h | 38 ++++++++++++++++++++++++++++++-------- src/config.h | 3 ++- 3 files changed, 64 insertions(+), 18 deletions(-) diff --git a/mongoose.h b/mongoose.h index bfb5c9db..f8f44725 100644 --- a/mongoose.h +++ b/mongoose.h @@ -424,6 +424,18 @@ static inline int mg_mkdir(const char *path, mode_t mode) { #if MG_ARCH == MG_ARCH_WIN32 +// Avoid name clashing; (macro expansion producing 'defined' has undefined +// behaviour). See config.h for user options +#ifndef MG_ENABLE_WINSOCK +#if (!defined(MG_ENABLE_TCPIP) || !MG_ENABLE_TCPIP) && \ + (!defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP) && \ + (!defined(MG_ENABLE_FREERTOS_TCP) || !MG_ENABLE_FREERTOS_TCP) +#define MG_ENABLE_WINSOCK 1 +#else +#define MG_ENABLE_WINSOCK 0 +#endif +#endif + #ifndef _CRT_RAND_S #define _CRT_RAND_S #endif @@ -469,17 +481,19 @@ typedef enum { false = 0, true = 1 } bool; #else #include #include +#if MG_ENABLE_WINSOCK #include #endif +#endif #include #include -#include // For mg_random() #if defined(_MSC_VER) && _MSC_VER < 1700 #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x400 // Let vc98 pick up wincrypt.h +#include // and fix missing macros #endif #include #pragma comment(lib, "advapi32.lib") @@ -495,25 +509,31 @@ typedef enum { false = 0, true = 1 } bool; #endif #endif -#define MG_INVALID_SOCKET INVALID_SOCKET -#define MG_SOCKET_TYPE SOCKET typedef unsigned long nfds_t; #if defined(_MSC_VER) +#if MG_ENABLE_WINSOCK #pragma comment(lib, "ws2_32.lib") +#endif #ifndef alloca #define alloca(a) _alloca(a) #endif #endif -#define poll(a, b, c) WSAPoll((a), (b), (c)) -#define closesocket(x) closesocket(x) -typedef int socklen_t; #define MG_DIRSEP '\\' #ifndef MG_PATH_MAX #define MG_PATH_MAX FILENAME_MAX #endif +#if MG_ENABLE_WINSOCK +#include + +#define MG_INVALID_SOCKET INVALID_SOCKET +#define MG_SOCKET_TYPE SOCKET +#define poll(a, b, c) WSAPoll((a), (b), (c)) +#define closesocket(x) closesocket(x) +typedef int socklen_t; + #ifndef SO_EXCLUSIVEADDRUSE #define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR)) #endif @@ -528,13 +548,15 @@ typedef int socklen_t; #define MG_SOCK_RESET(errcode) \ (((errcode) < 0) && (WSAGetLastError() == WSAECONNRESET)) +#endif // MG_ENABLE_WINSOCK + #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) -#define sleep(x) Sleep((x) * 1000) +#define sleep(x) Sleep((x) *1000) #define mkdir(a, b) _mkdir(a) #define timegm(x) _mkgmtime(x) #ifndef S_ISDIR -#define S_ISDIR(x) (((x) & _S_IFMT) == _S_IFDIR) +#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR) #endif #ifndef MG_ENABLE_DIRLIST @@ -774,7 +796,8 @@ struct timeval { #define MG_ENABLE_MD5 1 #endif -// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) +// Set MG_ENABLE_WINSOCK=0 for Win32 builds with other external IP stack not +// mentioned in arch_win32.h #ifndef MG_ENABLE_WINSOCK #define MG_ENABLE_WINSOCK 1 #endif diff --git a/src/arch_win32.h b/src/arch_win32.h index 78948d88..80968a7e 100644 --- a/src/arch_win32.h +++ b/src/arch_win32.h @@ -2,6 +2,18 @@ #if MG_ARCH == MG_ARCH_WIN32 +// Avoid name clashing; (macro expansion producing 'defined' has undefined +// behaviour). See config.h for user options +#ifndef MG_ENABLE_WINSOCK +#if (!defined(MG_ENABLE_TCPIP) || !MG_ENABLE_TCPIP) && \ + (!defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP) && \ + (!defined(MG_ENABLE_FREERTOS_TCP) || !MG_ENABLE_FREERTOS_TCP) +#define MG_ENABLE_WINSOCK 1 +#else +#define MG_ENABLE_WINSOCK 0 +#endif +#endif + #ifndef _CRT_RAND_S #define _CRT_RAND_S #endif @@ -47,17 +59,19 @@ typedef enum { false = 0, true = 1 } bool; #else #include #include +#if MG_ENABLE_WINSOCK #include #endif +#endif #include #include -#include // For mg_random() #if defined(_MSC_VER) && _MSC_VER < 1700 #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x400 // Let vc98 pick up wincrypt.h +#include // and fix missing macros #endif #include #pragma comment(lib, "advapi32.lib") @@ -73,25 +87,31 @@ typedef enum { false = 0, true = 1 } bool; #endif #endif -#define MG_INVALID_SOCKET INVALID_SOCKET -#define MG_SOCKET_TYPE SOCKET typedef unsigned long nfds_t; #if defined(_MSC_VER) +#if MG_ENABLE_WINSOCK #pragma comment(lib, "ws2_32.lib") +#endif #ifndef alloca #define alloca(a) _alloca(a) #endif #endif -#define poll(a, b, c) WSAPoll((a), (b), (c)) -#define closesocket(x) closesocket(x) -typedef int socklen_t; #define MG_DIRSEP '\\' #ifndef MG_PATH_MAX #define MG_PATH_MAX FILENAME_MAX #endif +#if MG_ENABLE_WINSOCK +#include + +#define MG_INVALID_SOCKET INVALID_SOCKET +#define MG_SOCKET_TYPE SOCKET +#define poll(a, b, c) WSAPoll((a), (b), (c)) +#define closesocket(x) closesocket(x) +typedef int socklen_t; + #ifndef SO_EXCLUSIVEADDRUSE #define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR)) #endif @@ -106,13 +126,15 @@ typedef int socklen_t; #define MG_SOCK_RESET(errcode) \ (((errcode) < 0) && (WSAGetLastError() == WSAECONNRESET)) +#endif // MG_ENABLE_WINSOCK + #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) -#define sleep(x) Sleep((x) * 1000) +#define sleep(x) Sleep((x) *1000) #define mkdir(a, b) _mkdir(a) #define timegm(x) _mkgmtime(x) #ifndef S_ISDIR -#define S_ISDIR(x) (((x) & _S_IFMT) == _S_IFDIR) +#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR) #endif #ifndef MG_ENABLE_DIRLIST diff --git a/src/config.h b/src/config.h index cb7b0150..2043ca44 100644 --- a/src/config.h +++ b/src/config.h @@ -56,7 +56,8 @@ #define MG_ENABLE_MD5 1 #endif -// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) +// Set MG_ENABLE_WINSOCK=0 for Win32 builds with other external IP stack not +// mentioned in arch_win32.h #ifndef MG_ENABLE_WINSOCK #define MG_ENABLE_WINSOCK 1 #endif