NTSTATUS NTAPI ReceiveComplete ( PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context ) { PAFD_FCB FCB = (PAFD_FCB)Context; PLIST_ENTRY NextIrpEntry; PIRP NextIrp; PAFD_RECV_INFO RecvReq; PIO_STACK_LOCATION NextIrpSp; UNREFERENCED_PARAMETER(DeviceObject); AFD_DbgPrint(MID_TRACE,("Called\n")); if( !SocketAcquireStateLock( FCB ) ) return STATUS_FILE_CLOSED; ASSERT(FCB->ReceiveIrp.InFlightRequest == Irp); FCB->ReceiveIrp.InFlightRequest = NULL; if( FCB->State == SOCKET_STATE_CLOSED ) { /* Cleanup our IRP queue because the FCB is being destroyed */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_RECV] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_RECV]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation(NextIrp); RecvReq = GetLockedData(NextIrp, NextIrpSp); NextIrp->IoStatus.Status = STATUS_FILE_CLOSED; NextIrp->IoStatus.Information = 0; UnlockBuffers(RecvReq->BufferArray, RecvReq->BufferCount, FALSE); if( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } SocketStateUnlock( FCB ); return STATUS_FILE_CLOSED; } else if( FCB->State == SOCKET_STATE_LISTENING ) { AFD_DbgPrint(MIN_TRACE,("!!! LISTENER GOT A RECEIVE COMPLETE !!!\n")); SocketStateUnlock( FCB ); return STATUS_INVALID_PARAMETER; } HandleReceiveComplete( FCB, Irp->IoStatus.Status, Irp->IoStatus.Information ); ReceiveActivity( FCB, NULL ); SocketStateUnlock( FCB ); return STATUS_SUCCESS; }
NTSTATUS NTAPI PacketSocketRecvComplete( PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context ) { NTSTATUS Status = STATUS_SUCCESS; PAFD_FCB FCB = Context; PIRP NextIrp; PIO_STACK_LOCATION NextIrpSp; PLIST_ENTRY ListEntry; PAFD_RECV_INFO RecvReq; PAFD_STORED_DATAGRAM DatagramRecv; UINT DGSize = Irp->IoStatus.Information + sizeof( AFD_STORED_DATAGRAM ); PLIST_ENTRY NextIrpEntry, DatagramRecvEntry; UNREFERENCED_PARAMETER(DeviceObject); AFD_DbgPrint(MID_TRACE,("Called on %p\n", FCB)); if( !SocketAcquireStateLock( FCB ) ) return STATUS_FILE_CLOSED; ASSERT(FCB->ReceiveIrp.InFlightRequest == Irp); FCB->ReceiveIrp.InFlightRequest = NULL; if( FCB->State == SOCKET_STATE_CLOSED ) { /* Cleanup our IRP queue because the FCB is being destroyed */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_RECV] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_RECV]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); RecvReq = GetLockedData(NextIrp, NextIrpSp); NextIrp->IoStatus.Status = STATUS_FILE_CLOSED; NextIrp->IoStatus.Information = 0; UnlockBuffers(RecvReq->BufferArray, RecvReq->BufferCount, CheckUnlockExtraBuffers(FCB, NextIrpSp)); if( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } /* Free all items on the datagram list */ while( !IsListEmpty( &FCB->DatagramList ) ) { DatagramRecvEntry = RemoveHeadList(&FCB->DatagramList); DatagramRecv = CONTAINING_RECORD(DatagramRecvEntry, AFD_STORED_DATAGRAM, ListEntry); ExFreePool( DatagramRecv->Address ); ExFreePool( DatagramRecv ); } SocketStateUnlock( FCB ); return STATUS_FILE_CLOSED; } if (Irp->IoStatus.Status != STATUS_SUCCESS) { SocketStateUnlock(FCB); return Irp->IoStatus.Status; } if (FCB->TdiReceiveClosed) { SocketStateUnlock(FCB); return STATUS_FILE_CLOSED; } DatagramRecv = ExAllocatePool( NonPagedPool, DGSize ); if( DatagramRecv ) { DatagramRecv->Len = Irp->IoStatus.Information; RtlCopyMemory( DatagramRecv->Buffer, FCB->Recv.Window, DatagramRecv->Len ); AFD_DbgPrint(MID_TRACE,("Received (A %p)\n", FCB->AddressFrom->RemoteAddress)); DatagramRecv->Address = TaCopyTransportAddress( FCB->AddressFrom->RemoteAddress ); if( !DatagramRecv->Address ) Status = STATUS_NO_MEMORY; } else Status = STATUS_NO_MEMORY; if( !NT_SUCCESS( Status ) ) { if( DatagramRecv ) ExFreePool( DatagramRecv ); SocketStateUnlock( FCB ); return Status; } else { FCB->Recv.Content += DatagramRecv->Len; InsertTailList( &FCB->DatagramList, &DatagramRecv->ListEntry ); } /* Satisfy as many requests as we can */ while( !IsListEmpty( &FCB->DatagramList ) && !IsListEmpty( &FCB->PendingIrpList[FUNCTION_RECV] ) ) { AFD_DbgPrint(MID_TRACE,("Looping trying to satisfy request\n")); ListEntry = RemoveHeadList( &FCB->DatagramList ); DatagramRecv = CONTAINING_RECORD( ListEntry, AFD_STORED_DATAGRAM, ListEntry ); ListEntry = RemoveHeadList( &FCB->PendingIrpList[FUNCTION_RECV] ); NextIrp = CONTAINING_RECORD( ListEntry, IRP, Tail.Overlay.ListEntry ); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); RecvReq = GetLockedData(NextIrp, NextIrpSp); AFD_DbgPrint(MID_TRACE,("RecvReq: %p, DatagramRecv: %p\n", RecvReq, DatagramRecv)); AFD_DbgPrint(MID_TRACE,("Satisfying\n")); Status = SatisfyPacketRecvRequest ( FCB, NextIrp, DatagramRecv, (PUINT)&NextIrp->IoStatus.Information ); if (RecvReq->TdiFlags & TDI_RECEIVE_PEEK) { InsertHeadList(&FCB->DatagramList, &DatagramRecv->ListEntry); } AFD_DbgPrint(MID_TRACE,("Unlocking\n")); UnlockBuffers( RecvReq->BufferArray, RecvReq->BufferCount, CheckUnlockExtraBuffers(FCB, NextIrpSp) ); if ( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); AFD_DbgPrint(MID_TRACE,("Completing\n")); (void)IoSetCancelRoutine(NextIrp, NULL); NextIrp->IoStatus.Status = Status; IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } if( !IsListEmpty( &FCB->DatagramList ) && IsListEmpty(&FCB->PendingIrpList[FUNCTION_RECV]) ) { AFD_DbgPrint(MID_TRACE,("Signalling\n")); FCB->PollState |= AFD_EVENT_RECEIVE; FCB->PollStatus[FD_READ_BIT] = STATUS_SUCCESS; PollReeval( FCB->DeviceExt, FCB->FileObject ); } else FCB->PollState &= ~AFD_EVENT_RECEIVE; if( NT_SUCCESS(Irp->IoStatus.Status) ) { /* Now relaunch the datagram request */ Status = TdiReceiveDatagram ( &FCB->ReceiveIrp.InFlightRequest, FCB->AddressFile.Object, 0, FCB->Recv.Window, FCB->Recv.Size, FCB->AddressFrom, &FCB->ReceiveIrp.Iosb, PacketSocketRecvComplete, FCB ); } SocketStateUnlock( FCB ); return STATUS_SUCCESS; }
static NTSTATUS NTAPI SatisfyPacketRecvRequest( PAFD_FCB FCB, PIRP Irp, PAFD_STORED_DATAGRAM DatagramRecv, PUINT TotalBytesCopied ) { NTSTATUS Status = STATUS_SUCCESS; PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp ); PAFD_RECV_INFO RecvReq = GetLockedData(Irp, IrpSp); UINT BytesToCopy = 0, BytesAvailable = DatagramRecv->Len, AddrLen = 0; PAFD_MAPBUF Map; BOOLEAN ExtraBuffers = CheckUnlockExtraBuffers(FCB, IrpSp); Map = (PAFD_MAPBUF)(RecvReq->BufferArray + RecvReq->BufferCount + (ExtraBuffers ? EXTRA_LOCK_BUFFERS : 0)); BytesToCopy = MIN( RecvReq->BufferArray[0].len, BytesAvailable ); AFD_DbgPrint(MID_TRACE,("BytesToCopy: %u len %u\n", BytesToCopy, RecvReq->BufferArray[0].len)); if( Map[0].Mdl ) { /* Copy the address */ if( ExtraBuffers && Map[1].Mdl && Map[2].Mdl ) { AFD_DbgPrint(MID_TRACE,("Checking TAAddressCount\n")); if( DatagramRecv->Address->TAAddressCount != 1 ) { AFD_DbgPrint (MIN_TRACE, ("Wierd address count %d\n", DatagramRecv->Address->TAAddressCount)); } AFD_DbgPrint(MID_TRACE,("Computing addr len\n")); AddrLen = MIN(DatagramRecv->Address->Address->AddressLength + sizeof(USHORT), RecvReq->BufferArray[1].len); AFD_DbgPrint(MID_TRACE,("Copying %u bytes of address\n", AddrLen)); Map[1].BufferAddress = MmMapLockedPages( Map[1].Mdl, KernelMode ); AFD_DbgPrint(MID_TRACE,("Done mapping, copying address\n")); RtlCopyMemory( Map[1].BufferAddress, &DatagramRecv->Address->Address->AddressType, AddrLen ); MmUnmapLockedPages( Map[1].BufferAddress, Map[1].Mdl ); AFD_DbgPrint(MID_TRACE,("Copying address len\n")); Map[2].BufferAddress = MmMapLockedPages( Map[2].Mdl, KernelMode ); *((PINT)Map[2].BufferAddress) = AddrLen; MmUnmapLockedPages( Map[2].BufferAddress, Map[2].Mdl ); } AFD_DbgPrint(MID_TRACE,("Mapping data buffer pages\n")); Map[0].BufferAddress = MmMapLockedPages( Map[0].Mdl, KernelMode ); AFD_DbgPrint(MID_TRACE,("Buffer %d: %p:%u\n", 0, Map[0].BufferAddress, BytesToCopy)); RtlCopyMemory( Map[0].BufferAddress, DatagramRecv->Buffer, BytesToCopy ); MmUnmapLockedPages( Map[0].BufferAddress, Map[0].Mdl ); *TotalBytesCopied = BytesToCopy; } if (*TotalBytesCopied == DatagramRecv->Len) { /* We copied the whole datagram */ Status = Irp->IoStatus.Status = STATUS_SUCCESS; } else { /* We only copied part of the datagram */ Status = Irp->IoStatus.Status = STATUS_BUFFER_OVERFLOW; } Irp->IoStatus.Information = *TotalBytesCopied; if (!(RecvReq->TdiFlags & TDI_RECEIVE_PEEK)) { FCB->Recv.Content -= DatagramRecv->Len; ExFreePool( DatagramRecv->Address ); ExFreePool( DatagramRecv ); } AFD_DbgPrint(MID_TRACE,("Done\n")); return Status; }
static NTSTATUS ReceiveActivity( PAFD_FCB FCB, PIRP Irp ) { PLIST_ENTRY NextIrpEntry; PIRP NextIrp; PIO_STACK_LOCATION NextIrpSp; PAFD_RECV_INFO RecvReq; UINT TotalBytesCopied = 0, RetBytesCopied = 0; NTSTATUS Status = STATUS_SUCCESS, RetStatus = STATUS_PENDING; AFD_DbgPrint(MID_TRACE,("%p %p\n", FCB, Irp)); AFD_DbgPrint(MID_TRACE,("FCB %p Receive data waiting %u\n", FCB, FCB->Recv.Content)); if( CantReadMore( FCB ) ) { /* Success here means that we got an EOF. Complete a pending read * with zero bytes if we haven't yet overread, then kill the others. */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_RECV] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_RECV]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); RecvReq = GetLockedData(NextIrp, NextIrpSp); AFD_DbgPrint(MID_TRACE,("Completing recv %p (%u)\n", NextIrp, TotalBytesCopied)); UnlockBuffers( RecvReq->BufferArray, RecvReq->BufferCount, FALSE ); if (FCB->Overread && FCB->LastReceiveStatus == STATUS_SUCCESS) { /* Overread after a graceful disconnect so complete with an error */ Status = STATUS_FILE_CLOSED; } else { /* Unexpected disconnect by the remote host or initial read after a graceful disconnnect */ Status = FCB->LastReceiveStatus; } NextIrp->IoStatus.Status = Status; NextIrp->IoStatus.Information = 0; if( NextIrp == Irp ) RetStatus = Status; if( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); FCB->Overread = TRUE; } } else { /* Kick the user that receive would be possible now */ /* XXX Not implemented yet */ AFD_DbgPrint(MID_TRACE,("FCB %p Receive data waiting %u\n", FCB, FCB->Recv.Content)); /*OskitDumpBuffer( FCB->Recv.Window, FCB->Recv.Content );*/ /* Try to clear some requests */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_RECV] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_RECV]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); RecvReq = GetLockedData(NextIrp, NextIrpSp); AFD_DbgPrint(MID_TRACE,("RecvReq @ %p\n", RecvReq)); Status = TryToSatisfyRecvRequestFromBuffer ( FCB, RecvReq, &TotalBytesCopied ); if( Status == STATUS_PENDING ) { AFD_DbgPrint(MID_TRACE,("Ran out of data for %p\n", NextIrp)); InsertHeadList(&FCB->PendingIrpList[FUNCTION_RECV], &NextIrp->Tail.Overlay.ListEntry); break; } else { AFD_DbgPrint(MID_TRACE,("Completing recv %p (%u)\n", NextIrp, TotalBytesCopied)); UnlockBuffers( RecvReq->BufferArray, RecvReq->BufferCount, FALSE ); NextIrp->IoStatus.Status = Status; NextIrp->IoStatus.Information = TotalBytesCopied; if( NextIrp == Irp ) { RetStatus = Status; RetBytesCopied = TotalBytesCopied; } if( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } } } if( FCB->Recv.Content - FCB->Recv.BytesUsed && IsListEmpty(&FCB->PendingIrpList[FUNCTION_RECV]) ) { FCB->PollState |= AFD_EVENT_RECEIVE; FCB->PollStatus[FD_READ_BIT] = STATUS_SUCCESS; PollReeval( FCB->DeviceExt, FCB->FileObject ); } else { FCB->PollState &= ~AFD_EVENT_RECEIVE; } /* Signal FD_CLOSE if no buffered data remains and the socket can't receive any more */ if (CantReadMore(FCB)) { if (FCB->LastReceiveStatus == STATUS_SUCCESS) { FCB->PollState |= AFD_EVENT_DISCONNECT; } else { FCB->PollState |= AFD_EVENT_CLOSE; } FCB->PollStatus[FD_CLOSE_BIT] = FCB->LastReceiveStatus; PollReeval(FCB->DeviceExt, FCB->FileObject); } AFD_DbgPrint(MID_TRACE,("RetStatus for irp %p is %x\n", Irp, RetStatus)); /* Sometimes we're called with a NULL Irp */ if( Irp ) { Irp->IoStatus.Status = RetStatus; Irp->IoStatus.Information = RetBytesCopied; } return RetStatus; }
static NTSTATUS NTAPI PacketSocketSendComplete ( PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context ) { PAFD_FCB FCB = (PAFD_FCB)Context; PLIST_ENTRY NextIrpEntry; PIRP NextIrp; PAFD_SEND_INFO SendReq; UNREFERENCED_PARAMETER(DeviceObject); AFD_DbgPrint(MID_TRACE,("Called, status %x, %u bytes used\n", Irp->IoStatus.Status, Irp->IoStatus.Information)); if( !SocketAcquireStateLock( FCB ) ) return STATUS_FILE_CLOSED; ASSERT(FCB->SendIrp.InFlightRequest == Irp); FCB->SendIrp.InFlightRequest = NULL; /* Request is not in flight any longer */ if( FCB->State == SOCKET_STATE_CLOSED ) { /* Cleanup our IRP queue because the FCB is being destroyed */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_SEND] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); SendReq = GetLockedData(NextIrp, IoGetCurrentIrpStackLocation(NextIrp)); NextIrp->IoStatus.Status = STATUS_FILE_CLOSED; NextIrp->IoStatus.Information = 0; (void)IoSetCancelRoutine(NextIrp, NULL); UnlockBuffers(SendReq->BufferArray, SendReq->BufferCount, FALSE); UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } SocketStateUnlock( FCB ); return STATUS_FILE_CLOSED; } ASSERT(!IsListEmpty(&FCB->PendingIrpList[FUNCTION_SEND])); /* TDI spec guarantees FIFO ordering on IRPs */ NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); SendReq = GetLockedData(NextIrp, IoGetCurrentIrpStackLocation(NextIrp)); NextIrp->IoStatus.Status = Irp->IoStatus.Status; NextIrp->IoStatus.Information = Irp->IoStatus.Information; (void)IoSetCancelRoutine(NextIrp, NULL); UnlockBuffers(SendReq->BufferArray, SendReq->BufferCount, FALSE); UnlockRequest(NextIrp, IoGetCurrentIrpStackLocation(NextIrp)); IoCompleteRequest(NextIrp, IO_NETWORK_INCREMENT); FCB->PollState |= AFD_EVENT_SEND; FCB->PollStatus[FD_WRITE_BIT] = STATUS_SUCCESS; PollReeval(FCB->DeviceExt, FCB->FileObject); SocketStateUnlock(FCB); return STATUS_SUCCESS; }
static NTSTATUS NTAPI SendComplete ( PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context ) { NTSTATUS Status = Irp->IoStatus.Status; PAFD_FCB FCB = (PAFD_FCB)Context; PLIST_ENTRY NextIrpEntry; PIRP NextIrp = NULL; PIO_STACK_LOCATION NextIrpSp; PAFD_SEND_INFO SendReq = NULL; PAFD_MAPBUF Map; UINT TotalBytesCopied = 0, TotalBytesProcessed = 0, SpaceAvail, i; UINT SendLength, BytesCopied; BOOLEAN HaltSendQueue; UNREFERENCED_PARAMETER(DeviceObject); /* * The Irp parameter passed in is the IRP of the stream between AFD and * TDI driver. It's not very usefull to us. We need the IRPs of the stream * between usermode and AFD. Those are chained from * FCB->PendingIrpList[FUNCTION_SEND] and you'll see them in the code * below as "NextIrp" ('cause they are the next usermode IRP to be * processed). */ AFD_DbgPrint(MID_TRACE,("Called, status %x, %u bytes used\n", Irp->IoStatus.Status, Irp->IoStatus.Information)); if( !SocketAcquireStateLock( FCB ) ) return STATUS_FILE_CLOSED; ASSERT(FCB->SendIrp.InFlightRequest == Irp); FCB->SendIrp.InFlightRequest = NULL; /* Request is not in flight any longer */ if( FCB->State == SOCKET_STATE_CLOSED ) { /* Cleanup our IRP queue because the FCB is being destroyed */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_SEND] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); NextIrp->IoStatus.Status = STATUS_FILE_CLOSED; NextIrp->IoStatus.Information = 0; UnlockBuffers(SendReq->BufferArray, SendReq->BufferCount, FALSE); if( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } RetryDisconnectCompletion(FCB); SocketStateUnlock( FCB ); return STATUS_FILE_CLOSED; } if( !NT_SUCCESS(Status) ) { /* Complete all following send IRPs with error */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_SEND] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); UnlockBuffers( SendReq->BufferArray, SendReq->BufferCount, FALSE ); NextIrp->IoStatus.Status = Status; NextIrp->IoStatus.Information = 0; if ( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } RetryDisconnectCompletion(FCB); SocketStateUnlock( FCB ); return STATUS_SUCCESS; } RtlMoveMemory( FCB->Send.Window, FCB->Send.Window + Irp->IoStatus.Information, FCB->Send.BytesUsed - Irp->IoStatus.Information ); TotalBytesProcessed = 0; SendLength = Irp->IoStatus.Information; HaltSendQueue = FALSE; while (!IsListEmpty(&FCB->PendingIrpList[FUNCTION_SEND]) && SendLength > 0) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); Map = (PAFD_MAPBUF)(SendReq->BufferArray + SendReq->BufferCount); TotalBytesCopied = (ULONG_PTR)NextIrp->Tail.Overlay.DriverContext[3]; ASSERT(TotalBytesCopied != 0); /* If we didn't get enough, keep waiting */ if (TotalBytesCopied > SendLength) { /* Update the bytes left to copy */ TotalBytesCopied -= SendLength; NextIrp->Tail.Overlay.DriverContext[3] = (PVOID)TotalBytesCopied; /* Update the state variables */ FCB->Send.BytesUsed -= SendLength; TotalBytesProcessed += SendLength; SendLength = 0; /* Pend the IRP */ InsertHeadList(&FCB->PendingIrpList[FUNCTION_SEND], &NextIrp->Tail.Overlay.ListEntry); HaltSendQueue = TRUE; break; } ASSERT(NextIrp->IoStatus.Information != 0); NextIrp->IoStatus.Status = Irp->IoStatus.Status; FCB->Send.BytesUsed -= TotalBytesCopied; TotalBytesProcessed += TotalBytesCopied; SendLength -= TotalBytesCopied; (void)IoSetCancelRoutine(NextIrp, NULL); UnlockBuffers( SendReq->BufferArray, SendReq->BufferCount, FALSE ); if (NextIrp->MdlAddress) UnlockRequest(NextIrp, NextIrpSp); IoCompleteRequest(NextIrp, IO_NETWORK_INCREMENT); } ASSERT(SendLength == 0); if ( !HaltSendQueue && !IsListEmpty( &FCB->PendingIrpList[FUNCTION_SEND] ) ) { NextIrpEntry = FCB->PendingIrpList[FUNCTION_SEND].Flink; NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); Map = (PAFD_MAPBUF)(SendReq->BufferArray + SendReq->BufferCount); AFD_DbgPrint(MID_TRACE,("SendReq @ %p\n", SendReq)); SpaceAvail = FCB->Send.Size - FCB->Send.BytesUsed; TotalBytesCopied = 0; /* Count the total transfer size */ SendLength = 0; for (i = 0; i < SendReq->BufferCount; i++) { SendLength += SendReq->BufferArray[i].len; } /* Make sure we've got the space */ if (SendLength > SpaceAvail) { /* Blocking sockets have to wait here */ if (SendLength <= FCB->Send.Size && !((SendReq->AfdFlags & AFD_IMMEDIATE) || (FCB->NonBlocking))) { FCB->PollState &= ~AFD_EVENT_SEND; NextIrp = NULL; } /* Check if we can send anything */ if (SpaceAvail == 0) { FCB->PollState &= ~AFD_EVENT_SEND; /* We should never be non-overlapped and get to this point */ ASSERT(SendReq->AfdFlags & AFD_OVERLAPPED); NextIrp = NULL; } } if (NextIrp != NULL) { for( i = 0; i < SendReq->BufferCount; i++ ) { BytesCopied = MIN(SendReq->BufferArray[i].len, SpaceAvail); Map[i].BufferAddress = MmMapLockedPages( Map[i].Mdl, KernelMode ); RtlCopyMemory( FCB->Send.Window + FCB->Send.BytesUsed, Map[i].BufferAddress, BytesCopied ); MmUnmapLockedPages( Map[i].BufferAddress, Map[i].Mdl ); TotalBytesCopied += BytesCopied; SpaceAvail -= BytesCopied; FCB->Send.BytesUsed += BytesCopied; } NextIrp->IoStatus.Information = TotalBytesCopied; NextIrp->Tail.Overlay.DriverContext[3] = (PVOID)NextIrp->IoStatus.Information; } } if (FCB->Send.Size - FCB->Send.BytesUsed != 0 && !FCB->SendClosed && IsListEmpty(&FCB->PendingIrpList[FUNCTION_SEND])) { FCB->PollState |= AFD_EVENT_SEND; FCB->PollStatus[FD_WRITE_BIT] = STATUS_SUCCESS; PollReeval( FCB->DeviceExt, FCB->FileObject ); } else { FCB->PollState &= ~AFD_EVENT_SEND; } /* Some data is still waiting */ if( FCB->Send.BytesUsed ) { Status = TdiSend( &FCB->SendIrp.InFlightRequest, FCB->Connection.Object, 0, FCB->Send.Window, FCB->Send.BytesUsed, SendComplete, FCB ); } else { /* Nothing is waiting so try to complete a pending disconnect */ RetryDisconnectCompletion(FCB); } SocketStateUnlock( FCB ); return STATUS_SUCCESS; }
static NTSTATUS NTAPI SendComplete ( PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context ) { NTSTATUS Status = Irp->IoStatus.Status; PAFD_FCB FCB = (PAFD_FCB)Context; PLIST_ENTRY NextIrpEntry; PIRP NextIrp = NULL; PIO_STACK_LOCATION NextIrpSp; PAFD_SEND_INFO SendReq = NULL; PAFD_MAPBUF Map; UINT TotalBytesCopied = 0, TotalBytesProcessed = 0, SpaceAvail, i; /* * The Irp parameter passed in is the IRP of the stream between AFD and * TDI driver. It's not very usefull to us. We need the IRPs of the stream * between usermode and AFD. Those are chained from * FCB->PendingIrpList[FUNCTION_SEND] and you'll see them in the code * below as "NextIrp" ('cause they are the next usermode IRP to be * processed). */ AFD_DbgPrint(MID_TRACE,("Called, status %x, %d bytes used\n", Irp->IoStatus.Status, Irp->IoStatus.Information)); if( !SocketAcquireStateLock( FCB ) ) return STATUS_FILE_CLOSED; ASSERT(FCB->SendIrp.InFlightRequest == Irp); FCB->SendIrp.InFlightRequest = NULL; /* Request is not in flight any longer */ if( FCB->State == SOCKET_STATE_CLOSED ) { /* Cleanup our IRP queue because the FCB is being destroyed */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_SEND] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); NextIrp->IoStatus.Status = STATUS_FILE_CLOSED; NextIrp->IoStatus.Information = 0; UnlockBuffers(SendReq->BufferArray, SendReq->BufferCount, FALSE); if( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } RetryDisconnectCompletion(FCB); SocketStateUnlock( FCB ); return STATUS_FILE_CLOSED; } if( !NT_SUCCESS(Status) ) { /* Complete all following send IRPs with error */ while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_SEND] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); UnlockBuffers( SendReq->BufferArray, SendReq->BufferCount, FALSE ); NextIrp->IoStatus.Status = Status; NextIrp->IoStatus.Information = 0; if ( NextIrp->MdlAddress ) UnlockRequest( NextIrp, IoGetCurrentIrpStackLocation( NextIrp ) ); (void)IoSetCancelRoutine(NextIrp, NULL); IoCompleteRequest( NextIrp, IO_NETWORK_INCREMENT ); } RetryDisconnectCompletion(FCB); SocketStateUnlock( FCB ); return STATUS_SUCCESS; } RtlMoveMemory( FCB->Send.Window, FCB->Send.Window + FCB->Send.BytesUsed, FCB->Send.BytesUsed - Irp->IoStatus.Information ); TotalBytesProcessed = 0; while (!IsListEmpty(&FCB->PendingIrpList[FUNCTION_SEND]) && TotalBytesProcessed != Irp->IoStatus.Information) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); Map = (PAFD_MAPBUF)(SendReq->BufferArray + SendReq->BufferCount); TotalBytesCopied = 0; for( i = 0; i < SendReq->BufferCount; i++ ) TotalBytesCopied += SendReq->BufferArray[i].len; NextIrp->IoStatus.Status = Irp->IoStatus.Status; NextIrp->IoStatus.Information = TotalBytesCopied; TotalBytesProcessed += TotalBytesCopied; (void)IoSetCancelRoutine(NextIrp, NULL); UnlockBuffers( SendReq->BufferArray, SendReq->BufferCount, FALSE ); if (NextIrp->MdlAddress) UnlockRequest(NextIrp, NextIrpSp); IoCompleteRequest(NextIrp, IO_NETWORK_INCREMENT); } ASSERT(TotalBytesProcessed == Irp->IoStatus.Information); FCB->Send.BytesUsed -= TotalBytesProcessed; while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_SEND] ) ) { NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_SEND]); NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry); NextIrpSp = IoGetCurrentIrpStackLocation( NextIrp ); SendReq = GetLockedData(NextIrp, NextIrpSp); Map = (PAFD_MAPBUF)(SendReq->BufferArray + SendReq->BufferCount); AFD_DbgPrint(MID_TRACE,("SendReq @ %x\n", SendReq)); SpaceAvail = FCB->Send.Size - FCB->Send.BytesUsed; TotalBytesCopied = 0; for( i = 0; i < SendReq->BufferCount; i++ ) { if (SpaceAvail < SendReq->BufferArray[i].len) { InsertHeadList(&FCB->PendingIrpList[FUNCTION_SEND], &NextIrp->Tail.Overlay.ListEntry); NextIrp = NULL; break; } Map[i].BufferAddress = MmMapLockedPages( Map[i].Mdl, KernelMode ); RtlCopyMemory( FCB->Send.Window + FCB->Send.BytesUsed, Map[i].BufferAddress, SendReq->BufferArray[i].len ); MmUnmapLockedPages( Map[i].BufferAddress, Map[i].Mdl ); TotalBytesCopied += SendReq->BufferArray[i].len; SpaceAvail -= SendReq->BufferArray[i].len; } if (NextIrp != NULL) { FCB->Send.BytesUsed += TotalBytesCopied; } else break; } if (FCB->Send.Size - FCB->Send.BytesUsed != 0 && !FCB->SendClosed) { FCB->PollState |= AFD_EVENT_SEND; FCB->PollStatus[FD_WRITE_BIT] = STATUS_SUCCESS; PollReeval( FCB->DeviceExt, FCB->FileObject ); } else { FCB->PollState &= ~AFD_EVENT_SEND; } /* Some data is still waiting */ if( FCB->Send.BytesUsed ) { Status = TdiSend( &FCB->SendIrp.InFlightRequest, FCB->Connection.Object, 0, FCB->Send.Window, FCB->Send.BytesUsed, &FCB->SendIrp.Iosb, SendComplete, FCB ); } else { /* Nothing is waiting so try to complete a pending disconnect */ RetryDisconnectCompletion(FCB); } SocketStateUnlock( FCB ); return STATUS_SUCCESS; }