TCP Server now seems to work, started fixing client (also need to clean up TCP code)
authorJohn Hodge <[email protected]>
Sat, 17 Apr 2010 05:29:39 +0000 (13:29 +0800)
committerJohn Hodge <[email protected]>
Sat, 17 Apr 2010 05:29:39 +0000 (13:29 +0800)
Modules/IPStack/ipv4.c
Modules/IPStack/tcp.c
Modules/IPStack/tcp.h
Usermode/Applications/testserver_src/main.c

index ae1c123..d65adb6 100644 (file)
@@ -104,8 +104,8 @@ void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buff
        if(Length < sizeof(tIPv4Header))        return;
        
        //Log_Log("IPv4", "Version = %i", hdr->Version);
-       Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
-       Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
+       //Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
+       //Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
        Log_Log("IPv4", "TotalLength = %i", ntohs(hdr->TotalLength) );
        //Log_Log("IPv4", "Identifcation = %i", ntohs(hdr->Identifcation) );
        //Log_Log("IPv4", "TTL = %i", hdr->TTL );
index ed103ec..a6fb60e 100644 (file)
@@ -72,13 +72,15 @@ void TCP_StartConnection(tTCPConnection *Conn)
 {
        tTCPHeader      hdr;
 
-       hdr.SourcePort = Conn->LocalPort;
-       hdr.DestPort = Conn->RemotePort;
+       Conn->State = TCP_ST_SYN_SENT;
+
+       hdr.SourcePort = htons(Conn->LocalPort);
+       hdr.DestPort = htons(Conn->RemotePort);
        Conn->NextSequenceSend = rand();
-       hdr.SequenceNumber = Conn->NextSequenceSend;
+       hdr.SequenceNumber = htonl(Conn->NextSequenceSend);
        hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
        hdr.Flags = TCP_FLAG_SYN;
-       hdr.WindowSize = 0xFFFF;        // Max
+       hdr.WindowSize = htons(TCP_WINDOW_SIZE);        // Max
        hdr.Checksum = 0;       // TODO
        hdr.UrgentPointer = 0;
        
@@ -128,9 +130,9 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe
 
        Log_Log("TCP", "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",
@@ -294,11 +296,27 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
        tTCPStoredPacket        *pkt;
         int    dataLen;
        
-       Connection->State = TCP_ST_OPEN;
        if(Header->Flags & TCP_FLAG_SYN) {
                Connection->NextSequenceRcv = ntohl(Header->SequenceNumber) + 1;
        }
        
+       if( Connection->State == TCP_ST_SYN_SENT )
+       {
+               if( Header->Flags & (TCP_FLAG_SYN|TCP_FLAG_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;
+                       Header->DataOffset = (sizeof(tTCPHeader)/4) << 4;
+                       Log_Log("TCP", "ACKing SYN-ACK");
+                       TCP_SendPacket( Connection, sizeof(tTCPHeader), Header );
+               }
+               Connection->State = TCP_ST_OPEN;
+       }
+       
        // Get length of data
        dataLen = Length - (Header->DataOffset>>4)*4;
        Log_Log("TCP", "HandleConnectionPacket - dataLen = %i", dataLen);
@@ -308,6 +326,23 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
                Log_Log("TCP", "Conn %p, Packet 0x%x ACKed", Connection, Header->AcknowlegementNumber);
        }
        
+       // TODO: Check what to do here
+       if(Header->Flags & TCP_FLAG_FIN) {
+               if( Connection->State == TCP_ST_FIN_SENT ) {
+                       
+               }
+               else {
+                       Connection->State = TCP_ST_FINISHED;
+                       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 ;
+               }
+       }
+       
        if(dataLen == 0) {
                Log_Log("TCP", "Empty Packet");
                return ;
@@ -355,7 +390,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
                TCP_INT_AppendRecieved(Connection, pkt);
                free(pkt);
                Log_Log("TCP", "0x%08x += %i", Connection->NextSequenceRcv, dataLen);
-               Connection->NextSequenceRcv += dataLen + 1;
+               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
@@ -363,6 +398,8 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
                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);
@@ -370,6 +407,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
                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 ++;
        }
 }
 
@@ -680,8 +718,8 @@ tVFS_Node *TCP_Client_Init(tInterface *Interface)
 
        conn->State = TCP_ST_CLOSED;
        conn->Interface = Interface;
-       conn->LocalPort = 0;
-       conn->RemotePort = 0;
+       conn->LocalPort = -1;
+       conn->RemotePort = -1;
        memset( &conn->RemoteIP, 0, sizeof(conn->RemoteIP) );
 
        conn->Node.ImplPtr = conn;
@@ -714,6 +752,7 @@ Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buff
        ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
        
        // Check if connection is open
+       while( conn->State == TCP_ST_HALFOPEN ) Threads_Yield();
        if( conn->State != TCP_ST_OPEN ) {
                LEAVE('i', 0);
                return 0;
@@ -758,7 +797,7 @@ void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, void *Dat
        //packet->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
        packet->AcknowlegementNumber = 0;
        packet->SequenceNumber = htonl(Connection->NextSequenceSend);
-       //packet->Flags = TCP_FLAG_PSH; // Hey, ACK if you can!
+       packet->Flags = TCP_FLAG_PSH;   // Hey, ACK if you can!
        
        memcpy(packet->Options, Data, Length);
        
@@ -778,6 +817,7 @@ 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 ) Threads_Yield();
        if( conn->State != TCP_ST_OPEN ) {
                LEAVE('i', 0);
                return 0;
index cdd3a8f..492e035 100644 (file)
@@ -136,8 +136,11 @@ struct sTCPConnection
 enum eTCPConnectionState
 {
        TCP_ST_CLOSED,
+       TCP_ST_SYN_SENT,
        TCP_ST_HALFOPEN,
-       TCP_ST_OPEN
+       TCP_ST_OPEN,
+       TCP_ST_FIN_SENT,
+       TCP_ST_FINISHED
 };
 
 #endif
index 3274174..0b787dd 100644 (file)
@@ -50,9 +50,9 @@ int main(int argc, char *argv[])
                
                #define RET_STR "HTTP/1.1 200 OK\r\n"\
                        "Content-Type: text/plain\r\n"\
-                       "Content-Length: 6\r\n"\
+                       "Content-Length: 92\r\n"\
                        "\r\n"\
-                       "Hello!"
+                       "<html><head><title>Acess2 Web Server</title></head><body><h1>Hello World!</h1></body></html>\r\n"
                
                write(con, sizeof(RET_STR), RET_STR);
                

UCC git Repository :: git.ucc.asn.au