]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
windows: do not use winsock2 `inet_ntop()`/`inet_pton()`
authorViktor Szakats <commit@vsz.me>
Wed, 5 Mar 2025 17:06:37 +0000 (18:06 +0100)
committerViktor Szakats <commit@vsz.me>
Thu, 6 Mar 2025 19:09:25 +0000 (20:09 +0100)
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

CMake/win32-cache.cmake
CMakeLists.txt
configure.ac
lib/inet_ntop.h
lib/inet_pton.h

index 75c8ce69baed7bc41d0b6a20672079a675e20c1c..615db9e49c644798f3eacaa9743f3854d3d0afdd 100644 (file)
@@ -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)
index d8243119b4b8d3d853d59b17ed6906df11fff2ab..b3e4f62a72f01c2edefc64255dc6c2c51f062f4a 100644 (file)
@@ -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)
index a5c3f079409bdc0910918a493739eab8c82248ab..25dc95903777aeb2f228e2d57739ad4c58a29f5b 100644 (file)
@@ -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
index 6bc7e27a79f2c36440bae77cabfe171d5e5db52e..9672283b491385494e9baf74bb6d7eafb18f8adf 100644 (file)
@@ -38,13 +38,7 @@ char *Curl_inet_ntop(int af, const void *addr, char *buf, size_t size);
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
 #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))
index 915385fc257315ab3b4536715c158886777873ee..280e5f6f5f64511aecd5731956bd25146fe913aa 100644 (file)
@@ -38,7 +38,7 @@ int Curl_inet_pton(int, const char *, void *);
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
 #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)