2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/net/afd/afd/write.c
5 * PURPOSE: Ancillary functions driver
6 * PROGRAMMER: Art Yerkes (ayerkes@speakeasy.net)
11 #include "tdi_proto.h"
15 static NTSTATUS NTAPI SendComplete
16 ( PDEVICE_OBJECT DeviceObject
,
19 NTSTATUS Status
= Irp
->IoStatus
.Status
;
20 PAFD_FCB FCB
= (PAFD_FCB
)Context
;
21 PLIST_ENTRY NextIrpEntry
;
23 PIO_STACK_LOCATION NextIrpSp
;
24 PAFD_SEND_INFO SendReq
= NULL
;
26 UINT TotalBytesCopied
= 0, SpaceAvail
, i
, CopySize
= 0;
29 * The Irp parameter passed in is the IRP of the stream between AFD and
30 * TDI driver. It's not very usefull to us. We need the IRPs of the stream
31 * between usermode and AFD. Those are chained from
32 * FCB->PendingIrpList[FUNCTION_SEND] and you'll see them in the code
33 * below as "NextIrp" ('cause they are the next usermode IRP to be
37 AFD_DbgPrint(MID_TRACE
,("Called, status %x, %d bytes used\n",
39 Irp
->IoStatus
.Information
));
41 ASSERT_IRQL(APC_LEVEL
);
43 if( !SocketAcquireStateLock( FCB
) )
44 return STATUS_FILE_CLOSED
;
46 FCB
->SendIrp
.InFlightRequest
= NULL
;
47 /* Request is not in flight any longer */
49 if( FCB
->State
== SOCKET_STATE_CLOSED
) {
50 /* Cleanup our IRP queue because the FCB is being destroyed */
51 while( !IsListEmpty( &FCB
->PendingIrpList
[FUNCTION_SEND
] ) ) {
52 NextIrpEntry
= RemoveHeadList(&FCB
->PendingIrpList
[FUNCTION_SEND
]);
53 NextIrp
= CONTAINING_RECORD(NextIrpEntry
, IRP
, Tail
.Overlay
.ListEntry
);
54 NextIrpSp
= IoGetCurrentIrpStackLocation( NextIrp
);
55 SendReq
= NextIrpSp
->Parameters
.DeviceIoControl
.Type3InputBuffer
;
56 NextIrp
->IoStatus
.Status
= STATUS_FILE_CLOSED
;
57 NextIrp
->IoStatus
.Information
= 0;
58 UnlockBuffers(SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
59 if( NextIrp
->MdlAddress
) UnlockRequest( NextIrp
, IoGetCurrentIrpStackLocation( NextIrp
) );
60 IoCompleteRequest( NextIrp
, IO_NETWORK_INCREMENT
);
62 SocketStateUnlock( FCB
);
63 return STATUS_FILE_CLOSED
;
66 if( !NT_SUCCESS(Status
) ) {
67 /* Complete all following send IRPs with error */
69 while( !IsListEmpty( &FCB
->PendingIrpList
[FUNCTION_SEND
] ) ) {
71 RemoveHeadList(&FCB
->PendingIrpList
[FUNCTION_SEND
]);
73 CONTAINING_RECORD(NextIrpEntry
, IRP
, Tail
.Overlay
.ListEntry
);
74 NextIrpSp
= IoGetCurrentIrpStackLocation( NextIrp
);
75 SendReq
= NextIrpSp
->Parameters
.DeviceIoControl
.Type3InputBuffer
;
77 UnlockBuffers( SendReq
->BufferArray
,
81 NextIrp
->IoStatus
.Status
= Status
;
82 NextIrp
->IoStatus
.Information
= 0;
84 if ( NextIrp
->MdlAddress
) UnlockRequest( NextIrp
, IoGetCurrentIrpStackLocation( NextIrp
) );
86 IoCompleteRequest( NextIrp
, IO_NETWORK_INCREMENT
);
89 SocketStateUnlock( FCB
);
91 return STATUS_SUCCESS
;
94 RtlMoveMemory( FCB
->Send
.Window
,
95 FCB
->Send
.Window
+ FCB
->Send
.BytesUsed
,
96 FCB
->Send
.BytesUsed
- Irp
->IoStatus
.Information
);
97 FCB
->Send
.BytesUsed
-= Irp
->IoStatus
.Information
;
99 if( !FCB
->Send
.BytesUsed
&&
100 !IsListEmpty( &FCB
->PendingIrpList
[FUNCTION_SEND
] ) ) {
102 RemoveHeadList(&FCB
->PendingIrpList
[FUNCTION_SEND
]);
104 CONTAINING_RECORD(NextIrpEntry
, IRP
, Tail
.Overlay
.ListEntry
);
105 NextIrpSp
= IoGetCurrentIrpStackLocation( NextIrp
);
106 SendReq
= NextIrpSp
->Parameters
.DeviceIoControl
.Type3InputBuffer
;
107 Map
= (PAFD_MAPBUF
)(SendReq
->BufferArray
+ SendReq
->BufferCount
);
109 AFD_DbgPrint(MID_TRACE
,("SendReq @ %x\n", SendReq
));
111 SpaceAvail
= FCB
->Send
.Size
- FCB
->Send
.BytesUsed
;
113 for( i
= 0; i
< SendReq
->BufferCount
; i
++ ) {
114 Map
[i
].BufferAddress
=
115 MmMapLockedPages( Map
[i
].Mdl
, KernelMode
);
117 CopySize
= MIN( SpaceAvail
,
118 SendReq
->BufferArray
[i
].len
);
120 RtlCopyMemory( FCB
->Send
.Window
+ FCB
->Send
.BytesUsed
,
121 Map
[i
].BufferAddress
,
124 MmUnmapLockedPages( Map
[i
].BufferAddress
, Map
[i
].Mdl
);
126 FCB
->Send
.BytesUsed
+= CopySize
;
127 TotalBytesCopied
+= CopySize
;
128 SpaceAvail
-= CopySize
;
132 /* Some data is still waiting */
133 if( FCB
->Send
.BytesUsed
) {
134 FCB
->PollState
&= ~AFD_EVENT_SEND
;
136 Status
= TdiSend( &FCB
->SendIrp
.InFlightRequest
,
137 FCB
->Connection
.Object
,
145 FCB
->PollState
|= AFD_EVENT_SEND
;
148 PollReeval( FCB
->DeviceExt
, FCB
->FileObject
);
150 if( TotalBytesCopied
> 0 ) {
151 UnlockBuffers( SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
153 if( Status
== STATUS_PENDING
)
154 Status
= STATUS_SUCCESS
;
156 AFD_DbgPrint(MID_TRACE
,("Dismissing request: %x\n", Status
));
158 return UnlockAndMaybeComplete( FCB
, Status
, NextIrp
, TotalBytesCopied
);
159 } else if( NextIrp
) {
160 AFD_DbgPrint(MID_TRACE
,("Could not do any more with Irp %x\n",
162 InsertHeadList( &FCB
->PendingIrpList
[FUNCTION_SEND
],
163 &NextIrp
->Tail
.Overlay
.ListEntry
);
166 SocketStateUnlock( FCB
);
168 return STATUS_SUCCESS
;
171 static NTSTATUS NTAPI PacketSocketSendComplete
172 ( PDEVICE_OBJECT DeviceObject
,
175 PAFD_FCB FCB
= (PAFD_FCB
)Context
;
176 PLIST_ENTRY NextIrpEntry
;
179 AFD_DbgPrint(MID_TRACE
,("Called, status %x, %d bytes used\n",
180 Irp
->IoStatus
.Status
,
181 Irp
->IoStatus
.Information
));
183 if( !SocketAcquireStateLock( FCB
) )
184 return STATUS_FILE_CLOSED
;
186 FCB
->SendIrp
.InFlightRequest
= NULL
;
187 /* Request is not in flight any longer */
189 FCB
->PollState
|= AFD_EVENT_SEND
;
190 PollReeval( FCB
->DeviceExt
, FCB
->FileObject
);
192 if( FCB
->State
== SOCKET_STATE_CLOSED
) {
193 /* Cleanup our IRP queue because the FCB is being destroyed */
194 while( !IsListEmpty( &FCB
->PendingIrpList
[FUNCTION_SEND
] ) ) {
195 NextIrpEntry
= RemoveHeadList(&FCB
->PendingIrpList
[FUNCTION_SEND
]);
196 NextIrp
= CONTAINING_RECORD(NextIrpEntry
, IRP
, Tail
.Overlay
.ListEntry
);
197 NextIrp
->IoStatus
.Status
= STATUS_FILE_CLOSED
;
198 NextIrp
->IoStatus
.Information
= 0;
199 if( NextIrp
->MdlAddress
) UnlockRequest( NextIrp
, IoGetCurrentIrpStackLocation( NextIrp
) );
200 IoCompleteRequest( NextIrp
, IO_NETWORK_INCREMENT
);
202 SocketStateUnlock( FCB
);
203 return STATUS_FILE_CLOSED
;
206 SocketStateUnlock( FCB
);
208 return STATUS_SUCCESS
;
212 AfdConnectedSocketWriteData(PDEVICE_OBJECT DeviceObject
, PIRP Irp
,
213 PIO_STACK_LOCATION IrpSp
, BOOLEAN Short
) {
214 NTSTATUS Status
= STATUS_SUCCESS
;
215 PFILE_OBJECT FileObject
= IrpSp
->FileObject
;
216 PAFD_FCB FCB
= FileObject
->FsContext
;
217 PAFD_SEND_INFO SendReq
;
219 UINT TotalBytesCopied
= 0, i
, CopySize
= 0,
220 SpaceAvail
= 0, TotalBytesEncountered
= 0;
222 AFD_DbgPrint(MID_TRACE
,("Called on %x\n", FCB
));
224 if( !SocketAcquireStateLock( FCB
) ) return LostSocket( Irp
);
226 if( FCB
->Flags
& AFD_ENDPOINT_CONNECTIONLESS
)
228 PAFD_SEND_INFO_UDP SendReq
;
229 PTDI_CONNECTION_INFORMATION TargetAddress
;
231 /* Check that the socket is bound */
232 if( FCB
->State
!= SOCKET_STATE_BOUND
)
233 return UnlockAndMaybeComplete( FCB
, STATUS_INVALID_PARAMETER
, Irp
,
236 if( !(SendReq
= LockRequest( Irp
, IrpSp
)) )
237 return UnlockAndMaybeComplete( FCB
, STATUS_NO_MEMORY
, Irp
, 0 );
239 /* Must lock buffers before handing off user data */
240 SendReq
->BufferArray
= LockBuffers( SendReq
->BufferArray
,
241 SendReq
->BufferCount
,
245 if( !SendReq
->BufferArray
) {
246 return UnlockAndMaybeComplete( FCB
, STATUS_ACCESS_VIOLATION
,
250 TdiBuildConnectionInfo( &TargetAddress
, FCB
->RemoteAddress
);
252 if( TargetAddress
) {
253 Status
= TdiSendDatagram
254 ( &FCB
->SendIrp
.InFlightRequest
,
255 FCB
->AddressFile
.Object
,
256 SendReq
->BufferArray
[0].buf
,
257 SendReq
->BufferArray
[0].len
,
260 PacketSocketSendComplete
,
263 ExFreePool( TargetAddress
);
264 } else Status
= STATUS_NO_MEMORY
;
266 if( Status
== STATUS_PENDING
) Status
= STATUS_SUCCESS
;
268 AFD_DbgPrint(MID_TRACE
,("Dismissing request: %x\n", Status
));
270 /* Even if we were pended, we're done with the user buffer at this
272 Information
= SendReq
->BufferArray
[0].len
;
273 UnlockBuffers(SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
274 return UnlockAndMaybeComplete( FCB
, Status
, Irp
, Information
);
277 if( !(SendReq
= LockRequest( Irp
, IrpSp
)) )
278 return UnlockAndMaybeComplete
279 ( FCB
, STATUS_NO_MEMORY
, Irp
, TotalBytesCopied
);
281 SendReq
->BufferArray
= LockBuffers( SendReq
->BufferArray
,
282 SendReq
->BufferCount
,
286 if( !SendReq
->BufferArray
) {
287 return UnlockAndMaybeComplete( FCB
, STATUS_ACCESS_VIOLATION
,
291 AFD_DbgPrint(MID_TRACE
,("Socket state %d\n", FCB
->State
));
293 if( FCB
->State
!= SOCKET_STATE_CONNECTED
) {
294 if( SendReq
->AfdFlags
& AFD_IMMEDIATE
) {
295 AFD_DbgPrint(MID_TRACE
,("Nonblocking\n"));
296 UnlockBuffers( SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
297 return UnlockAndMaybeComplete
298 ( FCB
, STATUS_CANT_WAIT
, Irp
, 0 );
300 AFD_DbgPrint(MID_TRACE
,("Queuing request\n"));
301 return LeaveIrpUntilLater( FCB
, Irp
, FUNCTION_SEND
);
305 AFD_DbgPrint(MID_TRACE
,("FCB->Send.BytesUsed = %d\n",
306 FCB
->Send
.BytesUsed
));
308 if( !FCB
->Send
.BytesUsed
) {
309 SpaceAvail
= FCB
->Send
.Size
- FCB
->Send
.BytesUsed
;
311 AFD_DbgPrint(MID_TRACE
,("We can accept %d bytes\n",
314 for( i
= 0; FCB
->Send
.BytesUsed
< FCB
->Send
.Size
&&
315 i
< SendReq
->BufferCount
; i
++ ) {
316 CopySize
= MIN( SpaceAvail
,
317 SendReq
->BufferArray
[i
].len
);
319 TotalBytesEncountered
+= SendReq
->BufferArray
[i
].len
;
321 AFD_DbgPrint(MID_TRACE
,("Copying Buffer %d, %x:%d to %x\n",
323 SendReq
->BufferArray
[i
].buf
,
325 FCB
->Send
.Window
+ FCB
->Send
.BytesUsed
));
327 RtlCopyMemory( FCB
->Send
.Window
+ FCB
->Send
.BytesUsed
,
328 SendReq
->BufferArray
[i
].buf
,
331 FCB
->Send
.BytesUsed
+= CopySize
;
332 TotalBytesCopied
+= CopySize
;
333 SpaceAvail
-= CopySize
;
336 if( TotalBytesEncountered
== 0 ) {
337 UnlockBuffers( SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
339 AFD_DbgPrint(MID_TRACE
,("Empty send\n"));
340 return UnlockAndMaybeComplete
341 ( FCB
, Status
, Irp
, TotalBytesCopied
);
344 AFD_DbgPrint(MID_TRACE
,("Completed %d bytes\n", TotalBytesCopied
));
346 if( TotalBytesCopied
> 0 ) {
347 UnlockBuffers( SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
349 Status
= TdiSend( &FCB
->SendIrp
.InFlightRequest
,
350 FCB
->Connection
.Object
,
358 if( Status
== STATUS_PENDING
)
359 Status
= STATUS_SUCCESS
;
361 AFD_DbgPrint(MID_TRACE
,("Dismissing request: %x (%d)\n",
362 Status
, TotalBytesCopied
));
364 return UnlockAndMaybeComplete
365 ( FCB
, Status
, Irp
, TotalBytesCopied
);
369 if( SendReq
->AfdFlags
& AFD_IMMEDIATE
) {
370 AFD_DbgPrint(MID_TRACE
,("Nonblocking\n"));
371 UnlockBuffers( SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
372 return UnlockAndMaybeComplete
373 ( FCB
, STATUS_CANT_WAIT
, Irp
, 0 );
375 AFD_DbgPrint(MID_TRACE
,("Queuing request\n"));
376 return LeaveIrpUntilLater( FCB
, Irp
, FUNCTION_SEND
);
381 AfdPacketSocketWriteData(PDEVICE_OBJECT DeviceObject
, PIRP Irp
,
382 PIO_STACK_LOCATION IrpSp
) {
383 NTSTATUS Status
= STATUS_SUCCESS
;
384 PTDI_CONNECTION_INFORMATION TargetAddress
;
385 PFILE_OBJECT FileObject
= IrpSp
->FileObject
;
386 PAFD_FCB FCB
= FileObject
->FsContext
;
387 PAFD_SEND_INFO_UDP SendReq
;
390 AFD_DbgPrint(MID_TRACE
,("Called on %x\n", FCB
));
392 if( !SocketAcquireStateLock( FCB
) ) return LostSocket( Irp
);
394 FCB
->PollState
&= ~AFD_EVENT_SEND
;
396 PollReeval( FCB
->DeviceExt
, FCB
->FileObject
);
398 /* Check that the socket is bound */
399 if( FCB
->State
!= SOCKET_STATE_BOUND
)
400 return UnlockAndMaybeComplete
401 ( FCB
, STATUS_INVALID_PARAMETER
, Irp
, 0 );
402 if( !(SendReq
= LockRequest( Irp
, IrpSp
)) )
403 return UnlockAndMaybeComplete
404 ( FCB
, STATUS_NO_MEMORY
, Irp
, 0 );
406 SendReq
->BufferArray
= LockBuffers( SendReq
->BufferArray
,
407 SendReq
->BufferCount
,
411 if( !SendReq
->BufferArray
)
412 return UnlockAndMaybeComplete( FCB
, STATUS_ACCESS_VIOLATION
,
416 (MID_TRACE
,("RemoteAddress #%d Type %d\n",
417 ((PTRANSPORT_ADDRESS
)SendReq
->RemoteAddress
)->
419 ((PTRANSPORT_ADDRESS
)SendReq
->RemoteAddress
)->
420 Address
[0].AddressType
));
422 TdiBuildConnectionInfo( &TargetAddress
,
423 ((PTRANSPORT_ADDRESS
)SendReq
->RemoteAddress
) );
425 /* Check the size of the Address given ... */
427 if( TargetAddress
) {
428 Status
= TdiSendDatagram
429 ( &FCB
->SendIrp
.InFlightRequest
,
430 FCB
->AddressFile
.Object
,
431 SendReq
->BufferArray
[0].buf
,
432 SendReq
->BufferArray
[0].len
,
435 PacketSocketSendComplete
,
438 ExFreePool( TargetAddress
);
439 } else Status
= STATUS_NO_MEMORY
;
441 if( Status
== STATUS_PENDING
) Status
= STATUS_SUCCESS
;
443 AFD_DbgPrint(MID_TRACE
,("Dismissing request: %x\n", Status
));
445 /* Even if we were pended, we're done with the user buffer at this
447 Information
= SendReq
->BufferArray
[0].len
;
448 UnlockBuffers(SendReq
->BufferArray
, SendReq
->BufferCount
, FALSE
);
449 return UnlockAndMaybeComplete( FCB
, Status
, Irp
, Information
);