From: Viktor Szakats Date: Thu, 6 Mar 2025 21:13:39 +0000 (+0100) Subject: tests/server: fix to check against winsock2 error codes on Windows X-Git-Tag: curl-8_13_0~216 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=abf80aae384319ef9b19ffbd0d69a1fbe7421f1f;p=thirdparty%2Fcurl.git tests/server: fix to check against winsock2 error codes on Windows 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 --- diff --git a/tests/server/mqttd.c b/tests/server/mqttd.c index fb21f704a4..11355f95e1 100644 --- a/tests/server/mqttd.c +++ b/tests/server/mqttd.c @@ -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", diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index 7f4784cb10..a0ef93bbd8 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -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", diff --git a/tests/server/socksd.c b/tests/server/socksd.c index 7afdea8d78..4d45a44b55 100644 --- a/tests/server/socksd.c +++ b/tests/server/socksd.c @@ -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", diff --git a/tests/server/sws.c b/tests/server/sws.c index 7e29d1ca52..9c2821884f 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -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; diff --git a/tests/server/util.c b/tests/server/util.c index dc28ef1a0c..8f2da0aefb 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -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; diff --git a/tests/server/util.h b/tests/server/util.h index aed144d6e3..d2a00e6632 100644 --- a/tests/server/util.h +++ b/tests/server/util.h @@ -26,17 +26,21 @@ #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 {