RealtimeCompLab2
Dependencies: mbed
Fork of PPP-Blinky by
Diff: main.cpp
- Revision:
- 61:b3c1a04efd0a
- Parent:
- 58:8f39f28f9b14
- Child:
- 62:f192926e42f1
--- a/main.cpp Mon Apr 24 05:39:02 2017 +0000 +++ b/main.cpp Sun Jun 04 20:14:36 2017 +0000 @@ -80,7 +80,7 @@ pppType ppp; // our global - definitely not thread safe -// intitialize our globals +// Initialize our globals void pppInitStruct() { ppp.online=0; @@ -516,6 +516,7 @@ int tcpSize = packetLength - headerSizeIP; int headerSizeTCP = ((offset[0]>>4)&0x0f)*4; // size of tcp header only + int protocolIP = protocol[0]; 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]); @@ -539,7 +540,7 @@ int flagsOut = TCP_FLAG_ACK; // the default case is an ACK packet int fastResponse = 0; // normally you wait 200ms before sending a packet but this can make it faster - ppp.seq = ack; // adopt their sequence number calculation in place of doing our own calculation + ppp.seq = ack; // always adopt their sequence number calculation in place of doing our own calculation if ( ((flagsTCP & ~TCP_FLAG_ACK) == 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) { if (incomingLen == 0) { // ignore - just an empty ack packet @@ -620,19 +621,16 @@ // Now we have to recalculate all the header sizes, swap IP address/port source and destination, and do the IP and TCP checksums - char src[4]; // temp hold - char dst[4]; // temp hold - memcpy(src, srcAdr,4); - memcpy(dst, dstAdr,4); - memcpy(srcAdr, dst,4); - memcpy(dstAdr, src,4); // swap ip address source/dest + char tempHold[12]; + memcpy(tempHold, srcAdr,4); + memcpy(tempHold+4, dstAdr,4); + memcpy(srcAdr, tempHold+4,4); + memcpy(dstAdr, tempHold,4); // swap ip address source/dest - char psrc[2]; // temp hold - char pdst[2]; // temp hold - memcpy(psrc, srctcp,2); - memcpy(pdst, dsttcp,2); - memcpy(srctcp, pdst,2); - memcpy(dsttcp, psrc,2); // swap ip port source/dest + memcpy(tempHold, srctcp,2); + memcpy(tempHold+2, dsttcp,2); + memcpy(srctcp, tempHold+2,2); + memcpy(dsttcp, tempHold,2); // swap ip port source/dest ack = seq + incomingLen; // acknowledge the number of bytes that they sent by adding it to "our" sequence number seq = ppp.seq; // set up the sequence number we have to respond with @@ -657,26 +655,28 @@ // the header is all set up, now do the IP and TCP checksums - headercheck[0]=0; - headercheck[1]=0; - headerCheckSum(); // redo the ip header checksum - - char pseudoHeader[12]; // we are building a fake TCP header - int sum; - memcpy( pseudoHeader+0, srcAdr, 8); // source and destination addresses. - pseudoHeader[8]=0; - pseudoHeader[9]=protocol[0]; - pseudoHeader[10]=tcpSize>>8; - pseudoHeader[11]=tcpSize; - char temp[12]; // temporary storage for the 12 bytes that are in the way - memcpy(temp, s-12, 12); // save the 12 bytes that are in the way - memcpy( s-12, pseudoHeader, 12); // copy in the fake header + headercheck[0]=0; // IP header checksum + headercheck[1]=0; // IP header checksum + headerCheckSum(); // calculate the IP header checksum + + // now we have to build a 12-byte TCP "pseudo-header" in front of the TCP header (containing some IP header values) in order to correctly calculate the TCP checksum + + char * pseudoHeader = s-12; + memcpy(tempHold, pseudoHeader, 12); // save the 12 bytes in the IP header where the TCP pseudo-Header must be built + memcpy( pseudoHeader+0, srcAdr, 8); // IP source and destination addresses from IP header + memset( pseudoHeader+8, 0, 1); // reserved, set to zero + memset( pseudoHeader+9, protocolIP, 1); // protocol from IP header + memset( pseudoHeader+10, tcpSize>>8, 1); // size of TCP header + memset( pseudoHeader+11, tcpSize, 1); // size of TCP header + + // pseudo-header built, now we can calculate TCP checksum + checksumtcp[0]=0; checksumtcp[1]=0; - sum=dataCheckSum(s-12,tcpSize+12); // calculate the TCP checksum with the fake header - checksumtcp[0]=sum>>8; - checksumtcp[1]=sum; - memcpy( s-12, temp, 12); // restore the 12 bytes that the fake header overwrote + int pseudoHeaderSum=dataCheckSum(s-12,tcpSize+12); // calculate the TCP checksum starting at the pseudo-header + checksumtcp[0]=pseudoHeaderSum>>8; + checksumtcp[1]=pseudoHeaderSum; + memcpy( s-12, tempHold, 12); // restore the 12 bytes that the pseudo-header overwrote if (fastResponse==1) { fastResponse=0; // reset and skip 200 ms wait } else {