From d9749fcc5ab4009adc0e6a071246aa7c903769a5 Mon Sep 17 00:00:00 2001 From: PgSocks Date: Thu, 9 May 2024 09:38:02 -0500 Subject: [PATCH] Use Winsock for Windows --- src/connection.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/connection.c b/src/connection.c index 10f81a7..ba5fef8 100644 --- a/src/connection.c +++ b/src/connection.c @@ -4,9 +4,26 @@ #include "connection.h" #include + +#ifdef _WIN32 +bool StartupWSA() { + static bool wsastarted = false; + if (!wsastarted) { + WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD(2, 2); + if (WSAStartup(wVersionRequested, &wsaData) == 0) { + wsastarted = true; + } + } + return wsastarted; +} +#else #include #include #include +#endif #include #include #include @@ -106,6 +123,10 @@ connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uin enum connection_error connection_connect(struct Connection* conn) { +#ifdef _WIN32 + StartupWSA(); +#endif + // Request a UDP socket conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0); @@ -114,8 +135,13 @@ connection_connect(struct Connection* conn) { return CONNECTION_LOCAL_BIND; // Make the socket non-blocking +#ifdef _WIN32 + static unsigned long sock_opts = 1; + ioctlsocket(conn->sockfd, FIONBIO, &sock_opts); //Non-Blocking +#else int flags = fcntl(conn->sockfd, F_GETFL); fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK); +#endif // File descriptors are numbers that count up sequentially, // so save the last one as the greatest file descriptor.