Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Revision:
59:e0e556c8bd46
Parent:
57:e0fb648acf48
--- a/tcp/tcp.cpp	Thu Dec 07 20:44:32 2017 +0000
+++ b/tcp/tcp.cpp	Thu Dec 14 20:55:40 2017 +0000
@@ -8,7 +8,7 @@
 #include   "dhcp.h"
 #include   "http.h"
 
-#define MAX_MSS 536 //This is 576 - 20 - 20
+static int maxMss = 0;
 
 bool TcpTrace = false;
 static bool doTrace = false;
@@ -40,22 +40,46 @@
 static bool            RST; //Reset the connection
 static bool            SYN; //Synchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags and fields change meaning based on this flag, and some are only valid for when it is set, and others when it is clear.
 static bool            FIN; //No more data from sender
-
 static uint16_t     window;
 static uint16_t   checksum;
 static uint16_t     urgent;
-static int    optionLength;
-    
-static char*    pOptions;
-static char*    pData;
+
+//Payload variables
+static char*    pOptionsRx;
+static char*    pOptionsTx;
+static int      optionLength;
+static char*    pDataRx;
+static char*    pDataTx;
 static int      dataLength;
+
+
+static void readHeader(void* pPacket, uint16_t size)
+{
+    struct header* pHeader = (header*)pPacket;
+                        
+        srcPort = NetToHost16(pHeader->srcPort);
+        dstPort = NetToHost16(pHeader->dstPort);
+         seqnum = NetToHost32(pHeader->seqnum);
+         acknum = NetToHost32(pHeader->acknum);
+     headersize =            (pHeader->dataOffset >> 2) & 0xFC; //Same as right shifting by 4 bits and multiplying by 4
+          flags =             pHeader->flags;
+            URG = flags & 0x20; //indicates that the Urgent pointer field is significant
+            ACK = flags & 0x10; //indicates that the Acknowledgment field is significant. All packets after the initial SYN packet sent by the client should have this flag set.
+            PSH = flags & 0x08; //Push function. Asks to push the buffered data to the receiving application.
+            RST = flags & 0x04; //Reset the connection
+            SYN = flags & 0x02; //Synchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags and fields change meaning based on this flag, and some are only valid for when it is set, and others when it is clear.
+            FIN = flags & 0x01; //No more data from sender
+
+         window = NetToHost16(pHeader->window);
+       checksum = NetToHost16(pHeader->checksum);
+         urgent = NetToHost16(pHeader->urgent);
+}
+
 static int positionInQuery;
 static int positionInReply;
 
 static uint16_t mss;
 
-static void (*pTraceback)(void);
-
 static struct tcb* pTcb;
 
 static void logFlags()
@@ -106,7 +130,7 @@
 static void readOptions()
 {
     mss = 536; //default MSS for IPv4 [576 - 20(TCP) - 20(IP)];
-    for (char* p = pOptions; p < pOptions + optionLength; p++)
+    for (char* p = pOptionsRx; p < pOptionsRx + optionLength; p++)
     {
         switch (*p)
         {
@@ -123,43 +147,17 @@
             default: LogTimeF("Unrecognised TCP option %d\r\n", *p);
         }
     }
-    if (mss > MAX_MSS) mss = MAX_MSS;
+    if (mss > maxMss) mss = maxMss;
 }
 static void writeOptions()
 {
-    pOptions[0]  = 2;
-    pOptions[1]  = 4;
-    pOptions[2]  = mss >> 8;
-    pOptions[3]  = mss & 0xFF;
+    pOptionsTx[0]  = 2;
+    pOptionsTx[1]  = 4;
+    pOptionsTx[2]  = mss >> 8;
+    pOptionsTx[3]  = mss & 0xFF;
     optionLength = 4;
 }
 
-void TcpReadHeader(void* pPacket, uint16_t size)
-{
-    struct header* pHeader = (header*)pPacket;
-                        
-        srcPort = NetToHost16(pHeader->srcPort);
-        dstPort = NetToHost16(pHeader->dstPort);
-         seqnum = NetToHost32(pHeader->seqnum);
-         acknum = NetToHost32(pHeader->acknum);
-     headersize =            (pHeader->dataOffset >> 2) & 0xFC; //Same as right shifting by 4 bits and multiplying by 4
-          flags =             pHeader->flags;
-            URG = flags & 0x20; //indicates that the Urgent pointer field is significant
-            ACK = flags & 0x10; //indicates that the Acknowledgment field is significant. All packets after the initial SYN packet sent by the client should have this flag set.
-            PSH = flags & 0x08; //Push function. Asks to push the buffered data to the receiving application.
-            RST = flags & 0x04; //Reset the connection
-            SYN = flags & 0x02; //Synchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags and fields change meaning based on this flag, and some are only valid for when it is set, and others when it is clear.
-            FIN = flags & 0x01; //No more data from sender
-
-         window = NetToHost16(pHeader->window);
-       checksum = NetToHost16(pHeader->checksum);
-         urgent = NetToHost16(pHeader->urgent);
-       pOptions = (char*)pPacket + 20;
-   optionLength =     headersize - 20;
-          pData = (char*)pPacket + headersize;
-     dataLength =           size - headersize;
-}
-
 void TcpMakeHeader(int size, void* pPacket)
 {
     struct header* pHeader = (header*)pPacket;
@@ -191,7 +189,7 @@
         Log("\r\n");
     }
     dataLength = 0;
