X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Modules%2FIPStack%2Ftcp.c;h=83a71b640851e3444fd72b5d2e7b348bd55dde7b;hb=4f1a9b430a3fa57bbe52a6a2fe546f6fe93c389d;hp=8b486d23aecfc89fdcfdcaae1968f05e63e33b99;hpb=7d881c2e5fef91a6570e46ef69a5d4a5cf0e8b4d;p=tpg%2Facess2.git diff --git a/Modules/IPStack/tcp.c b/Modules/IPStack/tcp.c index 8b486d23..83a71b64 100644 --- a/Modules/IPStack/tcp.c +++ b/Modules/IPStack/tcp.c @@ -7,6 +7,9 @@ #include "ipv4.h" #include "tcp.h" +#define USE_SELECT 1 +#define CACHE_FUTURE_PACKETS_IN_BYTES 1 // Use a ring buffer to cache out of order packets + #define TCP_MIN_DYNPORT 0xC000 #define TCP_MAX_HALFOPEN 1024 // Should be enough @@ -15,7 +18,7 @@ #define TCP_RECIEVE_BUFFER_SIZE 0x4000 // === PROTOTYPES === -void TCP_Initialise(); +void TCP_Initialise(void); void TCP_StartConnection(tTCPConnection *Conn); void TCP_SendPacket(tTCPConnection *Conn, size_t Length, tTCPHeader *Data); void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer); @@ -37,6 +40,8 @@ Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buff Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer); int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data); void TCP_Client_Close(tVFS_Node *Node); +// --- Helpers + int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue); // === TEMPLATES === tSocketFile gTCP_ServerFile = {NULL, "tcps", TCP_Server_Init}; @@ -57,7 +62,7 @@ Uint32 gaTCP_PortBitmap[0x800]; * * Registers the client and server files and the GetPacket callback */ -void TCP_Initialise() +void TCP_Initialise(void) { IPStack_AddFile(&gTCP_ServerFile); IPStack_AddFile(&gTCP_ClientFile); @@ -79,12 +84,12 @@ void TCP_SendPacket( tTCPConnection *Conn, size_t Length, tTCPHeader *Data ) case 4: // Append IPv4 Pseudo Header buflen = 4 + 4 + 4 + ((Length+1)&~1); buf = malloc( buflen ); - buf[0] = Conn->Interface->IP4.Address.L; + buf[0] = ((tIPv4*)Conn->Interface->Address)->L; buf[1] = Conn->RemoteIP.v4.L; buf[2] = (htons(Length)<<16) | (6<<8) | 0; Data->Checksum = 0; memcpy( &buf[3], Data, Length ); - Data->Checksum = IPv4_Checksum( buf, buflen ); + Data->Checksum = htons( IPv4_Checksum( buf, buflen ) ); free(buf); IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, Length, Data); break; @@ -104,27 +109,17 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe tTCPListener *srv; tTCPConnection *conn; - Log_Log("TCP", "SourcePort = %i, DestPort = %i", + Log_Log("TCP", "TCP_GetPacket: SourcePort = %i, DestPort = %i", ntohs(hdr->SourcePort), ntohs(hdr->DestPort)); /* - Log_Log("TCP", "SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber)); - Log_Log("TCP", "AcknowlegementNumber = 0x%x", ntohl(hdr->AcknowlegementNumber)); - Log_Log("TCP", "DataOffset = %i", hdr->DataOffset >> 4); - Log_Log("TCP", "Flags = {"); - Log_Log("TCP", " CWR = %B, ECE = %B", - !!(hdr->Flags & TCP_FLAG_CWR), !!(hdr->Flags & TCP_FLAG_ECE)); - Log_Log("TCP", " URG = %B, ACK = %B", - !!(hdr->Flags & TCP_FLAG_URG), !!(hdr->Flags & TCP_FLAG_ACK)); - Log_Log("TCP", " PSH = %B, RST = %B", - !!(hdr->Flags & TCP_FLAG_PSH), !!(hdr->Flags & TCP_FLAG_RST)); - Log_Log("TCP", " SYN = %B, FIN = %B", - !!(hdr->Flags & TCP_FLAG_SYN), !!(hdr->Flags & TCP_FLAG_FIN)); - Log_Log("TCP", "}"); - Log_Log("TCP", "WindowSize = %i", htons(hdr->WindowSize)); - Log_Log("TCP", "Checksum = 0x%x", htons(hdr->Checksum)); - Log_Log("TCP", "UrgentPointer = 0x%x", htons(hdr->UrgentPointer)); + Log_Log("TCP", "TCP_GetPacket: SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber)); + Log_Log("TCP", "TCP_GetPacket: AcknowlegementNumber = 0x%x", ntohl(hdr->AcknowlegementNumber)); + Log_Log("TCP", "TCP_GetPacket: DataOffset = %i", hdr->DataOffset >> 4); + Log_Log("TCP", "TCP_GetPacket: WindowSize = %i", htons(hdr->WindowSize)); + Log_Log("TCP", "TCP_GetPacket: Checksum = 0x%x", htons(hdr->Checksum)); + Log_Log("TCP", "TCP_GetPacket: UrgentPointer = 0x%x", htons(hdr->UrgentPointer)); */ - Log_Log("TCP", "Flags = %s%s%s%s%s%s", + Log_Log("TCP", "TCP_GetPacket: Flags = %s%s%s%s%s%s%s%s", (hdr->Flags & TCP_FLAG_CWR) ? "CWR " : "", (hdr->Flags & TCP_FLAG_ECE) ? "ECE " : "", (hdr->Flags & TCP_FLAG_URG) ? "URG " : "", @@ -137,6 +132,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe if( Length > (hdr->DataOffset >> 4)*4 ) { + Log_Log("TCP", "TCP_GetPacket: SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber)); Debug_HexDump( "[TCP ] Packet Data = ", (Uint8*)hdr + (hdr->DataOffset >> 4)*4, @@ -155,17 +151,17 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe // Check the destination port if(srv->Port != htons(hdr->DestPort)) continue; - Log_Log("TCP", "Matches server %p", srv); + Log_Log("TCP", "TCP_GetPacket: Matches server %p", srv); // Is this in an established connection? for( conn = srv->Connections; conn; conn = conn->Next ) { - Log_Log("TCP", "conn->Interface(%p) == Interface(%p)", + Log_Log("TCP", "TCP_GetPacket: conn->Interface(%p) == Interface(%p)", conn->Interface, Interface); // Check that it is coming in on the same interface if(conn->Interface != Interface) continue; // Check Source Port - Log_Log("TCP", "conn->RemotePort(%i) == hdr->SourcePort(%i)", + Log_Log("TCP", "TCP_GetPacket: conn->RemotePort(%i) == hdr->SourcePort(%i)", conn->RemotePort, ntohs(hdr->SourcePort)); if(conn->RemotePort != ntohs(hdr->SourcePort)) continue; @@ -175,24 +171,24 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address)) continue; - Log_Log("TCP", "Matches connection %p", conn); + Log_Log("TCP", "TCP_GetPacket: Matches connection %p", conn); // We have a response! TCP_INT_HandleConnectionPacket(conn, hdr, Length); return; } - Log_Log("TCP", "Opening Connection"); + Log_Log("TCP", "TCP_GetPacket: Opening Connection"); // Open a new connection (well, check that it's a SYN) if(hdr->Flags != TCP_FLAG_SYN) { - Log_Log("TCP", "Packet is not a SYN"); + Log_Log("TCP", "TCP_GetPacket: Packet is not a SYN"); return ; } // TODO: Check for halfopen max conn = calloc(1, sizeof(tTCPConnection)); - conn->State = TCP_ST_HALFOPEN; + conn->State = TCP_ST_SYN_RCVD; conn->LocalPort = srv->Port; conn->RemotePort = ntohs(hdr->SourcePort); conn->Interface = Interface; @@ -231,7 +227,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe srv->ConnectionsTail = conn; if(!srv->NewConnections) srv->NewConnections = conn; - SHORTLOCK(&srv->lConnections); + SHORTREL(&srv->lConnections); // Send the SYN ACK hdr->Flags |= TCP_FLAG_ACK; @@ -268,7 +264,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe } } - Log_Log("TCP", "No Match"); + Log_Log("TCP", "TCP_GetPacket: No Match"); } /** @@ -282,14 +278,45 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head tTCPStoredPacket *pkt; int dataLen; + // Silently drop once finished + // TODO: Check if this needs to be here + if( Connection->State == TCP_ST_FINISHED ) { + Log_Log("TCP", "Packet ignored - connection finnished"); + return ; + } + + // Syncronise sequence values if(Header->Flags & TCP_FLAG_SYN) { - Connection->NextSequenceRcv = ntohl(Header->SequenceNumber) + 1; + // TODO: What if the packet also has data? + Connection->NextSequenceRcv = ntohl(Header->SequenceNumber); } - if( Connection->State == TCP_ST_SYN_SENT ) + // Ackowledge a sent packet + if(Header->Flags & TCP_FLAG_ACK) { + // TODO: Process an ACKed Packet + Log_Log("TCP", "Conn %p, Sent packet 0x%x ACKed", Connection, Header->AcknowlegementNumber); + } + + // Get length of data + dataLen = Length - (Header->DataOffset>>4)*4; + Log_Log("TCP", "HandleConnectionPacket - dataLen = %i", dataLen); + + // + // State Machine + // + switch( Connection->State ) { - if( (Header->Flags & (TCP_FLAG_SYN|TCP_FLAG_ACK)) == (TCP_FLAG_SYN|TCP_FLAG_ACK) ) { - + // Pre-init conneciton? + case TCP_ST_CLOSED: + Log_Log("TCP", "Packets to a closed connection?!"); + break; + + // --- Init States --- + // SYN sent, expecting SYN-ACK Connection Opening + case TCP_ST_SYN_SENT: + if( Header->Flags & TCP_FLAG_SYN ) + { + Connection->NextSequenceRcv ++; Header->DestPort = Header->SourcePort; Header->SourcePort = htons(Connection->LocalPort); Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); @@ -297,105 +324,261 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head Header->WindowSize = htons(TCP_WINDOW_SIZE); Header->Flags = TCP_FLAG_ACK; Header->DataOffset = (sizeof(tTCPHeader)/4) << 4; - Log_Log("TCP", "ACKing SYN-ACK"); TCP_SendPacket( Connection, sizeof(tTCPHeader), Header ); + + if( Header->Flags & TCP_FLAG_ACK ) + { + Log_Log("TCP", "ACKing SYN-ACK"); + Connection->State = TCP_ST_OPEN; + } + else + { + Log_Log("TCP", "ACKing SYN"); + Connection->State = TCP_ST_SYN_RCVD; + } + } + break; + + // SYN-ACK sent, expecting ACK + case TCP_ST_SYN_RCVD: + if( Header->Flags & TCP_FLAG_ACK ) + { + // TODO: Handle max half-open limit Connection->State = TCP_ST_OPEN; + Log_Log("TCP", "Connection fully opened"); + } + break; + + // --- Established State --- + case TCP_ST_OPEN: + // - Handle State changes + // + if( Header->Flags & TCP_FLAG_FIN ) { + Log_Log("TCP", "Conn %p closed, recieved FIN, acknowledging", Connection); + VFS_MarkError(&Connection->Node, 1); + Connection->State = TCP_ST_CLOSE_WAIT; + // CLOSE WAIT requires the client to close (or does it?) + #if 0 + + #endif } - } - // Get length of data - dataLen = Length - (Header->DataOffset>>4)*4; - Log_Log("TCP", "HandleConnectionPacket - dataLen = %i", dataLen); + // Check for an empty packet + if(dataLen == 0) { + if( Header->Flags == TCP_FLAG_ACK ) + { + Log_Log("TCP", "ACK only packet"); + return ; + } + Connection->NextSequenceRcv ++; // TODO: Is this right? (empty packet counts as one byte) + Log_Log("TCP", "Empty Packet, inc and ACK the current sequence number"); + Header->DestPort = Header->SourcePort; + Header->SourcePort = htons(Connection->LocalPort); + Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); + Header->SequenceNumber = htonl(Connection->NextSequenceSend); + Header->Flags |= TCP_FLAG_ACK; + TCP_SendPacket( Connection, sizeof(tTCPHeader), Header ); + return ; + } + + // NOTES: + // Flags + // PSH - Has Data? + // /NOTES + + // Allocate and fill cached packet + pkt = malloc( sizeof(tTCPStoredPacket) + dataLen ); + pkt->Next = NULL; + pkt->Sequence = ntohl(Header->SequenceNumber); + pkt->Length = dataLen; + memcpy(pkt->Data, (Uint8*)Header + (Header->DataOffset>>4)*4, dataLen); + + Log_Log("TCP", "0x%08x <= 0x%08x < 0x%08x", + Connection->NextSequenceRcv, + pkt->Sequence, + Connection->NextSequenceRcv + TCP_WINDOW_SIZE + ); + + // Is this packet the next expected packet? + if( pkt->Sequence == Connection->NextSequenceRcv ) + { + // Ooh, Goodie! Add it to the recieved list + TCP_INT_AppendRecieved(Connection, pkt); + free(pkt); + Log_Log("TCP", "0x%08x += %i", Connection->NextSequenceRcv, dataLen); + Connection->NextSequenceRcv += dataLen; + + // TODO: This should be moved out of the watcher thread, + // so that a single lost packet on one connection doesn't cause + // all connections on the interface to lag. + TCP_INT_UpdateRecievedFromFuture(Connection); + + // ACK Packet + Header->DestPort = Header->SourcePort; + Header->SourcePort = htons(Connection->LocalPort); + Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); + Header->SequenceNumber = htonl(Connection->NextSequenceSend); + Header->WindowSize = htons(TCP_WINDOW_SIZE); + Header->Flags &= TCP_FLAG_SYN; // Eliminate all flags save for SYN + Header->Flags |= TCP_FLAG_ACK; // Add ACK + Log_Log("TCP", "Sending ACK for 0x%08x", Connection->NextSequenceRcv); + TCP_SendPacket( Connection, sizeof(tTCPHeader), Header ); + //Connection->NextSequenceSend ++; + } + // Check if the packet is in window + else if( WrapBetween(Connection->NextSequenceRcv, pkt->Sequence, + Connection->NextSequenceRcv+TCP_WINDOW_SIZE, 0xFFFFFFFF) ) + { + #if CACHE_FUTURE_PACKETS_IN_BYTES + Uint32 index; + int i; + + index = pkt->Sequence % TCP_WINDOW_SIZE; + for( i = 0; i < pkt->Length; i ++ ) + { + Connection->FuturePacketValidBytes[index/8] |= 1 << (index%8); + Connection->FuturePacketValidBytes[index] = pkt->Data[i]; + // Do a wrap increment + index ++; + if(index == TCP_WINDOW_SIZE) index = 0; + } + #else + tTCPStoredPacket *tmp, *prev = NULL; + + Log_Log("TCP", "We missed a packet, caching", + pkt->Sequence, Connection->NextSequenceRcv); + + // No? Well, let's cache it and look at it later + SHORTLOCK( &Connection->lFuturePackets ); + for(tmp = Connection->FuturePackets; + tmp; + prev = tmp, tmp = tmp->Next) + { + if(tmp->Sequence >= pkt->Sequence) break; + } + + // Add if before first, or sequences don't match + if( !tmp || tmp->Sequence != pkt->Sequence ) + { + if(prev) + prev->Next = pkt; + else + Connection->FuturePackets = pkt; + pkt->Next = tmp; + } + // Replace if larger + else if(pkt->Length > tmp->Length) + { + if(prev) + prev->Next = pkt; + pkt->Next = tmp->Next; + free(tmp); + } + else + { + free(pkt); + } + SHORTREL( &Connection->lFuturePackets ); + #endif + } + // Badly out of sequence packet + else + { + Log_Log("TCP", "Fully out of sequence packet (0x%08x not between 0x%08x and 0x%08x), dropped", + pkt->Sequence, Connection->NextSequenceRcv, Connection->NextSequenceRcv+TCP_WINDOW_SIZE); + free(pkt); + // TODO: Spec says we should send an empty ACK with the current state + } + break; - if(Header->Flags & TCP_FLAG_ACK) { - // TODO: Process an ACKed Packet - Log_Log("TCP", "Conn %p, Packet 0x%x ACKed", Connection, Header->AcknowlegementNumber); - } + // --- Remote close states + case TCP_ST_CLOSE_WAIT: + + // Ignore everything, CLOSE_WAIT is terminated by the client + Log_Debug("TCP", "CLOSE WAIT - Ignoring packets"); + + break; - // TODO: Check what to do here - if(Header->Flags & TCP_FLAG_FIN) { - if( Connection->State == TCP_ST_FIN_SENT ) { - Connection->State = TCP_ST_FINISHED; - return ; + // LAST-ACK - Waiting for the ACK of FIN (from CLOSE WAIT) + case TCP_ST_LAST_ACK: + if( Header->Flags & TCP_FLAG_ACK ) + { + Connection->State = TCP_ST_FINISHED; // Connection completed + Log_Log("TCP", "LAST-ACK to CLOSED - Connection remote closed"); + // TODO: Destrory the TCB } - else { - Connection->State = TCP_ST_FINISHED; + break; + + // --- Local close States + case TCP_ST_FIN_WAIT1: + if( Header->Flags & TCP_FLAG_FIN ) + { + Connection->State = TCP_ST_CLOSING; + Log_Debug("TCP", "Conn %p closed, sent FIN and recieved FIN", Connection); + VFS_MarkError(&Connection->Node, 1); + + // ACK Packet Header->DestPort = Header->SourcePort; Header->SourcePort = htons(Connection->LocalPort); Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); Header->SequenceNumber = htonl(Connection->NextSequenceSend); + Header->WindowSize = htons(TCP_WINDOW_SIZE); Header->Flags = TCP_FLAG_ACK; TCP_SendPacket( Connection, sizeof(tTCPHeader), Header ); + break ; + } + + // TODO: Make sure that the packet is actually ACKing the FIN + if( Header->Flags & TCP_FLAG_ACK ) + { + Connection->State = TCP_ST_FIN_WAIT2; + Log_Debug("TCP", "Conn %p closed, sent FIN ACKed", Connection); + VFS_MarkError(&Connection->Node, 1); return ; } - } - - if(dataLen == 0) { - Log_Log("TCP", "Empty Packet"); - return ; - } - - // NOTES: - // Flags - // PSH - Has Data? - // /NOTES + break; - // Allocate and fill cached packet - pkt = malloc( dataLen + sizeof(tTCPStoredPacket) ); - pkt->Next = NULL; - pkt->Sequence = ntohl(Header->SequenceNumber); - pkt->Length = dataLen; - memcpy(pkt->Data, (Uint8*)Header + (Header->DataOffset>>4)*4, dataLen); + case TCP_ST_FIN_WAIT2: + if( Header->Flags & TCP_FLAG_FIN ) + { + Connection->State = TCP_ST_TIME_WAIT; + Log_Debug("TCP", "FIN sent and recieved, ACKing and going into TIME WAIT %p FINWAIT-2 -> TIME WAIT", Connection); + // Send ACK + Header->DestPort = Header->SourcePort; + Header->SourcePort = htons(Connection->LocalPort); + Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); + Header->SequenceNumber = htonl(Connection->NextSequenceSend); + Header->WindowSize = htons(TCP_WINDOW_SIZE); + Header->Flags = TCP_FLAG_ACK; + TCP_SendPacket( Connection, sizeof(tTCPHeader), Header ); + } + break; - // Is this packet the next expected packet? - // TODO: Fix this to check if the packet is in the window. - if( pkt->Sequence != Connection->NextSequenceRcv ) - { - tTCPStoredPacket *tmp, *prev = NULL; - - Log_Log("TCP", "Out of sequence packet (0x%08x != 0x%08x)", - pkt->Sequence, Connection->NextSequenceRcv); - - // No? Well, let's cache it and look at it later - SHORTLOCK( &Connection->lFuturePackets ); - for(tmp = Connection->FuturePackets; - tmp; - prev = tmp, tmp = tmp->Next) + case TCP_ST_CLOSING: + // TODO: Make sure that the packet is actually ACKing the FIN + if( Header->Flags & TCP_FLAG_ACK ) { - if(tmp->Sequence > pkt->Sequence) break; + Connection->State = TCP_ST_TIME_WAIT; + Log_Debug("TCP", "Conn %p CLOSING -> TIME WAIT", Connection); + VFS_MarkError(&Connection->Node, 1); + return ; } - if(prev) - prev->Next = pkt; - else - Connection->FuturePackets = pkt; - pkt->Next = tmp; - SHORTREL( &Connection->lFuturePackets ); - } - else - { - // Ooh, Goodie! Add it to the recieved list - TCP_INT_AppendRecieved(Connection, pkt); - free(pkt); - Log_Log("TCP", "0x%08x += %i", Connection->NextSequenceRcv, dataLen); - Connection->NextSequenceRcv += dataLen; - - // TODO: This should be moved out of the watcher thread, - // so that a single lost packet on one connection doesn't cause - // all connections on the interface to lag. - TCP_INT_UpdateRecievedFromFuture(Connection); - - // ACK Packet - Header->DestPort = Header->SourcePort; - Header->SourcePort = htons(Connection->LocalPort); - Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv); - Header->SequenceNumber = htonl(Connection->NextSequenceSend); - Header->WindowSize = htons(TCP_WINDOW_SIZE); - Header->Flags &= TCP_FLAG_SYN; // Eliminate all flags save for SYN - Header->Flags |= TCP_FLAG_ACK; // Add ACK - Log_Log("TCP", "Sending ACK for 0x%08x", Connection->NextSequenceRcv); - TCP_SendPacket( Connection, sizeof(tTCPHeader), Header ); - //Connection->NextSequenceSend ++; + break; + + // --- Closed (or near closed) states) --- + case TCP_ST_TIME_WAIT: + Log_Log("TCP", "Packets on Time-Wait, ignored"); + break; + + case TCP_ST_FINISHED: + Log_Log("TCP", "Packets when CLOSED, ignoring"); + break; + + //default: + // Log_Warning("TCP", "Unhandled TCP state %i", Connection->State); + // break; } + } /** @@ -416,6 +599,10 @@ void TCP_INT_AppendRecieved(tTCPConnection *Connection, tTCPStoredPacket *Pkt) } RingBuffer_Write( Connection->RecievedBuffer, Pkt->Data, Pkt->Length ); + + #if USE_SELECT + VFS_MarkAvaliable(&Connection->Node, 1); + #endif Mutex_Release( &Connection->lRecievedPackets ); } @@ -430,27 +617,101 @@ void TCP_INT_AppendRecieved(tTCPConnection *Connection, tTCPStoredPacket *Pkt) */ void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection) { - tTCPStoredPacket *pkt, *prev; + #if CACHE_FUTURE_PACKETS_IN_BYTES + int i, length = 0; + Uint32 index; + + // Calculate length of contiguous bytes + length = Connection->HighestSequenceRcvd - Connection->NextSequenceRcv; + index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE; + for( i = 0; i < length; i ++ ) + { + if( Connection->FuturePacketValidBytes[i / 8] == 0xFF ) { + i += 7; index += 7; + continue; + } + else if( !(Connection->FuturePacketValidBytes[i / 8] & (1 << (i%8))) ) + break; + + index ++; + if(index > TCP_WINDOW_SIZE) + index -= TCP_WINDOW_SIZE; + } + length = i; + + index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE; + + // Write data to to the ring buffer + if( TCP_WINDOW_SIZE - index > length ) + { + // Simple case + RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, length ); + } + else + { + int endLen = TCP_WINDOW_SIZE - index; + // 2-part case + RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, endLen ); + RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData, endLen - length ); + } + + // Mark (now saved) bytes as invalid + // - Align index + while(index % 8 && length) + { + Connection->FuturePacketData[index] = 0; + Connection->FuturePacketData[index/8] &= ~(1 << (index%8)); + index ++; + if(index > TCP_WINDOW_SIZE) + index -= TCP_WINDOW_SIZE; + length --; + } + while( length > 7 ) + { + Connection->FuturePacketData[index] = 0; + Connection->FuturePacketValidBytes[index/8] = 0; + length -= 8; + index += 8; + if(index > TCP_WINDOW_SIZE) + index -= TCP_WINDOW_SIZE; + } + while(length) + { + Connection->FuturePacketData[index] = 0; + Connection->FuturePacketData[index/8] &= ~(1 << (index%8)); + index ++; + if(index > TCP_WINDOW_SIZE) + index -= TCP_WINDOW_SIZE; + length --; + } + + #else + tTCPStoredPacket *pkt; for(;;) { - prev = NULL; - // Look for the next expected packet in the cache. SHORTLOCK( &Connection->lFuturePackets ); - for(pkt = Connection->FuturePackets; - pkt && pkt->Sequence < Connection->NextSequenceRcv; - prev = pkt, pkt = pkt->Next); - // If we can't find the expected next packet, stop looking + // Clear out duplicates from cache + // - If a packet has just been recieved, and it is expected, then + // (since NextSequenceRcv = rcvd->Sequence + rcvd->Length) all + // packets in cache that are smaller than the next expected + // are now defunct. + pkt = Connection->FuturePackets; + while(pkt && pkt->Sequence < Connection->NextSequenceRcv) + { + tTCPStoredPacket *next = pkt->Next; + free(pkt); + pkt = next; + } + + // If there's no packets left in cache, stop looking if(!pkt || pkt->Sequence > Connection->NextSequenceRcv) { SHORTREL( &Connection->lFuturePackets ); return; } // Delete packet from future list - if(prev) - prev->Next = pkt->Next; - else - Connection->FuturePackets = pkt->Next; + Connection->FuturePackets = pkt->Next; // Release list SHORTREL( &Connection->lFuturePackets ); @@ -460,6 +721,7 @@ void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection) Connection->NextSequenceRcv += pkt->Length; free(pkt); } + #endif } /** @@ -701,13 +963,12 @@ void TCP_Server_Close(tVFS_Node *Node) */ tVFS_Node *TCP_Client_Init(tInterface *Interface) { - tTCPConnection *conn = malloc( sizeof(tTCPConnection) ); + tTCPConnection *conn = calloc( sizeof(tTCPConnection) + TCP_WINDOW_SIZE + TCP_WINDOW_SIZE/8, 1 ); conn->State = TCP_ST_CLOSED; conn->Interface = Interface; conn->LocalPort = -1; conn->RemotePort = -1; - memset( &conn->RemoteIP, 0, sizeof(conn->RemoteIP) ); conn->Node.ImplPtr = conn; conn->Node.NumACLs = 1; @@ -718,6 +979,16 @@ tVFS_Node *TCP_Client_Init(tInterface *Interface) conn->Node.Close = TCP_Client_Close; conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE ); + #if 0 + conn->SentBuffer = RingBuffer_Create( TCP_SEND_BUFFER_SIZE ); + Semaphore_Init(conn->SentBufferSpace, 0, TCP_SEND_BUFFER_SIZE, "TCP SentBuffer", conn->Name); + #endif + + #if CACHE_FUTURE_PACKETS_IN_BYTES + // Future recieved data (ahead of the expected sequence number) + conn->FuturePacketData = (Uint8*)conn + sizeof(tTCPConnection); + conn->FuturePacketValidBytes = conn->FuturePacketData + TCP_WINDOW_SIZE; + #endif SHORTLOCK(&glTCP_OutbountCons); conn->Next = gTCP_OutbountCons; @@ -735,42 +1006,51 @@ tVFS_Node *TCP_Client_Init(tInterface *Interface) Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) { tTCPConnection *conn = Node->ImplPtr; - char *destbuf = Buffer; size_t len; ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer); - LOG("conn = %p", conn); - LOG("conn->State = %i", conn->State); + LOG("conn = %p {State:%i}", conn, conn->State); - // Check if connection is open - while( conn->State == TCP_ST_HALFOPEN || conn->State == TCP_ST_SYN_SENT ) + // Check if connection is estabilishing + // - TODO: Sleep instead (maybe using VFS_SelectNode to wait for the + // data to be availiable + while( conn->State == TCP_ST_SYN_RCVD || conn->State == TCP_ST_SYN_SENT ) Threads_Yield(); - if( conn->State != TCP_ST_OPEN ) { - LEAVE('i', 0); - return 0; - } - // Poll packets - for(;;) + // If the conneciton is not nope, then clean out the recieved buffer + if( conn->State != TCP_ST_OPEN ) { - // Lock list and check if there is a packet Mutex_Acquire( &conn->lRecievedPackets ); - if( conn->RecievedBuffer->Length == 0 ) { - // If not, release the lock, yield and try again - Mutex_Release( &conn->lRecievedPackets ); - Threads_Yield(); // TODO: Less expensive wait - continue; - } + len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length ); + Mutex_Release( &conn->lRecievedPackets ); - // Attempt to read all `Length` bytes - len = RingBuffer_Read( destbuf, conn->RecievedBuffer, Length ); + if( len == 0 ) { + VFS_MarkAvaliable(Node, 0); + LEAVE('i', -1); + return -1; + } - // Release the lock (we don't need it any more) - Mutex_Release( &conn->lRecievedPackets ); - LEAVE('i', len); return len; } + + // Wait + VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "TCP_Client_Read"); + + // Lock list and read as much as possible (up to `Length`) + Mutex_Acquire( &conn->lRecievedPackets ); + len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length ); + + if( len == 0 || conn->RecievedBuffer->Length == 0 ) { + LOG("Marking as none avaliable (len = %i)", len); + VFS_MarkAvaliable(Node, 0); + } + + // Release the lock (we don't need it any more) + Mutex_Release( &conn->lRecievedPackets ); + + LEAVE('i', len); + return len; } /** @@ -792,6 +1072,10 @@ void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, void *Dat memcpy(packet->Options, Data, Length); + Log_Debug("TCP", "Send sequence 0x%08x", Connection->NextSequenceSend); + Debug_HexDump("[TCP ] TCP_INT_SendDataPacket: Data = ", + Data, Length); + TCP_SendPacket( Connection, sizeof(tTCPHeader)+Length, packet ); Connection->NextSequenceSend += Length; @@ -808,20 +1092,33 @@ Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buf ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer); // Check if connection is open - while( conn->State == TCP_ST_HALFOPEN || conn->State == TCP_ST_SYN_SENT ) + while( conn->State == TCP_ST_SYN_RCVD || conn->State == TCP_ST_SYN_SENT ) Threads_Yield(); + if( conn->State != TCP_ST_OPEN ) { - LEAVE('i', 0); - return 0; + VFS_MarkError(Node, 1); + LEAVE('i', -1); + return -1; } - while( rem > TCP_MAX_PACKET_SIZE ) + do { - TCP_INT_SendDataPacket(conn, TCP_MAX_PACKET_SIZE, Buffer); - Buffer += TCP_MAX_PACKET_SIZE; - } - - TCP_INT_SendDataPacket(conn, rem, Buffer); + int len = (rem < TCP_MAX_PACKET_SIZE) ? rem : TCP_MAX_PACKET_SIZE; + + #if 0 + // Wait for space in the buffer + Semaphore_Signal( &Connection->SentBufferSpace, len ); + + // Save data to buffer (and update the length read by the ammount written) + len = RingBuffer_Write( &Connection->SentBuffer, Buffer, len); + #endif + + // Send packet + TCP_INT_SendDataPacket(conn, len, Buffer); + + Buffer += len; + rem += len; + } while( rem > 0 ); LEAVE('i', Length); return Length; @@ -859,53 +1156,59 @@ void TCP_StartConnection(tTCPConnection *Conn) int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data) { tTCPConnection *conn = Node->ImplPtr; + + ENTER("pNode iID pData", Node, ID, Data); switch(ID) { case 4: // Get/Set local port if(!Data) - return conn->LocalPort; + LEAVE_RET('i', conn->LocalPort); if(conn->State != TCP_ST_CLOSED) - return -1; + LEAVE_RET('i', -1); if(!CheckMem(Data, sizeof(Uint16))) - return -1; + LEAVE_RET('i', -1); if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024) - return -1; + LEAVE_RET('i', -1); conn->LocalPort = *(Uint16*)Data; - return 0; + LEAVE_RET('i', conn->LocalPort); case 5: // Get/Set remote port - if(!Data) return conn->RemotePort; - if(conn->State != TCP_ST_CLOSED) return -1; - if(!CheckMem(Data, sizeof(Uint16))) return -1; + if(!Data) LEAVE_RET('i', conn->RemotePort); + if(conn->State != TCP_ST_CLOSED) LEAVE_RET('i', -1); + if(!CheckMem(Data, sizeof(Uint16))) LEAVE_RET('i', -1); conn->RemotePort = *(Uint16*)Data; - return 0; + LEAVE_RET('i', conn->RemotePort); case 6: // Set Remote IP if( conn->State != TCP_ST_CLOSED ) - return -1; + LEAVE_RET('i', -1); if( conn->Interface->Type == 4 ) { - if(!CheckMem(Data, sizeof(tIPv4))) return -1; + if(!CheckMem(Data, sizeof(tIPv4))) LEAVE_RET('i', -1); conn->RemoteIP.v4 = *(tIPv4*)Data; } else if( conn->Interface->Type == 6 ) { - if(!CheckMem(Data, sizeof(tIPv6))) return -1; + if(!CheckMem(Data, sizeof(tIPv6))) LEAVE_RET('i', -1); conn->RemoteIP.v6 = *(tIPv6*)Data; } - return 0; + LEAVE_RET('i', 0); case 7: // Connect if(conn->LocalPort == 0xFFFF) conn->LocalPort = TCP_GetUnusedPort(); if(conn->RemotePort == -1) - return 0; + LEAVE_RET('i', 0); TCP_StartConnection(conn); - return 1; + LEAVE_RET('i', 1); + + // Get recieve buffer length + case 8: + LEAVE_RET('i', conn->RecievedBuffer->Length); } return 0; @@ -916,20 +1219,75 @@ void TCP_Client_Close(tVFS_Node *Node) tTCPConnection *conn = Node->ImplPtr; tTCPHeader packet; - packet.SourcePort = htons(conn->LocalPort); - packet.DestPort = htons(conn->RemotePort); - packet.DataOffset = (sizeof(tTCPHeader)/4)*16; - packet.WindowSize = TCP_WINDOW_SIZE; + ENTER("pNode", Node); - packet.AcknowlegementNumber = 0; - packet.SequenceNumber = htonl(conn->NextSequenceSend); - packet.Flags = TCP_FLAG_FIN; + if( conn->State == TCP_ST_CLOSE_WAIT || conn->State == TCP_ST_OPEN ) + { + packet.SourcePort = htons(conn->LocalPort); + packet.DestPort = htons(conn->RemotePort); + packet.DataOffset = (sizeof(tTCPHeader)/4)*16; + packet.WindowSize = TCP_WINDOW_SIZE; + + packet.AcknowlegementNumber = 0; + packet.SequenceNumber = htonl(conn->NextSequenceSend); + packet.Flags = TCP_FLAG_FIN|TCP_FLAG_ACK; + + TCP_SendPacket( conn, sizeof(tTCPHeader), &packet ); + } + + switch( conn->State ) + { + case TCP_ST_CLOSE_WAIT: + conn->State = TCP_ST_CLOSED; + break; + case TCP_ST_OPEN: + conn->State = TCP_ST_FIN_WAIT1; + while( conn->State == TCP_ST_FIN_WAIT1 ) Threads_Yield(); + break; + default: + Log_Warning("TCP", "Unhandled connection state in TCP_Client_Close"); + break; + } + + free(conn); + + LEAVE('-'); +} + +/** + * \brief Checks if a value is between two others (after taking into account wrapping) + */ +int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue) +{ + if( MaxValue < 0xFFFFFFFF ) + { + Lower %= MaxValue + 1; + Value %= MaxValue + 1; + Higher %= MaxValue + 1; + } - conn->State = TCP_ST_FIN_SENT; + // Simple Case, no wrap ? + // Lower Value Higher + // | ... + ... + ... + ... | + + if( Lower < Higher ) { + return Lower < Value && Value < Higher; + } + // Higher has wrapped below lower - TCP_SendPacket( conn, sizeof(tTCPHeader), &packet ); + // Value > Lower ? + // Higher Lower Value + // | ... + ... + ... + ... | + if( Value > Lower ) { + return 1; + } - while( conn->State == TCP_ST_FIN_SENT ) Threads_Yield(); + // Value < Higher ? + // Value Higher Lower + // | ... + ... + ... + ... | + if( Value < Higher ) { + return 1; + } - free(conn); + return 0; }