#include "ipv4.h"
#include "tcp.h"
-#define TCP_MIN_DYNPORT 0x1000
+#define TCP_MIN_DYNPORT 0xC000
#define TCP_MAX_HALFOPEN 1024 // Should be enough
// === PROTOTYPES ===
void TCP_Initialise();
void TCP_StartConnection(tTCPConnection *Conn);
-void TCP_SendPacket( tTCPConnection *Conn, size_t Length, tTCPHeader *Data );
+void TCP_SendPacket(tTCPConnection *Conn, size_t Length, tTCPHeader *Data);
void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
-void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header);
+void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length);
Uint16 TCP_GetUnusedPort();
int TCP_AllocatePort(Uint16 Port);
int TCP_DeallocatePort(Uint16 Port);
tTCPConnection *conn;
Log("[TCP ] sizeof(tTCPHeader) = %i", sizeof(tTCPHeader));
+ Log("[TCP ] SourcePort = %i", ntohs(hdr->SourcePort));
Log("[TCP ] DestPort = %i", ntohs(hdr->DestPort));
- Log("[TCP ] DestPort = %i", ntohs(hdr->DestPort));
- Log("[TCP ] SequenceNumber = %i", ntohl(hdr->SequenceNumber));
- Log("[TCP ] AcknowlegementNumber = %i", ntohl(hdr->AcknowlegementNumber));
+ Log("[TCP ] SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
+ Log("[TCP ] AcknowlegementNumber = 0x%x", ntohl(hdr->AcknowlegementNumber));
Log("[TCP ] DataOffset = %i", hdr->DataOffset >> 4);
Log("[TCP ] Flags = {");
Log("[TCP ] CWR = %B", !!(hdr->Flags & TCP_FLAG_CWR));
continue;
// We have a response!
- TCP_INT_HandleConnectionPacket(conn, hdr);
+ TCP_INT_HandleConnectionPacket(conn, hdr, Length);
return;
}
// Open a new connection (well, check that it's a SYN)
- //TODO
+ if(hdr->Flags != TCP_FLAG_SYN) {
+ Log("[TCP ] Packet is not a SYN");
+ continue;
+ }
+
+ // TODO: Check for halfopen max
+
+ conn = calloc(1, sizeof(tTCPConnection));
+ conn->State = TCP_ST_HALFOPEN;
+ conn->LocalPort = srv->Port;
+ conn->RemotePort = hdr->SourcePort;
+ conn->Interface = Interface;
+
+ switch(Interface->Type)
+ {
+ case 4: conn->RemoteIP.v4 = *(tIPv4*)Address; break;
+ case 6: conn->RemoteIP.v6 = *(tIPv6*)Address; break;
+ }
+
+ conn->NextSequenceRcv = ntohl( hdr->SequenceNumber );
+ conn->NextSequenceSend = rand();
+
+ // Create node
+ conn->Node.NumACLs = 1;
+ conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
+ conn->Node.ImplInt = srv->NextID ++;
+ conn->Node.Read = TCP_Client_Read;
+ conn->Node.Write = TCP_Client_Write;
+ //conn->Node.Close = TCP_SrvConn_Close;
+
+ // Hmm... Theoretically, this lock will never have to wait,
+ // as the interface is locked to the watching thread, and this
+ // runs in the watching thread. But, it's a good idea to have
+ // it, just in case
+ LOCK(&srv->lConnections);
+ conn->Next = srv->Connections;
+ srv->Connections = conn;
+ RELEASE(&srv->lConnections);
+
+ // Send the SYN ACK
+ Log("[TCP ] TODO: Sending SYN ACK");
+ hdr->Flags |= TCP_FLAG_ACK;
+ hdr->AcknowlegementNumber = hdr->SequenceNumber;
+ hdr->SequenceNumber = conn->NextSequenceSend;
+ hdr->DestPort = hdr->SourcePort;
+ hdr->SourcePort = srv->Port;
+ TCP_SendPacket( conn, sizeof(tTCPHeader), hdr );
break;
}
if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
continue;
- TCP_INT_HandleConnectionPacket(conn, hdr);
+ TCP_INT_HandleConnectionPacket(conn, hdr, Length);
}
}
}
/**
* \brief Handles a packet sent to a specific connection
*/
-void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header)
-{
-
+void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length)
+{
+ tTCPStoredPacket *pkt;
+ int dataLen;
+
+ Connection->State = TCP_ST_OPEN;
+ if(Header->Flags & TCP_FLAG_SYN) {
+ Connection->NextSequenceRcv = Header->SequenceNumber + 1;
+ }
+
+ if(Header->Flags & TCP_FLAG_ACK) {
+ // TODO: Process an ACKed Packet
+ Log("[TCP ] Conn %p, Packet 0x%x ACKed", Connection, Header->AcknowlegementNumber);
+ return ;
+ }
+
+ // Allocate and fill cached packet
+ dataLen = Length - (Header->DataOffset&0xF)*4;
+ pkt = malloc( dataLen + sizeof(tTCPStoredPacket) );
+ pkt->Next = NULL;
+ pkt->Sequence = Header->SequenceNumber;
+ memcpy(pkt->Data, (Uint8*)Header + (Header->DataOffset&0xF)*4, dataLen);
+
+ if( Header->SequenceNumber != Connection->NextSequenceRcv )
+ {
+ tTCPStoredPacket *tmp, *prev;
+ for(tmp = Connection->FuturePackets;
+ tmp;
+ prev = tmp, tmp = tmp->Next)
+ {
+ if(tmp->Sequence > pkt->Sequence) break;
+ }
+ if(prev)
+ prev->Next = pkt;
+ else
+ Connection->FuturePackets = pkt;
+ pkt->Next = tmp;
+ }
+ else
+ {
+ LOCK( &Connection->lRecievedPackets );
+ if(Connection->RecievedPackets)
+ {
+ Connection->RecievedPacketsTail->Next = pkt;
+ Connection->RecievedPacketsTail = pkt;
+ }
+ else
+ {
+ Connection->RecievedPackets = pkt;
+ Connection->RecievedPacketsTail = pkt;
+ }
+ RELEASE( &Connection->lRecievedPackets );
+ }
}
/**