From 2feff0a1e9f30d37da142baa12cb4a7965d7bd5d Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:03:02 +0200 Subject: [PATCH 1/4] Add Windows socket support in server.h Added Windows-specific socket includes and pollfd definition. --- server.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/server.h b/server.h index 5acf664..646b6f7 100644 --- a/server.h +++ b/server.h @@ -4,9 +4,21 @@ #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200809L -#include -#include -#include +#ifdef _WIN32 + #include + #include + + // Add definition for pollfd to account for Windows not defining + struct pollfd { + SOCKET fd; // socket instead of int + short events; // same values as POSIX (POLLIN, POLLOUT, etc.) + short revents; + }; +#else + #include + #include + #include +#endif #pragma RcB2 DEP "server.c" From 608d58e2fe8e94b31da133ea47c83f4beb8c2bf8 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:03:44 +0200 Subject: [PATCH 2/4] Update sockssrv.c --- sockssrv.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/sockssrv.c b/sockssrv.c index d59fc27..6a5a193 100644 --- a/sockssrv.c +++ b/sockssrv.c @@ -22,6 +22,10 @@ */ #define _GNU_SOURCE +#ifdef _WIN32 + #include + // ^^ Ugly hack to force it to compile +#endif #include #define _POSIX_C_SOURCE 200809L #include @@ -29,8 +33,13 @@ #include #include #include -#include -#include +#ifdef _WIN32 + #include "wsa2unix.h" + #include "dprintf.c" +#else + #include + #include +#endif #include #include #include "server.h" @@ -276,7 +285,11 @@ static void copyloop(int fd1, int fd2) { /* inactive connections are reaped after 15 min to free resources. usually programs send keep-alive packets so this should only happen when a connection is really unused. */ - switch(poll(fds, 2, 60*15*1000)) { + #ifdef _WIN32 + switch(WSAPoll(fds, 2, 60*15*1000)) { + #else + switch(poll(fds, 2, 60*15*1000)) { + #endif case 0: return; case -1: @@ -471,7 +484,11 @@ int main(int argc, char** argv) { dprintf(2, "error: -1/-w options must be used together with user/pass\n"); return 1; } - signal(SIGPIPE, SIG_IGN); + #ifdef _WIN32 + // This is not needed because Windows doesn't kill processes on pipe failure + #else + signal(SIGPIPE, SIG_IGN); + #endif struct server s; sblist *threads = sblist_new(sizeof (struct thread*), 8); if(server_setup(&s, listenip, port)) { From 7587af24e9d8a23a848cb48e6e38d4a5a172bf06 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:04:10 +0200 Subject: [PATCH 3/4] Implement dprintf --- dprintf.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 dprintf.c diff --git a/dprintf.c b/dprintf.c new file mode 100644 index 0000000..96e6ca6 --- /dev/null +++ b/dprintf.c @@ -0,0 +1,36 @@ +#include +#include +#include + +int dprintf(int fd, const char *fmt, ...) +{ + char stack_buffer[256]; + char *buf = stack_buffer; + int len = sizeof(stack_buffer); + va_list ap; + int ret = -1; + + va_start(ap, fmt); + + // Method 1 try to just print it to stack if possible + ret = vsnprintf(buf, len, fmt, ap); + + if (ret >= len) { + // Method 2 dynamically allocate buffer + len = ret + 1; + buf = malloc(len); + if (buf) { + va_end(ap); + va_start(ap, fmt); + ret = vsnprintf(buf, len, fmt, ap); + } else { + ret = -1; // Malloc failed due to too little memory??? Couldn't be me + } + } + if (ret >= 0 && (write(fd, buf, ret) != ret)) { + ret = -1; + } + if (buf != stack_buffer) free(buf); + va_end(ap); + return ret; +} From 38004cb1557ce9968bde80bc95cbdc61e662d89c Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:04:47 +0200 Subject: [PATCH 4/4] Create wsa2unix.h --- wsa2unix.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 wsa2unix.h diff --git a/wsa2unix.h b/wsa2unix.h new file mode 100644 index 0000000..2be908b --- /dev/null +++ b/wsa2unix.h @@ -0,0 +1,64 @@ +/* Define POLLIN values manually if not defined by some compiler */ +#ifndef POLLIN + #define POLLIN 0x001 + #define POLLPRI 0x002 + #define POLLOUT 0x004 + #define POLLERR 0x008 + #define POLLHUP 0x010 + #define POLLNVAL 0x020 + #define POLLRDNORM 0x100 + #define POLLRDBAND 0x200 + #define POLLWRNORM 0x010 + #define POLLWRBAND 0x400 +#endif + +/* Operation would block */ +#define EWOULDBLOCK WSAEWOULDBLOCK +#define EAGAIN WSAEWOULDBLOCK + +/* Operation now in progress */ +#define EINPROGRESS WSAEINPROGRESS +#define EALREADY WSAEALREADY + +/* Socket errors */ +#define ENOTSOCK WSAENOTSOCK +#define EDESTADDRREQ WSAEDESTADDRREQ +#define EMSGSIZE WSAEMSGSIZE +#define EPROTOTYPE WSAEPROTOTYPE +#define ENOPROTOOPT WSAENOPROTOOPT +#define EPROTONOSUPPORT WSAEPROTONOSUPPORT +#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT +#define EOPNOTSUPP WSAEOPNOTSUPP +#define EPFNOSUPPORT WSAEPFNOSUPPORT +#define EAFNOSUPPORT WSAEAFNOSUPPORT + +/* Address errors */ +#define EADDRINUSE WSAEADDRINUSE +#define EADDRNOTAVAIL WSAEADDRNOTAVAIL + +/* Network subsystem errors */ +#define ENETDOWN WSAENETDOWN +#define ENETUNREACH WSAENETUNREACH +#define ENETRESET WSAENETRESET + +/* Connection errors */ +#define ECONNABORTED WSAECONNABORTED +#define ECONNRESET WSAECONNRESET +#define ENOBUFS WSAENOBUFS +#define EISCONN WSAEISCONN +#define ENOTCONN WSAENOTCONN +#define ESHUTDOWN WSAESHUTDOWN +#define ETIMEDOUT WSAETIMEDOUT +#define ECONNREFUSED WSAECONNREFUSED + +/* Host errors */ +#define EHOSTUNREACH WSAEHOSTUNREACH + +/* Some misc stuff */ +#define EINTR WSAEINTR +#define EFAULT WSAEFAULT +#define EINVAL WSAEINVAL +#define EMFILE WSAEMFILE +#define EACCES WSAEACCES +#define EPERM WSAEACCES +#define ENOMEM WSA_NOT_ENOUGH_MEMORY