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.

b06c12b724/lib/inet_ntop.c (L188-L190)
b06c12b724/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
This commit is contained in:
Viktor Szakats 2025-03-05 18:06:37 +01:00
parent ff86c1951b
commit 8537a5b0bc
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
5 changed files with 13 additions and 30 deletions

View 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)

View 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)

View 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

View 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))

View 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)