Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
95:40af49390daf
Parent:
94:8ee3eec2a2bb
Child:
96:e14f42ecff66
--- a/main.cpp	Mon Jul 24 23:02:53 2017 +0000
+++ b/main.cpp	Fri Jul 28 23:04:30 2017 +0000
@@ -19,7 +19,7 @@
 // https://ttssh2.osdn.jp/index.html.en - Tera Term, a good terminal program to monitor the debug output from the second serial port with!
 // https://www.microsoft.com/en-us/download/details.aspx?id=4865 - Microsoft network monitor - real-time monitoring of PPP packets -
 // http://pingtester.net/ - nice tool for high rate ping testing
-// http://www.sunshine2k.de/coding/javascript/crc/crc_js.html - Correctly calculates the 16-bit FCS (crc) on our frames (Choose CRC16_CCITT_FALSE)
+// http://www.sunshine2k.de/coding/javascript/crc/crc_js.html - Correctly calculates the 16-bit FCS (crc) on our frames (Choose CRC16_CCITT_FALSE), then custom relected-in=1, reflected-out=1
 // https://technet.microsoft.com/en-us/sysinternals/pstools.aspx - psping for fast testing of ICMP ping function
 // https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky
 // Windows Powershell invoke-webrequest command - use it to stress test the webserver like this:  while (1){ invoke-webrequest -uri 172.10.10.1/x }
@@ -109,7 +109,7 @@
         char buf[RXBUFLEN]; // RXBUFLEN MUST be a power of two because we use & operator for fast wrap-around in ring buffer
         int head;
         int tail;
-        int rtail; 
+        int rtail;
         int buflevel;
     } rx; // serial port objects
     struct {
@@ -200,6 +200,15 @@
     return x;
 }
 
+// Note - the hex output of dumpPPPFrame() can be imported into WireShark
+// Capture the frame's hex output in your terminal program and save as a text file
+// In WireShark, use "Import Hex File". Options are: Offset=None, Protocol=PPP.
+void dumpPPPFrame()
+{
+    for(int i=0; i<ppp.pkt.len; i++) debug("%02x ", ppp.pkt.buf[i]);
+    debug(" CRC=%04x Len=%d\n", ppp.pkt.crc, ppp.pkt.len);
+}
+
 void processHDLCFrame(int start, int end)   // process received frame
 {
     led1Toggle(); // change led1 state on every frame we receive
@@ -235,19 +244,12 @@
         void determinePacketType(); // declaration only
         determinePacketType();
     } else {
-        debug("PPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // ignore packets with CRC errors but print a debug line
+        if (0) { // don't normally report FCS because windows early-terminates packets, and early-terminated packets all have FCS errors.
+            debug("\nPPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // ignore packets with CRC errors but print a debug line
+        }
     }
 }
 
-// Note - the hex output of dumpFrame() can be imported into WireShark
-// Capture the frame's hex output in your terminal program and save as a text file
-// In WireShark, use "Import Hex File". Options are: Offset=None, Protocol=PPP.
-void dumpFrame()
-{
-    for(int i=0; i<ppp.pkt.len; i++) debug("%02x ", ppp.pkt.buf[i]);
-    debug(" C=%02x %02x L=%d\n", ppp.pkt.crc&0xff, (ppp.pkt.crc>>8)&0xff, ppp.pkt.len);
-}
-
 void hdlcPut(int ch)   // do hdlc handling of special (flag) characters
 {
     if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) ) {
@@ -264,9 +266,9 @@
     ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc)
     ppp.pkt.buf[ ppp.pkt.len-1 ] = (~crc>>8); // fcs hi (crc)
     pc.putc(0x7e); // hdlc start-of-frame "flag"
