RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Revision:
31:e000c1b9c565
Parent:
30:273431bccb02
Child:
32:512228c29209
--- a/main.cpp	Thu Jan 05 02:00:04 2017 +0000
+++ b/main.cpp	Thu Jan 05 02:16:53 2017 +0000
@@ -50,7 +50,7 @@
     int online;
     int ident;
     int sync;
-    int seq;
+    unsigned int seq;
     int crc;
     int ledState;
     struct {
@@ -66,7 +66,7 @@
         char * buf;
     } pkt; // ppp buffer
 };
- 
+
 pppType ppp; // our global - definitely not thread safe
 
 // intitialize our globals
@@ -100,8 +100,8 @@
 
 int crcBuf(char * buf, int size) // crc on an entire block of memory
 {
-    crcReset();    
-    for(int i=0; i<size; i++)crcDo(*buf++); 
+    crcReset();
+    for(int i=0; i<size; i++)crcDo(*buf++);
     return ppp.crc;
 }
 
@@ -447,8 +447,8 @@
     char * seqtcp        = tcpStart + 4;  // 4 bytes
     char * acktcp        = tcpStart + 8;  // 4 bytes
     char * flagbitstcp   = tcpStart + 12; // 9 bits
-    int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
-    int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
+    unsigned int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
+    unsigned int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
     int flags = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1];
 
     char flagInfo[10];
@@ -497,8 +497,8 @@
     int tcpHeaderLen = ((offset[0]>>4)&0x0f)*4; // size of tcp header only
     int dataLen = tcpSize - tcpHeaderLen; // data is what's left after the header
 
-    int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
-    int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
+    unsigned int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
+    unsigned int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
 
     char * dataStart = s + tcpHeaderLen; // start of data
 
@@ -535,35 +535,45 @@
         ack = seq + 1; // when you see SYN you increment their sequence
         seq = ppp.seq-1; // we cheat - our sequence will be one higher hereafter
     } else if ( (flagsTCP & TCP_FLAG_PSH) != 0 ) { // they are pushing data
-    
-    // Ok, we are done with figuring out the flags. Now we start preparing to respond
-    
-    int temp = ack;
-    ack = seq + incomingLen; // acknowledge the number of bytes they sent by adding it to seq
-    ppp.seq = temp; // adopt their calculation of our sequence number
-    seq = ppp.seq;
-    // let's check incoming text for an HTTP home page GET request
-    if ( strncmp(dataStart, "GET / HTTP/1.1", 14) == 0) {
-        flagbitstcp[1] = TCP_FLAG_FIN; // close connection after delivering page
-        dataLen = 15*32; // this block has to hold the web page below, but keep it under 1k
-        memset(dataStart,'x', dataLen ); // initialize the block
-        int n=0; // number of bytes we have printed so far
-        n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: PPP-Blinky\r\n"); // http header
-        n=n+sprintf(n+dataStart,"Content-Length: 378\r\n"); // http header
-        n=n+sprintf(n+dataStart,"Content-Type: text/html;charset=us-ascii\r\n\r\n"); // http header
-        int nHeader=n; // byte total of all headers
-        n=n+sprintf(n+dataStart,"<html><head><title>mbed-PPP-Blinky</title><script>window.onload=function()"); // html
-        n=n+sprintf(n+dataStart,"{setInterval(function(){function x(){return document.getElementById('w');};"); // html
-        n=n+sprintf(n+dataStart,"x().innerText = parseInt(x().innerText)+1;},100);};</script></head><body>"); // html
-        n=n+sprintf(n+dataStart,"<h1>mbed-PPP-Blinky Up and Running</h1><h1 id=\"w\" style=\"text-align:"); // html
-        n=n+sprintf(n+dataStart," center\";>0</h1><h1><a href=\"http://bit.ly/pppBlinky\">Source on mbed</h1></body></html>"); // html
-        int contentLength = dataLen-nHeader; // this is how to calculate Content-Length, but using curl -v is easier
-        contentLength = contentLength+0; // get around unreferenced variable warning
-        if (v0) {
-            debug(("HTTP GET dataLen %d*32=%d Header %d Content-Length %d Total %d Margin %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n-1));
+
+        // Ok, we are done with figuring out the flags. Now we start preparing to respond
+
+        int temp = ack;
+        ack = seq + incomingLen; // acknowledge the number of bytes they sent by adding it to seq
+        ppp.seq = temp; // adopt their calculation of our sequence number
+        seq = ppp.seq;
+        
+        // let's check incoming text for an HTTP home page GET request
+        if ( strncmp(dataStart, "GET / HTTP/1.1", 14) == 0) {
+            flagbitstcp[1] = TCP_FLAG_FIN; // close connection after delivering page
+            dataLen = 15*32; // this block has to hold the web page below, but keep it under 1k
+            memset(dataStart,'x', dataLen ); // initialize the block
+            int n=0; // number of bytes we have printed so far
+            n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: PPP-Blinky\r\n"); // http header
+            n=n+sprintf(n+dataStart,"Content-Length: 378\r\n"); // http header
+            n=n+sprintf(n+dataStart,"Content-Type: text/html;charset=us-ascii\r\n\r\n"); // http header
+            int nHeader=n; // byte total of all headers
+            n=n+sprintf(n+dataStart,"<html><head><title>mbed-PPP-Blinky</title><script>window.onload=function()"); // html
+            n=n+sprintf(n+dataStart,"{setInterval(function(){function x(){return document.getElementById('w');};"); // html
+            n=n+sprintf(n+dataStart,"x().innerText = parseInt(x().innerText)+1;},100);};</script></head><body>"); // html
+            n=n+sprintf(n+dataStart,"<h1>mbed-PPP-Blinky Up and Running</h1><h1 id=\"w\" style=\"text-align:"); // html
+            n=n+sprintf(n+dataStart," center\";>0</h1><h1><a href=\"http://bit.ly/pppBlinky\">Source on mbed</h1></body></html>"); // html
+            int contentLength = dataLen-nHeader; // this is how to calculate Content-Length, but using curl -v is easier
+            contentLength = contentLength+0; // get around unreferenced variable warning
+            if (v0) {
+                debug(("HTTP GET dataLen %d*32=%d Header %d Content-Length %d Total %d Margin %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n-1));
+            }
+        } else if ( strncmp(dataStart, "GET /", 4) == 0) {
+            flagbitstcp[1] = TCP_FLAG_FIN; // close connection after delivering page
+            dataLen = 2*32; // this block has to hold the web page below, but keep it under 1k
+            memset(dataStart,'x', dataLen ); // initialize the block
+            int n=0; // number of bytes we have printed so far
+            n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\n\r\n\r\n\r\n"); // http header
+            if (v0) {
+                debug(("HTTP 404 Not Found\n"));
+            }
         }
     }
-}
 
     // now we have to recalculate all the header sizes