162f6606d2076b3b1cb3a839e19d929793fa969c
[reactos.git] / reactos / drivers / network / afd / afd / info.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/net/afd/afd/info.c
5 * PURPOSE: Ancillary functions driver
6 * PROGRAMMER: Art Yerkes (ayerkes@speakeasy.net)
7 * UPDATE HISTORY:
8 * 20040708 Created
9 */
10
11 #include "afd.h"
12
13 NTSTATUS NTAPI
14 AfdGetInfo( PDEVICE_OBJECT DeviceObject, PIRP Irp,
15 PIO_STACK_LOCATION IrpSp ) {
16 NTSTATUS Status = STATUS_SUCCESS;
17 PAFD_INFO InfoReq = LockRequest(Irp, IrpSp, TRUE, NULL);
18 PFILE_OBJECT FileObject = IrpSp->FileObject;
19 PAFD_FCB FCB = FileObject->FsContext;
20 PLIST_ENTRY CurrentEntry;
21
22 UNREFERENCED_PARAMETER(DeviceObject);
23
24 AFD_DbgPrint(MID_TRACE,("Called %p %x\n", InfoReq,
25 InfoReq ? InfoReq->InformationClass : 0));
26
27 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
28
29 if (!InfoReq)
30 return UnlockAndMaybeComplete(FCB, STATUS_NO_MEMORY, Irp, 0);
31
32 _SEH2_TRY {
33 switch( InfoReq->InformationClass ) {
34 case AFD_INFO_RECEIVE_WINDOW_SIZE:
35 InfoReq->Information.Ulong = FCB->Recv.Size;
36 break;
37
38 case AFD_INFO_SEND_WINDOW_SIZE:
39 InfoReq->Information.Ulong = FCB->Send.Size;
40 AFD_DbgPrint(MID_TRACE,("Send window size %u\n", FCB->Send.Size));
41 break;
42
43 case AFD_INFO_GROUP_ID_TYPE:
44 InfoReq->Information.LargeInteger.u.HighPart = FCB->GroupType;
45 InfoReq->Information.LargeInteger.u.LowPart = FCB->GroupID;
46 AFD_DbgPrint(MID_TRACE, ("Group ID: %u Group Type: %u\n", FCB->GroupID, FCB->GroupType));
47 break;
48
49 case AFD_INFO_BLOCKING_MODE:
50 InfoReq->Information.Boolean = FCB->NonBlocking;
51 break;
52
53 case AFD_INFO_INLINING_MODE:
54 InfoReq->Information.Boolean = FCB->OobInline;
55 break;
56
57 case AFD_INFO_RECEIVE_CONTENT_SIZE:
58 InfoReq->Information.Ulong = FCB->Recv.Content - FCB->Recv.BytesUsed;
59 break;
60
61 case AFD_INFO_SENDS_IN_PROGRESS:
62 InfoReq->Information.Ulong = 0;
63
64 /* Count the queued sends */
65 CurrentEntry = FCB->PendingIrpList[FUNCTION_SEND].Flink;
66 while (CurrentEntry != &FCB->PendingIrpList[FUNCTION_SEND])
67 {
68 InfoReq->Information.Ulong++;
69 CurrentEntry = CurrentEntry->Flink;
70 }
71
72 /* This needs to count too because when this is dispatched
73 * the user-mode IRP has already been completed and therefore
74 * will NOT be in our pending IRP list. We count this as one send
75 * outstanding although it could be multiple since we batch sends
76 * when waiting for the in flight request to return, so this number
77 * may not be accurate but it really doesn't matter that much since
78 * it's more or less a zero/non-zero comparison to determine whether
79 * we can shutdown the socket
80 */
81 if (FCB->SendIrp.InFlightRequest)
82 InfoReq->Information.Ulong++;
83 break;
84
85 default:
86 AFD_DbgPrint(MIN_TRACE,("Unknown info id %x\n",
87 InfoReq->InformationClass));
88 Status = STATUS_INVALID_PARAMETER;
89 break;
90 }
91 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
92 AFD_DbgPrint(MIN_TRACE,("Exception executing GetInfo\n"));
93 Status = STATUS_INVALID_PARAMETER;
94 } _SEH2_END;
95
96 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
97
98 return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
99 }
100
101 NTSTATUS NTAPI
102 AfdSetInfo( PDEVICE_OBJECT DeviceObject, PIRP Irp,
103 PIO_STACK_LOCATION IrpSp ) {
104 NTSTATUS Status = STATUS_SUCCESS;
105 PAFD_INFO InfoReq = LockRequest(Irp, IrpSp, FALSE, NULL);
106 PFILE_OBJECT FileObject = IrpSp->FileObject;
107 PAFD_FCB FCB = FileObject->FsContext;
108 PCHAR NewBuffer;
109
110 UNREFERENCED_PARAMETER(DeviceObject);
111
112 if (!SocketAcquireStateLock(FCB)) return LostSocket(Irp);
113
114 if (!InfoReq)
115 return UnlockAndMaybeComplete(FCB, STATUS_NO_MEMORY, Irp, 0);
116
117 _SEH2_TRY {
118 switch (InfoReq->InformationClass) {
119 case AFD_INFO_BLOCKING_MODE:
120 AFD_DbgPrint(MID_TRACE,("Blocking mode set to %u\n", InfoReq->Information.Boolean));
121 FCB->NonBlocking = InfoReq->Information.Boolean;
122 break;
123 case AFD_INFO_INLINING_MODE:
124 FCB->OobInline = InfoReq->Information.Boolean;
125 break;
126 case AFD_INFO_RECEIVE_WINDOW_SIZE:
127 NewBuffer = ExAllocatePool(PagedPool, InfoReq->Information.Ulong);
128 if (NewBuffer)
129 {
130 if (FCB->Recv.Content > InfoReq->Information.Ulong)
131 FCB->Recv.Content = InfoReq->Information.Ulong;
132
133 if (FCB->Recv.Window)
134 {
135 RtlCopyMemory(NewBuffer,
136 FCB->Recv.Window,
137 FCB->Recv.Content);
138
139 ExFreePool(FCB->Recv.Window);
140 }
141
142 FCB->Recv.Size = InfoReq->Information.Ulong;
143 FCB->Recv.Window = NewBuffer;
144
145 Status = STATUS_SUCCESS;
146 }
147 else
148 {
149 Status = STATUS_NO_MEMORY;
150 }
151 break;
152 case AFD_INFO_SEND_WINDOW_SIZE:
153 NewBuffer = ExAllocatePool(PagedPool, InfoReq->Information.Ulong);
154 if (NewBuffer)
155 {
156 if (FCB->Send.BytesUsed > InfoReq->Information.Ulong)
157 FCB->Send.BytesUsed = InfoReq->Information.Ulong;
158
159 if (FCB->Send.Window)
160 {
161 RtlCopyMemory(NewBuffer,
162 FCB->Send.Window,
163 FCB->Send.BytesUsed);
164
165 ExFreePool(FCB->Send.Window);
166 }
167
168 FCB->Send.Size = InfoReq->Information.Ulong;
169 FCB->Send.Window = NewBuffer;
170
171 Status = STATUS_SUCCESS;
172 }
173 else
174 {
175 Status = STATUS_NO_MEMORY;
176 }
177 break;
178 default:
179 AFD_DbgPrint(MIN_TRACE,("Unknown request %u\n", InfoReq->InformationClass));
180 break;
181 }
182 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
183 AFD_DbgPrint(MIN_TRACE,("Exception executing SetInfo\n"));
184 Status = STATUS_INVALID_PARAMETER;
185 } _SEH2_END;
186
187 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
188
189 return UnlockAndMaybeComplete(FCB, Status, Irp, 0);
190 }
191
192 NTSTATUS NTAPI
193 AfdGetSockName( PDEVICE_OBJECT DeviceObject, PIRP Irp,
194 PIO_STACK_LOCATION IrpSp ) {
195 NTSTATUS Status = STATUS_SUCCESS;
196 PFILE_OBJECT FileObject = IrpSp->FileObject;
197 PAFD_FCB FCB = FileObject->FsContext;
198 PMDL Mdl = NULL;
199
200 UNREFERENCED_PARAMETER(DeviceObject);
201 ASSERT(Irp->MdlAddress == NULL);
202
203 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
204
205 if( FCB->AddressFile.Object == NULL && FCB->Connection.Object == NULL ) {
206 return UnlockAndMaybeComplete( FCB, STATUS_INVALID_PARAMETER, Irp, 0 );
207 }
208
209 Mdl = IoAllocateMdl( Irp->UserBuffer,
210 IrpSp->Parameters.DeviceIoControl.OutputBufferLength,
211 FALSE,
212 FALSE,
213 NULL );
214
215 if( Mdl != NULL ) {
216 _SEH2_TRY {
217 MmProbeAndLockPages( Mdl, Irp->RequestorMode, IoModifyAccess );
218 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
219 AFD_DbgPrint(MIN_TRACE, ("MmProbeAndLockPages() failed.\n"));
220 Status = _SEH2_GetExceptionCode();
221 } _SEH2_END;
222
223 if( NT_SUCCESS(Status) ) {
224 Status = TdiQueryInformation( FCB->Connection.Object
225 ? FCB->Connection.Object
226 : FCB->AddressFile.Object,
227 TDI_QUERY_ADDRESS_INFO,
228 Mdl );
229 }
230
231 /* Check if MmProbeAndLockPages or TdiQueryInformation failed and
232 * clean up Mdl */
233 if (!NT_SUCCESS(Status) && Irp->MdlAddress != Mdl)
234 IoFreeMdl(Mdl);
235 } else
236 Status = STATUS_INSUFFICIENT_RESOURCES;
237
238 return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
239 }
240
241 NTSTATUS NTAPI
242 AfdGetPeerName( PDEVICE_OBJECT DeviceObject, PIRP Irp,
243 PIO_STACK_LOCATION IrpSp ) {
244 NTSTATUS Status;
245 PFILE_OBJECT FileObject = IrpSp->FileObject;
246 PAFD_FCB FCB = FileObject->FsContext;
247
248 UNREFERENCED_PARAMETER(DeviceObject);
249
250 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
251
252 if (FCB->RemoteAddress == NULL) {
253 AFD_DbgPrint(MIN_TRACE,("Invalid parameter\n"));
254 return UnlockAndMaybeComplete( FCB, STATUS_INVALID_PARAMETER, Irp, 0 );
255 }
256
257 if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength >= TaLengthOfTransportAddress(FCB->RemoteAddress))
258 {
259 RtlCopyMemory(Irp->UserBuffer, FCB->RemoteAddress, TaLengthOfTransportAddress(FCB->RemoteAddress));
260 Status = STATUS_SUCCESS;
261 }
262 else
263 {
264 AFD_DbgPrint(MIN_TRACE,("Buffer too small\n"));
265 Status = STATUS_BUFFER_TOO_SMALL;
266 }
267
268 return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
269 }