Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Files at this revision

API Documentation at this revision

Comitter:
nixnax
Date:
Sat Sep 02 17:58:39 2017 +0000
Parent:
160:bd701ad564cb
Child:
162:594729c0e5c1
Commit message:
Checksum cleanup.

Changed in this revision

PPP-Blinky/ppp-blinky.cpp Show annotated file Show diff for this revision Revisions of this file
PPP-Blinky/ppp-blinky.h Show annotated file Show diff for this revision Revisions of this file
--- a/PPP-Blinky/ppp-blinky.cpp	Sat Sep 02 17:35:29 2017 +0000
+++ b/PPP-Blinky/ppp-blinky.cpp	Sat Sep 02 17:58:39 2017 +0000
@@ -486,15 +486,15 @@
 }
 
 /// 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, int restart)
+unsigned int dataCheckSum(char * ptr, int len, int restart)
 {
     unsigned int i,hi,lo;
     unsigned char placeHolder;
+    if (restart) ppp.sum=0;
     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
     }
-    if (restart) ppp.sum=0;
     i=0;
     while ( i<len ) {
         checkPc();
@@ -513,24 +513,10 @@
 /// perform the checksum on an IP header
 void IpHeaderCheckSum()
 {
-    ppp.pkt.buf[4+10]=0; // zero the checsum in the IP header
-    ppp.pkt.buf[4+11]=0; // zero the checksum in the IP header
-    int len =(ppp.pkt.buf[4]&0xf)*4; // length of IP header in bytes
-    char * ptr = ppp.pkt.buf+4; // start of IP packet
-    int sum=0;
-    for (int i=0; i<len/2; i++) {
-        int hi = *ptr;
-        ptr++;
-        int lo = *ptr;
-        ptr++;
-        int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 );
-        sum = sum + val;
-        checkPc();
-    }
-    sum = sum + (sum>>16);
-    sum = ~sum;
-    ppp.pkt.buf[4+10]= (sum>>8);
-    ppp.pkt.buf[4+11]= (sum   );
+    ppp.ip->checksumR=0; // zero the checsum in the IP header
+    int len = 4 * ppp.ip->headerLength; // length of IP header in bytes
+    unsigned int sum = dataCheckSum(ppp.ipStart,len,1);
+    ppp.ip->checksumR = __REV16( sum ); // insert fresh checksum
 }
 
 /// swap the IP source and destination addresses
@@ -659,7 +645,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, 1); // this checksum on icmp data portion
+        unsigned int sum = dataCheckSum(icmpType, icmpLength, 1); // this checksum on icmp data portion
         icmpSum[0]=(sum>>8)&0xff;
         icmpSum[1]=(sum   )&0xff; // new checksum for ICMP data portion
 
@@ -1002,7 +988,7 @@
     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
+    unsigned int pseudoHeaderSum=dataCheckSum(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
--- a/PPP-Blinky/ppp-blinky.h	Sat Sep 02 17:35:29 2017 +0000
+++ b/PPP-Blinky/ppp-blinky.h	Sat Sep 02 17:58:39 2017 +0000
@@ -35,7 +35,7 @@
     
     unsigned int ttl            :  8;
     unsigned int protocol       :  8;  // next protocol
-    unsigned int checksumR      : 16;
+    unsigned int checksumR      : 16;  // ip checksum, byte reversed
     
     unsigned int srcAdrR; // source IP address
     unsigned int dstAdrR; // destination IP address
@@ -44,7 +44,7 @@
 /// IP pseudoheader
 typedef struct {
     union {
-        unsigned char start [0];
+        char start [0];
         unsigned int srcAdrR; // source IP address
     };
     unsigned int dstAdrR; // destination IP address