e1ad87431abf4287c948efb01769735550020333
[reactos.git] / reactos / drivers / net / afd / afd / select.c
1 /* $Id$
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 #include "afd.h"
11 #include "tdi_proto.h"
12 #include "tdiconn.h"
13 #include "debug.h"
14
15 VOID PrintEvents( ULONG Events ) {
16 char *events_list[] = { "AFD_EVENT_RECEIVE",
17 "AFD_EVENT_OOB_RECEIVE",
18 "AFD_EVENT_SEND",
19 "AFD_EVENT_DISCONNECT",
20 "AFD_EVENT_ABORT",
21 "AFD_EVENT_CLOSE",
22 "AFD_EVENT_CONNECT",
23 "AFD_EVENT_ACCEPT",
24 "AFD_EVENT_CONNECT_FAIL",
25 "AFD_EVENT_QOS",
26 "AFD_EVENT_GROUP_QOS",
27 NULL };
28 int i;
29
30 for( i = 0; events_list[i]; i++ )
31 if( Events & (1 << i) ) DbgPrint("%s ", events_list[i] );
32 }
33
34 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 }
51
52 VOID RemoveSelect( PAFD_ACTIVE_POLL Poll ) {
53 AFD_DbgPrint(MID_TRACE,("Called\n"));
54
55 RemoveEntryList( &Poll->ListEntry );
56 KeCancelTimer( &Poll->Timer );
57
58 ExFreePool( Poll );
59
60 AFD_DbgPrint(MID_TRACE,("Done\n"));
61 }
62
63 VOID SignalSocket( PAFD_ACTIVE_POLL Poll, PAFD_POLL_INFO PollReq,
64 NTSTATUS Status ) {
65 UINT i;
66 PIRP Irp = Poll->Irp;
67 AFD_DbgPrint(MID_TRACE,("Called (Status %x)\n", Status));
68 KeCancelTimer( &Poll->Timer );
69 Poll->Irp->IoStatus.Status = Status;
70 Poll->Irp->IoStatus.Information =
71 FIELD_OFFSET(AFD_POLL_INFO, Handles) + sizeof(AFD_HANDLE) * PollReq->HandleCount;
72 CopyBackStatus( PollReq->Handles,
73 PollReq->HandleCount );
74 for( i = 0; i < PollReq->HandleCount; i++ ) {
75 AFD_DbgPrint
76 (MAX_TRACE,
77 ("Handle(%x): Got %x,%x\n",
78 PollReq->Handles[i].Handle,
79 PollReq->Handles[i].Events,
80 PollReq->Handles[i].Status));
81 }
82 UnlockHandles( AFD_HANDLES(PollReq), PollReq->HandleCount );
83 AFD_DbgPrint(MID_TRACE,("Completing\n"));
84 IoCompleteRequest( Irp, IO_NETWORK_INCREMENT );
85 RemoveEntryList( &Poll->ListEntry );
86 RemoveSelect( Poll );
87 AFD_DbgPrint(MID_TRACE,("Done\n"));
88 }
89
90 VOID SelectTimeout( PKDPC Dpc,
91 PVOID DeferredContext,
92 PVOID SystemArgument1,
93 PVOID SystemArgument2 ) {
94 PAFD_ACTIVE_POLL Poll = DeferredContext;
95 PAFD_POLL_INFO PollReq;
96 PIRP Irp;
97 KIRQL OldIrql;
98 PAFD_DEVICE_EXTENSION DeviceExt;
99
100 AFD_DbgPrint(MID_TRACE,("Called\n"));
101
102 Irp = Poll->Irp;
103 DeviceExt = Poll->DeviceExt;
104 PollReq = Irp->AssociatedIrp.SystemBuffer;
105
106 ZeroEvents( PollReq->Handles, PollReq->HandleCount );
107
108 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
109 SignalSocket( Poll, PollReq, STATUS_TIMEOUT );
110 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
111
112 AFD_DbgPrint(MID_TRACE,("Timeout\n"));
113 }
114
115 VOID KillSelectsForFCB( PAFD_DEVICE_EXTENSION DeviceExt,
116 PFILE_OBJECT FileObject,
117 BOOLEAN OnlyExclusive ) {
118 KIRQL OldIrql;
119 PLIST_ENTRY ListEntry;
120 PAFD_ACTIVE_POLL Poll;
121 PIRP Irp;
122 PAFD_POLL_INFO PollReq;
123 PAFD_HANDLE HandleArray;
124 UINT i;
125
126 AFD_DbgPrint(MID_TRACE,("Killing selects that refer to %x\n", FileObject));
127
128 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
129
130 ListEntry = DeviceExt->Polls.Flink;
131 while ( ListEntry != &DeviceExt->Polls ) {
132 Poll = CONTAINING_RECORD(ListEntry, AFD_ACTIVE_POLL, ListEntry);
133 ListEntry = ListEntry->Flink;
134 Irp = Poll->Irp;
135 PollReq = Irp->AssociatedIrp.SystemBuffer;
136 HandleArray = AFD_HANDLES(PollReq);
137
138 for( i = 0; i < PollReq->HandleCount; i++ ) {
139 AFD_DbgPrint(MAX_TRACE,("Req: %x, This %x\n",
140 HandleArray[i].Handle, FileObject));
141 if( (PVOID)HandleArray[i].Handle == FileObject &&
142 (!OnlyExclusive || (OnlyExclusive && Poll->Exclusive)) ) {
143 ZeroEvents( PollReq->Handles, PollReq->HandleCount );
144 SignalSocket( Poll, PollReq, STATUS_SUCCESS );
145 }
146 }
147 }
148
149 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
150
151 AFD_DbgPrint(MID_TRACE,("Done\n"));
152 }
153
154 NTSTATUS STDCALL
155 AfdSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
156 PIO_STACK_LOCATION IrpSp ) {
157 NTSTATUS Status = STATUS_NO_MEMORY;
158 PAFD_FCB FCB;
159 PFILE_OBJECT FileObject;
160 PAFD_POLL_INFO PollReq = Irp->AssociatedIrp.SystemBuffer;
161 PAFD_DEVICE_EXTENSION DeviceExt = DeviceObject->DeviceExtension;
162 PAFD_ACTIVE_POLL Poll = NULL;
163 UINT CopySize = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
164 UINT AllocSize =
165 CopySize + sizeof(AFD_ACTIVE_POLL) - sizeof(AFD_POLL_INFO);
166 KIRQL OldIrql;
167 UINT i, Signalled = 0;
168 ULONG Exclusive = PollReq->Exclusive;
169
170 AFD_DbgPrint(MID_TRACE,("Called (HandleCount %d Timeout %d)\n",
171 PollReq->HandleCount,
172 (INT)(PollReq->Timeout.QuadPart)));
173
174 SET_AFD_HANDLES(PollReq,
175 LockHandles( PollReq->Handles, PollReq->HandleCount ));
176
177 if( !AFD_HANDLES(PollReq) ) {
178 Irp->IoStatus.Status = STATUS_NO_MEMORY;
179 Irp->IoStatus.Information = 0;
180 IoCompleteRequest( Irp, IO_NETWORK_INCREMENT );
181 return Irp->IoStatus.Status;
182 }
183
184 if( Exclusive ) {
185 for( i = 0; i < PollReq->HandleCount; i++ ) {
186 if( !AFD_HANDLES(PollReq)[i].Handle ) continue;
187
188 KillSelectsForFCB( DeviceExt,
189 (PFILE_OBJECT)AFD_HANDLES(PollReq)[i].Handle,
190 TRUE );
191 }
192 }
193
194 ZeroEvents( PollReq->Handles,
195 PollReq->HandleCount );
196
197 Poll = ExAllocatePool( NonPagedPool, AllocSize );
198
199 if( Poll ) {
200 Poll->Irp = Irp;
201 Poll->DeviceExt = DeviceExt;
202 Poll->Exclusive = Exclusive;
203
204 KeInitializeTimerEx( &Poll->Timer, NotificationTimer );
205 KeSetTimer( &Poll->Timer, PollReq->Timeout, &Poll->TimeoutDpc );
206
207 KeInitializeDpc( (PRKDPC)&Poll->TimeoutDpc,
208 (PKDEFERRED_ROUTINE)SelectTimeout,
209 Poll );
210
211 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
212 InsertTailList( &DeviceExt->Polls, &Poll->ListEntry );
213
214 for( i = 0; i < PollReq->HandleCount; i++ ) {
215 if( !AFD_HANDLES(PollReq)[i].Handle ) continue;
216
217 FileObject = (PFILE_OBJECT)AFD_HANDLES(PollReq)[i].Handle;
218 FCB = FileObject->FsContext;
219
220 if( (FCB->PollState & AFD_EVENT_CLOSE) ||
221 (PollReq->Handles[i].Status & AFD_EVENT_CLOSE) ) {
222 AFD_HANDLES(PollReq)[i].Handle = 0;
223 PollReq->Handles[i].Events = 0;
224 PollReq->Handles[i].Status = AFD_EVENT_CLOSE;
225 Signalled++;
226 } else {
227 #ifdef DBG
228 DbgPrint("AFD: Select Events: ");
229 PrintEvents( PollReq->Handles[i].Events );
230 DbgPrint("\n");
231 #endif
232
233 PollReq->Handles[i].Status =
234 PollReq->Handles[i].Events & FCB->PollState;
235 if( PollReq->Handles[i].Status ) {
236 AFD_DbgPrint(MID_TRACE,("Signalling %x with %x\n",
237 FCB, FCB->PollState));
238 Signalled++;
239 }
240 }
241 }
242
243 if( Signalled ) {
244 Status = STATUS_SUCCESS;
245 Irp->IoStatus.Status = Status;
246 SignalSocket( Poll, PollReq, Status );
247 } else {
248 Status = STATUS_PENDING;
249 IoMarkIrpPending( Irp );
250 }
251
252 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
253 } else Status = STATUS_NO_MEMORY;
254
255 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
256
257 return Status;
258 }
259
260 NTSTATUS STDCALL
261 AfdEventSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
262 PIO_STACK_LOCATION IrpSp ) {
263 PFILE_OBJECT FileObject = IrpSp->FileObject;
264 NTSTATUS Status = STATUS_NO_MEMORY;
265 PAFD_EVENT_SELECT_INFO EventSelectInfo =
266 (PAFD_EVENT_SELECT_INFO)LockRequest( Irp, IrpSp );
267 PAFD_FCB FCB = FileObject->FsContext;
268
269 AFD_DbgPrint(MID_TRACE,("Called (Event %x Triggers %x)\n",
270 EventSelectInfo->EventObject,
271 EventSelectInfo->Events));
272
273 if( !SocketAcquireStateLock( FCB ) ) {
274 UnlockRequest( Irp, IrpSp );
275 return LostSocket( Irp, FALSE );
276 }
277
278 FCB->EventSelectTriggers = FCB->EventsFired = 0;
279 if( FCB->EventSelect ) ObDereferenceObject( FCB->EventSelect );
280 FCB->EventSelect = NULL;
281
282 if( EventSelectInfo->EventObject && EventSelectInfo->Events ) {
283 Status = ObReferenceObjectByHandle( (PVOID)EventSelectInfo->
284 EventObject,
285 FILE_ALL_ACCESS,
286 NULL,
287 KernelMode,
288 (PVOID *)&FCB->EventSelect,
289 NULL );
290
291 if( !NT_SUCCESS(Status) )
292 FCB->EventSelect = NULL;
293 else
294 FCB->EventSelectTriggers = EventSelectInfo->Events;
295 } else /* Work done, cancelling select */
296 Status = STATUS_SUCCESS;
297
298 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
299
300 return UnlockAndMaybeComplete( FCB, STATUS_SUCCESS, Irp,
301 0, NULL, TRUE );
302 }
303
304 NTSTATUS STDCALL
305 AfdEnumEvents( PDEVICE_OBJECT DeviceObject, PIRP Irp,
306 PIO_STACK_LOCATION IrpSp ) {
307 PFILE_OBJECT FileObject = IrpSp->FileObject;
308 PAFD_ENUM_NETWORK_EVENTS_INFO EnumReq =
309 (PAFD_ENUM_NETWORK_EVENTS_INFO)LockRequest( Irp, IrpSp );
310 PAFD_FCB FCB = FileObject->FsContext;
311
312 AFD_DbgPrint(MID_TRACE,("Called (FCB %x)\n", FCB));
313
314 if( !SocketAcquireStateLock( FCB ) ) {
315 UnlockRequest( Irp, IrpSp );
316 return LostSocket( Irp, FALSE );
317 }
318
319 EnumReq->PollEvents = FCB->PollState;
320 RtlZeroMemory( EnumReq->EventStatus, sizeof(EnumReq->EventStatus) );
321
322 return UnlockAndMaybeComplete( FCB, STATUS_SUCCESS, Irp,
323 0, NULL, TRUE );
324 }
325
326 /* * * NOTE ALWAYS CALLED AT DISPATCH_LEVEL * * */
327 BOOLEAN UpdatePollWithFCB( PAFD_ACTIVE_POLL Poll, PFILE_OBJECT FileObject ) {
328 UINT i;
329 PAFD_FCB FCB;
330 UINT Signalled = 0;
331 PAFD_POLL_INFO PollReq = Poll->Irp->AssociatedIrp.SystemBuffer;
332
333 ASSERT( KeGetCurrentIrql() == DISPATCH_LEVEL );
334
335 for( i = 0; i < PollReq->HandleCount; i++ ) {
336 if( !AFD_HANDLES(PollReq)[i].Handle ) continue;
337
338 FileObject = (PFILE_OBJECT)AFD_HANDLES(PollReq)[i].Handle;
339 FCB = FileObject->FsContext;
340
341 if( (FCB->PollState & AFD_EVENT_CLOSE) ||
342 (PollReq->Handles[i].Status & AFD_EVENT_CLOSE) ) {
343 AFD_HANDLES(PollReq)[i].Handle = 0;
344 PollReq->Handles[i].Events = 0;
345 PollReq->Handles[i].Status = AFD_EVENT_CLOSE;
346 Signalled++;
347 } else {
348 PollReq->Handles[i].Status =
349 PollReq->Handles[i].Events & FCB->PollState;
350 if( PollReq->Handles[i].Status ) {
351 AFD_DbgPrint(MID_TRACE,("Signalling %x with %x\n",
352 FCB, FCB->PollState));
353 Signalled++;
354 }
355 }
356 }
357
358 return Signalled ? 1 : 0;
359 }
360
361 VOID PollReeval( PAFD_DEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject ) {
362 PAFD_ACTIVE_POLL Poll = NULL;
363 PLIST_ENTRY ThePollEnt = NULL;
364 PAFD_FCB FCB;
365 KIRQL OldIrql;
366 PAFD_POLL_INFO PollReq;
367 PKEVENT EventSelect = NULL;
368
369 AFD_DbgPrint(MID_TRACE,("Called: DeviceExt %x FileObject %x\n",
370 DeviceExt, FileObject));
371
372 KeAcquireSpinLock( &DeviceExt->Lock, &OldIrql );
373
374 /* Take care of any event select signalling */
375 FCB = (PAFD_FCB)FileObject->FsContext;
376
377 /* Not sure if i can do this at DISPATCH_LEVEL ... try it at passive */
378 AFD_DbgPrint(MID_TRACE,("Current State: %x, Events Fired: %x, "
379 "Select Triggers %x\n",
380 FCB->PollState, FCB->EventsFired,
381 FCB->EventSelectTriggers));
382 if( FCB->PollState & ~FCB->EventsFired & FCB->EventSelectTriggers ) {
383 FCB->EventsFired |= FCB->PollState;
384 EventSelect = FCB->EventSelect;
385 }
386
387 if( !FCB ) {
388 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
389 return;
390 }
391
392 /* Now signal normal select irps */
393 ThePollEnt = DeviceExt->Polls.Flink;
394
395 while( ThePollEnt != &DeviceExt->Polls ) {
396 Poll = CONTAINING_RECORD( ThePollEnt, AFD_ACTIVE_POLL, ListEntry );
397 PollReq = Poll->Irp->AssociatedIrp.SystemBuffer;
398 AFD_DbgPrint(MID_TRACE,("Checking poll %x\n", Poll));
399
400 if( UpdatePollWithFCB( Poll, FileObject ) ) {
401 ThePollEnt = ThePollEnt->Flink;
402 AFD_DbgPrint(MID_TRACE,("Signalling socket\n"));
403 SignalSocket( Poll, PollReq, STATUS_SUCCESS );
404 } else
405 ThePollEnt = ThePollEnt->Flink;
406 }
407
408 KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
409
410 AFD_DbgPrint(MID_TRACE,("Setting event %x\n", EventSelect));
411 if( EventSelect ) KeSetEvent( EventSelect, IO_NETWORK_INCREMENT, FALSE );
412
413 AFD_DbgPrint(MID_TRACE,("Leaving\n"));
414 }