2835f9c2ef004452c92a8def6b8a21a83a367da9
[reactos.git] / drivers / network / afd / afd / select.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/net/afd/afd/select.c
5 * PURPOSE: Ancillary functions driver
6 * PROGRAMMER: Art Yerkes (ayerkes@speakeasy.net)
7 * UPDATE HISTORY:
8 * 20040708 Created
9 */
10
11 #include "afd.h"
12
13 static VOID PrintEvents( ULONG Events ) {
14 #if DBG
15 char *events_list[] = { "AFD_EVENT_RECEIVE",
16 "AFD_EVENT_OOB_RECEIVE",
17 "AFD_EVENT_SEND",
18 "AFD_EVENT_DISCONNECT",
19 "AFD_EVENT_ABORT",
20 "AFD_EVENT_CLOSE",
21 "AFD_EVENT_CONNECT",
22 "AFD_EVENT_ACCEPT",
23 "AFD_EVENT_CONNECT_FAIL",
24 "AFD_EVENT_QOS",
25 "AFD_EVENT_GROUP_QOS",
26 NULL };
27 int i;
28
29 for( i = 0; events_list[i]; i++ )
30 if( Events & (1 << i) ) AFD_DbgPrint(MID_TRACE,("%s ", events_list[i] ));
31 #endif
32 }
33
34 static VOID CopyBackStatus( PAFD_HANDLE HandleArray,
35 UINT HandleCount ) {
36 UINT i;
37
38 for( i = 0; i < HandleCount; i++ ) {
39 HandleArray[i].Events = HandleArray[i].Status;
40 HandleArray[i].Status = 0;
41 }
42 }
43
44 VOID ZeroEvents( PAFD_HANDLE HandleArray,
45 UINT HandleCount ) {
46 UINT i;
47
48 for( i = 0; i < HandleCount; i++ ) {
49 HandleArray[i].Status = 0;
50 HandleArray[i].Events = 0;
51 }
52 }
53
54
55 /* you must pass either Poll OR Irp */
56 VOID SignalSocket(
57 PAFD_ACTIVE_POLL Poll OPTIONAL,
58 PIRP _Irp OPTIONAL,
59 PAFD_POLL_INFO PollReq,
60 NTSTATUS Status
61 )
62 {
63 UINT i;
64 PIRP Irp = _Irp ? _Irp : Poll->Irp;
65 AFD_DbgPrint(MID_TRACE,("Called (Status %x)\n", Status));
66
67 if (Poll)
68 {
69 KeCancelTimer( &Poll->Timer );
70 RemoveEntryList( &Poll->ListEntry );
71 ExFreePool( Poll );
72 }
73
74 Irp->IoStatus.Status = Status;
75 Irp->IoStatus.Information =
76 FIELD_OFFSET(AFD_POLL_INFO, Handles) + sizeof(AFD_HANDLE) * PollReq->HandleCount;
77 CopyBackStatus( PollReq->Handles,
78 PollReq->HandleCount );
79 for( i = 0; i < PollReq->HandleCount; i++ ) {
80 AFD_DbgPrint
81 (MAX_TRACE,
82 ("Handle(%x): Got %x,%x\n",
83 PollReq->Handles[i].Handle,
84 PollReq->Handles[i].Events,
85 PollReq->Handles[i].Status));
86 }
87 UnlockHandles( AFD_HANDLES(PollReq), PollReq->HandleCount );
88 if( Irp->MdlAddress ) UnlockRequest( Irp, IoGetCurrentIrpStackLocation( Irp ) );
89 AFD_DbgPrint(MID_TRACE,("Completing\n"));
90 (void)IoSetCancelRoutine(Irp, NULL);
91 IoCompleteRequest( Irp, IO_NETWORK_INCREMENT );
92 AFD_DbgPrint(MID_TRACE,("Done\n"));
93 }
94
95 static KDEFERRED_ROUTINE SelectTimeout;
96 static VOID NTAPI SelectTimeout( PKDPC Dpc,
97 PVOID DeferredContext,
98 PVOID SystemArgument1,
99 PVOID SystemArgument2 ) {
100 PAFD_ACTIVE_POLL Poll = DeferredContext;
101 PAFD_POLL_INFO PollReq;
102 PIRP Irp;
103 KIRQL OldIrql;
104 PAFD_DEVICE_EXTENSION DeviceExt;
105
106 UNREFERENCED_PARAMETER(Dpc);
107 UNREFERENCED_PARAMETER(SystemArgument1);
108 UNREFERENCED_PARAMETER(SystemArgument2);
109
110 AFD_DbgPrint(MID_TRACE,("Called\n"));
111
112 Irp = Poll->Irp;
113 DeviceExt = Poll->DeviceExt;
114 PollReq = Irp->AssociatedIrp.SystemBuffer;
115
116 ZeroEvents( PollReq->Handles, PollReq->HandleCount );
117
118 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
119 SignalSocket( Poll, NULL, PollReq, STATUS_TIMEOUT );
120 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
121
122 AFD_DbgPrint(MID_TRACE,("Timeout\n"));
123 }
124
125 VOID KillSelectsForFCB( PAFD_DEVICE_EXTENSION DeviceExt,
126 PFILE_OBJECT FileObject,
127 BOOLEAN OnlyExclusive ) {
128 KIRQL OldIrql;
129 PLIST_ENTRY ListEntry;
130 PAFD_ACTIVE_POLL Poll;
131 PIRP Irp;
132 PAFD_POLL_INFO PollReq;
133 PAFD_HANDLE HandleArray;
134 UINT i;
135
136 AFD_DbgPrint(MID_TRACE,("Killing selects that refer to %p\n", FileObject));
137
138 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
139
140 ListEntry = DeviceExt->Polls.Flink;
141 while ( ListEntry != &DeviceExt->Polls ) {
142 Poll = CONTAINING_RECORD(ListEntry, AFD_ACTIVE_POLL, ListEntry);
143 ListEntry = ListEntry->Flink;
144 Irp = Poll->Irp;
145 PollReq = Irp->AssociatedIrp.SystemBuffer;
146 HandleArray = AFD_HANDLES(PollReq);
147
148 for( i = 0; i < PollReq->HandleCount; i++ ) {
149 AFD_DbgPrint(MAX_TRACE,("Req: %u, This %p\n",
150 HandleArray[i].Handle, FileObject));
151 if( (PVOID)HandleArray[i].Handle == FileObject &&
152 (!OnlyExclusive || (OnlyExclusive && Poll->Exclusive)) ) {
153 ZeroEvents( PollReq->Handles, PollReq->HandleCount );
154 SignalSocket( Poll, NULL, PollReq, STATUS_CANCELLED );
155 }
156 }
157 }
158
159 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
160
161 AFD_DbgPrint(MID_TRACE,("Done\n"));
162 }
163
164 NTSTATUS NTAPI
165 AfdSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
166 PIO_STACK_LOCATION IrpSp ) {
167 NTSTATUS Status = STATUS_NO_MEMORY;
168 PAFD_FCB FCB;
169 PFILE_OBJECT FileObject;
170 PAFD_POLL_INFO PollReq = Irp->AssociatedIrp.SystemBuffer;
171 PAFD_DEVICE_EXTENSION DeviceExt = DeviceObject->DeviceExtension;
172 KIRQL OldIrql;
173 UINT i, Signalled = 0;
174 ULONG Exclusive = PollReq->Exclusive;
175
176 UNREFERENCED_PARAMETER(IrpSp);
177
178 AFD_DbgPrint(MID_TRACE,("Called (HandleCount %u Timeout %d)\n",
179 PollReq->HandleCount,
180 (INT)(PollReq->Timeout.QuadPart)));
181
182 SET_AFD_HANDLES(PollReq,
183 LockHandles( PollReq->Handles, PollReq->HandleCount ));
184
185 if( !AFD_HANDLES(PollReq) ) {
186 Irp->IoStatus.Status = STATUS_NO_MEMORY;
187 Irp->IoStatus.Information = 0;
188 IoCompleteRequest( Irp, IO_NETWORK_INCREMENT );
189 return STATUS_NO_MEMORY;
190 }
191
192 if( Exclusive ) {
193 for( i = 0; i < PollReq->HandleCount; i++ ) {
194 if( !AFD_HANDLES(PollReq)[i].Handle ) continue;
195
196 KillSelectsForFCB( DeviceExt,
197 (PFILE_OBJECT)AFD_HANDLES(PollReq)[i].Handle,
198 TRUE );
199 }
200 }
201
202 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
203
204 for( i = 0; i < PollReq->HandleCount; i++ ) {
205 if( !AFD_HANDLES(PollReq)[i].Handle ) continue;
206
207 FileObject = (PFILE_OBJECT)AFD_HANDLES(PollReq)[i].Handle;
208 FCB = FileObject->FsContext;
209
210 AFD_DbgPrint(MID_TRACE, ("AFD: Select Events: "));
211 PrintEvents( PollReq->Handles[i].Events );
212 AFD_DbgPrint(MID_TRACE,("\n"));
213
214 PollReq->Handles[i].Status =
215 PollReq->Handles[i].Events & FCB->PollState;
216 if( PollReq->Handles[i].Status ) {
217 AFD_DbgPrint(MID_TRACE,("Signalling %p with %x\n",
218 FCB, FCB->PollState));
219 Signalled++;
220 }
221 }
222
223 if( Signalled ) {
224 Status = STATUS_SUCCESS;
225 Irp->IoStatus.Status = Status;
226 SignalSocket( NULL, Irp, PollReq, Status );
227 } else {
228
229 PAFD_ACTIVE_POLL Poll = NULL;
230
231 Poll = ExAllocatePool( NonPagedPool, sizeof(AFD_ACTIVE_POLL) );
232
233 if (Poll){
234 Poll->Irp = Irp;
235 Poll->DeviceExt = DeviceExt;
236 Poll->Exclusive = Exclusive;
237
238 KeInitializeTimerEx( &Poll->Timer, NotificationTimer );
239
240 KeInitializeDpc( (PRKDPC)&Poll->TimeoutDpc, SelectTimeout, Poll );
241
242 InsertTailList( &DeviceExt->Polls, &Poll->ListEntry );
243
244 KeSetTimer( &Poll->Timer, PollReq->Timeout, &Poll->TimeoutDpc );
245
246 Status = STATUS_PENDING;
247 IoMarkIrpPending( Irp );
248 (void)IoSetCancelRoutine(Irp, AfdCancelHandler);
249 } else {
250 AFD_DbgPrint(MAX_TRACE, ("FIXME: do something with the IRP!\n"));
251 Status = STATUS_NO_MEMORY;
252 }
253 }
254
255 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
256
257 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
258
259 return Status;
260 }
261
262 NTSTATUS NTAPI
263 AfdEventSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
264 PIO_STACK_LOCATION IrpSp ) {
265 PFILE_OBJECT FileObject = IrpSp->FileObject;
266 NTSTATUS Status = STATUS_NO_MEMORY;
267 PAFD_EVENT_SELECT_INFO EventSelectInfo =
268 (PAFD_EVENT_SELECT_INFO)LockRequest( Irp, IrpSp, FALSE, NULL );
269 PAFD_FCB FCB = FileObject->FsContext;
270
271 UNREFERENCED_PARAMETER(DeviceObject);
272
273 if( !SocketAcquireStateLock( FCB ) ) {
274 return LostSocket( Irp );
275 }
276
277 if ( !EventSelectInfo ) {
278 return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY, Irp,
279 0 );
280 }
281 AFD_DbgPrint(MID_TRACE,("Called (Event %p Triggers %u)\n",
282 EventSelectInfo->EventObject,
283 EventSelectInfo->Events));
284
285 if( FCB->EventSelect ) ObDereferenceObject( FCB->EventSelect );
286 FCB->EventSelect = NULL;
287
288 if( EventSelectInfo->EventObject && EventSelectInfo->Events ) {
289 Status = ObReferenceObjectByHandle( (PVOID)EventSelectInfo->
290 EventObject,
291 EVENT_ALL_ACCESS,
292 *ExEventObjectType,
293 UserMode,
294 (PVOID *)&FCB->EventSelect,
295 NULL );
296
297 if( !NT_SUCCESS(Status) )
298 {
299 AFD_DbgPrint(MIN_TRACE,("Failed reference event (0x%x)\n", Status));
300 FCB->EventSelect = NULL;
301 }
302 else
303 FCB->EventSelectTriggers = EventSelectInfo->Events;
304 } else {
305 FCB->EventSelect = NULL;
306 FCB->EventSelectTriggers = 0;
307 Status = STATUS_SUCCESS;
308 }
309
310 if((FCB->EventSelect) &&
311 (FCB->PollState & (FCB->EventSelectTriggers & ~FCB->EventSelectDisabled)))
312 {
313 AFD_DbgPrint(MID_TRACE,("Setting event %p\n", FCB->EventSelect));
314
315 /* Set the application's event */
316 KeSetEvent( FCB->EventSelect, IO_NETWORK_INCREMENT, FALSE );
317 }
318
319 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
320
321 return UnlockAndMaybeComplete( FCB, Status, Irp,
322 0 );
323 }
324
325 NTSTATUS NTAPI
326 AfdEnumEvents( PDEVICE_OBJECT DeviceObject, PIRP Irp,
327 PIO_STACK_LOCATION IrpSp ) {
328 PFILE_OBJECT FileObject = IrpSp->FileObject;
329 PAFD_ENUM_NETWORK_EVENTS_INFO EnumReq =
330 (PAFD_ENUM_NETWORK_EVENTS_INFO)LockRequest( Irp, IrpSp, TRUE, NULL );
331 PAFD_FCB FCB = FileObject->FsContext;
332 PKEVENT UserEvent;
333 NTSTATUS Status;
334
335 UNREFERENCED_PARAMETER(DeviceObject);
336
337 AFD_DbgPrint(MID_TRACE,("Called (FCB %p)\n", FCB));
338
339 if( !SocketAcquireStateLock( FCB ) ) {
340 return LostSocket( Irp );
341 }
342
343 if ( !EnumReq ) {
344 return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY, Irp, 0 );
345 }
346
347 /* An event may optionally be provided for us to clear */
348 if (EnumReq->Event != NULL)
349 {
350 Status = ObReferenceObjectByHandle(EnumReq->Event,
351 EVENT_ALL_ACCESS,
352 *ExEventObjectType,
353 UserMode,
354 (PVOID *)&UserEvent,
355 NULL);
356 if (!NT_SUCCESS(Status))
357 {
358 AFD_DbgPrint(MIN_TRACE,("Unable to reference event %x\n", Status));
359 return UnlockAndMaybeComplete(FCB, Status, Irp, 0);
360 }
361
362 /* Clear the event */
363 KeClearEvent(UserEvent);
364 ObDereferenceObject(UserEvent);
365 }
366
367 /* Copy the poll state, masking out disabled events */
368 EnumReq->PollEvents = (FCB->PollState & ~FCB->EventSelectDisabled);
369 RtlCopyMemory( EnumReq->EventStatus,
370 FCB->PollStatus,
371 sizeof(EnumReq->EventStatus) );
372
373 /* Disable the events that triggered the select until the reenabling function is called */
374 FCB->EventSelectDisabled |= (FCB->PollState & FCB->EventSelectTriggers);
375
376 return UnlockAndMaybeComplete( FCB, STATUS_SUCCESS, Irp, 0 );
377 }
378
379 /* * * NOTE ALWAYS CALLED AT DISPATCH_LEVEL * * */
380 static BOOLEAN UpdatePollWithFCB( PAFD_ACTIVE_POLL Poll, PFILE_OBJECT FileObject ) {
381 UINT i;
382 PAFD_FCB FCB;
383 UINT Signalled = 0;
384 PAFD_POLL_INFO PollReq = Poll->Irp->AssociatedIrp.SystemBuffer;
385
386 ASSERT( KeGetCurrentIrql() == DISPATCH_LEVEL );
387
388 for( i = 0; i < PollReq->HandleCount; i++ ) {
389 if( !AFD_HANDLES(PollReq)[i].Handle ) continue;
390
391 FileObject = (PFILE_OBJECT)AFD_HANDLES(PollReq)[i].Handle;
392 FCB = FileObject->FsContext;
393
394 PollReq->Handles[i].Status = PollReq->Handles[i].Events & FCB->PollState;
395 if( PollReq->Handles[i].Status ) {
396 AFD_DbgPrint(MID_TRACE,("Signalling %p with %x\n",
397 FCB, FCB->PollState));
398 Signalled++;
399 }
400 }
401
402 return Signalled ? 1 : 0;
403 }
404
405 VOID PollReeval( PAFD_DEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject ) {
406 PAFD_ACTIVE_POLL Poll = NULL;
407 PLIST_ENTRY ThePollEnt = NULL;
408 PAFD_FCB FCB;
409 KIRQL OldIrql;
410 PAFD_POLL_INFO PollReq;
411
412 AFD_DbgPrint(MID_TRACE,("Called: DeviceExt %p FileObject %p\n",
413 DeviceExt, FileObject));
414
415 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
416
417 /* Take care of any event select signalling */
418 FCB = (PAFD_FCB)FileObject->FsContext;
419
420 if( !FCB ) {
421 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
422 return;
423 }
424
425 /* Now signal normal select irps */
426 ThePollEnt = DeviceExt->Polls.Flink;
427
428 while( ThePollEnt != &DeviceExt->Polls ) {
429 Poll = CONTAINING_RECORD( ThePollEnt, AFD_ACTIVE_POLL, ListEntry );
430 PollReq = Poll->Irp->AssociatedIrp.SystemBuffer;
431 AFD_DbgPrint(MID_TRACE,("Checking poll %p\n", Poll));
432
433 if( UpdatePollWithFCB( Poll, FileObject ) ) {
434 ThePollEnt = ThePollEnt->Flink;
435 AFD_DbgPrint(MID_TRACE,("Signalling socket\n"));
436 SignalSocket( Poll, NULL, PollReq, STATUS_SUCCESS );
437 } else
438 ThePollEnt = ThePollEnt->Flink;
439 }
440
441 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
442
443 if((FCB->EventSelect) &&
444 (FCB->PollState & (FCB->EventSelectTriggers & ~FCB->EventSelectDisabled)))
445 {
446 AFD_DbgPrint(MID_TRACE,("Setting event %p\n", FCB->EventSelect));
447
448 /* Set the application's event */
449 KeSetEvent( FCB->EventSelect, IO_NETWORK_INCREMENT, FALSE );
450 }
451
452 AFD_DbgPrint(MID_TRACE,("Leaving\n"));
453 }