RealTimeCompLab2 / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Revision:
159:4d1bf96a59cd
Parent:
158:841592aed956
Child:
160:bd701ad564cb
--- a/PPP-Blinky/ppp-blinky.cpp	Sat Sep 02 01:55:19 2017 +0000
+++ b/PPP-Blinky/ppp-blinky.cpp	Sat Sep 02 07:48:53 2017 +0000
@@ -173,6 +173,7 @@
     int ledState; // state of LED1
     int responseCounter;
     int firstFrame; // cleared after first frame
+    unsigned int sum; // a checksum used in headers
     struct {
 #define RXBUFLEN (1<<11)
         // the serial port receive buffer and packet buffer, size is RXBUFLEN (currently 2048 bytes)
@@ -485,28 +486,28 @@
 }
 
 /// perform a 16-bit checksum. if the byte count is odd, stuff in an extra zero byte.
-unsigned int dataCheckSum(unsigned char * ptr, int len)
+unsigned int dataCheckSum(unsigned char * ptr, int len, int restart)
 {
-    unsigned int i,hi,lo,sum;
+    unsigned int i,hi,lo;
     unsigned char placeHolder;
     if (len&1) {
         placeHolder = ptr[len];
         ptr[len]=0;  // if the byte count is odd, insert one extra zero byte is after the last real byte because we sum byte PAIRS
     }
-    sum=0;
+    if (restart) ppp.sum=0;
     i=0;
     while ( i<len ) {
         checkPc();
         hi = ptr[i++];
         lo = ptr[i++];
-        sum = sum + ( (hi<<8) | lo );
+        ppp.sum = ppp.sum + ((hi<<8)|lo);
     }
     if (len&1) {
         ptr[len] = placeHolder;    // restore the extra byte we made zero
     }
-    sum = (sum & 0xffff) + (sum>>16);
-    sum = (sum & 0xffff) + (sum>>16); // sum one more time to catch any carry from the carry
-    return ~sum;
+    ppp.sum = (ppp.sum & 0xffff) + (ppp.sum>>16);
+    ppp.sum = (ppp.sum & 0xffff) + (ppp.sum>>16); // sum one more time to catch any carry from the carry
+    return ~ppp.sum;
 }
 
 /// perform the checksum on an IP header
@@ -658,7 +659,7 @@
         icmpSum[0]=0;
         icmpSum[1]=0; // zero the checksum for recalculation
         int icmpLength = packetLength - headerSizeIP; // length of ICMP data portion
-        unsigned int sum = dataCheckSum( (unsigned char *)icmpType, icmpLength); // this checksum on icmp data portion
+        unsigned int sum = dataCheckSum( (unsigned char *)icmpType, icmpLength, 1); // this checksum on icmp data portion
         icmpSum[0]=(sum>>8)&0xff;
         icmpSum[1]=(sum   )&0xff; // new checksum for ICMP data portion
 
