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