-    for(int i=0; i<ppp.pkt.len; i++) { 
+    for(int i=0; i<ppp.pkt.len; i++) {
         hdlcPut( ppp.pkt.buf[i] ); // send a character
-    
+
         if((i&0x7f)==0) fillbuf(); // handle received characters very every 128 sent
     }
     pc.putc(0x7e); // hdlc end-of-frame "flag"
@@ -472,41 +474,43 @@
 
 void dumpHeaderIP ()
 {
-    char * ipPkt = ppp.pkt.buf+4; // ip packet start
+    if (v0) {
+        char * ipPkt = ppp.pkt.buf+4; // ip packet start
 #ifndef SERIAL_PORT_MONITOR_NO
-    char * version =    ipPkt; // top 4 bits
-    char * ihl =        ipPkt; // bottom 4 bits
-    char * dscp =       ipPkt+1; // top 6 bits
-    char * ecn =        ipPkt+1; // lower 2 bits
-    char * pktLen =     ipPkt+2; // 2 bytes
-    char * ident =      ipPkt+4; // 2 bytes
-    char * flags =      ipPkt+6; // 2 bits
-    char * ttl =        ipPkt+8; // 1 byte
-    char * protocol =   ipPkt+9; // 1 byte
-    char * headercheck= ipPkt+10; // 2 bytes
+        char * version =    ipPkt; // top 4 bits
+        char * ihl =        ipPkt; // bottom 4 bits
+        char * dscp =       ipPkt+1; // top 6 bits
+        char * ecn =        ipPkt+1; // lower 2 bits
+        char * pktLen =     ipPkt+2; // 2 bytes
+        char * ident =      ipPkt+4; // 2 bytes
+        char * flags =      ipPkt+6; // 2 bits
+        char * ttl =        ipPkt+8; // 1 byte
+        char * protocol =   ipPkt+9; // 1 byte
+        char * headercheck= ipPkt+10; // 2 bytes
 #endif
-    char * srcAdr =     ipPkt+12; // 4 bytes
-    char * dstAdr =     ipPkt+16; // 4 bytes = total of 20 bytes
+        char * srcAdr =     ipPkt+12; // 4 bytes
+        char * dstAdr =     ipPkt+16; // 4 bytes = total of 20 bytes
 
 #ifndef SERIAL_PORT_MONITOR_NO
-    int versionIP = (version[0]>>4)&0xf;
-    int headerSizeIP = (ihl[0]&0xf)*4;
-    int dscpIP = (dscp[0]>>2)&0x3f;
-    int ecnIP = ecn[0]&3;
-    int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length
-    int identIP = (ident[0]<<8)|ident[1];
-    int flagsIP = flags[0]>>14&3;
-    int ttlIP = ttl[0];
-    int protocolIP = protocol[0];
-    unsigned int checksumIP = (headercheck[0]<<8)|headercheck[1];
+        int versionIP = (version[0]>>4)&0xf;
+        int headerSizeIP = (ihl[0]&0xf)*4;
+        int dscpIP = (dscp[0]>>2)&0x3f;
+        int ecnIP = ecn[0]&3;
+        int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length
+        int identIP = (ident[0]<<8)|ident[1];
+        int flagsIP = flags[0]>>14&3;
+        int ttlIP = ttl[0];
+        int protocolIP = protocol[0];
+        unsigned int checksumIP = (headercheck[0]<<8)|headercheck[1];
 #endif
 
-    char srcIP [16];
-    snprintf(srcIP,16, "%d.%d.%d.%d", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3]);
-    char dstIP [16];
-    snprintf(dstIP,16, "%d.%d.%d.%d", dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]);
-    if (v0) debug("IP %s %s v%d h%d d%d e%d L%03d ",srcIP,dstIP,versionIP,headerSizeIP,dscpIP,ecnIP,packetLength);
-    if (v0) debug("i%04x f%d t%d p%d C%04x\n",identIP,flagsIP,ttlIP,protocolIP,checksumIP);
+        char srcIP [16];
+        snprintf(srcIP,16, "%d.%d.%d.%d", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3]);
+        char dstIP [16];
+        snprintf(dstIP,16, "%d.%d.%d.%d", dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]);
+        debug("IP %s %s v%d h%d d%d e%d L%03d ",srcIP,dstIP,versionIP,headerSizeIP,dscpIP,ecnIP,packetLength);
+        debug("i%04x f%d t%d p%d C%04x\n",identIP,flagsIP,ttlIP,protocolIP,checksumIP);
+    }
 }
 
 void dumpHeaderTCP()
@@ -579,7 +583,7 @@
             n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed-ppp-blinky</title>"); // html title (W3C.org required element)
             n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.httpFrameCount); // body = the http frame count
 #else
-            n=n+sprintf(n+dataStart,"%d",ppp.httpFrameCount);//ppp.httpFrameCount); // not valid html but fast, most browsers and curl are ok with it
+            n=n+sprintf(n+dataStart,"%d",ppp.httpFrameCount); // not valid html but fast, most browsers and curl are ok with it
 #endif
 
         } else {
@@ -698,9 +702,9 @@
     } // switch
 
     // The TCP flag handling is now done
-    
+
     // first we swap sourc and destination TCP addresses and insert the new ack and seq numbers
-    
+
     char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header
     memcpy(tempHold, srcAdr,4);
     memcpy(srcAdr, dstAdr,4);
@@ -709,7 +713,7 @@
     memcpy(tempHold, srctcp,2);
     memcpy(srctcp, dsttcp,2);
     memcpy(dsttcp, tempHold,2); // swap ip port source/dest
-    
+
 sendTCP:
 
     acktcp[0]=ack_out>>24;
@@ -762,7 +766,7 @@
     checksumtcp[0]=pseudoHeaderSum>>8;
     checksumtcp[1]=pseudoHeaderSum;
     memcpy( tcp-12, tempHold, 12); // restore the 12 bytes that the pseudo-header overwrote
-    
+
     send_pppFrame(); // All preparation complete - send the TCP response
 
     if (doFin==1) { // they want to shut down the link, so we have to send another packet to close our side of the link
@@ -770,9 +774,9 @@
         flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // tell them we are also finished
         goto sendTCP; // send our final packet for this conversation.
     }
-    
+
     dumpHeaderIP();
-    //dumpHeaderTCP();
+    dumpHeaderTCP();
 
 }
 
@@ -861,7 +865,7 @@
 void LCPother()
 {
     debug("LCP Other\n");
-    dumpFrame();
+    dumpPPPFrame();
 }
 
 void LCPframe()