ReactOS  r76032
event.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Ancillary Function Driver
4  * FILE: afd/event.c
5  * PURPOSE: TDI event handlers
6  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7  * REVISIONS:
8  * CSH 01/09-2000 Created
9  */
10 
11 #include <afd.h>
12 
14  IN PVOID TdiEventContext,
16 {
17  AFD_DbgPrint(MAX_TRACE, ("Called.\n"));
18 
19  return STATUS_SUCCESS;
20 }
21 
22 
24  IN PVOID TdiEventContext,
25  IN CONNECTION_CONTEXT ConnectionContext,
26  IN LONG DisconnectDataLength,
27  IN PVOID DisconnectData,
28  IN LONG DisconnectInformationLength,
29  IN PVOID DisconnectInformation,
30  IN ULONG DisconnectFlags)
31 {
32  AFD_DbgPrint(MAX_TRACE, ("Called.\n"));
33 
34  return STATUS_SUCCESS;
35 }
36 
37 
39  IN PVOID TdiEventContext,
40  IN CONNECTION_CONTEXT ConnectionContext,
41  IN ULONG ReceiveFlags,
42  IN ULONG BytesIndicated,
43  IN ULONG BytesAvailable,
44  OUT ULONG *BytesTaken,
45  IN PVOID Tsdu,
46  OUT PIRP *IoRequestPacket)
47 {
48  PAFDFCB FCB = (PAFDFCB)TdiEventContext;
49  PVOID ReceiveBuffer;
50  PAFD_BUFFER Buffer;
51  KIRQL OldIrql;
52 
53  AFD_DbgPrint(MAX_TRACE, ("Called.\n"));
54 
55  AFD_DbgPrint(MID_TRACE, ("Receiving (%d) bytes on socket\n",
56  BytesAvailable));
57 
58  ReceiveBuffer = ExAllocatePool(NonPagedPool, BytesAvailable);
59  if (!ReceiveBuffer)
61 
62  /*Buffer = (PAFD_BUFFER)ExAllocateFromNPagedLookasideList(
63  &BufferLookasideList);*/
64  Buffer = (PAFD_BUFFER)ExAllocatePool(NonPagedPool, sizeof(AFD_BUFFER));
65  if (!Buffer) {
66  ExFreePool(ReceiveBuffer);
68  }
69 
70  /* Copy the data to a local buffer */
71  RtlCopyMemory(ReceiveBuffer, Tsdu, BytesAvailable);
72 
73  Buffer->Buffer.len = BytesAvailable;
74  Buffer->Buffer.buf = ReceiveBuffer;
75  Buffer->Offset = 0;
76 
77  KeAcquireSpinLock(&FCB->ReceiveQueueLock, &OldIrql);
78 
79  InsertTailList( &FCB->ReceiveQueue, &Buffer->ListEntry );
80 
81  TryToSatisfyRecvRequest( FCB, TRUE );
82 
83  KeReleaseSpinLock(&FCB->ReceiveQueueLock, OldIrql);
84 
85  *BytesTaken = BytesAvailable;
86 
87  AFD_DbgPrint(MAX_TRACE, ("Leaving.\n"));
88 
89  return STATUS_SUCCESS;
90 }
91 
92 
94  IN PVOID TdiEventContext,
95  IN CONNECTION_CONTEXT ConnectionContext,
96  IN ULONG ReceiveFlags,
97  IN ULONG BytesIndicated,
98  IN ULONG BytesAvailable,
99  OUT ULONG *BytesTaken,
100  IN PVOID Tsdu,
101  OUT PIRP *IoRequestPacket)
102 {
103  AFD_DbgPrint(MAX_TRACE, ("Called.\n"));
104 
105  return STATUS_SUCCESS;
106 }
107 
108 
110  IN PVOID TdiEventContext,
111  IN CONNECTION_CONTEXT ConnectionContext,
112  IN ULONG ReceiveFlags,
113  IN ULONG ReceiveLength,
115  IN PMDL Tsdu,
116  IN PVOID TsduDescriptor)
117 {
118  AFD_DbgPrint(MAX_TRACE, ("Called.\n"));
119 
120  return STATUS_SUCCESS;
121 }
122 
123 
125  IN PVOID TdiEventContext,
126  IN LONG SourceAddressLength,
128  IN LONG OptionsLength,
129  IN PVOID Options,
130  IN ULONG ReceiveDatagramFlags,
131  IN ULONG BytesIndicated,
132  IN ULONG BytesAvailable,
133  OUT ULONG *BytesTaken,
134  IN PVOID Tsdu,
135  OUT PIRP *IoRequestPacket)
136 {
137  PAFDFCB FCB = (PAFDFCB)TdiEventContext;
138  PAFD_READ_REQUEST ReadRequest;
139  PVOID ReceiveBuffer;
140  PAFD_BUFFER Buffer;
143  KIRQL OldIrql;
144  ULONG Count;
146 
147  AFD_DbgPrint(MAX_TRACE, ("Called.\n"));
148 
149  AFD_DbgPrint(MID_TRACE, ("Receiving (%d) bytes from (0x%X).\n",
150  BytesAvailable, *(PULONG)SourceAddress));
151 
152  ReceiveBuffer = ExAllocatePool(NonPagedPool, BytesAvailable);
153  if (!ReceiveBuffer)
155 
156  /*Buffer = (PAFD_BUFFER)ExAllocateFromNPagedLookasideList(
157  &BufferLookasideList);*/
158  Buffer = (PAFD_BUFFER)ExAllocatePool(NonPagedPool, sizeof(AFD_BUFFER));
159  if (!Buffer) {
160  ExFreePool(ReceiveBuffer);
162  }
163 
164  /* Copy the data to a local buffer */
165  RtlCopyMemory(ReceiveBuffer, Tsdu, BytesAvailable);
166 
167  Buffer->Buffer.len = BytesAvailable;
168  Buffer->Buffer.buf = ReceiveBuffer;
169  Buffer->Offset = 0;
170 
171  ExInterlockedInsertTailList(&FCB->ReceiveQueue,
172  &Buffer->ListEntry,
173  &FCB->ReceiveQueueLock);
174 
175  KeAcquireSpinLock(&FCB->ReceiveQueueLock, &OldIrql);
176 
177  if (CompleteIrp = !IsListEmpty(&FCB->ReadRequestQueue)) {
178  AFD_DbgPrint(MAX_TRACE, ("Satisfying read request.\n"));
179 
180  Entry = RemoveHeadList(&FCB->ReceiveQueue);
181  ReadRequest = CONTAINING_RECORD(Entry, AFD_READ_REQUEST, ListEntry);
182 
183  Status = FillWSABuffers(FCB,
184  ReadRequest->RecvFromRequest->Buffers,
185  ReadRequest->RecvFromRequest->BufferCount,
186  &Count,
187  FALSE); /* Packet oriented */
188  ReadRequest->RecvFromReply->NumberOfBytesRecvd = Count;
189  ReadRequest->RecvFromReply->Status = NO_ERROR;
190 
191  ReadRequest->Irp->IoStatus.Information = 0;
192  ReadRequest->Irp->IoStatus.Status = Status;
193  }
194 
195  KeReleaseSpinLock(&FCB->ReceiveQueueLock, OldIrql);
196 
197  if (CompleteIrp) {
198  AFD_DbgPrint(MAX_TRACE, ("Completing IRP at (0x%X).\n", ReadRequest->Irp));
199  IoCompleteRequest(ReadRequest->Irp, IO_NETWORK_INCREMENT);
200  *BytesTaken = BytesAvailable;
201  }
202 
203  AFD_DbgPrint(MAX_TRACE, ("Leaving.\n"));
204 
205  return STATUS_SUCCESS;
206 }
207 
208 
210  PAFDFCB FCB)
211 {
213 
214  assert(FCB->TdiAddressObject);
215 
216  /* Report errors for all types of sockets */
217  Status = TdiSetEventHandler(FCB->TdiAddressObject,
220  (PVOID)FCB);
221  if (!NT_SUCCESS(Status)) { return Status; }
222 
223  switch (FCB->SocketType) {
224  case SOCK_STREAM:
225  Status = TdiSetEventHandler(FCB->TdiAddressObject,
228  (PVOID)FCB);
229  if (!NT_SUCCESS(Status)) { return Status; }
230 
231  Status = TdiSetEventHandler(FCB->TdiAddressObject,
234  (PVOID)FCB);
235  if (!NT_SUCCESS(Status)) { return Status; }
236 
237  Status = TdiSetEventHandler(FCB->TdiAddressObject,
240  (PVOID)FCB);
241  if (!NT_SUCCESS(Status)) { return Status; }
242 
243  Status = TdiSetEventHandler(FCB->TdiAddressObject,
246  (PVOID)FCB);
247  if (!NT_SUCCESS(Status)) { return Status; }
248  break;
249 
250  case SOCK_DGRAM:
251  case SOCK_RAW:
252  Status = TdiSetEventHandler(FCB->TdiAddressObject,
255  (PVOID)FCB);
256  if (!NT_SUCCESS(Status)) { return Status; }
257  }
258  return STATUS_SUCCESS;
259 }
260 
261 
263  PAFDFCB FCB)
264 {
266 
267  Status = TdiSetEventHandler(FCB->TdiAddressObject,
269  NULL,
270  NULL);
271  if (!NT_SUCCESS(Status)) { return Status; }
272 
273  switch (FCB->SocketType) {
274  case SOCK_STREAM:
275  Status = TdiSetEventHandler(FCB->TdiAddressObject,
277  NULL,
278  NULL);
279  if (!NT_SUCCESS(Status)) { return Status; }
280 
281  Status = TdiSetEventHandler(FCB->TdiAddressObject,
283  NULL,
284  NULL);
285  if (!NT_SUCCESS(Status)) { return Status; }
286 
287  Status = TdiSetEventHandler(FCB->TdiAddressObject,
289  NULL,
290  NULL);
291  if (!NT_SUCCESS(Status)) { return Status; }
292 
293  Status = TdiSetEventHandler(FCB->TdiAddressObject,
295  NULL,
296  NULL);
297  if (!NT_SUCCESS(Status)) { return Status; }
298  break;
299 
300  case SOCK_DGRAM:
301  case SOCK_RAW:
302  Status = TdiSetEventHandler(FCB->TdiAddressObject,
304  NULL,
305  NULL);
306  if (!NT_SUCCESS(Status)) { return Status; }
307  }
308  return STATUS_SUCCESS;
309 }
310 
311 /* EOF */
DWORD *typedef PVOID
Definition: winlogon.h:52
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define MID_TRACE
Definition: debug.h:15
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
NTSTATUS AfdRegisterEventHandlers(PAFDFCB FCB)
Definition: event.c:209
struct _Entry Entry
Definition: kefuncs.h:640
Definition: cdfs.h:191
return STATUS_SUCCESS
Definition: btrfs.c:2664
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
#define TDI_EVENT_RECEIVE_EXPEDITED
Definition: tdikrnl.h:151
#define TDI_EVENT_DISCONNECT
Definition: tdikrnl.h:147
#define assert(x)
Definition: debug.h:53
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
LONG TDI_STATUS
Definition: tdi.h:36
NTSTATUS AfdEventDisconnect(IN PVOID TdiEventContext, IN CONNECTION_CONTEXT ConnectionContext, IN LONG DisconnectDataLength, IN PVOID DisconnectData, IN LONG DisconnectInformationLength, IN PVOID DisconnectInformation, IN ULONG DisconnectFlags)
Definition: event.c:23
#define InsertTailList(ListHead, Entry)
#define NO_ERROR
Definition: dderror.h:5
PVOID PMDL
Definition: usb.h:39
enum OPTION_FLAGS Options
Definition: stats.c:44
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
TDI_STATUS ClientEventReceiveExpedited(IN PVOID TdiEventContext, IN CONNECTION_CONTEXT ConnectionContext, IN ULONG ReceiveFlags, IN ULONG BytesIndicated, IN ULONG BytesAvailable, OUT ULONG *BytesTaken, IN PVOID Tsdu, OUT PIRP *IoRequestPacket)
Definition: event.c:93
VOID CompleteIrp(IN PIRP Irp, IN NTSTATUS Status, IN ULONG_PTR Information)
Definition: pnp.c:12
#define SOCK_RAW
Definition: winsock.h:337
NTSTATUS AfdEventReceive(IN PVOID TdiEventContext, IN CONNECTION_CONTEXT ConnectionContext, IN ULONG ReceiveFlags, IN ULONG BytesIndicated, IN ULONG BytesAvailable, OUT ULONG *BytesTaken, IN PVOID Tsdu, OUT PIRP *IoRequestPacket)
Definition: event.c:38
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define TDI_EVENT_RECEIVE
Definition: tdikrnl.h:149
NTSTATUS TdiSetEventHandler(PFILE_OBJECT FileObject, LONG EventType, PVOID Handler, PVOID Context)
Definition: tdi.c:539
#define FALSE
Definition: types.h:117
long LONG
Definition: pedump.c:60
PVOID CONNECTION_CONTEXT
Definition: tdi.h:37
#define AFD_DbgPrint(_t_, _x_)
Definition: debug.h:60
NTSTATUS AfdEventError(IN PVOID TdiEventContext, IN NTSTATUS Status)
Definition: event.c:13
smooth NULL
Definition: ftsmooth.c:557
#define IoCompleteRequest
Definition: irp.c:1177
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
#define TDI_EVENT_CHAINED_RECEIVE
Definition: tdikrnl.h:153
NTSTATUS AfdDeregisterEventHandlers(PAFDFCB FCB)
Definition: event.c:262
#define IO_NETWORK_INCREMENT
Definition: tcpip.h:43
unsigned char BOOLEAN
_Must_inspect_result_ __drv_aliasesMem _In_ PDEVICE_OBJECT _Inout_opt_ PVOID _In_opt_ ULONG _In_opt_ PLARGE_INTEGER StartingOffset
Definition: iofuncs.h:699
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
UINTN VOID * Buffer
Definition: acefiex.h:370
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
Definition: typedefs.h:117
#define TDI_EVENT_RECEIVE_DATAGRAM
Definition: tdikrnl.h:150
Status
Definition: gdiplustypes.h:24
NTSTATUS AfdEventReceiveDatagramHandler(IN PVOID TdiEventContext, IN LONG SourceAddressLength, IN PVOID SourceAddress, IN LONG OptionsLength, IN PVOID Options, IN ULONG ReceiveDatagramFlags, IN ULONG BytesIndicated, IN ULONG BytesAvailable, OUT ULONG *BytesTaken, IN PVOID Tsdu, OUT PIRP *IoRequestPacket)
Definition: event.c:124
LONG NTSTATUS
Definition: DriverTester.h:11
#define MAX_TRACE
Definition: debug.h:16
unsigned int * PULONG
Definition: retypes.h:1
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
NTSTATUS ClientEventChainedReceive(IN PVOID TdiEventContext, IN CONNECTION_CONTEXT ConnectionContext, IN ULONG ReceiveFlags, IN ULONG ReceiveLength, IN ULONG StartingOffset, IN PMDL Tsdu, IN PVOID TsduDescriptor)
Definition: event.c:109
#define OUT
Definition: typedefs.h:39
PVOID PIRP
Definition: usb.h:38
unsigned int ULONG
Definition: retypes.h:1
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:259
#define SOCK_STREAM
Definition: tcpip.h:118
#define SOCK_DGRAM
Definition: winsock.h:336
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS SourceAddress
Definition: iotypes.h:1089
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define TDI_EVENT_ERROR
Definition: tdikrnl.h:148