RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Committer:
plebs96
Date:
Mon Nov 20 22:03:28 2017 +0000
Revision:
193:8d3c9e94a4fb
Parent:
185:4cd8f91e9d49
webserverBlinky

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nixnax 182:f039ec218c9f 1 // PPP-Blinky - "The Most Basic Internet Thing"
nixnax 142:54d1543e23e5 2 // A Tiny HTTP Webserver Using Windows XP/7/8/10/Linux Dial-Up Networking Over A Serial Port.
nixnax 142:54d1543e23e5 3 // Receives UDP packets and responds to ping (ICMP Echo requests)
nixnax 142:54d1543e23e5 4 // WebSocket Service - see https://en.wikipedia.org/wiki/WebSocket
nixnax 142:54d1543e23e5 5
nixnax 142:54d1543e23e5 6 // Copyright 2016/2017 Nicolas Nackel aka Nixnax. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nixnax 142:54d1543e23e5 7
nixnax 142:54d1543e23e5 8 // Notes and Instructions
nixnax 142:54d1543e23e5 9 // http://bit.ly/PPP-Blinky-Instructions
nixnax 142:54d1543e23e5 10 // http://bit.ly/win-rasdial-config
nixnax 142:54d1543e23e5 11
nixnax 142:54d1543e23e5 12 // Handy reading material
nixnax 142:54d1543e23e5 13 // https://technet.microsoft.com/en-us/library/cc957992.aspx
nixnax 142:54d1543e23e5 14 // https://en.wikibooks.org/wiki/Serial_Programming/IP_Over_Serial_Connections
nixnax 142:54d1543e23e5 15 // http://atari.kensclassics.org/wcomlog.htm
nixnax 142:54d1543e23e5 16
nixnax 142:54d1543e23e5 17 // Handy tools
nixnax 142:54d1543e23e5 18 // https://ttssh2.osdn.jp/index.html.en - Tera Term, a good terminal program to monitor the debug output from the second serial port with!
nixnax 142:54d1543e23e5 19 // https://www.microsoft.com/en-us/download/details.aspx?id=4865 - Microsoft network monitor - real-time monitoring of PPP packets
nixnax 142:54d1543e23e5 20 // http://pingtester.net/ - nice tool for high rate ping testing
nixnax 142:54d1543e23e5 21 // http://www.sunshine2k.de/coding/javascript/crc/crc_js.html - Correctly calculates the 16-bit FCS (crc) on our frames (Choose CRC16_CCITT_FALSE), then custom relected-in=1, reflected-out=1
nixnax 142:54d1543e23e5 22 // https://technet.microsoft.com/en-us/sysinternals/pstools.aspx - psping for fast testing of ICMP ping function
nixnax 142:54d1543e23e5 23 // https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky
nixnax 142:54d1543e23e5 24 // Windows Powershell invoke-webrequest command - use it to stress test the webserver like this: while (1){ invoke-webrequest -uri 172.10.10.1/x }
nixnax 142:54d1543e23e5 25
nixnax 142:54d1543e23e5 26 // Connecting PPP-Blinky to Linux
nixnax 142:54d1543e23e5 27 // PPP-Blinky can be made to talk to Linux - tested on Fedora - the following command, which uses pppd, works:
nixnax 142:54d1543e23e5 28 // pppd /dev/ttyACM0 115200 debug dump local passive noccp novj nodetach nocrtscts 172.10.10.1:172.10.10.2
nixnax 142:54d1543e23e5 29 // in the above command 172.10.10.1 is the adapter IP, and 172.10.10.2 is the IP of PPP-Blinky.
nixnax 142:54d1543e23e5 30 // See also https://en.wikipedia.org/wiki/Point-to-Point_Protocol_daemon
nixnax 142:54d1543e23e5 31
nixnax 142:54d1543e23e5 32 // Special pages when PPP-Blinky is running
nixnax 142:54d1543e23e5 33 // 172.10.10.2 root page
nixnax 182:f039ec218c9f 34 // 172.10.10.2/x returns the number of ppp frames sent - this is handy for testing
nixnax 182:f039ec218c9f 35 // 172.10.10.2/xb also returns number of ppp frames sent, but issues a fast refresh meta command. This allows you to use your browser to benchmark page load speed
nixnax 142:54d1543e23e5 36 // 172.10.10.2/ws a simple WebSocket demo
nixnax 182:f039ec218c9f 37 // http://jsfiddle.net/d26cyuh2/ more complete WebSocket demo in JSFiddle, showing cross-domain access
nixnax 142:54d1543e23e5 38
nixnax 142:54d1543e23e5 39 // Ok, enough talking, time to check out some code!!
nixnax 142:54d1543e23e5 40
nixnax 142:54d1543e23e5 41 #include "ppp-blinky.h"
nixnax 142:54d1543e23e5 42
nixnax 142:54d1543e23e5 43 // The #define below enables/disables a second (OPTIONAL) serial port that prints out interesting diagnostic messages.
nixnax 146:a45f49a2b29c 44 // Change to SERIAL_PORT_MONITOR_YES to enable diagnostics messages. You need to wire a second serial port to your mbed hardware to monitor the debug output.
nixnax 142:54d1543e23e5 45 // Using the second serial port will slow down packet response time
nixnax 142:54d1543e23e5 46 // Note - the LPC11U24 does NOT have a second serial port
nixnax 146:a45f49a2b29c 47
nixnax 183:6d3ba874b267 48 #define SERIAL_PORT_MONITOR_NO /* change to SERIAL_PORT_MONITOR_YES for debug messages */
nixnax 142:54d1543e23e5 49
nixnax 142:54d1543e23e5 50 // here we define the OPTIONAL, second debug serial port for various mbed target boards
nixnax 142:54d1543e23e5 51 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 142:54d1543e23e5 52 #if defined(TARGET_LPC1768)
nixnax 179:ba2b2ddd0da5 53 RawSerial xx(p9, p10); // Second serial port on LPC1768 - not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
nixnax 142:54d1543e23e5 54 #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_L476RG) || defined(TARGET_NUCLEO_F401RE)
nixnax 179:ba2b2ddd0da5 55 RawSerial xx(PC_10, PC_11); // Second serial port on NUCLEO boards - not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
nixnax 142:54d1543e23e5 56 #elif defined(TARGET_LPC11U24)
nixnax 142:54d1543e23e5 57 #error The LPC11U24 does not have a second serial port to use for debugging - change SERIAL_PORT_MONITOR_YES back to SERIAL_PORT_MONITOR_NO
nixnax 142:54d1543e23e5 58 #elif defined (TARGET_KL46Z) || (TARGET_KL25Z)
nixnax 179:ba2b2ddd0da5 59 RawSerial xx(PTE0,PTE1); // Second serial port on FRDM-KL46Z board
plebs96 193:8d3c9e94a4fb 60 #elif defined(TARGET_KW41Z)
nixnax 142:54d1543e23e5 61 // change the next line to YOUR target board's second serial port pin definition if it's not present - and if it works, please send it to me - thanks!!!
nixnax 179:ba2b2ddd0da5 62 RawSerial xx(p9, p10); // change this to YOUR board second serial port pin definition - and please send it to me if it works!!!
nixnax 142:54d1543e23e5 63 #else
nixnax 142:54d1543e23e5 64 #error Add your target board's second serial port here if you want to use debugging - or simply change SERIAL_PORT_MONITOR_YES to SERIAL_PORT_MONITOR_NO
nixnax 142:54d1543e23e5 65 #endif
nixnax 179:ba2b2ddd0da5 66 #define debugPrintf(x...) xx.printf (x) /* if we have a serial port we can print debug messages */
nixnax 142:54d1543e23e5 67 #define debugPutc(x...) xx.putc(x)
nixnax 142:54d1543e23e5 68 #define debugBaudRate(x...) xx.baud(x)
nixnax 142:54d1543e23e5 69 #else
nixnax 142:54d1543e23e5 70 // if we don't have a debug port the debug print functions do nothing
nixnax 142:54d1543e23e5 71 #define debugPrintf(x...) {}
nixnax 142:54d1543e23e5 72 #define debugPutc(x...) {}
nixnax 142:54d1543e23e5 73 #define debugBaudRate(x...) {}
nixnax 142:54d1543e23e5 74 #endif
nixnax 142:54d1543e23e5 75
nixnax 142:54d1543e23e5 76 // verbosity flags used in debug printouts - change to 1 to see increasingly more detailed debug info.
nixnax 182:f039ec218c9f 77 #define v0 1
nixnax 142:54d1543e23e5 78 #define v1 0
nixnax 142:54d1543e23e5 79 #define v2 0
nixnax 182:f039ec218c9f 80 #define IP_HEADER_DUMP_YES /* YES for ip header dump */
nixnax 182:f039ec218c9f 81 #define TCP_HEADER_DUMP_YES /* YES for tcp header dump */
nixnax 142:54d1543e23e5 82
nixnax 142:54d1543e23e5 83 // this is the webpage we serve when we get an HTTP request to root (/)
nixnax 142:54d1543e23e5 84 // keep size under ~900 bytes to fit into a single PPP packet
plebs96 193:8d3c9e94a4fb 85 char body[] = \
plebs96 193:8d3c9e94a4fb 86 "<html>\
plebs96 193:8d3c9e94a4fb 87 <head><title>A simple Web Server</title></head>\
plebs96 193:8d3c9e94a4fb 88 <body>\
plebs96 193:8d3c9e94a4fb 89 <h1>COMP3215</h1>\
plebs96 193:8d3c9e94a4fb 90 <form action=\"\" method=\"post\">\
plebs96 193:8d3c9e94a4fb 91 <button name=\"button\" value='1' disable=\"disable\">LED ON</button>\
plebs96 193:8d3c9e94a4fb 92 <button name=\"button\" value='0' disable=\"disable\">LED OFF</button>\
plebs96 193:8d3c9e94a4fb 93 </form>\
plebs96 193:8d3c9e94a4fb 94 </body>\
plebs96 193:8d3c9e94a4fb 95 </html>";
plebs96 193:8d3c9e94a4fb 96 //const static char rootWebPage[] = "\
plebs96 193:8d3c9e94a4fb 97 //<!DOCTYPE html>
plebs96 193:8d3c9e94a4fb 98 //<html>\
plebs96 193:8d3c9e94a4fb 99 //<head><title>A simple Web Server</title></head>\
plebs96 193:8d3c9e94a4fb 100 //<body>\
plebs96 193:8d3c9e94a4fb 101 //<h1>COMP3215</h1>\
plebs96 193:8d3c9e94a4fb 102 //<form action=\"\" method=\"post\">\
plebs96 193:8d3c9e94a4fb 103 //<button name=\"button\" value='1' disable=\"disable\">LED ON</button>\
plebs96 193:8d3c9e94a4fb 104 //<button name=\"button\" value='0' disable=\"disable\">LED OFF</button>\
plebs96 193:8d3c9e94a4fb 105 //</form>\
plebs96 193:8d3c9e94a4fb 106 //</body>\
plebs96 193:8d3c9e94a4fb 107 //</html>";
plebs96 193:8d3c9e94a4fb 108 // size = 644 bytes plus 1 null byte = 645 bytes
nixnax 142:54d1543e23e5 109
nixnax 167:ff8a2d8beeb1 110 // this is a websocket demo html page we serve when GET /ws is requested
nixnax 142:54d1543e23e5 111 const static char webSocketPage[] = "\
nixnax 142:54d1543e23e5 112 <!DOCTYPE html>\
nixnax 142:54d1543e23e5 113 <html>\
nixnax 142:54d1543e23e5 114 <head>\
nixnax 142:54d1543e23e5 115 <title>mbed PPP-Blinky</title>\
nixnax 142:54d1543e23e5 116 <script>\
nixnax 142:54d1543e23e5 117 window.onload=function(){\
nixnax 142:54d1543e23e5 118 var url=\"ws://172.10.10.2\";\
nixnax 142:54d1543e23e5 119 var sts=document.getElementById(\"sts\");\
nixnax 142:54d1543e23e5 120 var btn=document.getElementById(\"btn\");\
nixnax 142:54d1543e23e5 121 var ctr=0;\
nixnax 142:54d1543e23e5 122 function show(text){sts.textContent=text;}\
nixnax 142:54d1543e23e5 123 btn.onclick=function(){\
nixnax 142:54d1543e23e5 124 if(btn.textContent==\"Connect\"){\
nixnax 142:54d1543e23e5 125 x=new WebSocket(url);\
nixnax 142:54d1543e23e5 126 x.onopen=function(){\
nixnax 142:54d1543e23e5 127 show(\"Connected to : \"+url);\
nixnax 142:54d1543e23e5 128 btn.textContent=\"Send \\\"\"+ctr+\"\\\"\";\
nixnax 142:54d1543e23e5 129 };\
nixnax 142:54d1543e23e5 130 x.onclose=function(){show(\"closed\");};\
nixnax 142:54d1543e23e5 131 x.onmessage=function(msg){show(\"PPP-Blinky Sent: \\\"\"+msg.data+\"\\\"\");};\
nixnax 142:54d1543e23e5 132 } else {\
nixnax 142:54d1543e23e5 133 x.send(ctr);\
nixnax 142:54d1543e23e5 134 ctr=ctr+1;\
nixnax 142:54d1543e23e5 135 btn.textContent=\"Send \\\"\"+ctr+\"\\\"\";\
nixnax 142:54d1543e23e5 136 }\
nixnax 142:54d1543e23e5 137 };\
nixnax 142:54d1543e23e5 138 };\
nixnax 142:54d1543e23e5 139 </script>\
nixnax 142:54d1543e23e5 140 <body style=\"font-family: sans-serif; font-size:25px; color:#807070\">\
nixnax 142:54d1543e23e5 141 <h1>PPP-Blinky WebSocket Test</h1>\
nixnax 142:54d1543e23e5 142 <div id=\"sts\">Idle</div>\
nixnax 142:54d1543e23e5 143 <button id=\"btn\" style=\"font-size: 100%; margin-top: 55px; margin-bottom: 55px;\">Connect</button>\
nixnax 142:54d1543e23e5 144 <h4><a href=\"/\">PPP-Blinky home</a></h4>\
nixnax 142:54d1543e23e5 145 </body>\
nixnax 142:54d1543e23e5 146 </html>"; // size = 916 bytes + 1 null byte = 917 bytes
nixnax 142:54d1543e23e5 147
nixnax 142:54d1543e23e5 148 // The serial port on your mbed hardware. Your PC should be configured to view this port as a standard dial-up networking modem.
nixnax 142:54d1543e23e5 149 // On Windows the model type of the modem should be selected as "Communications cable between two computers"
nixnax 142:54d1543e23e5 150 // The modem baud rate should be set to 115200 baud
nixnax 142:54d1543e23e5 151 // See instructions at the top.
nixnax 142:54d1543e23e5 152 // On a typical mbed hardware platform this serial port is a USB virtual com port (VCP) and the USB serial driver is supplied by the board vendor.
nixnax 179:ba2b2ddd0da5 153 RawSerial pc (USBTX, USBRX); // usb virtual com port for mbed hardware
nixnax 142:54d1543e23e5 154
plebs96 193:8d3c9e94a4fb 155 DigitalOut led1(LED1);// this led toggles when a packet is received
nixnax 142:54d1543e23e5 156
nixnax 142:54d1543e23e5 157 // the standard hdlc frame start/end character. It's the tilde character "~"
nixnax 142:54d1543e23e5 158 #define FRAME_7E (0x7e)
nixnax 142:54d1543e23e5 159
nixnax 142:54d1543e23e5 160 pppType ppp; // our global - definitely not thread safe
nixnax 142:54d1543e23e5 161
nixnax 142:54d1543e23e5 162 /// Initialize the ppp structure and clear the receive buffer
nixnax 142:54d1543e23e5 163 void pppInitStruct()
nixnax 142:54d1543e23e5 164 {
nixnax 142:54d1543e23e5 165 memset( ppp.rx.buf, 0, RXBUFLEN);
nixnax 142:54d1543e23e5 166 ppp.online=0;
nixnax 142:54d1543e23e5 167 ppp.rx.tail=0;
nixnax 142:54d1543e23e5 168 ppp.rx.rtail=0;
nixnax 142:54d1543e23e5 169 ppp.rx.head=0;
nixnax 142:54d1543e23e5 170 ppp.rx.buflevel=0;
nixnax 142:54d1543e23e5 171 ppp.pkt.len=0;
nixnax 154:18b2bd92f557 172 ppp.ipData.ident=10000; // easy to recognize in ip packet dumps
nixnax 142:54d1543e23e5 173 ppp.ledState=0;
nixnax 142:54d1543e23e5 174 ppp.hdlc.frameStartIndex=0;
nixnax 146:a45f49a2b29c 175 ppp.responseCounter=0;
nixnax 142:54d1543e23e5 176 ppp.firstFrame=1;
nixnax 175:b4e6f8a6fe00 177 ppp.ppp = (pppHeaderType *)ppp.pkt.buf; // pointer to ppp header
nixnax 175:b4e6f8a6fe00 178 ppp.ip = (ipHeaderType *)(ppp.pkt.buf+4); // pointer to IP header
nixnax 142:54d1543e23e5 179 }
nixnax 142:54d1543e23e5 180
nixnax 142:54d1543e23e5 181 /// Toggle the LED on every second PPP packet received
nixnax 142:54d1543e23e5 182 void led1Toggle()
nixnax 142:54d1543e23e5 183 {
nixnax 142:54d1543e23e5 184 led1 = (ppp.ledState >> 1) & 1; // use second bit, in other words toggle LED only every second packet
nixnax 143:c5019f856a56 185 ppp.ledState++;
nixnax 142:54d1543e23e5 186 }
nixnax 142:54d1543e23e5 187
nixnax 179:ba2b2ddd0da5 188 /// Returns 1 after a connect message, 0 at startup or after a disconnect message
nixnax 150:3366bf3d294e 189 int connectedPpp()
nixnax 142:54d1543e23e5 190 {
plebs96 193:8d3c9e94a4fb 191 // cnt = 0;
plebs96 193:8d3c9e94a4fb 192 // if(cnt == 0)
plebs96 193:8d3c9e94a4fb 193 // led_flash();
nixnax 142:54d1543e23e5 194 return ppp.online;
nixnax 143:c5019f856a56 195 }
nixnax 142:54d1543e23e5 196
nixnax 179:ba2b2ddd0da5 197 /// Previously used to check for characters in receive buffer.
nixnax 179:ba2b2ddd0da5 198 /// Now just a stub.
nixnax 179:ba2b2ddd0da5 199 void checkPc() {};
nixnax 179:ba2b2ddd0da5 200
nixnax 179:ba2b2ddd0da5 201 /// PPP serial port receive interrupt handler.
nixnax 143:c5019f856a56 202 /// Check for available characters from the PC and read them into our own circular serial receive buffer at ppp.rx.buf.
nixnax 143:c5019f856a56 203 /// Also, if we are offline and a 0x7e frame start character is seen, we go online immediately
nixnax 179:ba2b2ddd0da5 204 void pppReceiveHandler()
nixnax 142:54d1543e23e5 205 {
nixnax 142:54d1543e23e5 206 char ch;
nixnax 179:ba2b2ddd0da5 207 while ( pc.readable() ) {
nixnax 142:54d1543e23e5 208 int hd = (ppp.rx.head+1)&(RXBUFLEN-1); // increment/wrap head index
nixnax 142:54d1543e23e5 209 if ( hd == ppp.rx.rtail ) {
nixnax 142:54d1543e23e5 210 debugPrintf("\nReceive buffer full\n");
nixnax 142:54d1543e23e5 211 return;
nixnax 142:54d1543e23e5 212 }
nixnax 142:54d1543e23e5 213 ch = pc.getc(); // read new character
nixnax 142:54d1543e23e5 214 ppp.rx.buf[ppp.rx.head] = ch; // insert in our receive buffer
nixnax 142:54d1543e23e5 215 if ( ppp.online == 0 ) {
nixnax 142:54d1543e23e5 216 if (ch == 0x7E) {
nixnax 142:54d1543e23e5 217 ppp.online = 1;
nixnax 142:54d1543e23e5 218 }
nixnax 142:54d1543e23e5 219 }
nixnax 142:54d1543e23e5 220 ppp.rx.head = hd; // update head pointer
nixnax 142:54d1543e23e5 221 ppp.rx.buflevel++;
nixnax 142:54d1543e23e5 222 }
nixnax 142:54d1543e23e5 223 }
nixnax 142:54d1543e23e5 224
nixnax 142:54d1543e23e5 225 /// print to debug port while checking for incoming characters
nixnax 142:54d1543e23e5 226 void putcWhileCheckingInput( char outByte )
nixnax 142:54d1543e23e5 227 {
nixnax 142:54d1543e23e5 228 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 142:54d1543e23e5 229 checkPc();
nixnax 142:54d1543e23e5 230 debugPutc( outByte );
nixnax 142:54d1543e23e5 231 checkPc();
nixnax 142:54d1543e23e5 232 #endif
nixnax 142:54d1543e23e5 233 }
nixnax 142:54d1543e23e5 234
nixnax 142:54d1543e23e5 235 /// puts to debug port while checking the PPP input stream
nixnax 142:54d1543e23e5 236 void putsWhileCheckingInput( char * data )
nixnax 142:54d1543e23e5 237 {
nixnax 142:54d1543e23e5 238 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 142:54d1543e23e5 239 char * nextChar = data;
nixnax 142:54d1543e23e5 240 while( *nextChar != 0 ) {
nixnax 142:54d1543e23e5 241 putcWhileCheckingInput( *nextChar ); // write one character to debug port while checking input
nixnax 142:54d1543e23e5 242 nextChar++;
nixnax 142:54d1543e23e5 243 }
nixnax 142:54d1543e23e5 244 #endif
nixnax 142:54d1543e23e5 245 }
nixnax 142:54d1543e23e5 246
nixnax 145:098b6ed2f7f2 247 /// Initialize the PPP FCS (frame check sequence) total
nixnax 145:098b6ed2f7f2 248 void fcsReset()
nixnax 142:54d1543e23e5 249 {
nixnax 145:098b6ed2f7f2 250 ppp.fcs=0xffff; // crc restart
nixnax 142:54d1543e23e5 251 }
nixnax 142:54d1543e23e5 252
nixnax 145:098b6ed2f7f2 253 /// update the cumulative PPP FCS (frame check sequence)
nixnax 145:098b6ed2f7f2 254 void fcsDo(int x)
nixnax 142:54d1543e23e5 255 {
nixnax 142:54d1543e23e5 256 for (int i=0; i<8; i++) {
nixnax 145:098b6ed2f7f2 257 ppp.fcs=((ppp.fcs&1)^(x&1))?(ppp.fcs>>1)^0x8408:ppp.fcs>>1; // crc calculator
nixnax 142:54d1543e23e5 258 x>>=1;
nixnax 142:54d1543e23e5 259 }
nixnax 142:54d1543e23e5 260 checkPc(); // handle input
nixnax 142:54d1543e23e5 261 }
nixnax 142:54d1543e23e5 262
nixnax 145:098b6ed2f7f2 263 /// calculate the PPP FCS (frame check sequence) on an entire block of memory
nixnax 145:098b6ed2f7f2 264 int fcsBuf(char * buf, int size) // crc on an entire block of memory
nixnax 142:54d1543e23e5 265 {
nixnax 145:098b6ed2f7f2 266 fcsReset();
nixnax 145:098b6ed2f7f2 267 for(int i=0; i<size; i++)fcsDo(*buf++);
nixnax 145:098b6ed2f7f2 268 return ppp.fcs;
nixnax 142:54d1543e23e5 269 }
nixnax 142:54d1543e23e5 270
nixnax 142:54d1543e23e5 271 /// Get one character from our received PPP buffer
nixnax 142:54d1543e23e5 272 int pc_getBuf()
nixnax 142:54d1543e23e5 273 {
nixnax 142:54d1543e23e5 274 int x = ppp.rx.buf[ ppp.rx.tail ];
nixnax 142:54d1543e23e5 275 ppp.rx.tail=(ppp.rx.tail+1)&(RXBUFLEN-1);
nixnax 142:54d1543e23e5 276 ppp.rx.buflevel--;
nixnax 142:54d1543e23e5 277 return x;
nixnax 142:54d1543e23e5 278 }
nixnax 142:54d1543e23e5 279
nixnax 142:54d1543e23e5 280 /// Dump a PPP frame to the debug serial port
nixnax 142:54d1543e23e5 281 /// Note - the hex output of dumpPPPFrame() can be imported into WireShark
nixnax 142:54d1543e23e5 282 /// Capture the frame's hex output in your terminal program and save as a text file
nixnax 164:c3de3d212c4b 283 /// In WireShark, use "Import Hex File". Import options are: Offset=None, Protocol=PPP.
nixnax 142:54d1543e23e5 284 void dumpPPPFrame()
nixnax 142:54d1543e23e5 285 {
nixnax 142:54d1543e23e5 286 char pbuf[30];
nixnax 142:54d1543e23e5 287 for(int i=0; i<ppp.pkt.len; i++) {
nixnax 142:54d1543e23e5 288 checkPc();
nixnax 142:54d1543e23e5 289 sprintf(pbuf, "%02x ", ppp.pkt.buf[i]);
nixnax 142:54d1543e23e5 290 checkPc();
nixnax 142:54d1543e23e5 291 putsWhileCheckingInput(pbuf);
nixnax 142:54d1543e23e5 292 }
nixnax 142:54d1543e23e5 293 checkPc();
nixnax 142:54d1543e23e5 294 sprintf(pbuf, " CRC=%04x Len=%d\n", ppp.pkt.crc, ppp.pkt.len);
nixnax 142:54d1543e23e5 295 checkPc();
nixnax 142:54d1543e23e5 296 putsWhileCheckingInput(pbuf);
nixnax 142:54d1543e23e5 297 }
nixnax 142:54d1543e23e5 298
nixnax 142:54d1543e23e5 299 /// Process a received PPP frame
nixnax 142:54d1543e23e5 300 void processPPPFrame(int start, int end)
nixnax 142:54d1543e23e5 301 {
nixnax 142:54d1543e23e5 302 led1Toggle(); // change led1 state on every frame we receive
nixnax 142:54d1543e23e5 303 if(start==end) {
nixnax 142:54d1543e23e5 304 return; // empty frame
nixnax 142:54d1543e23e5 305 }
nixnax 145:098b6ed2f7f2 306 fcsReset();
nixnax 142:54d1543e23e5 307 char * dest = ppp.pkt.buf;
nixnax 142:54d1543e23e5 308 ppp.pkt.len=0;
nixnax 142:54d1543e23e5 309 int unstuff=0;
nixnax 142:54d1543e23e5 310 int idx = start;
nixnax 142:54d1543e23e5 311 while(1) {
nixnax 142:54d1543e23e5 312 checkPc();
nixnax 142:54d1543e23e5 313 if (unstuff==0) {
nixnax 142:54d1543e23e5 314 if (ppp.rx.buf[idx]==0x7d) unstuff=1;
nixnax 142:54d1543e23e5 315 else {
nixnax 142:54d1543e23e5 316 *dest = ppp.rx.buf[idx];
nixnax 142:54d1543e23e5 317 ppp.pkt.len++;
nixnax 142:54d1543e23e5 318 dest++;
nixnax 145:098b6ed2f7f2 319 fcsDo(ppp.rx.buf[idx]);
nixnax 142:54d1543e23e5 320 }
nixnax 142:54d1543e23e5 321 } else { // unstuff characters prefixed with 0x7d
nixnax 142:54d1543e23e5 322 *dest = ppp.rx.buf[idx]^0x20;
nixnax 142:54d1543e23e5 323 ppp.pkt.len++;
nixnax 142:54d1543e23e5 324 dest++;
nixnax 145:098b6ed2f7f2 325 fcsDo(ppp.rx.buf[idx]^0x20);
nixnax 142:54d1543e23e5 326 unstuff=0;
nixnax 142:54d1543e23e5 327 }
nixnax 142:54d1543e23e5 328 idx = (idx+1) & (RXBUFLEN-1);
nixnax 142:54d1543e23e5 329 if (idx == end) break;
nixnax 142:54d1543e23e5 330 }
nixnax 145:098b6ed2f7f2 331 ppp.pkt.crc = ppp.fcs & 0xffff;
nixnax 142:54d1543e23e5 332 if(0) dumpPPPFrame(); // set to 1 to dump ALL ppp frames
nixnax 142:54d1543e23e5 333 if (ppp.pkt.crc == 0xf0b8) { // check for good CRC
nixnax 142:54d1543e23e5 334 determinePacketType();
nixnax 142:54d1543e23e5 335 } else {
nixnax 142:54d1543e23e5 336 #define REPORT_FCS_ERROR_YES
nixnax 142:54d1543e23e5 337 #ifdef REPORT_FCS_ERROR_YES
nixnax 142:54d1543e23e5 338 char pbuf[50]; // local print buffer
nixnax 142:54d1543e23e5 339 checkPc();
nixnax 142:54d1543e23e5 340 sprintf(pbuf, "\nPPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // print a debug line
nixnax 142:54d1543e23e5 341 checkPc();
nixnax 142:54d1543e23e5 342 putsWhileCheckingInput( pbuf );
nixnax 142:54d1543e23e5 343 if(0) dumpPPPFrame(); // set to 1 to dump frames with errors in them
nixnax 142:54d1543e23e5 344 #endif
nixnax 142:54d1543e23e5 345 }
nixnax 142:54d1543e23e5 346 }
nixnax 142:54d1543e23e5 347
nixnax 142:54d1543e23e5 348 /// output a character to the PPP port while checking for incoming characters
nixnax 142:54d1543e23e5 349 void pcPutcWhileCheckingInput(int ch)
nixnax 142:54d1543e23e5 350 {
nixnax 142:54d1543e23e5 351 checkPc(); // check input
nixnax 142:54d1543e23e5 352 pc.putc(ch);
nixnax 142:54d1543e23e5 353 checkPc();
nixnax 142:54d1543e23e5 354 }
nixnax 142:54d1543e23e5 355
nixnax 142:54d1543e23e5 356 /// do PPP HDLC-like handling of special (flag) characters
nixnax 143:c5019f856a56 357 void hdlcPut(int ch)
nixnax 142:54d1543e23e5 358 {
nixnax 142:54d1543e23e5 359 if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) ) {
nixnax 142:54d1543e23e5 360 pcPutcWhileCheckingInput(0x7d);
nixnax 142:54d1543e23e5 361 pcPutcWhileCheckingInput(ch^0x20); // these characters need special handling
nixnax 142:54d1543e23e5 362 } else {
nixnax 142:54d1543e23e5 363 pcPutcWhileCheckingInput(ch);
nixnax 142:54d1543e23e5 364 }
nixnax 142:54d1543e23e5 365 }
nixnax 142:54d1543e23e5 366
nixnax 142:54d1543e23e5 367 /// send a PPP frame in HDLC format
nixnax 144:01d98cf7738e 368 void sendPppFrame()
nixnax 143:c5019f856a56 369 {
nixnax 172:449dd7a28955 370 ppp.responseCounter++; // count the number of ppp frames we send
nixnax 145:098b6ed2f7f2 371 int crc = fcsBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc
nixnax 142:54d1543e23e5 372 ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc)
nixnax 142:54d1543e23e5 373 ppp.pkt.buf[ ppp.pkt.len-1 ] = (~crc>>8); // fcs hi (crc)
nixnax 142:54d1543e23e5 374 pcPutcWhileCheckingInput(0x7e); // hdlc start-of-frame "flag"
nixnax 142:54d1543e23e5 375 for(int i=0; i<ppp.pkt.len; i++) {
nixnax 142:54d1543e23e5 376 wait_us(86); // wait one character time
nixnax 142:54d1543e23e5 377 checkPc();
nixnax 142:54d1543e23e5 378 hdlcPut( ppp.pkt.buf[i] ); // send a character
nixnax 142:54d1543e23e5 379 }
nixnax 142:54d1543e23e5 380 pcPutcWhileCheckingInput(0x7e); // hdlc end-of-frame "flag"
nixnax 142:54d1543e23e5 381 }
nixnax 142:54d1543e23e5 382
nixnax 143:c5019f856a56 383 /// convert a network ip address in the buffer to an integer (IP adresses are big-endian, i.e most significant byte first)
nixnax 143:c5019f856a56 384 int bufferToIP(char * buffer)
nixnax 143:c5019f856a56 385 {
nixnax 143:c5019f856a56 386 int result=0;
nixnax 143:c5019f856a56 387 for(int i=0; i<4; i++) result = (result<<8)|(*buffer++ & 0xff);
nixnax 143:c5019f856a56 388 return result;
nixnax 143:c5019f856a56 389 }
nixnax 143:c5019f856a56 390
nixnax 177:dab9e685af53 391 /// convert 4-byte ip address to 32-bit
nixnax 179:ba2b2ddd0da5 392 unsigned int ip( int a, int b, int c, int d)
nixnax 179:ba2b2ddd0da5 393 {
nixnax 177:dab9e685af53 394 return a<<24 | b<<16 | c<<8 | d;
nixnax 177:dab9e685af53 395 }
nixnax 177:dab9e685af53 396
nixnax 142:54d1543e23e5 397 /// handle IPCP configuration requests
nixnax 142:54d1543e23e5 398 void ipcpConfigRequestHandler()
nixnax 142:54d1543e23e5 399 {
nixnax 142:54d1543e23e5 400 debugPrintf("Their IPCP Config Req, Our Ack\n");
nixnax 173:6774a0c851c4 401 if(ppp.ipcp->request[0]==3) {
nixnax 143:c5019f856a56 402 ppp.hostIP = bufferToIP(ppp.pkt.buf+10);
nixnax 173:6774a0c851c4 403 debugPrintf("Host IP = %d.%d.%d.%d (%08x)\n", ppp.ipcp->request[2],ppp.ipcp->request[3],ppp.ipcp->request[4],ppp.ipcp->request[5],ppp.hostIP);
nixnax 143:c5019f856a56 404 }
nixnax 143:c5019f856a56 405
nixnax 167:ff8a2d8beeb1 406 ppp.ipcp->code=2; // change code to ack
nixnax 144:01d98cf7738e 407 sendPppFrame(); // acknowledge everything they ask for - assume it's IP addresses
nixnax 142:54d1543e23e5 408
nixnax 142:54d1543e23e5 409 debugPrintf("Our IPCP Ask (no options)\n");
nixnax 167:ff8a2d8beeb1 410 ppp.ipcp->code=1; // change code to request
nixnax 167:ff8a2d8beeb1 411 ppp.ipcp->lengthR = __REV16( 4 ); // 4 is minimum length - no options in this request
nixnax 167:ff8a2d8beeb1 412 ppp.pkt.len=4+4+2; // no options in this request shortest ipcp packet possible (4 ppp + 4 ipcp + 2 crc)
nixnax 144:01d98cf7738e 413 sendPppFrame(); // send our request
nixnax 142:54d1543e23e5 414 }
nixnax 142:54d1543e23e5 415
nixnax 142:54d1543e23e5 416 /// handle IPCP acknowledge (do nothing)
nixnax 142:54d1543e23e5 417 void ipcpAckHandler()
nixnax 142:54d1543e23e5 418 {
nixnax 142:54d1543e23e5 419 debugPrintf("Their IPCP Grant\n");
nixnax 142:54d1543e23e5 420 }
nixnax 142:54d1543e23e5 421
nixnax 143:c5019f856a56 422 /// Handle IPCP NACK by sending our suggested IP address if there is an IP involved.
nixnax 143:c5019f856a56 423 /// This is how Linux responds to an IPCP request with no options - Windows assumes any IP address on the submnet is OK.
nixnax 142:54d1543e23e5 424 void ipcpNackHandler()
nixnax 142:54d1543e23e5 425 {
nixnax 146:a45f49a2b29c 426 debugPrintf("Their IPCP Nack\n");
nixnax 173:6774a0c851c4 427 if (ppp.ipcp->request[0]==3) { // check if the NACK contains an IP address parameter
nixnax 167:ff8a2d8beeb1 428 ppp.ipcp->code=1; // assume the NACK contains our "suggested" IP address
nixnax 144:01d98cf7738e 429 sendPppFrame(); // let's request this IP address as ours
nixnax 143:c5019f856a56 430 debugPrintf("Our IPCP ACK (received an IP)\n");
nixnax 146:a45f49a2b29c 431 } else { // if it's not an IP nack we ignore it
nixnax 146:a45f49a2b29c 432 debugPrintf("IPCP Nack Ignored\n");
nixnax 146:a45f49a2b29c 433 }
nixnax 142:54d1543e23e5 434 }
nixnax 142:54d1543e23e5 435
nixnax 142:54d1543e23e5 436 /// handle all other IPCP requests (by ignoring them)
nixnax 142:54d1543e23e5 437 void ipcpDefaultHandler()
nixnax 142:54d1543e23e5 438 {
nixnax 142:54d1543e23e5 439 debugPrintf("Their IPCP Other\n");
nixnax 142:54d1543e23e5 440 }
nixnax 142:54d1543e23e5 441
nixnax 142:54d1543e23e5 442 /// process an incoming IPCP packet
nixnax 142:54d1543e23e5 443 void IPCPframe()
nixnax 142:54d1543e23e5 444 {
nixnax 167:ff8a2d8beeb1 445 int action = ppp.ipcp->code; // packet type is here
nixnax 167:ff8a2d8beeb1 446 switch (action) {
nixnax 142:54d1543e23e5 447 case 1:
nixnax 142:54d1543e23e5 448 ipcpConfigRequestHandler();
nixnax 142:54d1543e23e5 449 break;
nixnax 142:54d1543e23e5 450 case 2:
nixnax 142:54d1543e23e5 451 ipcpAckHandler();
nixnax 142:54d1543e23e5 452 break;
nixnax 142:54d1543e23e5 453 case 3:
nixnax 142:54d1543e23e5 454 ipcpNackHandler();
nixnax 142:54d1543e23e5 455 break;
nixnax 142:54d1543e23e5 456 default:
nixnax 142:54d1543e23e5 457 ipcpDefaultHandler();
nixnax 142:54d1543e23e5 458 }
nixnax 142:54d1543e23e5 459 }
nixnax 142:54d1543e23e5 460
nixnax 142:54d1543e23e5 461 /// perform a 16-bit checksum. if the byte count is odd, stuff in an extra zero byte.
nixnax 161:d59f778bc8ab 462 unsigned int dataCheckSum(char * ptr, int len, int restart)
nixnax 142:54d1543e23e5 463 {
nixnax 159:4d1bf96a59cd 464 unsigned int i,hi,lo;
nixnax 142:54d1543e23e5 465 unsigned char placeHolder;
nixnax 161:d59f778bc8ab 466 if (restart) ppp.sum=0;
nixnax 142:54d1543e23e5 467 if (len&1) {
nixnax 142:54d1543e23e5 468 placeHolder = ptr[len];
nixnax 142:54d1543e23e5 469 ptr[len]=0; // if the byte count is odd, insert one extra zero byte is after the last real byte because we sum byte PAIRS
nixnax 142:54d1543e23e5 470 }
nixnax 142:54d1543e23e5 471 i=0;
nixnax 142:54d1543e23e5 472 while ( i<len ) {
nixnax 142:54d1543e23e5 473 checkPc();
nixnax 142:54d1543e23e5 474 hi = ptr[i++];
nixnax 142:54d1543e23e5 475 lo = ptr[i++];
nixnax 159:4d1bf96a59cd 476 ppp.sum = ppp.sum + ((hi<<8)|lo);
nixnax 142:54d1543e23e5 477 }
nixnax 142:54d1543e23e5 478 if (len&1) {
nixnax 142:54d1543e23e5 479 ptr[len] = placeHolder; // restore the extra byte we made zero
nixnax 142:54d1543e23e5 480 }
nixnax 159:4d1bf96a59cd 481 ppp.sum = (ppp.sum & 0xffff) + (ppp.sum>>16);
nixnax 159:4d1bf96a59cd 482 ppp.sum = (ppp.sum & 0xffff) + (ppp.sum>>16); // sum one more time to catch any carry from the carry
nixnax 159:4d1bf96a59cd 483 return ~ppp.sum;
nixnax 142:54d1543e23e5 484 }
nixnax 142:54d1543e23e5 485
nixnax 142:54d1543e23e5 486 /// perform the checksum on an IP header
nixnax 146:a45f49a2b29c 487 void IpHeaderCheckSum()
nixnax 142:54d1543e23e5 488 {
nixnax 161:d59f778bc8ab 489 ppp.ip->checksumR=0; // zero the checsum in the IP header
nixnax 161:d59f778bc8ab 490 int len = 4 * ppp.ip->headerLength; // length of IP header in bytes
nixnax 161:d59f778bc8ab 491 unsigned int sum = dataCheckSum(ppp.ipStart,len,1);
nixnax 161:d59f778bc8ab 492 ppp.ip->checksumR = __REV16( sum ); // insert fresh checksum
nixnax 146:a45f49a2b29c 493 }
nixnax 146:a45f49a2b29c 494
nixnax 147:0f40005cbc5f 495 /// swap the IP source and destination addresses
nixnax 147:0f40005cbc5f 496 void swapIpAddresses()
nixnax 147:0f40005cbc5f 497 {
nixnax 162:594729c0e5c1 498 unsigned int tempHold;
nixnax 162:594729c0e5c1 499 tempHold = ppp.ip->srcAdrR; // tempHold <- source IP
nixnax 162:594729c0e5c1 500 ppp.ip->srcAdrR = ppp.ip->dstAdrR; // source <- dest
nixnax 162:594729c0e5c1 501 ppp.ip->dstAdrR = tempHold; // dest <- tempHold*/
nixnax 147:0f40005cbc5f 502 }
nixnax 147:0f40005cbc5f 503
nixnax 149:969d98f6fb88 504 /// swap the IP source and destination ports
nixnax 149:969d98f6fb88 505 void swapIpPorts()
nixnax 149:969d98f6fb88 506 {
nixnax 173:6774a0c851c4 507 int headerSizeIP = 4 * (ppp.ip->headerLength); // calculate size of IP header
nixnax 173:6774a0c851c4 508 char * ipSrcPort = ppp.ipStart + headerSizeIP + 0; // ip source port location
nixnax 173:6774a0c851c4 509 char * ipDstPort = ppp.ipStart + headerSizeIP + 2; // ip destin port location
nixnax 149:969d98f6fb88 510 char tempHold[2];
nixnax 149:969d98f6fb88 511 memcpy(tempHold, ipSrcPort,2); // tempHold <- source
nixnax 149:969d98f6fb88 512 memcpy(ipSrcPort,ipDstPort,2); // source <- dest
nixnax 149:969d98f6fb88 513 memcpy(ipDstPort,tempHold, 2); // dest <- tempHold
nixnax 149:969d98f6fb88 514 }
nixnax 149:969d98f6fb88 515
nixnax 170:3d3b2126181c 516 /// Build the "pseudo header" required for UDP and TCP, then calculate its checksum
nixnax 170:3d3b2126181c 517 void checkSumPseudoHeader( unsigned int packetLength )
nixnax 170:3d3b2126181c 518 {
nixnax 170:3d3b2126181c 519 // this header contains the most important parts of the IP header, i.e. source and destination address, protocol number and data length.
nixnax 170:3d3b2126181c 520 pseudoIpHeaderType pseudoHeader; // create pseudo header
nixnax 170:3d3b2126181c 521 pseudoHeader.srcAdrR = ppp.ip->srcAdrR; // copy in ip source address
nixnax 170:3d3b2126181c 522 pseudoHeader.dstAdrR = ppp.ip->dstAdrR; // copy in ip dest address
nixnax 170:3d3b2126181c 523 pseudoHeader.zero = 0; // zero byte
nixnax 170:3d3b2126181c 524 pseudoHeader.protocol = ppp.ip->protocol; // protocol number (udp or tcp)
nixnax 170:3d3b2126181c 525 pseudoHeader.lengthR = __REV16( packetLength ); // size of tcp or udp packet
nixnax 170:3d3b2126181c 526 dataCheckSum(pseudoHeader.start, 12, 1); // calculate this header's checksum
nixnax 170:3d3b2126181c 527 }
nixnax 170:3d3b2126181c 528
nixnax 171:46b36d4edd1d 529 /// initialize an IP packet to send
nixnax 171:46b36d4edd1d 530 void initIP (unsigned int srcIp, unsigned int dstIp, unsigned int srcPort, unsigned int dstPort, unsigned int protocol)
nixnax 171:46b36d4edd1d 531 {
nixnax 171:46b36d4edd1d 532 ppp.ppp->address = 0xff;
nixnax 171:46b36d4edd1d 533 ppp.ppp->control = 3;
nixnax 171:46b36d4edd1d 534 ppp.ppp->protocolR = __REV16( 0x0021 );
nixnax 171:46b36d4edd1d 535 ppp.ip->version = 4;
nixnax 171:46b36d4edd1d 536 ppp.ip->headerLength = 5; // 5 words = 20 bytes
nixnax 171:46b36d4edd1d 537 ppp.ip->identR = __REV16(ppp.ipData.ident++); // insert our ident
nixnax 171:46b36d4edd1d 538 ppp.ip->dontFragment=1;
nixnax 171:46b36d4edd1d 539 ppp.ip->ttl=128;
nixnax 171:46b36d4edd1d 540 ppp.ip->protocol = protocol; // udp
nixnax 171:46b36d4edd1d 541 ppp.ip->srcAdrR = __REV(srcIp);
nixnax 171:46b36d4edd1d 542 ppp.ip->dstAdrR = __REV(dstIp);
nixnax 171:46b36d4edd1d 543 ppp.udpStart = ppp.ipStart + 20; // calculate start of udp header
nixnax 171:46b36d4edd1d 544 ppp.udp->srcPortR = __REV16(srcPort); // source port
nixnax 171:46b36d4edd1d 545 ppp.udp->dstPortR = __REV16(dstPort); // dest port
nixnax 171:46b36d4edd1d 546 }
nixnax 171:46b36d4edd1d 547
nixnax 171:46b36d4edd1d 548
nixnax 171:46b36d4edd1d 549 /// Build a UDP packet from scratch
nixnax 171:46b36d4edd1d 550 void sendUdp(unsigned int srcIp, unsigned int dstIp, unsigned int srcPort, unsigned int dstPort, char * message,int msgLen)
nixnax 171:46b36d4edd1d 551 {
nixnax 171:46b36d4edd1d 552 struct {
nixnax 171:46b36d4edd1d 553 unsigned int ipAll; // length of entire ip packet
nixnax 171:46b36d4edd1d 554 unsigned int ipHeader; // length of ip header
nixnax 171:46b36d4edd1d 555 unsigned int udpAll; // length of entire udp packet
nixnax 171:46b36d4edd1d 556 unsigned int udpData; // length of udp data segment
nixnax 171:46b36d4edd1d 557 } len;
nixnax 171:46b36d4edd1d 558 len.ipHeader = 20; // ip header length
nixnax 171:46b36d4edd1d 559 len.udpData = msgLen; // udp data size
nixnax 171:46b36d4edd1d 560 len.udpAll = len.udpData+8; // update local udp packet length
nixnax 171:46b36d4edd1d 561 len.ipAll = len.ipHeader + len.udpAll; // update IP Length
nixnax 171:46b36d4edd1d 562 initIP(srcIp, dstIp, srcPort, dstPort, 17); // init a UDP packet
nixnax 171:46b36d4edd1d 563 ppp.ip->lengthR = __REV16(len.ipAll); // update IP length in buffer
nixnax 171:46b36d4edd1d 564 ppp.udpStart = ppp.ipStart + len.ipHeader; // calculate start of udp header
nixnax 171:46b36d4edd1d 565 memcpy( ppp.udp->data, message, len.udpData ); // copy the message to the buffer
nixnax 171:46b36d4edd1d 566 ppp.udp->lengthR = __REV16(len.udpAll); // update UDP length in buffer
nixnax 171:46b36d4edd1d 567 ppp.pkt.len = len.ipAll+2+4; // update ppp packet length
nixnax 171:46b36d4edd1d 568 IpHeaderCheckSum(); // refresh IP header checksum
nixnax 171:46b36d4edd1d 569 checkSumPseudoHeader( len.udpAll ); // get the UDP pseudo-header checksum
nixnax 171:46b36d4edd1d 570 ppp.udp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared
nixnax 171:46b36d4edd1d 571 unsigned int pseudoHeaderSum=dataCheckSum(ppp.udpStart,len.udpAll, 0); // continue the TCP checksum on the whole TCP packet
nixnax 171:46b36d4edd1d 572 ppp.udp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header
nixnax 171:46b36d4edd1d 573 sendPppFrame(); // send the UDP message back
nixnax 171:46b36d4edd1d 574 }
nixnax 171:46b36d4edd1d 575
nixnax 146:a45f49a2b29c 576 /// Process an incoming UDP packet.
nixnax 146:a45f49a2b29c 577 /// If the packet starts with the string "echo " or "test" we echo back a special packet
nixnax 146:a45f49a2b29c 578 void UDPpacket()
nixnax 146:a45f49a2b29c 579 {
nixnax 163:d1b4328e9f08 580 struct {
nixnax 163:d1b4328e9f08 581 unsigned int all; // length of entire ip packet
nixnax 163:d1b4328e9f08 582 unsigned int header; // length of ip header
nixnax 163:d1b4328e9f08 583 } ipLength;
nixnax 163:d1b4328e9f08 584
nixnax 163:d1b4328e9f08 585 struct {
nixnax 165:c47826d07e0d 586 unsigned int all; // length of entire udp packet
nixnax 163:d1b4328e9f08 587 unsigned int data; // length of udp data segment
nixnax 163:d1b4328e9f08 588 } udpLength;
nixnax 163:d1b4328e9f08 589
nixnax 163:d1b4328e9f08 590 ipLength.header = 4 * ppp.ip->headerLength; // length of ip header
nixnax 163:d1b4328e9f08 591 ppp.udpStart = ppp.ipStart + ipLength.header; // calculate start of udp header
nixnax 171:46b36d4edd1d 592 udpLength.all = __REV16( ppp.udp->lengthR ); // size of udp packet
nixnax 163:d1b4328e9f08 593 udpLength.data = udpLength.all - 8; // size of udp data
nixnax 147:0f40005cbc5f 594
nixnax 146:a45f49a2b29c 595 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 171:46b36d4edd1d 596 char * srcIP = ppp.ip->srcAdrPtr; // IP source
nixnax 171:46b36d4edd1d 597 char * dstIP = ppp.ip->dstAdrPtr; //IP destination
nixnax 163:d1b4328e9f08 598
nixnax 171:46b36d4edd1d 599 unsigned int udpSrcPort = __REV16( ppp.udp->srcPortR ); // integer of UDP source port
nixnax 171:46b36d4edd1d 600 unsigned int udpDstPort = __REV16( ppp.udp->dstPortR ); // integer of UDP dest port
nixnax 163:d1b4328e9f08 601
nixnax 163:d1b4328e9f08 602 if(v0) {
nixnax 163:d1b4328e9f08 603 debugPrintf("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],udpSrcPort);
nixnax 163:d1b4328e9f08 604 debugPrintf("%d.%d.%d.%d:%d ", dstIP[0],dstIP[1],dstIP[2],dstIP[3],udpDstPort);
nixnax 163:d1b4328e9f08 605 debugPrintf("Len %03d", udpLength);
nixnax 163:d1b4328e9f08 606 }
nixnax 146:a45f49a2b29c 607 if (v1) {
nixnax 163:d1b4328e9f08 608 int printSize = udpLength.data;
nixnax 146:a45f49a2b29c 609 if (printSize > 20) printSize = 20; // print only first 20 characters
nixnax 146:a45f49a2b29c 610 for (int i=0; i<printSize; i++) {
nixnax 163:d1b4328e9f08 611 char ch = ppp.udp->data[i];
nixnax 146:a45f49a2b29c 612 if (ch>31 && ch<127) {
nixnax 146:a45f49a2b29c 613 debugPrintf("%c", ch);
nixnax 146:a45f49a2b29c 614 } else {
nixnax 146:a45f49a2b29c 615 debugPrintf("_");
nixnax 146:a45f49a2b29c 616 }
nixnax 146:a45f49a2b29c 617 }
nixnax 146:a45f49a2b29c 618 }
nixnax 146:a45f49a2b29c 619 if (v0) debugPrintf("\n");
nixnax 146:a45f49a2b29c 620 #endif
nixnax 163:d1b4328e9f08 621 int echoFound = !strncmp(ppp.udp->data,"echo ",5); // true if UDP message starts with "echo "
nixnax 163:d1b4328e9f08 622 int testFound = !strncmp(ppp.udp->data,"test" ,4); // true if UDP message starts with "test"
nixnax 146:a45f49a2b29c 623 if ( (echoFound) || (testFound)) { // if the UDP message starts with "echo " or "test" we answer back
nixnax 146:a45f49a2b29c 624 if (echoFound) {
nixnax 171:46b36d4edd1d 625 swapIpAddresses(); // swap IP source and destination
nixnax 171:46b36d4edd1d 626 swapIpPorts(); // swap IP source and destination ports
nixnax 163:d1b4328e9f08 627 memcpy(ppp.udp->data,"Got{",4); // in the UDP data modify "echo" to "Got:"
nixnax 170:3d3b2126181c 628 int n=0;
nixnax 170:3d3b2126181c 629 n=n+sprintf(n+ppp.udp->data+udpLength.data, "} UDP Server: PPP-Blinky\n"); // an appendix
nixnax 168:c77eb908042a 630 udpLength.data = udpLength.data + n; // update udp data size with the size of the appendix
nixnax 171:46b36d4edd1d 631 // we may have changed data length, update all the lengths
nixnax 171:46b36d4edd1d 632 udpLength.all = udpLength.data+8; // update local udp packet length
nixnax 171:46b36d4edd1d 633 ipLength.all = ipLength.header + udpLength.all; // update IP Length
nixnax 171:46b36d4edd1d 634 ppp.ip->lengthR = __REV16(ipLength.all); // update IP length in buffer
nixnax 171:46b36d4edd1d 635 ppp.udp->lengthR = __REV16(udpLength.all); // update UDP length in buffer
nixnax 171:46b36d4edd1d 636 ppp.pkt.len = ipLength.all+2+4; // update ppp packet length
nixnax 171:46b36d4edd1d 637 IpHeaderCheckSum(); // refresh IP header checksum
nixnax 171:46b36d4edd1d 638 checkSumPseudoHeader( udpLength.all ); // get the UDP pseudo-header checksum
nixnax 171:46b36d4edd1d 639 ppp.udp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared
nixnax 171:46b36d4edd1d 640 unsigned int pseudoHeaderSum=dataCheckSum(ppp.udpStart,udpLength.all, 0); // continue the TCP checksum on the whole TCP packet
nixnax 171:46b36d4edd1d 641 ppp.udp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header
nixnax 171:46b36d4edd1d 642 sendPppFrame(); // send the UDP message back
nixnax 171:46b36d4edd1d 643 } else if ( testFound ) {
nixnax 171:46b36d4edd1d 644 unsigned int sI = __REV( ppp.ip->srcAdrR );
nixnax 171:46b36d4edd1d 645 unsigned int dI = __REV( ppp.ip->dstAdrR );
nixnax 171:46b36d4edd1d 646 unsigned int sp = __REV16( ppp.udp->srcPortR );
nixnax 171:46b36d4edd1d 647 unsigned int dp = __REV16( ppp.udp->dstPortR );
nixnax 172:449dd7a28955 648 int n=sprintf(ppp.pkt.buf+200,"Response Count %d\n", ppp.responseCounter);
nixnax 171:46b36d4edd1d 649 sendUdp(dI,sI,dp,sp,ppp.pkt.buf+200,n); // build a udp packet from the ground up
nixnax 146:a45f49a2b29c 650 }
nixnax 146:a45f49a2b29c 651 }
nixnax 142:54d1543e23e5 652 }
nixnax 142:54d1543e23e5 653
nixnax 174:e5a3f16421a5 654 /// UDP demo that sends a udp packet containing a character received from the second debug serial port.
nixnax 174:e5a3f16421a5 655 /// Sends a 48 byte IP/UDP header for every 1 byte of data so line-mode would probably be better.
nixnax 178:3f17aa0ce5ce 656 /// If you want ip packets from ppp blinky to be routed to other networks, ensure you have ip routing enabled.
nixnax 178:3f17aa0ce5ce 657 /// See http://www.wikihow.com/Enable-IP-Routing.
nixnax 179:ba2b2ddd0da5 658 /// Also ensure that the firewall on the receiving machine has the receiving UDP port (12345 in this example) enabled.
nixnax 179:ba2b2ddd0da5 659 /// The netcat UDP receive command on the remote host would be: nc -ul 12345
nixnax 174:e5a3f16421a5 660 void sendUdpData()
nixnax 174:e5a3f16421a5 661 {
nixnax 174:e5a3f16421a5 662 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 179:ba2b2ddd0da5 663 if (ppp.online) {
nixnax 179:ba2b2ddd0da5 664 if (xx.readable()) {
nixnax 179:ba2b2ddd0da5 665 char inchar = xx.getc();
nixnax 179:ba2b2ddd0da5 666 xx.putc( inchar ); // echo the received character on the debug serial port
nixnax 179:ba2b2ddd0da5 667 sendUdp(ip(172,10,10,2), ip(192,168,0,109), 1, 12345, &inchar, 1); // send a 1 byte UDP message to a remote machine at IP 192.168.0.109:12345
nixnax 179:ba2b2ddd0da5 668 }
nixnax 174:e5a3f16421a5 669 }
nixnax 174:e5a3f16421a5 670 #endif
nixnax 174:e5a3f16421a5 671 }
nixnax 174:e5a3f16421a5 672
nixnax 142:54d1543e23e5 673 /// handle a PING ICMP (internet control message protocol) packet
nixnax 142:54d1543e23e5 674 void ICMPpacket() // internet control message protocol
nixnax 142:54d1543e23e5 675 {
nixnax 165:c47826d07e0d 676 struct {
nixnax 165:c47826d07e0d 677 unsigned int all; // length of entire ip packet
nixnax 165:c47826d07e0d 678 unsigned int header; // length of ip header
nixnax 165:c47826d07e0d 679 } ipLength;
nixnax 165:c47826d07e0d 680 struct {
nixnax 165:c47826d07e0d 681 unsigned int all; // length of entire udp packet
nixnax 165:c47826d07e0d 682 unsigned int data; // length of udp data segment
nixnax 165:c47826d07e0d 683 } icmpLength;
nixnax 165:c47826d07e0d 684 ipLength.all = __REV16( ppp.ip->lengthR ); // length of ip packet
nixnax 165:c47826d07e0d 685 ipLength.header = 4 * ppp.ip->headerLength; // length of ip header
nixnax 165:c47826d07e0d 686 ppp.icmpStart = ppp.ipStart + ipLength.header; // calculate start of udp header
nixnax 165:c47826d07e0d 687 icmpLength.all = ipLength.all - ipLength.header; // length of icmp packet
nixnax 165:c47826d07e0d 688 icmpLength.data = icmpLength.all - 8; // length of icmp data
nixnax 142:54d1543e23e5 689 #define ICMP_TYPE_PING_REQUEST 8
nixnax 165:c47826d07e0d 690 if ( ppp.icmp->type == ICMP_TYPE_PING_REQUEST ) {
nixnax 165:c47826d07e0d 691 ppp.ip->ttl--; // decrement time to live (so we have to update header checksum)
nixnax 147:0f40005cbc5f 692 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 167:ff8a2d8beeb1 693 char * srcAdr = ppp.ip->srcAdrPtr;
nixnax 167:ff8a2d8beeb1 694 char * dstAdr = ppp.ip->dstAdrPtr;
nixnax 165:c47826d07e0d 695 int icmpIdent = __REV16( ppp.ip->identR ); // byte reversed - big endian
nixnax 165:c47826d07e0d 696 int icmpSequence = __REV16( ppp.icmp->sequenceR ); // byte reversed - big endian
nixnax 142:54d1543e23e5 697 if(1) {
nixnax 142:54d1543e23e5 698 char pbuf[50];
nixnax 142:54d1543e23e5 699 checkPc();
nixnax 142:54d1543e23e5 700 sprintf(pbuf, "ICMP PING %d.%d.%d.%d %d.%d.%d.%d ", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3],dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]);
nixnax 142:54d1543e23e5 701 putsWhileCheckingInput( pbuf );
nixnax 142:54d1543e23e5 702 checkPc();
nixnax 142:54d1543e23e5 703 sprintf(pbuf, "Ident %04x Sequence %04d \n",icmpIdent,icmpSequence);
nixnax 142:54d1543e23e5 704 checkPc();
nixnax 142:54d1543e23e5 705 putsWhileCheckingInput( pbuf );
nixnax 142:54d1543e23e5 706 }
nixnax 142:54d1543e23e5 707 #endif
nixnax 147:0f40005cbc5f 708 swapIpAddresses(); // swap the IP source and destination addresses
nixnax 147:0f40005cbc5f 709 IpHeaderCheckSum(); // new ip header checksum (required because we changed TTL)
nixnax 142:54d1543e23e5 710 #define ICMP_TYPE_ECHO_REPLY 0
nixnax 165:c47826d07e0d 711 ppp.icmp->type = ICMP_TYPE_ECHO_REPLY; // icmp echo reply
nixnax 165:c47826d07e0d 712 ppp.icmp->checkSumR = 0; // zero the checksum for recalculation
nixnax 165:c47826d07e0d 713 unsigned int sum = dataCheckSum(ppp.icmpStart, icmpLength.all, 1); // icmp checksum
nixnax 165:c47826d07e0d 714 ppp.icmp->checkSumR = __REV16( sum ); // save big-endian icmp checksum
nixnax 142:54d1543e23e5 715
nixnax 165:c47826d07e0d 716 int printSize = icmpLength.data; // exclude size of icmp header
nixnax 142:54d1543e23e5 717 if (printSize > 10) printSize = 10; // print up to 20 characters
nixnax 142:54d1543e23e5 718 if (0) {
nixnax 142:54d1543e23e5 719 for (int i=0; i<printSize; i++) {
nixnax 165:c47826d07e0d 720 char ch = ppp.icmp->data[i];
nixnax 142:54d1543e23e5 721 if (ch>31 && ch<127) {
nixnax 142:54d1543e23e5 722 putcWhileCheckingInput(ch);
nixnax 142:54d1543e23e5 723 } else {
nixnax 142:54d1543e23e5 724 putcWhileCheckingInput('_');
nixnax 142:54d1543e23e5 725 }
nixnax 142:54d1543e23e5 726 }
nixnax 142:54d1543e23e5 727 putcWhileCheckingInput('\n');
nixnax 142:54d1543e23e5 728 }
nixnax 144:01d98cf7738e 729 sendPppFrame(); // reply to the ping
nixnax 142:54d1543e23e5 730 } else {
nixnax 142:54d1543e23e5 731 if (v0) {
nixnax 165:c47826d07e0d 732 debugPrintf("ICMP type=%x \n", ppp.icmp->type);
nixnax 142:54d1543e23e5 733 }
nixnax 142:54d1543e23e5 734 }
nixnax 142:54d1543e23e5 735 }
nixnax 142:54d1543e23e5 736
nixnax 142:54d1543e23e5 737 /// handle an IGMP (internet group managment protocol) packet (by ignoring it)
nixnax 142:54d1543e23e5 738 void IGMPpacket()
nixnax 142:54d1543e23e5 739 {
nixnax 142:54d1543e23e5 740 if (v0) debugPrintf("IGMP type=%d \n", ppp.pkt.buf[28]);
nixnax 142:54d1543e23e5 741 }
nixnax 142:54d1543e23e5 742
nixnax 142:54d1543e23e5 743 /// dump the header of an IP pakcet on the (optional) debug serial port
nixnax 142:54d1543e23e5 744 void dumpHeaderIP (int outGoing)
nixnax 142:54d1543e23e5 745 {
nixnax 142:54d1543e23e5 746 #if defined(IP_HEADER_DUMP_YES) && defined(SERIAL_PORT_MONITOR_YES)
nixnax 142:54d1543e23e5 747 checkPc(); // we are expecting the first character of the next packet
nixnax 155:9c6a1d249e26 748 int IPv4Id = __REV16(ppp.ip->identR);
nixnax 152:025c73b6c0a9 749 char pbuf[80]; // local print buffer
nixnax 142:54d1543e23e5 750 int n=0;
nixnax 142:54d1543e23e5 751 n=n+sprintf(pbuf+n, outGoing ? "\x1b[34m" : "\x1b[30m" ); // VT100 color code, print black for incoming, blue for outgoing headers
nixnax 142:54d1543e23e5 752 n=n+sprintf(pbuf+n, "%05d ",IPv4Id); // IPv4Id is a good way to correlate our dumps with net monitor or wireshark traces
nixnax 142:54d1543e23e5 753 #define DUMP_FULL_IP_ADDRESS_YES
nixnax 142:54d1543e23e5 754 #ifdef DUMP_FULL_IP_ADDRESS_YES
nixnax 167:ff8a2d8beeb1 755 char * srcAdr = ppp.ip->srcAdrPtr;
nixnax 167:ff8a2d8beeb1 756 char * dstAdr = ppp.ip->dstAdrPtr;
nixnax 142:54d1543e23e5 757 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
nixnax 142:54d1543e23e5 758 #endif
nixnax 142:54d1543e23e5 759 putsWhileCheckingInput( pbuf );
nixnax 142:54d1543e23e5 760 #ifndef TCP_HEADER_DUMP_YES
nixnax 160:bd701ad564cb 761 putsWhileCheckingInput('\x1b[30m\n'); // if there's no TCP header dump we terminate the line with \n and VT100 code for black
nixnax 142:54d1543e23e5 762 #endif
nixnax 142:54d1543e23e5 763 #endif
nixnax 142:54d1543e23e5 764 }
nixnax 142:54d1543e23e5 765
nixnax 142:54d1543e23e5 766 /// dump a TCP header on the optional debug serial port
nixnax 142:54d1543e23e5 767 void dumpHeaderTCP(int outGoing)
nixnax 142:54d1543e23e5 768 {
nixnax 142:54d1543e23e5 769 #if defined(TCP_HEADER_DUMP_YES) && defined(SERIAL_PORT_MONITOR_YES)
nixnax 155:9c6a1d249e26 770 char flagString[9]; // text string presenting the 8 most important TCP flags
nixnax 142:54d1543e23e5 771 #define PRINT_ALL_TCP_FLAGS_YES
nixnax 142:54d1543e23e5 772 #ifdef PRINT_ALL_TCP_FLAGS_YES
nixnax 155:9c6a1d249e26 773 memset(flagString,'.', 8); // fill string with "........"
nixnax 156:163c23249731 774 if (ppp.tcp->flag.fin) flagString[7]='F';
nixnax 156:163c23249731 775 if (ppp.tcp->flag.syn) flagString[6]='S';
nixnax 156:163c23249731 776 if (ppp.tcp->flag.rst) flagString[5]='R';
nixnax 156:163c23249731 777 if (ppp.tcp->flag.psh) flagString[4]='P';
nixnax 156:163c23249731 778 if (ppp.tcp->flag.ack) flagString[3]='A';
nixnax 156:163c23249731 779 if (ppp.tcp->flag.urg) flagString[2]='U';
nixnax 156:163c23249731 780 if (ppp.tcp->flag.ece) flagString[1]='E';
nixnax 156:163c23249731 781 if (ppp.tcp->flag.cwr) flagString[0]='C';
nixnax 155:9c6a1d249e26 782 flagString[8]=0; // null terminate string
nixnax 142:54d1543e23e5 783 #else
nixnax 156:163c23249731 784 if (ppp.tcp->flag.ack) flagString[0]='A'; // choose only the most important flag to print
nixnax 156:163c23249731 785 if (ppp.tcp->flag.syn) flagString[0]='S';
nixnax 156:163c23249731 786 if (ppp.tcp->flag.fin) flagString[0]='F';
nixnax 156:163c23249731 787 if (ppp.tcp->flag.psh) flagString[0]='P';
nixnax 156:163c23249731 788 if (ppp.tcp->flag.rst) flagString[0]='R';
nixnax 155:9c6a1d249e26 789 flagString[1]=0; // null terminate string
nixnax 142:54d1543e23e5 790 #endif
nixnax 155:9c6a1d249e26 791 putsWhileCheckingInput( flagString );
nixnax 142:54d1543e23e5 792 #define EVERY_PACKET_ON_A_NEW_LINE_YES
nixnax 142:54d1543e23e5 793 #ifdef EVERY_PACKET_ON_A_NEW_LINE_YES
nixnax 142:54d1543e23e5 794 putsWhileCheckingInput("\x1b[30m\n"); // write a black color and newline after every packet
nixnax 160:bd701ad564cb 795 #else
nixnax 160:bd701ad564cb 796 putsWhileCheckingInput("\x1b[30m"); // write a black color after every packet
nixnax 142:54d1543e23e5 797 #endif
nixnax 156:163c23249731 798 if( outGoing && ppp.tcp->flag.fin ) { // ACK/FIN - if this is an outgoing FIN it's the end of a tcp conversation
nixnax 160:bd701ad564cb 799 putcWhileCheckingInput('\n'); // insert an extra new line to mark the end (except for final ack) of an HTTP conversation
nixnax 142:54d1543e23e5 800 }
nixnax 142:54d1543e23e5 801 #endif
nixnax 142:54d1543e23e5 802 }
nixnax 142:54d1543e23e5 803
nixnax 143:c5019f856a56 804 /// Encode a buffer in base-64
nixnax 184:4133f557d4d4 805 const static char lut [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
nixnax 142:54d1543e23e5 806 void enc64(char * in, char * out, int len)
nixnax 142:54d1543e23e5 807 {
nixnax 142:54d1543e23e5 808 int i,j,a,b,c;
nixnax 142:54d1543e23e5 809 i=0;
nixnax 142:54d1543e23e5 810 j=0;
nixnax 142:54d1543e23e5 811 while(1) {
nixnax 142:54d1543e23e5 812 if (i<len) {
nixnax 142:54d1543e23e5 813 a = in[i++];
nixnax 142:54d1543e23e5 814 out[j++] = lut[ ( (a >> 2) & 0x3f) ];
nixnax 142:54d1543e23e5 815 } else break;
nixnax 142:54d1543e23e5 816 if (i<len) {
nixnax 142:54d1543e23e5 817 b = in[i++];
nixnax 142:54d1543e23e5 818 out[j++] = lut[ ( (a << 4) & 0x30) | ( (b >> 4) & 0x0f) ];
nixnax 142:54d1543e23e5 819 out[j++] = lut[ ( (b << 2) & 0x3c) ];
nixnax 142:54d1543e23e5 820 } else out[j++] = '=';
nixnax 142:54d1543e23e5 821 if (i<len) {
nixnax 142:54d1543e23e5 822 c = in[i++];
nixnax 142:54d1543e23e5 823 j--;
nixnax 142:54d1543e23e5 824 out[j++] = lut[ ( (b << 2) & 0x3c) | ( (c >> 6) & 0x03) ];
nixnax 142:54d1543e23e5 825 out[j++] = lut[ ( (c >> 0) & 0x3f) ];
nixnax 142:54d1543e23e5 826 } else out[j++] = '=';
nixnax 142:54d1543e23e5 827 }
nixnax 142:54d1543e23e5 828 out[j]=0;
nixnax 142:54d1543e23e5 829 }
nixnax 142:54d1543e23e5 830
nixnax 160:bd701ad564cb 831 /// Handle a request for an http websocket.
nixnax 160:bd701ad564cb 832 /// We end up here if we enter the following javascript in a web browser console: x = new WebSocket("ws://172.10.10.2");
nixnax 142:54d1543e23e5 833 int webSocketHandler(char * dataStart)
nixnax 142:54d1543e23e5 834 {
nixnax 142:54d1543e23e5 835 int n=0; // byte counter
nixnax 184:4133f557d4d4 836 char * key = strstr(dataStart, "Sec-WebSocket-Key:"); // search for the key in the payload
nixnax 142:54d1543e23e5 837 if (key != NULL) {
nixnax 184:4133f557d4d4 838 key = key + 18; // skip over the key ident string "Sec-WebSocket-Key:"
nixnax 142:54d1543e23e5 839 if (v0) putsWhileCheckingInput("WebSocket Request\n");
nixnax 184:4133f557d4d4 840 while ( strchr(lut, *key) == NULL) key++; // skip non-valid base-64 characters (whitespace)
nixnax 184:4133f557d4d4 841 char challenge [80];
nixnax 184:4133f557d4d4 842 int i=0;
nixnax 184:4133f557d4d4 843 char * copyTo = challenge;
nixnax 184:4133f557d4d4 844 while (strchr(lut, *key) != NULL) { // copy while we see valid base-64 characters
nixnax 184:4133f557d4d4 845 if (i++ >40) break; // prevent buffer overflow
nixnax 184:4133f557d4d4 846 *copyTo++ = *key++; // copy next valid base-64 character
nixnax 184:4133f557d4d4 847 }
nixnax 184:4133f557d4d4 848 strcpy(copyTo,"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); // append websocket gui code
nixnax 184:4133f557d4d4 849 if (0) debugPrintf("Challenge is %s\n", challenge); // the string we hash for the challenge
nixnax 142:54d1543e23e5 850 char shaOutput [20]; // sha1 output
nixnax 184:4133f557d4d4 851 sha1( shaOutput, challenge, strlen(challenge)); // hash the challenge
nixnax 142:54d1543e23e5 852 char encOut[50];
nixnax 184:4133f557d4d4 853 enc64( shaOutput, encOut, 20); // base-64 encode
nixnax 142:54d1543e23e5 854 char * versionstring = strstr(dataStart, "Sec-WebSocket-Version:");
nixnax 142:54d1543e23e5 855 char * version = challenge;
nixnax 184:4133f557d4d4 856 strncpy(version, versionstring,70); // copy their version string
nixnax 142:54d1543e23e5 857 *strchr(version,'\r')=0; // null terminate so we can sprintf it
nixnax 142:54d1543e23e5 858 memset(dataStart,0,500); // blank out old data befor send the websocket response header
nixnax 142:54d1543e23e5 859 n=n+sprintf(dataStart+n, "HTTP/1.1 101 Switching Protocols\r\n");
nixnax 142:54d1543e23e5 860 n=n+sprintf(dataStart+n, "Upgrade: websocket\r\n");
nixnax 142:54d1543e23e5 861 n=n+sprintf(dataStart+n, "Connection: Upgrade\r\n");
nixnax 142:54d1543e23e5 862 n=n+sprintf(dataStart+n, "Sec-WebSocket-Accept: %s\r\n",encOut);
nixnax 142:54d1543e23e5 863 n=n+sprintf(dataStart+n, "%s\r\n",version);
nixnax 142:54d1543e23e5 864 n=n+sprintf(dataStart+n, "mbed-Code: PPP-Blinky\r\n");
nixnax 142:54d1543e23e5 865 n=n+sprintf(dataStart+n, "\r\n"); // websocket response header ending
nixnax 142:54d1543e23e5 866 }
nixnax 142:54d1543e23e5 867 return n; // this response should satisfy a web browser's websocket protocol request
nixnax 142:54d1543e23e5 868 }
nixnax 142:54d1543e23e5 869
nixnax 142:54d1543e23e5 870 #define TCP_FLAG_ACK (1<<4)
nixnax 142:54d1543e23e5 871 #define TCP_FLAG_SYN (1<<1)
nixnax 142:54d1543e23e5 872 #define TCP_FLAG_PSH (1<<3)
nixnax 142:54d1543e23e5 873 #define TCP_FLAG_RST (1<<2)
nixnax 142:54d1543e23e5 874 #define TCP_FLAG_FIN (1<<0)
nixnax 142:54d1543e23e5 875
nixnax 142:54d1543e23e5 876 /// respond to an HTTP request
nixnax 185:4cd8f91e9d49 877 int httpResponse(char * dataStart, int * flags)
nixnax 142:54d1543e23e5 878 {
nixnax 142:54d1543e23e5 879 int n=0; // number of bytes we have printed so far
nixnax 142:54d1543e23e5 880 n = webSocketHandler( dataStart ); // test for and handle WebSocket upgrade requests
nixnax 160:bd701ad564cb 881 if (n>0) return n; // if n>0 we already have a response, so return
nixnax 142:54d1543e23e5 882
nixnax 142:54d1543e23e5 883 int nHeader; // byte size of HTTP header
nixnax 142:54d1543e23e5 884 int contentLengthStart; // index where HTML starts
plebs96 193:8d3c9e94a4fb 885 int httpGet5,httpGet6,httpGetx, httpGetRoot;
plebs96 193:8d3c9e94a4fb 886 int httpGetLedOff,httpGetLedOn; // temporary storage of strncmp results
nixnax 185:4cd8f91e9d49 887 *flags = TCP_FLAG_ACK | TCP_FLAG_FIN; // the default case is that we close the connection
nixnax 142:54d1543e23e5 888
nixnax 142:54d1543e23e5 889 httpGetRoot = strncmp(dataStart, "GET / HTTP/1.", 13); // found a GET to the root directory
nixnax 142:54d1543e23e5 890 httpGetx = strncmp(dataStart, "GET /x", 6); // found a GET to /x which we will treat special (anything starting with /x, e.g. /x, /xyz, /xABC?pqr=123
nixnax 142:54d1543e23e5 891 httpGet5 = dataStart[5]; // the first character in the path name, we use it for special functions later on
nixnax 142:54d1543e23e5 892 httpGet6 = dataStart[6]; // the second character in the path name, we use it for special functions later on
plebs96 193:8d3c9e94a4fb 893 httpGetLedOff = strncmp(dataStart,"button=0",9);
plebs96 193:8d3c9e94a4fb 894 httpGetLedOn = strncmp(dataStart,"button=1",9);
plebs96 193:8d3c9e94a4fb 895
nixnax 142:54d1543e23e5 896 // for example, you could try this using netcat (nc): echo "GET /x" | nc 172.10.10.2
nixnax 142:54d1543e23e5 897 if( (httpGetRoot==0) || (httpGetx==0) ) {
nixnax 142:54d1543e23e5 898 n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: mbed-PPP-Blinky-v1\r\n"); // 200 OK header
nixnax 142:54d1543e23e5 899 } else {
nixnax 142:54d1543e23e5 900 n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\nServer: mbed-PPP-Blinky\r\n"); // 404 header
nixnax 142:54d1543e23e5 901 }
plebs96 193:8d3c9e94a4fb 902
nixnax 142:54d1543e23e5 903 n=n+sprintf(n+dataStart,"Content-Length: "); // http header
nixnax 142:54d1543e23e5 904 contentLengthStart = n; // remember where Content-Length is in buffer
nixnax 142:54d1543e23e5 905 n=n+sprintf(n+dataStart,"?????\r\n"); // leave five spaces for content length - will be updated later
nixnax 142:54d1543e23e5 906 n=n+sprintf(n+dataStart,"Connection: close\r\n"); // close connection immediately
nixnax 142:54d1543e23e5 907 n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header must end with empty line (\r\n)
nixnax 142:54d1543e23e5 908 nHeader=n; // size of HTTP header
nixnax 185:4cd8f91e9d49 909
nixnax 142:54d1543e23e5 910 if( httpGetRoot == 0 ) {
nixnax 142:54d1543e23e5 911 // this is where we insert our web page into the buffer
plebs96 193:8d3c9e94a4fb 912 memcpy(n+dataStart,body,sizeof(body));
plebs96 193:8d3c9e94a4fb 913 n = n + sizeof(body)-1; // one less than sizeof because we don't count the null byte at the end
nixnax 185:4cd8f91e9d49 914
nixnax 142:54d1543e23e5 915 } else if ( (httpGet5 == 'w') && (httpGet6 == 's') ) { // "ws" is a special page for websocket demo
plebs96 193:8d3c9e94a4fb 916 memcpy(n+dataStart,body,sizeof(body));
plebs96 193:8d3c9e94a4fb 917 n = n + sizeof(body)-1; // one less than size
nixnax 185:4cd8f91e9d49 918 *flags = TCP_FLAG_ACK | TCP_FLAG_PSH; // for a websocket page we do NOT close the connection
plebs96 193:8d3c9e94a4fb 919 led1=!led1;
plebs96 193:8d3c9e94a4fb 920 }
plebs96 193:8d3c9e94a4fb 921
plebs96 193:8d3c9e94a4fb 922 else {
nixnax 142:54d1543e23e5 923 if (httpGetx == 0) { // the page request started with "GET /x" - here we treat anything starting with /x special:
nixnax 142:54d1543e23e5 924 #define W3C_COMPLIANT_RESPONSE_NO
nixnax 142:54d1543e23e5 925 // change the above to W3C_COMPLIANT_RESPONSE_YES if you want a W3C.org compliant HTTP response
nixnax 142:54d1543e23e5 926 #ifdef W3C_COMPLIANT_RESPONSE_YES
nixnax 142:54d1543e23e5 927 n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed PPP-Blinky</title>"); // html title (W3C.org required elements)
nixnax 146:a45f49a2b29c 928 n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.responseCounter); // body = the http frame count
nixnax 142:54d1543e23e5 929 #else
nixnax 142:54d1543e23e5 930 if( httpGet6 == 'b' ) { // if the fetched page is "xb" send a meta command to let the browser continuously reload
nixnax 167:ff8a2d8beeb1 931 n=n+sprintf(n+dataStart, "<meta http-equiv=\"refresh\" content=\"0\">"); // reload loop - handy for benchmarking
nixnax 142:54d1543e23e5 932 }
nixnax 142:54d1543e23e5 933 // /x is a very short page, in fact, it is only a decimal number showing the http Page count
nixnax 146:a45f49a2b29c 934 n=n+sprintf(n+dataStart,"%d ",ppp.responseCounter); // not really valid html but most browsers and curl are ok with it
nixnax 142:54d1543e23e5 935 #endif
nixnax 142:54d1543e23e5 936 } else {
nixnax 142:54d1543e23e5 937 // all other requests get 404 Not Found response with a http frame count - nice for debugging
nixnax 142:54d1543e23e5 938 n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed PPP-Blinky</title>"); // html title (required element)
nixnax 142:54d1543e23e5 939 n=n+sprintf(n+dataStart,"<body>Not Found</body>"); // not found message
nixnax 142:54d1543e23e5 940 }
nixnax 142:54d1543e23e5 941 }
plebs96 193:8d3c9e94a4fb 942 //add
plebs96 193:8d3c9e94a4fb 943 if(httpGetLedOn !=0)
plebs96 193:8d3c9e94a4fb 944 {
plebs96 193:8d3c9e94a4fb 945 led1 = 1;
plebs96 193:8d3c9e94a4fb 946 }
plebs96 193:8d3c9e94a4fb 947
plebs96 193:8d3c9e94a4fb 948 if(httpGetLedOff !=0)
plebs96 193:8d3c9e94a4fb 949 {
plebs96 193:8d3c9e94a4fb 950 led1 = 0;
plebs96 193:8d3c9e94a4fb 951 }
nixnax 142:54d1543e23e5 952 #define CONTENTLENGTHSIZE 5
nixnax 142:54d1543e23e5 953 char contentLengthString[CONTENTLENGTHSIZE+1];
nixnax 142:54d1543e23e5 954 snprintf(contentLengthString,CONTENTLENGTHSIZE+1,"%*d",CONTENTLENGTHSIZE,n-nHeader); // print Content-Length with leading spaces and fixed width equal to csize
nixnax 142:54d1543e23e5 955 memcpy(dataStart+contentLengthStart, contentLengthString, CONTENTLENGTHSIZE); // copy Content-Length to it's place in the send buffer
nixnax 142:54d1543e23e5 956 return n; // total byte size of our response
nixnax 142:54d1543e23e5 957 }
nixnax 142:54d1543e23e5 958
nixnax 160:bd701ad564cb 959 /// Handle TCP data that is not an HTTP GET.
nixnax 160:bd701ad564cb 960 /// This is handy when for example you want to use netcat (nc.exe) to talk to PPP-Blinky.
nixnax 160:bd701ad564cb 961 /// This could also be a websocket receive event - especially if the first byte is 0x81 (websocket data push)
nixnax 142:54d1543e23e5 962 int tcpResponse(char * dataStart, int len, int * outFlags)
nixnax 142:54d1543e23e5 963 {
nixnax 142:54d1543e23e5 964 int n=0; // number of bytes we have printed so far
nixnax 142:54d1543e23e5 965 if (dataStart[0] == 0x81) { // check if this is a websocket push message
nixnax 160:bd701ad564cb 966 char mask [4];
nixnax 160:bd701ad564cb 967 memcpy ( mask, dataStart+2, 4); // websocket messages are "masked", so first we obtain the 4-byte mask
nixnax 160:bd701ad564cb 968 int websocketMessageSize = len - 6; // 1 byte prefix (0x81), 1 byte, 4 bytes mask = 6 bytes
nixnax 160:bd701ad564cb 969 if((dataStart[1]&0x80)==0x80) // test if the mask bit is set, which means all data is xor'ed with the mask
nixnax 160:bd701ad564cb 970 for (int i=0; i<websocketMessageSize; i++) dataStart[i+6]^= mask[i%4]; // unmask each byte with one of the mask bytes
nixnax 160:bd701ad564cb 971 dataStart[1] = len-2; // add four extra bytes to the message length because we don't use mask bytes for the send
nixnax 160:bd701ad564cb 972 memcpy(dataStart+2, "Got:",4); // insert our own text into the four mask bytes
nixnax 160:bd701ad564cb 973 n = len; // our response size remains exactly the same length as what we received
nixnax 142:54d1543e23e5 974 } else if ( (dataStart[0]==0x88) && (dataStart[1]==0x80) && (len == 6) ) { // test for a websocket close request
nixnax 142:54d1543e23e5 975 n=2; // our close command is only two bytes long because we don't use the four mask bytes
nixnax 142:54d1543e23e5 976 dataStart[1]=0; // we don't have mask bytes on
nixnax 160:bd701ad564cb 977 } else {
nixnax 184:4133f557d4d4 978 if ( len > 1 ) { // we assume a length of 1 is a keep-alive or push packet
nixnax 184:4133f557d4d4 979 if (v1) putsWhileCheckingInput("TCP data received\n"); // all other tcp push packets
nixnax 184:4133f557d4d4 980 }
nixnax 163:d1b4328e9f08 981 }
nixnax 142:54d1543e23e5 982 return n; // total byte size of our response
nixnax 142:54d1543e23e5 983 }
nixnax 142:54d1543e23e5 984
nixnax 173:6774a0c851c4 985 /// dump the TCP data to the debug serial port
nixnax 173:6774a0c851c4 986 void dumpDataTCP(int outGoing)
nixnax 173:6774a0c851c4 987 {
nixnax 173:6774a0c851c4 988 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 173:6774a0c851c4 989 if (v2) {
nixnax 173:6774a0c851c4 990 int packetLengthIp = __REV16(ppp.ip->lengthR ); // size of ip packet
nixnax 173:6774a0c851c4 991 int headerSizeIp = 4 * ppp.ip->headerLength; // size of ip header
nixnax 173:6774a0c851c4 992 ppp.tcpStart = ppp.ipStart + headerSizeIp; // calculate where the TCP header starts
nixnax 173:6774a0c851c4 993 int headerSizeTcp = 4 * (ppp.tcp->offset); // tcp "offset" for start of data is also the header size
nixnax 173:6774a0c851c4 994 ppp.tcpData = ppp.tcpStart + headerSizeTcp; // start of tcp data
nixnax 173:6774a0c851c4 995 int tcpSize = packetLengthIp - headerSizeIp; // tcp size = size of ip payload
nixnax 173:6774a0c851c4 996 int tcpDataSize = tcpSize - headerSizeTcp; // size of data block after TCP header
nixnax 173:6774a0c851c4 997 char pbuf[80]; // local print buffer
nixnax 173:6774a0c851c4 998 int n=0;
nixnax 173:6774a0c851c4 999 checkPc();
nixnax 173:6774a0c851c4 1000 n=n+sprintf(pbuf+n, outGoing ? "\x1b[34m" : "\x1b[30m" ); // VT100 color code, print black for incoming, blue for outgoing headers
nixnax 173:6774a0c851c4 1001 checkPc();
nixnax 173:6774a0c851c4 1002 n=n+sprintf(pbuf+n, "IP:%d ipHeader:%d tcpHeader:%d tcpData:%d\n", packetLengthIp, headerSizeIp, headerSizeTcp, tcpDataSize); // 1 for more verbose
nixnax 184:4133f557d4d4 1003 if (n>70) putsWhileCheckingInput("n>pbuf overflow in dumpDataTCP()\n");
nixnax 173:6774a0c851c4 1004 checkPc();
nixnax 173:6774a0c851c4 1005 putsWhileCheckingInput( pbuf );
nixnax 173:6774a0c851c4 1006 if (tcpDataSize > 0) {
nixnax 173:6774a0c851c4 1007 ppp.tcpData[tcpDataSize]=0; // insert a null after the data so debug printf stops printing after the data
nixnax 173:6774a0c851c4 1008 putsWhileCheckingInput( ppp.tcpData ); // print the tcp payload data
nixnax 173:6774a0c851c4 1009 putsWhileCheckingInput("\n");
nixnax 173:6774a0c851c4 1010 }
nixnax 173:6774a0c851c4 1011 putsWhileCheckingInput( "\x1b[30m" ); // VT100 color code, print black
nixnax 173:6774a0c851c4 1012 }
nixnax 173:6774a0c851c4 1013 #endif
nixnax 173:6774a0c851c4 1014 }
nixnax 173:6774a0c851c4 1015
nixnax 142:54d1543e23e5 1016 /// handle an incoming TCP packet
nixnax 142:54d1543e23e5 1017 /// use the first few bytes to figure out if it's a websocket, an http request or just pure incoming TCP data
nixnax 142:54d1543e23e5 1018 void tcpHandler()
nixnax 142:54d1543e23e5 1019 {
nixnax 154:18b2bd92f557 1020 int packetLengthIp = __REV16(ppp.ip->lengthR ); // size of ip packet
nixnax 154:18b2bd92f557 1021 int headerSizeIp = 4 * ppp.ip->headerLength; // size of ip header
nixnax 173:6774a0c851c4 1022 ppp.tcpStart = ppp.ipStart + headerSizeIp; // calculate TCP header start
nixnax 154:18b2bd92f557 1023 int tcpSize = packetLengthIp - headerSizeIp; // tcp size = size of ip payload
nixnax 154:18b2bd92f557 1024 int headerSizeTcp = 4 * (ppp.tcp->offset); // tcp "offset" for start of data is also the header size
nixnax 173:6774a0c851c4 1025 char * tcpDataIn = ppp.tcpStart + headerSizeTcp; // start of data TCP data after TCP header
nixnax 154:18b2bd92f557 1026 int tcpDataSize = tcpSize - headerSizeTcp; // size of data block after TCP header
nixnax 154:18b2bd92f557 1027
nixnax 154:18b2bd92f557 1028 unsigned int seq_in = __REV(ppp.tcp->seqTcpR);
nixnax 154:18b2bd92f557 1029 unsigned int ack_in = __REV(ppp.tcp->ackTcpR);
nixnax 142:54d1543e23e5 1030 unsigned int ack_out = seq_in + tcpDataSize;
nixnax 142:54d1543e23e5 1031 unsigned int seq_out = ack_in; // use their version of our current sequence number
nixnax 142:54d1543e23e5 1032
nixnax 142:54d1543e23e5 1033 // first we shorten the TCP response header to only 20 bytes. This means we ignore all TCP option requests
nixnax 173:6774a0c851c4 1034 headerSizeIp=20;
nixnax 173:6774a0c851c4 1035 ppp.ip->headerLength = headerSizeIp/4; // ip header is 20 bytes long
nixnax 173:6774a0c851c4 1036 ppp.ip->lengthR = __REV(40); // 20 ip header + 20 tcp header
nixnax 173:6774a0c851c4 1037 //tcpSize = 20; // shorten total TCP packet size to 20 bytes (no data)
nixnax 154:18b2bd92f557 1038 headerSizeTcp = 20; // shorten outgoing TCP header size 20 bytes
nixnax 173:6774a0c851c4 1039 ppp.tcpStart = ppp.ipStart + headerSizeIp; // recalc TCP header start
nixnax 154:18b2bd92f557 1040 ppp.tcp->offset = (headerSizeTcp/4);
nixnax 173:6774a0c851c4 1041 char * tcpDataOut = ppp.tcpStart + headerSizeTcp; // start of outgoing data
nixnax 142:54d1543e23e5 1042
nixnax 142:54d1543e23e5 1043 int dataLen = 0; // most of our responses will have zero TCP data, only a header
nixnax 142:54d1543e23e5 1044 int flagsOut = TCP_FLAG_ACK; // the default case is an ACK packet
nixnax 160:bd701ad564cb 1045
nixnax 185:4cd8f91e9d49 1046 ppp.tcp->windowR = __REV16( 1200 ); // set tcp window size to 1200 bytes
nixnax 142:54d1543e23e5 1047
nixnax 142:54d1543e23e5 1048 // A sparse TCP flag interpreter that implements stateless TCP connections
nixnax 142:54d1543e23e5 1049
nixnax 156:163c23249731 1050 switch ( ppp.tcp->flag.All ) {
nixnax 142:54d1543e23e5 1051 case TCP_FLAG_SYN:
nixnax 142:54d1543e23e5 1052 flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // something wants to connect - acknowledge it
nixnax 142:54d1543e23e5 1053 seq_out = seq_in+0x10000000U; // create a new sequence number using their sequence as a starting point, increase the highest digit
nixnax 142:54d1543e23e5 1054 ack_out++; // for SYN flag we have to increase the sequence by 1
nixnax 142:54d1543e23e5 1055 break;
nixnax 184:4133f557d4d4 1056 case TCP_FLAG_ACK:
nixnax 142:54d1543e23e5 1057 case TCP_FLAG_ACK | TCP_FLAG_PSH:
nixnax 184:4133f557d4d4 1058 if ( (ppp.tcp->flag.All == TCP_FLAG_ACK) && (tcpDataSize == 0)) return; // handle zero-size ack messages by ignoring them
nixnax 142:54d1543e23e5 1059 if ( (strncmp(tcpDataIn, "GET /", 5) == 0) ) { // check for an http GET command
nixnax 179:ba2b2ddd0da5 1060 flagsOut = TCP_FLAG_ACK | TCP_FLAG_PSH; // we have data, set the PSH flag
nixnax 185:4cd8f91e9d49 1061 dataLen = httpResponse(tcpDataOut, &flagsOut); // send an http response
nixnax 142:54d1543e23e5 1062 } else {
nixnax 158:841592aed956 1063 dataLen = tcpResponse(tcpDataOut,tcpDataSize, &flagsOut); // not an http GET, handle as a tcp connection
nixnax 179:ba2b2ddd0da5 1064 if (dataLen > 0) flagsOut = TCP_FLAG_ACK | TCP_FLAG_PSH; // if we have any data set the PSH flag
nixnax 142:54d1543e23e5 1065 }
nixnax 142:54d1543e23e5 1066 break;
nixnax 142:54d1543e23e5 1067 case TCP_FLAG_FIN:
nixnax 142:54d1543e23e5 1068 case TCP_FLAG_FIN | TCP_FLAG_ACK:
nixnax 142:54d1543e23e5 1069 case TCP_FLAG_FIN | TCP_FLAG_PSH | TCP_FLAG_ACK:
nixnax 142:54d1543e23e5 1070 flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set outgoing FIN flag to ask them to close from their side
nixnax 142:54d1543e23e5 1071 ack_out++; // for FIN flag we have to increase the sequence by 1
nixnax 142:54d1543e23e5 1072 break;
nixnax 142:54d1543e23e5 1073 default:
nixnax 179:ba2b2ddd0da5 1074 return; // ignore all other packets
nixnax 142:54d1543e23e5 1075 } // switch
nixnax 142:54d1543e23e5 1076
nixnax 142:54d1543e23e5 1077 // The TCP flag handling is now done
nixnax 142:54d1543e23e5 1078 // first we swap source and destination TCP addresses and insert the new ack and seq numbers
nixnax 147:0f40005cbc5f 1079 swapIpAddresses(); // swap IP source and destination addresses
nixnax 149:969d98f6fb88 1080 swapIpPorts(); // swap IP source and destination ports
nixnax 142:54d1543e23e5 1081
nixnax 158:841592aed956 1082 ppp.tcp->ackTcpR = __REV( ack_out ); // byte reversed - tcp/ip messages are big-endian (high byte first)
nixnax 158:841592aed956 1083 ppp.tcp->seqTcpR = __REV( seq_out ); // byte reversed - tcp/ip messages are big-endian (high byte first)
nixnax 160:bd701ad564cb 1084
nixnax 156:163c23249731 1085 ppp.tcp->flag.All = flagsOut; // update the TCP flags
nixnax 142:54d1543e23e5 1086
nixnax 159:4d1bf96a59cd 1087 // recalculate all the header sizes
nixnax 154:18b2bd92f557 1088 tcpSize = headerSizeTcp + dataLen; // tcp packet size
nixnax 154:18b2bd92f557 1089 int newPacketSize = headerSizeIp + tcpSize; // calculate size of the outgoing packet
nixnax 159:4d1bf96a59cd 1090 ppp.ip->lengthR = __REV16 ( newPacketSize );
nixnax 142:54d1543e23e5 1091 ppp.pkt.len = newPacketSize+4+2; // ip packet length + 4-byte ppp prefix (ff 03 00 21) + 2 fcs (crc) bytes bytes at the end of the packet
nixnax 142:54d1543e23e5 1092
nixnax 142:54d1543e23e5 1093 // the header is all set up, now do the IP and TCP checksums
nixnax 146:a45f49a2b29c 1094 IpHeaderCheckSum(); // calculate new IP header checksum
nixnax 170:3d3b2126181c 1095 checkSumPseudoHeader( tcpSize ); // get the TCP pseudo-header checksum
nixnax 159:4d1bf96a59cd 1096 ppp.tcp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared
nixnax 161:d59f778bc8ab 1097 unsigned int pseudoHeaderSum=dataCheckSum(ppp.tcpStart,tcpSize, 0); // continue the TCP checksum on the whole TCP packet
nixnax 159:4d1bf96a59cd 1098 ppp.tcp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header
nixnax 160:bd701ad564cb 1099
nixnax 170:3d3b2126181c 1100 dumpHeaderIP(1); // dump outgoing IP header before sending the frame
nixnax 170:3d3b2126181c 1101 dumpHeaderTCP(1); // dump outgoing TCP header before sending the frame
nixnax 173:6774a0c851c4 1102 dumpDataTCP(1); // dump outgoing TCP data before sending the frame
nixnax 182:f039ec218c9f 1103 if (0) wait_ms(45); // 45 ms delay before sending frame - a typical internet delay time
nixnax 144:01d98cf7738e 1104 sendPppFrame(); // All preparation complete - send the TCP response
nixnax 142:54d1543e23e5 1105 if(0) dumpPPPFrame(); // set to 1 to dump transmitted ppp frame
nixnax 142:54d1543e23e5 1106 memset(ppp.pkt.buf+44,0,500); // flush out traces of previous data that we may scan for
nixnax 142:54d1543e23e5 1107 }
nixnax 142:54d1543e23e5 1108
nixnax 142:54d1543e23e5 1109 /// handle an incoming TCP packet
nixnax 142:54d1543e23e5 1110 void TCPpacket()
nixnax 142:54d1543e23e5 1111 {
nixnax 173:6774a0c851c4 1112 dumpHeaderIP(0); // dump incoming packet IP header
nixnax 173:6774a0c851c4 1113 dumpHeaderTCP(0); // dump incoming packet TCP header
nixnax 173:6774a0c851c4 1114 dumpDataTCP(0); // dump incoming packet data
nixnax 142:54d1543e23e5 1115 tcpHandler();
nixnax 142:54d1543e23e5 1116 }
nixnax 142:54d1543e23e5 1117
nixnax 142:54d1543e23e5 1118 /// handle the remaining IP protocols by ignoring them
nixnax 142:54d1543e23e5 1119 void otherProtocol()
nixnax 142:54d1543e23e5 1120 {
nixnax 142:54d1543e23e5 1121 debugPrintf("Other IP protocol");
nixnax 142:54d1543e23e5 1122 }
nixnax 142:54d1543e23e5 1123
nixnax 142:54d1543e23e5 1124 /// process an incoming IP packet
nixnax 142:54d1543e23e5 1125 void IPframe()
nixnax 142:54d1543e23e5 1126 {
nixnax 167:ff8a2d8beeb1 1127 int protocol = ppp.ip->protocol;
nixnax 142:54d1543e23e5 1128 switch (protocol) {
nixnax 142:54d1543e23e5 1129 case 1:
nixnax 142:54d1543e23e5 1130 ICMPpacket();
nixnax 142:54d1543e23e5 1131 break;
nixnax 142:54d1543e23e5 1132 case 2:
nixnax 142:54d1543e23e5 1133 IGMPpacket();
nixnax 142:54d1543e23e5 1134 break;
nixnax 142:54d1543e23e5 1135 case 17:
nixnax 142:54d1543e23e5 1136 UDPpacket();
nixnax 142:54d1543e23e5 1137 break;
nixnax 142:54d1543e23e5 1138 case 6:
nixnax 142:54d1543e23e5 1139 TCPpacket();
nixnax 142:54d1543e23e5 1140 break;
nixnax 142:54d1543e23e5 1141 default:
nixnax 142:54d1543e23e5 1142 otherProtocol();
nixnax 142:54d1543e23e5 1143 }
nixnax 142:54d1543e23e5 1144 }
nixnax 142:54d1543e23e5 1145
nixnax 142:54d1543e23e5 1146 /// respond to LCP (line configuration protocol) configuration request) by allowing no options
nixnax 142:54d1543e23e5 1147 void LCPconfReq()
nixnax 142:54d1543e23e5 1148 {
nixnax 142:54d1543e23e5 1149 debugPrintf("LCP Config ");
nixnax 175:b4e6f8a6fe00 1150 if ( ppp.lcp->lengthR != __REV16(4) ) {
nixnax 175:b4e6f8a6fe00 1151 ppp.lcp->code=4; // allow only "no options" which means Maximum Receive Unit (MRU) is default 1500 bytes
nixnax 142:54d1543e23e5 1152 debugPrintf("Reject\n");
nixnax 144:01d98cf7738e 1153 sendPppFrame();
nixnax 142:54d1543e23e5 1154 } else {
nixnax 175:b4e6f8a6fe00 1155 ppp.lcp->code=2; // ack zero conf
nixnax 142:54d1543e23e5 1156 debugPrintf("Ack\n");
nixnax 144:01d98cf7738e 1157 sendPppFrame();
nixnax 142:54d1543e23e5 1158 debugPrintf("LCP Ask\n");
nixnax 175:b4e6f8a6fe00 1159 ppp.lcp->code=1; // request no options
nixnax 144:01d98cf7738e 1160 sendPppFrame();
nixnax 142:54d1543e23e5 1161 }
nixnax 142:54d1543e23e5 1162 }
nixnax 142:54d1543e23e5 1163
nixnax 142:54d1543e23e5 1164 /// handle LCP acknowledge packets by ignoring them
nixnax 142:54d1543e23e5 1165 void LCPconfAck()
nixnax 142:54d1543e23e5 1166 {
nixnax 142:54d1543e23e5 1167 debugPrintf("LCP Ack\n");
nixnax 142:54d1543e23e5 1168 }
nixnax 142:54d1543e23e5 1169
nixnax 143:c5019f856a56 1170 /// handle LCP end (disconnect) packets by acknowledging them and by setting ppp.online to false
nixnax 142:54d1543e23e5 1171 void LCPend()
nixnax 142:54d1543e23e5 1172 {
nixnax 175:b4e6f8a6fe00 1173 ppp.lcp->code=6; // end
nixnax 144:01d98cf7738e 1174 sendPppFrame(); // acknowledge
nixnax 142:54d1543e23e5 1175 ppp.online=0; // start hunting for connect string again
nixnax 142:54d1543e23e5 1176 pppInitStruct(); // flush the receive buffer
nixnax 143:c5019f856a56 1177 debugPrintf("LCP End (Disconnect from host)\n");
nixnax 142:54d1543e23e5 1178 }
nixnax 142:54d1543e23e5 1179
nixnax 142:54d1543e23e5 1180 /// respond to other LCP requests by ignoring them
nixnax 142:54d1543e23e5 1181 void LCPother()
nixnax 142:54d1543e23e5 1182 {
nixnax 142:54d1543e23e5 1183 debugPrintf("LCP Other\n");
nixnax 142:54d1543e23e5 1184 dumpPPPFrame();
nixnax 142:54d1543e23e5 1185 }
nixnax 142:54d1543e23e5 1186
nixnax 142:54d1543e23e5 1187 /// process incoming LCP packets
nixnax 142:54d1543e23e5 1188 void LCPframe()
nixnax 142:54d1543e23e5 1189 {
nixnax 175:b4e6f8a6fe00 1190 int code = ppp.lcp->code;
nixnax 142:54d1543e23e5 1191 switch (code) {
nixnax 142:54d1543e23e5 1192 case 1:
nixnax 142:54d1543e23e5 1193 LCPconfReq();
nixnax 142:54d1543e23e5 1194 break; // config request
nixnax 142:54d1543e23e5 1195 case 2:
nixnax 142:54d1543e23e5 1196 LCPconfAck();
nixnax 142:54d1543e23e5 1197 break; // config ack
nixnax 142:54d1543e23e5 1198 case 5:
nixnax 142:54d1543e23e5 1199 LCPend();
nixnax 142:54d1543e23e5 1200 break; // end connection
nixnax 142:54d1543e23e5 1201 default:
nixnax 142:54d1543e23e5 1202 LCPother();
nixnax 142:54d1543e23e5 1203 }
nixnax 142:54d1543e23e5 1204 }
nixnax 142:54d1543e23e5 1205
nixnax 142:54d1543e23e5 1206 /// discard packets that are not IP, IPCP, or LCP
nixnax 142:54d1543e23e5 1207 void discardedFrame()
nixnax 142:54d1543e23e5 1208 {
nixnax 142:54d1543e23e5 1209 if (v0) debugPrintf("Frame is not IP, IPCP or LCP: %02x %02x %02x %02x\n", ppp.pkt.buf[0],ppp.pkt.buf[1],ppp.pkt.buf[2],ppp.pkt.buf[3]);
nixnax 142:54d1543e23e5 1210 }
nixnax 142:54d1543e23e5 1211
nixnax 142:54d1543e23e5 1212 /// determine the packet type (IP, IPCP or LCP) of incoming packets
nixnax 142:54d1543e23e5 1213 void determinePacketType()
nixnax 142:54d1543e23e5 1214 {
nixnax 167:ff8a2d8beeb1 1215 if ( ppp.ppp->address != 0xff ) {
nixnax 167:ff8a2d8beeb1 1216 debugPrintf("Unexpected: PPP address != ff\n");
nixnax 142:54d1543e23e5 1217 return;
nixnax 142:54d1543e23e5 1218 }
nixnax 167:ff8a2d8beeb1 1219 if ( ppp.ppp->control != 3 ) {
nixnax 167:ff8a2d8beeb1 1220 debugPrintf("Unexpected: PPP control != 3\n");
nixnax 142:54d1543e23e5 1221 return;
nixnax 142:54d1543e23e5 1222 }
nixnax 167:ff8a2d8beeb1 1223 unsigned int protocol = __REV16( ppp.ppp->protocolR );
nixnax 167:ff8a2d8beeb1 1224 switch ( protocol ) {
nixnax 167:ff8a2d8beeb1 1225 case 0xc021:
nixnax 142:54d1543e23e5 1226 LCPframe();
nixnax 142:54d1543e23e5 1227 break; // link control
nixnax 167:ff8a2d8beeb1 1228 case 0x8021:
nixnax 142:54d1543e23e5 1229 IPCPframe();
nixnax 142:54d1543e23e5 1230 break; // IP control
nixnax 167:ff8a2d8beeb1 1231 case 0x0021:
nixnax 142:54d1543e23e5 1232 IPframe();
nixnax 142:54d1543e23e5 1233 break; // IP itself
nixnax 142:54d1543e23e5 1234 default:
nixnax 142:54d1543e23e5 1235 discardedFrame();
nixnax 142:54d1543e23e5 1236 }
nixnax 142:54d1543e23e5 1237 }
nixnax 142:54d1543e23e5 1238
nixnax 146:a45f49a2b29c 1239 /// a sniffer tool to assist in figuring out where in the code we are having characters in the input buffer
nixnax 146:a45f49a2b29c 1240 void sniff()
nixnax 146:a45f49a2b29c 1241 {
nixnax 146:a45f49a2b29c 1242 if ( pc.readable() ) putsWhileCheckingInput( "Sniff - Char available!\n" ); // if this prints anything it means there is a character in the serial receive buffer
nixnax 146:a45f49a2b29c 1243 }
nixnax 146:a45f49a2b29c 1244
nixnax 142:54d1543e23e5 1245 /// scan the PPP serial input stream for frame start markers
nixnax 143:c5019f856a56 1246 void waitForPppFrame()
nixnax 142:54d1543e23e5 1247 {
nixnax 142:54d1543e23e5 1248 while(1) {
nixnax 182:f039ec218c9f 1249 sendUdpData(); // handle received characters from the DEBUG TERMINAL
nixnax 142:54d1543e23e5 1250 if ( ppp.rx.head != ppp.rx.tail ) {
nixnax 142:54d1543e23e5 1251 int oldTail = ppp.rx.tail; // remember where the character is located in the buffer
nixnax 142:54d1543e23e5 1252 int rx = pc_getBuf(); // get the character
nixnax 142:54d1543e23e5 1253 if (rx==FRAME_7E) {
nixnax 142:54d1543e23e5 1254 if (ppp.firstFrame) { // is this the start of the first frame start
nixnax 142:54d1543e23e5 1255 ppp.rx.rtail = ppp.rx.tail; // update real-time tail with the virtual tail
nixnax 142:54d1543e23e5 1256 ppp.hdlc.frameStartIndex = ppp.rx.tail; // remember where first frame started
nixnax 181:bcfe7fe2142d 1257 ppp.firstFrame=0; // clear first frame flag
nixnax 142:54d1543e23e5 1258 } else {
nixnax 142:54d1543e23e5 1259 ppp.hdlc.frameEndIndex=oldTail; // mark the frame end character
nixnax 142:54d1543e23e5 1260 processPPPFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); // process the frame
nixnax 142:54d1543e23e5 1261 ppp.rx.rtail = ppp.rx.tail; // update real-time tail with the virtual tail
nixnax 142:54d1543e23e5 1262 ppp.hdlc.frameStartIndex = ppp.rx.tail; // remember where next frame started
nixnax 142:54d1543e23e5 1263 break;
nixnax 142:54d1543e23e5 1264 }
nixnax 142:54d1543e23e5 1265 }
nixnax 142:54d1543e23e5 1266 }
nixnax 142:54d1543e23e5 1267 }
nixnax 142:54d1543e23e5 1268 }
nixnax 142:54d1543e23e5 1269
nixnax 153:7993def8663f 1270 /// Wait for a dial-up modem connect command ("CLIENT") from the host PC, if found, we set ppp.online to true, which will start the IP packet scanner.
nixnax 153:7993def8663f 1271 // Note: a 0x7E in the input stream (ppp start of frame character) will also set ppp.online to true - see checkPc();
nixnax 153:7993def8663f 1272 void waitForPcConnectString()
nixnax 142:54d1543e23e5 1273 {
nixnax 142:54d1543e23e5 1274 while(ppp.online == 0) {
nixnax 142:54d1543e23e5 1275 checkPc(); // gather received characters
nixnax 142:54d1543e23e5 1276 // search for Windows Dialup Networking "Direct Connection Between Two Computers" expected connect string
nixnax 142:54d1543e23e5 1277 char * found1 = strstr( (char *)ppp.rx.buf, "CLIENT" );
nixnax 142:54d1543e23e5 1278 if (found1 != NULL) {
nixnax 142:54d1543e23e5 1279 // respond with Windows Dialup networking expected "Direct Connection Between Two Computers" response string
nixnax 143:c5019f856a56 1280 if (v0) debugPrintf("Connected: Found connect string \"CLIENT\", sent \"CLIENTSERVER\"\n");
nixnax 142:54d1543e23e5 1281 pc.puts("CLIENTSERVER");
nixnax 142:54d1543e23e5 1282 ppp.online=1; // we are connected - set flag so we stop looking for the connect string
nixnax 142:54d1543e23e5 1283 checkPc();
nixnax 142:54d1543e23e5 1284 }
nixnax 142:54d1543e23e5 1285 }
nixnax 142:54d1543e23e5 1286 }
nixnax 142:54d1543e23e5 1287
nixnax 144:01d98cf7738e 1288 /// Initialize PPP data structure and set serial port(s) baud rate(s)
nixnax 144:01d98cf7738e 1289 void initializePpp()
nixnax 142:54d1543e23e5 1290 {
plebs96 193:8d3c9e94a4fb 1291 int i = 0;
plebs96 193:8d3c9e94a4fb 1292 while(1)
plebs96 193:8d3c9e94a4fb 1293 {
plebs96 193:8d3c9e94a4fb 1294 debugPrintf("1111111111111\n");
plebs96 193:8d3c9e94a4fb 1295 i ++;
plebs96 193:8d3c9e94a4fb 1296 if(i==010000)
plebs96 193:8d3c9e94a4fb 1297 break;
plebs96 193:8d3c9e94a4fb 1298 }
nixnax 144:01d98cf7738e 1299 debugBaudRate(115200); // baud rate for (optional) debug serial port
nixnax 184:4133f557d4d4 1300 debugPrintf("\x1b[2J\x1b[H\x1b[30m");
nixnax 184:4133f557d4d4 1301 wait_ms(200); // a brief wait so a human can see the reset event
nixnax 184:4133f557d4d4 1302 debugPrintf("mbed PPP-Blinky HTTP & WebSocket server ready :)\n"); // VT100 codes for clear_screen, home, black_text - Tera Term is a handy VT100 terminal
nixnax 184:4133f557d4d4 1303
nixnax 179:ba2b2ddd0da5 1304 pppInitStruct(); // initialize all the variables/properties/buffers
nixnax 179:ba2b2ddd0da5 1305 pc.baud(115200); // pc serial port acting as a dial-up modem - for PPP traffic
nixnax 179:ba2b2ddd0da5 1306 pc.attach(&pppReceiveHandler, RawSerial::RxIrq); // set up serial port receive interrupt handler
nixnax 143:c5019f856a56 1307 }
plebs96 193:8d3c9e94a4fb 1308
plebs96 193:8d3c9e94a4fb 1309
plebs96 193:8d3c9e94a4fb 1310 void t_print(int i, char* process)
plebs96 193:8d3c9e94a4fb 1311 {
plebs96 193:8d3c9e94a4fb 1312
plebs96 193:8d3c9e94a4fb 1313 while(1)
plebs96 193:8d3c9e94a4fb 1314 {
plebs96 193:8d3c9e94a4fb 1315 printf("%s\n", *process);
plebs96 193:8d3c9e94a4fb 1316 i++;
plebs96 193:8d3c9e94a4fb 1317 led1=!led1;
plebs96 193:8d3c9e94a4fb 1318 wait(0.5);
plebs96 193:8d3c9e94a4fb 1319 if(i==i*3)
plebs96 193:8d3c9e94a4fb 1320 break;
plebs96 193:8d3c9e94a4fb 1321 }
plebs96 193:8d3c9e94a4fb 1322 }
plebs96 193:8d3c9e94a4fb 1323 void l_off(void)
plebs96 193:8d3c9e94a4fb 1324 {
plebs96 193:8d3c9e94a4fb 1325 led1 = 0;
plebs96 193:8d3c9e94a4fb 1326 }
plebs96 193:8d3c9e94a4fb 1327
plebs96 193:8d3c9e94a4fb 1328 void l_on(void)
plebs96 193:8d3c9e94a4fb 1329 {
plebs96 193:8d3c9e94a4fb 1330 led1 = 1;
plebs96 193:8d3c9e94a4fb 1331 }
plebs96 193:8d3c9e94a4fb 1332 void led_flash(void)
plebs96 193:8d3c9e94a4fb 1333 {
plebs96 193:8d3c9e94a4fb 1334 int num = 0;
plebs96 193:8d3c9e94a4fb 1335 for(num = 0; num < 20; num ++)
plebs96 193:8d3c9e94a4fb 1336 {
plebs96 193:8d3c9e94a4fb 1337 led1= !led1;
plebs96 193:8d3c9e94a4fb 1338 wait(1);
plebs96 193:8d3c9e94a4fb 1339 }
plebs96 193:8d3c9e94a4fb 1340 }