Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
154:18b2bd92f557
Parent:
153:7993def8663f
Child:
155:9c6a1d249e26
--- a/PPP-Blinky/ppp-blinky.cpp	Fri Sep 01 15:44:06 2017 +0000
+++ b/PPP-Blinky/ppp-blinky.cpp	Fri Sep 01 20:23: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_YES /* change to SERIAL_PORT_MONITOR_YES for debug messages */
+#define SERIAL_PORT_MONITOR_NO /* 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
@@ -158,6 +158,15 @@
 
 /// a structure to keep all our ppp globals in
 struct pppType {
+    pppHeaderType * ppp; // pointer to ppp struct
+    union {
+        ipHeaderType * ip; // pointer to ip header struct
+        char * ipStart; // char pointer to ip header struct
+    };
+    union {
+        tcpHeaderType * tcp; // pointer to tcp header struct
+        char * tcpStart; // char pointer to tcp header struct
+    };         
     int online; // we hunt for a PPP connection if this is zero
     int hostIP; // ip address of host
     int fcs; // PPP "frame check sequence" - a 16-bit HDLC-like checksum used in all PPP frames
@@ -178,12 +187,7 @@
         int crc; // PPP CRC (frame check)
 #define PPP_max_size 1600
         // we are assuming 100 bytes more than MTU size of 1500
-        union {
-            char buf[PPP_max_size]; // send and receive buffer large enough for largest IP packet
-            ipHeaderType ip;
-            tcpHeaderType1 tcp1;
-            tcpHeaderType2 tcp2;
-        };
+        char buf[PPP_max_size]; // send and receive buffer large enough for largest IP packet
     } pkt; // ppp buffer objects
     struct {
         int frameStartIndex; // frame start marker
@@ -191,7 +195,7 @@
     } hdlc; // hdlc frame objects
     struct {
         unsigned int ident; // our IP ident value (outgoing frame count)
-    } ip; // ip related object
+    } ipData; // ip related object
 };
 
 pppType ppp; // our global - definitely not thread safe
@@ -206,11 +210,13 @@
     ppp.rx.head=0;
     ppp.rx.buflevel=0;
     ppp.pkt.len=0;
-    ppp.ip.ident=10000; // easy to recognize in ip packet dumps
+    ppp.ipData.ident=10000; // easy to recognize in ip packet dumps
     ppp.ledState=0;
     ppp.hdlc.frameStartIndex=0;
     ppp.responseCounter=0;
     ppp.firstFrame=1;
+    ppp.ppp = (pppHeaderType *)ppp.pkt.buf;
+    ppp.ip = (ipHeaderType *)(ppp.pkt.buf+4); // ppp header is 4 bytes long
 }
 
 /// Toggle the LED on every second PPP packet received
@@ -720,8 +726,8 @@
     n=n+sprintf(pbuf+n, "%05d ",IPv4Id); // IPv4Id is a good way to correlate our dumps with net monitor or wireshark traces
 #define DUMP_FULL_IP_ADDRESS_YES
 #ifdef DUMP_FULL_IP_ADDRESS_YES
-    char * srcAdr =     (char *)&ppp.pkt.ip.srcAdr; // 4 bytes
-    char * dstAdr =     (char *)&ppp.pkt.ip.dstAdr; // 4 bytes = total of 20 bytes
+    char * srcAdr =     (char *)&ppp.ip->srcAdrR; // 4 bytes
+    char * dstAdr =     (char *)&ppp.ip->dstAdrR; // 4 bytes = total of 20 bytes
     //n=n+sprintf(pbuf+n, " %x %x ", ppp.pkt.ip.x, ppp.pkt.ip.y);
     n=n+sprintf(pbuf+n, " %d.%d.%d.%d %d.%d.%d.%d ",srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3], dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]); // full ip addresses
 #endif
@@ -948,43 +954,50 @@
     char * pktLen =     ipPkt+2;  // 2 bytes
     char * protocol =   ipPkt+9;  // 1 byte
     char * srcAdr =     ipPkt+12; // 4 bytes
-    int headerSizeIP = 4 * ppp.pkt.ip.headerLength;
-    int packetLength = __REV16(ppp.pkt.ip.length );//(pktLen[0]<<8)|pktLen[1]; // ip total packet length
-    int tcpSize = packetLength - headerSizeIP;
+
+    int packetLengthIp = __REV16(ppp.ip->lengthR ); // size of ip packet
+    int headerSizeIp = 4 * ppp.ip->headerLength;  // size of ip header
 
     // TCP header
-    char * tcp           = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet
-    char * seqtcp        = tcp + 4;  // 4 bytes
-    char * acktcp        = tcp + 8;  // 4 bytes
-    char * offset        = tcp + 12; // 4 bits
+
+    int tcpSize = packetLengthIp - headerSizeIp; // tcp size = size of ip payload
+
+    // calculate where the TCP header starts
+    ppp.tcp = (tcpHeaderType *)(ppp.ipStart+headerSizeIp);
+
+    char * tcp           = ppp.ipStart+headerSizeIp; // start of tcp packet
+    //char * seqtcp        = tcp + 4;  // 4 bytes
+    //char * acktcp        = tcp + 8;  // 4 bytes
+    //char * offset        = tcp + 12; // 4 bits
     char * flagbitstcp   = tcp + 12; // 9 bits
     char * windowsizetcp = tcp + 14; // 2 bytes
     char * checksumtcp   = tcp + 16; // 2 bytes
 
