xref: /reactos/dll/win32/ws2help/apc.c (revision 4561998a)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS WinSock 2 DLL
4  * FILE:        include/ws2_32.h
5  * PURPOSE:     WinSock 2 DLL header
6  */
7 
8 /* INCLUDES ******************************************************************/
9 
10 #include "precomp.h"
11 
12 /* DATA **********************************************************************/
13 
14 #define APCH        (HANDLE)'SOR '
15 
16 /* FUNCTIONS *****************************************************************/
17 
18 DWORD
19 WINAPI
20 WahOpenApcHelper(OUT PHANDLE ApcHelperHandle)
21 {
22     DWORD ErrorCode;
23 
24     /* Enter the prolog, make sure we're initialized */
25     ErrorCode = WS2HELP_PROLOG();
26     if (ErrorCode != ERROR_SUCCESS) return ErrorCode;
27 
28     /* Validate handle */
29     if (!ApcHelperHandle) return ERROR_INVALID_PARAMETER;
30 
31     /*
32      * Return a bogus handle ("ROS")
33      * Historical note:(MS sends "CKM", which probably stands for "Keith Moore"
34      * (KM), one of the core architects of Winsock 2.2 from Microsoft.
35      */
36     *ApcHelperHandle = APCH;
37     return ERROR_SUCCESS;
38 }
39 
40 DWORD
41 WINAPI
42 WahCloseApcHelper(IN HANDLE ApcHelperHandle)
43 {
44     DWORD ErrorCode;
45 
46     /* Enter the prolog, make sure we're initialized */
47     ErrorCode = WS2HELP_PROLOG();
48     if (ErrorCode != ERROR_SUCCESS) return ErrorCode;
49 
50     /* Validate handle */
51     if (ApcHelperHandle != APCH) return ERROR_INVALID_PARAMETER;
52 
53     /* return */
54     return ERROR_SUCCESS;
55 }
56 
57 DWORD
58 WINAPI
59 WahCloseThread(IN HANDLE ApcHelperHandle,
60                IN LPWSATHREADID ThreadId)
61 {
62     DWORD ErrorCode;
63 
64     /* Enter the prolog, make sure we're initialized */
65     ErrorCode = WS2HELP_PROLOG();
66     if (ErrorCode != ERROR_SUCCESS) return ErrorCode;
67 
68     /* Validate handles */
69     if ((ApcHelperHandle != APCH) || (!ThreadId) || (!ThreadId->ThreadHandle))
70     {
71         /* Invalid helper/thread handles */
72         return ERROR_INVALID_PARAMETER;
73     }
74 
75     /* Close the thread handle */
76     if (CloseHandle(ThreadId->ThreadHandle))
77     {
78         /* Clear the structure */
79         ThreadId->ThreadHandle = NULL;
80         ThreadId->Reserved = 0;
81         return NO_ERROR;
82     }
83 
84     /* return */
85     return GetLastError();
86 }
87 
88 INT
89 WINAPI
90 WahQueueUserApc(IN HANDLE ApcHelperHandle,
91                 IN LPWSATHREADID ThreadId,
92                 IN LPWSAUSERAPC ApcRoutine,
93                 IN PVOID ApcContext OPTIONAL)
94 {
95     /* Validate params  */
96     if ((ApcHelperHandle != APCH) ||
97         (!ThreadId) ||
98         (!ThreadId->ThreadHandle) ||
99         (!ApcRoutine))
100     {
101         /* Invalid parameters */
102         return ERROR_INVALID_PARAMETER;
103     }
104 
105     /* Queue the APC */
106     if (QueueUserAPC(ApcRoutine, ThreadId->ThreadHandle, (ULONG_PTR)ApcContext))
107     {
108         /* Return success */
109         return ERROR_SUCCESS;
110     }
111 
112     /* Fail */
113     return GetLastError();
114 }
115 
116 DWORD
117 WINAPI
118 WahOpenCurrentThread(IN HANDLE ApcHelperHandle,
119                      OUT LPWSATHREADID ThreadId)
120 {
121     HANDLE ProcessHandle, ThreadHandle;
122 
123     /* Validate params  */
124     if ((ApcHelperHandle != APCH) || (!ThreadId))
125     {
126         /* Invalid parameters */
127         return ERROR_INVALID_PARAMETER;
128     }
129 
130     /* Get the process/thread handles */
131     ProcessHandle = GetCurrentProcess();
132     ThreadHandle = GetCurrentThread();
133 
134     /* Duplicate the handle */
135     if (DuplicateHandle(ProcessHandle,
136                         ThreadHandle,
137                         ProcessHandle,
138                         &ThreadId->ThreadHandle,
139                         0,
140                         FALSE,
141                         DUPLICATE_SAME_ACCESS))
142     {
143         /* Save the thread handle and return */
144         ThreadId->Reserved = (DWORD_PTR)ThreadHandle;
145         return ERROR_SUCCESS;
146     }
147 
148     /* Fail */
149     return GetLastError();
150 }
151 
152 /* EOF */
153