Synchronize with trunk revision 59781.
[reactos.git] / drivers / network / afd / afd / context.c
1 /*
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 UNREFERENCED_PARAMETER(DeviceObject);
21
22 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
23
24 if( FCB->ContextSize < ContextSize ) ContextSize = FCB->ContextSize;
25
26 if( FCB->Context ) {
27 RtlCopyMemory( Irp->UserBuffer,
28 FCB->Context,
29 ContextSize );
30 Status = STATUS_SUCCESS;
31 }
32
33 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
34
35 return UnlockAndMaybeComplete( FCB, Status, Irp, ContextSize );
36 }
37
38 NTSTATUS NTAPI
39 AfdGetContextSize( PDEVICE_OBJECT DeviceObject, PIRP Irp,
40 PIO_STACK_LOCATION IrpSp )
41 {
42 PFILE_OBJECT FileObject = IrpSp->FileObject;
43 PAFD_FCB FCB = FileObject->FsContext;
44
45 UNREFERENCED_PARAMETER(DeviceObject);
46
47 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
48
49 if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(ULONG))
50 {
51 AFD_DbgPrint(MIN_TRACE,("Buffer too small\n"));
52 return UnlockAndMaybeComplete(FCB, STATUS_BUFFER_TOO_SMALL, Irp, sizeof(ULONG));
53 }
54
55 RtlCopyMemory(Irp->UserBuffer,
56 &FCB->ContextSize,
57 sizeof(ULONG));
58
59 return UnlockAndMaybeComplete(FCB, STATUS_SUCCESS, Irp, sizeof(ULONG));
60 }
61
62 NTSTATUS NTAPI
63 AfdSetContext( PDEVICE_OBJECT DeviceObject, PIRP Irp,
64 PIO_STACK_LOCATION IrpSp ) {
65 PFILE_OBJECT FileObject = IrpSp->FileObject;
66 PAFD_FCB FCB = FileObject->FsContext;
67 PVOID Context = LockRequest(Irp, IrpSp, FALSE, NULL);
68
69 UNREFERENCED_PARAMETER(DeviceObject);
70
71 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
72
73 if (!Context)
74 return UnlockAndMaybeComplete(FCB, STATUS_NO_MEMORY, Irp, 0);
75
76 if( FCB->Context ) {
77 ExFreePool( FCB->Context );
78 FCB->ContextSize = 0;
79 }
80
81 FCB->Context = ExAllocatePool( PagedPool,
82 IrpSp->Parameters.DeviceIoControl.InputBufferLength );
83
84 if( !FCB->Context ) return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY, Irp, 0 );
85
86 FCB->ContextSize = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
87
88 RtlCopyMemory( FCB->Context,
89 Context,
90 FCB->ContextSize );
91
92 return UnlockAndMaybeComplete( FCB, STATUS_SUCCESS, Irp, 0 );
93 }