Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
69:23f560087c16
Parent:
68:0b74763ae67f
Child:
70:713f86229288
--- a/main.cpp	Mon Jun 05 22:45:39 2017 +0000
+++ b/main.cpp	Wed Jun 07 11:18:49 2017 +0000
@@ -61,7 +61,9 @@
 <h1 id=\"w\" style=\"text-align:center;\">0</h1>\
 <h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\
 </body>\
-</html>"; // current size is approximately 470 characters
+</html>"; // around 464 bytes long
+
+
 
 // The serial port on your mbed hardware. Your PC should view this as a standard dial-up networking modem. See instructions at the top.
 // On a typical mbed hardware platform this is a USB virtual com port (VCP)
@@ -73,9 +75,9 @@
 #define FRAME_7E (0x7e)
 
 // the serial port receive buffer and packet buffer
-#define BUFLEN (1<<12)
+#define BUFLEN (1<<13)
 char rxbufppp[BUFLEN]; // BUFLEN MUST be a power of two because we use & operator for fast wrap-around in rxHandler
-char hdlcBuffer[2000]; // send and receive buffer for unstuffed (decoded) hdlc frames
+char hdlcBuffer[3000]; // send and receive buffer for unstuffed (decoded) hdlc frames
 
 // a structure to keep all our ppp globals in
 struct pppType {
@@ -602,35 +604,44 @@
 #define TCP_FLAG_RST (1<<2)
 #define TCP_FLAG_FIN (1<<0)
 
-    // A sparse TCP flag interpreter that implements simple TCP connections from a single source
-    // Clients are allowed ONE push packet, after which the link is closed with a FIN flag in the ACK packet
-    // This strategy allows web browsers, netcat and curl to work ok while keeping the state machine simple
-
     int dataLen = 0; // most of our responses will have zero TCP data, only a header
     int flagsOut = TCP_FLAG_ACK; // the default case is an ACK packet
     int fastResponse = 0; // normally you wait 200ms before sending a packet but this can make it faster
 
     ppp.seq = ack;    // always adopt their sequence number calculation in place of doing our own calculation
 
-    if ( flagsTCP == TCP_FLAG_ACK ) {
-        if (tcpDataSize == 0) { // ignore - just an empty ack packet
-            return;
-        }
-    } else if ( (flagsTCP & TCP_FLAG_SYN) != 0 ) { // got SYN flag
-        flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // do a syn-ack
-        seq++; // for SYN flag we have to increase sequence by 1
-    } else if ( (flagsTCP & TCP_FLAG_FIN) != 0 ) { // got FIN flag
-        seq++; // for FIN flag we have to increase sequence by 1
-    } else if ( (flagsTCP & TCP_FLAG_PSH) != 0 ) { // got PSH flag (push)
-        flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // for every push we answer once AND close the link
-        fastResponse = 1; // we can respond fast to a push
-        // It's a push, so let's check the incoming data for an HTTP GET request
-        if ( strncmp(dataStart, "GET ", 4) == 0) { // do we see an http GET command
-            dataLen = httpResponse(dataStart); // send an http response
-        }
+    // A sparse TCP flag interpreter that implements simple TCP connections from a single source
+    // Clients are allowed ONE push packet, after which the link is closed with a FIN flag in the ACK packet
+    // This strategy allows web browsers, netcat and curl to work ok while keeping the state machine simple
+    
+    switch ( flagsTCP ) {
+        case TCP_FLAG_ACK:
+            if ( tcpDataSize != 1 ) return;
+        case TCP_FLAG_SYN:
+            flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // something wants to connect - ack it
+            seq++; // for SYN flag we have to increase sequence by 1
+            break;
+        case TCP_FLAG_ACK | TCP_FLAG_PSH:            
+            flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // for every push we answer once AND close the link
+            fastResponse = 1; // we can respond fast to a push
+            if ( strncmp(dataStart, "GET ", 4) == 0) { // do we see an http GET command
+                dataLen = httpResponse(dataStart); // send an http response
+                while((dataLen %4 ) !=0) { // dataLen must be a multiple of four
+                 dataLen++; // must be a multiple of four
+                 dataStart[dataLen-1]=0; // clear the byte in the buffer
+                }
+            }
+            break;
+        case TCP_FLAG_FIN:            
+        case TCP_FLAG_FIN | TCP_FLAG_ACK:
+        case TCP_FLAG_FIN | TCP_FLAG_ACK | TCP_FLAG_PSH:
+            seq++; // for FIN flag we have to increase sequence by 1
+            break;
+        default: 
+            return; // ignore remaining packets
     }
-
-    // All the TCP flag handling is now done
+    
+    // The TCP flag handling is now done
     // Now we have to recalculate all the header sizes, swap IP address/port source and destination, and do the IP and TCP checksums
 
     char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header