From 627e32fa12fe94dbbd4302259a979399f2a9a8db Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Tue, 16 Dec 2025 16:41:05 +0100 Subject: [PATCH] cf-socket: enable Win10 `TCP_KEEP*` options with old SDKs 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 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/cf-socket.c b/lib/cf-socket.c index 3e22c4ad26..4e122093cd 100644 --- a/lib/cf-socket.c +++ b/lib/cf-socket.c @@ -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)