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: PPP-Blinky/ppp-blinky.cpp
- Revision:
- 170:3d3b2126181c
- Parent:
- 169:ee90aed3a735
- Child:
- 171:46b36d4edd1d
--- a/PPP-Blinky/ppp-blinky.cpp Mon Sep 04 04:28:55 2017 +0000 +++ b/PPP-Blinky/ppp-blinky.cpp Mon Sep 04 05:45:59 2017 +0000 @@ -546,6 +546,19 @@ memcpy(ipDstPort,tempHold, 2); // dest <- tempHold } +/// Build the "pseudo header" required for UDP and TCP, then calculate its checksum +void checkSumPseudoHeader( unsigned int packetLength ) +{ + // this header contains the most important parts of the IP header, i.e. source and destination address, protocol number and data length. + pseudoIpHeaderType pseudoHeader; // create pseudo header + pseudoHeader.srcAdrR = ppp.ip->srcAdrR; // copy in ip source address + pseudoHeader.dstAdrR = ppp.ip->dstAdrR; // copy in ip dest address + pseudoHeader.zero = 0; // zero byte + pseudoHeader.protocol = ppp.ip->protocol; // protocol number (udp or tcp) + pseudoHeader.lengthR = __REV16( packetLength ); // size of tcp or udp packet + dataCheckSum(pseudoHeader.start, 12, 1); // calculate this header's checksum +} + /// Process an incoming UDP packet. /// If the packet starts with the string "echo " or "test" we echo back a special packet void UDPpacket() @@ -598,14 +611,13 @@ swapIpPorts(); // swap IP source and destination ports if (echoFound) { memcpy(ppp.udp->data,"Got{",4); // in the UDP data modify "echo" to "Got:" - int n = sprintf(ppp.udp->data+udpLength.data, "} UDP Server: PPP-Blinky\n"); // an appendix + int n=0; + n=n+sprintf(n+ppp.udp->data+udpLength.data, "} UDP Server: PPP-Blinky\n"); // an appendix udpLength.data = udpLength.data + n; // update udp data size with the size of the appendix } if (testFound) { - char udpResponse[50]; - int n = 0; - n = n+sprintf(udpResponse+n,"Response count %d\n",ppp.responseCounter++); - memcpy(ppp.udp->data,udpResponse,n); // copy response to the UDP data area + int n=0; + n=n+sprintf(n+ppp.udp->data,"Response count %d\n",ppp.responseCounter++); udpLength.data = n; // update udp data size } // we may have changed data length, update all the lengths @@ -616,7 +628,10 @@ ppp.pkt.len = ipLength.all+2+4; // update ppp packet length IpHeaderCheckSum(); // refresh IP header checksum - ppp.udp->checkSumR = 0; // we don't yet recompute the UDP checksum (it's optional), so have to zero it + checkSumPseudoHeader( udpLength.all ); // get the UDP pseudo-header checksum + ppp.udp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared + unsigned int pseudoHeaderSum=dataCheckSum(ppp.udpStart,udpLength.all, 0); // continue the TCP checksum on the whole TCP packet + ppp.udp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header ppp.responseCounter++; // count UDP packet as a response sendPppFrame(); // send the UDP message back } else { @@ -997,22 +1012,14 @@ // the header is all set up, now do the IP and TCP checksums IpHeaderCheckSum(); // calculate new IP header checksum - - // create the TCP "pseudo-header" (containing some IP header values) in order to correctly calculate the TCP checksum - // this header contains the most important parts of the IP header, i.e. source and destination address, protocol number and data length. - pseudoIpHeaderType pseudoHeader; // create pseudo header - pseudoHeader.srcAdrR = ppp.ip->srcAdrR; // copy in ip source address - pseudoHeader.dstAdrR = ppp.ip->dstAdrR; // copy in ip dest address - pseudoHeader.zero = 0; // padding - pseudoHeader.protocol = ppp.ip->protocol; // protocol number - pseudoHeader.lengthR = __REV16( tcpSize ); // size of tcp packet - dataCheckSum(pseudoHeader.start, 12, 1); // start with the TCP checksum of the pseudoheader + checkSumPseudoHeader( tcpSize ); // get the TCP pseudo-header checksum ppp.tcp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared unsigned int pseudoHeaderSum=dataCheckSum(ppp.tcpStart,tcpSize, 0); // continue the TCP checksum on the whole TCP packet ppp.tcp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header - dumpHeaderIP(1); // dump outgoing IP header - dumpHeaderTCP(1); // dump outgoing TCP header + dumpHeaderIP(1); // dump outgoing IP header before sending the frame + dumpHeaderTCP(1); // dump outgoing TCP header before sending the frame + for (int i=0; i<45*1000/10; i++) { // 45 ms delay before sending frame - a typical internet delay time checkPc(); // catch any incoming characters wait_us(10); // wait less than 1 character duration at 115200