[MSAFD] Don't update shared state on close if we still have active references to...
[reactos.git] / reactos / dll / win32 / msafd / msafd.h
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Ancillary Function Driver DLL
4 * FILE: dll/win32/msafd/msafd.h
5 * PURPOSE: Ancillary Function Driver DLL header
6 */
7
8 #ifndef __MSAFD_H
9 #define __MSAFD_H
10
11 #include <stdarg.h>
12
13 #define WIN32_NO_STATUS
14 #define _INC_WINDOWS
15 #define COM_NO_WINDOWS_H
16
17 #include <windef.h>
18 #include <winbase.h>
19 #include <ws2spi.h>
20 #define NTOS_MODE_USER
21 #include <ndk/exfuncs.h>
22 #include <ndk/iofuncs.h>
23 #include <ndk/obfuncs.h>
24 #include <ndk/rtlfuncs.h>
25
26 /* This includes ntsecapi.h so it needs to come after the NDK */
27 #include <wsahelp.h>
28 #include <tdi.h>
29 #include <afd/shared.h>
30 #include <mswsock.h>
31 #include "include/helpers.h"
32
33 extern HANDLE GlobalHeap;
34 extern WSPUPCALLTABLE Upcalls;
35 extern LPWPUCOMPLETEOVERLAPPEDREQUEST lpWPUCompleteOverlappedRequest;
36 extern LIST_ENTRY SockHelpersListHead;
37 extern HANDLE SockEvent;
38 extern HANDLE SockAsyncCompletionPort;
39 extern BOOLEAN SockAsyncSelectCalled;
40
41 typedef enum _SOCKET_STATE {
42 SocketOpen,
43 SocketBound,
44 SocketBoundUdp,
45 SocketConnected,
46 SocketClosed
47 } SOCKET_STATE, *PSOCKET_STATE;
48
49 typedef struct _SOCK_SHARED_INFO {
50 SOCKET_STATE State;
51 LONG RefCount;
52 INT AddressFamily;
53 INT SocketType;
54 INT Protocol;
55 INT SizeOfLocalAddress;
56 INT SizeOfRemoteAddress;
57 struct linger LingerData;
58 ULONG SendTimeout;
59 ULONG RecvTimeout;
60 ULONG SizeOfRecvBuffer;
61 ULONG SizeOfSendBuffer;
62 ULONG ConnectTime;
63 struct {
64 BOOLEAN Listening:1;
65 BOOLEAN Broadcast:1;
66 BOOLEAN Debug:1;
67 BOOLEAN OobInline:1;
68 BOOLEAN ReuseAddresses:1;
69 BOOLEAN ExclusiveAddressUse:1;
70 BOOLEAN NonBlocking:1;
71 BOOLEAN DontUseWildcard:1;
72 BOOLEAN ReceiveShutdown:1;
73 BOOLEAN SendShutdown:1;
74 BOOLEAN UseDelayedAcceptance:1;
75 BOOLEAN UseSAN:1;
76 }; // Flags
77 DWORD CreateFlags;
78 DWORD ServiceFlags1;
79 DWORD ProviderFlags;
80 GROUP GroupID;
81 DWORD GroupType;
82 INT GroupPriority;
83 INT SocketLastError;
84 HWND hWnd;
85 LONG Unknown;
86 DWORD SequenceNumber;
87 UINT wMsg;
88 LONG AsyncEvents;
89 LONG AsyncDisabledEvents;
90 SOCKADDR WSLocalAddress;
91 SOCKADDR WSRemoteAddress;
92 } SOCK_SHARED_INFO, *PSOCK_SHARED_INFO;
93
94 typedef struct _SOCKET_INFORMATION {
95 SOCKET Handle;
96 PSOCK_SHARED_INFO SharedData;
97 HANDLE SharedDataHandle;
98 DWORD HelperEvents;
99 PHELPER_DATA HelperData;
100 PVOID HelperContext;
101 PSOCKADDR LocalAddress;
102 PSOCKADDR RemoteAddress;
103 HANDLE TdiAddressHandle;
104 HANDLE TdiConnectionHandle;
105 PVOID AsyncData;
106 HANDLE EventObject;
107 LONG NetworkEvents;
108 CRITICAL_SECTION Lock;
109 PVOID SanData;
110 BOOL TrySAN;
111 WSAPROTOCOL_INFOW ProtocolInfo;
112 struct _SOCKET_INFORMATION *NextSocket;
113 } SOCKET_INFORMATION, *PSOCKET_INFORMATION;
114
115
116 typedef struct _SOCKET_CONTEXT {
117 SOCK_SHARED_INFO SharedData;
118 ULONG SizeOfHelperData;
119 ULONG Padding;
120 SOCKADDR LocalAddress;
121 SOCKADDR RemoteAddress;
122 /* Plus Helper Data */
123 } SOCKET_CONTEXT, *PSOCKET_CONTEXT;
124
125 typedef struct _ASYNC_DATA {
126 PSOCKET_INFORMATION ParentSocket;
127 DWORD SequenceNumber;
128 IO_STATUS_BLOCK IoStatusBlock;
129 AFD_POLL_INFO AsyncSelectInfo;
130 } ASYNC_DATA, *PASYNC_DATA;
131
132 typedef struct _AFDAPCCONTEXT
133 {
134 LPWSAOVERLAPPED lpOverlapped;
135 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine;
136 PSOCKET_INFORMATION lpSocket;
137 } AFDAPCCONTEXT, *PAFDAPCCONTEXT;
138
139 SOCKET
140 WSPAPI
141 WSPAccept(
142 IN SOCKET s,
143 OUT LPSOCKADDR addr,
144 IN OUT LPINT addrlen,
145 IN LPCONDITIONPROC lpfnCondition,
146 IN DWORD dwCallbackData,
147 OUT LPINT lpErrno);
148
149 INT
150 WSPAPI
151 WSPAddressToString(
152 IN LPSOCKADDR lpsaAddress,
153 IN DWORD dwAddressLength,
154 IN LPWSAPROTOCOL_INFOW lpProtocolInfo,
155 OUT LPWSTR lpszAddressString,
156 IN OUT LPDWORD lpdwAddressStringLength,
157 OUT LPINT lpErrno);
158
159 INT
160 WSPAPI
161 WSPAsyncSelect(
162 IN SOCKET s,
163 IN HWND hWnd,
164 IN UINT wMsg,
165 IN LONG lEvent,
166 OUT LPINT lpErrno);
167
168 INT
169 WSPAPI WSPBind(
170 IN SOCKET s,
171 IN CONST SOCKADDR *name,
172 IN INT namelen,
173 OUT LPINT lpErrno);
174
175 INT
176 WSPAPI
177 WSPCancelBlockingCall(
178 OUT LPINT lpErrno);
179
180 INT
181 WSPAPI
182 WSPCleanup(
183 OUT LPINT lpErrno);
184
185 INT
186 WSPAPI
187 WSPCloseSocket(
188 IN SOCKET s,
189 OUT LPINT lpErrno);
190
191 INT
192 WSPAPI
193 WSPConnect(
194 IN SOCKET s,
195 IN CONST SOCKADDR *name,
196 IN INT namelen,
197 IN LPWSABUF lpCallerData,
198 OUT LPWSABUF lpCalleeData,
199 IN LPQOS lpSQOS,
200 IN LPQOS lpGQOS,
201 OUT LPINT lpErrno);
202
203 INT
204 WSPAPI
205 WSPDuplicateSocket(
206 IN SOCKET s,
207 IN DWORD dwProcessId,
208 OUT LPWSAPROTOCOL_INFOW lpProtocolInfo,
209 OUT LPINT lpErrno);
210
211 INT
212 WSPAPI
213 WSPEnumNetworkEvents(
214 IN SOCKET s,
215 IN WSAEVENT hEventObject,
216 OUT LPWSANETWORKEVENTS lpNetworkEvents,
217 OUT LPINT lpErrno);
218
219 INT
220 WSPAPI
221 WSPEventSelect(
222 IN SOCKET s,
223 IN WSAEVENT hEventObject,
224 IN LONG lNetworkEvents,
225 OUT LPINT lpErrno);
226
227 BOOL
228 WSPAPI
229 WSPGetOverlappedResult(
230 IN SOCKET s,
231 IN LPWSAOVERLAPPED lpOverlapped,
232 OUT LPDWORD lpcbTransfer,
233 IN BOOL fWait,
234 OUT LPDWORD lpdwFlags,
235 OUT LPINT lpErrno);
236
237 INT
238 WSPAPI
239 WSPGetPeerName(
240 IN SOCKET s,
241 OUT LPSOCKADDR name,
242 IN OUT LPINT namelen,
243 OUT LPINT lpErrno);
244
245 BOOL
246 WSPAPI
247 WSPGetQOSByName(
248 IN SOCKET s,
249 IN OUT LPWSABUF lpQOSName,
250 OUT LPQOS lpQOS,
251 OUT LPINT lpErrno);
252
253 INT
254 WSPAPI
255 WSPGetSockName(
256 IN SOCKET s,
257 OUT LPSOCKADDR name,
258 IN OUT LPINT namelen,
259 OUT LPINT lpErrno);
260
261 INT
262 WSPAPI
263 WSPGetSockOpt(
264 IN SOCKET s,
265 IN INT level,
266 IN INT optname,
267 OUT CHAR FAR* optval,
268 IN OUT LPINT optlen,
269 OUT LPINT lpErrno);
270
271 INT
272 WSPAPI
273 WSPIoctl(
274 IN SOCKET s,
275 IN DWORD dwIoControlCode,
276 IN LPVOID lpvInBuffer,
277 IN DWORD cbInBuffer,
278 OUT LPVOID lpvOutBuffer,
279 IN DWORD cbOutBuffer,
280 OUT LPDWORD lpcbBytesReturned,
281 IN LPWSAOVERLAPPED lpOverlapped,
282 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
283 IN LPWSATHREADID lpThreadId,
284 OUT LPINT lpErrno);
285
286 SOCKET
287 WSPAPI
288 WSPJoinLeaf(
289 IN SOCKET s,
290 IN CONST SOCKADDR *name,
291 IN INT namelen,
292 IN LPWSABUF lpCallerData,
293 OUT LPWSABUF lpCalleeData,
294 IN LPQOS lpSQOS,
295 IN LPQOS lpGQOS,
296 IN DWORD dwFlags,
297 OUT LPINT lpErrno);
298
299 INT
300 WSPAPI
301 WSPListen(
302 IN SOCKET s,
303 IN INT backlog,
304 OUT LPINT lpErrno);
305
306 INT
307 WSPAPI
308 WSPRecv(
309 IN SOCKET s,
310 IN OUT LPWSABUF lpBuffers,
311 IN DWORD dwBufferCount,
312 OUT LPDWORD lpNumberOfBytesRecvd,
313 IN OUT LPDWORD lpFlags,
314 IN LPWSAOVERLAPPED lpOverlapped,
315 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
316 IN LPWSATHREADID lpThreadId,
317 OUT LPINT lpErrno);
318
319 INT
320 WSPAPI
321 WSPRecvDisconnect(
322 IN SOCKET s,
323 OUT LPWSABUF lpInboundDisconnectData,
324 OUT LPINT lpErrno);
325
326 INT
327 WSPAPI
328 WSPRecvFrom(
329 IN SOCKET s,
330 IN OUT LPWSABUF lpBuffers,
331 IN DWORD dwBufferCount,
332 OUT LPDWORD lpNumberOfBytesRecvd,
333 IN OUT LPDWORD lpFlags,
334 OUT LPSOCKADDR lpFrom,
335 IN OUT LPINT lpFromlen,
336 IN LPWSAOVERLAPPED lpOverlapped,
337 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
338 IN LPWSATHREADID lpThreadId,
339 OUT LPINT lpErrno);
340
341 INT
342 WSPAPI
343 WSPSelect(
344 IN INT nfds,
345 IN OUT fd_set *readfds,
346 IN OUT fd_set *writefds,
347 IN OUT fd_set *exceptfds,
348 IN CONST struct timeval *timeout,
349 OUT LPINT lpErrno);
350
351 INT
352 WSPAPI
353 WSPSend(
354 IN SOCKET s,
355 IN LPWSABUF lpBuffers,
356 IN DWORD dwBufferCount,
357 OUT LPDWORD lpNumberOfBytesSent,
358 IN DWORD dwFlags,
359 IN LPWSAOVERLAPPED lpOverlapped,
360 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
361 IN LPWSATHREADID lpThreadId,
362 OUT LPINT lpErrno);
363
364 INT
365 WSPAPI
366 WSPSendDisconnect(
367 IN SOCKET s,
368 IN LPWSABUF lpOutboundDisconnectData,
369 OUT LPINT lpErrno);
370
371 INT
372 WSPAPI
373 WSPSendTo(
374 IN SOCKET s,
375 IN LPWSABUF lpBuffers,
376 IN DWORD dwBufferCount,
377 OUT LPDWORD lpNumberOfBytesSent,
378 IN DWORD dwFlags,
379 IN CONST SOCKADDR *lpTo,
380 IN INT iTolen,
381 IN LPWSAOVERLAPPED lpOverlapped,
382 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
383 IN LPWSATHREADID lpThreadId,
384 OUT LPINT lpErrno);
385
386 INT
387 WSPAPI
388 WSPSetSockOpt(
389 IN SOCKET s,
390 IN INT level,
391 IN INT optname,
392 IN CONST CHAR FAR* optval,
393 IN INT optlen,
394 OUT LPINT lpErrno);
395
396 INT
397 WSPAPI
398 WSPShutdown(
399 IN SOCKET s,
400 IN INT how,
401 OUT LPINT lpErrno);
402
403 SOCKET
404 WSPAPI
405 WSPSocket(
406 IN INT af,
407 IN INT type,
408 IN INT protocol,
409 IN LPWSAPROTOCOL_INFOW lpProtocolInfo,
410 IN GROUP g,
411 IN DWORD dwFlags,
412 OUT LPINT lpErrno);
413
414 INT
415 WSPAPI
416 WSPStringToAddress(
417 IN LPWSTR AddressString,
418 IN INT AddressFamily,
419 IN LPWSAPROTOCOL_INFOW lpProtocolInfo,
420 OUT LPSOCKADDR lpAddress,
421 IN OUT LPINT lpAddressLength,
422 OUT LPINT lpErrno);
423
424
425 PSOCKET_INFORMATION GetSocketStructure(
426 SOCKET Handle
427 );
428
429 INT TranslateNtStatusError( NTSTATUS Status );
430
431 VOID DeleteSocketStructure( SOCKET Handle );
432
433 int GetSocketInformation(
434 PSOCKET_INFORMATION Socket,
435 ULONG AfdInformationClass,
436 PBOOLEAN Boolean OPTIONAL,
437 PULONG Ulong OPTIONAL,
438 PLARGE_INTEGER LargeInteger OPTIONAL,
439 LPWSAOVERLAPPED Overlapped OPTIONAL,
440 LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine OPTIONAL
441 );
442
443 int SetSocketInformation(
444 PSOCKET_INFORMATION Socket,
445 ULONG AfdInformationClass,
446 PBOOLEAN Boolean OPTIONAL,
447 PULONG Ulong OPTIONAL,
448 PLARGE_INTEGER LargeInteger OPTIONAL,
449 LPWSAOVERLAPPED Overlapped OPTIONAL,
450 LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine OPTIONAL
451 );
452
453 int CreateContext(
454 PSOCKET_INFORMATION Socket
455 );
456
457 int SockAsyncThread(
458 PVOID ThreadParam
459 );
460
461 VOID
462 SockProcessAsyncSelect(
463 PSOCKET_INFORMATION Socket,
464 PASYNC_DATA AsyncData
465 );
466
467 VOID
468 SockAsyncSelectCompletionRoutine(
469 PVOID Context,
470 PIO_STATUS_BLOCK IoStatusBlock
471 );
472
473 BOOLEAN
474 SockCreateOrReferenceAsyncThread(
475 VOID
476 );
477
478 BOOLEAN SockGetAsyncSelectHelperAfdHandle(
479 VOID
480 );
481
482 VOID SockProcessQueuedAsyncSelect(
483 PVOID Context,
484 PIO_STATUS_BLOCK IoStatusBlock
485 );
486
487 VOID
488 SockReenableAsyncSelectEvent (
489 IN PSOCKET_INFORMATION Socket,
490 IN ULONG Event
491 );
492
493 typedef VOID (*PASYNC_COMPLETION_ROUTINE)(PVOID Context, PIO_STATUS_BLOCK IoStatusBlock);
494
495 FORCEINLINE
496 DWORD
497 MsafdReturnWithErrno(NTSTATUS Status,
498 LPINT Errno,
499 DWORD Received,
500 LPDWORD ReturnedBytes)
501 {
502 if (Errno)
503 {
504 *Errno = TranslateNtStatusError(Status);
505
506 if (ReturnedBytes)
507 *ReturnedBytes = (*Errno == 0) ? Received : 0;
508
509 return (*Errno == 0) ? 0 : SOCKET_ERROR;
510 }
511 else
512 {
513 DbgPrint("%s: Received invalid lpErrno pointer!\n", __FUNCTION__);
514
515 if (ReturnedBytes)
516 *ReturnedBytes = (Status == STATUS_SUCCESS) ? Received : 0;
517
518 return (Status == STATUS_SUCCESS) ? 0 : SOCKET_ERROR;
519 }
520 }
521
522 #endif /* __MSAFD_H */