- Return STATUS_NETWORK_UNREACHABLE if we can't get a route to the remote address
[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.RequestContext));
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->SendRequest) ?
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 TI_DbgPrint(DEBUG_TCP, ("Freeing TCP Endpoint\n"));
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 if (Status != STATUS_SUCCESS)
531 DbgPrint("%d -> %x\n", OskitError, Status);
532
533 TI_DbgPrint(DEBUG_TCP,("Error %d -> %x\n", OskitError, Status));
534 return Status;
535 }
536
537 NTSTATUS TCPConnect
538 ( PCONNECTION_ENDPOINT Connection,
539 PTDI_CONNECTION_INFORMATION ConnInfo,
540 PTDI_CONNECTION_INFORMATION ReturnInfo,
541 PTCP_COMPLETION_ROUTINE Complete,
542 PVOID Context ) {
543 NTSTATUS Status;
544 SOCKADDR_IN AddressToConnect = { 0 }, AddressToBind = { 0 };
545 IP_ADDRESS RemoteAddress;
546 USHORT RemotePort;
547 PTDI_BUCKET Bucket;
548 PNEIGHBOR_CACHE_ENTRY NCE;
549
550 TI_DbgPrint(DEBUG_TCP,("TCPConnect: Called\n"));
551
552 Status = AddrBuildAddress
553 ((PTRANSPORT_ADDRESS)ConnInfo->RemoteAddress,
554 &RemoteAddress,
555 &RemotePort);
556
557 if (!(NCE = RouteGetRouteToDestination(&RemoteAddress)))
558 {
559 return STATUS_NETWORK_UNREACHABLE;
560 }
561
562 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
563
564 if (Connection->State & SEL_FIN)
565 {
566 TcpipRecursiveMutexLeave( &TCPLock );
567 return STATUS_REMOTE_DISCONNECT;
568 }
569
570 /* Freed in TCPSocketState */
571 TI_DbgPrint(DEBUG_TCP,
572 ("Connecting to address %x:%x\n",
573 RemoteAddress.Address.IPv4Address,
574 RemotePort));
575
576 if (!NT_SUCCESS(Status)) {
577 TI_DbgPrint(DEBUG_TCP, ("Could not AddrBuildAddress in TCPConnect\n"));
578 TcpipRecursiveMutexLeave( &TCPLock );
579 return Status;
580 }
581
582 AddressToConnect.sin_family = AF_INET;
583 AddressToBind = AddressToConnect;
584 AddressToBind.sin_addr.s_addr = NCE->Interface->Unicast.Address.IPv4Address;
585
586 Status = TCPTranslateError
587 ( OskitTCPBind( Connection->SocketContext,
588 Connection,
589 &AddressToBind,
590 sizeof(AddressToBind) ) );
591
592 if (NT_SUCCESS(Status)) {
593 memcpy( &AddressToConnect.sin_addr,
594 &RemoteAddress.Address.IPv4Address,
595 sizeof(AddressToConnect.sin_addr) );
596 AddressToConnect.sin_port = RemotePort;
597
598 Status = TCPTranslateError
599 ( OskitTCPConnect( Connection->SocketContext,
600 Connection,
601 &AddressToConnect,
602 sizeof(AddressToConnect) ) );
603
604 if (Status == STATUS_PENDING)
605 {
606 Bucket = exAllocatePool( NonPagedPool, sizeof(*Bucket) );
607 if( !Bucket ) return STATUS_NO_MEMORY;
608
609 Bucket->Request.RequestNotifyObject = (PVOID)Complete;
610 Bucket->Request.RequestContext = Context;
611
612 InsertHeadList( &Connection->ConnectRequest, &Bucket->Entry );
613 }
614 }
615
616 TcpipRecursiveMutexLeave( &TCPLock );
617
618 return Status;
619 }
620
621 NTSTATUS TCPDisconnect
622 ( PCONNECTION_ENDPOINT Connection,
623 UINT Flags,
624 PTDI_CONNECTION_INFORMATION ConnInfo,
625 PTDI_CONNECTION_INFORMATION ReturnInfo,
626 PTCP_COMPLETION_ROUTINE Complete,
627 PVOID Context ) {
628 NTSTATUS Status;
629
630 TI_DbgPrint(DEBUG_TCP,("started\n"));
631
632 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
633
634 switch( Flags & (TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE) ) {
635 case 0:
636 case TDI_DISCONNECT_ABORT:
637 Flags = 0;
638 break;
639
640 case TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE:
641 Flags = 2;
642 break;
643
644 case TDI_DISCONNECT_RELEASE:
645 Flags = 1;
646 break;
647 }
648
649 Status = TCPTranslateError
650 ( OskitTCPShutdown( Connection->SocketContext, Flags ) );
651
652 TcpipRecursiveMutexLeave( &TCPLock );
653
654 TI_DbgPrint(DEBUG_TCP,("finished %x\n", Status));
655
656 return Status;
657 }
658
659 NTSTATUS TCPClose
660 ( PCONNECTION_ENDPOINT Connection ) {
661 NTSTATUS Status;
662
663 TI_DbgPrint(DEBUG_TCP,("TCPClose started\n"));
664
665 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
666
667 Status = TCPTranslateError( OskitTCPClose( Connection->SocketContext ) );
668
669 /* Make our code remove all pending IRPs */
670 Connection->State |= SEL_FIN;
671 DrainSignals();
672
673 TcpipRecursiveMutexLeave( &TCPLock );
674
675 TI_DbgPrint(DEBUG_TCP,("TCPClose finished %x\n", Status));
676
677 return Status;
678 }
679
680 NTSTATUS TCPReceiveData
681 ( PCONNECTION_ENDPOINT Connection,
682 PNDIS_BUFFER Buffer,
683 ULONG ReceiveLength,
684 PULONG BytesReceived,
685 ULONG ReceiveFlags,
686 PTCP_COMPLETION_ROUTINE Complete,
687 PVOID Context ) {
688 OSK_PCHAR DataBuffer;
689 UINT DataLen, Received = 0;
690 NTSTATUS Status;
691 PTDI_BUCKET Bucket;
692
693 TI_DbgPrint(DEBUG_TCP,("Called for %d bytes (on socket %x)\n",
694 ReceiveLength, Connection->SocketContext));
695
696 ASSERT_KM_POINTER(Connection->SocketContext);
697
698 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
699
700 /* Closing */
701 if (Connection->State & SEL_FIN)
702 {
703 TcpipRecursiveMutexLeave( &TCPLock );
704 *BytesReceived = 0;
705 return STATUS_REMOTE_DISCONNECT;
706 }
707
708 NdisQueryBuffer( Buffer, &DataBuffer, &DataLen );
709
710 TI_DbgPrint(DEBUG_TCP,("TCP>|< Got an MDL %x (%x:%d)\n", Buffer, DataBuffer, DataLen));
711
712 Status = TCPTranslateError
713 ( OskitTCPRecv
714 ( Connection->SocketContext,
715 DataBuffer,
716 DataLen,
717 &Received,
718 ReceiveFlags ) );
719
720 TI_DbgPrint(DEBUG_TCP,("OskitTCPReceive: %x, %d\n", Status, Received));
721
722 /* Keep this request around ... there was no data yet */
723 if( Status == STATUS_PENDING ) {
724 /* Freed in TCPSocketState */
725 Bucket = exAllocatePool( NonPagedPool, sizeof(*Bucket) );
726 if( !Bucket ) {
727 TI_DbgPrint(DEBUG_TCP,("Failed to allocate bucket\n"));
728 TcpipRecursiveMutexLeave( &TCPLock );
729 return STATUS_NO_MEMORY;
730 }
731
732 Bucket->Request.RequestNotifyObject = Complete;
733 Bucket->Request.RequestContext = Context;
734 *BytesReceived = 0;
735
736 InsertTailList( &Connection->ReceiveRequest, &Bucket->Entry );
737 Status = STATUS_PENDING;
738 TI_DbgPrint(DEBUG_TCP,("Queued read irp\n"));
739 } else {
740 TI_DbgPrint(DEBUG_TCP,("Got status %x, bytes %d\n", Status, Received));
741 *BytesReceived = Received;
742 }
743
744 TcpipRecursiveMutexLeave( &TCPLock );
745
746 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
747
748 return Status;
749 }
750
751 NTSTATUS TCPSendData
752 ( PCONNECTION_ENDPOINT Connection,
753 PCHAR BufferData,
754 ULONG SendLength,
755 PULONG BytesSent,
756 ULONG Flags,
757 PTCP_COMPLETION_ROUTINE Complete,
758 PVOID Context ) {
759 UINT Sent = 0;
760 NTSTATUS Status;
761 PTDI_BUCKET Bucket;
762
763 TI_DbgPrint(DEBUG_TCP,("Called for %d bytes (on socket %x)\n",
764 SendLength, Connection->SocketContext));
765
766 ASSERT_KM_POINTER(Connection->SocketContext);
767
768 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
769
770 TI_DbgPrint(DEBUG_TCP,("Connection = %x\n", Connection));
771 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext = %x\n",
772 Connection->SocketContext));
773
774 /* Closing */
775 if (Connection->State & SEL_FIN)
776 {
777 TcpipRecursiveMutexLeave( &TCPLock );
778 *BytesSent = 0;
779 return STATUS_REMOTE_DISCONNECT;
780 }
781
782 Status = TCPTranslateError
783 ( OskitTCPSend( Connection->SocketContext,
784 (OSK_PCHAR)BufferData, SendLength,
785 &Sent, 0 ) );
786
787 TI_DbgPrint(DEBUG_TCP,("OskitTCPSend: %x, %d\n", Status, Sent));
788
789 /* Keep this request around ... there was no data yet */
790 if( Status == STATUS_PENDING ) {
791 /* Freed in TCPSocketState */
792 Bucket = exAllocatePool( NonPagedPool, sizeof(*Bucket) );
793 if( !Bucket ) {
794 TI_DbgPrint(DEBUG_TCP,("Failed to allocate bucket\n"));
795 TcpipRecursiveMutexLeave( &TCPLock );
796 return STATUS_NO_MEMORY;
797 }
798
799 Bucket->Request.RequestNotifyObject = Complete;
800 Bucket->Request.RequestContext = Context;
801 *BytesSent = 0;
802
803 InsertTailList( &Connection->SendRequest, &Bucket->Entry );
804 TI_DbgPrint(DEBUG_TCP,("Queued write irp\n"));
805 } else {
806 TI_DbgPrint(DEBUG_TCP,("Got status %x, bytes %d\n", Status, Sent));
807 *BytesSent = Sent;
808 }
809
810 TcpipRecursiveMutexLeave( &TCPLock );
811
812 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
813
814 return Status;
815 }
816
817 VOID TCPTimeout(VOID) {
818 /* Now handled by TimerThread */
819 }
820
821 UINT TCPAllocatePort( UINT HintPort ) {
822 if( HintPort ) {
823 if( AllocatePort( &TCPPorts, HintPort ) ) return HintPort;
824 else {
825 TI_DbgPrint
826 (MID_TRACE,("We got a hint port but couldn't allocate it\n"));
827 return (UINT)-1;
828 }
829 } else return AllocatePortFromRange( &TCPPorts, 1024, 5000 );
830 }
831
832 VOID TCPFreePort( UINT Port ) {
833 DeallocatePort( &TCPPorts, Port );
834 }
835
836 NTSTATUS TCPGetSockAddress
837 ( PCONNECTION_ENDPOINT Connection,
838 PTRANSPORT_ADDRESS Address,
839 BOOLEAN GetRemote ) {
840 OSK_UINT LocalAddress, RemoteAddress;
841 OSK_UI16 LocalPort, RemotePort;
842 PTA_IP_ADDRESS AddressIP = (PTA_IP_ADDRESS)Address;
843
844 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
845
846 OskitTCPGetAddress
847 ( Connection->SocketContext,
848 &LocalAddress, &LocalPort,
849 &RemoteAddress, &RemotePort );
850
851 AddressIP->TAAddressCount = 1;
852 AddressIP->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
853 AddressIP->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
854 AddressIP->Address[0].Address[0].sin_port = GetRemote ? RemotePort : LocalPort;
855 AddressIP->Address[0].Address[0].in_addr = GetRemote ? RemoteAddress : LocalAddress;
856
857 TcpipRecursiveMutexLeave( &TCPLock );
858
859 return STATUS_SUCCESS;
860 }
861
862 VOID TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp ) {
863 PLIST_ENTRY Entry;
864 PLIST_ENTRY ListHead[4];
865 KIRQL OldIrql;
866 PTDI_BUCKET Bucket;
867 UINT i = 0;
868
869 ListHead[0] = &Endpoint->SendRequest;
870 ListHead[1] = &Endpoint->ReceiveRequest;
871 ListHead[2] = &Endpoint->ConnectRequest;
872 ListHead[3] = &Endpoint->ListenRequest;
873
874 TcpipAcquireSpinLock( &Endpoint->Lock, &OldIrql );
875
876 for( i = 0; i < 4; i++ )
877 {
878 for( Entry = ListHead[i]->Flink;
879 Entry != ListHead[i];
880 Entry = Entry->Flink )
881 {
882 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
883 if( Bucket->Request.RequestContext == Irp )
884 {
885 RemoveEntryList( &Bucket->Entry );
886 exFreePool( Bucket );
887 break;
888 }
889 }
890 }
891
892 TcpipReleaseSpinLock( &Endpoint->Lock, OldIrql );
893 }
894
895 /* EOF */