Cleanup commit, fixes to UDP, TCP and ICMP
[tpg/acess2.git] / Modules / IPStack / tcp.c
index dd94b92..9b3f8f4 100644 (file)
@@ -12,6 +12,7 @@
 // === PROTOTYPES ===
 void   TCP_Initialise();
 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);
 void   TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header);
 Uint16 TCP_GetUnusedPort();
@@ -61,16 +62,16 @@ void TCP_Initialise()
 void TCP_StartConnection(tTCPConnection *Conn)
 {
        tTCPHeader      hdr;
-       
-       hdr->SourcePort = Conn->LocalPort;
-       hdr->DestPort = Conn->RemotePort;
+
+       hdr.SourcePort = Conn->LocalPort;
+       hdr.DestPort = Conn->RemotePort;
        Conn->NextSequenceSend = rand();
-       hdr->SequenceNumber = Conn->NextSequenceSend;
-       hdr->DataOffset = (sizeof(tTCPHeader)+3)/4;
-       hdr->Flags = TCP_FLAG_SYN;
-       hdr->WindowSize = 0;    // TODO
-       hdr->Checksum = 0;      // TODO
-       hdr->UrgentPointer = 0;
+       hdr.SequenceNumber = Conn->NextSequenceSend;
+       hdr.DataOffset = (sizeof(tTCPHeader)+3)/4;
+       hdr.Flags = TCP_FLAG_SYN;
+       hdr.WindowSize = 0;     // TODO
+       hdr.Checksum = 0;       // TODO
+       hdr.UrgentPointer = 0;
        // SEND PACKET
        TCP_SendPacket( Conn, sizeof(tTCPHeader), &hdr );
        return ;
@@ -86,19 +87,19 @@ void TCP_SendPacket( tTCPConnection *Conn, size_t Length, tTCPHeader *Data )
 {
        size_t  buflen;
        Uint32  *buf;
-       switch( Conn->Interface.Type )
+       switch( Conn->Interface->Type )
        {
-       case 4:
+       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] = Conn->Interface->IP4.Address.L;
                buf[1] = Conn->RemoteIP.v4.L;
                buf[2] = (htons(Length)<<16) | (6<<8) | 0;
                Data->Checksum = 0;
-               memcpy( &buf[3], Data, buflen );
+               memcpy( &buf[3], Data, Length );
                Data->Checksum = IPv4_Checksum( buf, buflen );
                free(buf);
-               IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, sizeof(tTCPHeader), &hdr);
+               IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, buflen, buf);
                break;
        }
 }
@@ -112,12 +113,12 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe
        tTCPHeader      *hdr = Buffer;
        tTCPListener    *srv;
        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));
@@ -132,7 +133,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe
        Log("[TCP  ] WindowSize = %i", htons(hdr->WindowSize));
        Log("[TCP  ] Checksum = 0x%x", htons(hdr->Checksum));
        Log("[TCP  ] UrgentPointer = 0x%x", htons(hdr->UrgentPointer));
-       
+
        // Check Servers
        {
                for( srv = gTCP_Listeners; srv; srv = srv->Next )
@@ -143,53 +144,97 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe
                        if(srv->Interface && srv->Interface != Interface)       continue;
                        // Check the destination port
                        if(srv->Port != hdr->DestPort)  continue;
-                       
+
                        // Is this in an established connection?
                        for( conn = srv->Connections; conn; conn = conn->Next )
                        {
                                // Check that it is coming in on the same interface
                                if(conn->Interface != Interface)        continue;
-                               
+
                                // Check Source Port
                                if(conn->RemotePort != hdr->SourcePort) continue;
-                               
+
                                // Check Source IP
                                if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
                                        continue;
                                if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
                                        continue;
-                               
+
                                // We have a response!
-                               TCP_INT_HandleConnectionPacket(conn, hdr)
-                               
+                               TCP_INT_HandleConnectionPacket(conn, hdr);
+
                                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;
+                       }
+                       
+                       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;
                }
        }
-       
-       
+
+
        // Check Open Connections
        {
                for( conn = gTCP_OutbountCons; conn; conn = conn->Next )
                {
                        // Check that it is coming in on the same interface
                        if(conn->Interface != Interface)        continue;
-                       
+
                        // Check Source Port
                        if(conn->RemotePort != hdr->SourcePort) continue;
-                       
+
                        // Check Source IP
                        if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
                                continue;
                        if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
                                continue;
-                       
-                       TCP_INT_HandleConnectionPacket(conn, hdr)
+
+                       TCP_INT_HandleConnectionPacket(conn, hdr);
                }
        }
 }
