Skip to content

Commit

Permalink
[port] Windows XP: try to support threading and event subsystems
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh352 committed Jan 13, 2025
1 parent 0518d40 commit e5d3e63
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,13 @@ CHECK_SYMBOL_EXISTS (gettimeofday "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_GETTIME
CHECK_SYMBOL_EXISTS (if_indextoname "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_IF_INDEXTONAME)
CHECK_SYMBOL_EXISTS (if_nametoindex "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_IF_NAMETOINDEX)
CHECK_SYMBOL_EXISTS (GetBestRoute2 "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_GETBESTROUTE2)
CHECK_SYMBOL_EXISTS (WSAIoctl "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_WSAIOCTL)
CHECK_SYMBOL_EXISTS (GetQueuedCompletionStatusEx "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_GETQUEUEDCOMPLETIONSTATUSEX)
CHECK_SYMBOL_EXISTS (ConvertInterfaceIndexToLuid "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_CONVERTINTERFACEINDEXTOLUID)
CHECK_SYMBOL_EXISTS (ConvertInterfaceLuidToNameA "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_CONVERTINTERFACELUIDTONAMEA)
CHECK_SYMBOL_EXISTS (NotifyIpInterfaceChange "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_NOTIFYIPINTERFACECHANGE)
CHECK_SYMBOL_EXISTS (RegisterWaitForSingleObject "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_REGISTERWAITFORSINGLEOBJECT)
CHECK_SYMBOL_EXISTS (SetFileCompletionNotificationModes "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_SETFILECOMPLETIONNOTIFICATIONMODES)


CHECK_SYMBOL_EXISTS (inet_net_pton "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_INET_NET_PTON)
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ environment:
SKIP_TESTS: no
MSVC_SETUP_ARG: x86
MSVC_SETUP_PATH: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat
CMAKE_EXTRA_OPTIONS: -GNinja -DCARES_BUILD_TESTS=ON -DGTEST_ROOT=C:\projects\googletest -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebugDLL -DCARES_THREADS=OFF -DCMAKE_C_FLAGS="-D_WIN32_WINNT=0x0501" -DCMAKE_CXX_FLAGS="-D_WIN32_WINNT=0x0501"
CMAKE_EXTRA_OPTIONS: -GNinja -DCARES_BUILD_TESTS=ON -DGTEST_ROOT=C:\projects\googletest -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebugDLL -DCMAKE_C_FLAGS="-D_WIN32_WINNT=0x0501" -DCMAKE_CXX_FLAGS="-D_WIN32_WINNT=0x0501"
TOOLSDIR: ./build/bin
TESTDIR: ./build/bin
BUILD_GOOGLETEST: yes
Expand Down Expand Up @@ -82,7 +82,7 @@ environment:
TOOLSDIR: ./build/bin
TESTDIR: ./build/bin
PATH: C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin;%PATH%
CMAKE_EXTRA_OPTIONS: -DCARES_SHARED=OFF -GNinja -DCARES_BUILD_TESTS=ON -DGTEST_ROOT=C:\projects\googletest -DCARES_THREADS=OFF -DCMAKE_C_FLAGS="-D_WIN32_WINNT=0x0501" -DCMAKE_CXX_FLAGS="-D_WIN32_WINNT=0x0501"
CMAKE_EXTRA_OPTIONS: -DCARES_SHARED=OFF -GNinja -DCARES_BUILD_TESTS=ON -DGTEST_ROOT=C:\projects\googletest -DCMAKE_C_FLAGS="-D_WIN32_WINNT=0x0501" -DCMAKE_CXX_FLAGS="-D_WIN32_WINNT=0x0501"
BUILD_GOOGLETEST: yes

# Disabled until AppVeyor updates their Visual Studio with this patch:
Expand Down
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,14 @@ AC_CHECK_DECL(pipe2, [AC_DEFINE([HAVE_PIPE2], 1, [Define t
AC_CHECK_DECL(kqueue, [AC_DEFINE([HAVE_KQUEUE], 1, [Define to 1 if you have `kqueue`] )], [], $cares_all_includes)
AC_CHECK_DECL(epoll_create1, [AC_DEFINE([HAVE_EPOLL], 1, [Define to 1 if you have `epoll_{create1,ctl,wait}`])], [], $cares_all_includes)
AC_CHECK_DECL(GetBestRoute2, [AC_DEFINE([HAVE_GETBESTROUTE2], 1, [Define to 1 if you have `GetBestRoute2`] )], [], $cares_all_includes)
AC_CHECK_DECL(GetQueuedCompletionStatusEx, [AC_DEFINE([HAVE_GETQUEUEDCOMPLETIONSTATUSEX], 1, [Define to 1 if you have `GetQueuedCompletionStatusEx`])], [], $cares_all_includes)
AC_CHECK_DECL(ConvertInterfaceIndexToLuid, [AC_DEFINE([HAVE_CONVERTINTERFACEINDEXTOLUID], 1, [Define to 1 if you have `ConvertInterfaceIndexToLuid`])], [], $cares_all_includes)
AC_CHECK_DECL(ConvertInterfaceLuidToNameA, [AC_DEFINE([HAVE_CONVERTINTERFACELUIDTONAMEA], 1, [Define to 1 if you have `ConvertInterfaceLuidToNameA`])], [], $cares_all_includes)
AC_CHECK_DECL(NotifyIpInterfaceChange, [AC_DEFINE([HAVE_NOTIFYIPINTERFACECHANGE], 1, [Define to 1 if you have `NotifyIpInterfaceChange`] )], [], $cares_all_includes)
AC_CHECK_DECL(RegisterWaitForSingleObject, [AC_DEFINE([HAVE_REGISTERWAITFORSINGLEOBJECT], 1, [Define to 1 if you have `RegisterWaitForSingleObject`])], [], $cares_all_includes)
AC_CHECK_DECL(__system_property_get, [AC_DEFINE([HAVE___SYSTEM_PROPERTY_GET], 1, [Define to 1 if you have `__system_property_get`] )], [], $cares_all_includes)
AC_CHECK_DECL(SetFileCompletionNotificationModes, [AC_DEFINE([HAVE_SETFILECOMPLETIONNOTIFICATIONMODES], 1, [Define to 1 if you have `SetFileCompletionNotificationModes`])], [], $cares_all_includes)
AC_CHECK_DECL(WSAIoctl, [AC_DEFINE([HAVE_WSAIoctl], 1, [Define to 1 if you have `WSAIoctl`])], [], $cares_all_includes)


dnl ###############################################################################
Expand Down
9 changes: 9 additions & 0 deletions src/lib/ares_config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
/* Define to 1 if you have the `GetBestRoute2' function. */
#cmakedefine HAVE_GETBESTROUTE2 1

/* Define to 1 if you have the `WSAIoctl' function. */
#cmakedefine HAVE_WSAIOCTL 1

/* Define to 1 if you have the `GetQueuedCompletionStatusEx' function. */
#cmakedefine HAVE_GETQUEUEDCOMPLETIONSTATUSEX 1

/* Define to 1 if you have the `ConvertInterfaceIndexToLuid' function. */
#cmakedefine HAVE_CONVERTINTERFACEINDEXTOLUID 1

Expand All @@ -160,6 +166,9 @@
/* Define to 1 if you have the `RegisterWaitForSingleObject' function. */
#cmakedefine HAVE_REGISTERWAITFORSINGLEOBJECT 1

/* Define to 1 if you have the `SetFileCompletionNotificationModes' function. */
#cmakedefine HAVE_SETFILECOMPLETIONNOTIFICATIONMODES 1

/* Define to 1 if you have a IPv6 capable working inet_net_pton function. */
#cmakedefine HAVE_INET_NET_PTON 1

Expand Down
15 changes: 11 additions & 4 deletions src/lib/config-win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,8 @@
# undef HAVE_NETIOAPI_H
#endif

/* Threading support enabled for Vista+ */
#if !defined(_WIN32_WINNT) || _WIN32_WINNT >= 0x0600
# define CARES_THREADS 1
#endif
/* Threading support enabled on Windows always (really XP+ only). */
#define CARES_THREADS 1

/* ---------------------------------------------------------------- */
/* TYPEDEF REPLACEMENTS */
Expand Down Expand Up @@ -360,6 +358,15 @@
/* Define to 1 if you have the `RegisterWaitForSingleObject' function. */
#define HAVE_REGISTERWAITFORSINGLEOBJECT 1

#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
/* Define to 1 if you have the `SetFileCompletionNotificationModes' function. */
# define HAVE_SETFILECOMPLETIONNOTIFICATIONMODES 1
/* Define to 1 if you have the `WSAIoctl' function. */
# define HAVE_WSAIOCTL 1
/* Define to 1 if you have the `GetQueuedCompletionStatusEx' function. */
# define HAVE_GETQUEUEDCOMPLETIONSTATUSEX 1
#endif

#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) && \
!defined(__WATCOMC__) && !defined(WATT32)
/* Define if you have if_nametoindex() */
Expand Down
53 changes: 51 additions & 2 deletions src/lib/event/ares_event_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,12 @@ static ares_slist_node_t *ares_afd_handle_create(ares_evsys_win32_t *ew)
goto fail;
}

#ifdef HAVE_SETFILECOMPLETIONNOTIFICATIONMODES
if (!SetFileCompletionNotificationModes(afd->afd_handle,
FILE_SKIP_SET_EVENT_ON_HANDLE)) {
goto fail;
}
#endif

node = ares_slist_insert(ew->afd_handles, afd);
if (node == NULL) {
Expand Down Expand Up @@ -521,6 +523,11 @@ static ares_bool_t ares_evsys_win32_init(ares_event_thread_t *e)

static ares_socket_t ares_evsys_win32_basesocket(ares_socket_t socket)
{
#ifndef HAVE_WSAIOCTL
/* Assume we don't have an LSP and return the provided socket as the base
* socket. WSAIoctl() isn't supported on Windows XP or below */
return socket;
#else
while (1) {
DWORD bytes; /* Not used */
ares_socket_t base_socket = ARES_SOCKET_BAD;
Expand Down Expand Up @@ -558,6 +565,7 @@ static ares_socket_t ares_evsys_win32_basesocket(ares_socket_t socket)
}

return socket;
#endif
}

static ares_bool_t ares_evsys_win32_afd_enqueue(ares_event_t *event,
Expand Down Expand Up @@ -906,6 +914,46 @@ static ares_bool_t ares_evsys_win32_process_socket_event(
return ARES_TRUE;
}

static BOOL ares_GetQueuedCompletionStatusEx(
HANDLE CompletionPort,
LPOVERLAPPED_ENTRY lpCompletionPortEntries,
ULONG ulCount,
PULONG ulNumEntriesRemoved,
DWORD dwMilliseconds,
BOOL fAlertable)
{
#ifdef HAVE_GETQUEUEDCOMPLETIONSTATUSEX
return GetQueuedCompletionStatusEx(CompletionPort, lpCompletionPortEntries,
ulCount, ulNumEntriesRemoved, dwMilliseconds, fAlertable);
#else
ULONG i;

(void)fAlertable;

memset(lpCompletionPortEntries, 0,
ulCount * sizeof(*lpCompletionPortEntries));
(*ulNumEntriesRemoved) = 0;

for (i=0; i<ulCount; i++) {
if (!GetQueuedCompletionStatus(CompletionPort,
&lpCompletionPortEntries[i].dwNumberOfBytesTransferred,
&lpCompletionPortEntries[i].lpCompletionKey,
&lpCompletionPortEntries[i].lpOverlapped,
dwMilliseconds)) {
break;
}

(*ulNumEntriesRemoved)++;
}

if (*ulNumEntriesRemove > 0) {
return TRUE;
}

return FALSE;
#endif
}

static size_t ares_evsys_win32_wait(ares_event_thread_t *e,
unsigned long timeout_ms)
{
Expand All @@ -923,8 +971,9 @@ static size_t ares_evsys_win32_wait(ares_event_thread_t *e,
* on subsequent attempts, ensure the timeout is 0 */
do {
nentries = maxentries;
status = GetQueuedCompletionStatusEx(ew->iocp_handle, entries, nentries,
&nentries, tout, FALSE);
status = ares_GetQueuedCompletionStatusEx(ew->iocp_handle, entries,
nentries, &nentries, tout,
FALSE);

/* Next loop around, we want to return instantly if there are no events to
* be processed */
Expand Down
10 changes: 10 additions & 0 deletions src/lib/event/ares_event_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ typedef NTSTATUS(NTAPI *NtCreateFile_t)(
# define HANDLE_FLAG_INHERIT 0x00000001
# endif


# if defined(_WIN32_WINNT) && (_WIN32_WINNT < 0x0600)
typedef struct _OVERLAPPED_ENTRY {
ULONG_PTR lpCompletionKey;
LPOVERLAPPED lpOverlapped;
ULONG_PTR Internal;
DWORD dwNumberOfBytesTransferred;
} OVERLAPPED_ENTRY, *LPOVERLAPPED_ENTRY;
# endif

#endif /* _WIN32 */

#endif /* __ARES_EVENT_WIN32_H */
Loading

0 comments on commit e5d3e63

Please sign in to comment.