xref: /reactos/dll/win32/ws2_32/src/bhook.c (revision c2c66aff)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS WinSock 2 API
4  * FILE:        dll/win32/ws2_32_new/src/bhook.c
5  * PURPOSE:     Blocking Hook support for 1.x clients
6  * PROGRAMMER:  Alex Ionescu (alex@relsoft.net)
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include <ws2_32.h>
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* FUNCTIONS *****************************************************************/
17 
18 /*
19  * @implemented
20  */
21 INT
22 WSAAPI
23 WSACancelBlockingCall(VOID)
24 {
25     PWSPROCESS Process;
26     PWSTHREAD Thread;
27     INT ErrorCode;
28     DPRINT("WSACancelBlockingCall\n");
29 
30     /* Call the prolog */
31     ErrorCode = WsApiProlog(&Process, &Thread);
32     if (ErrorCode != ERROR_SUCCESS)
33     {
34         /* Fail */
35         SetLastError(ErrorCode);
36         return SOCKET_ERROR;
37     }
38 
39     /* Make sure this isn't a 2.2 client */
40     if (LOBYTE(Process->Version) >= 2)
41     {
42         /* Only valid for 1.x */
43         SetLastError(WSAEOPNOTSUPP);
44         return SOCKET_ERROR;
45     }
46 
47     /* Cancel the call */
48     ErrorCode = WsThreadCancelBlockingCall(Thread);
49     if (ErrorCode != ERROR_SUCCESS)
50     {
51         /* Fail */
52         SetLastError(ErrorCode);
53         return SOCKET_ERROR;
54     }
55 
56     /* Return success */
57     return ERROR_SUCCESS;
58 }
59 
60 /*
61  * @implemented
62  */
63 BOOL
64 WSAAPI
65 WSAIsBlocking(VOID)
66 {
67     PWSPROCESS Process;
68     PWSTHREAD Thread;
69     INT ErrorCode;
70     DPRINT("WSAIsBlocking\n");
71 
72     /* Call the prolog */
73     ErrorCode = WsApiProlog(&Process, &Thread);
74     if (ErrorCode != ERROR_SUCCESS)
75     {
76         /* Fail unless its because we're busy */
77         if (ErrorCode != WSAEINPROGRESS) return FALSE;
78     }
79 
80     /* Return the value from the thread */
81     return Thread->Blocking;
82 }
83 
84 /*
85  * @implemented
86  */
87 FARPROC
88 WSAAPI
89 WSASetBlockingHook(IN FARPROC lpBlockFunc)
90 {
91     PWSPROCESS Process;
92     PWSTHREAD Thread;
93     INT ErrorCode;
94     DPRINT("WSASetBlockingHook: %p\n", lpBlockFunc);
95 
96     /* Call the prolog */
97     ErrorCode = WsApiProlog(&Process, &Thread);
98     if (ErrorCode != ERROR_SUCCESS)
99     {
100         /* Fail */
101         SetLastError(ErrorCode);
102         return NULL;
103     }
104 
105     /* Make sure this isn't a 2.2 client */
106     if (LOBYTE(Process->Version) >= 2)
107     {
108         /* Only valid for 1.x */
109         SetLastError(WSAEOPNOTSUPP);
110         return NULL;
111     }
112 
113     /* Make sure the pointer is safe */
114     if (IsBadCodePtr(lpBlockFunc))
115     {
116         /* Invalid pointer */
117         SetLastError(WSAEFAULT);
118         return NULL;
119     }
120 
121     /* Set the blocking hook and return the previous one */
122     return WsThreadSetBlockingHook(Thread, lpBlockFunc);
123 }
124 
125 /*
126  * @implemented
127  */
128 INT
129 WSAAPI
130 WSAUnhookBlockingHook(VOID)
131 {
132     PWSPROCESS Process;
133     PWSTHREAD Thread;
134     INT ErrorCode;
135     DPRINT("WSAUnhookBlockingHook\n");
136 
137     /* Call the prolog */
138     ErrorCode = WsApiProlog(&Process, &Thread);
139     if (ErrorCode != ERROR_SUCCESS)
140     {
141         /* Fail */
142         SetLastError(ErrorCode);
143         return SOCKET_ERROR;
144     }
145 
146     /* Make sure this isn't a 2.2 client */
147     if (LOBYTE(Process->Version) >= 2)
148     {
149         /* Only valid for 1.x */
150         SetLastError(WSAEOPNOTSUPP);
151         return SOCKET_ERROR;
152     }
153 
154     /* Set the blocking hook and return the previous one */
155     return WsThreadUnhookBlockingHook(Thread);
156 }
157