1 /* MIT License
2  *
3  * Copyright (c) 2024 Brad House
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * SPDX-License-Identifier: MIT
25  */
26 #ifndef __ARES_EVENT_WIN32_H
27 #define __ARES_EVENT_WIN32_H
28 
29 #ifdef _WIN32
30 #  ifdef HAVE_WINSOCK2_H
31 #    include <winsock2.h>
32 #  endif
33 #  ifdef HAVE_WS2TCPIP_H
34 #    include <ws2tcpip.h>
35 #  endif
36 #  ifdef HAVE_MSWSOCK_H
37 #    include <mswsock.h>
38 #  endif
39 #  ifdef HAVE_WINDOWS_H
40 #    include <windows.h>
41 #  endif
42 
43 /* From winternl.h */
44 
45 /* If WDK is not installed and not using MinGW, provide the needed definitions
46  */
47 typedef LONG NTSTATUS;
48 
49 typedef struct _IO_STATUS_BLOCK {
50   union {
51     NTSTATUS Status;
52     PVOID    Pointer;
53   };
54 
55   ULONG_PTR Information;
56 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
57 
58 typedef VOID(NTAPI *PIO_APC_ROUTINE)(PVOID            ApcContext,
59                                      PIO_STATUS_BLOCK IoStatusBlock,
60                                      ULONG            Reserved);
61 
62 /* From ntstatus.h */
63 #  define STATUS_SUCCESS ((NTSTATUS)0x00000000)
64 #  ifndef STATUS_PENDING
65 #    define STATUS_PENDING ((NTSTATUS)0x00000103L)
66 #  endif
67 #  define STATUS_CANCELLED ((NTSTATUS)0xC0000120L)
68 #  define STATUS_NOT_FOUND ((NTSTATUS)0xC0000225L)
69 
70 /* Not sure what headers might have these */
71 #  define IOCTL_AFD_POLL 0x00012024
72 
73 #  define AFD_POLL_RECEIVE           0x0001
74 #  define AFD_POLL_RECEIVE_EXPEDITED 0x0002
75 #  define AFD_POLL_SEND              0x0004
76 #  define AFD_POLL_DISCONNECT        0x0008
77 #  define AFD_POLL_ABORT             0x0010
78 #  define AFD_POLL_LOCAL_CLOSE       0x0020
79 #  define AFD_POLL_ACCEPT            0x0080
80 #  define AFD_POLL_CONNECT_FAIL      0x0100
81 
82 typedef struct _AFD_POLL_HANDLE_INFO {
83   HANDLE   Handle;
84   ULONG    Events;
85   NTSTATUS Status;
86 } AFD_POLL_HANDLE_INFO, *PAFD_POLL_HANDLE_INFO;
87 
88 typedef struct _AFD_POLL_INFO {
89   LARGE_INTEGER        Timeout;
90   ULONG                NumberOfHandles;
91   ULONG                Exclusive;
92   AFD_POLL_HANDLE_INFO Handles[1];
93 } AFD_POLL_INFO, *PAFD_POLL_INFO;
94 
95 /* Prototypes for dynamically loaded functions from ntdll.dll */
96 typedef NTSTATUS(NTAPI *NtCancelIoFileEx_t)(HANDLE           FileHandle,
97                                             PIO_STATUS_BLOCK IoRequestToCancel,
98                                             PIO_STATUS_BLOCK IoStatusBlock);
99 typedef NTSTATUS(NTAPI *NtDeviceIoControlFile_t)(
100   HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
101   PIO_STATUS_BLOCK IoStatusBlock, ULONG IoControlCode, PVOID InputBuffer,
102   ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength);
103 
104 /* On UWP/Windows Store, these definitions aren't there for some reason */
105 #  ifndef SIO_BSP_HANDLE_POLL
106 #    define SIO_BSP_HANDLE_POLL 0x4800001D
107 #  endif
108 
109 #  ifndef SIO_BASE_HANDLE
110 #    define SIO_BASE_HANDLE 0x48000022
111 #  endif
112 
113 #  ifndef HANDLE_FLAG_INHERIT
114 #    define HANDLE_FLAG_INHERIT 0x00000001
115 #  endif
116 
117 #endif /* _WIN32 */
118 
119 #endif /* __ARES_EVENT_WIN32_H */
120