Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
165:c47826d07e0d
Parent:
164:c3de3d212c4b
Child:
166:0386c2d5dc89
--- a/PPP-Blinky/ppp-blinky.cpp	Sun Sep 03 18:20:37 2017 +0000
+++ b/PPP-Blinky/ppp-blinky.cpp	Sun Sep 03 19:19:03 2017 +0000
@@ -45,7 +45,7 @@
 // Using the second serial port will slow down packet response time
 // Note - the LPC11U24 does NOT have a second serial port
 
-#define SERIAL_PORT_MONITOR_NO /* change to SERIAL_PORT_MONITOR_YES for debug messages */
+#define SERIAL_PORT_MONITOR_YES /* change to SERIAL_PORT_MONITOR_YES for debug messages */
 
 // here we define the OPTIONAL, second debug serial port for various mbed target boards
 #ifdef SERIAL_PORT_MONITOR_YES
@@ -155,7 +155,7 @@
 // the standard hdlc frame start/end character. It's the tilde character "~"
 #define FRAME_7E (0x7e)
 
-/// a structure to keep all our ppp globals in
+/// a structure to keep all our ppp variables in
 struct pppType {
     pppHeaderType * ppp; // pointer to ppp struct
     union {
@@ -167,6 +167,8 @@
         char * tcpStart; // char pointer to tcp header struct (need a char pointer for byte offset calculations)
         udpHeaderType * udp; // pointer to udp header struct
         char * udpStart; // char pointer to udp header struct (need a char pointer for byte offset calculations)
+        icmpHeaderType * icmp; // pointer to udp header struct
+        char * icmpStart; // char pointer to icmp header struct (need a char pointer for byte offset calculations)
     };
     int online; // we hunt for a PPP connection if this is zero
     int hostIP; // ip address of host
@@ -551,7 +553,7 @@
     } ipLength;
 
     struct {
-        unsigned int all; // lenght of entire udp packet
+        unsigned int all; // length of entire udp packet
         unsigned int data; // length of udp data segment
     } udpLength;
 
@@ -613,28 +615,39 @@
 
         IpHeaderCheckSum();  // refresh IP header checksum
         ppp.udp->checkSumR = 0; // we don't yet recompute the UDP checksum (it's optional), so have to zero it
+        ppp.responseCounter++; // count UDP packet as a response
         sendPppFrame(); // send the UDP message back
+    } else {
+        // do nothing with other incoming UDP packets
     }
 }
 
 /// handle a PING ICMP (internet control message protocol) packet
 void ICMPpacket()   // internet control message protocol
 {
-    char * ipPkt = ppp.pkt.buf+4; // ip packet start
-    char * pktLen = ipPkt+2;
-    int packetLength = (pktLen[0]<<8) | pktLen[1]; // icmp packet length
-    int headerSizeIP = (( ipPkt[0]&0xf)*4);
-    char * icmpType = ipPkt + headerSizeIP; // icmp data start
-    char * icmpSum = icmpType+2; // icmp checksum
+    struct {
+        unsigned int all; // length of entire ip packet
+        unsigned int header; // length of ip header
+    } ipLength;
+    struct {
+        unsigned int all; // length of entire udp packet
+        unsigned int data; // length of udp data segment
+    } icmpLength;
+
+    ipLength.all = __REV16( ppp.ip->lengthR );  // length of ip packet
+    ipLength.header = 4 * ppp.ip->headerLength; // length of ip header
+    ppp.icmpStart = ppp.ipStart + ipLength.header; // calculate start of udp header
+    icmpLength.all = ipLength.all - ipLength.header; // length of icmp packet
+    icmpLength.data = icmpLength.all - 8; // length of icmp data
+    
 #define ICMP_TYPE_PING_REQUEST 8
-    if ( icmpType[0] == ICMP_TYPE_PING_REQUEST ) {
-        char * ipTTL = ipPkt+8; // time to live
-        ipTTL[0]--; // decrement time to live (so we have to update header checksum)
+    if ( ppp.icmp->type == ICMP_TYPE_PING_REQUEST ) {
+        ppp.ip->ttl--; // decrement time to live (so we have to update header checksum)
 #ifdef SERIAL_PORT_MONITOR_YES
-        char * srcAdr = ipPkt+12;
-        char * dstAdr = ipPkt+16;
-        int icmpIdent = (icmpType[4]<<8)|icmpType[5];
-        int icmpSequence = (icmpType[6]<<8)|icmpType[7];
+        char * srcAdr = (char *) &ppp.ip->srcAdrR;
+        char * dstAdr = (char *) &ppp.ip->dstAdrR;
+        int icmpIdent = __REV16( ppp.ip->identR ); // byte reversed - big endian
+        int icmpSequence = __REV16( ppp.icmp->sequenceR ); // byte reversed - big endian
         if(1) {
             char pbuf[50];
             checkPc();
@@ -649,20 +662,16 @@
         swapIpAddresses(); // swap the IP source and destination addresses
         IpHeaderCheckSum();  // new ip header checksum (required because we changed TTL)
 #define ICMP_TYPE_ECHO_REPLY 0
-        icmpType[0]=ICMP_TYPE_ECHO_REPLY; // icmp echo reply
-        icmpSum[0]=0;
-        icmpSum[1]=0; // zero the checksum for recalculation
-        int icmpLength = packetLength - headerSizeIP; // length of 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
+        ppp.icmp->type = ICMP_TYPE_ECHO_REPLY; // icmp echo reply
+        ppp.icmp->checkSumR = 0; // zero the checksum for recalculation
+        unsigned int sum = dataCheckSum(ppp.icmpStart, icmpLength.all, 1); // icmp checksum
+        ppp.icmp->checkSumR = __REV16( sum ); // save big-endian icmp checksum
 
-        int printSize = icmpLength-8; // exclude size of icmp header
-        char * icmpData = icmpType+8; // the actual payload data is after the header
+        int printSize = icmpLength.data; // exclude size of icmp header
         if (printSize > 10) printSize = 10; // print up to 20 characters
         if (0) {
             for (int i=0; i<printSize; i++) {
-                char ch = icmpData[i];
+                char ch = ppp.icmp->data[i];
                 if (ch>31 && ch<127) {
                     putcWhileCheckingInput(ch);
                 } else {
@@ -671,10 +680,11 @@
             }
             putcWhileCheckingInput('\n');
         }
+        ppp.responseCounter++; // count a ping as a response
         sendPppFrame(); // reply to the ping
     } else {
         if (v0) {
-            debugPrintf("ICMP type=%d \n", icmpType[0]);
+            debugPrintf("ICMP type=%x \n", ppp.icmp->type);
         }
     }
 }
@@ -857,7 +867,7 @@
             n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.responseCounter); // body = the http frame count
 #else
             if( httpGet6 == 'b' ) { // if the fetched page is "xb" send a meta command to let the browser continuously reload
-                n=n+sprintf(n+dataStart, "<meta http-equiv=\"refresh\" content=\"0\">"); // reload loop - handy for benchmarking
+                n=n+sprintf(n+dataStart, "<title>%d</title><meta http-equiv=\"refresh\" content=\"0\">",ppp.responseCounter); // reload loop - handy for benchmarking
             }
             // /x is a very short page, in fact, it is only a decimal number showing the http Page count
             n=n+sprintf(n+dataStart,"%d ",ppp.responseCounter); // not really valid html but most browsers and curl are ok with it