xref: /reactos/drivers/network/afd/include/afd.h (revision c31d8d66)
1 /* $Id$
2  *
3  * COPYRIGHT:        See COPYING in the top level directory
4  * PROJECT:          ReactOS kernel
5  * FILE:             drivers/net/afd/include/afd.h
6  * PURPOSE:          Ancillary functions driver -- constants and structures
7  * PROGRAMMER:       Art Yerkes (ayerkes@speakeasy.net)
8  * UPDATE HISTORY:
9  * 20040630 Created
10  */
11 
12 #ifndef _AFD_H
13 #define _AFD_H
14 
15 #include <ntddk.h>
16 #include <ntifs.h>
17 #include <ndk/obtypes.h>
18 #include <ndk/obfuncs.h>
19 #include <ndk/mmtypes.h>
20 #include <ndk/mmfuncs.h>
21 #include <tdi.h>
22 #include <tdikrnl.h>
23 #include <tdiinfo.h>
24 #include <string.h>
25 #define _WINBASE_
26 #define _WINDOWS_H
27 #define _INC_WINDOWS
28 #include <windef.h>
29 #include <winsock2.h>
30 #include <afd/shared.h>
31 
32 #ifndef MIN
33 #define MIN(x,y) (((x)<(y))?(x):(y))
34 #endif
35 
36 #define IOCTL_TCP_QUERY_INFORMATION_EX \
37 	CTL_CODE(FILE_DEVICE_NETWORK, 0, METHOD_NEITHER, FILE_ANY_ACCESS)
38 
39 #define TL_INSTANCE 0
40 #define	IP_MIB_STATS_ID 1
41 #define	IP_MIB_ADDRTABLE_ENTRY_ID 0x102
42 
43 typedef struct IPSNMP_INFO {
44 	ULONG Forwarding;
45 	ULONG DefaultTTL;
46 	ULONG InReceives;
47 	ULONG InHdrErrors;
48 	ULONG InAddrErrors;
49 	ULONG ForwDatagrams;
50 	ULONG InUnknownProtos;
51 	ULONG InDiscards;
52 	ULONG InDelivers;
53 	ULONG OutRequests;
54 	ULONG RoutingDiscards;
55 	ULONG OutDiscards;
56 	ULONG OutNoRoutes;
57 	ULONG ReasmTimeout;
58 	ULONG ReasmReqds;
59 	ULONG ReasmOks;
60 	ULONG ReasmFails;
61 	ULONG FragOks;
62 	ULONG FragFails;
63 	ULONG FragCreates;
64 	ULONG NumIf;
65 	ULONG NumAddr;
66 	ULONG NumRoutes;
67 } IPSNMP_INFO, *PIPSNMP_INFO;
68 
69 typedef struct IPADDR_ENTRY {
70 	ULONG  Addr;
71 	ULONG  Index;
72 	ULONG  Mask;
73 	ULONG  BcastAddr;
74 	ULONG  ReasmSize;
75 	USHORT Context;
76 	USHORT Pad;
77 } IPADDR_ENTRY, *PIPADDR_ENTRY;
78 
79 #define DN2H(dw) \
80     ((((dw) & 0xFF000000L) >> 24) | \
81 	 (((dw) & 0x00FF0000L) >> 8) | \
82 	 (((dw) & 0x0000FF00L) << 8) | \
83 	 (((dw) & 0x000000FFL) << 24))
84 
85 #define SOCKET_STATE_INVALID_TRANSITION ((DWORD)-1)
86 #define SOCKET_STATE_CREATED            0
87 #define SOCKET_STATE_BOUND              1
88 #define SOCKET_STATE_CONNECTING         2
89 #define SOCKET_STATE_CONNECTED          3
90 #define SOCKET_STATE_LISTENING          4
91 #define SOCKET_STATE_MASK               0x0000ffff
92 #define SOCKET_STATE_EOF_READ           0x20000000
93 #define SOCKET_STATE_LOCKED             0x40000000
94 #define SOCKET_STATE_NEW                0x80000000
95 #define SOCKET_STATE_CLOSED             0x00000100
96 
97 #define FUNCTION_CONNECT                0
98 #define FUNCTION_RECV                   1
99 #define FUNCTION_SEND                   2
100 #define FUNCTION_PREACCEPT              3
101 #define FUNCTION_ACCEPT                 4
102 #define FUNCTION_CLOSE                  5
103 #define MAX_FUNCTIONS                   6
104 
105 #define IN_FLIGHT_REQUESTS              4
106 
107 #define EXTRA_LOCK_BUFFERS              2 /* Number of extra buffers needed
108 					   * for ancillary data on packet
109 					   * requests. */
110 
111 /* XXX This is a hack we should clean up later
112  * We do this in order to get some storage for the locked handle table
113  * Maybe I'll use some tail item in the irp instead */
114 #define AFD_HANDLES(x) ((PAFD_HANDLE)(x)->Exclusive)
115 #define SET_AFD_HANDLES(x,y) (((x)->Exclusive) = (ULONG_PTR)(y))
116 
117 typedef struct _AFD_MAPBUF {
118     PVOID BufferAddress;
119     PMDL  Mdl;
120 } AFD_MAPBUF, *PAFD_MAPBUF;
121 
122 typedef struct _AFD_DEVICE_EXTENSION {
123     PDEVICE_OBJECT DeviceObject;
124     LIST_ENTRY Polls;
125     KSPIN_LOCK Lock;
126 } AFD_DEVICE_EXTENSION, *PAFD_DEVICE_EXTENSION;
127 
128 typedef struct _AFD_ACTIVE_POLL {
129     LIST_ENTRY ListEntry;
130     PIRP Irp;
131     PAFD_DEVICE_EXTENSION DeviceExt;
132     KDPC TimeoutDpc;
133     KTIMER Timer;
134     PKEVENT EventObject;
135     BOOLEAN Exclusive;
136 } AFD_ACTIVE_POLL, *PAFD_ACTIVE_POLL;
137 
138 typedef struct _IRP_LIST {
139     LIST_ENTRY ListEntry;
140     PIRP Irp;
141 } IRP_LIST, *PIRP_LIST;
142 
143 typedef struct _AFD_TDI_OBJECT {
144     PFILE_OBJECT Object;
145     HANDLE Handle;
146 } AFD_TDI_OBJECT, *PAFD_TDI_OBJECT;
147 
148 typedef struct _AFD_TDI_OBJECT_QELT {
149     LIST_ENTRY ListEntry;
150     UINT Seq;
151     PTDI_CONNECTION_INFORMATION ConnInfo;
152     AFD_TDI_OBJECT Object;
153 } AFD_TDI_OBJECT_QELT, *PAFD_TDI_OBJECT_QELT;
154 
155 typedef struct _AFD_IN_FLIGHT_REQUEST {
156     PIRP InFlightRequest;
157     IO_STATUS_BLOCK Iosb;
158     PTDI_CONNECTION_INFORMATION ConnectionCallInfo;
159     PTDI_CONNECTION_INFORMATION ConnectionReturnInfo;
160 } AFD_IN_FLIGHT_REQUEST, *PAFD_IN_FLIGHT_REQUEST;
161 
162 typedef struct _AFD_DATA_WINDOW {
163     PCHAR Window;
164     UINT BytesUsed, Size, Content;
165 } AFD_DATA_WINDOW, *PAFD_DATA_WINDOW;
166 
167 typedef struct _AFD_STORED_DATAGRAM {
168     LIST_ENTRY ListEntry;
169     UINT Len;
170     PTRANSPORT_ADDRESS Address;
171     CHAR Buffer[1];
172 } AFD_STORED_DATAGRAM, *PAFD_STORED_DATAGRAM;
173 
174 typedef struct _AFD_FCB {
175     BOOLEAN Locked, Critical, Overread;
176     UINT State, Flags, BlockingMode, GroupID, GroupType;
177     KIRQL OldIrql;
178     UINT LockCount;
179     PVOID CurrentThread;
180     PFILE_OBJECT FileObject;
181     PAFD_DEVICE_EXTENSION DeviceExt;
182     BOOLEAN DelayedAccept;
183     UINT ConnSeq;
184     PTRANSPORT_ADDRESS LocalAddress, RemoteAddress;
185     PTDI_CONNECTION_INFORMATION AddressFrom, ConnectInfo;
186     AFD_TDI_OBJECT AddressFile, Connection;
187     AFD_IN_FLIGHT_REQUEST ConnectIrp, ListenIrp, ReceiveIrp, SendIrp;
188     AFD_DATA_WINDOW Send, Recv;
189     KMUTEX Mutex;
190     PKEVENT EventSelect;
191     DWORD EventSelectTriggers;
192     UNICODE_STRING TdiDeviceName;
193     PVOID Context;
194     DWORD PollState;
195     NTSTATUS PollStatus[FD_MAX_EVENTS];
196     UINT ContextSize;
197     PVOID ConnectData;
198     UINT FilledConnectData;
199     UINT ConnectDataSize;
200     PVOID DisconnectData;
201     UINT FilledDisconnectData;
202     UINT DisconnectDataSize;
203     PVOID ConnectOptions;
204     UINT FilledConnectOptions;
205     UINT ConnectOptionsSize;
206     PVOID DisconnectOptions;
207     UINT FilledDisconnectOptions;
208     UINT DisconnectOptionsSize;
209     LIST_ENTRY PendingIrpList[MAX_FUNCTIONS];
210     LIST_ENTRY DatagramList;
211     LIST_ENTRY PendingConnections;
212 } AFD_FCB, *PAFD_FCB;
213 
214 /* bind.c */
215 
216 NTSTATUS WarmSocketForBind( PAFD_FCB FCB );
217 NTSTATUS NTAPI
218 AfdBindSocket(PDEVICE_OBJECT DeviceObject, PIRP Irp,
219 	      PIO_STACK_LOCATION IrpSp);
220 
221 /* connect.c */
222 
223 NTSTATUS MakeSocketIntoConnection( PAFD_FCB FCB );
224 NTSTATUS WarmSocketForConnection( PAFD_FCB FCB );
225 NTSTATUS NTAPI
226 AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
227 		       PIO_STACK_LOCATION IrpSp);
228 NTSTATUS NTAPI
229 AfdGetConnectData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
230 	          PIO_STACK_LOCATION IrpSp);
231 NTSTATUS NTAPI
232 AfdSetConnectData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
233                   PIO_STACK_LOCATION IrpSp);
234 NTSTATUS NTAPI
235 AfdSetConnectDataSize(PDEVICE_OBJECT DeviceObject, PIRP Irp,
236                       PIO_STACK_LOCATION IrpSp);
237 NTSTATUS NTAPI
238 AfdGetConnectOptions(PDEVICE_OBJECT DeviceObject, PIRP Irp,
239 	             PIO_STACK_LOCATION IrpSp);
240 NTSTATUS NTAPI
241 AfdSetConnectOptions(PDEVICE_OBJECT DeviceObject, PIRP Irp,
242                      PIO_STACK_LOCATION IrpSp);
243 NTSTATUS NTAPI
244 AfdSetConnectOptionsSize(PDEVICE_OBJECT DeviceObject, PIRP Irp,
245                          PIO_STACK_LOCATION IrpSp);
246 
247 /* context.c */
248 
249 NTSTATUS NTAPI
250 AfdGetContext( PDEVICE_OBJECT DeviceObject, PIRP Irp,
251 	       PIO_STACK_LOCATION IrpSp );
252 NTSTATUS NTAPI
253 AfdGetContextSize( PDEVICE_OBJECT DeviceObject, PIRP Irp,
254 	           PIO_STACK_LOCATION IrpSp );
255 NTSTATUS NTAPI
256 AfdSetContext( PDEVICE_OBJECT DeviceObject, PIRP Irp,
257 	       PIO_STACK_LOCATION IrpSp );
258 
259 /* info.c */
260 
261 NTSTATUS NTAPI
262 AfdGetInfo( PDEVICE_OBJECT DeviceObject, PIRP Irp,
263 	    PIO_STACK_LOCATION IrpSp );
264 
265 NTSTATUS NTAPI
266 AfdSetInfo( PDEVICE_OBJECT DeviceObject, PIRP Irp,
267 	    PIO_STACK_LOCATION IrpSp );
268 
269 NTSTATUS NTAPI
270 AfdGetSockName( PDEVICE_OBJECT DeviceObject, PIRP Irp,
271                 PIO_STACK_LOCATION IrpSp );
272 
273 NTSTATUS NTAPI
274 AfdGetPeerName( PDEVICE_OBJECT DeviceObject, PIRP Irp,
275                 PIO_STACK_LOCATION IrpSp );
276 
277 /* listen.c */
278 NTSTATUS AfdWaitForListen( PDEVICE_OBJECT DeviceObject, PIRP Irp,
279 			   PIO_STACK_LOCATION IrpSp );
280 
281 NTSTATUS AfdListenSocket(PDEVICE_OBJECT DeviceObject, PIRP Irp,
282 			 PIO_STACK_LOCATION IrpSp);
283 
284 NTSTATUS AfdAccept( PDEVICE_OBJECT DeviceObject, PIRP Irp,
285 		    PIO_STACK_LOCATION IrpSp );
286 
287 /* lock.c */
288 
289 PAFD_WSABUF LockBuffers( PAFD_WSABUF Buf, UINT Count,
290 			 PVOID AddressBuf, PINT AddressLen,
291 			 BOOLEAN Write, BOOLEAN LockAddress );
292 VOID UnlockBuffers( PAFD_WSABUF Buf, UINT Count, BOOL Address );
293 BOOLEAN SocketAcquireStateLock( PAFD_FCB FCB );
294 NTSTATUS NTAPI UnlockAndMaybeComplete
295 ( PAFD_FCB FCB, NTSTATUS Status, PIRP Irp,
296   UINT Information );
297 VOID SocketStateUnlock( PAFD_FCB FCB );
298 NTSTATUS LostSocket( PIRP Irp );
299 PAFD_HANDLE LockHandles( PAFD_HANDLE HandleArray, UINT HandleCount );
300 VOID UnlockHandles( PAFD_HANDLE HandleArray, UINT HandleCount );
301 PVOID LockRequest( PIRP Irp, PIO_STACK_LOCATION IrpSp );
302 VOID UnlockRequest( PIRP Irp, PIO_STACK_LOCATION IrpSp );
303 
304 /* main.c */
305 
306 VOID OskitDumpBuffer( PCHAR Buffer, UINT Len );
307 NTSTATUS LeaveIrpUntilLater( PAFD_FCB FCB, PIRP Irp, UINT Function );
308 VOID DestroySocket( PAFD_FCB FCB );
309 VOID NTAPI AfdCancelHandler(PDEVICE_OBJECT DeviceObject,
310                  PIRP Irp);
311 
312 /* read.c */
313 
314 NTSTATUS NTAPI ReceiveComplete
315 ( PDEVICE_OBJECT DeviceObject,
316   PIRP Irp,
317   PVOID Context );
318 
319 NTSTATUS NTAPI PacketSocketRecvComplete
320 ( PDEVICE_OBJECT DeviceObject,
321   PIRP Irp,
322   PVOID Context );
323 
324 NTSTATUS NTAPI
325 AfdConnectedSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
326 			   PIO_STACK_LOCATION IrpSp, BOOLEAN Short);
327 NTSTATUS NTAPI
328 AfdPacketSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
329 			PIO_STACK_LOCATION IrpSp );
330 
331 /* select.c */
332 
333 NTSTATUS NTAPI
334 AfdSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
335 	   PIO_STACK_LOCATION IrpSp );
336 NTSTATUS NTAPI
337 AfdEventSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
338 		PIO_STACK_LOCATION IrpSp );
339 NTSTATUS NTAPI
340 AfdEnumEvents( PDEVICE_OBJECT DeviceObject, PIRP Irp,
341 	       PIO_STACK_LOCATION IrpSp );
342 VOID PollReeval( PAFD_DEVICE_EXTENSION DeviceObject, PFILE_OBJECT FileObject );
343 VOID KillSelectsForFCB( PAFD_DEVICE_EXTENSION DeviceExt,
344                         PFILE_OBJECT FileObject, BOOLEAN ExclusiveOnly );
345 VOID ZeroEvents( PAFD_HANDLE HandleArray,
346 		 UINT HandleCount );
347 VOID SignalSocket(
348    PAFD_ACTIVE_POLL Poll OPTIONAL, PIRP _Irp OPTIONAL,
349    PAFD_POLL_INFO PollReq, NTSTATUS Status);
350 
351 /* tdi.c */
352 
353 NTSTATUS TdiOpenAddressFile(
354     PUNICODE_STRING DeviceName,
355     PTRANSPORT_ADDRESS Name,
356     PHANDLE AddressHandle,
357     PFILE_OBJECT *AddressObject);
358 
359 NTSTATUS TdiAssociateAddressFile(
360   HANDLE AddressHandle,
361   PFILE_OBJECT ConnectionObject);
362 
363 NTSTATUS TdiDisassociateAddressFile(
364   PFILE_OBJECT ConnectionObject);
365 
366 NTSTATUS TdiListen
367 ( PIRP *Irp,
368   PFILE_OBJECT ConnectionObject,
369   PTDI_CONNECTION_INFORMATION *RequestConnectionInfo,
370   PTDI_CONNECTION_INFORMATION *ReturnConnectionInfo,
371   PIO_STATUS_BLOCK Iosb,
372   PIO_COMPLETION_ROUTINE  CompletionRoutine,
373   PVOID CompletionContext);
374 
375 NTSTATUS TdiReceive
376 ( PIRP *Irp,
377   PFILE_OBJECT ConnectionObject,
378   USHORT Flags,
379   PCHAR Buffer,
380   UINT BufferLength,
381   PIO_STATUS_BLOCK Iosb,
382   PIO_COMPLETION_ROUTINE  CompletionRoutine,
383   PVOID CompletionContext);
384 
385 NTSTATUS TdiSend
386 ( PIRP *Irp,
387   PFILE_OBJECT ConnectionObject,
388   USHORT Flags,
389   PCHAR Buffer,
390   UINT BufferLength,
391   PIO_STATUS_BLOCK Iosb,
392   PIO_COMPLETION_ROUTINE  CompletionRoutine,
393   PVOID CompletionContext);
394 
395 NTSTATUS TdiReceiveDatagram(
396     PIRP *Irp,
397     PFILE_OBJECT TransportObject,
398     USHORT Flags,
399     PCHAR Buffer,
400     UINT BufferLength,
401     PTDI_CONNECTION_INFORMATION From,
402     PIO_STATUS_BLOCK Iosb,
403     PIO_COMPLETION_ROUTINE CompletionRoutine,
404     PVOID CompletionContext);
405 
406 NTSTATUS TdiSendDatagram(
407     PIRP *Irp,
408     PFILE_OBJECT TransportObject,
409     PCHAR Buffer,
410     UINT BufferLength,
411     PTDI_CONNECTION_INFORMATION To,
412     PIO_STATUS_BLOCK Iosb,
413     PIO_COMPLETION_ROUTINE CompletionRoutine,
414     PVOID CompletionContext);
415 
416 NTSTATUS TdiQueryMaxDatagramLength(
417         PFILE_OBJECT FileObject,
418         PUINT MaxDatagramLength);
419 
420 /* write.c */
421 
422 NTSTATUS NTAPI
423 AfdConnectedSocketWriteData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
424 			    PIO_STACK_LOCATION IrpSp, BOOLEAN Short);
425 NTSTATUS NTAPI
426 AfdPacketSocketWriteData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
427 			 PIO_STACK_LOCATION IrpSp);
428 
429 #endif/*_AFD_H*/
430