@@ -199,7 +244,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe
  */
 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header)
 {
-       
+
 }
 
 /**
@@ -209,7 +254,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
 Uint16 TCP_GetUnusedPort()
 {
        Uint16  ret;
-       
+
        // Get Next outbound port
        ret = giTCP_NextOutPort++;
        while( gaTCP_PortBitmap[ret/32] & (1 << (ret%32)) )
@@ -220,10 +265,10 @@ Uint16 TCP_GetUnusedPort()
                        ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
                }
        }
-       
+
        // Mark the new port as used
        gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
-       
+
        return ret;
 }
 
@@ -236,10 +281,10 @@ int TCP_AllocatePort(Uint16 Port)
        // Check if the port has already been allocated
        if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
                return 0;
-       
+
        // Allocate
        gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
-       
+
        return 1;
 }
 
@@ -252,10 +297,10 @@ int TCP_DeallocatePort(Uint16 Port)
        // Check if the port has already been allocated
        if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
                return 0;
-       
+
        // Allocate
        gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));
-       
+
        return 1;
 }
 
@@ -263,7 +308,7 @@ int TCP_DeallocatePort(Uint16 Port)
 tVFS_Node *TCP_Server_Init(tInterface *Interface)
 {
        tTCPListener    *srv = malloc( sizeof(tTCPListener) );
-       
+
        srv->Interface = Interface;
        srv->Port = 0;
        srv->NextID = 0;
@@ -276,12 +321,12 @@ tVFS_Node *TCP_Server_Init(tInterface *Interface)
        srv->Node.FindDir = TCP_Server_FindDir;
        srv->Node.IOCtl = TCP_Server_IOCtl;
        srv->Node.Close = TCP_Server_Close;
-       
+
        LOCK(&glTCP_Listeners);
        srv->Next = gTCP_Listeners;
        gTCP_Listeners = srv;
        RELEASE(&glTCP_Listeners);
-       
+
        return &srv->Node;
 }
 
@@ -296,14 +341,14 @@ char *TCP_Server_ReadDir(tVFS_Node *Node, int Pos)
        tTCPListener    *srv = Node->ImplPtr;
        tTCPConnection  *conn;
        char    *ret;
-       
+
        while( srv->NewConnections == NULL )    Threads_Yield();
-       
+
        conn = srv->NewConnections;
        srv->NewConnections = conn->Next;
-       
+
        ret = malloc(9);
-       itoa(ret, conn->ImplInt, 16, '0', 8);
+       itoa(ret, Node->ImplInt, 16, '0', 8);
        return ret;
 }
 
@@ -320,27 +365,27 @@ tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, char *Name)
 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data)
 {
        tTCPListener    *srv = Node->ImplPtr;
-       
+
        switch(ID)
        {
        case 4: // Get/Set Port
                if(!Data)       // Get Port
                        return srv->Port;
-               
+
                if(srv->Port)   // Wait, you can't CHANGE the port
                        return -1;
-               
+
                if(!CheckMem(Data, sizeof(Uint16)))     // Sanity check
                        return -1;
-               
+
                // Permissions check
                if(Threads_GetUID() != 0
                && *(Uint16*)Data != 0
                && *(Uint16*)Data < 1024)
                        return -1;
-               
+
                // TODO: Check if a port is in use
-               
+
                // Set Port
                srv->Port = *(Uint16*)Data;
                if(srv->Port == 0)      // Allocate a random port
@@ -361,13 +406,13 @@ void TCP_Server_Close(tVFS_Node *Node)
 tVFS_Node *TCP_Client_Init(tInterface *Interface)
 {
        tTCPConnection  *conn = malloc( sizeof(tTCPConnection) );
-       
+
        conn->State = TCP_ST_CLOSED;
        conn->Interface = Interface;
        conn->LocalPort = 0;
        conn->RemotePort = 0;
        memset( &conn->RemoteIP, 0, sizeof(conn->RemoteIP) );
-       
+
        conn->Node.ImplPtr = conn;
        conn->Node.NumACLs = 1;
        conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
@@ -375,12 +420,12 @@ tVFS_Node *TCP_Client_Init(tInterface *Interface)
        conn->Node.Write = TCP_Client_Write;
        conn->Node.IOCtl = TCP_Client_IOCtl;
        conn->Node.Close = TCP_Client_Close;
-       
+
        LOCK(&glTCP_OutbountCons);
        conn->Next = gTCP_OutbountCons;
        gTCP_OutbountCons = conn;
        RELEASE(&glTCP_OutbountCons);
-       
+
        return &conn->Node;
 }
 
@@ -394,10 +439,13 @@ Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buf
        return 0;
 }
 
+/**
+ * \brief Control a client socket
+ */
 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
 {
        tTCPConnection  *conn = Node->ImplPtr;
-       
+
        switch(ID)
        {
        case 4: // Get/Set local port
@@ -407,20 +455,20 @@ int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
                        return -1;
                if(!CheckMem(Data, sizeof(Uint16)))
                        return -1;
-               
+
                if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
                        return -1;
-               
+
                conn->LocalPort = *(Uint16*)Data;
                return 0;
-       
+
        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;
                conn->RemotePort = *(Uint16*)Data;
                return 0;
-       
+
        case 6: // Set Remote IP
                if( conn->State != TCP_ST_CLOSED )
                        return -1;
@@ -435,17 +483,17 @@ int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
                        conn->RemoteIP.v6 = *(tIPv6*)Data;
                }
                return 0;
-       
+
        case 7: // Connect
                if(conn->LocalPort == -1)
                        conn->LocalPort = TCP_GetUnusedPort();
                if(conn->RemotePort == -1)
                        return 0;
-               
+
                TCP_StartConnection(conn);
                return 1;
        }
-       
+
        return 0;
 }
 

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