+
+ // 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.
+ // - Meh, no real issue, as the cache shouldn't be that large
+ 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, sequence_num,
+ Connection->NextSequenceRcv+TCP_WINDOW_SIZE, 0xFFFFFFFF) )
+ {
+ Uint8 *dataptr = (Uint8*)Header + (Header->DataOffset>>4)*4;
+ #if CACHE_FUTURE_PACKETS_IN_BYTES
+ Uint32 index;
+ int i;
+
+ index = sequence_num % TCP_WINDOW_SIZE;
+ for( i = 0; i < dataLen; i ++ )
+ {
+ Connection->FuturePacketValidBytes[index/8] |= 1 << (index%8);
+ Connection->FuturePacketData[index] = dataptr[i];
+ // Do a wrap increment
+ index ++;
+ if(index == TCP_WINDOW_SIZE) index = 0;
+ }
+ #else
+ tTCPStoredPacket *pkt, *tmp, *prev = NULL;
+
+ // Allocate and fill cached packet
+ pkt = malloc( sizeof(tTCPStoredPacket) + dataLen );
+ pkt->Next = NULL;
+ pkt->Sequence = ntohl(Header->SequenceNumber);
+ pkt->Length = dataLen;
+ memcpy(pkt->Data, dataptr, dataLen);
+
+ 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); // TODO: Find some way to remove this
+ }
+ SHORTREL( &Connection->lFuturePackets );
+ #endif
+ }
+ // Badly out of sequence packet