[SHELL-EXPERIMENTS]
[reactos.git] / drivers / network / tcpip / tcpip / fileobjs.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: tcpip/fileobjs.c
5 * PURPOSE: Routines for handling file objects
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * REVISIONS:
8 * CSH 01/08-2000 Created
9 */
10
11 #include "precomp.h"
12
13 /* Uncomment for logging of connections and address files every 10 seconds */
14 //#define LOG_OBJECTS
15
16 /* List of all address file objects managed by this driver */
17 LIST_ENTRY AddressFileListHead;
18 KSPIN_LOCK AddressFileListLock;
19
20 /* List of all connection endpoint file objects managed by this driver */
21 LIST_ENTRY ConnectionEndpointListHead;
22 KSPIN_LOCK ConnectionEndpointListLock;
23
24 /*
25 * FUNCTION: Searches through address file entries to find the first match
26 * ARGUMENTS:
27 * Address = IP address
28 * Port = Port number
29 * Protocol = Protocol number
30 * SearchContext = Pointer to search context
31 * RETURNS:
32 * Pointer to address file, NULL if none was found
33 */
34 PADDRESS_FILE AddrSearchFirst(
35 PIP_ADDRESS Address,
36 USHORT Port,
37 USHORT Protocol,
38 PAF_SEARCH SearchContext)
39 {
40 KIRQL OldIrql;
41
42 SearchContext->Address = Address;
43 SearchContext->Port = Port;
44 SearchContext->Protocol = Protocol;
45
46 TcpipAcquireSpinLock(&AddressFileListLock, &OldIrql);
47
48 SearchContext->Next = AddressFileListHead.Flink;
49
50 if (!IsListEmpty(&AddressFileListHead))
51 ReferenceObject(CONTAINING_RECORD(SearchContext->Next, ADDRESS_FILE, ListEntry));
52
53 TcpipReleaseSpinLock(&AddressFileListLock, OldIrql);
54
55 return AddrSearchNext(SearchContext);
56 }
57
58 BOOLEAN AddrIsBroadcastMatch(
59 PIP_ADDRESS UnicastAddress,
60 PIP_ADDRESS BroadcastAddress ) {
61 IF_LIST_ITER(IF);
62
63 ForEachInterface(IF) {
64 if ((AddrIsUnspecified(UnicastAddress) ||
65 AddrIsEqual(&IF->Unicast, UnicastAddress)) &&
66 (AddrIsEqual(&IF->Broadcast, BroadcastAddress)))
67 return TRUE;
68 } EndFor(IF);
69
70 return FALSE;
71 }
72
73 BOOLEAN AddrReceiveMatch(
74 PIP_ADDRESS LocalAddress,
75 PIP_ADDRESS RemoteAddress)
76 {
77 if (AddrIsEqual(LocalAddress, RemoteAddress))
78 {
79 /* Unicast address match */
80 return TRUE;
81 }
82
83 if (AddrIsBroadcastMatch(LocalAddress, RemoteAddress))
84 {
85 /* Broadcast address match */
86 return TRUE;
87 }
88
89 if (AddrIsUnspecified(LocalAddress))
90 {
91 /* Local address unspecified */
92 return TRUE;
93 }
94
95 if (AddrIsUnspecified(RemoteAddress))
96 {
97 /* Remote address unspecified */
98 return TRUE;
99 }
100
101 return FALSE;
102 }
103
104 VOID
105 LogActiveObjects(VOID)
106 {
107 #ifdef LOG_OBJECTS
108 PLIST_ENTRY CurrentEntry;
109 KIRQL OldIrql;
110 PADDRESS_FILE AddrFile;
111 PCONNECTION_ENDPOINT Conn;
112
113 DbgPrint("----------- TCP/IP Active Object Dump -------------\n");
114
115 TcpipAcquireSpinLock(&AddressFileListLock, &OldIrql);
116
117 CurrentEntry = AddressFileListHead.Flink;
118 while (CurrentEntry != &AddressFileListHead)
119 {
120 AddrFile = CONTAINING_RECORD(CurrentEntry, ADDRESS_FILE, ListEntry);
121
122 DbgPrint("Address File (%s, %d, %d) @ 0x%p | Ref count: %d | Sharers: %d\n",
123 A2S(&AddrFile->Address), WN2H(AddrFile->Port), AddrFile->Protocol,
124 AddrFile, AddrFile->RefCount, AddrFile->Sharers);
125 DbgPrint("\tListener: ");
126 if (AddrFile->Listener == NULL)
127 DbgPrint("<None>\n");
128 else
129 DbgPrint("0x%p\n", AddrFile->Listener);
130 DbgPrint("\tAssociated endpoints: ");
131 if (AddrFile->Connection == NULL)
132 DbgPrint("<None>\n");
133 else
134 {
135 Conn = AddrFile->Connection;
136 while (Conn)
137 {
138 DbgPrint("0x%p ", Conn);
139 Conn = Conn->Next;
140 }
141 DbgPrint("\n");
142 }
143
144 CurrentEntry = CurrentEntry->Flink;
145 }
146
147 TcpipReleaseSpinLock(&AddressFileListLock, OldIrql);
148
149 TcpipAcquireSpinLock(&ConnectionEndpointListLock, &OldIrql);
150
151 CurrentEntry = ConnectionEndpointListHead.Flink;
152 while (CurrentEntry != &ConnectionEndpointListHead)
153 {
154 Conn = CONTAINING_RECORD(CurrentEntry, CONNECTION_ENDPOINT, ListEntry);
155
156 DbgPrint("Connection @ 0x%p | Ref count: %d\n", Conn, Conn->RefCount);
157 DbgPrint("\tPCB: ");
158 if (Conn->SocketContext == NULL)
159 DbgPrint("<None>\n");
160 else
161 {
162 DbgPrint("0x%p\n", Conn->SocketContext);
163 LibTCPDumpPcb(Conn->SocketContext);
164 }
165 DbgPrint("\tPacket queue status: %s\n", IsListEmpty(&Conn->PacketQueue) ? "Empty" : "Not Empty");
166 DbgPrint("\tRequest lists: Connect: %s | Recv: %s | Send: %s | Shutdown: %s | Listen: %s\n",
167 IsListEmpty(&Conn->ConnectRequest) ? "Empty" : "Not Empty",
168 IsListEmpty(&Conn->ReceiveRequest) ? "Empty" : "Not Empty",
169 IsListEmpty(&Conn->SendRequest) ? "Empty" : "Not Empty",
170 IsListEmpty(&Conn->ShutdownRequest) ? "Empty" : "Not Empty",
171 IsListEmpty(&Conn->ListenRequest) ? "Empty" : "Not Empty");
172 DbgPrint("\tSend shutdown: %s\n", Conn->SendShutdown ? "Yes" : "No");
173 DbgPrint("\tReceive shutdown: %s\n", Conn->ReceiveShutdown ? "Yes" : "No");
174 if (Conn->ReceiveShutdown) DbgPrint("\tReceive shutdown status: 0x%x\n", Conn->ReceiveShutdownStatus);
175 DbgPrint("\tClosing: %s\n", Conn->Closing ? "Yes" : "No");
176
177 CurrentEntry = CurrentEntry->Flink;
178 }
179
180 TcpipReleaseSpinLock(&ConnectionEndpointListLock, OldIrql);
181
182 DbgPrint("---------------------------------------------------\n");
183 #endif
184 }
185
186 PADDRESS_FILE AddrFindShared(
187 PIP_ADDRESS BindAddress,
188 USHORT Port,
189 USHORT Protocol)
190 {
191 PLIST_ENTRY CurrentEntry;
192 KIRQL OldIrql;
193 PADDRESS_FILE Current = NULL;
194
195 TcpipAcquireSpinLock(&AddressFileListLock, &OldIrql);
196
197 CurrentEntry = AddressFileListHead.Flink;
198 while (CurrentEntry != &AddressFileListHead) {
199 Current = CONTAINING_RECORD(CurrentEntry, ADDRESS_FILE, ListEntry);
200
201 /* See if this address matches the search criteria */
202 if ((Current->Port == Port) &&
203 (Current->Protocol == Protocol))
204 {
205 /* Increase the sharer count */
206 ASSERT(Current->Sharers != 0);
207 InterlockedIncrement(&Current->Sharers);
208 break;
209 }
210
211 CurrentEntry = CurrentEntry->Flink;
212 Current = NULL;
213 }
214
215 TcpipReleaseSpinLock(&AddressFileListLock, OldIrql);
216
217 return Current;
218 }
219
220 /*
221 * FUNCTION: Searches through address file entries to find next match
222 * ARGUMENTS:
223 * SearchContext = Pointer to search context
224 * RETURNS:
225 * Pointer to referenced address file, NULL if none was found
226 */
227 PADDRESS_FILE AddrSearchNext(
228 PAF_SEARCH SearchContext)
229 {
230 PLIST_ENTRY CurrentEntry;
231 PIP_ADDRESS IPAddress;
232 KIRQL OldIrql;
233 PADDRESS_FILE Current = NULL;
234 BOOLEAN Found = FALSE;
235 PADDRESS_FILE StartingAddrFile;
236
237 TcpipAcquireSpinLock(&AddressFileListLock, &OldIrql);
238
239 if (SearchContext->Next == &AddressFileListHead)
240 {
241 TcpipReleaseSpinLock(&AddressFileListLock, OldIrql);
242 return NULL;
243 }
244
245 /* Save this pointer so we can dereference it later */
246 StartingAddrFile = CONTAINING_RECORD(SearchContext->Next, ADDRESS_FILE, ListEntry);
247
248 CurrentEntry = SearchContext->Next;
249
250 while (CurrentEntry != &AddressFileListHead) {
251 Current = CONTAINING_RECORD(CurrentEntry, ADDRESS_FILE, ListEntry);
252
253 IPAddress = &Current->Address;
254
255 TI_DbgPrint(DEBUG_ADDRFILE, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
256 WN2H(Current->Port),
257 Current->Protocol,
258 A2S(IPAddress),
259 WN2H(SearchContext->Port),
260 SearchContext->Protocol,
261 A2S(SearchContext->Address)));
262
263 /* See if this address matches the search criteria */
264 if ((Current->Port == SearchContext->Port) &&
265 (Current->Protocol == SearchContext->Protocol) &&
266 (AddrReceiveMatch(IPAddress, SearchContext->Address))) {
267 /* We've found a match */
268 Found = TRUE;
269 break;
270 }
271 CurrentEntry = CurrentEntry->Flink;
272 }
273
274 if (Found)
275 {
276 SearchContext->Next = CurrentEntry->Flink;
277
278 if (SearchContext->Next != &AddressFileListHead)
279 {
280 /* Reference the next address file to prevent the link from disappearing behind our back */
281 ReferenceObject(CONTAINING_RECORD(SearchContext->Next, ADDRESS_FILE, ListEntry));
282 }
283
284 /* Reference the returned address file before dereferencing the starting
285 * address file because it may be that Current == StartingAddrFile */
286 ReferenceObject(Current);
287 }
288 else
289 Current = NULL;
290
291 DereferenceObject(StartingAddrFile);
292
293 TcpipReleaseSpinLock(&AddressFileListLock, OldIrql);
294
295 return Current;
296 }
297
298 VOID AddrFileFree(
299 PVOID Object)
300 /*
301 * FUNCTION: Frees an address file object
302 * ARGUMENTS:
303 * Object = Pointer to address file object to free
304 */
305 {
306 PADDRESS_FILE AddrFile = Object;
307 KIRQL OldIrql;
308 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest;
309 PDATAGRAM_SEND_REQUEST SendRequest;
310 PLIST_ENTRY CurrentEntry;
311
312 TI_DbgPrint(MID_TRACE, ("Called.\n"));
313
314 /* We should not be associated with a connection here */
315 ASSERT(!AddrFile->Connection);
316
317 /* Remove address file from the global list */
318 TcpipAcquireSpinLock(&AddressFileListLock, &OldIrql);
319 RemoveEntryList(&AddrFile->ListEntry);
320 TcpipReleaseSpinLock(&AddressFileListLock, OldIrql);
321
322 /* FIXME: Kill TCP connections on this address file object */
323
324 /* Return pending requests with error */
325
326 TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile));
327
328 /* Go through pending receive request list and cancel them all */
329 while ((CurrentEntry = ExInterlockedRemoveHeadList(&AddrFile->ReceiveQueue, &AddrFile->Lock))) {
330 ReceiveRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_RECEIVE_REQUEST, ListEntry);
331 (*ReceiveRequest->Complete)(ReceiveRequest->Context, STATUS_CANCELLED, 0);
332 /* ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG); FIXME: WTF? */
333 }
334
335 TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting send requests on address file at (0x%X).\n", AddrFile));
336
337 /* Go through pending send request list and cancel them all */
338 while ((CurrentEntry = ExInterlockedRemoveHeadList(&AddrFile->ReceiveQueue, &AddrFile->Lock))) {
339 SendRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_SEND_REQUEST, ListEntry);
340 (*SendRequest->Complete)(SendRequest->Context, STATUS_CANCELLED, 0);
341 ExFreePoolWithTag(SendRequest, DATAGRAM_SEND_TAG);
342 }
343
344 /* Protocol specific handling */
345 switch (AddrFile->Protocol) {
346 case IPPROTO_TCP:
347 if (AddrFile->Port)
348 {
349 TCPFreePort(AddrFile->Port);
350 }
351 break;
352
353 case IPPROTO_UDP:
354 UDPFreePort( AddrFile->Port );
355 break;
356 }
357
358 RemoveEntityByContext(AddrFile);
359
360 ExFreePoolWithTag(Object, ADDR_FILE_TAG);
361 }
362
363
364 VOID ControlChannelFree(
365 PVOID Object)
366 /*
367 * FUNCTION: Frees an address file object
368 * ARGUMENTS:
369 * Object = Pointer to address file object to free
370 */
371 {
372 ExFreePoolWithTag(Object, CONTROL_CHANNEL_TAG);
373 }
374
375
376 /*
377 * FUNCTION: Open an address file object
378 * ARGUMENTS:
379 * Request = Pointer to TDI request structure for this request
380 * Address = Pointer to address to be opened
381 * Protocol = Protocol on which to open the address
382 * Shared = Specifies if the address is opened for shared access
383 * Options = Pointer to option buffer
384 * RETURNS:
385 * Status of operation
386 */
387 NTSTATUS FileOpenAddress(
388 PTDI_REQUEST Request,
389 PTA_IP_ADDRESS Address,
390 USHORT Protocol,
391 BOOLEAN Shared,
392 PVOID Options)
393 {
394 PADDRESS_FILE AddrFile;
395
396 TI_DbgPrint(MID_TRACE, ("Called (Proto %d).\n", Protocol));
397
398 /* If it's shared and has a port specified, look for a match */
399 if ((Shared != FALSE) && (Address->Address[0].Address[0].sin_port != 0))
400 {
401 AddrFile = AddrFindShared(NULL, Address->Address[0].Address[0].sin_port, Protocol);
402 if (AddrFile != NULL)
403 {
404 Request->Handle.AddressHandle = AddrFile;
405 return STATUS_SUCCESS;
406 }
407 }
408
409 AddrFile = ExAllocatePoolWithTag(NonPagedPool, sizeof(ADDRESS_FILE),
410 ADDR_FILE_TAG);
411 if (!AddrFile) {
412 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
413 return STATUS_INSUFFICIENT_RESOURCES;
414 }
415
416 RtlZeroMemory(AddrFile, sizeof(ADDRESS_FILE));
417
418 AddrFile->RefCount = 1;
419 AddrFile->Free = AddrFileFree;
420 AddrFile->Sharers = 1;
421
422 /* Set our default options */
423 AddrFile->TTL = 128;
424 AddrFile->DF = 0;
425 AddrFile->BCast = 1;
426 AddrFile->HeaderIncl = 1;
427
428 /* Make sure address is a local unicast address or 0 */
429 /* FIXME: IPv4 only */
430 AddrFile->Family = Address->Address[0].AddressType;
431 AddrFile->Address.Address.IPv4Address = Address->Address[0].Address[0].in_addr;
432 AddrFile->Address.Type = IP_ADDRESS_V4;
433
434 if (!AddrIsUnspecified(&AddrFile->Address) &&
435 !AddrLocateInterface(&AddrFile->Address)) {
436 TI_DbgPrint(MIN_TRACE, ("Non-local address given (0x%X).\n", A2S(&AddrFile->Address)));
437 ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
438 return STATUS_INVALID_ADDRESS;
439 }
440
441 TI_DbgPrint(MID_TRACE, ("Opening address %s for communication (P=%d U=%d).\n",
442 A2S(&AddrFile->Address), Protocol, IPPROTO_UDP));
443
444 /* Protocol specific handling */
445 switch (Protocol) {
446 case IPPROTO_TCP:
447 if (Address->Address[0].Address[0].sin_port)
448 {
449 /* The client specified an explicit port so we force a bind to this */
450 AddrFile->Port = TCPAllocatePort(Address->Address[0].Address[0].sin_port);
451
452 /* Check for bind success */
453 if (AddrFile->Port == 0xffff)
454 {
455 ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
456 return STATUS_ADDRESS_ALREADY_EXISTS;
457 }
458
459 /* Sanity check */
460 ASSERT(Address->Address[0].Address[0].sin_port == AddrFile->Port);
461 }
462 else if (!AddrIsUnspecified(&AddrFile->Address))
463 {
464 /* The client is trying to bind to a local address so allocate a port now too */
465 AddrFile->Port = TCPAllocatePort(0);
466
467 /* Check for bind success */
468 if (AddrFile->Port == 0xffff)
469 {
470 ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
471 return STATUS_ADDRESS_ALREADY_EXISTS;
472 }
473 }
474 else
475 {
476 /* The client wants an unspecified port with an unspecified address so we wait to see what the TCP library gives us */
477 AddrFile->Port = 0;
478 }
479
480 AddEntity(CO_TL_ENTITY, AddrFile, CO_TL_TCP);
481
482 AddrFile->Send = NULL; /* TCPSendData */
483 break;
484
485 case IPPROTO_UDP:
486 TI_DbgPrint(MID_TRACE,("Allocating udp port\n"));
487 AddrFile->Port =
488 UDPAllocatePort(Address->Address[0].Address[0].sin_port);
489
490 if ((Address->Address[0].Address[0].sin_port &&
491 AddrFile->Port != Address->Address[0].Address[0].sin_port) ||
492 AddrFile->Port == 0xffff)
493 {
494 ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
495 return STATUS_ADDRESS_ALREADY_EXISTS;
496 }
497
498 TI_DbgPrint(MID_TRACE,("Setting port %d (wanted %d)\n",
499 AddrFile->Port,
500 Address->Address[0].Address[0].sin_port));
501
502 AddEntity(CL_TL_ENTITY, AddrFile, CL_TL_UDP);
503
504 AddrFile->Send = UDPSendDatagram;
505 break;
506
507 case IPPROTO_ICMP:
508 AddrFile->Port = 0;
509 AddrFile->Send = ICMPSendDatagram;
510
511 /* FIXME: Verify this */
512 AddEntity(ER_ENTITY, AddrFile, ER_ICMP);
513 break;
514
515 default:
516 /* Use raw IP for all other protocols */
517 AddrFile->Port = 0;
518 AddrFile->Send = RawIPSendDatagram;
519
520 /* FIXME: Verify this */
521 AddEntity(CL_TL_ENTITY, AddrFile, 0);
522 break;
523 }
524
525 TI_DbgPrint(MID_TRACE, ("IP protocol number for address file object is %d.\n",
526 Protocol));
527
528 TI_DbgPrint(MID_TRACE, ("Port number for address file object is %d.\n",
529 WN2H(AddrFile->Port)));
530
531 /* Set protocol */
532 AddrFile->Protocol = Protocol;
533
534 /* Initialize receive and transmit queues */
535 InitializeListHead(&AddrFile->ReceiveQueue);
536 InitializeListHead(&AddrFile->TransmitQueue);
537
538 /* Initialize spin lock that protects the address file object */
539 KeInitializeSpinLock(&AddrFile->Lock);
540
541 /* Return address file object */
542 Request->Handle.AddressHandle = AddrFile;
543
544 /* Add address file to global list */
545 ExInterlockedInsertTailList(
546 &AddressFileListHead,
547 &AddrFile->ListEntry,
548 &AddressFileListLock);
549
550 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
551
552 return STATUS_SUCCESS;
553 }
554
555
556 /*
557 * FUNCTION: Closes an address file object
558 * ARGUMENTS:
559 * Request = Pointer to TDI request structure for this request
560 * RETURNS:
561 * Status of operation
562 */
563 NTSTATUS FileCloseAddress(
564 PTDI_REQUEST Request)
565 {
566 PADDRESS_FILE AddrFile = Request->Handle.AddressHandle;
567 KIRQL OldIrql;
568
569 if (!Request->Handle.AddressHandle) return STATUS_INVALID_PARAMETER;
570
571 LockObject(AddrFile, &OldIrql);
572
573 if (InterlockedDecrement(&AddrFile->Sharers) != 0)
574 {
575 /* Still other guys have open handles to this, so keep it around */
576 UnlockObject(AddrFile, OldIrql);
577 return STATUS_SUCCESS;
578 }
579
580 /* We have to close this listener because we started it */
581 if( AddrFile->Listener )
582 {
583 TCPClose( AddrFile->Listener );
584 }
585
586 UnlockObject(AddrFile, OldIrql);
587
588 DereferenceObject(AddrFile);
589
590 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
591
592 return STATUS_SUCCESS;
593 }
594
595
596 /*
597 * FUNCTION: Opens a connection file object
598 * ARGUMENTS:
599 * Request = Pointer to TDI request structure for this request
600 * ClientContext = Pointer to client context information
601 * RETURNS:
602 * Status of operation
603 */
604 NTSTATUS FileOpenConnection(
605 PTDI_REQUEST Request,
606 PVOID ClientContext)
607 {
608 NTSTATUS Status;
609 PCONNECTION_ENDPOINT Connection;
610
611 TI_DbgPrint(MID_TRACE, ("Called.\n"));
612
613 Connection = TCPAllocateConnectionEndpoint( ClientContext );
614
615 if( !Connection ) return STATUS_NO_MEMORY;
616
617 Status = TCPSocket( Connection, AF_INET, SOCK_STREAM, IPPROTO_TCP );
618
619 if( !NT_SUCCESS(Status) ) {
620 DereferenceObject( Connection );
621 return Status;
622 }
623
624 /* Return connection endpoint file object */
625 Request->Handle.ConnectionContext = Connection;
626
627 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
628
629 return STATUS_SUCCESS;
630 }
631
632 /*
633 * FUNCTION: Closes an connection file object
634 * ARGUMENTS:
635 * Request = Pointer to TDI request structure for this request
636 * RETURNS:
637 * Status of operation
638 */
639 NTSTATUS FileCloseConnection(
640 PTDI_REQUEST Request)
641 {
642 PCONNECTION_ENDPOINT Connection;
643
644 TI_DbgPrint(MID_TRACE, ("Called.\n"));
645
646 Connection = Request->Handle.ConnectionContext;
647
648 if (!Connection) return STATUS_INVALID_PARAMETER;
649
650 TCPClose( Connection );
651
652 Request->Handle.ConnectionContext = NULL;
653
654 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
655
656 return STATUS_SUCCESS;
657 }
658
659 /*
660 * FUNCTION: Opens a control channel file object
661 * ARGUMENTS:
662 * Request = Pointer to TDI request structure for this request
663 * RETURNS:
664 * Status of operation
665 */
666 NTSTATUS FileOpenControlChannel(
667 PTDI_REQUEST Request)
668 {
669 PCONTROL_CHANNEL ControlChannel;
670 TI_DbgPrint(MID_TRACE, ("Called.\n"));
671
672 ControlChannel = ExAllocatePoolWithTag(NonPagedPool, sizeof(*ControlChannel),
673 CONTROL_CHANNEL_TAG);
674
675 if (!ControlChannel) {
676 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
677 return STATUS_INSUFFICIENT_RESOURCES;
678 }
679
680 RtlZeroMemory(ControlChannel, sizeof(CONTROL_CHANNEL));
681
682 /* Make sure address is a local unicast address or 0 */
683
684 /* Locate address entry. If specified address is 0, a random address is chosen */
685
686 /* Initialize receive and transmit queues */
687 InitializeListHead(&ControlChannel->ListEntry);
688
689 /* Initialize spin lock that protects the address file object */
690 KeInitializeSpinLock(&ControlChannel->Lock);
691
692 ControlChannel->RefCount = 1;
693 ControlChannel->Free = ControlChannelFree;
694
695 /* Return address file object */
696 Request->Handle.ControlChannel = ControlChannel;
697
698 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
699
700 return STATUS_SUCCESS;
701 }
702
703 /*
704 * FUNCTION: Closes a control channel file object
705 * ARGUMENTS:
706 * Request = Pointer to TDI request structure for this request
707 * RETURNS:
708 * Status of operation
709 */
710 NTSTATUS FileCloseControlChannel(
711 PTDI_REQUEST Request)
712 {
713 if (!Request->Handle.ControlChannel) return STATUS_INVALID_PARAMETER;
714
715 DereferenceObject((PCONTROL_CHANNEL)Request->Handle.ControlChannel);
716
717 Request->Handle.ControlChannel = NULL;
718
719 return STATUS_SUCCESS;
720 }
721
722 /* EOF */