11 #define HEXDUMP_INCOMING 0
12 #define HEXDUMP_OUTGOING 0
14 #define TCP_MIN_DYNPORT 0xC000
15 #define TCP_MAX_HALFOPEN 1024 // Should be enough
17 #define TCP_MAX_PACKET_SIZE 1024
18 #define TCP_WINDOW_SIZE 0x2000
19 #define TCP_RECIEVE_BUFFER_SIZE 0x8000
20 #define TCP_DACK_THRESHOLD 4096
21 #define TCP_DACK_TIMEOUT 500
23 #define TCP_DEBUG 0 // Set to non-0 to enable TCP packet logging
26 void TCP_Initialise(void);
27 void TCP_StartConnection(tTCPConnection *Conn);
28 void TCP_SendPacket(tTCPConnection *Conn, tTCPHeader *Header, size_t DataLen, const void *Data);
29 void TCP_int_SendPacket(tInterface *Interface, const void *Dest, tTCPHeader *Header, size_t Length, const void *Data);
30 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
31 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length);
32 int TCP_INT_AppendRecieved(tTCPConnection *Connection, const void *Data, size_t Length);
33 void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection);
34 void TCP_int_SendDelayedACK(void *ConnPtr);
35 void TCP_INT_SendACK(tTCPConnection *Connection, const char *Reason);
36 Uint16 TCP_GetUnusedPort();
37 int TCP_AllocatePort(Uint16 Port);
38 int TCP_DeallocatePort(Uint16 Port);
39 tTCPConnection *TCP_int_CreateConnection(tInterface *Interface, enum eTCPConnectionState State);
40 void TCP_int_FreeTCB(tTCPConnection *Connection);
42 tVFS_Node *TCP_Server_Init(tInterface *Interface);
43 int TCP_Server_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX]);
44 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
45 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data);
46 void TCP_Server_Close(tVFS_Node *Node);
48 tVFS_Node *TCP_Client_Init(tInterface *Interface);
49 size_t TCP_Client_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
50 size_t TCP_Client_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
51 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data);
52 void TCP_Client_Close(tVFS_Node *Node);
54 int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue);
57 tSocketFile gTCP_ServerFile = {NULL, "tcps", TCP_Server_Init};
58 tSocketFile gTCP_ClientFile = {NULL, "tcpc", TCP_Client_Init};
59 tVFS_NodeType gTCP_ServerNodeType = {
60 .TypeName = "TCP Server",
61 .ReadDir = TCP_Server_ReadDir,
62 .FindDir = TCP_Server_FindDir,
63 .IOCtl = TCP_Server_IOCtl,
64 .Close = TCP_Server_Close
66 tVFS_NodeType gTCP_ClientNodeType = {
67 .TypeName = "TCP Client/Connection",
68 .Read = TCP_Client_Read,
69 .Write = TCP_Client_Write,
70 .IOCtl = TCP_Client_IOCtl,
71 .Close = TCP_Client_Close
75 int giTCP_NumHalfopen = 0;
76 tShortSpinlock glTCP_Listeners;
77 tTCPListener *gTCP_Listeners;
78 tShortSpinlock glTCP_OutbountCons;
79 tTCPConnection *gTCP_OutbountCons;
80 Uint32 gaTCP_PortBitmap[0x800];
81 int giTCP_NextOutPort = TCP_MIN_DYNPORT;
85 * \brief Initialise the TCP Layer
87 * Registers the client and server files and the GetPacket callback
89 void TCP_Initialise(void)
91 giTCP_NextOutPort += rand()%128;
92 IPStack_AddFile(&gTCP_ServerFile);
93 IPStack_AddFile(&gTCP_ClientFile);
94 IPv4_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
95 IPv6_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
99 * \brief Sends a packet from the specified connection, calculating the checksums
100 * \param Conn Connection
101 * \param Length Length of data
102 * \param Data Packet data (cast as a TCP Header)
104 void TCP_SendPacket( tTCPConnection *Conn, tTCPHeader *Header, size_t Length, const void *Data )
106 TCP_int_SendPacket(Conn->Interface, &Conn->RemoteIP, Header, Length, Data);
109 void TCP_int_SendPacket(tInterface *Interface, const void *Dest, tTCPHeader *Header, size_t Length, const void *Data )
111 tIPStackBuffer *buffer;
113 int packlen = sizeof(*Header) + Length;
115 buffer = IPStack_Buffer_CreateBuffer(2 + IPV4_BUFFERS);
117 IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, Data, NULL, NULL);
118 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(*Header), 0, Header, NULL, NULL);
120 LOG("Sending %i+%i to %s:%i", sizeof(*Header), Length,
121 IPStack_PrintAddress(Interface->Type, Dest),
122 ntohs(Header->RemotePort)
125 Header->Checksum = 0;
126 checksum[1] = htons( ~IPv4_Checksum(Header, sizeof(tTCPHeader)) );
127 checksum[2] = htons( ~IPv4_Checksum(Data, Length) );
129 // TODO: Fragment packet
131 switch( Interface->Type )
134 // Get IPv4 pseudo-header checksum
137 buf[0] = ((tIPv4*)Interface->Address)->L;
138 buf[1] = ((tIPv4*)Dest)->L;
139 buf[2] = htonl( (packlen) | (IP4PROT_TCP<<16) | (0<<24) );
140 checksum[0] = htons( ~IPv4_Checksum(buf, sizeof(buf)) ); // Partial checksum
142 // - Combine checksums
143 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) );
144 IPv4_SendPacket(Interface, *(tIPv4*)Dest, IP4PROT_TCP, 0, buffer);
148 // Append IPv6 Pseudo Header
151 memcpy(buf, Interface->Address, 16);
152 memcpy(&buf[4], Dest, 16);
153 buf[8] = htonl(packlen);
154 buf[9] = htonl(IP4PROT_TCP);
155 checksum[0] = htons( ~IPv4_Checksum(buf, sizeof(buf)) ); // Partial checksum
157 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) ); // Combine the two
158 IPv6_SendPacket(Interface, *(tIPv6*)Dest, IP4PROT_TCP, buffer);
163 void TCP_int_SendRSTTo(tInterface *Interface, void *Address, size_t Length, const tTCPHeader *Header)
165 tTCPHeader out_hdr = {0};
167 out_hdr.DataOffset = (sizeof(out_hdr)/4) << 4;
168 out_hdr.DestPort = Header->SourcePort;
169 out_hdr.SourcePort = Header->DestPort;
171 size_t data_len = Length - (Header->DataOffset>>4)*4;
172 out_hdr.AcknowlegementNumber = htonl( ntohl(Header->SequenceNumber) + data_len );
173 if( Header->Flags & TCP_FLAG_ACK ) {
174 out_hdr.Flags = TCP_FLAG_RST;
175 out_hdr.SequenceNumber = Header->AcknowlegementNumber;
178 out_hdr.Flags = TCP_FLAG_RST|TCP_FLAG_ACK;
179 out_hdr.SequenceNumber = 0;
181 TCP_int_SendPacket(Interface, Address, &out_hdr, 0, NULL);
185 * \brief Handles a packet from the IP Layer
186 * \param Interface Interface the packet arrived from
187 * \param Address Pointer to the addres structure
188 * \param Length Size of packet in bytes
189 * \param Buffer Packet data
191 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
193 tTCPHeader *hdr = Buffer;
196 Log_Log("TCP", "TCP_GetPacket: <Local>:%i from [%s]:%i, Flags = %s%s%s%s%s%s%s%s",
197 ntohs(hdr->DestPort),
198 IPStack_PrintAddress(Interface->Type, Address),
199 ntohs(hdr->SourcePort),
200 (hdr->Flags & TCP_FLAG_CWR) ? "CWR " : "",
201 (hdr->Flags & TCP_FLAG_ECE) ? "ECE " : "",
202 (hdr->Flags & TCP_FLAG_URG) ? "URG " : "",
203 (hdr->Flags & TCP_FLAG_ACK) ? "ACK " : "",
204 (hdr->Flags & TCP_FLAG_PSH) ? "PSH " : "",
205 (hdr->Flags & TCP_FLAG_RST) ? "RST " : "",
206 (hdr->Flags & TCP_FLAG_SYN) ? "SYN " : "",
207 (hdr->Flags & TCP_FLAG_FIN) ? "FIN " : ""
211 if( Length > (hdr->DataOffset >> 4)*4 )
213 LOG("SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
216 "TCP_GetPacket: Packet Data = ",
217 (Uint8*)hdr + (hdr->DataOffset >> 4)*4,
218 Length - (hdr->DataOffset >> 4)*4
224 for( tTCPListener *srv = gTCP_Listeners; srv; srv = srv->Next )
226 // Check if the server is active
227 if(srv->Port == 0) continue;
228 // Check the interface
229 if(srv->Interface && srv->Interface != Interface) continue;
230 // Check the destination port
231 if(srv->Port != htons(hdr->DestPort)) continue;
233 Log_Log("TCP", "TCP_GetPacket: Matches server %p", srv);
234 // Is this in an established connection?
235 for( tTCPConnection *conn = srv->Connections; conn; conn = conn->Next )
237 // Check that it is coming in on the same interface
238 if(conn->Interface != Interface) continue;
241 Log_Log("TCP", "TCP_GetPacket: conn->RemotePort(%i) == hdr->SourcePort(%i)",
242 conn->RemotePort, ntohs(hdr->SourcePort));
243 if(conn->RemotePort != ntohs(hdr->SourcePort)) continue;
246 Log_Debug("TCP", "TCP_GetPacket: conn->RemoteIP(%s)",
247 IPStack_PrintAddress(conn->Interface->Type, &conn->RemoteIP));
248 Log_Debug("TCP", " == Address(%s)",
249 IPStack_PrintAddress(conn->Interface->Type, Address));
250 if( IPStack_CompareAddress(conn->Interface->Type, &conn->RemoteIP, Address, -1) == 0 )
253 Log_Log("TCP", "TCP_GetPacket: Matches connection %p", conn);
254 // We have a response!
255 TCP_INT_HandleConnectionPacket(conn, hdr, Length);
261 if( hdr->Flags & TCP_FLAG_RST ) {
265 else if( hdr->Flags & TCP_FLAG_ACK ) {
266 LOG("ACK, send RST");
267 TCP_int_SendRSTTo(Interface, Address, Length, hdr);
270 else if( !(hdr->Flags & TCP_FLAG_SYN) ) {
271 LOG("Other, ignore");
274 Log_Log("TCP", "TCP_GetPacket: Opening Connection");
276 // TODO: Check for halfopen max
278 tTCPConnection *conn = TCP_int_CreateConnection(Interface, TCP_ST_SYN_RCVD);
279 conn->LocalPort = srv->Port;
280 conn->RemotePort = ntohs(hdr->SourcePort);
282 switch(Interface->Type)
284 case 4: conn->RemoteIP.v4 = *(tIPv4*)Address; break;
285 case 6: conn->RemoteIP.v6 = *(tIPv6*)Address; break;
286 default: ASSERTC(Interface->Type,==,4); return;
289 conn->NextSequenceRcv = ntohl( hdr->SequenceNumber ) + 1;
290 conn->HighestSequenceRcvd = conn->NextSequenceRcv;
291 conn->NextSequenceSend = rand();
293 conn->Node.ImplInt = srv->NextID ++;
295 // Hmm... Theoretically, this lock will never have to wait,
296 // as the interface is locked to the watching thread, and this
297 // runs in the watching thread. But, it's a good idea to have
299 // Oh, wait, there is a case where a wildcard can be used
300 // (srv->Interface == NULL) so having the lock is a good idea
301 SHORTLOCK(&srv->lConnections);
303 conn->Prev = srv->ConnectionsTail;
304 if(srv->Connections) {
305 ASSERT(srv->ConnectionsTail);
306 srv->ConnectionsTail->Next = conn;
309 ASSERT(!srv->ConnectionsTail);
310 srv->Connections = conn;
312 srv->ConnectionsTail = conn;
313 if(!srv->NewConnections)
314 srv->NewConnections = conn;
315 VFS_MarkAvaliable( &srv->Node, 1 );
316 SHORTREL(&srv->lConnections);
317 Semaphore_Signal(&srv->WaitingConnections, 1);
320 hdr->Flags |= TCP_FLAG_ACK;
321 hdr->AcknowlegementNumber = htonl(conn->NextSequenceRcv);
322 hdr->SequenceNumber = htonl(conn->NextSequenceSend);
323 hdr->DestPort = hdr->SourcePort;
324 hdr->SourcePort = htons(srv->Port);
325 hdr->DataOffset = (sizeof(tTCPHeader)/4) << 4;
326 TCP_SendPacket( conn, hdr, 0, NULL );
327 conn->NextSequenceSend ++;
331 // Check Open Connections
333 for( tTCPConnection *conn = gTCP_OutbountCons; conn; conn = conn->Next )
335 // Check that it is coming in on the same interface
336 if(conn->Interface != Interface) continue;
339 if(conn->RemotePort != ntohs(hdr->SourcePort)) continue;
342 if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
344 if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
347 TCP_INT_HandleConnectionPacket(conn, hdr, Length);
352 Log_Log("TCP", "TCP_GetPacket: No Match");
353 // If not a RST, send a RST
354 if( !(hdr->Flags & TCP_FLAG_RST) )
356 TCP_int_SendRSTTo(Interface, Address, Length, hdr);
361 * \brief Handles a packet sent to a specific connection
362 * \param Connection TCP Connection pointer
363 * \param Header TCP Packet pointer
364 * \param Length Length of the packet
366 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length)
371 // Silently drop once finished
372 // TODO: Check if this needs to be here
373 if( Connection->State == TCP_ST_FINISHED ) {
374 Log_Log("TCP", "Packet ignored - connection finnished");
378 // Syncronise sequence values
379 if(Header->Flags & TCP_FLAG_SYN) {
380 // TODO: What if the packet also has data?
381 if( Connection->LastACKSequence != Connection->NextSequenceRcv )
382 TCP_INT_SendACK(Connection, "SYN");
383 Connection->NextSequenceRcv = ntohl(Header->SequenceNumber);
384 // TODO: Process HighestSequenceRcvd
386 if( Connection->HighestSequenceRcvd == 0 )
387 Connection->HighestSequenceRcvd = Connection->NextSequenceRcv;
388 Connection->LastACKSequence = Connection->NextSequenceRcv;
391 // Ackowledge a sent packet
392 if(Header->Flags & TCP_FLAG_ACK) {
393 // TODO: Process an ACKed Packet
394 LOG("Conn %p, Sent packet 0x%x ACKed", Connection, Header->AcknowlegementNumber);
397 // Get length of data
398 dataLen = Length - (Header->DataOffset>>4)*4;
399 LOG("dataLen = %i", dataLen);
401 Log_Debug("TCP", "State %i, dataLen = %x", Connection->State, dataLen);
407 switch( Connection->State )
409 // Pre-init connection?
411 Log_Log("TCP", "Packets to a closed connection?!");
414 // --- Init States ---
415 // SYN sent, expecting SYN-ACK Connection Opening
416 case TCP_ST_SYN_SENT:
417 if( Header->Flags & TCP_FLAG_SYN )
419 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
420 Connection->HighestSequenceRcvd ++;
421 Connection->NextSequenceRcv ++;
423 if( Header->Flags & TCP_FLAG_ACK )
425 Log_Log("TCP", "ACKing SYN-ACK");
426 Connection->State = TCP_ST_OPEN;
427 VFS_MarkFull(&Connection->Node, 0);
431 Log_Log("TCP", "ACKing SYN");
432 Connection->State = TCP_ST_SYN_RCVD;
434 Header->DestPort = Header->SourcePort;
435 Header->SourcePort = htons(Connection->LocalPort);
436 Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
437 Header->SequenceNumber = htonl(Connection->NextSequenceSend);
438 Header->WindowSize = htons(TCP_WINDOW_SIZE);
439 Header->Flags = TCP_FLAG_ACK;
440 Header->DataOffset = (sizeof(tTCPHeader)/4) << 4;
441 TCP_SendPacket( Connection, Header, 0, NULL );
445 // SYN-ACK sent, expecting ACK
446 case TCP_ST_SYN_RCVD:
447 if( Header->Flags & TCP_FLAG_ACK )
449 // TODO: Handle max half-open limit
450 Log_Log("TCP", "Connection fully opened");
451 Connection->State = TCP_ST_OPEN;
452 VFS_MarkFull(&Connection->Node, 0);
456 // --- Established State ---
458 // - Handle State changes
460 if( Header->Flags & TCP_FLAG_FIN ) {
461 Log_Log("TCP", "Conn %p closed, recieved FIN", Connection);
462 VFS_MarkError(&Connection->Node, 1);
463 Connection->NextSequenceRcv ++;
464 TCP_INT_SendACK(Connection, "FIN Received");
465 Connection->State = TCP_ST_CLOSE_WAIT;
466 // CLOSE WAIT requires the client to close
470 // Check for an empty packet
472 if( Header->Flags == TCP_FLAG_ACK )
474 Log_Log("TCP", "ACK only packet");
477 // TODO: Is this right? (empty packet counts as one byte)
478 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
479 Connection->HighestSequenceRcvd ++;
480 Connection->NextSequenceRcv ++;
481 Log_Log("TCP", "Empty Packet, inc and ACK the current sequence number");
482 TCP_INT_SendACK(Connection, "Empty");
491 sequence_num = ntohl(Header->SequenceNumber);
493 LOG("0x%08x <= 0x%08x < 0x%08x",
494 Connection->NextSequenceRcv,
495 ntohl(Header->SequenceNumber),
496 Connection->NextSequenceRcv + TCP_WINDOW_SIZE
499 // Is this packet the next expected packet?
500 if( sequence_num == Connection->NextSequenceRcv )
503 // Ooh, Goodie! Add it to the recieved list
504 rv = TCP_INT_AppendRecieved(Connection,
505 (Uint8*)Header + (Header->DataOffset>>4)*4,
509 Log_Notice("TCP", "TCP_INT_AppendRecieved rv %i", rv);
512 LOG("0x%08x += %i", Connection->NextSequenceRcv, dataLen);
513 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
514 Connection->HighestSequenceRcvd += dataLen;
515 Connection->NextSequenceRcv += dataLen;
517 // TODO: This should be moved out of the watcher thread,
518 // so that a single lost packet on one connection doesn't cause
519 // all connections on the interface to lag.
520 // - Meh, no real issue, as the cache shouldn't be that large
521 TCP_INT_UpdateRecievedFromFuture(Connection);
524 // - Only send an ACK if we've had a burst
525 if( Connection->NextSequenceRcv > (Uint32)(TCP_DACK_THRESHOLD + Connection->LastACKSequence) )
527 TCP_INT_SendACK(Connection, "DACK Burst");
528 // - Extend TCP deferred ACK timer
529 Time_RemoveTimer(Connection->DeferredACKTimer);
531 // - Schedule the deferred ACK timer (if already scheduled, this is a NOP)
532 Time_ScheduleTimer(Connection->DeferredACKTimer, TCP_DACK_TIMEOUT);
534 TCP_INT_SendACK(Connection, "RX");
537 // Check if the packet is in window
538 else if( WrapBetween(Connection->NextSequenceRcv, sequence_num,
539 Connection->NextSequenceRcv+TCP_WINDOW_SIZE, 0xFFFFFFFF) )
541 Uint8 *dataptr = (Uint8*)Header + (Header->DataOffset>>4)*4;
542 #if CACHE_FUTURE_PACKETS_IN_BYTES
545 index = sequence_num % TCP_WINDOW_SIZE;
546 for( int i = 0; i < dataLen; i ++ )
548 Connection->FuturePacketValidBytes[index/8] |= 1 << (index%8);
549 Connection->FuturePacketData[index] = dataptr[i];
550 // Do a wrap increment
552 if(index == TCP_WINDOW_SIZE) index = 0;
555 tTCPStoredPacket *pkt, *tmp, *prev = NULL;
557 // Allocate and fill cached packet
558 pkt = malloc( sizeof(tTCPStoredPacket) + dataLen );
560 pkt->Sequence = ntohl(Header->SequenceNumber);
561 pkt->Length = dataLen;
562 memcpy(pkt->Data, dataptr, dataLen);
564 Log_Log("TCP", "We missed a packet, caching",
565 pkt->Sequence, Connection->NextSequenceRcv);
567 // No? Well, let's cache it and look at it later
568 SHORTLOCK( &Connection->lFuturePackets );
569 for(tmp = Connection->FuturePackets;
571 prev = tmp, tmp = tmp->Next)
573 if(tmp->Sequence >= pkt->Sequence) break;
576 // Add if before first, or sequences don't match
577 if( !tmp || tmp->Sequence != pkt->Sequence )
582 Connection->FuturePackets = pkt;
586 else if(pkt->Length > tmp->Length)
590 pkt->Next = tmp->Next;
595 free(pkt); // TODO: Find some way to remove this
597 SHORTREL( &Connection->lFuturePackets );
600 // Badly out of sequence packet
603 Log_Log("TCP", "Fully out of sequence packet (0x%08x not between 0x%08x and 0x%08x), dropped",
604 sequence_num, Connection->NextSequenceRcv, Connection->NextSequenceRcv+TCP_WINDOW_SIZE);
605 // Spec says we should send an empty ACK with the current state
606 TCP_INT_SendACK(Connection, "Bad Seq");
610 // --- Remote close states
611 case TCP_ST_CLOSE_WAIT:
613 // Ignore everything, CLOSE_WAIT is terminated by the client
614 Log_Debug("TCP", "CLOSE WAIT - Ignoring packets");
618 // LAST-ACK - Waiting for the ACK of FIN (from CLOSE WAIT)
619 case TCP_ST_LAST_ACK:
620 if( Header->Flags & TCP_FLAG_ACK )
622 Connection->State = TCP_ST_FINISHED; // Connection completed
623 Log_Log("TCP", "LAST-ACK to CLOSED - Connection remote closed");
624 TCP_int_FreeTCB(Connection);
628 // --- Local close States
629 case TCP_ST_FIN_WAIT1:
630 if( Header->Flags & TCP_FLAG_FIN )
632 Connection->State = TCP_ST_CLOSING;
633 Log_Debug("TCP", "Conn %p closed, sent FIN and recieved FIN", Connection);
634 VFS_MarkError(&Connection->Node, 1);
637 Header->DestPort = Header->SourcePort;
638 Header->SourcePort = htons(Connection->LocalPort);
639 Header->AcknowlegementNumber = Header->SequenceNumber;
640 Header->SequenceNumber = htonl(Connection->NextSequenceSend);
641 Header->WindowSize = htons(TCP_WINDOW_SIZE);
642 Header->Flags = TCP_FLAG_ACK;
643 TCP_SendPacket( Connection, Header, 0, NULL );
647 // TODO: Make sure that the packet is actually ACKing the FIN
648 if( Header->Flags & TCP_FLAG_ACK )
650 Connection->State = TCP_ST_FIN_WAIT2;
651 Log_Debug("TCP", "Conn %p closed, sent FIN ACKed", Connection);
652 VFS_MarkError(&Connection->Node, 1);
657 case TCP_ST_FIN_WAIT2:
658 if( Header->Flags & TCP_FLAG_FIN )
660 Connection->State = TCP_ST_TIME_WAIT;
661 Log_Debug("TCP", "FIN sent and recieved, ACKing and going into TIME WAIT %p FINWAIT-2 -> TIME WAIT", Connection);
663 Header->DestPort = Header->SourcePort;
664 Header->SourcePort = htons(Connection->LocalPort);
665 Header->AcknowlegementNumber = Header->SequenceNumber;
666 Header->SequenceNumber = htonl(Connection->NextSequenceSend);
667 Header->WindowSize = htons(TCP_WINDOW_SIZE);
668 Header->Flags = TCP_FLAG_ACK;
669 TCP_SendPacket( Connection, Header, 0, NULL );
674 // TODO: Make sure that the packet is actually ACKing the FIN
675 if( Header->Flags & TCP_FLAG_ACK )
677 Connection->State = TCP_ST_TIME_WAIT;
678 Log_Debug("TCP", "Conn %p CLOSING -> TIME WAIT", Connection);
679 VFS_MarkError(&Connection->Node, 1);
684 // --- Closed (or near closed) states) ---
685 case TCP_ST_TIME_WAIT:
686 Log_Log("TCP", "Packets on Time-Wait, ignored");
689 case TCP_ST_FINISHED:
690 Log_Log("TCP", "Packets when CLOSED, ignoring");
694 // Log_Warning("TCP", "Unhandled TCP state %i", Connection->State);
701 * \brief Appends a packet to the recieved list
702 * \param Connection Connection structure
703 * \param Data Packet contents
704 * \param Length Length of \a Data
706 int TCP_INT_AppendRecieved(tTCPConnection *Connection, const void *Data, size_t Length)
708 Mutex_Acquire( &Connection->lRecievedPackets );
710 if(Connection->RecievedBuffer->Length + Length > Connection->RecievedBuffer->Space )
712 VFS_MarkAvaliable(&Connection->Node, 1);
713 Log_Error("TCP", "Buffer filled, packet dropped (:%i) - %i + %i > %i",
714 Connection->LocalPort, Connection->RecievedBuffer->Length, Length,
715 Connection->RecievedBuffer->Space
717 Mutex_Release( &Connection->lRecievedPackets );
721 RingBuffer_Write( Connection->RecievedBuffer, Data, Length );
723 VFS_MarkAvaliable(&Connection->Node, 1);
725 Mutex_Release( &Connection->lRecievedPackets );
730 * \brief Updates the connections recieved list from the future list
731 * \param Connection Connection structure
733 * Updates the recieved packets list with packets from the future (out
734 * of order) packets list that are now able to be added in direct
737 void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection)
739 #if CACHE_FUTURE_PACKETS_IN_BYTES
740 // Calculate length of contiguous bytes
741 int length = Connection->HighestSequenceRcvd - Connection->NextSequenceRcv;
742 Uint32 index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE;
743 LOG("length=%i, index=%i", length, index);
744 for( int i = 0; i < length; i ++ )
747 Uint8 bitfield_byte = Connection->FuturePacketValidBytes[index / 8];
748 if( (bitfield_byte & (1 << bit)) == 0 ) {
753 if( bitfield_byte == 0xFF ) {
761 if(index > TCP_WINDOW_SIZE)
762 index -= TCP_WINDOW_SIZE;
765 index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE;
767 // Write data to to the ring buffer
768 if( TCP_WINDOW_SIZE - index > length )
771 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, length );
775 int endLen = TCP_WINDOW_SIZE - index;
777 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, endLen );
778 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData, endLen - length );
781 // Mark (now saved) bytes as invalid
783 while(index % 8 && length > 0)
785 Connection->FuturePacketData[index] = 0;
786 Connection->FuturePacketValidBytes[index/8] &= ~(1 << (index%8));
788 if(index > TCP_WINDOW_SIZE)
789 index -= TCP_WINDOW_SIZE;
794 Connection->FuturePacketData[index] = 0;
795 Connection->FuturePacketValidBytes[index/8] = 0;
798 if(index > TCP_WINDOW_SIZE)
799 index -= TCP_WINDOW_SIZE;
803 Connection->FuturePacketData[index] = 0;
804 Connection->FuturePacketData[index/8] &= ~(1 << (index%8));
806 if(index > TCP_WINDOW_SIZE)
807 index -= TCP_WINDOW_SIZE;
812 tTCPStoredPacket *pkt;
815 SHORTLOCK( &Connection->lFuturePackets );
817 // Clear out duplicates from cache
818 // - If a packet has just been recieved, and it is expected, then
819 // (since NextSequenceRcv = rcvd->Sequence + rcvd->Length) all
820 // packets in cache that are smaller than the next expected
822 pkt = Connection->FuturePackets;
823 while(pkt && pkt->Sequence < Connection->NextSequenceRcv)
825 tTCPStoredPacket *next = pkt->Next;
830 // If there's no packets left in cache, stop looking
831 if(!pkt || pkt->Sequence > Connection->NextSequenceRcv) {
832 SHORTREL( &Connection->lFuturePackets );
836 // Delete packet from future list
837 Connection->FuturePackets = pkt->Next;
840 SHORTREL( &Connection->lFuturePackets );
842 // Looks like we found one
843 TCP_INT_AppendRecieved(Connection, pkt->Data, pkt->Length);
844 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
845 Connection->HighestSequenceRcvd += pkt->Length;
846 Connection->NextSequenceRcv += pkt->Length;
852 void TCP_int_SendDelayedACK(void *ConnPtr)
854 TCP_INT_SendACK(ConnPtr, "DACK Timeout");
857 void TCP_INT_SendACK(tTCPConnection *Connection, const char *Reason)
861 hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
862 hdr.DestPort = htons(Connection->RemotePort);
863 hdr.SourcePort = htons(Connection->LocalPort);
864 hdr.AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
865 hdr.SequenceNumber = htonl(Connection->NextSequenceSend);
866 hdr.WindowSize = htons(TCP_WINDOW_SIZE);
867 hdr.Flags = TCP_FLAG_ACK; // TODO: Determine if SYN is wanted too
868 hdr.Checksum = 0; // TODO: Checksum
869 hdr.UrgentPointer = 0;
870 Log_Debug("TCP", "Sending ACK for 0x%08x (%s)", Connection->NextSequenceRcv, Reason);
871 TCP_SendPacket( Connection, &hdr, 0, NULL );
872 //Connection->NextSequenceSend ++;
873 Connection->LastACKSequence = Connection->NextSequenceRcv;
877 * \fn Uint16 TCP_GetUnusedPort()
878 * \brief Gets an unused port and allocates it
880 Uint16 TCP_GetUnusedPort()
884 // Get Next outbound port
885 ret = giTCP_NextOutPort++;
886 while( gaTCP_PortBitmap[ret/32] & (1UL << (ret%32)) )
890 if(giTCP_NextOutPort == 0x10000) {
891 ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
895 // Mark the new port as used
896 gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
902 * \fn int TCP_AllocatePort(Uint16 Port)
903 * \brief Marks a port as used
905 int TCP_AllocatePort(Uint16 Port)
907 // Check if the port has already been allocated
908 if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
912 gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
918 * \fn int TCP_DeallocatePort(Uint16 Port)
919 * \brief Marks a port as unused
921 int TCP_DeallocatePort(Uint16 Port)
923 // Check if the port has already been allocated
924 if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
928 gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));
933 tTCPConnection *TCP_int_CreateConnection(tInterface *Interface, enum eTCPConnectionState State)
935 tTCPConnection *conn = calloc( sizeof(tTCPConnection) + TCP_WINDOW_SIZE + TCP_WINDOW_SIZE/8, 1 );
938 conn->Interface = Interface;
939 conn->LocalPort = -1;
940 conn->RemotePort = -1;
942 conn->Node.ReferenceCount = 1;
943 conn->Node.ImplPtr = conn;
944 conn->Node.NumACLs = 1;
945 conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
946 conn->Node.Type = &gTCP_ClientNodeType;
947 conn->Node.BufferFull = 1; // Cleared when connection opens
949 conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE );
951 conn->SentBuffer = RingBuffer_Create( TCP_SEND_BUFFER_SIZE );
952 Semaphore_Init(conn->SentBufferSpace, 0, TCP_SEND_BUFFER_SIZE, "TCP SentBuffer", conn->Name);
955 #if CACHE_FUTURE_PACKETS_IN_BYTES
956 // Future recieved data (ahead of the expected sequence number)
957 conn->FuturePacketData = (Uint8*)conn + sizeof(tTCPConnection);
958 conn->FuturePacketValidBytes = conn->FuturePacketData + TCP_WINDOW_SIZE;
961 conn->DeferredACKTimer = Time_AllocateTimer( TCP_int_SendDelayedACK, conn);
965 void TCP_int_FreeTCB(tTCPConnection *Connection)
967 ASSERTC(Connection->State, ==, TCP_ST_FINISHED);
968 ASSERTC(Connection->Node.ReferenceCount, ==, 0);
970 if( Connection->Server )
972 tTCPListener *srv = Connection->Server;
973 SHORTLOCK(&srv->lConnections);
975 Connection->Prev->Next = Connection->Next;
977 srv->Connections = Connection->Next;
979 Connection->Next->Prev = Connection->Prev;
981 ASSERT(srv->ConnectionsTail == Connection);
982 srv->ConnectionsTail = Connection->Prev;
984 SHORTREL(&srv->lConnections);
988 SHORTLOCK(&glTCP_OutbountCons);
990 Connection->Prev->Next = Connection->Next;
992 gTCP_OutbountCons = Connection->Next;
994 Connection->Next->Prev = Connection->Prev;
997 SHORTREL(&glTCP_OutbountCons);
1000 RingBuffer_Free(Connection->RecievedBuffer);
1001 Time_FreeTimer(Connection->DeferredACKTimer);
1002 // TODO: Force VFS to close handles? (they should all be closed);
1007 tVFS_Node *TCP_Server_Init(tInterface *Interface)
1011 srv = calloc( 1, sizeof(tTCPListener) );
1014 Log_Warning("TCP", "malloc failed for listener (%i) bytes", sizeof(tTCPListener));
1018 srv->Interface = Interface;
1021 srv->Connections = NULL;
1022 srv->ConnectionsTail = NULL;
1023 srv->NewConnections = NULL;
1025 srv->Node.Flags = VFS_FFLAG_DIRECTORY;
1026 srv->Node.Size = -1;
1027 srv->Node.ImplPtr = srv;
1028 srv->Node.NumACLs = 1;
1029 srv->Node.ACLs = &gVFS_ACL_EveryoneRW;
1030 srv->Node.Type = &gTCP_ServerNodeType;
1032 SHORTLOCK(&glTCP_Listeners);
1033 srv->Next = gTCP_Listeners;
1034 gTCP_Listeners = srv;
1035 SHORTREL(&glTCP_Listeners);
1041 * \brief Wait for a new connection and return the connection ID
1042 * \note Blocks until a new connection is made
1043 * \param Node Server node
1044 * \param Pos Position (ignored)
1046 int TCP_Server_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
1048 tTCPListener *srv = Node->ImplPtr;
1049 tTCPConnection *conn;
1051 ENTER("pNode iPos", Node, Pos);
1053 Log_Log("TCP", "Thread %i waiting for a connection", Threads_GetTID());
1054 Semaphore_Wait( &srv->WaitingConnections, 1 );
1056 SHORTLOCK(&srv->lConnections);
1057 // Increment the new list (the current connection is still on the
1059 conn = srv->NewConnections;
1060 srv->NewConnections = conn->Next;
1062 if( srv->NewConnections == NULL )
1063 VFS_MarkAvaliable( Node, 0 );
1065 SHORTREL( &srv->lConnections );
1067 LOG("conn = %p", conn);
1068 LOG("srv->Connections = %p", srv->Connections);
1069 LOG("srv->NewConnections = %p", srv->NewConnections);
1070 LOG("srv->ConnectionsTail = %p", srv->ConnectionsTail);
1072 itoa(Dest, conn->Node.ImplInt, 16, 8, '0');
1073 Log_Log("TCP", "Thread %i got connection '%s'", Threads_GetTID(), Dest);
1079 * \brief Gets a client connection node
1080 * \param Node Server node
1081 * \param Name Hexadecimal ID of the node
1083 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
1085 tTCPConnection *conn;
1086 tTCPListener *srv = Node->ImplPtr;
1088 int id = atoi(Name);
1090 ENTER("pNode sName", Node, Name);
1092 // Check for a non-empty name
1096 itoa(tmp, id, 16, 8, '0');
1097 if(strcmp(tmp, Name) != 0) {
1098 LOG("'%s' != '%s' (%08x)", Name, tmp, id);
1103 Log_Debug("TCP", "srv->Connections = %p", srv->Connections);
1104 Log_Debug("TCP", "srv->NewConnections = %p", srv->NewConnections);
1105 Log_Debug("TCP", "srv->ConnectionsTail = %p", srv->ConnectionsTail);
1108 SHORTLOCK( &srv->lConnections );
1109 for(conn = srv->Connections;
1113 LOG("conn->Node.ImplInt = %i", conn->Node.ImplInt);
1114 if(conn->Node.ImplInt == id) break;
1116 SHORTREL( &srv->lConnections );
1118 // If not found, ret NULL
1120 LOG("Connection %i not found", id);
1125 // Empty Name - Check for a new connection and if it's there, open it
1128 SHORTLOCK( &srv->lConnections );
1129 conn = srv->NewConnections;
1131 srv->NewConnections = conn->Next;
1132 VFS_MarkAvaliable( Node, srv->NewConnections != NULL );
1133 SHORTREL( &srv->lConnections );
1135 LOG("No new connections");
1142 LEAVE('p', &conn->Node);
1147 * \brief Handle IOCtl calls
1149 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data)
1151 tTCPListener *srv = Node->ImplPtr;
1155 case 4: // Get/Set Port
1156 if(!Data) // Get Port
1159 if(srv->Port) // Wait, you can't CHANGE the port
1162 if(!CheckMem(Data, sizeof(Uint16))) // Sanity check
1165 // Permissions check
1166 if(Threads_GetUID() != 0
1167 && *(Uint16*)Data != 0
1168 && *(Uint16*)Data < 1024)
1171 // TODO: Check if a port is in use
1174 srv->Port = *(Uint16*)Data;
1175 if(srv->Port == 0) // Allocate a random port
1176 srv->Port = TCP_GetUnusedPort();
1177 else // Else, mark this as used
1178 TCP_AllocatePort(srv->Port);
1180 Log_Log("TCP", "Server %p listening on port %i", srv, srv->Port);
1187 void TCP_Server_Close(tVFS_Node *Node)
1189 free(Node->ImplPtr);
1194 * \brief Create a client node
1196 tVFS_Node *TCP_Client_Init(tInterface *Interface)
1198 tTCPConnection *conn = TCP_int_CreateConnection(Interface, TCP_ST_CLOSED);
1200 SHORTLOCK(&glTCP_OutbountCons);
1201 conn->Server = NULL;
1203 conn->Next = gTCP_OutbountCons;
1204 gTCP_OutbountCons->Prev = conn;
1205 gTCP_OutbountCons = conn;
1206 SHORTREL(&glTCP_OutbountCons);
1212 * \brief Wait for a packet and return it
1213 * \note If \a Length is smaller than the size of the packet, the rest
1214 * of the packet's data will be discarded.
1216 size_t TCP_Client_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
1218 tTCPConnection *conn = Node->ImplPtr;
1221 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1222 LOG("conn = %p {State:%i}", conn, conn->State);
1224 // If the connection has been closed (state > ST_OPEN) then clear
1225 // any stale data in the buffer (until it is empty (until it is empty))
1226 if( conn->State > TCP_ST_OPEN )
1228 Mutex_Acquire( &conn->lRecievedPackets );
1229 len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
1230 Mutex_Release( &conn->lRecievedPackets );
1233 VFS_MarkAvaliable(Node, 0);
1245 tTime *timeout = NULL;
1246 tTime timeout_zero = 0;
1247 if( Flags & VFS_IOFLAG_NOBLOCK )
1248 timeout = &timeout_zero;
1249 if( !VFS_SelectNode(Node, VFS_SELECT_READ|VFS_SELECT_ERROR, timeout, "TCP_Client_Read") ) {
1250 errno = EWOULDBLOCK;
1256 // Lock list and read as much as possible (up to `Length`)
1257 Mutex_Acquire( &conn->lRecievedPackets );
1258 len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
1260 if( len == 0 || conn->RecievedBuffer->Length == 0 ) {
1261 LOG("Marking as none avaliable (len = %i)", len);
1262 VFS_MarkAvaliable(Node, 0);
1265 // Release the lock (we don't need it any more)
1266 Mutex_Release( &conn->lRecievedPackets );
1273 * \brief Send a data packet on a connection
1275 void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, const void *Data)
1277 char buf[sizeof(tTCPHeader)+Length];
1278 tTCPHeader *packet = (void*)buf;
1280 // - Stop Delayed ACK timer (as this data packet ACKs)
1281 Time_RemoveTimer(Connection->DeferredACKTimer);
1283 packet->SourcePort = htons(Connection->LocalPort);
1284 packet->DestPort = htons(Connection->RemotePort);
1285 packet->DataOffset = (sizeof(tTCPHeader)/4)*16;
1286 packet->WindowSize = htons(TCP_WINDOW_SIZE);
1288 packet->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
1289 packet->SequenceNumber = htonl(Connection->NextSequenceSend);
1290 packet->Flags = TCP_FLAG_PSH|TCP_FLAG_ACK; // Hey, ACK if you can!
1291 packet->UrgentPointer = 0;
1293 memcpy(packet->Options, Data, Length);
1295 Log_Debug("TCP", "Send sequence 0x%08x", Connection->NextSequenceSend);
1296 #if HEXDUMP_OUTGOING
1297 Debug_HexDump("TCP_INT_SendDataPacket: Data = ", Data, Length);
1300 TCP_SendPacket( Connection, packet, Length, Data );
1302 Connection->NextSequenceSend += Length;
1306 * \brief Send some bytes on a connection
1308 size_t TCP_Client_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
1310 tTCPConnection *conn = Node->ImplPtr;
1311 size_t rem = Length;
1313 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1316 // Debug_HexDump("TCP_Client_Write: Buffer = ",
1320 // Don't allow a write to a closed connection
1321 if( conn->State > TCP_ST_OPEN ) {
1322 VFS_MarkError(Node, 1);
1330 tTime *timeout = NULL;
1331 tTime timeout_zero = 0;
1332 if( Flags & VFS_IOFLAG_NOBLOCK )
1333 timeout = &timeout_zero;
1334 if( !VFS_SelectNode(Node, VFS_SELECT_WRITE|VFS_SELECT_ERROR, timeout, "TCP_Client_Write") ) {
1335 errno = EWOULDBLOCK;
1343 int len = (rem < TCP_MAX_PACKET_SIZE) ? rem : TCP_MAX_PACKET_SIZE;
1346 // Wait for space in the buffer
1347 Semaphore_Signal( &Connection->SentBufferSpace, len );
1349 // Save data to buffer (and update the length read by the ammount written)
1350 len = RingBuffer_Write( &Connection->SentBuffer, Buffer, len);
1354 TCP_INT_SendDataPacket(conn, len, Buffer);
1365 * \brief Open a connection to another host using TCP
1366 * \param Conn Connection structure
1368 void TCP_StartConnection(tTCPConnection *Conn)
1370 tTCPHeader hdr = {0};
1372 Conn->State = TCP_ST_SYN_SENT;
1374 hdr.SourcePort = htons(Conn->LocalPort);
1375 hdr.DestPort = htons(Conn->RemotePort);
1376 Conn->NextSequenceSend = rand();
1377 hdr.SequenceNumber = htonl(Conn->NextSequenceSend);
1378 hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
1379 hdr.Flags = TCP_FLAG_SYN;
1380 hdr.WindowSize = htons(TCP_WINDOW_SIZE); // Max
1381 hdr.Checksum = 0; // TODO
1383 TCP_SendPacket( Conn, &hdr, 0, NULL );
1385 Conn->NextSequenceSend ++;
1386 Conn->State = TCP_ST_SYN_SENT;
1392 * \brief Control a client socket
1394 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
1396 tTCPConnection *conn = Node->ImplPtr;
1398 ENTER("pNode iID pData", Node, ID, Data);
1402 case 4: // Get/Set local port
1404 LEAVE_RET('i', conn->LocalPort);
1405 if(conn->State != TCP_ST_CLOSED)
1407 if(!CheckMem(Data, sizeof(Uint16)))
1410 if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
1413 conn->LocalPort = *(Uint16*)Data;
1414 LEAVE_RET('i', conn->LocalPort);
1416 case 5: // Get/Set remote port
1417 if(!Data) LEAVE_RET('i', conn->RemotePort);
1418 if(conn->State != TCP_ST_CLOSED) LEAVE_RET('i', -1);
1419 if(!CheckMem(Data, sizeof(Uint16))) LEAVE_RET('i', -1);
1420 conn->RemotePort = *(Uint16*)Data;
1421 LEAVE_RET('i', conn->RemotePort);
1423 case 6: // Set Remote IP
1424 if( conn->State != TCP_ST_CLOSED )
1426 if( conn->Interface->Type == 4 )
1428 if(!CheckMem(Data, sizeof(tIPv4))) LEAVE_RET('i', -1);
1429 conn->RemoteIP.v4 = *(tIPv4*)Data;
1431 else if( conn->Interface->Type == 6 )
1433 if(!CheckMem(Data, sizeof(tIPv6))) LEAVE_RET('i', -1);
1434 conn->RemoteIP.v6 = *(tIPv6*)Data;
1439 if(conn->LocalPort == 0xFFFF)
1440 conn->LocalPort = TCP_GetUnusedPort();
1441 if(conn->RemotePort == -1)
1445 tTime timeout = conn->Interface->TimeoutDelay;
1447 TCP_StartConnection(conn);
1448 VFS_SelectNode(&conn->Node, VFS_SELECT_WRITE, &timeout, "TCP Connection");
1449 if( conn->State == TCP_ST_SYN_SENT )
1455 // Get recieve buffer length
1457 LEAVE_RET('i', conn->RecievedBuffer->Length);
1463 void TCP_Client_Close(tVFS_Node *Node)
1465 tTCPConnection *conn = Node->ImplPtr;
1468 ENTER("pNode", Node);
1470 ASSERT(Node->ReferenceCount != 0);
1472 if( Node->ReferenceCount > 1 ) {
1473 Node->ReferenceCount --;
1474 LOG("Dereference only");
1479 if( conn->State == TCP_ST_CLOSE_WAIT || conn->State == TCP_ST_OPEN )
1481 packet.SourcePort = htons(conn->LocalPort);
1482 packet.DestPort = htons(conn->RemotePort);
1483 packet.DataOffset = (sizeof(tTCPHeader)/4)*16;
1484 packet.WindowSize = TCP_WINDOW_SIZE;
1486 packet.AcknowlegementNumber = 0;
1487 packet.SequenceNumber = htonl(conn->NextSequenceSend);
1488 packet.Flags = TCP_FLAG_FIN;
1490 TCP_SendPacket( conn, &packet, 0, NULL );
1493 Time_RemoveTimer(conn->DeferredACKTimer);
1495 switch( conn->State )
1498 Log_Warning("TCP", "Closing connection that was never opened");
1499 TCP_int_FreeTCB(conn);
1501 case TCP_ST_CLOSE_WAIT:
1502 conn->State = TCP_ST_LAST_ACK;
1505 conn->State = TCP_ST_FIN_WAIT1;
1506 while( conn->State == TCP_ST_FIN_WAIT1 )
1510 Log_Warning("TCP", "Unhandled connection state %i in TCP_Client_Close",
1519 * \brief Checks if a value is between two others (after taking into account wrapping)
1521 int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue)
1523 if( MaxValue < 0xFFFFFFFF )
1525 Lower %= MaxValue + 1;
1526 Value %= MaxValue + 1;
1527 Higher %= MaxValue + 1;
1530 // Simple Case, no wrap ?
1531 // Lower Value Higher
1532 // | ... + ... + ... + ... |
1534 if( Lower < Higher ) {
1535 return Lower < Value && Value < Higher;
1537 // Higher has wrapped below lower
1540 // Higher Lower Value
1541 // | ... + ... + ... + ... |
1542 if( Value > Lower ) {
1547 // Value Higher Lower
1548 // | ... + ... + ... + ... |
1549 if( Value < Higher ) {