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