@@ -915,10 +916,8 @@
 {
     // IP header
     char * ipPkt = ppp.pkt.buf+4; // ip packet start
-    char * pktLen =     ipPkt+2;  // 2 bytes
     char * protocol =   ipPkt+9;  // 1 byte
-    char * srcAdr =     ipPkt+12; // 4 bytes
-
+    char protocolIP = protocol[0];
     int packetLengthIp = __REV16(ppp.ip->lengthR ); // size of ip packet
     int headerSizeIp = 4 * ppp.ip->headerLength;  // size of ip header
 
@@ -927,13 +926,9 @@
     ppp.tcpStart = ppp.ipStart + headerSizeIp; // calculate where the TCP header starts
     int tcpSize = packetLengthIp - headerSizeIp; // tcp size = size of ip payload
     char * tcp           = ppp.ipStart+headerSizeIp; // start of tcp packet
-    //char * flagbitstcp   = tcp + 12; // 9 bits
-    char * windowsizetcp = tcp + 14; // 2 bytes
-    char * checksumtcp   = tcp + 16; // 2 bytes
 
     int headerSizeTcp = 4 * (ppp.tcp->offset); // tcp "offset" for start of data is also the header size
 
-    int protocolIP = protocol[0];
     char * tcpDataIn = tcp + headerSizeTcp; // start of data block after TCP header
     int tcpDataSize = tcpSize - headerSizeTcp; // size of data block after TCP header
 
@@ -955,8 +950,8 @@
     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 flagsTCP = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; // the tcp flags we received
-    windowsizetcp[0] = (700 >> 8  );   // tcp window size hi byte
-    windowsizetcp[1] = (700 & 0xff);   // tcp window size lo byte
+    
+    ppp.tcp->windowR = __REV16( 700 ); // set tcp window size to 700 bytes
 
     // A sparse TCP flag interpreter that implements stateless TCP connections
 
@@ -996,37 +991,28 @@
     
     ppp.tcp->flag.All = flagsOut; // update the TCP flags
 
-    // increment our outgoing ip packet counter
-    //ppp.ipData.ident++; // get next ident number for our packet
-
-    // Now we recalculate all the header sizes
+    // recalculate all the header sizes
     tcpSize = headerSizeTcp + dataLen; // tcp packet size
     int newPacketSize = headerSizeIp + tcpSize; // calculate size of the outgoing packet
-    pktLen[0] = (newPacketSize>>8);
-    pktLen[1]=newPacketSize; // ip total packet size
+    ppp.ip->lengthR = __REV16 ( newPacketSize );
     ppp.pkt.len = newPacketSize+4+2; // ip packet length + 4-byte ppp prefix (ff 03 00 21) + 2 fcs (crc) bytes bytes at the end of the packet
 
     // the header is all set up, now do the IP and TCP checksums
     IpHeaderCheckSum(); // calculate new IP header checksum
 
-    // now we have to build the so-called 12-byte TCP "pseudo-header" in front of the TCP header (containing some IP header values) in order to correctly calculate the TCP checksum
+    // create the TCP "pseudo-header" (containing some IP header values) in order to correctly calculate the TCP checksum
     // this header  contains the most important parts of the IP header, i.e. source and destination address, protocol number and data length.
-    char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header
-    char * pseudoHeader = tcp-12; // mark the start of the TCP pseudo-header
-    memcpy(tempHold, pseudoHeader, 12); // preserve the 12 bytes of the IP header where the TCP pseudo-Header will be built
-    memcpy( pseudoHeader+0, srcAdr, 8); // IP source and destination addresses from IP header
-    memset( pseudoHeader+8, 0, 1); // reserved, set to zero
-    memset( pseudoHeader+9, protocolIP, 1); // protocol from IP header
-    memset( pseudoHeader+10, tcpSize>>8, 1); // size of IP data (TCP packet size)
-    memset( pseudoHeader+11, tcpSize, 1); // size of IP data (TCP packet size)
-    // pseudo-header built, now we can calculate TCP checksum
-    checksumtcp[0]=0;
-    checksumtcp[1]=0;
-    unsigned int pseudoHeaderSum=dataCheckSum((unsigned char *)pseudoHeader,tcpSize+12); // calculate the TCP checksum starting at the pseudo-header
-    checksumtcp[0]=pseudoHeaderSum>>8;
-    checksumtcp[1]=pseudoHeaderSum;
-    memcpy( tcp-12, tempHold, 12); // restore the 12 bytes that the pseudo-header overwrote
-    // IP and TCP checksums done
+    pseudoIpHeaderType pseudoHeader; // create pseudo header
+    pseudoHeader.srcAdrR = ppp.ip->srcAdrR; // copy in ip source address
+    pseudoHeader.dstAdrR = ppp.ip->dstAdrR; // copy in ip dest address
+    pseudoHeader.zero = 0; // padding
+    pseudoHeader.protocol = protocolIP; // protocol number
+    pseudoHeader.lengthR = __REV16( tcpSize ); // size of tcp packet
+    dataCheckSum(pseudoHeader.start, 12, 1); // start with the TCP checksum of the pseudoheader
+    ppp.tcp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared
+    unsigned int pseudoHeaderSum=dataCheckSum((unsigned char *)ppp.tcpStart,tcpSize, 0); // continue the TCP checksum on the whole TCP packet
+    ppp.tcp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header
+   
     dumpHeaderIP(1); // dump outgoing IP header
     dumpHeaderTCP(1); // dump outgoing TCP header
     for (int i=0; i<45*1000/10; i++) { // 45 ms delay before sending frame - a typical internet delay time
@@ -1038,7 +1024,6 @@
     memset(ppp.pkt.buf+44,0,500); // flush out traces of previous data that we may scan for
 }
 
-
 /// dump the TCP data to the debug serial port
 void dumpDataTCP()
 {