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:
- 167:ff8a2d8beeb1
- Parent:
- 166:0386c2d5dc89
- Child:
- 168:c77eb908042a
diff -r 0386c2d5dc89 -r ff8a2d8beeb1 PPP-Blinky/ppp-blinky.cpp --- a/PPP-Blinky/ppp-blinky.cpp Sun Sep 03 19:20:11 2017 +0000 +++ b/PPP-Blinky/ppp-blinky.cpp Mon Sep 04 04:13:15 2017 +0000 @@ -45,7 +45,7 @@ // Using the second serial port will slow down packet response time // Note - the LPC11U24 does NOT have a second serial port -#define SERIAL_PORT_MONITOR_NO /* change to SERIAL_PORT_MONITOR_YES for debug messages */ +#define SERIAL_PORT_MONITOR_YES /* change to SERIAL_PORT_MONITOR_YES for debug messages */ // here we define the OPTIONAL, second debug serial port for various mbed target boards #ifdef SERIAL_PORT_MONITOR_YES @@ -105,7 +105,7 @@ </body>\ </html>\r\n"; // size = 644 bytes plus 1 null byte = 645 bytes -// this is websocket demo we serve when GET /ws is requested +// this is a websocket demo html page we serve when GET /ws is requested const static char webSocketPage[] = "\ <!DOCTYPE html>\ <html>\ @@ -157,17 +157,20 @@ /// a structure to keep all our ppp variables in struct pppType { - pppHeaderType * ppp; // pointer to ppp struct + union { + pppHeaderType * ppp; // pointer to ppp structure + ipcpHeaderType * ipcp; // pointer to ipcp structure + }; union { ipHeaderType * ip; // pointer to ip header struct char * ipStart; // char pointer to ip header struct (need a char pointer for byte offset calculations) }; - 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) + union { // a union for the packet type contained in the IP packet + tcpHeaderType * tcp; // pointer to tcp header struct + udpHeaderType * udp; // pointer to udp header struct icmpHeaderType * icmp; // pointer to udp header struct + char * tcpStart; // char pointer to tcp header struct (need a char pointer for byte offset calculations) + char * udpStart; // char pointer to udp header struct (need a char pointer for byte offset calculations) char * icmpStart; // char pointer to icmp header struct (need a char pointer for byte offset calculations) }; int online; // we hunt for a PPP connection if this is zero @@ -433,13 +436,13 @@ debugPrintf("Host IP = %d.%d.%d.%d (%08x)\n", ppp.pkt.buf[10],ppp.pkt.buf[11],ppp.pkt.buf[12],ppp.pkt.buf[13],ppp.hostIP); } - ppp.pkt.buf[4]=2; // change code to ack + ppp.ipcp->code=2; // change code to ack sendPppFrame(); // acknowledge everything they ask for - assume it's IP addresses debugPrintf("Our IPCP Ask (no options)\n"); - ppp.pkt.buf[4]=1; // change code to request - ppp.pkt.buf[7]=4; // no options in this request - ppp.pkt.len=10; // no options in this request shortest ipcp packet possible (4 ppp + 4 ipcp + 2 crc) + ppp.ipcp->code=1; // change code to request + ppp.ipcp->lengthR = __REV16( 4 ); // 4 is minimum length - no options in this request + ppp.pkt.len=4+4+2; // no options in this request shortest ipcp packet possible (4 ppp + 4 ipcp + 2 crc) sendPppFrame(); // send our request } @@ -455,7 +458,7 @@ { debugPrintf("Their IPCP Nack\n"); if (ppp.pkt.buf[8]==3) { // check if the NACK contains an IP address parameter - ppp.pkt.buf[4]=1; // assume the NACK contains our "suggested" IP address + ppp.ipcp->code=1; // assume the NACK contains our "suggested" IP address sendPppFrame(); // let's request this IP address as ours debugPrintf("Our IPCP ACK (received an IP)\n"); } else { // if it's not an IP nack we ignore it @@ -472,8 +475,8 @@ /// process an incoming IPCP packet void IPCPframe() { - int code = ppp.pkt.buf[4]; // packet type is here - switch (code) { + int action = ppp.ipcp->code; // packet type is here + switch (action) { case 1: ipcpConfigRequestHandler(); break; @@ -563,8 +566,8 @@ udpLength.data = udpLength.all - 8; // size of udp data #ifdef SERIAL_PORT_MONITOR_YES - char * srcIP = (char *)&ppp.ip->srcAdrR; //ipHeader+12; // IP source - char * dstIP = (char *)&ppp.ip->dstAdrR; //ipHeader+16; // IP destination + char * srcIP = ppp.ip->srcAdrPtr; //ipHeader+12; // IP source + char * dstIP = ppp.ip->dstAdrPtr; //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 @@ -639,13 +642,13 @@ ppp.icmpStart = ppp.ipStart + ipLength.header; // calculate start of udp header icmpLength.all = ipLength.all - ipLength.header; // length of icmp packet icmpLength.data = icmpLength.all - 8; // length of icmp data - + #define ICMP_TYPE_PING_REQUEST 8 if ( ppp.icmp->type == ICMP_TYPE_PING_REQUEST ) { ppp.ip->ttl--; // decrement time to live (so we have to update header checksum) #ifdef SERIAL_PORT_MONITOR_YES - char * srcAdr = (char *) &ppp.ip->srcAdrR; - char * dstAdr = (char *) &ppp.ip->dstAdrR; + char * srcAdr = ppp.ip->srcAdrPtr; + char * dstAdr = ppp.ip->dstAdrPtr; int icmpIdent = __REV16( ppp.ip->identR ); // byte reversed - big endian int icmpSequence = __REV16( ppp.icmp->sequenceR ); // byte reversed - big endian if(1) { @@ -707,8 +710,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 = ppp.ip->srcAdrPtr; + char * dstAdr = ppp.ip->dstAdrPtr; 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 ); @@ -867,7 +870,7 @@ n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.responseCounter); // body = the http frame count #else if( httpGet6 == 'b' ) { // if the fetched page is "xb" send a meta command to let the browser continuously reload - n=n+sprintf(n+dataStart, "<title>%d</title><meta http-equiv=\"refresh\" content=\"0\">",ppp.responseCounter); // reload loop - handy for benchmarking + n=n+sprintf(n+dataStart, "<meta http-equiv=\"refresh\" content=\"0\">"); // reload loop - handy for benchmarking } // /x is a very short page, in fact, it is only a decimal number showing the http Page count n=n+sprintf(n+dataStart,"%d ",ppp.responseCounter); // not really valid html but most browsers and curl are ok with it @@ -1059,7 +1062,7 @@ /// process an incoming IP packet void IPframe() { - int protocol = ppp.pkt.buf[13]; + int protocol = ppp.ip->protocol; switch (protocol) { case 1: ICMPpacket(); @@ -1147,27 +1150,23 @@ /// determine the packet type (IP, IPCP or LCP) of incoming packets void determinePacketType() { - if ( ppp.pkt.buf[0] != 0xff ) { - debugPrintf("byte0 != ff\n"); + if ( ppp.ppp->address != 0xff ) { + debugPrintf("Unexpected: PPP address != ff\n"); return; } - if ( ppp.pkt.buf[1] != 3 ) { - debugPrintf("byte1 != 3\n"); + if ( ppp.ppp->control != 3 ) { + debugPrintf("Unexpected: PPP control != 3\n"); return; } - if ( ppp.pkt.buf[3] != 0x21 ) { - debugPrintf("byte2 != 21\n"); - return; - } - int packetType = ppp.pkt.buf[2]; - switch (packetType) { - case 0xc0: + unsigned int protocol = __REV16( ppp.ppp->protocolR ); + switch ( protocol ) { + case 0xc021: LCPframe(); break; // link control - case 0x80: + case 0x8021: IPCPframe(); break; // IP control - case 0x00: + case 0x0021: IPframe(); break; // IP itself default: