]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tests/server: fix to check against winsock2 error codes on Windows
authorViktor Szakats <commit@vsz.me>
Thu, 6 Mar 2025 21:13:39 +0000 (22:13 +0100)
committerViktor Szakats <commit@vsz.me>
Sat, 8 Mar 2025 00:53:48 +0000 (01:53 +0100)
Windows's winsock2 returns socket errors via `WSAGetLastError()` and
not via `errno` like most systems out there. This was covered by
switching to the `SOCKERRNO` curl macro earlier. But, on Windows the
returned socket error codes have different values than the standard
POSIX errno values. Existing code was using the POSIX values for all
these checks. Meaning they never actually matched on Windows.

This patch defines a set of `SOCKERRNO` constants that map to the
correct socket error values for Windows and other platforms.

The reverse issue exists in core curl code, which redefines POSIX errno
values to winsock2 ones, breaking non-socket uses on Windows.

Cherry-picked from #15000
Follow-up to adcfd4fb3e9be1de0e506728066bea2aaa53c394 #16553
Bug: https://github.com/curl/curl/pull/16553#issuecomment-2704679377

Closes #16612

tests/server/mqttd.c
tests/server/sockfilt.c
tests/server/socksd.c
tests/server/sws.c
tests/server/util.c
tests/server/util.h

index fb21f704a455641c55b0e7c6cc7558d636e60cf0..11355f95e141cef4e9c24efecb493c466cf84847 100644 (file)
@@ -704,7 +704,7 @@ static bool mqttd_incoming(curl_socket_t listenfd)
         logmsg("signalled to die, exiting...");
         return FALSE;
       }
-    } while((rc == -1) && ((error = SOCKERRNO) == EINTR));
+    } while((rc == -1) && ((error = SOCKERRNO) == SOCKEINTR));
 
     if(rc < 0) {
       logmsg("select() failed with error (%d) %s",
index 7f4784cb1092f6cafa127ad3691c23e2f263826c..a0ef93bbd864bcd34f7cc49805956810e4398954 100644 (file)
@@ -1075,7 +1075,7 @@ static bool juggle(curl_socket_t *sockfdp,
       return FALSE;
     }
 
-  } while((rc == -1) && ((error = SOCKERRNO) == EINTR));
+  } while((rc == -1) && ((error = SOCKERRNO) == SOCKEINTR));
 
   if(rc < 0) {
     logmsg("select() failed with error (%d) %s",
index 7afdea8d783e6602310c96994c3c7d62099fa060..4d45a44b5528f227770a4d3060600c99a02d2164 100644 (file)
@@ -699,7 +699,7 @@ static bool socksd_incoming(curl_socket_t listenfd)
         logmsg("signalled to die, exiting...");
         return FALSE;
       }
-    } while((rc == -1) && ((error = SOCKERRNO) == EINTR));
+    } while((rc == -1) && ((error = SOCKERRNO) == SOCKEINTR));
 
     if(rc < 0) {
       logmsg("select() failed with error (%d) %s",
index 7e29d1ca521e40b45a68803e5c82ea1a286d21b4..9c2821884ff8a0b9a390bf0b664c7e71e9a1984f 100644 (file)
@@ -861,7 +861,7 @@ static int sws_get_request(curl_socket_t sock, struct sws_httprequest *req)
         }
 
         if((got == -1) && ((SOCKERRNO == EAGAIN) ||
-                           (SOCKERRNO == EWOULDBLOCK))) {
+                           (SOCKERRNO == SOCKEWOULDBLOCK))) {
           int rc;
           fd_set input;
           fd_set output;
@@ -883,7 +883,7 @@ static int sws_get_request(curl_socket_t sock, struct sws_httprequest *req)
           do {
             logmsg("Wait until readable");
             rc = select((int)sock + 1, &input, &output, NULL, &timeout);
-          } while(rc < 0 && SOCKERRNO == EINTR && !got_exit_signal);
+          } while(rc < 0 && SOCKERRNO == SOCKEINTR && !got_exit_signal);
           logmsg("readable %d", rc);
           if(rc)
             got = 1;
@@ -926,7 +926,7 @@ static int sws_get_request(curl_socket_t sock, struct sws_httprequest *req)
     }
     else if(got < 0) {
       int error = SOCKERRNO;
-      if(EAGAIN == error || EWOULDBLOCK == error) {
+      if(EAGAIN == error || SOCKEWOULDBLOCK == error) {
         /* nothing to read at the moment */
         return 0;
       }
@@ -1149,7 +1149,7 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req)
 retry:
     written = swrite(sock, buffer, num);
     if(written < 0) {
-      if((EWOULDBLOCK == SOCKERRNO) || (EAGAIN == SOCKERRNO)) {
+      if((SOCKEWOULDBLOCK == SOCKERRNO) || (EAGAIN == SOCKERRNO)) {
         wait_ms(10);
         goto retry;
       }
@@ -1342,7 +1342,7 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port)
 
   if(rc) {
     error = SOCKERRNO;
-    if((error == EINPROGRESS) || (error == EWOULDBLOCK)) {
+    if((error == SOCKEINPROGRESS) || (error == SOCKEWOULDBLOCK)) {
       fd_set output;
       struct timeval timeout = {0};
       timeout.tv_sec = 1; /* 1000 ms */
@@ -1358,16 +1358,16 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port)
 #endif
       while(1) {
         rc = select((int)serverfd + 1, NULL, &output, NULL, &timeout);
-        if(rc < 0 && SOCKERRNO != EINTR)
+        if(rc < 0 && SOCKERRNO != SOCKEINTR)
           goto error;
         else if(rc > 0) {
           curl_socklen_t errSize = sizeof(error);
           if(0 != getsockopt(serverfd, SOL_SOCKET, SO_ERROR,
                              (void *)&error, &errSize))
             error = SOCKERRNO;
-          if((0 == error) || (EISCONN == error))
+          if((0 == error) || (SOCKEISCONN == error))
             goto success;
-          else if((error != EINPROGRESS) && (error != EWOULDBLOCK))
+          else if((error != SOCKEINPROGRESS) && (error != SOCKEWOULDBLOCK))
             goto error;
         }
         else if(!rc) {
@@ -1559,7 +1559,7 @@ static void http_connect(curl_socket_t *infdp,
 
     do {
       rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
-    } while(rc < 0 && SOCKERRNO == EINTR && !got_exit_signal);
+    } while(rc < 0 && SOCKERRNO == SOCKEINTR && !got_exit_signal);
 
     if(got_exit_signal)
       break;
@@ -1871,7 +1871,7 @@ static curl_socket_t accept_connection(curl_socket_t sock)
 
   if(CURL_SOCKET_BAD == msgsock) {
     error = SOCKERRNO;
-    if(EAGAIN == error || EWOULDBLOCK == error) {
+    if(EAGAIN == error || SOCKEWOULDBLOCK == error) {
       /* nothing to accept */
       return 0;
     }
@@ -2380,7 +2380,7 @@ int main(int argc, char *argv[])
 
     do {
       rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
-    } while(rc < 0 && SOCKERRNO == EINTR && !got_exit_signal);
+    } while(rc < 0 && SOCKERRNO == SOCKEINTR && !got_exit_signal);
 
     if(got_exit_signal)
       goto sws_cleanup;
index dc28ef1a0ce5690e702d18a2d474e8865536d784..8f2da0aefbe46cbb9652816413134003f06faced 100644 (file)
@@ -299,7 +299,7 @@ int wait_ms(int timeout_ms)
       if(r != -1)
         break;
       error = errno;
-      if(error && (error != EINTR))
+      if(error && (error != SOCKEINTR))
         break;
       pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
       if(pending_ms <= 0)
@@ -866,7 +866,7 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
   }
   strcpy(sau->sun_path, unix_socket);
   rc = bind(sock, (struct sockaddr*)sau, sizeof(struct sockaddr_un));
-  if(0 != rc && SOCKERRNO == EADDRINUSE) {
+  if(0 != rc && SOCKERRNO == SOCKEADDRINUSE) {
     struct_stat statbuf;
     /* socket already exists. Perhaps it is stale? */
     curl_socket_t unixfd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -879,7 +879,7 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
     rc = connect(unixfd, (struct sockaddr*)sau, sizeof(struct sockaddr_un));
     error = SOCKERRNO;
     sclose(unixfd);
-    if(0 != rc && ECONNREFUSED != error) {
+    if(0 != rc && SOCKECONNREFUSED != error) {
       logmsg("Failed to connect to %s (%d) %s",
              unix_socket, error, sstrerror(error));
       return rc;
index aed144d6e384c371734e79cffffbaad2fc032569..d2a00e663214cf50a1dea6d84040c77bf58a81ff 100644 (file)
 #include "server_setup.h"
 
 #ifdef USE_WINSOCK
-/* errno.h values */
-#undef  EINTR
-#define EINTR    4
-#undef  EAGAIN
-#define EAGAIN  11
-#undef  ENOMEM
-#define ENOMEM  12
-#undef  EINVAL
-#define EINVAL  22
-#undef  ERANGE
-#define ERANGE  34
+#define SOCKEADDRINUSE   WSAEADDRINUSE
+#define SOCKECONNREFUSED WSAECONNREFUSED
+#define SOCKEINPROGRESS  WSAEINPROGRESS
+#define SOCKEINTR        WSAEINTR
+#define SOCKEINVAL       WSAEINVAL
+#define SOCKEISCONN      WSAEISCONN
+#define SOCKEWOULDBLOCK  WSAEWOULDBLOCK
+#else
+#define SOCKEADDRINUSE   EADDRINUSE
+#define SOCKECONNREFUSED ECONNREFUSED
+#define SOCKEINPROGRESS  EINPROGRESS
+#define SOCKEINTR        EINTR
+#define SOCKEINVAL       EINVAL
+#define SOCKEISCONN      EISCONN
+#define SOCKEWOULDBLOCK  EWOULDBLOCK
 #endif
 
 enum {