Synchronize up to trunk's revision r57689.
[reactos.git] / drivers / network / afd / afd / context.c
1 /* $Id$
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/net/afd/afd/context.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
12 NTSTATUS NTAPI
13 AfdGetContext( PDEVICE_OBJECT DeviceObject, PIRP Irp,
14 PIO_STACK_LOCATION IrpSp ) {
15 NTSTATUS Status = STATUS_INVALID_PARAMETER;
16 PFILE_OBJECT FileObject = IrpSp->FileObject;
17 PAFD_FCB FCB = FileObject->FsContext;
18 UINT ContextSize = IrpSp->Parameters.DeviceIoControl.OutputBufferLength;
19
20 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
21
22 if( FCB->ContextSize < ContextSize ) ContextSize = FCB->ContextSize;
23
24 if( FCB->Context ) {
25 RtlCopyMemory( Irp->UserBuffer,
26 FCB->Context,
27 ContextSize );
28 Status = STATUS_SUCCESS;
29 }
30
31 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
32
33 return UnlockAndMaybeComplete( FCB, Status, Irp, ContextSize );
34 }
35
36 NTSTATUS NTAPI
37 AfdGetContextSize( PDEVICE_OBJECT DeviceObject, PIRP Irp,
38 PIO_STACK_LOCATION IrpSp )
39 {
40 PFILE_OBJECT FileObject = IrpSp->FileObject;
41 PAFD_FCB FCB = FileObject->FsContext;
42
43 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
44
45 if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(ULONG))
46 {
47 AFD_DbgPrint(MIN_TRACE,("Buffer too small\n"));
48 return UnlockAndMaybeComplete(FCB, STATUS_BUFFER_TOO_SMALL, Irp, sizeof(ULONG));
49 }
50
51 RtlCopyMemory(Irp->UserBuffer,
52 &FCB->ContextSize,
53 sizeof(ULONG));
54
55 return UnlockAndMaybeComplete(FCB, STATUS_SUCCESS, Irp, sizeof(ULONG));
56 }
57
58 NTSTATUS NTAPI
59 AfdSetContext( PDEVICE_OBJECT DeviceObject, PIRP Irp,
60 PIO_STACK_LOCATION IrpSp ) {
61 PFILE_OBJECT FileObject = IrpSp->FileObject;
62 PAFD_FCB FCB = FileObject->FsContext;
63 PVOID Context = LockRequest(Irp, IrpSp, FALSE, NULL);
64
65 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
66
67 if (!Context)
68 return UnlockAndMaybeComplete(FCB, STATUS_NO_MEMORY, Irp, 0);
69
70 if( FCB->Context ) {
71 ExFreePool( FCB->Context );
72 FCB->ContextSize = 0;
73 }
74
75 FCB->Context = ExAllocatePool( PagedPool,
76 IrpSp->Parameters.DeviceIoControl.InputBufferLength );
77
78 if( !FCB->Context ) return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY, Irp, 0 );
79
80 FCB->ContextSize = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
81
82 RtlCopyMemory( FCB->Context,
83 Context,
84 FCB->ContextSize );
85
86 return UnlockAndMaybeComplete( FCB, STATUS_SUCCESS, Irp, 0 );
87 }