xref: /reactos/dll/win32/msafd/msafd.h (revision 40462c92)
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 
32 #include <wine/debug.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(msafd);
34 
35 #include "include/helpers.h"
36 
37 extern HANDLE GlobalHeap;
38 extern WSPUPCALLTABLE Upcalls;
39 extern LPWPUCOMPLETEOVERLAPPEDREQUEST lpWPUCompleteOverlappedRequest;
40 extern LIST_ENTRY SockHelpersListHead;
41 extern HANDLE SockEvent;
42 extern HANDLE SockAsyncCompletionPort;
43 extern BOOLEAN SockAsyncSelectCalled;
44 
45 typedef enum _SOCKET_STATE {
46     SocketOpen,
47     SocketBound,
48     SocketBoundUdp,
49     SocketConnected,
50     SocketClosed
51 } SOCKET_STATE, *PSOCKET_STATE;
52 
53 typedef struct _SOCK_SHARED_INFO {
54     SOCKET_STATE				State;
55     LONG						RefCount;
56     INT							AddressFamily;
57     INT							SocketType;
58     INT							Protocol;
59     INT							SizeOfLocalAddress;
60     INT							SizeOfRemoteAddress;
61     struct linger				LingerData;
62     ULONG						SendTimeout;
63     ULONG						RecvTimeout;
64     ULONG						SizeOfRecvBuffer;
65     ULONG						SizeOfSendBuffer;
66     ULONG						ConnectTime;
67     struct {
68         BOOLEAN					Listening:1;
69         BOOLEAN					Broadcast:1;
70         BOOLEAN					Debug:1;
71         BOOLEAN					OobInline:1;
72         BOOLEAN					ReuseAddresses:1;
73         BOOLEAN					ExclusiveAddressUse:1;
74         BOOLEAN					NonBlocking:1;
75         BOOLEAN					DontUseWildcard:1;
76         BOOLEAN					ReceiveShutdown:1;
77         BOOLEAN					SendShutdown:1;
78         BOOLEAN					UseDelayedAcceptance:1;
79 		BOOLEAN					UseSAN:1;
80     }; // Flags
81     DWORD						CreateFlags;
82     DWORD						ServiceFlags1;
83     DWORD						ProviderFlags;
84     GROUP						GroupID;
85     DWORD						GroupType;
86     INT							GroupPriority;
87     INT							SocketLastError;
88     HWND						hWnd;
89     LONG						Unknown;
90     DWORD						SequenceNumber;
91     UINT						wMsg;
92     LONG						AsyncEvents;
93     LONG						AsyncDisabledEvents;
94     SOCKADDR					WSLocalAddress;
95     SOCKADDR					WSRemoteAddress;
96 } SOCK_SHARED_INFO, *PSOCK_SHARED_INFO;
97 
98 typedef struct _SOCKET_INFORMATION {
99 	SOCKET Handle;
100 	PSOCK_SHARED_INFO SharedData;
101 	HANDLE SharedDataHandle;
102 	DWORD HelperEvents;
103 	PHELPER_DATA HelperData;
104 	PVOID HelperContext;
105 	PSOCKADDR LocalAddress;
106 	PSOCKADDR RemoteAddress;
107 	HANDLE TdiAddressHandle;
108 	HANDLE TdiConnectionHandle;
109 	PVOID AsyncData;
110 	HANDLE EventObject;
111 	LONG NetworkEvents;
112 	CRITICAL_SECTION Lock;
113 	PVOID SanData;
114 	BOOL TrySAN;
115 	WSAPROTOCOL_INFOW ProtocolInfo;
116 	struct _SOCKET_INFORMATION *NextSocket;
117 } SOCKET_INFORMATION, *PSOCKET_INFORMATION;
118 
119 
120 typedef struct _SOCKET_CONTEXT {
121 	SOCK_SHARED_INFO SharedData;
122 	ULONG SizeOfHelperData;
123 	ULONG Padding;
124 	SOCKADDR LocalAddress;
125 	SOCKADDR RemoteAddress;
126 	/* Plus Helper Data */
127 } SOCKET_CONTEXT, *PSOCKET_CONTEXT;
128 
129 typedef struct _ASYNC_DATA {
130 	PSOCKET_INFORMATION ParentSocket;
131 	DWORD SequenceNumber;
132 	IO_STATUS_BLOCK IoStatusBlock;
133 	AFD_POLL_INFO AsyncSelectInfo;
134 } ASYNC_DATA, *PASYNC_DATA;
135 
136 typedef struct _AFDAPCCONTEXT
137 {
138     LPWSAOVERLAPPED lpOverlapped;
139     LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine;
140     PSOCKET_INFORMATION lpSocket;
141 } AFDAPCCONTEXT, *PAFDAPCCONTEXT;
142 
143 _Must_inspect_result_
144 SOCKET
145 WSPAPI
146 WSPAccept(
147     _In_ SOCKET s,
148     _Out_writes_bytes_to_opt_(*addrlen, *addrlen) struct sockaddr FAR * addr,
149     _Inout_opt_ LPINT addrlen,
150     _In_opt_ LPCONDITIONPROC lpfnCondition,
151     _In_opt_ DWORD_PTR 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 BOOL
430 WSPAPI
431 WSPAcceptEx(
432     IN SOCKET sListenSocket,
433     IN SOCKET sAcceptSocket,
434     OUT PVOID lpOutputBuffer,
435     IN DWORD dwReceiveDataLength,
436     IN DWORD dwLocalAddressLength,
437     IN DWORD dwRemoteAddressLength,
438     OUT LPDWORD lpdwBytesReceived,
439     IN OUT LPOVERLAPPED lpOverlapped);
440 
441 BOOL
442 WSPAPI
443 WSPConnectEx(
444     IN SOCKET s,
445     IN const struct sockaddr *name,
446     IN int namelen,
447     IN PVOID lpSendBuffer,
448     IN DWORD dwSendDataLength,
449     OUT LPDWORD lpdwBytesSent,
450     IN OUT LPOVERLAPPED lpOverlapped);
451 
452 BOOL
453 WSPAPI
454 WSPDisconnectEx(
455     IN SOCKET hSocket,
456     IN LPOVERLAPPED lpOverlapped,
457     IN DWORD dwFlags,
458     IN DWORD reserved);
459 
460 VOID
461 WSPAPI
462 WSPGetAcceptExSockaddrs(
463     IN PVOID lpOutputBuffer,
464     IN DWORD dwReceiveDataLength,
465     IN DWORD dwLocalAddressLength,
466     IN DWORD dwRemoteAddressLength,
467     OUT struct sockaddr **LocalSockaddr,
468     OUT LPINT LocalSockaddrLength,
469     OUT struct sockaddr **RemoteSockaddr,
470     OUT LPINT RemoteSockaddrLength);
471 
472 PSOCKET_INFORMATION GetSocketStructure(
473 	SOCKET Handle
474 );
475 
476 INT TranslateNtStatusError( NTSTATUS Status );
477 
478 VOID DeleteSocketStructure( SOCKET Handle );
479 
480 int GetSocketInformation(
481 	PSOCKET_INFORMATION Socket,
482 	ULONG				AfdInformationClass,
483     PBOOLEAN            Boolean      OPTIONAL,
484 	PULONG              Ulong        OPTIONAL,
485 	PLARGE_INTEGER		LargeInteger OPTIONAL,
486     LPWSAOVERLAPPED     Overlapped   OPTIONAL,
487     LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine OPTIONAL
488 );
489 
490 int SetSocketInformation(
491 	PSOCKET_INFORMATION Socket,
492 	ULONG				AfdInformationClass,
493     PBOOLEAN            Boolean      OPTIONAL,
494 	PULONG				Ulong		 OPTIONAL,
495 	PLARGE_INTEGER		LargeInteger OPTIONAL,
496     LPWSAOVERLAPPED     Overlapped   OPTIONAL,
497     LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine OPTIONAL
498 );
499 
500 int CreateContext(
501 	PSOCKET_INFORMATION Socket
502 );
503 
504 ULONG
505 NTAPI
506 SockAsyncThread(
507 	PVOID ThreadParam
508 );
509 
510 VOID
511 SockProcessAsyncSelect(
512 	PSOCKET_INFORMATION Socket,
513 	PASYNC_DATA AsyncData
514 );
515 
516 VOID
517 SockAsyncSelectCompletionRoutine(
518 	PVOID Context,
519 	PIO_STATUS_BLOCK IoStatusBlock
520 );
521 
522 BOOLEAN
523 SockCreateOrReferenceAsyncThread(
524 	VOID
525 );
526 
527 BOOLEAN SockGetAsyncSelectHelperAfdHandle(
528 	VOID
529 );
530 
531 VOID SockProcessQueuedAsyncSelect(
532 	PVOID Context,
533 	PIO_STATUS_BLOCK IoStatusBlock
534 );
535 
536 VOID
537 SockReenableAsyncSelectEvent (
538     IN PSOCKET_INFORMATION Socket,
539     IN ULONG Event
540     );
541 
542 typedef VOID (*PASYNC_COMPLETION_ROUTINE)(PVOID Context, PIO_STATUS_BLOCK IoStatusBlock);
543 
544 FORCEINLINE
545 DWORD
546 MsafdReturnWithErrno(NTSTATUS Status,
547                      LPINT Errno,
548                      DWORD Received,
549                      LPDWORD ReturnedBytes)
550 {
551     if (Errno)
552     {
553         *Errno = TranslateNtStatusError(Status);
554 
555         if (ReturnedBytes)
556             *ReturnedBytes = (*Errno == 0) ? Received : 0;
557 
558         return (*Errno == 0) ? 0 : SOCKET_ERROR;
559     }
560     else
561     {
562         DbgPrint("%s: Received invalid lpErrno pointer!\n", __FUNCTION__);
563 
564         if (ReturnedBytes)
565             *ReturnedBytes = (Status == STATUS_SUCCESS) ? Received : 0;
566 
567         return (Status == STATUS_SUCCESS) ? 0 : SOCKET_ERROR;
568     }
569 }
570 
571 #endif /* __MSAFD_H */
572