-    headersize = 20;
+    headersize = sizeof(header);
     ACK = false;
     PSH = false;
     RST = true;
@@ -207,11 +205,10 @@
     pTcb->port    = srcPort;
     pTcb->hadFin  = false;
         
+    mss = maxMss;                 //Ethernet 1500 - 20 - 20; or, in our case 768 - 20 - 20
+    writeOptions();
+    headersize = sizeof(header) + optionLength;
     dataLength = 0;
-    
-    mss = MAX_MSS;                 //Ethernet 1500 - 20 - 20; or, in our case 768 - 20 - 20
-    writeOptions();
-    headersize = 24;               //20 header plus 4 option
    
     ACK = true;                    //Send ACK and SYN
     PSH = false;
@@ -227,14 +224,11 @@
 }
 static void handleEstablishedConnection()
 {    
-    char* pRequestStream = pData;
-    char* pReplyStream   = pOptions;
+    pDataTx   = pOptionsTx; //No options
+    headersize = sizeof(header);
 
-    HttpHandleRequest(&dataLength, pRequestStream, positionInQuery - 1, pReplyStream, positionInReply - 1, mss, &pTcb->todo);
+    HttpHandleRequest(&dataLength, pDataRx, positionInQuery - 1, pDataTx, positionInReply - 1, mss, &pTcb->todo);
     
-    headersize = 20;
-    pData = pReplyStream;
-        
     ACK = true;                 //Send ACK
     RST = false;
     SYN = false;
@@ -261,15 +255,24 @@
     SYN = false;
     FIN = false;
     
-    headersize = 20;
+    headersize = sizeof(header);
     dataLength = 0;
     
     pTcb->state  = TCB_EMPTY;
 }
 
-int TcpHandleReceivedPacket(void (*traceback)(void), int* pSize, void* pPacket)
+int TcpHandleReceivedPacket(void (*traceback)(void), int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx)
 {
-    pTraceback = traceback;
+    readHeader(pPacketRx, sizeRx);
+    
+     pDataRx    = (char*)pPacketRx + headersize;
+     dataLength =           sizeRx - headersize;
+     maxMss     =         *pSizeTx - sizeof(header);
+    
+     pOptionsRx = (char*)pPacketRx + sizeof(header);
+     pOptionsTx = (char*)pPacketTx + sizeof(header);
+   optionLength =       headersize - sizeof(header);
+    
     doTrace = false;
     
     //Filter out unwanted links
@@ -289,7 +292,7 @@
             {
                 if (NetTraceNewLine) Log("\r\n");
                 LogTimeF("TCP unknown port %d\r\n", dstPort);
-                if (NetTraceStack) pTraceback();
+                if (NetTraceStack) traceback();
             }
             return DO_NOTHING; //Ignore unknown ports
     }
@@ -303,7 +306,7 @@
         {
             if (NetTraceNewLine) Log("\r\n");
             LogTime("TCP no more tcbs are available\r\n");
-            if (NetTraceStack) pTraceback();
+            if (NetTraceStack) traceback();
         }
         return DO_NOTHING; //Bomb out if no more tcbs are available
     }
@@ -315,7 +318,7 @@
         {
             if (NetTraceNewLine) Log("\r\n");
             LogTime("TCP received reset - resetting TCB\r\n");
-            if (NetTraceStack) pTraceback();
+            if (NetTraceStack) traceback();
         }
         pTcb->state  = TCB_EMPTY; //Reset connection
         return DO_NOTHING;        //Don't reply
@@ -346,13 +349,13 @@
             if (positionInReply != pTcb->sentBytes) LogF(": acknowledging byte %u rather than %u so data resent", positionInReply, pTcb->sentBytes);
             Log("\r\n");
             
-            if (NetTraceStack) pTraceback();
+            if (NetTraceStack) traceback();
             doTrace = true;
         }
         pTcb->recdBytes = positionInQuery;
         pTcb->sentBytes = positionInReply;
     }
-    if (doTrace && NetTraceStack) pTraceback(); //This will already include the TCP header
+    if (doTrace && NetTraceStack) traceback(); //This will already include the TCP header
     
     if (SYN) pTcb->recdBytes += 1;            //Add one to acknowledge the SYN
              pTcb->recdBytes += dataLength;   //Add the number of bytes received
@@ -405,7 +408,7 @@
     dstPort = pTcb->port;
     
     //Calculate the size of the reply
-    *pSize = dataLength + headersize;
+    *pSizeTx = dataLength + headersize;
         
     return ActionMakeFromDestAndTrace(UNICAST, doTrace && NetTraceStack);