-    int shortIP = ppp.pkt.ip.headerLength==5 ? 1 : 0; // ip headers can be 20 or 24 bytes long
-
-    int headerSizeTCP = shortIP ? 4 * ppp.pkt.tcp1.offset : 4 * ppp.pkt.tcp2.offset;
+    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
-    char * tcpDataOut = tcp + 20; // start of outgoing data
-    unsigned int seq_in = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
-    unsigned int ack_in = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
+    char * tcpDataIn = tcp + headerSizeTcp; // start of data block after TCP header
+    int tcpDataSize = tcpSize - headerSizeTcp; // size of data block after TCP header
+
+    unsigned int seq_in = __REV(ppp.tcp->seqTcpR);
+    unsigned int ack_in = __REV(ppp.tcp->ackTcpR);
     unsigned int ack_out = seq_in + tcpDataSize;
     unsigned int seq_out = ack_in; // use their version of our current sequence number
 
     // first we shorten the TCP response header to only 20 bytes. This means we ignore all TCP option requests
     tcpSize = 20; // shorten total TCP packet size to 20 bytes (no data)
-    headerSizeTCP = 20; // shorten outgoing TCP header size 20 bytes
-    ipPkt[0] = 0x45; // short IP packet size to 45
-    offset[0] =  (headerSizeTCP/4)<<4; // shorten tcp header size to 20 bytes
-    packetLength = 40; // shorten total packet size to 40 bytes (20 ip + 20 tcp)
-    
-    
+    headerSizeTcp = 20; // shorten outgoing TCP header size 20 bytes
+    //ipPkt[0] = 0x45; // short IP packet size to 45
+    ppp.ip->headerLength = 5; // ip header is 20 bytes long
+    //offset[0] =  (headerSizeTcp/4)<<4; // shorten tcp header size to 20 bytes
+    ppp.tcp->offset = (headerSizeTcp/4);
+    //packetLengthIp = 40; // shorten total packet size to 40 bytes (20 ip + 20 tcp)
+    ppp.ip->lengthR = __REV(40);
+    char * tcpDataOut = tcp + 20; // start of outgoing data
+
     //pktLen[1] = 40; // set total packet size to 40 bytes (20 ip + 20 tcp)
     //pktLen[0] =  0; // set total packet size to 40 bytes (20 ip + 20 tcp)
-    ppp.pkt.ip.length = __REV16(40);
+    ppp.ip->lengthR = __REV16(40);
 
     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
@@ -1025,23 +1038,17 @@
     swapIpAddresses(); // swap IP source and destination addresses
     swapIpPorts(); // swap IP  source and destination ports
 
-    acktcp[0]=ack_out>>24;
-    acktcp[1]=ack_out>>16;
-    acktcp[2]=ack_out>>8;
-    acktcp[3]=ack_out>>0; // save ack 32-bit integer
-    seqtcp[0]=seq_out>>24;
-    seqtcp[1]=seq_out>>16;
-    seqtcp[2]=seq_out>>8;
-    seqtcp[3]=seq_out>>0; // save seq 32-bit integer
-
+    ppp.tcp->ackTcpR = __REV( ack_out );
+    ppp.tcp->seqTcpR = __REV( seq_out );
+    
     flagbitstcp[1] = flagsOut; // update the TCP flags
 
     // increment our outgoing ip packet counter
-    ppp.ip.ident++; // get next ident number for our packet
+    //ppp.ipData.ident++; // get next ident number for our packet
 
     // Now we recalculate all the header sizes
-    tcpSize = headerSizeTCP + dataLen; // tcp packet size
-    int newPacketSize = headerSizeIP + tcpSize; // calculate size of the outgoing packet
+    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.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
@@ -1084,18 +1091,18 @@
 {
     int ipPktLen     = (ppp.pkt.buf[6]<<8)|ppp.pkt.buf[7]; // overall length of ip packet
     int ipHeaderLen  = (ppp.pkt.buf[4]&0xf)*4; // length of ip header
-    int headerSizeTCP = ((ppp.pkt.buf[4+ipHeaderLen+12]>>4)&0xf)*4;; // length of tcp header
-    int dataLen = ipPktLen - ipHeaderLen - headerSizeTCP; // data is what's left after the two headers
+    int headerSizeTcp = ((ppp.pkt.buf[4+ipHeaderLen+12]>>4)&0xf)*4;; // length of tcp header
+    int dataLen = ipPktLen - ipHeaderLen - headerSizeTcp; // data is what's left after the two headers
     if (v1) {
         char pbuf[50]; // local print buffer
         checkPc();
-        sprintf(pbuf, "TCP %d ipHeader %d tcpHeader %d Data %d\n", ipPktLen, ipHeaderLen, headerSizeTCP, dataLen);    // 1 for more verbose
+        sprintf(pbuf, "TCP %d ipHeader %d tcpHeader %d Data %d\n", ipPktLen, ipHeaderLen, headerSizeTcp, dataLen);    // 1 for more verbose
         checkPc();
         putsWhileCheckingInput( pbuf );
     }
     if (dataLen > 0) {
-        ppp.pkt.buf[4+ipHeaderLen+headerSizeTCP+dataLen]=0; // insert a null after the data so debug printf stops printing after the data
-        putsWhileCheckingInput( ppp.pkt.buf+4+ipHeaderLen+headerSizeTCP );    // print the tcp payload data
+        ppp.pkt.buf[4+ipHeaderLen+headerSizeTcp+dataLen]=0; // insert a null after the data so debug printf stops printing after the data
+        putsWhileCheckingInput( ppp.pkt.buf+4+ipHeaderLen+headerSizeTcp );    // print the tcp payload data
         putsWhileCheckingInput("\n");
     }
 }