Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of webserverBlinky by
Diff: main.cpp
- Revision:
- 35:e7068df4d971
- Parent:
- 34:8a6fbc73a7f1
- Child:
- 36:2a9b457f8276
diff -r 8a6fbc73a7f1 -r e7068df4d971 main.cpp --- a/main.cpp Thu Jan 05 04:14:35 2017 +0000 +++ b/main.cpp Thu Jan 05 08:16:50 2017 +0000 @@ -81,7 +81,7 @@ ppp.pkt.len=0; ppp.ident=0; ppp.sync=0; - ppp.seq=77; + ppp.seq=1000; ppp.ledState=0; } @@ -494,15 +494,15 @@ char * checksumtcp = s + 16; // 2 bytes int tcpSize = packetLength - headerSizeIP; - 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 headerSizeTCP = ((offset[0]>>4)&0x0f)*4; // size of tcp header only 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 + int flagsTCP = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; - int flagsTCP = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; + char * dataStart = ppp.pkt.buf + 4 + headerSizeIP + headerSizeTCP; // start of data block after TCP header + int incomingLen = tcpSize - headerSizeTCP; // size of data block after TCP header #define TCP_FLAG_ACK (1<<4) #define TCP_FLAG_SYN (1<<1) @@ -512,50 +512,26 @@ // A sparse TCP flag interpreter that implements basic TCP connections from a single source - int incomingLen = dataLen; // remember length of incoming packet - dataLen = 0; // most of our responses will have zero TCP data, only a header - - if ( ((flagsTCP & TCP_FLAG_FIN) != 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) { - if (ack <= ppp.seq) return; // their idea of our sequence is larger - int temp = ack; // remember what they thought - ack = seq + incomingLen; // ack their sequence - ppp.seq = temp; // adopt their calculation of our sequence - } else if ( ((flagsTCP & ~TCP_FLAG_ACK) == 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) { - if (incomingLen > 0) { // they sent data in the ack - ack = seq + incomingLen; // acknowledge the number of bytes they sent - seq = ppp.seq; // confirm our current sequence position - } else { - if (ack <= ppp.seq) return; // their idea of our sequence is larger - int temp = ack; // remember what they thought - ack = seq; // ack their sequence - ppp.seq = temp; // adopt their calculation of our sequence - seq = ppp.seq; - } + int dataLen = 0; // most of our responses will have zero TCP data, only a header + int teardown = 0; // see if we must tear down the link + + if ( ((flagsTCP & ~TCP_FLAG_ACK) == 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) { + if (incomingLen == 0) return; // no new data } else if ( (flagsTCP & TCP_FLAG_FIN) != 0 ) { // got FIN - flagbitstcp[1] |= TCP_FLAG_ACK; // do a fin-ack - ack = seq; - seq = ppp.seq; + flagbitstcp[1] = TCP_FLAG_ACK; // do an ack + teardown=1; } else if ( (flagsTCP & TCP_FLAG_SYN) != 0 ) { // got SYN flagbitstcp[1] |= TCP_FLAG_ACK; // do a syn-ack - ack = seq + 1; // when you see SYN you increment their sequence - seq = ppp.seq-1; // we cheat - our sequence will be one higher hereafter + seq++; } 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 + // It's a push - let's check the data for an HTTP home page GET request if ( strncmp(dataStart, "GET / HTTP/1.1", 14) == 0) { - flagbitstcp[1] = TCP_FLAG_ACK | TCP_FLAG_FIN; // close connection after delivering page + flagbitstcp[1] = TCP_FLAG_ACK; // | 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 + int n=1; // 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: 377\r\n"); // http header + n=n+sprintf(n+dataStart,"Content-Length: 376\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 @@ -566,10 +542,10 @@ 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)); + 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)); } } else if ( strncmp(dataStart, "GET /", 4) == 0) { // all other files get 404 not found - flagbitstcp[1] = TCP_FLAG_ACK | TCP_FLAG_FIN; // close connection after delivering page + flagbitstcp[1] = TCP_FLAG_ACK;// | TCP_FLAG_FIN; // close connection after delivering page dataLen = 5*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; @@ -582,18 +558,28 @@ 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)); + 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)); } + } else { + flagbitstcp[1] = TCP_FLAG_ACK; // we have nothing to say, just ack + dataLen=0; } } - // now we have to recalculate all the header sizes + // All the flag checking is now donw + + // Now we have to recalculate all the header sizes - int newPacketSize = headerSizeIP + tcpHeaderLen + dataLen; + ack = seq + incomingLen; // acknowledge the number of bytes they sent by adding it to seq + seq = ppp.seq; + +teardownEntry: + + int newPacketSize = headerSizeIP + headerSizeTCP + dataLen; pktLen[0] = (newPacketSize>>8); pktLen[1]=newPacketSize; // ip total packet size ppp.pkt.len = newPacketSize+6; // ppp packet length - tcpSize = tcpHeaderLen + dataLen; // tcp packet size + tcpSize = headerSizeTCP + dataLen; // tcp packet size // redo all the header stuff @@ -639,19 +625,26 @@ checksumtcp[1]=sum; memcpy( s-12, temp, 12); // overwrite the pseudoheader sendFrame(); // return the TCP packet + ppp.seq = ppp.seq + dataLen; // increase OUR sequence by outgoing data length - for the next round + if (teardown) { + teardown=0; // clear the teardown flag + flagbitstcp[1] = TCP_FLAG_FIN; // we want to end the link + dataLen=0; // no data needed + goto teardownEntry; + } } void dumpDataTCP() { 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 tcpHeaderLen = ((ppp.pkt.buf[4+ipHeaderLen+12]>>4)&0xf)*4;; // length of tcp header - int dataLen = ipPktLen - ipHeaderLen - tcpHeaderLen; // 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) { - debug(("TCP %d ipHeader %d tcpHeader %d Data %d\n", ipPktLen, ipHeaderLen, tcpHeaderLen, dataLen)); // 1 for more verbose + debug(("TCP %d ipHeader %d tcpHeader %d Data %d\n", ipPktLen, ipHeaderLen, headerSizeTCP, dataLen)); // 1 for more verbose } if (dataLen > 0) { - debug(("%s\n",ppp.pkt.buf+4+ipHeaderLen+tcpHeaderLen)); // show the data + debug(("%s\n",ppp.pkt.buf+4+ipHeaderLen+headerSizeTCP)); // show the data } }