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:
- 163:d1b4328e9f08
- Parent:
- 162:594729c0e5c1
- Child:
- 164:c3de3d212c4b
--- a/PPP-Blinky/ppp-blinky.cpp Sat Sep 02 18:14:26 2017 +0000 +++ b/PPP-Blinky/ppp-blinky.cpp Sun Sep 03 01:29:01 2017 +0000 @@ -165,6 +165,8 @@ union { tcpHeaderType * tcp; // pointer to tcp header struct char * tcpStart; // char pointer to tcp header struct (need a char pointer for byte offset calculations) + udpHeaderType * udp; // pointer to udp header struct + char * udpStart; // char pointer to udp header struct (need a char pointer for byte offset calculations) }; int online; // we hunt for a PPP connection if this is zero int hostIP; // ip address of host @@ -543,31 +545,38 @@ /// If the packet starts with the string "echo " or "test" we echo back a special packet void UDPpacket() { - char * ipHeader = ppp.pkt.buf+4; // udp packet start - char * ipSizeBuf = ipHeader+2; // size of IP packet - int headerSizeIP = 4*(ipHeader[0]&0xf); // size of IP header - char * udpHeader = ipHeader + headerSizeIP; // udp info start - char * udpLen = udpHeader+4; // udp data length - char * udpCheckSum = udpHeader+6; // udp checksum - char * udpData = udpHeader+8; // start of UDP data - int udpLength = ((udpLen[0]<<8) | udpLen[1]); // size of udp packet - int udpDataSize = udpLength - 8; // size of udp data + struct { + unsigned int all; // length of entire ip packet + unsigned int header; // length of ip header + } ipLength; + + struct { + unsigned int all; // lenght of entire udp packet + unsigned int data; // length of udp data segment + } udpLength; + + ipLength.header = 4 * ppp.ip->headerLength; // length of ip header + ppp.udpStart = ppp.ipStart + ipLength.header; // calculate start of udp header + udpLength.all = __REV16( ppp.udp->lengthR ); //((udpLen[0]<<8) | udpLen[1]); // size of udp packet + udpLength.data = udpLength.all - 8; // size of udp data #ifdef SERIAL_PORT_MONITOR_YES - char * srcIP = ipHeader+12; // IP source - char * dstIP = ipHeader+16; // IP destination - char * udpSrcPort = udpHeader+0; // source port - char * udpDstPort = udpHeader+2; // destination port - int udpSrc = (udpSrcPort[0]<<8)|udpSrcPort[1]; // integer of UDP source port - int udpDst = (udpDstPort[0]<<8)|udpDstPort[1]; // integer of UDP dest port - if(v0) debugPrintf("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],udpSrc); - if(v0) debugPrintf("%d.%d.%d.%d:%d ", dstIP[0],dstIP[1],dstIP[2],dstIP[3],udpDst); - if(v0) debugPrintf("Len %03d", udpLength); + char * srcIP = (char *)&ppp.ip->srcAdrR; //ipHeader+12; // IP source + char * dstIP = (char *)&ppp.ip->dstAdrR; //ipHeader+16; // IP destination + + unsigned int udpSrcPort = __REV16( ppp.udp->srcPortR ); //(udpSrcPort[0]<<8)|udpSrcPort[1]; // integer of UDP source port + unsigned int udpDstPort = __REV16( ppp.udp->dstPortR ); //(udpDstPort[0]<<8)|udpDstPort[1]; // integer of UDP dest port + + if(v0) { + debugPrintf("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],udpSrcPort); + debugPrintf("%d.%d.%d.%d:%d ", dstIP[0],dstIP[1],dstIP[2],dstIP[3],udpDstPort); + debugPrintf("Len %03d", udpLength); + } if (v1) { - int printSize = udpLength-8; + int printSize = udpLength.data; if (printSize > 20) printSize = 20; // print only first 20 characters for (int i=0; i<printSize; i++) { - char ch = udpData[i]; + char ch = ppp.udp->data[i]; if (ch>31 && ch<127) { debugPrintf("%c", ch); } else { @@ -577,33 +586,33 @@ } if (v0) debugPrintf("\n"); #endif - int echoFound = !strncmp(udpData,"echo ",5); // true if UDP message starts with "echo " - int testFound = !strncmp(udpData,"test" ,4); // true if UDP message starts with "test" + int echoFound = !strncmp(ppp.udp->data,"echo ",5); // true if UDP message starts with "echo " + int testFound = !strncmp(ppp.udp->data,"test" ,4); // true if UDP message starts with "test" if ( (echoFound) || (testFound)) { // if the UDP message starts with "echo " or "test" we answer back swapIpAddresses(); // swap IP source and destination swapIpPorts(); // swap IP source and destination ports if (echoFound) { - memcpy(udpData,"Got{",4); // in the UDP data modify "echo" to "Got:" + memcpy(ppp.udp->data,"Got{",4); // in the UDP data modify "echo" to "Got:" char endString[] = "} UDP Server: PPP-Blinky\n"; // an appendix - memcpy(udpData+udpLength-8,endString,sizeof(endString)-1); // add endstring size to the UDP message - udpDataSize = udpDataSize + sizeof(endString)-1; // update udp data size with the size of the appendix + memcpy(ppp.udp->data+udpLength.data,endString,sizeof(endString)-1); // add endstring size to the UDP message + udpLength.all = udpLength.data + sizeof(endString)-1; // 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(udpData,udpResponse,n); // copy response to the UDP data area - udpDataSize = n; // new udp data size + memcpy(ppp.udp->data,udpResponse,n); // copy response to the UDP data area + udpLength.data = n; // update udp data size } - udpLength = udpDataSize+8; // update udp packet length - int ipSize = headerSizeIP + udpLength; // update ip packet length - ppp.pkt.len = ipSize+2+4; // update ppp packet length - udpLen[0] = udpLength>>8; - udpLen[1] = udpLength; // update UDP length - ipSizeBuf[0] = ipSize>>8; - ipSizeBuf[1] = ipSize; // update IP length - IpHeaderCheckSum(); // update IP header checksum - memset(udpCheckSum,0,2); // we don't yet recompute the checksum (it's optional), so have to zero it + // we may have changed data length, update all the lengths + udpLength.all = udpLength.data+8; // update local udp packet length + ipLength.all = ipLength.header + udpLength.all; // update IP Length + ppp.ip->lengthR = __REV16(ipLength.all); // update IP length in buffer + ppp.udp->lengthR = __REV16(udpLength.all); // update UDP length in buffer + 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 sendPppFrame(); // send the UDP message back } } @@ -688,8 +697,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.ip->srcAdrR; - char * dstAdr = (char *)&ppp.ip->dstAdrR; + char * srcAdr = (char *)&ppp.ip->srcAdrR; + char * dstAdr = (char *)&ppp.ip->dstAdrR; 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 putsWhileCheckingInput( pbuf ); @@ -886,7 +895,7 @@ dataStart[1]=0; // we don't have mask bytes on } else { if (v1) putsWhileCheckingInput("TCP data received\n"); // all other tcp push packets - } + } return n; // total byte size of our response }