Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
60:2b770949c911
Parent:
58:8f39f28f9b14
Child:
62:f192926e42f1
--- a/main.cpp	Mon Apr 24 05:39:02 2017 +0000
+++ b/main.cpp	Sun Jun 04 18:18:29 2017 +0000
@@ -80,7 +80,7 @@
 
 pppType ppp; // our global - definitely not thread safe
 
-// intitialize our globals
+// Initialize our globals
 void pppInitStruct()
 {
     ppp.online=0;
@@ -620,19 +620,17 @@
 
     // Now we have to recalculate all the header sizes, swap IP address/port source and destination, and do the IP and TCP checksums
 
-    char src[4]; // temp hold
-    char dst[4]; // temp hold
-    memcpy(src, srcAdr,4);
-    memcpy(dst, dstAdr,4);
-    memcpy(srcAdr, dst,4);
-    memcpy(dstAdr, src,4); // swap ip address source/dest
+    char tempHold[12]; // a temporary hold buffer
 
-    char psrc[2]; // temp hold
-    char pdst[2]; // temp hold
-    memcpy(psrc, srctcp,2);
-    memcpy(pdst, dsttcp,2);
-    memcpy(srctcp, pdst,2);
-    memcpy(dsttcp, psrc,2); // swap ip port source/dest
+    memcpy(tempHold, srcAdr,4);
+    memcpy(tempHold+4, dstAdr,4);
+    memcpy(srcAdr, tempHold+4,4);
+    memcpy(dstAdr, tempHold,4); // swap ip address source/dest
+
+    memcpy(tempHold, srctcp,2);
+    memcpy(tempHold+2, dsttcp,2);
+    memcpy(srctcp, tempHold+2,2);
+    memcpy(dsttcp, tempHold,2); // swap ip port source/dest
 
     ack = seq + incomingLen; // acknowledge the number of bytes that they sent by adding it to "our" sequence number
     seq = ppp.seq; // set up the sequence number we have to respond with
@@ -660,23 +658,24 @@
     headercheck[0]=0;
     headercheck[1]=0;
     headerCheckSum(); // redo the ip header checksum
+    
+    // now we have to build a 12-byte TCP pseudo-header in front of the TCP packet in order to calculate the correct TCP checksum
+    
+    memcpy(tempHold, s-12, 12); // preserve the 12 bytes in the IP header that are in the way of the pseudo-header
+    
+    memcpy( s-12, srcAdr, 8); // source and destination addresses.
+    memset( s-4, 0, 1); // reserved - must be set to zero
+    memset( s-3, protocol[0], 1); // protocol field from the IP header
+    memset( s-2, tcpSize>>8, 1); // length of TCP header
+    memset( s-1, tcpSize, 1); // length of TCP header
 
-    char pseudoHeader[12]; // we are building a fake TCP header
-    int sum;
-    memcpy( pseudoHeader+0, srcAdr, 8); // source and destination addresses.
-    pseudoHeader[8]=0;
-    pseudoHeader[9]=protocol[0];
-    pseudoHeader[10]=tcpSize>>8;
-    pseudoHeader[11]=tcpSize;
-    char temp[12]; // temporary storage for the 12 bytes that are in the way
-    memcpy(temp, s-12, 12); // save the 12 bytes that are in the way
-    memcpy( s-12, pseudoHeader, 12); // copy in the fake header
-    checksumtcp[0]=0;
-    checksumtcp[1]=0;
-    sum=dataCheckSum(s-12,tcpSize+12); // calculate the TCP checksum with the fake header
+    checksumtcp[0]=0; // has to be zero for fake header calculation
+    checksumtcp[1]=0; // has to be zero for fake header calculation
+
+    int sum=dataCheckSum(s-12,tcpSize+12); // calculate the TCP checksum starting at the fake 12-byte header
     checksumtcp[0]=sum>>8;
     checksumtcp[1]=sum;
-    memcpy( s-12, temp, 12); // restore the 12 bytes that the fake header overwrote
+    memcpy( s-12, tempHold, 12); // restore the 12 bytes that the fake header overwrote
     if (fastResponse==1) {
         fastResponse=0; // reset and skip 200 ms wait
     } else {