There's a difference between a handle and an object... Sorry about this
[reactos.git] / reactos / drivers / lib / ip / transport / tcp / tcp.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: transport/tcp/tcp.c
5 * PURPOSE: Transmission Control Protocol
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Art Yerkes (arty@users.sf.net)
8 * REVISIONS:
9 * CSH 01/08-2000 Created
10 * arty 12/21/2004 Added accept
11 */
12
13 #include "precomp.h"
14
15 LONG TCP_IPIdentification = 0;
16 static BOOLEAN TCPInitialized = FALSE;
17 static NPAGED_LOOKASIDE_LIST TCPSegmentList;
18 LIST_ENTRY SignalledConnections;
19 LIST_ENTRY SleepingThreadsList;
20 FAST_MUTEX SleepingThreadsLock;
21 RECURSIVE_MUTEX TCPLock;
22 PORT_SET TCPPorts;
23
24 static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
25 ULONG NewState ) {
26 NTSTATUS Status = STATUS_SUCCESS;
27 PTCP_COMPLETION_ROUTINE Complete;
28 PTDI_BUCKET Bucket;
29 PLIST_ENTRY Entry;
30 PIRP Irp;
31 PMDL Mdl;
32
33 TI_DbgPrint(MID_TRACE,("Handling signalled state on %x (%x)\n",
34 Connection, Connection->SocketContext));
35
36 /* Things that can happen when we try the initial connection */
37 if( NewState & SEL_CONNECT ) {
38 while( !IsListEmpty( &Connection->ConnectRequest ) ) {
39 Connection->State |= NewState;
40 Entry = RemoveHeadList( &Connection->ConnectRequest );
41 TI_DbgPrint(DEBUG_TCP, ("Connect Event\n"));
42
43 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
44 Complete = Bucket->Request.RequestNotifyObject;
45 TI_DbgPrint(DEBUG_TCP,
46 ("Completing Request %x\n", Bucket->Request));
47
48 if( (NewState & (SEL_CONNECT | SEL_FIN)) ==
49 (SEL_CONNECT | SEL_FIN) )
50 Status = STATUS_CONNECTION_REFUSED;
51 else
52 Status = STATUS_SUCCESS;
53
54 Complete( Bucket->Request.RequestContext, Status, 0 );
55
56 /* Frees the bucket allocated in TCPConnect */
57 PoolFreeBuffer( Bucket );
58 }
59 }
60
61 if( NewState & SEL_ACCEPT ) {
62 /* Handle readable on a listening socket --
63 * TODO: Implement filtering
64 */
65
66 TI_DbgPrint(DEBUG_TCP,("Accepting new connection on %x (Queue: %s)\n",
67 Connection,
68 IsListEmpty(&Connection->ListenRequest) ?
69 "empty" : "nonempty"));
70
71 while( !IsListEmpty( &Connection->ListenRequest ) ) {
72 PIO_STACK_LOCATION IrpSp;
73
74 Entry = RemoveHeadList( &Connection->ListenRequest );
75 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
76 Complete = Bucket->Request.RequestNotifyObject;
77
78 Irp = Bucket->Request.RequestContext;
79 IrpSp = IoGetCurrentIrpStackLocation( Irp );
80
81 TI_DbgPrint(DEBUG_TCP,("Getting the socket\n"));
82 Status = TCPServiceListeningSocket
83 ( Connection->AddressFile->Listener,
84 Bucket->AssociatedEndpoint,
85 (PTDI_REQUEST_KERNEL)&IrpSp->Parameters );
86
87 TI_DbgPrint(DEBUG_TCP,("Socket: Status: %x\n"));
88
89 if( Status == STATUS_PENDING ) {
90 InsertHeadList( &Connection->ListenRequest, &Bucket->Entry );
91 break;
92 } else
93 Complete( Bucket->Request.RequestContext, Status, 0 );
94 }
95 }
96
97 /* Things that happen after we're connected */
98 if( NewState & SEL_READ ) {
99 TI_DbgPrint(DEBUG_TCP,("Readable: irp list %s\n",
100 IsListEmpty(&Connection->ReceiveRequest) ?
101 "empty" : "nonempty"));
102
103 while( !IsListEmpty( &Connection->ReceiveRequest ) ) {
104 OSK_UINT RecvLen = 0, Received = 0;
105 OSK_PCHAR RecvBuffer = 0;
106
107 Entry = RemoveHeadList( &Connection->ReceiveRequest );
108 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
109 Complete = Bucket->Request.RequestNotifyObject;
110
111 Irp = Bucket->Request.RequestContext;
112 Mdl = Irp->MdlAddress;
113
114 TI_DbgPrint(DEBUG_TCP,
115 ("Getting the user buffer from %x\n", Mdl));
116
117 NdisQueryBuffer( Mdl, &RecvBuffer, &RecvLen );
118
119 TI_DbgPrint(DEBUG_TCP,
120 ("Reading %d bytes to %x\n", RecvLen, RecvBuffer));
121
122 TI_DbgPrint(DEBUG_TCP, ("Connection: %x\n", Connection));
123 TI_DbgPrint
124 (DEBUG_TCP,
125 ("Connection->SocketContext: %x\n",
126 Connection->SocketContext));
127 TI_DbgPrint(DEBUG_TCP, ("RecvBuffer: %x\n", RecvBuffer));
128
129 Status = TCPTranslateError
130 ( OskitTCPRecv( Connection->SocketContext,
131 RecvBuffer,
132 RecvLen,
133 &Received,
134 0 ) );
135
136 TI_DbgPrint(DEBUG_TCP,("TCP Bytes: %d\n", Received));
137
138 if( Status == STATUS_SUCCESS ) {
139 TI_DbgPrint(DEBUG_TCP,("Received %d bytes with status %x\n",
140 Received, Status));
141
142 Complete( Bucket->Request.RequestContext,
143 STATUS_SUCCESS, Received );
144 } else if( Status == STATUS_PENDING ) {
145 InsertHeadList
146 ( &Connection->ReceiveRequest, &Bucket->Entry );
147 break;
148 } else {
149 TI_DbgPrint(DEBUG_TCP,
150 ("Completing Receive request: %x %x\n",
151 Bucket->Request, Status));
152 Complete( Bucket->Request.RequestContext, Status, 0 );
153 }
154 }
155 }
156
157 if( NewState & SEL_FIN ) {
158 PLIST_ENTRY ListsToErase[4];
159 NTSTATUS IrpStatus[4];
160 UINT i;
161
162 TI_DbgPrint(DEBUG_TCP, ("EOF From socket\n"));
163
164 ListsToErase[0] = &Connection->ReceiveRequest;
165 IrpStatus [0] = STATUS_SUCCESS;
166 ListsToErase[1] = &Connection->ListenRequest;
167 IrpStatus [1] = STATUS_UNSUCCESSFUL;
168 ListsToErase[2] = &Connection->ConnectRequest;
169 IrpStatus [2] = STATUS_UNSUCCESSFUL;
170 ListsToErase[3] = 0;
171
172 for( i = 0; ListsToErase[i]; i++ ) {
173 while( !IsListEmpty( ListsToErase[i] ) ) {
174 Entry = RemoveHeadList( ListsToErase[i] );
175 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
176 Complete = Bucket->Request.RequestNotifyObject;
177 Complete( Bucket->Request.RequestContext, STATUS_SUCCESS, 0 );
178 }
179 }
180 }
181
182 Connection->Signalled = FALSE;
183 }
184
185 VOID DrainSignals() {
186 PCONNECTION_ENDPOINT Connection;
187 PLIST_ENTRY ListEntry;
188
189 while( !IsListEmpty( &SignalledConnections ) ) {
190 ListEntry = RemoveHeadList( &SignalledConnections );
191 Connection = CONTAINING_RECORD( ListEntry, CONNECTION_ENDPOINT,
192 SignalList );
193 HandleSignalledConnection( Connection, Connection->SignalState );
194 }
195 }
196
197 PCONNECTION_ENDPOINT TCPAllocateConnectionEndpoint( PVOID ClientContext ) {
198 PCONNECTION_ENDPOINT Connection =
199 ExAllocatePool(NonPagedPool, sizeof(CONNECTION_ENDPOINT));
200 if (!Connection)
201 return Connection;
202
203 TI_DbgPrint(DEBUG_CPOINT, ("Connection point file object allocated at (0x%X).\n", Connection));
204
205 RtlZeroMemory(Connection, sizeof(CONNECTION_ENDPOINT));
206
207 /* Initialize spin lock that protects the connection endpoint file object */
208 TcpipInitializeSpinLock(&Connection->Lock);
209 InitializeListHead(&Connection->ConnectRequest);
210 InitializeListHead(&Connection->ListenRequest);
211 InitializeListHead(&Connection->ReceiveRequest);
212
213 /* Save client context pointer */
214 Connection->ClientContext = ClientContext;
215
216 return Connection;
217 }
218
219 VOID TCPFreeConnectionEndpoint( PCONNECTION_ENDPOINT Connection ) {
220 TI_DbgPrint(MAX_TRACE,("FIXME: Cancel all pending requests\n"));
221 /* XXX Cancel all pending requests */
222 ExFreePool( Connection );
223 }
224
225 NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection,
226 UINT Family, UINT Type, UINT Proto ) {
227 NTSTATUS Status;
228
229 TI_DbgPrint(DEBUG_TCP,("Called: Connection %x, Family %d, Type %d, "
230 "Proto %d\n",
231 Connection, Family, Type, Proto));
232
233 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
234 Status = TCPTranslateError( OskitTCPSocket( Connection,
235 &Connection->SocketContext,
236 Family,
237 Type,
238 Proto ) );
239
240 ASSERT_KM_POINTER(Connection->SocketContext);
241
242 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext %x\n",
243 Connection->SocketContext));
244
245 TcpipRecursiveMutexLeave( &TCPLock );
246
247 return Status;
248 }
249
250 VOID TCPReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
251 /*
252 * FUNCTION: Receives and queues TCP data
253 * ARGUMENTS:
254 * IPPacket = Pointer to an IP packet that was received
255 * NOTES:
256 * This is the low level interface for receiving TCP data
257 */
258 {
259 TI_DbgPrint(DEBUG_TCP,("Sending packet %d (%d) to oskit\n",
260 IPPacket->TotalSize,
261 IPPacket->HeaderSize));
262
263 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
264
265 OskitTCPReceiveDatagram( IPPacket->Header,
266 IPPacket->TotalSize,
267 IPPacket->HeaderSize );
268
269 DrainSignals();
270
271 TcpipRecursiveMutexLeave( &TCPLock );
272 }
273
274 /* event.c */
275 int TCPSocketState( void *ClientData,
276 void *WhichSocket,
277 void *WhichConnection,
278 OSK_UINT NewState );
279
280 int TCPPacketSend( void *ClientData,
281 OSK_PCHAR Data,
282 OSK_UINT Len );
283
284 POSK_IFADDR TCPFindInterface( void *ClientData,
285 OSK_UINT AddrType,
286 OSK_UINT FindType,
287 OSK_SOCKADDR *ReqAddr );
288
289 void *TCPMalloc( void *ClientData,
290 OSK_UINT bytes, OSK_PCHAR file, OSK_UINT line );
291 void TCPFree( void *ClientData,
292 void *data, OSK_PCHAR file, OSK_UINT line );
293
294 int TCPSleep( void *ClientData, void *token, int priority, char *msg,
295 int tmio );
296
297 void TCPWakeup( void *ClientData, void *token );
298
299 OSKITTCP_EVENT_HANDLERS EventHandlers = {
300 NULL, /* Client Data */
301 TCPSocketState, /* SocketState */
302 TCPPacketSend, /* PacketSend */
303 TCPFindInterface, /* FindInterface */
304 TCPMalloc, /* Malloc */
305 TCPFree, /* Free */
306 TCPSleep, /* Sleep */
307 TCPWakeup /* Wakeup */
308 };
309
310 static KEVENT TimerLoopEvent;
311 static HANDLE TimerThreadHandle;
312
313 /*
314 * We are running 2 timers here, one with a 200ms interval (fast) and the other
315 * with a 500ms interval (slow). So we need to time out at 200, 400, 500, 600,
316 * 800, 1000 and process the "fast" events at 200, 400, 600, 800, 1000 and the
317 * "slow" events at 500 and 1000.
318 */
319 static VOID DDKAPI
320 TimerThread(PVOID Context)
321 {
322 LARGE_INTEGER Timeout;
323 NTSTATUS Status;
324 unsigned Current, NextFast, NextSlow, Next;
325
326 Current = 0;
327 Next = 0;
328 NextFast = 0;
329 NextSlow = 0;
330 while ( 1 ) {
331 if (Next == NextFast) {
332 NextFast += 2;
333 }
334 if (Next == NextSlow) {
335 NextSlow += 5;
336 }
337 Next = min(NextFast, NextSlow);
338 Timeout.QuadPart = (LONGLONG) (Next - Current) * -1000000; /* 100 ms */
339 Status = KeWaitForSingleObject(&TimerLoopEvent, Executive, KernelMode,
340 FALSE, &Timeout);
341 if (STATUS_SUCCESS == Status) {
342 PsTerminateSystemThread(STATUS_SUCCESS);
343 }
344 ASSERT(STATUS_TIMEOUT == Status);
345
346 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
347 TimerOskitTCP( Next == NextFast, Next == NextSlow );
348 if (Next == NextSlow) {
349 DrainSignals();
350 }
351 TcpipRecursiveMutexLeave( &TCPLock );
352
353 Current = Next;
354 if (10 <= Current) {
355 Current = 0;
356 Next = 0;
357 NextFast = 0;
358 NextSlow = 0;
359 }
360 }
361 }
362
363 static VOID
364 StartTimer(VOID)
365 {
366 KeInitializeEvent(&TimerLoopEvent, NotificationEvent, FALSE);
367 PsCreateSystemThread(&TimerThreadHandle, THREAD_ALL_ACCESS, 0, 0, 0,
368 TimerThread, NULL);
369 }
370
371
372 NTSTATUS TCPStartup(VOID)
373 /*
374 * FUNCTION: Initializes the TCP subsystem
375 * RETURNS:
376 * Status of operation
377 */
378 {
379 TcpipRecursiveMutexInit( &TCPLock );
380 ExInitializeFastMutex( &SleepingThreadsLock );
381 InitializeListHead( &SleepingThreadsList );
382 InitializeListHead( &SignalledConnections );
383
384 PortsStartup( &TCPPorts, 1, 0xfffe );
385
386 RegisterOskitTCPEventHandlers( &EventHandlers );
387 InitOskitTCP();
388
389 /* Register this protocol with IP layer */
390 IPRegisterProtocol(IPPROTO_TCP, TCPReceive);
391
392 ExInitializeNPagedLookasideList(
393 &TCPSegmentList, /* Lookaside list */
394 NULL, /* Allocate routine */
395 NULL, /* Free routine */
396 0, /* Flags */
397 sizeof(TCP_SEGMENT), /* Size of each entry */
398 TAG('T','C','P','S'), /* Tag */
399 0); /* Depth */
400
401 StartTimer();
402
403 TCPInitialized = TRUE;
404
405 return STATUS_SUCCESS;
406 }
407
408
409 NTSTATUS TCPShutdown(VOID)
410 /*
411 * FUNCTION: Shuts down the TCP subsystem
412 * RETURNS:
413 * Status of operation
414 */
415 {
416 LARGE_INTEGER WaitForThread;
417
418 if (!TCPInitialized)
419 return STATUS_SUCCESS;
420
421 WaitForThread.QuadPart = -2500000; /* 250 ms */
422 KeSetEvent(&TimerLoopEvent, IO_NO_INCREMENT, FALSE);
423 ZwWaitForSingleObject(TimerThreadHandle, FALSE, &WaitForThread);
424
425 /* Deregister this protocol with IP layer */
426 IPRegisterProtocol(IPPROTO_TCP, NULL);
427
428 ExDeleteNPagedLookasideList(&TCPSegmentList);
429
430 TCPInitialized = FALSE;
431
432 DeinitOskitTCP();
433
434 PortsShutdown( &TCPPorts );
435
436 return STATUS_SUCCESS;
437 }
438
439 NTSTATUS TCPTranslateError( int OskitError ) {
440 NTSTATUS Status = STATUS_UNSUCCESSFUL;
441
442 switch( OskitError ) {
443 case 0: Status = STATUS_SUCCESS; break;
444 case OSK_EADDRNOTAVAIL:
445 case OSK_EAFNOSUPPORT: Status = STATUS_INVALID_CONNECTION; break;
446 case OSK_ECONNREFUSED:
447 case OSK_ECONNRESET: Status = STATUS_REMOTE_NOT_LISTENING; break;
448 case OSK_EINPROGRESS:
449 case OSK_EAGAIN: Status = STATUS_PENDING; break;
450 default: Status = STATUS_INVALID_CONNECTION; break;
451 }
452
453 TI_DbgPrint(DEBUG_TCP,("Error %d -> %x\n", OskitError, Status));
454 return Status;
455 }
456
457 NTSTATUS TCPConnect
458 ( PCONNECTION_ENDPOINT Connection,
459 PTDI_CONNECTION_INFORMATION ConnInfo,
460 PTDI_CONNECTION_INFORMATION ReturnInfo,
461 PTCP_COMPLETION_ROUTINE Complete,
462 PVOID Context ) {
463 NTSTATUS Status;
464 SOCKADDR_IN AddressToConnect = { 0 }, AddressToBind = { 0 };
465 IP_ADDRESS RemoteAddress;
466 USHORT RemotePort;
467 PTDI_BUCKET Bucket;
468
469 DbgPrint("TCPConnect: Called\n");
470
471 Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
472 if( !Bucket ) return STATUS_NO_MEMORY;
473
474 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
475
476 /* Freed in TCPSocketState */
477 Bucket->Request.RequestNotifyObject = (PVOID)Complete;
478 Bucket->Request.RequestContext = Context;
479
480 InsertHeadList( &Connection->ConnectRequest, &Bucket->Entry );
481
482 Status = AddrBuildAddress
483 ((PTRANSPORT_ADDRESS)ConnInfo->RemoteAddress,
484 &RemoteAddress,
485 &RemotePort);
486
487 DbgPrint("Connecting to address %x:%x\n",
488 RemoteAddress.Address.IPv4Address,
489 RemotePort);
490
491 if (!NT_SUCCESS(Status)) {
492 TI_DbgPrint(DEBUG_TCP, ("Could not AddrBuildAddress in TCPConnect\n"));
493 return Status;
494 }
495
496 AddressToConnect.sin_family = AF_INET;
497 AddressToBind = AddressToConnect;
498
499 OskitTCPBind( Connection->SocketContext,
500 Connection,
501 &AddressToBind,
502 sizeof(AddressToBind) );
503
504 memcpy( &AddressToConnect.sin_addr,
505 &RemoteAddress.Address.IPv4Address,
506 sizeof(AddressToConnect.sin_addr) );
507 AddressToConnect.sin_port = RemotePort;
508
509 Status = TCPTranslateError
510 ( OskitTCPConnect( Connection->SocketContext,
511 Connection,
512 &AddressToConnect,
513 sizeof(AddressToConnect) ) );
514
515 TcpipRecursiveMutexLeave( &TCPLock );
516
517 if( Status == OSK_EINPROGRESS )
518 return STATUS_PENDING;
519 else
520 return Status;
521 }
522
523 NTSTATUS TCPDisconnect
524 ( PCONNECTION_ENDPOINT Connection,
525 UINT Flags,
526 PTDI_CONNECTION_INFORMATION ConnInfo,
527 PTDI_CONNECTION_INFORMATION ReturnInfo,
528 PTCP_COMPLETION_ROUTINE Complete,
529 PVOID Context ) {
530 NTSTATUS Status;
531
532 TI_DbgPrint(DEBUG_TCP,("started\n"));
533
534 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
535
536 switch( Flags & (TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE) ) {
537 case 0:
538 case TDI_DISCONNECT_ABORT:
539 Flags = 0;
540 break;
541
542 case TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE:
543 Flags = 2;
544 break;
545
546 case TDI_DISCONNECT_RELEASE:
547 Flags = 1;
548 break;
549 }
550
551 Status = TCPTranslateError
552 ( OskitTCPShutdown( Connection->SocketContext, Flags ) );
553
554 TcpipRecursiveMutexLeave( &TCPLock );
555
556 TI_DbgPrint(DEBUG_TCP,("finished %x\n", Status));
557
558 return Status;
559 }
560
561 NTSTATUS TCPClose
562 ( PCONNECTION_ENDPOINT Connection ) {
563 NTSTATUS Status;
564
565 TI_DbgPrint(DEBUG_TCP,("TCPClose started\n"));
566
567 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
568
569 Status = TCPTranslateError( OskitTCPClose( Connection->SocketContext ) );
570
571 /* Make our code remove all pending IRPs */
572 Connection->State |= SEL_FIN;
573 DrainSignals();
574
575 TcpipRecursiveMutexLeave( &TCPLock );
576
577 TI_DbgPrint(DEBUG_TCP,("TCPClose finished %x\n", Status));
578
579 return Status;
580 }
581
582 NTSTATUS TCPReceiveData
583 ( PCONNECTION_ENDPOINT Connection,
584 PNDIS_BUFFER Buffer,
585 ULONG ReceiveLength,
586 PULONG BytesReceived,
587 ULONG ReceiveFlags,
588 PTCP_COMPLETION_ROUTINE Complete,
589 PVOID Context ) {
590 OSK_PCHAR DataBuffer;
591 UINT DataLen, Received = 0;
592 NTSTATUS Status;
593 PTDI_BUCKET Bucket;
594
595 TI_DbgPrint(DEBUG_TCP,("Called for %d bytes (on socket %x)\n",
596 ReceiveLength, Connection->SocketContext));
597
598 ASSERT_KM_POINTER(Connection->SocketContext);
599
600 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
601
602 NdisQueryBuffer( Buffer, &DataBuffer, &DataLen );
603
604 TI_DbgPrint(DEBUG_TCP,("TCP>|< Got an MDL %x (%x:%d)\n", Buffer, DataBuffer, DataLen));
605
606 Status = TCPTranslateError
607 ( OskitTCPRecv
608 ( Connection->SocketContext,
609 DataBuffer,
610 DataLen,
611 &Received,
612 ReceiveFlags ) );
613
614 TI_DbgPrint(DEBUG_TCP,("OskitTCPReceive: %x, %d\n", Status, Received));
615
616 /* Keep this request around ... there was no data yet */
617 if( Status == STATUS_PENDING ) {
618 /* Freed in TCPSocketState */
619 Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
620 if( !Bucket ) {
621 TI_DbgPrint(DEBUG_TCP,("Failed to allocate bucket\n"));
622 TcpipRecursiveMutexLeave( &TCPLock );
623 return STATUS_NO_MEMORY;
624 }
625
626 Bucket->Request.RequestNotifyObject = Complete;
627 Bucket->Request.RequestContext = Context;
628 *BytesReceived = 0;
629
630 InsertHeadList( &Connection->ReceiveRequest, &Bucket->Entry );
631 Status = STATUS_PENDING;
632 TI_DbgPrint(DEBUG_TCP,("Queued read irp\n"));
633 } else {
634 TI_DbgPrint(DEBUG_TCP,("Got status %x, bytes %d\n", Status, Received));
635 *BytesReceived = Received;
636 }
637
638 TcpipRecursiveMutexLeave( &TCPLock );
639
640 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
641
642 return Status;
643 }
644
645 NTSTATUS TCPSendData
646 ( PCONNECTION_ENDPOINT Connection,
647 PCHAR BufferData,
648 ULONG PacketSize,
649 PULONG DataUsed,
650 ULONG Flags) {
651 NTSTATUS Status;
652
653 ASSERT_KM_POINTER(Connection->SocketContext);
654
655 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
656
657 TI_DbgPrint(DEBUG_TCP,("Connection = %x\n", Connection));
658 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext = %x\n",
659 Connection->SocketContext));
660
661 Status = OskitTCPSend( Connection->SocketContext,
662 (OSK_PCHAR)BufferData, PacketSize,
663 (PUINT)DataUsed, 0 );
664
665 TcpipRecursiveMutexLeave( &TCPLock );
666
667 return Status;
668 }
669
670 VOID TCPTimeout(VOID) {
671 /* Now handled by TimerThread */
672 }
673
674 UINT TCPAllocatePort( UINT HintPort ) {
675 if( HintPort ) {
676 if( AllocatePort( &TCPPorts, HintPort ) ) return HintPort;
677 else {
678 TI_DbgPrint
679 (MID_TRACE,("We got a hint port but couldn't allocate it\n"));
680 return (UINT)-1;
681 }
682 } else return AllocatePortFromRange( &TCPPorts, 1024, 5000 );
683 }
684
685 VOID TCPFreePort( UINT Port ) {
686 DeallocatePort( &TCPPorts, Port );
687 }
688
689 NTSTATUS TCPGetPeerAddress
690 ( PCONNECTION_ENDPOINT Connection,
691 PTRANSPORT_ADDRESS Address ) {
692 OSK_UINT LocalAddress, RemoteAddress;
693 OSK_UI16 LocalPort, RemotePort;
694 PTA_IP_ADDRESS AddressIP = (PTA_IP_ADDRESS)Address;
695
696 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
697
698 OskitTCPGetAddress
699 ( Connection->SocketContext,
700 &LocalAddress, &LocalPort,
701 &RemoteAddress, &RemotePort );
702
703 AddressIP->TAAddressCount = 1;
704 AddressIP->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
705 AddressIP->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
706 AddressIP->Address[0].Address[0].sin_port = RemotePort;
707 AddressIP->Address[0].Address[0].in_addr = RemoteAddress;
708
709 TcpipRecursiveMutexLeave( &TCPLock );
710
711 return STATUS_SUCCESS;
712 }
713
714 VOID TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp ) {
715 PLIST_ENTRY Entry;
716 PLIST_ENTRY ListHead[4];
717 KIRQL OldIrql;
718 PTDI_BUCKET Bucket;
719 UINT i = 0;
720
721 ListHead[0] = &Endpoint->ReceiveRequest;
722 ListHead[1] = &Endpoint->ConnectRequest;
723 ListHead[2] = &Endpoint->ListenRequest;
724 ListHead[3] = 0;
725
726 TcpipAcquireSpinLock( &Endpoint->Lock, &OldIrql );
727
728 for( i = 0; ListHead[i]; i++ ) {
729 for( Entry = ListHead[i]->Flink;
730 Entry != ListHead[i];
731 Entry = Entry->Flink ) {
732 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
733
734 if( Bucket->Request.RequestContext == Irp ) {
735 RemoveEntryList( &Bucket->Entry );
736 break;
737 }
738 }
739 }
740
741 TcpipReleaseSpinLock( &Endpoint->Lock, OldIrql );
742 }
743
744 /* EOF */