From 28dae2a8ef6ea44508f8ad5b202efd22fe378671 Mon Sep 17 00:00:00 2001 From: alex-Symbroson Date: Mon, 27 Jan 2025 17:33:24 +0100 Subject: [PATCH 1/2] MSYS mingw-w64-ucrt-x86_64 support --- src/CMakeLists.txt | 5 +++++ src/main/c/Connection.cpp | 10 +++++----- src/main/c/Server.cpp | 16 ++++++++-------- src/main/c/StringUtil.cpp | 8 ++++---- src/main/c/seasocks/Server.h | 2 ++ src/main/c/seasocks/win32/wepoll.c | 8 ++++++++ src/main/c/seasocks/win32/win_getopt.h | 4 +++- 7 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a5d3c086..315a8f91 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,6 +4,11 @@ add_subdirectory(main/web) macro(add_seasocks_executable _name) add_executable(${_name} ${ARGN}) target_link_libraries(${_name} PRIVATE seasocks) + + if(MINGW) + message(STATUS "Building in MinGW environment, linking with ws2_32") + target_link_libraries(${_name} PRIVATE ws2_32) + endif() endmacro() if (SEASOCKS_EXAMPLE_APP) diff --git a/src/main/c/Connection.cpp b/src/main/c/Connection.cpp index 21f03785..c4b91c1b 100644 --- a/src/main/c/Connection.cpp +++ b/src/main/c/Connection.cpp @@ -278,7 +278,7 @@ void Connection::closeInternal() { // It only actually only calls shutdown on the socket, // leaving the close of the FD and the cleanup until the destructor runs. _server.checkThread(); - if (_fd != -1 && !_shutdown && ::shutdown(_fd, SHUT_RDWR) == -1) { + if (_fd != (NativeSocketType) -1 && !_shutdown && ::shutdown(_fd, SHUT_RDWR) == -1) { LS_WARNING(_logger, "Unable to shutdown socket : " << getLastError()); } _shutdown = true; @@ -296,7 +296,7 @@ void Connection::finalise() { _webSocketHandler->onDisconnect(this); _webSocketHandler.reset(); } - if (_fd != -1) { + if (_fd != (NativeSocketType) -1) { _server.remove(this); LS_DEBUG(_logger, "Closing socket"); #ifndef _WIN32 @@ -309,7 +309,7 @@ void Connection::finalise() { } ssize_t Connection::safeSend(const void* data, size_t size) { - if (_fd == -1 || _hadSendError || _shutdown) { + if ((_fd == (NativeSocketType) -1) || _hadSendError || _shutdown) { // Ignore further writes to the socket, it's already closed or has been shutdown return -1; } @@ -439,7 +439,7 @@ bool Connection::flush() { } bool Connection::closed() const { - return _fd == -1 || _shutdown; + return _fd == (NativeSocketType) -1 || _shutdown; } void Connection::handleNewData() { @@ -1302,7 +1302,7 @@ void Connection::bufferResponseAndCommonHeaders(ResponseCode code) { } void Connection::setLinger() { - if (_fd == -1) { + if (_fd == (NativeSocketType) -1) { return; } const int secondsToLinger = 1; diff --git a/src/main/c/Server.cpp b/src/main/c/Server.cpp index ff0381a7..97117a49 100644 --- a/src/main/c/Server.cpp +++ b/src/main/c/Server.cpp @@ -170,8 +170,8 @@ Server::Server(std::shared_ptr logger) return; } - epoll_event eventWake = {EPOLLIN, {&_eventFd}}; #ifndef _WIN32 + epoll_event eventWake = {EPOLLIN, {&_eventFd}}; if (epoll_ctl(_epollFd, EPOLL_CTL_ADD, _eventFd, &eventWake) == -1) { LS_ERROR(_logger, "Unable to add wake socket to epoll: " << getLastError()); return; @@ -201,7 +201,7 @@ Server::~Server() { void Server::shutdown() { // Stop listening to any further incoming connections. - if (_listenSock != -1) { + if (_listenSock != (NativeSocketType) -1) { #ifdef _WIN32 ::closesocket(_listenSock); #else @@ -309,7 +309,7 @@ bool Server::startListening(uint32_t ipInHostOrder, int port) { return false; } _listenSock = socket(AF_INET, SOCK_STREAM, 0); - if (_listenSock == -1) { + if (_listenSock == (NativeSocketType) -1) { LS_ERROR(_logger, "Unable to create listen socket: " << getLastError()); return false; } @@ -346,7 +346,7 @@ bool Server::startListeningUnix(const char* socketPath) { struct sockaddr_un sock; _listenSock = socket(AF_UNIX, SOCK_STREAM, 0); - if (_listenSock == -1) { + if (_listenSock == (NativeSocketType) -1) { LS_ERROR(_logger, "Unable to create unix listen socket: " << getLastError()); return false; } @@ -452,7 +452,7 @@ void Server::checkAndDispatchEpoll(int epollMillis) { } else if (events[i].data.ptr == &_eventFd) { // This is never true in windows #ifdef _WIN32 - throw std::exception("Win32 uses a seperate, native wake-up HANDLE as an event"); + throw std::runtime_error("Win32 uses a seperate, native wake-up HANDLE as an event"); #else if (events[i].events & ~EPOLLIN) { LS_SEVERE(_logger, "Got unexpected event on management pipe (" @@ -500,7 +500,7 @@ bool Server::serve(const char* staticPath, int port) { } bool Server::loop() { - if (_listenSock == -1) { + if (_listenSock == (NativeSocketType) -1) { LS_ERROR(_logger, "Server not initialised"); return false; } @@ -528,7 +528,7 @@ Server::PollResult Server::poll(int millis) { LS_ERROR(_logger, "poll() called from the wrong thread"); return PollResult::Error; } - if (_listenSock == -1) { + if (_listenSock == (NativeSocketType) -1) { LS_ERROR(_logger, "Server not initialised"); return PollResult::Error; } @@ -581,7 +581,7 @@ void Server::handleAccept() { NativeSocketType fd = ::accept(_listenSock, reinterpret_cast(&address), &addrLen); - if (fd == -1) { + if (fd == (NativeSocketType) -1) { LS_ERROR(_logger, "Unable to accept: " << getLastError()); return; } diff --git a/src/main/c/StringUtil.cpp b/src/main/c/StringUtil.cpp index 6b0f7e10..867a2e77 100644 --- a/src/main/c/StringUtil.cpp +++ b/src/main/c/StringUtil.cpp @@ -95,10 +95,10 @@ std::string formatAddress(const sockaddr_in& address) { std::snprintf(ipBuffer, bufferSize, "%d.%d.%d.%d:%d", - (address.sin_addr.s_addr >> 0) & 0xff, - (address.sin_addr.s_addr >> 8) & 0xff, - (address.sin_addr.s_addr >> 16) & 0xff, - (address.sin_addr.s_addr >> 24) & 0xff, + (int) (address.sin_addr.s_addr >> 0) & 0xff, + (int) (address.sin_addr.s_addr >> 8) & 0xff, + (int) (address.sin_addr.s_addr >> 16) & 0xff, + (int) (address.sin_addr.s_addr >> 24) & 0xff, htons(address.sin_port)); return ipBuffer; } diff --git a/src/main/c/seasocks/Server.h b/src/main/c/seasocks/Server.h index 6a572749..094241b5 100644 --- a/src/main/c/seasocks/Server.h +++ b/src/main/c/seasocks/Server.h @@ -42,7 +42,9 @@ #ifdef _WIN32 #include "seasocks/win32/winsock_includes.h" #define ioctl ioctlsocket +#ifndef _INC_TYPES using pid_t = DWORD; +#endif #include // windows has unix sockets -- who'da guessed? #endif diff --git a/src/main/c/seasocks/win32/wepoll.c b/src/main/c/seasocks/win32/wepoll.c index 7620a6e9..736e5bb6 100644 --- a/src/main/c/seasocks/win32/wepoll.c +++ b/src/main/c/seasocks/win32/wepoll.c @@ -895,6 +895,10 @@ int nt_global_init(void) { ntdll = GetModuleHandleW(L"ntdll.dll"); if (ntdll == NULL) return -1; +#if _WIN32 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +#endif #define X(return_type, attributes, name, parameters) \ fn_ptr = GetProcAddress(ntdll, #name); \ @@ -902,6 +906,10 @@ int nt_global_init(void) { return -1; \ name = (return_type(attributes*) parameters)(nt__fn_ptr_cast_t) fn_ptr; NT_NTDLL_IMPORT_LIST(X) + +#if _WIN32 +#pragma GCC diagnostic pop +#endif #undef X return 0; diff --git a/src/main/c/seasocks/win32/win_getopt.h b/src/main/c/seasocks/win32/win_getopt.h index 3bfb11f4..cf1d5b98 100644 --- a/src/main/c/seasocks/win32/win_getopt.h +++ b/src/main/c/seasocks/win32/win_getopt.h @@ -57,7 +57,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#if defined(_MSC_VER) #pragma warning(disable : 4996) +#endif #define __GETOPT_H__ @@ -113,7 +115,7 @@ static inline char* optarg = nullptr; /* argument associated with option */ extern char __declspec(dllimport) * __progname; #endif -#ifdef __CYGWIN__ +#if defined(__CYGWIN__) || defined(__MINGW64__) static char EMSG[] = ""; #else #define EMSG "" From df838c9a61fde58a520f58b5dfa977e8d94a7013 Mon Sep 17 00:00:00 2001 From: alex-Symbroson Date: Fri, 25 Apr 2025 22:30:49 +0200 Subject: [PATCH 2/2] fix for missing TCP_KEEPALIVE define --- .gitignore | 9 +++++++++ src/main/c/seasocks/win32/winsock_includes.h | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/.gitignore b/.gitignore index d498b199..65e14015 100644 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,12 @@ ipch/ # TFS 2012 Local Workspace $tf/ +# cmake +*.cmake +CMakeFiles/ +CMakeCache.txt +Makefile + +*.tcl +internal/Config.h +src/main/web/Embedded.cpp diff --git a/src/main/c/seasocks/win32/winsock_includes.h b/src/main/c/seasocks/win32/winsock_includes.h index d144d005..aa281908 100644 --- a/src/main/c/seasocks/win32/winsock_includes.h +++ b/src/main/c/seasocks/win32/winsock_includes.h @@ -3,4 +3,11 @@ #ifdef _WIN32 #define NOMINMAX 1 #include + +#ifndef TCP_KEEPALIVE +#define TCP_KEEPALIVE 3 +#define TCP_KEEPIDLE TCP_KEEPALIVE +#define TCP_KEEPCNT 16 +#define TCP_KEEPINTVL 17 +#endif #endif