+ // Check for an empty packet
+ if(dataLen == 0) {
+ if( Header->Flags != TCP_FLAG_ACK )
+ {
+ Connection->NextSequenceRcv ++; // TODO: Is this right? (empty packet counts as one byte)
+ Log_Log("TCP", "Empty Packet, inc and ACK the current sequence number");
+ }
+ else
+ Log_Log("TCP", "Empty Packet, just ACKing 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( dataLen + sizeof(tTCPStoredPacket) );
+ 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) )
+ {
+ 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 );
+ }
+ // 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;