]> git.feebdaed.xyz Git - 0xmirror/curl.git/commitdiff
cf-socket: enable Win10 `TCP_KEEP*` options with old SDKs
authorViktor Szakats <commit@vsz.me>
Tue, 16 Dec 2025 15:41:05 +0000 (16:41 +0100)
committerViktor Szakats <commit@vsz.me>
Tue, 16 Dec 2025 16:22:50 +0000 (17:22 +0100)
Define `TCP_KEEP*` macros if they are missing in Windows builds.
To allow using these runtime `setsockopt()` options regardless of
build-time SDK version, when running on Windows 10.0.16299+.

In practice in enables them for builds using mingw-w64 <12, and
MSVC with Windows SDK <10.

Before this patch these runtime options required building curl with
a recent toolchain.

Follow-up to f0de14168a4d1c3a4ed43a04af92c5755c84b9fc #19559

Closes #19999

lib/cf-socket.c

index 3e22c4ad26b7e436dfc1ec256de9526096d57723..4e122093cdf9589af8c92775c1dd737f04b07b54 100644 (file)
@@ -151,14 +151,26 @@ static void tcpkeepalive(struct Curl_cfilter *cf,
   }
   else {
 #ifdef USE_WINSOCK
-/* Offered by mingw-w64 v12+. MS SDK ~10+/~VS2017+. */
-#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
     /* Windows 10, version 1709 (10.0.16299) and later versions can use
        setsockopt() TCP_KEEP*. Older versions return with failure. */
     if(curlx_verify_windows_version(10, 0, 16299, PLATFORM_WINNT,
                                     VERSION_GREATER_THAN_EQUAL)) {
       CURL_TRC_CF(data, cf, "Set TCP_KEEP* on fd=%" FMT_SOCKET_T, sockfd);
       optval = curlx_sltosi(data->set.tcp_keepidle);
+/* Offered by mingw-w64 v12+. MS SDK 6.0A+. */
+#ifndef TCP_KEEPALIVE
+#define TCP_KEEPALIVE 3
+#endif
+/* Offered by mingw-w64 v12+. MS SDK ~10+/~VS2017+. */
+#ifndef TCP_KEEPCNT
+#define TCP_KEEPCNT 16
+#endif
+#ifndef TCP_KEEPIDLE
+#define TCP_KEEPIDLE TCP_KEEPALIVE
+#endif
+#ifndef TCP_KEEPINTVL
+#define TCP_KEEPINTVL 17
+#endif
       if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE,
                     (const char *)&optval, sizeof(optval)) < 0) {
         CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPIDLE on fd "
@@ -177,9 +189,7 @@ static void tcpkeepalive(struct Curl_cfilter *cf,
                     "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
       }
     }
-    else
-#endif /* TCP_KEEPIDLE && TCP_KEEPINTVL && TCP_KEEPCNT */
-    {
+    else {
 /* Offered by mingw-w64 and MS SDK. Latter only when targeting Win7+. */
 #ifndef SIO_KEEPALIVE_VALS
 #define SIO_KEEPALIVE_VALS  _WSAIOW(IOC_VENDOR, 4)