From: Viktor Szakats Date: Wed, 5 Mar 2025 17:06:37 +0000 (+0100) Subject: windows: do not use winsock2 `inet_ntop()`/`inet_pton()` X-Git-Tag: curl-8_13_0~238 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8537a5b0bcf4565551774c2b2375c49767e405a7;p=thirdparty%2Fcurl.git windows: do not use winsock2 `inet_ntop()`/`inet_pton()` Disable these winsock2 functions on Windows to use the curl wrappers and preserve `WSAGetLastError()` aka `SOCKERRNO` error codes. curl sources uses `inet_pton()` and `inet_ntop()` via its own `Curl_` prefixed wrappers. These wrappers promise to not overwrite `WSAGetLastError()` aka `SOCKERRNO` error codes when calling them. But, for Windows builds with these built-in winsock2 functions detected (meaning all supported Windows versions, except Windows CE), the wrappers were 1-to-1 mapped to the winsock2 functions, which broke this promise. https://github.com/curl/curl/blob/b06c12b7248592cf001e621d7cd8dc78a827212b/lib/inet_ntop.c#L188-L190 https://github.com/curl/curl/blob/b06c12b7248592cf001e621d7cd8dc78a827212b/lib/inet_pton.c#L66-L70 These promises are old (a1d598399146984c99baa46db148e87c75261033) and may not be valid anymore. In this case, the callers would have to be updated to use `SOCKERRNO` to retrieve any error, instead of using `errno` as they do now. https://learn.microsoft.com/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_ntop https://learn.microsoft.com/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_pton Closes #16577 --- diff --git a/CMake/win32-cache.cmake b/CMake/win32-cache.cmake index 75c8ce69ba..615db9e49c 100644 --- a/CMake/win32-cache.cmake +++ b/CMake/win32-cache.cmake @@ -118,6 +118,8 @@ set(HAVE_GLIBC_STRERROR_R 0) set(HAVE_GMTIME_R 0) set(HAVE_IFADDRS_H 0) set(HAVE_IF_NAMETOINDEX 0) +set(HAVE_INET_NTOP 0) +set(HAVE_INET_PTON 0) set(HAVE_IOCTLSOCKET 1) set(HAVE_IOCTLSOCKET_CAMEL 0) set(HAVE_IOCTLSOCKET_CAMEL_FIONBIO 0) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8243119b4..b3e4f62a72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1617,24 +1617,9 @@ if(WIN32) list(APPEND CURL_INCLUDES "winsock2.h") list(APPEND CURL_INCLUDES "ws2tcpip.h") - if(HAVE_WIN32_WINNT) - if(HAVE_WIN32_WINNT LESS 0x0501 AND NOT WINCE) - # Windows XP is required for freeaddrinfo, getaddrinfo - message(FATAL_ERROR "Building for Windows XP or newer is required.") - endif() - - # Pre-fill detection results based on target OS version - if(MINGW OR MSVC OR WINCE) - if(WINCE OR HAVE_WIN32_WINNT LESS 0x0600) - set(HAVE_INET_NTOP 0) - set(HAVE_INET_PTON 0) - else() # Windows Vista or newer - set(HAVE_INET_NTOP 1) - set(HAVE_INET_PTON 1) - endif() - unset(HAVE_INET_NTOP CACHE) - unset(HAVE_INET_PTON CACHE) - endif() + if(HAVE_WIN32_WINNT AND HAVE_WIN32_WINNT LESS 0x0501 AND NOT WINCE) + # Windows XP is required for freeaddrinfo, getaddrinfo + message(FATAL_ERROR "Building for Windows XP or newer is required.") endif() endif() @@ -1819,8 +1804,10 @@ endif() if(APPLE) check_function_exists("mach_absolute_time" HAVE_MACH_ABSOLUTE_TIME) endif() -check_symbol_exists("inet_ntop" "${CURL_INCLUDES};stdlib.h;string.h" HAVE_INET_NTOP) # arpa/inet.h netinet/in.h sys/socket.h -check_symbol_exists("inet_pton" "${CURL_INCLUDES};stdlib.h;string.h" HAVE_INET_PTON) # arpa/inet.h netinet/in.h sys/socket.h +if(NOT WIN32) + check_symbol_exists("inet_ntop" "${CURL_INCLUDES};stdlib.h;string.h" HAVE_INET_NTOP) # arpa/inet.h netinet/in.h sys/socket.h + check_symbol_exists("inet_pton" "${CURL_INCLUDES};stdlib.h;string.h" HAVE_INET_PTON) # arpa/inet.h netinet/in.h sys/socket.h +endif() check_symbol_exists("fsetxattr" "sys/xattr.h" HAVE_FSETXATTR) if(HAVE_FSETXATTR) diff --git a/configure.ac b/configure.ac index a5c3f07940..25dc959037 100644 --- a/configure.ac +++ b/configure.ac @@ -4076,8 +4076,6 @@ CURL_CHECK_FUNC_GETIFADDRS CURL_CHECK_FUNC_GETPEERNAME CURL_CHECK_FUNC_GETSOCKNAME CURL_CHECK_FUNC_GMTIME_R -CURL_CHECK_FUNC_INET_NTOP -CURL_CHECK_FUNC_INET_PTON CURL_CHECK_FUNC_IOCTL CURL_CHECK_FUNC_IOCTLSOCKET CURL_CHECK_FUNC_IOCTLSOCKET_CAMEL @@ -4127,6 +4125,8 @@ if test "$curl_cv_native_windows" != 'yes'; then realpath \ sched_yield \ ]) + CURL_CHECK_FUNC_INET_NTOP + CURL_CHECK_FUNC_INET_PTON CURL_CHECK_FUNC_STRCASECMP CURL_CHECK_FUNC_STRCMPI CURL_CHECK_FUNC_STRICMP diff --git a/lib/inet_ntop.h b/lib/inet_ntop.h index 6bc7e27a79..9672283b49 100644 --- a/lib/inet_ntop.h +++ b/lib/inet_ntop.h @@ -38,13 +38,7 @@ char *Curl_inet_ntop(int af, const void *addr, char *buf, size_t size); #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef _WIN32 -#if defined(_MSC_VER) && (_MSC_VER <= 1900) -#define Curl_inet_ntop(af,addr,buf,size) inet_ntop(af, (void *)addr, buf, size) -#else -#define Curl_inet_ntop(af,addr,buf,size) inet_ntop(af, addr, buf, size) -#endif -#elif defined(__AMIGA__) +#ifdef __AMIGA__ #define Curl_inet_ntop(af,addr,buf,size) \ (char *)inet_ntop(af, (void *)addr, (unsigned char *)buf, \ (curl_socklen_t)(size)) diff --git a/lib/inet_pton.h b/lib/inet_pton.h index 915385fc25..280e5f6f5f 100644 --- a/lib/inet_pton.h +++ b/lib/inet_pton.h @@ -38,7 +38,7 @@ int Curl_inet_pton(int, const char *, void *); #ifdef HAVE_ARPA_INET_H #include #endif -#if defined(__AMIGA__) +#ifdef __AMIGA__ #define Curl_inet_pton(x,y,z) inet_pton(x,(unsigned char *)y,z) #else #define Curl_inet_pton(x,y,z) inet_pton(x,y,z)