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