Small Internet Protocol Stack using a standard serial port.

Dependencies:   mbed

PPP-Blinky - TCP/IP Networking Over a Serial Port

Note: The source code is at the bottom of this page.

/media/uploads/nixnax/blinky-connected.gif
A Windows desktop showing PPP-Blinky in the network connections list.

Describe PPP-Blinky in Three Sentences

PPP-Blinky is a tiny library that enables Internet protocols (IPv4) to any mbed target hardware by using only a serial port.

The code runs on processors with as little as 8k RAM, for example the Nucleo-L053R8 board.

PPP-Blinky uses the industry-standard PPP (Point-to-Point) Protocol and a tiny "stateless" TCP/IP stack.

No Ethernet Port Required

No ethernet port is required - PPP-Blinky uses a serial port to send IP packets to your PC.

PPP-Blinky emulates a standard dial-up modem and therefore connects to Windows, Linux or Adroid machines.

The code runs on most ARM mbed platforms such as the LPC11U24 shown in the picture below:

/media/uploads/nixnax/blinky-to-laptop1.jpg mbed LPC11u24 acting as a webserver to a Windows laptop.

Webserver

The Webserver and WebSocket functions are ideal for building browser-based GUIs on mbed-enabled hardware.

PPP-Blinky's HTTP webserver works with most web clients such as Internet Explorer, Mozilla Firefox, Google Chrome, Safari, Curl, wget and Lynx as well as Microsoft Powershell Invoke-Webrequest command.

In the image below Firefox web browser displays the main web page embedded into PPP-Blinky's code:

/media/uploads/nixnax/ppp-blinky-firefox.jpg Firefox web browser displays a web page embedded into PPP-Blinky's code

WebSocket Service

WebSocket is the most popular protocol standard for real-time bidirectional TCP/IP communication between clients and servers.
In the image below a small Internet Explorer script has connected to PPP-Blinky's WebSocket Service.
A websocket message was then sent by the browser and was echoed back by the WebSocket, triggering the onmessage event in the script.
The WebSocket service enables bidirectional real-time interaction between PPP-Blinky and any element in the browser DOM via JavaScript.
If you already have PPP-Blinky up and running you can test your WebSocket service using this: http://jsfiddle.net/d26cyuh2/112/embedded/result
Websockets are ideal for building browser-based GUIs for mbed hardware.

/media/uploads/nixnax/ppp-blinky-websocke-2.gif

Trying PPP-Blinky on your mbed board

You will need an mbed-enabled hardware board: https://developer.mbed.org/platforms/

Establish a serial port connection between your host PC and your mbed board. The easiest way is to use mbed hardware with a USB serial debug port. I've tried the ST-Micro Nucleo-L476RG, Nucleo-L152RE, Nucleo-F401RE, Nucleo-L432KC, Nucleo-L053R8, mbed-LPC11U24 and mbed-LPC1768 boards and they all work out of the box. Use the mbed online compiler to compile the software for your target board. Save the compiled binary to your hardware.

Before establishing a network connection, you can verify the operation of the code by opening a terminal program such as Tera Term, and setting the baud rate of the COM port on your mbed board to 115200 baud. LED1 should toggle for every two 0x7E (~) (i.e. tilde) characters you type, as 0x7E is the PPP frame start/end marker. Don't forget to close the port when your'e done testing, or else Windows Dial-up Networking will report that the COM port is in use by another program when you try to connect.

Once you are certain that the serial port and firmware is working, proceed to creating a new network connection on your PC -see below.

Creating a Dial-up Connection in Windows

/media/uploads/nixnax/modem.jpg

Setting up Dial-Up Networking (DUN) on your Windows 7 or 8 PC is essentially a two-step process: First, you create a new modem device, because PPP-blinky partially emulates a standard Windows serial port modem device. Second, you create a new Internet connection (in practice, a new network adapter) which is associated with your new "modem".

Step-by-step description of how to configure Windows for PPP-Blinky here:

/users/nixnax/code/PPP-Blinky/wiki/Configuring-Windows-Dial-Up-Networking

There is also a screen on how to set up Linux dial-up networking near the bottom of this page.

Connecting to PPP-Blinky from your PC

Once Windows networking is configured you can establish a dial-up connection to your mbed board over the USB virtual com port.

The IP address you manually assigned to the new dial-up network adapter (172.10.10.1) functions as a gateway to any valid IP address on that subnet. In the screen capture below, I'm sending pings from the Windows 8 command line to my ST-Micro Nucleo-L476RG board over the USB virtual serial Port. I'm also using a second serial port and Tera Term to capture the debug output from a second serial port on the hardware. The optional debug output from the board prints out the IP source and destination address and the first few bytes of the data payload. Note that the source is the adapter IP address, (172.10.10.1 in this case) and the destination is some other address on that subnet - all packets to the subnet are sent to our mbed hardware. For example, you could also ping 172.10.10.123 or, if your PPP-Blinky is running, simply click on this link: http://172.10.10.123

/media/uploads/nixnax/ping-cap-3.gif

One Million Pings!

In the image below the ICMP ("ping") echo reply service was tested by sending one million pings to ppp-Blinky. This took over two hours.
The ping tool used on the Windows 8 PC was psping.exe from PsTools by Mark Russinovich - http://bit.ly/PingFast
The average reply time for a short ping (1 byte of payload data) was 11 milliseconds at 115200 baud on the $10 Nucleo-L053R8 board - barely enough time for 130 bytes to be sent over the port!

/media/uploads/nixnax/ppp-blinky-ping-results.jpg

Monitoring PPP-Blinky Packets

The image below is from a Microsoft Network Monitor 3.4 capture session.

Responses from PPP-Blinky are shown in blue.

Frame 2 - Internet Explorer at IP 172.10.10.1 (the Dial-Up Adapter IP) requests a TCP connection by sending an S (SYN) flag.
Frame 3 - PPP-Blinky at IP 172.10.10.2 responds with an ACK in frame 3. One direction of the link is now established.
Frame 4 - The PC acknowledges the SYN sent by PPP-Blinky in frame 3. The TCP link is now fully established.
Frame 5 - The browser "pushes" (P flag is set) an HTTP GET request to PPP-Blinky.
Frame 6 - PPP-Blinky responds with a standard HTTP response "pushes" (P flag set) back a small web page. It also sets the A (ACK) flag to acknowledge the message sent in frame 6.
Frame 7 - The PC acknowledges reception of the HTTP payload.
Frame 8 - The PC starts to shut down the TCP connection by sending a FIN flag.
Frame 9 - PPP-Blinky acknowledges the FIN request - the connection is now closed in one direction. It also sets a FIN flag in the response to request closure of the opposite direction of the connection.
Frame 10 - The PC acknowledges the FIN request. The closing of the TCP connection is now confirmed in both directions.

/media/uploads/nixnax/ms-network-monitor-http-get-1.gif

Debug Output

PPP-Blinky can output handy debug information to an optional second serial port.
The image below shows the debug output (Ident, Source, Destination, TCP Flags) for a complete HTTP conversation.
The PC messages are displayed in black. PPP-Blinky messages are blue.
Notice how PPP-blinky automatically inserts a blank line after each full HTTP conversation.

/media/uploads/nixnax/tcp-data-3.gif

Creating a Dial-Up Connection in Linux

The screen below shows the required pppd command to connect to PPP-Blinky from a Linux machine. This was much simpler than Windows! The USB serial port of the mbed LPC1768 board registered as /dev/ttyACM0 on my Linux box. Do a websearch on pppd if you want to learn more about pppd, the Linux PPP handler. Near the bottom of the screen below, two webpages are fetched (/ and /y) by using the curl command on the command line. Gnome Webkit and Firefox work fine, too. Also try echo GET / HTTP/1.1 | nc 172.10.10.2 which uses netcat, the "Swiss army knife" of networking tools. PPP-Blinky was also tested with ApacheBench, the Apache server benchmark software. After 100000 fetches, the mean page fetch rate was reported as 6 page fetches per second for a small page.

/media/uploads/nixnax/pppd-screen.png

Caveats

PPP Blinky is an extremely sparse implementation (1.5k lines) of HTTP,WebSocket,TCP, UDP, ICMP, IPCP and LCP over PPP, requiring around 8kB of RAM. The minimum functionality required to establish connectivity is implemented. These are often acceptable tradeoffs for embedded projects as well as a handy tool to learn the practical details of everyday networking implementations.

Committer:
nixnax
Date:
Sun Oct 18 17:30:29 2020 +0000
Revision:
275:bb2d582c20d6
Parent:
274:b1bc40deb637
Formatting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nixnax 182:f039ec218c9f 1 // PPP-Blinky - "The Most Basic Internet Thing"
nixnax 259:f694046fc06f 2 // A Tiny IPv4 Stack and HTTP Webserver Using Windows XP/7/8/10/Linux Dial-Up Networking Over A Serial Port.
nixnax 259:f694046fc06f 3 // Aka PPP over serial (PPPOS)
nixnax 142:54d1543e23e5 4 // Receives UDP packets and responds to ping (ICMP Echo requests)
nixnax 142:54d1543e23e5 5 // WebSocket Service - see https://en.wikipedia.org/wiki/WebSocket
nixnax 142:54d1543e23e5 6
nixnax 267:40ea40e040d2 7 // Copyright 2016/2017/2018/2019/2020 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 8
nixnax 142:54d1543e23e5 9 // Notes and Instructions
nixnax 142:54d1543e23e5 10 // http://bit.ly/PPP-Blinky-Instructions
nixnax 142:54d1543e23e5 11 // http://bit.ly/win-rasdial-config
nixnax 142:54d1543e23e5 12
nixnax 201:9fcf92e9f427 13 // Important - ensure that you are using the latest mbed firmware for your specific mbed target board
nixnax 201:9fcf92e9f427 14
nixnax 142:54d1543e23e5 15 // Handy reading material
nixnax 142:54d1543e23e5 16 // https://technet.microsoft.com/en-us/library/cc957992.aspx
nixnax 142:54d1543e23e5 17 // https://en.wikibooks.org/wiki/Serial_Programming/IP_Over_Serial_Connections
nixnax 142:54d1543e23e5 18 // http://atari.kensclassics.org/wcomlog.htm
nixnax 249:9ff0e20325f7 19 // https://en.wikipedia.org/wiki/Favicon (see also html5 <link> tag)
nixnax 142:54d1543e23e5 20
nixnax 142:54d1543e23e5 21 // Handy tools
nixnax 142:54d1543e23e5 22 // 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 23 // https://www.microsoft.com/en-us/download/details.aspx?id=4865 - Microsoft network monitor - real-time monitoring of PPP packets
nixnax 142:54d1543e23e5 24 // http://pingtester.net/ - nice tool for high rate ping testing
nixnax 142:54d1543e23e5 25 // 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 26 // https://technet.microsoft.com/en-us/sysinternals/pstools.aspx - psping for fast testing of ICMP ping function
nixnax 142:54d1543e23e5 27 // https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky
nixnax 142:54d1543e23e5 28 // 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 29
nixnax 142:54d1543e23e5 30 // Connecting PPP-Blinky to Linux
nixnax 142:54d1543e23e5 31 // PPP-Blinky can be made to talk to Linux - tested on Fedora - the following command, which uses pppd, works:
nixnax 142:54d1543e23e5 32 // pppd /dev/ttyACM0 115200 debug dump local passive noccp novj nodetach nocrtscts 172.10.10.1:172.10.10.2
nixnax 142:54d1543e23e5 33 // 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 34 // See also https://en.wikipedia.org/wiki/Point-to-Point_Protocol_daemon
nixnax 142:54d1543e23e5 35
nixnax 142:54d1543e23e5 36 // Special pages when PPP-Blinky is running
nixnax 142:54d1543e23e5 37 // 172.10.10.2 root page
nixnax 182:f039ec218c9f 38 // 172.10.10.2/x returns the number of ppp frames sent - this is handy for testing
nixnax 182:f039ec218c9f 39 // 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 40 // 172.10.10.2/ws a simple WebSocket demo
nixnax 182:f039ec218c9f 41 // http://jsfiddle.net/d26cyuh2/ more complete WebSocket demo in JSFiddle, showing cross-domain access
nixnax 207:28cd834b785d 42
nixnax 142:54d1543e23e5 43 // Ok, enough talking, time to check out some code!!
nixnax 142:54d1543e23e5 44
nixnax 142:54d1543e23e5 45 #include "ppp-blinky.h"
nixnax 142:54d1543e23e5 46
nixnax 142:54d1543e23e5 47 // The #define below enables/disables a second (OPTIONAL) serial port that prints out interesting diagnostic messages.
nixnax 146:a45f49a2b29c 48 // 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 49 // Using the second serial port will slow down packet response time
nixnax 142:54d1543e23e5 50 // Note - the LPC11U24 does NOT have a second serial port
nixnax 146:a45f49a2b29c 51
nixnax 183:6d3ba874b267 52 #define SERIAL_PORT_MONITOR_NO /* change to SERIAL_PORT_MONITOR_YES for debug messages */
nixnax 142:54d1543e23e5 53
nixnax 142:54d1543e23e5 54 // here we define the OPTIONAL, second debug serial port for various mbed target boards
nixnax 142:54d1543e23e5 55 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 142:54d1543e23e5 56 #if defined(TARGET_LPC1768)
nixnax 179:ba2b2ddd0da5 57 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 58 #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_L476RG) || defined(TARGET_NUCLEO_F401RE)
nixnax 179:ba2b2ddd0da5 59 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 196:8791e241be80 60 #elif defined(TARGET_NUCLEO_L432KC)
nixnax 196:8791e241be80 61 RawSerial xx(PA_9, PA_10); // Second serial port on NUCLEO-L432KC - 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 62 #elif defined(TARGET_LPC11U24)
nixnax 142:54d1543e23e5 63 #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 64 #elif defined (TARGET_KL46Z) || (TARGET_KL25Z)
nixnax 179:ba2b2ddd0da5 65 RawSerial xx(PTE0,PTE1); // Second serial port on FRDM-KL46Z board
nixnax 193:48072570f41d 66 #elif defined(TARGET_KW41Z)
nixnax 194:5b23ebfb3626 67 RawSerial xx(PTC3, PTC2); // Second serial port on FRDM-KW41Z board
nixnax 142:54d1543e23e5 68 #elif defined(YOUR_TARGET_BOARD_NAME_HERE)
nixnax 142:54d1543e23e5 69 // 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 226:4898247048c7 70 RawSerial xx(p9, p10); // change this to YOUR board's second serial port pin definition - and please send it to me if it works!!!
nixnax 142:54d1543e23e5 71 #else
nixnax 142:54d1543e23e5 72 #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 73 #endif
nixnax 179:ba2b2ddd0da5 74 #define debugPrintf(x...) xx.printf (x) /* if we have a serial port we can print debug messages */
nixnax 142:54d1543e23e5 75 #define debugPutc(x...) xx.putc(x)
nixnax 221:1d7c32d8256b 76 #define debugPuts(x...) xx.puts(x)
nixnax 142:54d1543e23e5 77 #define debugBaudRate(x...) xx.baud(x)
nixnax 142:54d1543e23e5 78 #else
nixnax 142:54d1543e23e5 79 // if we don't have a debug port the debug print functions do nothing
nixnax 142:54d1543e23e5 80 #define debugPrintf(x...) {}
nixnax 142:54d1543e23e5 81 #define debugPutc(x...) {}
nixnax 221:1d7c32d8256b 82 #define debugPuts(x...) {}
nixnax 142:54d1543e23e5 83 #define debugBaudRate(x...) {}
nixnax 142:54d1543e23e5 84 #endif
nixnax 142:54d1543e23e5 85
nixnax 142:54d1543e23e5 86 // verbosity flags used in debug printouts - change to 1 to see increasingly more detailed debug info.
nixnax 182:f039ec218c9f 87 #define v0 1
nixnax 142:54d1543e23e5 88 #define v1 0
nixnax 142:54d1543e23e5 89 #define v2 0
nixnax 182:f039ec218c9f 90 #define IP_HEADER_DUMP_YES /* YES for ip header dump */
nixnax 182:f039ec218c9f 91 #define TCP_HEADER_DUMP_YES /* YES for tcp header dump */
nixnax 226:4898247048c7 92 #define DUMP_RECEIVED_PPP_FRAMES_NO /* YES to dump received PPP frames */
nixnax 142:54d1543e23e5 93
nixnax 226:4898247048c7 94 // below is the webpage we serve when we get an HTTP request to root (/)
nixnax 226:4898247048c7 95 // keep size under ~1400 bytes to fit into a single PPP packet
nixnax 249:9ff0e20325f7 96 // the <link rel="icon" ...> tag stops browsers from asking for file favicon.ico
nixnax 142:54d1543e23e5 97
nixnax 142:54d1543e23e5 98 const static char rootWebPage[] = "\
nixnax 142:54d1543e23e5 99 <!DOCTYPE html>\
nixnax 142:54d1543e23e5 100 <html>\
nixnax 248:5aca1c98fc24 101 <!--Comment-->\
nixnax 142:54d1543e23e5 102 <head>\
nixnax 146:a45f49a2b29c 103 <title>mbed PPP-Blinky</title>\r\n\
nixnax 248:5aca1c98fc24 104 <link rel=\"icon\" href=\"data:image/gif;base64,R0lGODlhAQABAIABAP8AAAAAACwAAAAAAQABAAACAkQBADs=\">\
nixnax 226:4898247048c7 105 <style>\
nixnax 241:dece92cb7296 106 body {font-family:sans-serif; font-size:140%; text-align:center; color:#807070;}\
nixnax 248:5aca1c98fc24 107 </style>\r\n\
nixnax 142:54d1543e23e5 108 </head>\
nixnax 226:4898247048c7 109 <body>\
nixnax 142:54d1543e23e5 110 <h1>mbed PPP-Blinky Up and Running</h1>\
nixnax 142:54d1543e23e5 111 <h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\
nixnax 210:a2a651249a9a 112 <h1><a href=\"/w\">WebSocket Test</a></h1>\
nixnax 245:544cea761769 113 <h1><a href=\"/x?Parameter1=Example\">Benchmark 1</a></h1>\
nixnax 142:54d1543e23e5 114 <h1><a href=\"/xb\">Benchmark 2</a></h1>\
nixnax 257:1990a41a1e31 115 <h1><a href=\"http://bit.ly/websocket-demo-jsfiddle\">JSFiddle Demo</a></h1>\
nixnax 270:98099fbc46fb 116 <h1><a href=\"/xt\">Toggle LED</a></h1>\
nixnax 252:b3c8c5699153 117 <h1>TCP, LCP & Ping Count: 0000000000</h1>\
nixnax 254:7345c9a6f4c2 118 <h1>System Core Clock: 0000000000</h1>\
nixnax 241:dece92cb7296 119 </body>\
nixnax 271:06c2c2a4e0bf 120 </html>\r\n"; // size = 718 bytes + 1 null byte = 719 bytes
nixnax 142:54d1543e23e5 121
nixnax 167:ff8a2d8beeb1 122 // this is a websocket demo html page we serve when GET /ws is requested
nixnax 226:4898247048c7 123 // it contains some javascript to handle the websocket activities
nixnax 249:9ff0e20325f7 124 // the <link rel="icon" ...> tag stops browsers from asking for file favicon.ico
nixnax 142:54d1543e23e5 125 const static char webSocketPage[] = "\
nixnax 142:54d1543e23e5 126 <!DOCTYPE html>\
nixnax 142:54d1543e23e5 127 <html>\
nixnax 142:54d1543e23e5 128 <head>\
nixnax 226:4898247048c7 129 <title>mbed PPP-Blinky WebSocket</title>\
nixnax 248:5aca1c98fc24 130 <link rel=\"icon\" href=\"data:image/gif;base64,R0lGODlhAQABAIABAP8AAAAAACwAAAAAAQABAAACAkQBADs=\">\
nixnax 142:54d1543e23e5 131 <script>\
nixnax 142:54d1543e23e5 132 window.onload=function(){\
nixnax 142:54d1543e23e5 133 var url=\"ws://172.10.10.2\";\
nixnax 142:54d1543e23e5 134 var sts=document.getElementById(\"sts\");\
nixnax 142:54d1543e23e5 135 var btn=document.getElementById(\"btn\");\
nixnax 142:54d1543e23e5 136 var ctr=0;\
nixnax 142:54d1543e23e5 137 function show(text){sts.textContent=text;}\
nixnax 142:54d1543e23e5 138 btn.onclick=function(){\
nixnax 142:54d1543e23e5 139 if(btn.textContent==\"Connect\"){\
nixnax 142:54d1543e23e5 140 x=new WebSocket(url);\
nixnax 142:54d1543e23e5 141 x.onopen=function(){\
nixnax 226:4898247048c7 142 show(\"Connected to PPP-Blinky WebSocket service at: \"+url);\
nixnax 142:54d1543e23e5 143 btn.textContent=\"Send \\\"\"+ctr+\"\\\"\";\
nixnax 142:54d1543e23e5 144 };\
nixnax 226:4898247048c7 145 x.onclose=function(){show(\"WebSocket closed\");};\
nixnax 226:4898247048c7 146 x.onmessage=function(msg){show(\"PPP-Blinky WebSocket service responded with: \\\"\"+msg.data+\"\\\"\");};\
nixnax 142:54d1543e23e5 147 } else {\
nixnax 142:54d1543e23e5 148 x.send(ctr);\
nixnax 142:54d1543e23e5 149 ctr=ctr+1;\
nixnax 142:54d1543e23e5 150 btn.textContent=\"Send \\\"\"+ctr+\"\\\"\";\
nixnax 142:54d1543e23e5 151 }\
nixnax 142:54d1543e23e5 152 };\
nixnax 142:54d1543e23e5 153 };\
nixnax 142:54d1543e23e5 154 </script>\
nixnax 226:4898247048c7 155 <style>\
nixnax 241:dece92cb7296 156 body {font-family:sans-serif; font-size:140%; text-align:center; color:#807070;}\
nixnax 241:dece92cb7296 157 button {font-size:140%; background-color:#7eeeee; border-radius:15px; border:none; margin-top:20px; }\
nixnax 226:4898247048c7 158 </style>\
nixnax 226:4898247048c7 159 <body>\
nixnax 226:4898247048c7 160 <h1>mbed PPP-Blinky WebSocket Test</h1>\
nixnax 226:4898247048c7 161 <h1><div id=\"sts\">Idle</div><h1>\
nixnax 226:4898247048c7 162 <button id=\"btn\">Connect</button>\
nixnax 142:54d1543e23e5 163 </body>\
nixnax 226:4898247048c7 164 </html>"; // size = 1037 bytes + 1 null byte = 1038 bytes
nixnax 142:54d1543e23e5 165
nixnax 142:54d1543e23e5 166 // 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 167 // On Windows the model type of the modem should be selected as "Communications cable between two computers"
nixnax 142:54d1543e23e5 168 // The modem baud rate should be set to 115200 baud
nixnax 142:54d1543e23e5 169 // See instructions at the top.
nixnax 142:54d1543e23e5 170 // 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 264:71c191a24d30 171 // Stream does not allow putc/getc in an interrupt routine, making serial interrupts useless. RawSerial allows use of putc/getc in interrupt.
nixnax 179:ba2b2ddd0da5 172 RawSerial pc (USBTX, USBRX); // usb virtual com port for mbed hardware
nixnax 142:54d1543e23e5 173
nixnax 142:54d1543e23e5 174 DigitalOut led1(LED1); // this led toggles when a packet is received
nixnax 142:54d1543e23e5 175
nixnax 142:54d1543e23e5 176 pppType ppp; // our global - definitely not thread safe
nixnax 142:54d1543e23e5 177
nixnax 142:54d1543e23e5 178 /// Initialize the ppp structure and clear the receive buffer
nixnax 142:54d1543e23e5 179 void pppInitStruct()
nixnax 142:54d1543e23e5 180 {
nixnax 223:917b554cb8a5 181 memset( (void *)ppp.rx.buf, 0, RXBUFLEN+1); // one extra byte at the end which always remains zero so strstr() cannot exceed the buffer
nixnax 142:54d1543e23e5 182 ppp.online=0;
nixnax 142:54d1543e23e5 183 ppp.rx.tail=0;
nixnax 142:54d1543e23e5 184 ppp.rx.rtail=0;
nixnax 142:54d1543e23e5 185 ppp.rx.head=0;
nixnax 142:54d1543e23e5 186 ppp.rx.buflevel=0;
nixnax 209:aa514cd631ee 187 ppp.rx.maxbuflevel=0;
nixnax 221:1d7c32d8256b 188 ppp.rx.bufferfull=0;
nixnax 142:54d1543e23e5 189 ppp.pkt.len=0;
nixnax 269:ad9e99728fdf 190 ppp.ipData.ident=11111; // easy to recognize in ip packet dumps
nixnax 142:54d1543e23e5 191 ppp.ledState=0;
nixnax 142:54d1543e23e5 192 ppp.hdlc.frameStartIndex=0;
nixnax 146:a45f49a2b29c 193 ppp.responseCounter=0;
nixnax 262:c044fed611aa 194 ppp.pppCount=0; // number of ICMP pings we have received
nixnax 142:54d1543e23e5 195 ppp.firstFrame=1;
nixnax 175:b4e6f8a6fe00 196 ppp.ppp = (pppHeaderType *)ppp.pkt.buf; // pointer to ppp header
nixnax 175:b4e6f8a6fe00 197 ppp.ip = (ipHeaderType *)(ppp.pkt.buf+4); // pointer to IP header
nixnax 142:54d1543e23e5 198 }
nixnax 142:54d1543e23e5 199
nixnax 275:bb2d582c20d6 200 /// Toggle the LED
nixnax 142:54d1543e23e5 201 void led1Toggle()
nixnax 142:54d1543e23e5 202 {
nixnax 263:83a6d767c349 203 led1 = (ppp.ledState >> 0) & 1; // use first bit, in other words toggle LED only every packet
nixnax 143:c5019f856a56 204 ppp.ledState++;
nixnax 142:54d1543e23e5 205 }
nixnax 142:54d1543e23e5 206
nixnax 179:ba2b2ddd0da5 207 /// Returns 1 after a connect message, 0 at startup or after a disconnect message
nixnax 150:3366bf3d294e 208 int connectedPpp()
nixnax 142:54d1543e23e5 209 {
nixnax 142:54d1543e23e5 210 return ppp.online;
nixnax 143:c5019f856a56 211 }
nixnax 142:54d1543e23e5 212
nixnax 179:ba2b2ddd0da5 213 /// PPP serial port receive interrupt handler.
nixnax 143:c5019f856a56 214 /// Check for available characters from the PC and read them into our own circular serial receive buffer at ppp.rx.buf.
nixnax 143:c5019f856a56 215 /// Also, if we are offline and a 0x7e frame start character is seen, we go online immediately
nixnax 179:ba2b2ddd0da5 216 void pppReceiveHandler()
nixnax 142:54d1543e23e5 217 {
nixnax 142:54d1543e23e5 218 char ch;
nixnax 179:ba2b2ddd0da5 219 while ( pc.readable() ) {
nixnax 223:917b554cb8a5 220 int localHead = (ppp.rx.head+1); // local head index
nixnax 223:917b554cb8a5 221 if (localHead > RXBUFLEN) localHead=0; // increment and wrap head index
nixnax 223:917b554cb8a5 222 if ( localHead == ppp.rx.rtail ) {
nixnax 221:1d7c32d8256b 223 ppp.rx.bufferfull = 1; // set flag if rx buffer is full
nixnax 252:b3c8c5699153 224 #define BUFFER_FULL_TRAP_YES
nixnax 258:1eb9abd66922 225 #ifdef BUFFER_FULL_TRAP_YES
nixnax 243:24f899297799 226 while(1) {
nixnax 270:98099fbc46fb 227 //led1Toggle(); // flash LED at a slow rate to indicate buffer overfloe
nixnax 258:1eb9abd66922 228 wait_ms(150);
nixnax 243:24f899297799 229 }
nixnax 258:1eb9abd66922 230 #else
nixnax 221:1d7c32d8256b 231 return; // don't read if rx buffer is full
nixnax 258:1eb9abd66922 232 #endif
nixnax 142:54d1543e23e5 233 }
nixnax 142:54d1543e23e5 234 ch = pc.getc(); // read new character
nixnax 142:54d1543e23e5 235 ppp.rx.buf[ppp.rx.head] = ch; // insert in our receive buffer
nixnax 209:aa514cd631ee 236 if (ch == 0x7E) { // check for ppp frame start character
nixnax 258:1eb9abd66922 237 ppp.online = 1; // if we see a frame sync character we immediately change status to online
nixnax 142:54d1543e23e5 238 }
nixnax 223:917b554cb8a5 239 ppp.rx.head = localHead; // update real head pointer
nixnax 221:1d7c32d8256b 240 ppp.rx.buflevel++; // update buffer level counter
nixnax 209:aa514cd631ee 241 if ( ppp.rx.buflevel > ppp.rx.maxbuflevel ) {
nixnax 221:1d7c32d8256b 242 ppp.rx.maxbuflevel = ppp.rx.buflevel; // remember the maximum usage of rx buffer
nixnax 209:aa514cd631ee 243 }
nixnax 142:54d1543e23e5 244 }
nixnax 142:54d1543e23e5 245 }
nixnax 142:54d1543e23e5 246
nixnax 145:098b6ed2f7f2 247 /// update the cumulative PPP FCS (frame check sequence)
nixnax 226:4898247048c7 248 void fcsDo(register int x)
nixnax 142:54d1543e23e5 249 {
nixnax 230:ee097c486e45 250 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 220:54f45f924707 251 x>>=1;
nixnax 230:ee097c486e45 252 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 220:54f45f924707 253 x>>=1;
nixnax 230:ee097c486e45 254 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 220:54f45f924707 255 x>>=1;
nixnax 230:ee097c486e45 256 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 220:54f45f924707 257 x>>=1;
nixnax 230:ee097c486e45 258 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 220:54f45f924707 259 x>>=1;
nixnax 230:ee097c486e45 260 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 220:54f45f924707 261 x>>=1;
nixnax 230:ee097c486e45 262 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 220:54f45f924707 263 x>>=1;
nixnax 230:ee097c486e45 264 ppp.fcs=((ppp.fcs^x)&1) ? (ppp.fcs>>1)^0x8408 : ppp.fcs>>1;
nixnax 142:54d1543e23e5 265 }
nixnax 142:54d1543e23e5 266
nixnax 145:098b6ed2f7f2 267 /// calculate the PPP FCS (frame check sequence) on an entire block of memory
nixnax 145:098b6ed2f7f2 268 int fcsBuf(char * buf, int size) // crc on an entire block of memory
nixnax 142:54d1543e23e5 269 {
nixnax 226:4898247048c7 270 ppp.fcs=0xffff; // fcs initial value
nixnax 145:098b6ed2f7f2 271 for(int i=0; i<size; i++)fcsDo(*buf++);
nixnax 145:098b6ed2f7f2 272 return ppp.fcs;
nixnax 142:54d1543e23e5 273 }
nixnax 142:54d1543e23e5 274
nixnax 142:54d1543e23e5 275 /// Get one character from our received PPP buffer
nixnax 142:54d1543e23e5 276 int pc_getBuf()
nixnax 142:54d1543e23e5 277 {
nixnax 142:54d1543e23e5 278 int x = ppp.rx.buf[ ppp.rx.tail ];
nixnax 230:ee097c486e45 279 int temptail = ppp.rx.tail+1;
nixnax 230:ee097c486e45 280 if (temptail > RXBUFLEN) temptail=0;
nixnax 142:54d1543e23e5 281 ppp.rx.buflevel--;
nixnax 230:ee097c486e45 282 ppp.rx.tail = temptail;
nixnax 142:54d1543e23e5 283 return x;
nixnax 142:54d1543e23e5 284 }
nixnax 142:54d1543e23e5 285
nixnax 142:54d1543e23e5 286 /// Dump a PPP frame to the debug serial port
nixnax 142:54d1543e23e5 287 /// Note - the hex output of dumpPPPFrame() can be imported into WireShark
nixnax 142:54d1543e23e5 288 /// Capture the frame's hex output in your terminal program and save as a text file
nixnax 164:c3de3d212c4b 289 /// In WireShark, use "Import Hex File". Import options are: Offset=None, Protocol=PPP.
nixnax 142:54d1543e23e5 290 void dumpPPPFrame()
nixnax 142:54d1543e23e5 291 {
nixnax 230:ee097c486e45 292 char pbuf[100];
nixnax 142:54d1543e23e5 293 for(int i=0; i<ppp.pkt.len; i++) {
nixnax 210:a2a651249a9a 294
nixnax 142:54d1543e23e5 295 sprintf(pbuf, "%02x ", ppp.pkt.buf[i]);
nixnax 210:a2a651249a9a 296
nixnax 221:1d7c32d8256b 297 debugPuts(pbuf);
nixnax 142:54d1543e23e5 298 }
nixnax 210:a2a651249a9a 299
nixnax 142:54d1543e23e5 300 sprintf(pbuf, " CRC=%04x Len=%d\n", ppp.pkt.crc, ppp.pkt.len);
nixnax 210:a2a651249a9a 301
nixnax 221:1d7c32d8256b 302 debugPuts(pbuf);
nixnax 142:54d1543e23e5 303 }
nixnax 142:54d1543e23e5 304
nixnax 142:54d1543e23e5 305 /// Process a received PPP frame
nixnax 142:54d1543e23e5 306 void processPPPFrame(int start, int end)
nixnax 142:54d1543e23e5 307 {
nixnax 262:c044fed611aa 308 if(start==end) return; // ignore empty frames
nixnax 268:a81648a9c7bf 309 // led1Toggle(); // change led1 state on received frames
nixnax 262:c044fed611aa 310 ppp.pppCount++; // count any non-empty frame
nixnax 226:4898247048c7 311 ppp.fcs=0xffff; // fcs initial value
nixnax 142:54d1543e23e5 312 char * dest = ppp.pkt.buf;
nixnax 142:54d1543e23e5 313 ppp.pkt.len=0;
nixnax 142:54d1543e23e5 314 int unstuff=0;
nixnax 142:54d1543e23e5 315 int idx = start;
nixnax 258:1eb9abd66922 316 char bufchar;
nixnax 142:54d1543e23e5 317 while(1) {
nixnax 258:1eb9abd66922 318 bufchar = ppp.rx.buf[idx];
nixnax 142:54d1543e23e5 319 if (unstuff==0) {
nixnax 258:1eb9abd66922 320 if (bufchar==0x7d) unstuff=1;
nixnax 142:54d1543e23e5 321 else {
nixnax 258:1eb9abd66922 322 *dest = bufchar;
nixnax 258:1eb9abd66922 323 fcsDo(*dest);
nixnax 258:1eb9abd66922 324 dest++;
nixnax 142:54d1543e23e5 325 ppp.pkt.len++;
nixnax 142:54d1543e23e5 326 }
nixnax 142:54d1543e23e5 327 } else { // unstuff characters prefixed with 0x7d
nixnax 258:1eb9abd66922 328 *dest = bufchar^0x20;
nixnax 258:1eb9abd66922 329 fcsDo(*dest);
nixnax 258:1eb9abd66922 330 dest++;
nixnax 142:54d1543e23e5 331 ppp.pkt.len++;
nixnax 142:54d1543e23e5 332 unstuff=0;
nixnax 142:54d1543e23e5 333 }
nixnax 216:e3c5a5b47295 334 idx = idx+1;
nixnax 216:e3c5a5b47295 335 if (idx > RXBUFLEN) idx=0;
nixnax 142:54d1543e23e5 336 if (idx == end) break;
nixnax 142:54d1543e23e5 337 }
nixnax 145:098b6ed2f7f2 338 ppp.pkt.crc = ppp.fcs & 0xffff;
nixnax 226:4898247048c7 339 #ifdef DUMP_RECEIVED_PPP_FRAMES_YES
nixnax 226:4898247048c7 340 dumpPPPFrame(); // hex dump ALL ppp frames to the debug port
nixnax 226:4898247048c7 341 #endif
nixnax 142:54d1543e23e5 342 if (ppp.pkt.crc == 0xf0b8) { // check for good CRC
nixnax 142:54d1543e23e5 343 determinePacketType();
nixnax 142:54d1543e23e5 344 } else {
nixnax 142:54d1543e23e5 345 #define REPORT_FCS_ERROR_YES
nixnax 142:54d1543e23e5 346 #ifdef REPORT_FCS_ERROR_YES
nixnax 230:ee097c486e45 347 char pbuf[100]; // local print buffer
nixnax 142:54d1543e23e5 348 sprintf(pbuf, "\nPPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // print a debug line
nixnax 221:1d7c32d8256b 349 debugPuts( pbuf );
nixnax 226:4898247048c7 350 #define DUMP_PPP_FRAME_ON_ERROR_NO
nixnax 226:4898247048c7 351 #ifdef DUMP_PPP_FRAME_ON_ERROR_YES
nixnax 226:4898247048c7 352 dumpPPPFrame(); // dump frames with errors in them
nixnax 226:4898247048c7 353 #endif
nixnax 142:54d1543e23e5 354 #endif
nixnax 142:54d1543e23e5 355 }
nixnax 142:54d1543e23e5 356 }
nixnax 142:54d1543e23e5 357
nixnax 142:54d1543e23e5 358 /// do PPP HDLC-like handling of special (flag) characters
nixnax 143:c5019f856a56 359 void hdlcPut(int ch)
nixnax 142:54d1543e23e5 360 {
nixnax 258:1eb9abd66922 361 if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) /* || (ch>0x7f) */ ) {
nixnax 226:4898247048c7 362 pc.putc(0x7d);
nixnax 226:4898247048c7 363 pc.putc(ch^0x20); // these characters need special handling
nixnax 142:54d1543e23e5 364 } else {
nixnax 226:4898247048c7 365 pc.putc(ch);
nixnax 142:54d1543e23e5 366 }
nixnax 142:54d1543e23e5 367 }
nixnax 142:54d1543e23e5 368
nixnax 142:54d1543e23e5 369 /// send a PPP frame in HDLC format
nixnax 144:01d98cf7738e 370 void sendPppFrame()
nixnax 143:c5019f856a56 371 {
nixnax 172:449dd7a28955 372 ppp.responseCounter++; // count the number of ppp frames we send
nixnax 145:098b6ed2f7f2 373 int crc = fcsBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc
nixnax 142:54d1543e23e5 374 ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc)
nixnax 142:54d1543e23e5 375 ppp.pkt.buf[ ppp.pkt.len-1 ] = (~crc>>8); // fcs hi (crc)
nixnax 226:4898247048c7 376 pc.putc(0x7e); // hdlc start-of-frame "flag"
nixnax 142:54d1543e23e5 377 for(int i=0; i<ppp.pkt.len; i++) {
nixnax 226:4898247048c7 378 // wait_us(86); // wait one character time
nixnax 210:a2a651249a9a 379
nixnax 142:54d1543e23e5 380 hdlcPut( ppp.pkt.buf[i] ); // send a character
nixnax 142:54d1543e23e5 381 }
nixnax 226:4898247048c7 382 pc.putc(0x7e); // hdlc end-of-frame "flag"
nixnax 142:54d1543e23e5 383 }
nixnax 142:54d1543e23e5 384
nixnax 143:c5019f856a56 385 /// 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 386 int bufferToIP(char * buffer)
nixnax 143:c5019f856a56 387 {
nixnax 143:c5019f856a56 388 int result=0;
nixnax 143:c5019f856a56 389 for(int i=0; i<4; i++) result = (result<<8)|(*buffer++ & 0xff);
nixnax 143:c5019f856a56 390 return result;
nixnax 143:c5019f856a56 391 }
nixnax 143:c5019f856a56 392
nixnax 177:dab9e685af53 393 /// convert 4-byte ip address to 32-bit
nixnax 228:7fb878500862 394 unsigned int integersToIp( int a, int b, int c, int d)
nixnax 179:ba2b2ddd0da5 395 {
nixnax 177:dab9e685af53 396 return a<<24 | b<<16 | c<<8 | d;
nixnax 177:dab9e685af53 397 }
nixnax 177:dab9e685af53 398
nixnax 142:54d1543e23e5 399 /// handle IPCP configuration requests
nixnax 142:54d1543e23e5 400 void ipcpConfigRequestHandler()
nixnax 142:54d1543e23e5 401 {
nixnax 230:ee097c486e45 402 debugPuts("Their IPCP Config Req, Our Ack\n");
nixnax 173:6774a0c851c4 403 if(ppp.ipcp->request[0]==3) {
nixnax 143:c5019f856a56 404 ppp.hostIP = bufferToIP(ppp.pkt.buf+10);
nixnax 173:6774a0c851c4 405 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 406 }
nixnax 143:c5019f856a56 407
nixnax 167:ff8a2d8beeb1 408 ppp.ipcp->code=2; // change code to ack
nixnax 144:01d98cf7738e 409 sendPppFrame(); // acknowledge everything they ask for - assume it's IP addresses
nixnax 142:54d1543e23e5 410
nixnax 230:ee097c486e45 411 debugPuts("Our IPCP Ask (no options)\n");
nixnax 167:ff8a2d8beeb1 412 ppp.ipcp->code=1; // change code to request
nixnax 167:ff8a2d8beeb1 413 ppp.ipcp->lengthR = __REV16( 4 ); // 4 is minimum length - no options in this request
nixnax 167:ff8a2d8beeb1 414 ppp.pkt.len=4+4+2; // no options in this request shortest ipcp packet possible (4 ppp + 4 ipcp + 2 crc)
nixnax 144:01d98cf7738e 415 sendPppFrame(); // send our request
nixnax 142:54d1543e23e5 416 }
nixnax 142:54d1543e23e5 417
nixnax 142:54d1543e23e5 418 /// handle IPCP acknowledge (do nothing)
nixnax 142:54d1543e23e5 419 void ipcpAckHandler()
nixnax 142:54d1543e23e5 420 {
nixnax 230:ee097c486e45 421 debugPuts("Their IPCP Grant\n");
nixnax 142:54d1543e23e5 422 }
nixnax 142:54d1543e23e5 423
nixnax 143:c5019f856a56 424 /// Handle IPCP NACK by sending our suggested IP address if there is an IP involved.
nixnax 143:c5019f856a56 425 /// 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 426 void ipcpNackHandler()
nixnax 142:54d1543e23e5 427 {
nixnax 230:ee097c486e45 428 debugPuts("Their IPCP Nack\n");
nixnax 173:6774a0c851c4 429 if (ppp.ipcp->request[0]==3) { // check if the NACK contains an IP address parameter
nixnax 167:ff8a2d8beeb1 430 ppp.ipcp->code=1; // assume the NACK contains our "suggested" IP address
nixnax 144:01d98cf7738e 431 sendPppFrame(); // let's request this IP address as ours
nixnax 230:ee097c486e45 432 debugPuts("Our IPCP ACK (received an IP)\n");
nixnax 146:a45f49a2b29c 433 } else { // if it's not an IP nack we ignore it
nixnax 230:ee097c486e45 434 debugPuts("IPCP Nack Ignored\n");
nixnax 146:a45f49a2b29c 435 }
nixnax 142:54d1543e23e5 436 }
nixnax 142:54d1543e23e5 437
nixnax 142:54d1543e23e5 438 /// handle all other IPCP requests (by ignoring them)
nixnax 142:54d1543e23e5 439 void ipcpDefaultHandler()
nixnax 142:54d1543e23e5 440 {
nixnax 230:ee097c486e45 441 debugPuts("Their IPCP Other\n");
nixnax 142:54d1543e23e5 442 }
nixnax 142:54d1543e23e5 443
nixnax 142:54d1543e23e5 444 /// process an incoming IPCP packet
nixnax 142:54d1543e23e5 445 void IPCPframe()
nixnax 142:54d1543e23e5 446 {
nixnax 167:ff8a2d8beeb1 447 int action = ppp.ipcp->code; // packet type is here
nixnax 167:ff8a2d8beeb1 448 switch (action) {
nixnax 142:54d1543e23e5 449 case 1:
nixnax 142:54d1543e23e5 450 ipcpConfigRequestHandler();
nixnax 142:54d1543e23e5 451 break;
nixnax 142:54d1543e23e5 452 case 2:
nixnax 142:54d1543e23e5 453 ipcpAckHandler();
nixnax 142:54d1543e23e5 454 break;
nixnax 142:54d1543e23e5 455 case 3:
nixnax 142:54d1543e23e5 456 ipcpNackHandler();
nixnax 142:54d1543e23e5 457 break;
nixnax 142:54d1543e23e5 458 default:
nixnax 142:54d1543e23e5 459 ipcpDefaultHandler();
nixnax 142:54d1543e23e5 460 }
nixnax 142:54d1543e23e5 461 }
nixnax 142:54d1543e23e5 462
nixnax 142:54d1543e23e5 463 /// perform a 16-bit checksum. if the byte count is odd, stuff in an extra zero byte.
nixnax 161:d59f778bc8ab 464 unsigned int dataCheckSum(char * ptr, int len, int restart)
nixnax 142:54d1543e23e5 465 {
nixnax 159:4d1bf96a59cd 466 unsigned int i,hi,lo;
nixnax 142:54d1543e23e5 467 unsigned char placeHolder;
nixnax 161:d59f778bc8ab 468 if (restart) ppp.sum=0;
nixnax 142:54d1543e23e5 469 if (len&1) {
nixnax 142:54d1543e23e5 470 placeHolder = ptr[len];
nixnax 142:54d1543e23e5 471 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 472 }
nixnax 142:54d1543e23e5 473 i=0;
nixnax 142:54d1543e23e5 474 while ( i<len ) {
nixnax 210:a2a651249a9a 475
nixnax 142:54d1543e23e5 476 hi = ptr[i++];
nixnax 142:54d1543e23e5 477 lo = ptr[i++];
nixnax 159:4d1bf96a59cd 478 ppp.sum = ppp.sum + ((hi<<8)|lo);
nixnax 142:54d1543e23e5 479 }
nixnax 142:54d1543e23e5 480 if (len&1) {
nixnax 142:54d1543e23e5 481 ptr[len] = placeHolder; // restore the extra byte we made zero
nixnax 142:54d1543e23e5 482 }
nixnax 159:4d1bf96a59cd 483 ppp.sum = (ppp.sum & 0xffff) + (ppp.sum>>16);
nixnax 159:4d1bf96a59cd 484 ppp.sum = (ppp.sum & 0xffff) + (ppp.sum>>16); // sum one more time to catch any carry from the carry
nixnax 159:4d1bf96a59cd 485 return ~ppp.sum;
nixnax 142:54d1543e23e5 486 }
nixnax 142:54d1543e23e5 487
nixnax 142:54d1543e23e5 488 /// perform the checksum on an IP header
nixnax 146:a45f49a2b29c 489 void IpHeaderCheckSum()
nixnax 142:54d1543e23e5 490 {
nixnax 161:d59f778bc8ab 491 ppp.ip->checksumR=0; // zero the checsum in the IP header
nixnax 161:d59f778bc8ab 492 int len = 4 * ppp.ip->headerLength; // length of IP header in bytes
nixnax 161:d59f778bc8ab 493 unsigned int sum = dataCheckSum(ppp.ipStart,len,1);
nixnax 161:d59f778bc8ab 494 ppp.ip->checksumR = __REV16( sum ); // insert fresh checksum
nixnax 146:a45f49a2b29c 495 }
nixnax 146:a45f49a2b29c 496
nixnax 147:0f40005cbc5f 497 /// swap the IP source and destination addresses
nixnax 147:0f40005cbc5f 498 void swapIpAddresses()
nixnax 147:0f40005cbc5f 499 {
nixnax 162:594729c0e5c1 500 unsigned int tempHold;
nixnax 162:594729c0e5c1 501 tempHold = ppp.ip->srcAdrR; // tempHold <- source IP
nixnax 162:594729c0e5c1 502 ppp.ip->srcAdrR = ppp.ip->dstAdrR; // source <- dest
nixnax 162:594729c0e5c1 503 ppp.ip->dstAdrR = tempHold; // dest <- tempHold*/
nixnax 147:0f40005cbc5f 504 }
nixnax 147:0f40005cbc5f 505
nixnax 149:969d98f6fb88 506 /// swap the IP source and destination ports
nixnax 149:969d98f6fb88 507 void swapIpPorts()
nixnax 149:969d98f6fb88 508 {
nixnax 173:6774a0c851c4 509 int headerSizeIP = 4 * (ppp.ip->headerLength); // calculate size of IP header
nixnax 173:6774a0c851c4 510 char * ipSrcPort = ppp.ipStart + headerSizeIP + 0; // ip source port location
nixnax 173:6774a0c851c4 511 char * ipDstPort = ppp.ipStart + headerSizeIP + 2; // ip destin port location
nixnax 149:969d98f6fb88 512 char tempHold[2];
nixnax 149:969d98f6fb88 513 memcpy(tempHold, ipSrcPort,2); // tempHold <- source
nixnax 149:969d98f6fb88 514 memcpy(ipSrcPort,ipDstPort,2); // source <- dest
nixnax 149:969d98f6fb88 515 memcpy(ipDstPort,tempHold, 2); // dest <- tempHold
nixnax 149:969d98f6fb88 516 }
nixnax 149:969d98f6fb88 517
nixnax 170:3d3b2126181c 518 /// Build the "pseudo header" required for UDP and TCP, then calculate its checksum
nixnax 170:3d3b2126181c 519 void checkSumPseudoHeader( unsigned int packetLength )
nixnax 170:3d3b2126181c 520 {
nixnax 170:3d3b2126181c 521 // 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 522 pseudoIpHeaderType pseudoHeader; // create pseudo header
nixnax 170:3d3b2126181c 523 pseudoHeader.srcAdrR = ppp.ip->srcAdrR; // copy in ip source address
nixnax 170:3d3b2126181c 524 pseudoHeader.dstAdrR = ppp.ip->dstAdrR; // copy in ip dest address
nixnax 170:3d3b2126181c 525 pseudoHeader.zero = 0; // zero byte
nixnax 170:3d3b2126181c 526 pseudoHeader.protocol = ppp.ip->protocol; // protocol number (udp or tcp)
nixnax 170:3d3b2126181c 527 pseudoHeader.lengthR = __REV16( packetLength ); // size of tcp or udp packet
nixnax 170:3d3b2126181c 528 dataCheckSum(pseudoHeader.start, 12, 1); // calculate this header's checksum
nixnax 170:3d3b2126181c 529 }
nixnax 170:3d3b2126181c 530
nixnax 171:46b36d4edd1d 531 /// initialize an IP packet to send
nixnax 171:46b36d4edd1d 532 void initIP (unsigned int srcIp, unsigned int dstIp, unsigned int srcPort, unsigned int dstPort, unsigned int protocol)
nixnax 171:46b36d4edd1d 533 {
nixnax 171:46b36d4edd1d 534 ppp.ppp->address = 0xff;
nixnax 171:46b36d4edd1d 535 ppp.ppp->control = 3;
nixnax 171:46b36d4edd1d 536 ppp.ppp->protocolR = __REV16( 0x0021 );
nixnax 171:46b36d4edd1d 537 ppp.ip->version = 4;
nixnax 171:46b36d4edd1d 538 ppp.ip->headerLength = 5; // 5 words = 20 bytes
nixnax 171:46b36d4edd1d 539 ppp.ip->identR = __REV16(ppp.ipData.ident++); // insert our ident
nixnax 171:46b36d4edd1d 540 ppp.ip->dontFragment=1;
nixnax 171:46b36d4edd1d 541 ppp.ip->ttl=128;
nixnax 171:46b36d4edd1d 542 ppp.ip->protocol = protocol; // udp
nixnax 171:46b36d4edd1d 543 ppp.ip->srcAdrR = __REV(srcIp);
nixnax 171:46b36d4edd1d 544 ppp.ip->dstAdrR = __REV(dstIp);
nixnax 171:46b36d4edd1d 545 ppp.udpStart = ppp.ipStart + 20; // calculate start of udp header
nixnax 171:46b36d4edd1d 546 ppp.udp->srcPortR = __REV16(srcPort); // source port
nixnax 171:46b36d4edd1d 547 ppp.udp->dstPortR = __REV16(dstPort); // dest port
nixnax 171:46b36d4edd1d 548 }
nixnax 171:46b36d4edd1d 549
nixnax 171:46b36d4edd1d 550
nixnax 171:46b36d4edd1d 551 /// Build a UDP packet from scratch
nixnax 171:46b36d4edd1d 552 void sendUdp(unsigned int srcIp, unsigned int dstIp, unsigned int srcPort, unsigned int dstPort, char * message,int msgLen)
nixnax 171:46b36d4edd1d 553 {
nixnax 171:46b36d4edd1d 554 struct {
nixnax 171:46b36d4edd1d 555 unsigned int ipAll; // length of entire ip packet
nixnax 171:46b36d4edd1d 556 unsigned int ipHeader; // length of ip header
nixnax 171:46b36d4edd1d 557 unsigned int udpAll; // length of entire udp packet
nixnax 171:46b36d4edd1d 558 unsigned int udpData; // length of udp data segment
nixnax 171:46b36d4edd1d 559 } len;
nixnax 171:46b36d4edd1d 560 len.ipHeader = 20; // ip header length
nixnax 171:46b36d4edd1d 561 len.udpData = msgLen; // udp data size
nixnax 171:46b36d4edd1d 562 len.udpAll = len.udpData+8; // update local udp packet length
nixnax 171:46b36d4edd1d 563 len.ipAll = len.ipHeader + len.udpAll; // update IP Length
nixnax 171:46b36d4edd1d 564 initIP(srcIp, dstIp, srcPort, dstPort, 17); // init a UDP packet
nixnax 171:46b36d4edd1d 565 ppp.ip->lengthR = __REV16(len.ipAll); // update IP length in buffer
nixnax 171:46b36d4edd1d 566 ppp.udpStart = ppp.ipStart + len.ipHeader; // calculate start of udp header
nixnax 171:46b36d4edd1d 567 memcpy( ppp.udp->data, message, len.udpData ); // copy the message to the buffer
nixnax 171:46b36d4edd1d 568 ppp.udp->lengthR = __REV16(len.udpAll); // update UDP length in buffer
nixnax 171:46b36d4edd1d 569 ppp.pkt.len = len.ipAll+2+4; // update ppp packet length
nixnax 171:46b36d4edd1d 570 IpHeaderCheckSum(); // refresh IP header checksum
nixnax 171:46b36d4edd1d 571 checkSumPseudoHeader( len.udpAll ); // get the UDP pseudo-header checksum
nixnax 171:46b36d4edd1d 572 ppp.udp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared
nixnax 171:46b36d4edd1d 573 unsigned int pseudoHeaderSum=dataCheckSum(ppp.udpStart,len.udpAll, 0); // continue the TCP checksum on the whole TCP packet
nixnax 171:46b36d4edd1d 574 ppp.udp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header
nixnax 171:46b36d4edd1d 575 sendPppFrame(); // send the UDP message back
nixnax 171:46b36d4edd1d 576 }
nixnax 171:46b36d4edd1d 577
nixnax 146:a45f49a2b29c 578 /// Process an incoming UDP packet.
nixnax 146:a45f49a2b29c 579 /// If the packet starts with the string "echo " or "test" we echo back a special packet
nixnax 146:a45f49a2b29c 580 void UDPpacket()
nixnax 146:a45f49a2b29c 581 {
nixnax 163:d1b4328e9f08 582 struct {
nixnax 163:d1b4328e9f08 583 unsigned int all; // length of entire ip packet
nixnax 163:d1b4328e9f08 584 unsigned int header; // length of ip header
nixnax 163:d1b4328e9f08 585 } ipLength;
nixnax 163:d1b4328e9f08 586
nixnax 163:d1b4328e9f08 587 struct {
nixnax 165:c47826d07e0d 588 unsigned int all; // length of entire udp packet
nixnax 163:d1b4328e9f08 589 unsigned int data; // length of udp data segment
nixnax 163:d1b4328e9f08 590 } udpLength;
nixnax 163:d1b4328e9f08 591
nixnax 163:d1b4328e9f08 592 ipLength.header = 4 * ppp.ip->headerLength; // length of ip header
nixnax 163:d1b4328e9f08 593 ppp.udpStart = ppp.ipStart + ipLength.header; // calculate start of udp header
nixnax 171:46b36d4edd1d 594 udpLength.all = __REV16( ppp.udp->lengthR ); // size of udp packet
nixnax 163:d1b4328e9f08 595 udpLength.data = udpLength.all - 8; // size of udp data
nixnax 147:0f40005cbc5f 596
nixnax 146:a45f49a2b29c 597 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 171:46b36d4edd1d 598 char * srcIP = ppp.ip->srcAdrPtr; // IP source
nixnax 171:46b36d4edd1d 599 char * dstIP = ppp.ip->dstAdrPtr; //IP destination
nixnax 163:d1b4328e9f08 600
nixnax 171:46b36d4edd1d 601 unsigned int udpSrcPort = __REV16( ppp.udp->srcPortR ); // integer of UDP source port
nixnax 171:46b36d4edd1d 602 unsigned int udpDstPort = __REV16( ppp.udp->dstPortR ); // integer of UDP dest port
nixnax 163:d1b4328e9f08 603
nixnax 163:d1b4328e9f08 604 if(v0) {
nixnax 163:d1b4328e9f08 605 debugPrintf("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],udpSrcPort);
nixnax 163:d1b4328e9f08 606 debugPrintf("%d.%d.%d.%d:%d ", dstIP[0],dstIP[1],dstIP[2],dstIP[3],udpDstPort);
nixnax 163:d1b4328e9f08 607 debugPrintf("Len %03d", udpLength);
nixnax 163:d1b4328e9f08 608 }
nixnax 146:a45f49a2b29c 609 if (v1) {
nixnax 163:d1b4328e9f08 610 int printSize = udpLength.data;
nixnax 146:a45f49a2b29c 611 if (printSize > 20) printSize = 20; // print only first 20 characters
nixnax 146:a45f49a2b29c 612 for (int i=0; i<printSize; i++) {
nixnax 163:d1b4328e9f08 613 char ch = ppp.udp->data[i];
nixnax 146:a45f49a2b29c 614 if (ch>31 && ch<127) {
nixnax 146:a45f49a2b29c 615 debugPrintf("%c", ch);
nixnax 146:a45f49a2b29c 616 } else {
nixnax 230:ee097c486e45 617 debugPuts("_");
nixnax 146:a45f49a2b29c 618 }
nixnax 146:a45f49a2b29c 619 }
nixnax 146:a45f49a2b29c 620 }
nixnax 230:ee097c486e45 621 if (v0) debugPuts("\n");
nixnax 146:a45f49a2b29c 622 #endif
nixnax 163:d1b4328e9f08 623 int echoFound = !strncmp(ppp.udp->data,"echo ",5); // true if UDP message starts with "echo "
nixnax 258:1eb9abd66922 624 int testFound = !strncmp(ppp.udp->data,"test",4); // true if UDP message starts with "test"
nixnax 146:a45f49a2b29c 625 if ( (echoFound) || (testFound)) { // if the UDP message starts with "echo " or "test" we answer back
nixnax 146:a45f49a2b29c 626 if (echoFound) {
nixnax 171:46b36d4edd1d 627 swapIpAddresses(); // swap IP source and destination
nixnax 171:46b36d4edd1d 628 swapIpPorts(); // swap IP source and destination ports
nixnax 163:d1b4328e9f08 629 memcpy(ppp.udp->data,"Got{",4); // in the UDP data modify "echo" to "Got:"
nixnax 170:3d3b2126181c 630 int n=0;
nixnax 230:ee097c486e45 631 #define IDENTIFY_UDP_SERVER_NO
nixnax 258:1eb9abd66922 632 #ifdef IDENTIFY_UDP_SERVER_YES
nixnax 170:3d3b2126181c 633 n=n+sprintf(n+ppp.udp->data+udpLength.data, "} UDP Server: PPP-Blinky\n"); // an appendix
nixnax 230:ee097c486e45 634 #endif
nixnax 168:c77eb908042a 635 udpLength.data = udpLength.data + n; // update udp data size with the size of the appendix
nixnax 171:46b36d4edd1d 636 // we may have changed data length, update all the lengths
nixnax 171:46b36d4edd1d 637 udpLength.all = udpLength.data+8; // update local udp packet length
nixnax 171:46b36d4edd1d 638 ipLength.all = ipLength.header + udpLength.all; // update IP Length
nixnax 171:46b36d4edd1d 639 ppp.ip->lengthR = __REV16(ipLength.all); // update IP length in buffer
nixnax 171:46b36d4edd1d 640 ppp.udp->lengthR = __REV16(udpLength.all); // update UDP length in buffer
nixnax 171:46b36d4edd1d 641 ppp.pkt.len = ipLength.all+2+4; // update ppp packet length
nixnax 171:46b36d4edd1d 642 IpHeaderCheckSum(); // refresh IP header checksum
nixnax 171:46b36d4edd1d 643 checkSumPseudoHeader( udpLength.all ); // get the UDP pseudo-header checksum
nixnax 171:46b36d4edd1d 644 ppp.udp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared
nixnax 171:46b36d4edd1d 645 unsigned int pseudoHeaderSum=dataCheckSum(ppp.udpStart,udpLength.all, 0); // continue the TCP checksum on the whole TCP packet
nixnax 171:46b36d4edd1d 646 ppp.udp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header
nixnax 171:46b36d4edd1d 647 sendPppFrame(); // send the UDP message back
nixnax 171:46b36d4edd1d 648 } else if ( testFound ) {
nixnax 171:46b36d4edd1d 649 unsigned int sI = __REV( ppp.ip->srcAdrR );
nixnax 171:46b36d4edd1d 650 unsigned int dI = __REV( ppp.ip->dstAdrR );
nixnax 171:46b36d4edd1d 651 unsigned int sp = __REV16( ppp.udp->srcPortR );
nixnax 171:46b36d4edd1d 652 unsigned int dp = __REV16( ppp.udp->dstPortR );
nixnax 172:449dd7a28955 653 int n=sprintf(ppp.pkt.buf+200,"Response Count %d\n", ppp.responseCounter);
nixnax 171:46b36d4edd1d 654 sendUdp(dI,sI,dp,sp,ppp.pkt.buf+200,n); // build a udp packet from the ground up
nixnax 146:a45f49a2b29c 655 }
nixnax 146:a45f49a2b29c 656 }
nixnax 142:54d1543e23e5 657 }
nixnax 142:54d1543e23e5 658
nixnax 174:e5a3f16421a5 659 /// UDP demo that sends a udp packet containing a character received from the second debug serial port.
nixnax 174:e5a3f16421a5 660 /// Sends a 48 byte IP/UDP header for every 1 byte of data so line-mode would probably be better.
nixnax 178:3f17aa0ce5ce 661 /// If you want ip packets from ppp blinky to be routed to other networks, ensure you have ip routing enabled.
nixnax 178:3f17aa0ce5ce 662 /// See http://www.wikihow.com/Enable-IP-Routing.
nixnax 179:ba2b2ddd0da5 663 /// Also ensure that the firewall on the receiving machine has the receiving UDP port (12345 in this example) enabled.
nixnax 179:ba2b2ddd0da5 664 /// The netcat UDP receive command on the remote host would be: nc -ul 12345
nixnax 174:e5a3f16421a5 665 void sendUdpData()
nixnax 174:e5a3f16421a5 666 {
nixnax 174:e5a3f16421a5 667 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 179:ba2b2ddd0da5 668 if (ppp.online) {
nixnax 179:ba2b2ddd0da5 669 if (xx.readable()) {
nixnax 179:ba2b2ddd0da5 670 char inchar = xx.getc();
nixnax 179:ba2b2ddd0da5 671 xx.putc( inchar ); // echo the received character on the debug serial port
nixnax 228:7fb878500862 672 sendUdp(integersToIp(172,10,10,2), integersToIp(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 673 }
nixnax 174:e5a3f16421a5 674 }
nixnax 174:e5a3f16421a5 675 #endif
nixnax 174:e5a3f16421a5 676 }
nixnax 174:e5a3f16421a5 677
nixnax 142:54d1543e23e5 678 /// handle a PING ICMP (internet control message protocol) packet
nixnax 142:54d1543e23e5 679 void ICMPpacket() // internet control message protocol
nixnax 142:54d1543e23e5 680 {
nixnax 165:c47826d07e0d 681 struct {
nixnax 165:c47826d07e0d 682 unsigned int all; // length of entire ip packet
nixnax 165:c47826d07e0d 683 unsigned int header; // length of ip header
nixnax 165:c47826d07e0d 684 } ipLength;
nixnax 165:c47826d07e0d 685 struct {
nixnax 165:c47826d07e0d 686 unsigned int all; // length of entire udp packet
nixnax 165:c47826d07e0d 687 unsigned int data; // length of udp data segment
nixnax 165:c47826d07e0d 688 } icmpLength;
nixnax 165:c47826d07e0d 689 ipLength.all = __REV16( ppp.ip->lengthR ); // length of ip packet
nixnax 165:c47826d07e0d 690 ipLength.header = 4 * ppp.ip->headerLength; // length of ip header
nixnax 165:c47826d07e0d 691 ppp.icmpStart = ppp.ipStart + ipLength.header; // calculate start of udp header
nixnax 165:c47826d07e0d 692 icmpLength.all = ipLength.all - ipLength.header; // length of icmp packet
nixnax 165:c47826d07e0d 693 icmpLength.data = icmpLength.all - 8; // length of icmp data
nixnax 142:54d1543e23e5 694 #define ICMP_TYPE_PING_REQUEST 8
nixnax 165:c47826d07e0d 695 if ( ppp.icmp->type == ICMP_TYPE_PING_REQUEST ) {
nixnax 165:c47826d07e0d 696 ppp.ip->ttl--; // decrement time to live (so we have to update header checksum)
nixnax 147:0f40005cbc5f 697 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 167:ff8a2d8beeb1 698 char * srcAdr = ppp.ip->srcAdrPtr;
nixnax 167:ff8a2d8beeb1 699 char * dstAdr = ppp.ip->dstAdrPtr;
nixnax 165:c47826d07e0d 700 int icmpIdent = __REV16( ppp.ip->identR ); // byte reversed - big endian
nixnax 165:c47826d07e0d 701 int icmpSequence = __REV16( ppp.icmp->sequenceR ); // byte reversed - big endian
nixnax 142:54d1543e23e5 702 if(1) {
nixnax 230:ee097c486e45 703 char pbuf[100];
nixnax 210:a2a651249a9a 704
nixnax 142:54d1543e23e5 705 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 221:1d7c32d8256b 706 debugPuts( pbuf );
nixnax 210:a2a651249a9a 707
nixnax 142:54d1543e23e5 708 sprintf(pbuf, "Ident %04x Sequence %04d \n",icmpIdent,icmpSequence);
nixnax 210:a2a651249a9a 709
nixnax 221:1d7c32d8256b 710 debugPuts( pbuf );
nixnax 142:54d1543e23e5 711 }
nixnax 142:54d1543e23e5 712 #endif
nixnax 147:0f40005cbc5f 713 swapIpAddresses(); // swap the IP source and destination addresses
nixnax 147:0f40005cbc5f 714 IpHeaderCheckSum(); // new ip header checksum (required because we changed TTL)
nixnax 142:54d1543e23e5 715 #define ICMP_TYPE_ECHO_REPLY 0
nixnax 165:c47826d07e0d 716 ppp.icmp->type = ICMP_TYPE_ECHO_REPLY; // icmp echo reply
nixnax 165:c47826d07e0d 717 ppp.icmp->checkSumR = 0; // zero the checksum for recalculation
nixnax 165:c47826d07e0d 718 unsigned int sum = dataCheckSum(ppp.icmpStart, icmpLength.all, 1); // icmp checksum
nixnax 165:c47826d07e0d 719 ppp.icmp->checkSumR = __REV16( sum ); // save big-endian icmp checksum
nixnax 226:4898247048c7 720 #define DUMP_ICMP_PACKETS_NO
nixnax 226:4898247048c7 721 #ifdef DUMP_ICMP_PACKETS_YES
nixnax 165:c47826d07e0d 722 int printSize = icmpLength.data; // exclude size of icmp header
nixnax 142:54d1543e23e5 723 if (printSize > 10) printSize = 10; // print up to 20 characters
nixnax 226:4898247048c7 724 for (int i=0; i<printSize; i++) {
nixnax 226:4898247048c7 725 char ch = ppp.icmp->data[i];
nixnax 226:4898247048c7 726 if (ch>31 && ch<127) {
nixnax 226:4898247048c7 727 debugPutc(ch);
nixnax 226:4898247048c7 728 } else {
nixnax 226:4898247048c7 729 debugPutc('_'); // for non-printable characters
nixnax 142:54d1543e23e5 730 }
nixnax 142:54d1543e23e5 731 }
nixnax 226:4898247048c7 732 debugPutc('\n');
nixnax 226:4898247048c7 733 #endif
nixnax 144:01d98cf7738e 734 sendPppFrame(); // reply to the ping
nixnax 142:54d1543e23e5 735 } else {
nixnax 142:54d1543e23e5 736 if (v0) {
nixnax 165:c47826d07e0d 737 debugPrintf("ICMP type=%x \n", ppp.icmp->type);
nixnax 142:54d1543e23e5 738 }
nixnax 142:54d1543e23e5 739 }
nixnax 142:54d1543e23e5 740 }
nixnax 142:54d1543e23e5 741
nixnax 142:54d1543e23e5 742 /// handle an IGMP (internet group managment protocol) packet (by ignoring it)
nixnax 142:54d1543e23e5 743 void IGMPpacket()
nixnax 142:54d1543e23e5 744 {
nixnax 142:54d1543e23e5 745 if (v0) debugPrintf("IGMP type=%d \n", ppp.pkt.buf[28]);
nixnax 142:54d1543e23e5 746 }
nixnax 142:54d1543e23e5 747
nixnax 142:54d1543e23e5 748 /// dump the header of an IP pakcet on the (optional) debug serial port
nixnax 142:54d1543e23e5 749 void dumpHeaderIP (int outGoing)
nixnax 142:54d1543e23e5 750 {
nixnax 142:54d1543e23e5 751 #if defined(IP_HEADER_DUMP_YES) && defined(SERIAL_PORT_MONITOR_YES)
nixnax 210:a2a651249a9a 752
nixnax 155:9c6a1d249e26 753 int IPv4Id = __REV16(ppp.ip->identR);
nixnax 230:ee097c486e45 754 char pbuf[100]; // local print buffer
nixnax 142:54d1543e23e5 755 int n=0;
nixnax 142:54d1543e23e5 756 n=n+sprintf(pbuf+n, outGoing ? "\x1b[34m" : "\x1b[30m" ); // VT100 color code, print black for incoming, blue for outgoing headers
nixnax 142:54d1543e23e5 757 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 758 #define DUMP_FULL_IP_ADDRESS_YES
nixnax 142:54d1543e23e5 759 #ifdef DUMP_FULL_IP_ADDRESS_YES
nixnax 167:ff8a2d8beeb1 760 char * srcAdr = ppp.ip->srcAdrPtr;
nixnax 167:ff8a2d8beeb1 761 char * dstAdr = ppp.ip->dstAdrPtr;
nixnax 142:54d1543e23e5 762 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 763 #endif
nixnax 221:1d7c32d8256b 764 debugPuts( pbuf );
nixnax 142:54d1543e23e5 765 #ifndef TCP_HEADER_DUMP_YES
nixnax 221:1d7c32d8256b 766 debugPuts('\x1b[30m\n'); // if there's no TCP header dump we terminate the line with \n and VT100 code for black
nixnax 142:54d1543e23e5 767 #endif
nixnax 142:54d1543e23e5 768 #endif
nixnax 142:54d1543e23e5 769 }
nixnax 142:54d1543e23e5 770
nixnax 142:54d1543e23e5 771 /// dump a TCP header on the optional debug serial port
nixnax 142:54d1543e23e5 772 void dumpHeaderTCP(int outGoing)
nixnax 142:54d1543e23e5 773 {
nixnax 142:54d1543e23e5 774 #if defined(TCP_HEADER_DUMP_YES) && defined(SERIAL_PORT_MONITOR_YES)
nixnax 155:9c6a1d249e26 775 char flagString[9]; // text string presenting the 8 most important TCP flags
nixnax 142:54d1543e23e5 776 #define PRINT_ALL_TCP_FLAGS_YES
nixnax 142:54d1543e23e5 777 #ifdef PRINT_ALL_TCP_FLAGS_YES
nixnax 155:9c6a1d249e26 778 memset(flagString,'.', 8); // fill string with "........"
nixnax 156:163c23249731 779 if (ppp.tcp->flag.fin) flagString[7]='F';
nixnax 156:163c23249731 780 if (ppp.tcp->flag.syn) flagString[6]='S';
nixnax 156:163c23249731 781 if (ppp.tcp->flag.rst) flagString[5]='R';
nixnax 156:163c23249731 782 if (ppp.tcp->flag.psh) flagString[4]='P';
nixnax 156:163c23249731 783 if (ppp.tcp->flag.ack) flagString[3]='A';
nixnax 156:163c23249731 784 if (ppp.tcp->flag.urg) flagString[2]='U';
nixnax 156:163c23249731 785 if (ppp.tcp->flag.ece) flagString[1]='E';
nixnax 156:163c23249731 786 if (ppp.tcp->flag.cwr) flagString[0]='C';
nixnax 155:9c6a1d249e26 787 flagString[8]=0; // null terminate string
nixnax 142:54d1543e23e5 788 #else
nixnax 156:163c23249731 789 if (ppp.tcp->flag.ack) flagString[0]='A'; // choose only the most important flag to print
nixnax 156:163c23249731 790 if (ppp.tcp->flag.syn) flagString[0]='S';
nixnax 156:163c23249731 791 if (ppp.tcp->flag.fin) flagString[0]='F';
nixnax 156:163c23249731 792 if (ppp.tcp->flag.psh) flagString[0]='P';
nixnax 156:163c23249731 793 if (ppp.tcp->flag.rst) flagString[0]='R';
nixnax 155:9c6a1d249e26 794 flagString[1]=0; // null terminate string
nixnax 142:54d1543e23e5 795 #endif
nixnax 221:1d7c32d8256b 796 debugPuts( flagString );
nixnax 142:54d1543e23e5 797 #define EVERY_PACKET_ON_A_NEW_LINE_YES
nixnax 142:54d1543e23e5 798 #ifdef EVERY_PACKET_ON_A_NEW_LINE_YES
nixnax 221:1d7c32d8256b 799 debugPuts("\x1b[30m\n"); // write a black color and newline after every packet
nixnax 160:bd701ad564cb 800 #else
nixnax 221:1d7c32d8256b 801 debugPuts("\x1b[30m"); // write a black color after every packet
nixnax 142:54d1543e23e5 802 #endif
nixnax 156:163c23249731 803 if( outGoing && ppp.tcp->flag.fin ) { // ACK/FIN - if this is an outgoing FIN it's the end of a tcp conversation
nixnax 221:1d7c32d8256b 804 debugPutc('\n'); // insert an extra new line to mark the end (except for final ack) of an HTTP conversation
nixnax 142:54d1543e23e5 805 }
nixnax 142:54d1543e23e5 806 #endif
nixnax 142:54d1543e23e5 807 }
nixnax 142:54d1543e23e5 808
nixnax 143:c5019f856a56 809 /// Encode a buffer in base-64
nixnax 184:4133f557d4d4 810 const static char lut [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
nixnax 142:54d1543e23e5 811 void enc64(char * in, char * out, int len)
nixnax 142:54d1543e23e5 812 {
nixnax 142:54d1543e23e5 813 int i,j,a,b,c;
nixnax 142:54d1543e23e5 814 i=0;
nixnax 142:54d1543e23e5 815 j=0;
nixnax 142:54d1543e23e5 816 while(1) {
nixnax 142:54d1543e23e5 817 if (i<len) {
nixnax 142:54d1543e23e5 818 a = in[i++];
nixnax 142:54d1543e23e5 819 out[j++] = lut[ ( (a >> 2) & 0x3f) ];
nixnax 142:54d1543e23e5 820 } else break;
nixnax 142:54d1543e23e5 821 if (i<len) {
nixnax 142:54d1543e23e5 822 b = in[i++];
nixnax 142:54d1543e23e5 823 out[j++] = lut[ ( (a << 4) & 0x30) | ( (b >> 4) & 0x0f) ];
nixnax 142:54d1543e23e5 824 out[j++] = lut[ ( (b << 2) & 0x3c) ];
nixnax 142:54d1543e23e5 825 } else out[j++] = '=';
nixnax 142:54d1543e23e5 826 if (i<len) {
nixnax 142:54d1543e23e5 827 c = in[i++];
nixnax 142:54d1543e23e5 828 j--;
nixnax 142:54d1543e23e5 829 out[j++] = lut[ ( (b << 2) & 0x3c) | ( (c >> 6) & 0x03) ];
nixnax 142:54d1543e23e5 830 out[j++] = lut[ ( (c >> 0) & 0x3f) ];
nixnax 142:54d1543e23e5 831 } else out[j++] = '=';
nixnax 142:54d1543e23e5 832 }
nixnax 142:54d1543e23e5 833 out[j]=0;
nixnax 142:54d1543e23e5 834 }
nixnax 142:54d1543e23e5 835
nixnax 160:bd701ad564cb 836 /// Handle a request for an http websocket.
nixnax 160:bd701ad564cb 837 /// 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 838 int webSocketHandler(char * dataStart)
nixnax 142:54d1543e23e5 839 {
nixnax 142:54d1543e23e5 840 int n=0; // byte counter
nixnax 184:4133f557d4d4 841 char * key = strstr(dataStart, "Sec-WebSocket-Key:"); // search for the key in the payload
nixnax 142:54d1543e23e5 842 if (key != NULL) {
nixnax 184:4133f557d4d4 843 key = key + 18; // skip over the key ident string "Sec-WebSocket-Key:"
nixnax 221:1d7c32d8256b 844 if (v0) debugPuts("WebSocket Request\n");
nixnax 184:4133f557d4d4 845 while ( strchr(lut, *key) == NULL) key++; // skip non-valid base-64 characters (whitespace)
nixnax 184:4133f557d4d4 846 char challenge [80];
nixnax 184:4133f557d4d4 847 int i=0;
nixnax 184:4133f557d4d4 848 char * copyTo = challenge;
nixnax 184:4133f557d4d4 849 while (strchr(lut, *key) != NULL) { // copy while we see valid base-64 characters
nixnax 184:4133f557d4d4 850 if (i++ >40) break; // prevent buffer overflow
nixnax 184:4133f557d4d4 851 *copyTo++ = *key++; // copy next valid base-64 character
nixnax 184:4133f557d4d4 852 }
nixnax 184:4133f557d4d4 853 strcpy(copyTo,"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); // append websocket gui code
nixnax 226:4898247048c7 854 #define DUMP_WEBSOCKET_CHALLENGE_NO
nixnax 226:4898247048c7 855 #ifdef DUMP_WEBSOCKET_CHALLENGE_YES
nixnax 226:4898247048c7 856 debugPrintf("Challenge is %s\n", challenge); // the string we hash for the challenge
nixnax 227:f87156f9ef11 857 #endif
nixnax 142:54d1543e23e5 858 char shaOutput [20]; // sha1 output
nixnax 184:4133f557d4d4 859 sha1( shaOutput, challenge, strlen(challenge)); // hash the challenge
nixnax 142:54d1543e23e5 860 char encOut[50];
nixnax 184:4133f557d4d4 861 enc64( shaOutput, encOut, 20); // base-64 encode
nixnax 142:54d1543e23e5 862 char * versionstring = strstr(dataStart, "Sec-WebSocket-Version:");
nixnax 142:54d1543e23e5 863 char * version = challenge;
nixnax 184:4133f557d4d4 864 strncpy(version, versionstring,70); // copy their version string
nixnax 142:54d1543e23e5 865 *strchr(version,'\r')=0; // null terminate so we can sprintf it
nixnax 240:d7a01533397d 866 memset(dataStart,0,500); // blank out old data before sending the websocket response header
nixnax 142:54d1543e23e5 867 n=n+sprintf(dataStart+n, "HTTP/1.1 101 Switching Protocols\r\n");
nixnax 142:54d1543e23e5 868 n=n+sprintf(dataStart+n, "Upgrade: websocket\r\n");
nixnax 142:54d1543e23e5 869 n=n+sprintf(dataStart+n, "Connection: Upgrade\r\n");
nixnax 142:54d1543e23e5 870 n=n+sprintf(dataStart+n, "Sec-WebSocket-Accept: %s\r\n",encOut);
nixnax 142:54d1543e23e5 871 n=n+sprintf(dataStart+n, "%s\r\n",version);
nixnax 261:272ec8e2a7d4 872 #define SHOW_MBED_IN_RESPONSE_YES // change to SHOW_MBED_IN_RESPONSE_NO to exclude
nixnax 261:272ec8e2a7d4 873 #ifdef SHOW_MBED_IN_RESPONSE_YES
nixnax 142:54d1543e23e5 874 n=n+sprintf(dataStart+n, "mbed-Code: PPP-Blinky\r\n");
nixnax 262:c044fed611aa 875 #endif
nixnax 142:54d1543e23e5 876 n=n+sprintf(dataStart+n, "\r\n"); // websocket response header ending
nixnax 142:54d1543e23e5 877 }
nixnax 142:54d1543e23e5 878 return n; // this response should satisfy a web browser's websocket protocol request
nixnax 142:54d1543e23e5 879 }
nixnax 142:54d1543e23e5 880
nixnax 142:54d1543e23e5 881 #define TCP_FLAG_ACK (1<<4)
nixnax 142:54d1543e23e5 882 #define TCP_FLAG_SYN (1<<1)
nixnax 142:54d1543e23e5 883 #define TCP_FLAG_PSH (1<<3)
nixnax 142:54d1543e23e5 884 #define TCP_FLAG_RST (1<<2)
nixnax 142:54d1543e23e5 885 #define TCP_FLAG_FIN (1<<0)
nixnax 142:54d1543e23e5 886
nixnax 142:54d1543e23e5 887 /// respond to an HTTP request
nixnax 185:4cd8f91e9d49 888 int httpResponse(char * dataStart, int * flags)
nixnax 142:54d1543e23e5 889 {
nixnax 142:54d1543e23e5 890 int n=0; // number of bytes we have printed so far
nixnax 142:54d1543e23e5 891 n = webSocketHandler( dataStart ); // test for and handle WebSocket upgrade requests
nixnax 160:bd701ad564cb 892 if (n>0) return n; // if n>0 we already have a response, so return
nixnax 142:54d1543e23e5 893
nixnax 142:54d1543e23e5 894 int nHeader; // byte size of HTTP header
nixnax 142:54d1543e23e5 895 int contentLengthStart; // index where HTML starts
nixnax 142:54d1543e23e5 896 int httpGet5,httpGet6,httpGetx, httpGetRoot; // temporary storage of strncmp results
nixnax 185:4cd8f91e9d49 897 *flags = TCP_FLAG_ACK | TCP_FLAG_FIN; // the default case is that we close the connection
nixnax 142:54d1543e23e5 898
nixnax 142:54d1543e23e5 899 httpGetRoot = strncmp(dataStart, "GET / HTTP/1.", 13); // found a GET to the root directory
nixnax 142:54d1543e23e5 900 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 901 httpGet5 = dataStart[5]; // the first character in the path name, we use it for special functions later on
nixnax 142:54d1543e23e5 902 httpGet6 = dataStart[6]; // the second character in the path name, we use it for special functions later on
nixnax 142:54d1543e23e5 903 // for example, you could try this using netcat (nc): echo "GET /x" | nc 172.10.10.2
nixnax 271:06c2c2a4e0bf 904 n=n+sprintf(n+dataStart,"HTTP/1.1 "); // start of response header
nixnax 270:98099fbc46fb 905 if( (httpGetRoot==0) || (httpGetx==0) || (httpGet5 == 'w' ) ) {
nixnax 271:06c2c2a4e0bf 906 n=n+sprintf(n+dataStart,"200 OK\r\n"); // 200 OK header
nixnax 142:54d1543e23e5 907 } else {
nixnax 271:06c2c2a4e0bf 908 n=n+sprintf(n+dataStart,"404 Not Found\r\n"); // 404 header
nixnax 142:54d1543e23e5 909 }
nixnax 271:06c2c2a4e0bf 910 #define SHOWSERVERNAME_NO
nixnax 271:06c2c2a4e0bf 911 #ifdef SHOWSERVERNAME_YES
nixnax 271:06c2c2a4e0bf 912 n=n+sprintf(n+dataStart,"Server: mbed PPP-Blinky\r\n"); // Server in response header
nixnax 275:bb2d582c20d6 913 #endif
nixnax 271:06c2c2a4e0bf 914 #define SHOWMAXBUFLEVEL_NO
nixnax 209:aa514cd631ee 915 #ifdef SHOWMAXBUFLEVEL_YES
nixnax 209:aa514cd631ee 916 // if this is enabled then the maximum value of the serial port receive buffer will show up in the http headers of your browser - nice for debugging
nixnax 210:a2a651249a9a 917 n=n+sprintf(n+dataStart,"Maxbuflevel: %d\r\n", ppp.rx.maxbuflevel);
nixnax 210:a2a651249a9a 918 #endif
nixnax 262:c044fed611aa 919 #define SHOWPPPCOUNT_NO
nixnax 262:c044fed611aa 920 #ifdef SHOWPPPCOUNT_YES
nixnax 262:c044fed611aa 921 // if this is enabled then the received PPP packet count will show up in the http headers of your browser - disabled for now because it's also displayed in the root page
nixnax 262:c044fed611aa 922 n=n+sprintf(n+dataStart,"PPP-RX-Packets: %d\r\n", ppp.pppCount);
nixnax 240:d7a01533397d 923 #endif
nixnax 271:06c2c2a4e0bf 924 n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n"); // http header must end with empty line (\r\n)
nixnax 271:06c2c2a4e0bf 925 n=n+sprintf(n+dataStart,"Content-Length: ?????\r\n\r\n"); // leave five spaces for content length - will be updated later
nixnax 271:06c2c2a4e0bf 926 contentLengthStart = n-9; // remember where Content-Length starts in the buffer
nixnax 271:06c2c2a4e0bf 927 nHeader=n; // size of HTTP header and start of payload
nixnax 142:54d1543e23e5 928 if( httpGetRoot == 0 ) {
nixnax 142:54d1543e23e5 929 // this is where we insert our web page into the buffer
nixnax 142:54d1543e23e5 930 memcpy(n+dataStart,rootWebPage,sizeof(rootWebPage));
nixnax 142:54d1543e23e5 931 n = n + sizeof(rootWebPage)-1; // one less than sizeof because we don't count the null byte at the end
nixnax 262:c044fed611aa 932 // naximum length of pppCount (maximum number of digits of an unsigned 32-bit int)
nixnax 262:c044fed611aa 933 #define PPPCOUNTMAXLEN (10)
nixnax 262:c044fed611aa 934 char pppCountString[PPPCOUNTMAXLEN+1]; // space for the ten digits and a string terminator 0
nixnax 262:c044fed611aa 935 snprintf(pppCountString,PPPCOUNTMAXLEN+1,"%*d",PPPCOUNTMAXLEN,ppp.pppCount); // print number of received PPP packets right justified in 10 spaces
nixnax 258:1eb9abd66922 936 char * tenNulls = "0000000000";
nixnax 262:c044fed611aa 937 char * pppCountStart = strstr(dataStart,tenNulls); // find the ten zeros in the page - this is where we must paste in the PPP Rx packet count
nixnax 262:c044fed611aa 938 if ( pppCountStart != NULL )
nixnax 262:c044fed611aa 939 memcpy(pppCountStart, pppCountString, PPPCOUNTMAXLEN); // copy ping count over the ten zeros in the page
nixnax 262:c044fed611aa 940 snprintf(pppCountString,PPPCOUNTMAXLEN+1,"%*d",PPPCOUNTMAXLEN,SystemCoreClock); // print System Core Clock speed
nixnax 258:1eb9abd66922 941 char * coreClockStart = strstr(dataStart,tenNulls); // find the ten zeros in the page - this is where we must paste in the core clock speed
nixnax 258:1eb9abd66922 942 if ( coreClockStart != NULL )
nixnax 262:c044fed611aa 943 memcpy(coreClockStart, pppCountString, PPPCOUNTMAXLEN); // copy core clock speed over the ten zeros in the page
nixnax 210:a2a651249a9a 944 } else if ( httpGet5 == 'w' ) { // "w" is a special page for websocket demo
nixnax 142:54d1543e23e5 945 memcpy(n+dataStart,webSocketPage,sizeof(webSocketPage));
nixnax 142:54d1543e23e5 946 n = n + sizeof(webSocketPage)-1; // one less than size
nixnax 185:4cd8f91e9d49 947 *flags = TCP_FLAG_ACK | TCP_FLAG_PSH; // for a websocket page we do NOT close the connection
nixnax 142:54d1543e23e5 948 } else {
nixnax 142:54d1543e23e5 949 if (httpGetx == 0) { // the page request started with "GET /x" - here we treat anything starting with /x special:
nixnax 142:54d1543e23e5 950 #define W3C_COMPLIANT_RESPONSE_NO
nixnax 142:54d1543e23e5 951 // change the above to W3C_COMPLIANT_RESPONSE_YES if you want a W3C.org compliant HTTP response
nixnax 142:54d1543e23e5 952 #ifdef W3C_COMPLIANT_RESPONSE_YES
nixnax 142:54d1543e23e5 953 n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed PPP-Blinky</title>"); // html title (W3C.org required elements)
nixnax 258:1eb9abd66922 954 #endif
nixnax 272:9ea94556c6f6 955 if( (ppp.ledState & 1) == 0) // check lowest bit of LED state and respond with red or green favicon - this stops browsers from asking for favicon.ico file
nixnax 258:1eb9abd66922 956 n=n+sprintf(n+dataStart,"<link rel=\"icon\" id=\"red-pixel\" href=\"data:image/gif;base64,R0lGODlhAQABAIABAP8AAAAAACwAAAAAAQABAAACAkQBADs\">"); // data url containing 1 red pixel gif
nixnax 248:5aca1c98fc24 957 else
nixnax 258:1eb9abd66922 958 n=n+sprintf(n+dataStart,"<link rel=\"icon\" id=\"grn-pixel\" href=\"data:image/gif;base64,R0lGODlhAQABAIABAAD/AAAAACwAAAAAAQABAAACAkQBADs=\">"); // data url containing 1 green pixel gif
nixnax 248:5aca1c98fc24 959 if( httpGet6 == 'b' ) // if the fetched page is "xb" send a meta command to let the browser continuously reload
nixnax 248:5aca1c98fc24 960 n=n+sprintf(n+dataStart, "<meta http-equiv=\"refresh\" content=\"0\">"); // reload loop - handy for benchmarking
nixnax 248:5aca1c98fc24 961 // /x is a very short page, in fact, it is only a decimal number showing the http Page count
nixnax 270:98099fbc46fb 962 if (httpGet6 == 't' ) // if the fetched pages is "xt" toggle the LEd
nixnax 275:bb2d582c20d6 963 led1Toggle();
nixnax 248:5aca1c98fc24 964 #ifdef W3C_COMPLIANT_RESPONSE_YES
nixnax 146:a45f49a2b29c 965 n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.responseCounter); // body = the http frame count
nixnax 258:1eb9abd66922 966 #else
nixnax 272:9ea94556c6f6 967 n=n+sprintf(n+dataStart,"%d",ppp.responseCounter); // not really valid html but most browsers and curl are ok with it
nixnax 142:54d1543e23e5 968 #endif
nixnax 258:1eb9abd66922 969 } else {
nixnax 273:3b3de186ea2f 970 // all other requests get a Not Found response
nixnax 257:1990a41a1e31 971 n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed PPP-Blinky</title>"); // html title (W3C.org required elements)
nixnax 274:b1bc40deb637 972 n=n+sprintf(n+dataStart,"<body>404 Not Found</body>"); // not found message
nixnax 142:54d1543e23e5 973 }
nixnax 142:54d1543e23e5 974 }
nixnax 142:54d1543e23e5 975 #define CONTENTLENGTHSIZE 5
nixnax 142:54d1543e23e5 976 char contentLengthString[CONTENTLENGTHSIZE+1];
nixnax 142:54d1543e23e5 977 snprintf(contentLengthString,CONTENTLENGTHSIZE+1,"%*d",CONTENTLENGTHSIZE,n-nHeader); // print Content-Length with leading spaces and fixed width equal to csize
nixnax 142:54d1543e23e5 978 memcpy(dataStart+contentLengthStart, contentLengthString, CONTENTLENGTHSIZE); // copy Content-Length to it's place in the send buffer
nixnax 142:54d1543e23e5 979 return n; // total byte size of our response
nixnax 142:54d1543e23e5 980 }
nixnax 142:54d1543e23e5 981
nixnax 160:bd701ad564cb 982 /// Handle TCP data that is not an HTTP GET.
nixnax 160:bd701ad564cb 983 /// This is handy when for example you want to use netcat (nc.exe) to talk to PPP-Blinky.
nixnax 160:bd701ad564cb 984 /// This could also be a websocket receive event - especially if the first byte is 0x81 (websocket data push)
nixnax 142:54d1543e23e5 985 int tcpResponse(char * dataStart, int len, int * outFlags)
nixnax 142:54d1543e23e5 986 {
nixnax 142:54d1543e23e5 987 int n=0; // number of bytes we have printed so far
nixnax 142:54d1543e23e5 988 if (dataStart[0] == 0x81) { // check if this is a websocket push message
nixnax 160:bd701ad564cb 989 char mask [4];
nixnax 160:bd701ad564cb 990 memcpy ( mask, dataStart+2, 4); // websocket messages are "masked", so first we obtain the 4-byte mask
nixnax 160:bd701ad564cb 991 int websocketMessageSize = len - 6; // 1 byte prefix (0x81), 1 byte, 4 bytes mask = 6 bytes
nixnax 160:bd701ad564cb 992 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 993 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 994 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 995 memcpy(dataStart+2, "Got:",4); // insert our own text into the four mask bytes
nixnax 160:bd701ad564cb 996 n = len; // our response size remains exactly the same length as what we received
nixnax 142:54d1543e23e5 997 } else if ( (dataStart[0]==0x88) && (dataStart[1]==0x80) && (len == 6) ) { // test for a websocket close request
nixnax 142:54d1543e23e5 998 n=2; // our close command is only two bytes long because we don't use the four mask bytes
nixnax 142:54d1543e23e5 999 dataStart[1]=0; // we don't have mask bytes on
nixnax 160:bd701ad564cb 1000 } else {
nixnax 184:4133f557d4d4 1001 if ( len > 1 ) { // we assume a length of 1 is a keep-alive or push packet
nixnax 221:1d7c32d8256b 1002 if (v1) debugPuts("TCP data received\n"); // all other tcp push packets
nixnax 184:4133f557d4d4 1003 }
nixnax 163:d1b4328e9f08 1004 }
nixnax 142:54d1543e23e5 1005 return n; // total byte size of our response
nixnax 142:54d1543e23e5 1006 }
nixnax 142:54d1543e23e5 1007
nixnax 173:6774a0c851c4 1008 /// dump the TCP data to the debug serial port
nixnax 173:6774a0c851c4 1009 void dumpDataTCP(int outGoing)
nixnax 173:6774a0c851c4 1010 {
nixnax 173:6774a0c851c4 1011 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 173:6774a0c851c4 1012 if (v2) {
nixnax 173:6774a0c851c4 1013 int packetLengthIp = __REV16(ppp.ip->lengthR ); // size of ip packet
nixnax 173:6774a0c851c4 1014 int headerSizeIp = 4 * ppp.ip->headerLength; // size of ip header
nixnax 173:6774a0c851c4 1015 ppp.tcpStart = ppp.ipStart + headerSizeIp; // calculate where the TCP header starts
nixnax 173:6774a0c851c4 1016 int headerSizeTcp = 4 * (ppp.tcp->offset); // tcp "offset" for start of data is also the header size
nixnax 173:6774a0c851c4 1017 ppp.tcpData = ppp.tcpStart + headerSizeTcp; // start of tcp data
nixnax 173:6774a0c851c4 1018 int tcpSize = packetLengthIp - headerSizeIp; // tcp size = size of ip payload
nixnax 173:6774a0c851c4 1019 int tcpDataSize = tcpSize - headerSizeTcp; // size of data block after TCP header
nixnax 230:ee097c486e45 1020 char pbuf[100]; // local print buffer
nixnax 173:6774a0c851c4 1021 int n=0;
nixnax 210:a2a651249a9a 1022
nixnax 173:6774a0c851c4 1023 n=n+sprintf(pbuf+n, outGoing ? "\x1b[34m" : "\x1b[30m" ); // VT100 color code, print black for incoming, blue for outgoing headers
nixnax 210:a2a651249a9a 1024
nixnax 173:6774a0c851c4 1025 n=n+sprintf(pbuf+n, "IP:%d ipHeader:%d tcpHeader:%d tcpData:%d\n", packetLengthIp, headerSizeIp, headerSizeTcp, tcpDataSize); // 1 for more verbose
nixnax 230:ee097c486e45 1026 if (n>95) debugPuts("n>pbuf overflow in dumpDataTCP()\n");
nixnax 210:a2a651249a9a 1027
nixnax 221:1d7c32d8256b 1028 debugPuts( pbuf );
nixnax 173:6774a0c851c4 1029 if (tcpDataSize > 0) {
nixnax 173:6774a0c851c4 1030 ppp.tcpData[tcpDataSize]=0; // insert a null after the data so debug printf stops printing after the data
nixnax 221:1d7c32d8256b 1031 debugPuts( ppp.tcpData ); // print the tcp payload data
nixnax 221:1d7c32d8256b 1032 debugPuts("\n");
nixnax 173:6774a0c851c4 1033 }
nixnax 221:1d7c32d8256b 1034 debugPuts( "\x1b[30m" ); // VT100 color code, print black
nixnax 173:6774a0c851c4 1035 }
nixnax 173:6774a0c851c4 1036 #endif
nixnax 173:6774a0c851c4 1037 }
nixnax 173:6774a0c851c4 1038
nixnax 142:54d1543e23e5 1039 /// handle an incoming TCP packet
nixnax 142:54d1543e23e5 1040 /// 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 1041 void tcpHandler()
nixnax 142:54d1543e23e5 1042 {
nixnax 154:18b2bd92f557 1043 int packetLengthIp = __REV16(ppp.ip->lengthR ); // size of ip packet
nixnax 154:18b2bd92f557 1044 int headerSizeIp = 4 * ppp.ip->headerLength; // size of ip header
nixnax 173:6774a0c851c4 1045 ppp.tcpStart = ppp.ipStart + headerSizeIp; // calculate TCP header start
nixnax 154:18b2bd92f557 1046 int tcpSize = packetLengthIp - headerSizeIp; // tcp size = size of ip payload
nixnax 154:18b2bd92f557 1047 int headerSizeTcp = 4 * (ppp.tcp->offset); // tcp "offset" for start of data is also the header size
nixnax 227:f87156f9ef11 1048 char * tcpDataIn = ppp.tcpStart + headerSizeTcp; // start of TCP data after TCP header
nixnax 154:18b2bd92f557 1049 int tcpDataSize = tcpSize - headerSizeTcp; // size of data block after TCP header
nixnax 154:18b2bd92f557 1050
nixnax 227:f87156f9ef11 1051 unsigned int seq_in = __REV(ppp.tcp->seqTcpR); // incoming sequence number
nixnax 227:f87156f9ef11 1052 unsigned int ack_in = __REV(ppp.tcp->ackTcpR); // incoming acknowledge number
nixnax 227:f87156f9ef11 1053 unsigned int ack_out = seq_in + tcpDataSize; // calculate the acknowledge based on size of received packet
nixnax 227:f87156f9ef11 1054 unsigned int seq_out = ack_in; // adopt their version of our sequence number as our sequence number
nixnax 142:54d1543e23e5 1055
nixnax 142:54d1543e23e5 1056 // first we shorten the TCP response header to only 20 bytes. This means we ignore all TCP option requests
nixnax 173:6774a0c851c4 1057 headerSizeIp=20;
nixnax 173:6774a0c851c4 1058 ppp.ip->headerLength = headerSizeIp/4; // ip header is 20 bytes long
nixnax 173:6774a0c851c4 1059 ppp.ip->lengthR = __REV(40); // 20 ip header + 20 tcp header
nixnax 227:f87156f9ef11 1060 headerSizeTcp = 20; // shorten outgoing TCP header size to 20 bytes (no data)
nixnax 173:6774a0c851c4 1061 ppp.tcpStart = ppp.ipStart + headerSizeIp; // recalc TCP header start
nixnax 154:18b2bd92f557 1062 ppp.tcp->offset = (headerSizeTcp/4);
nixnax 173:6774a0c851c4 1063 char * tcpDataOut = ppp.tcpStart + headerSizeTcp; // start of outgoing data
nixnax 142:54d1543e23e5 1064
nixnax 142:54d1543e23e5 1065 int dataLen = 0; // most of our responses will have zero TCP data, only a header
nixnax 142:54d1543e23e5 1066 int flagsOut = TCP_FLAG_ACK; // the default case is an ACK packet
nixnax 160:bd701ad564cb 1067
nixnax 185:4cd8f91e9d49 1068 ppp.tcp->windowR = __REV16( 1200 ); // set tcp window size to 1200 bytes
nixnax 142:54d1543e23e5 1069
nixnax 142:54d1543e23e5 1070 // A sparse TCP flag interpreter that implements stateless TCP connections
nixnax 142:54d1543e23e5 1071
nixnax 156:163c23249731 1072 switch ( ppp.tcp->flag.All ) {
nixnax 142:54d1543e23e5 1073 case TCP_FLAG_SYN:
nixnax 142:54d1543e23e5 1074 flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // something wants to connect - acknowledge it
nixnax 142:54d1543e23e5 1075 seq_out = seq_in+0x10000000U; // create a new sequence number using their sequence as a starting point, increase the highest digit
nixnax 142:54d1543e23e5 1076 ack_out++; // for SYN flag we have to increase the sequence by 1
nixnax 142:54d1543e23e5 1077 break;
nixnax 184:4133f557d4d4 1078 case TCP_FLAG_ACK:
nixnax 142:54d1543e23e5 1079 case TCP_FLAG_ACK | TCP_FLAG_PSH:
nixnax 259:f694046fc06f 1080 if (tcpDataSize == 0) return; // handle zero-size ack messages by ignoring them
nixnax 259:f694046fc06f 1081 // if ( (ppp.tcp->flag.All == TCP_FLAG_ACK) && (tcpDataSize == 0)) return; // handle zero-size ack messages by ignoring them
nixnax 142:54d1543e23e5 1082 if ( (strncmp(tcpDataIn, "GET /", 5) == 0) ) { // check for an http GET command
nixnax 179:ba2b2ddd0da5 1083 flagsOut = TCP_FLAG_ACK | TCP_FLAG_PSH; // we have data, set the PSH flag
nixnax 185:4cd8f91e9d49 1084 dataLen = httpResponse(tcpDataOut, &flagsOut); // send an http response
nixnax 142:54d1543e23e5 1085 } else {
nixnax 158:841592aed956 1086 dataLen = tcpResponse(tcpDataOut,tcpDataSize, &flagsOut); // not an http GET, handle as a tcp connection
nixnax 179:ba2b2ddd0da5 1087 if (dataLen > 0) flagsOut = TCP_FLAG_ACK | TCP_FLAG_PSH; // if we have any data set the PSH flag
nixnax 142:54d1543e23e5 1088 }
nixnax 142:54d1543e23e5 1089 break;
nixnax 142:54d1543e23e5 1090 case TCP_FLAG_FIN:
nixnax 142:54d1543e23e5 1091 case TCP_FLAG_FIN | TCP_FLAG_ACK:
nixnax 142:54d1543e23e5 1092 case TCP_FLAG_FIN | TCP_FLAG_PSH | TCP_FLAG_ACK:
nixnax 142:54d1543e23e5 1093 flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set outgoing FIN flag to ask them to close from their side
nixnax 142:54d1543e23e5 1094 ack_out++; // for FIN flag we have to increase the sequence by 1
nixnax 142:54d1543e23e5 1095 break;
nixnax 142:54d1543e23e5 1096 default:
nixnax 179:ba2b2ddd0da5 1097 return; // ignore all other packets
nixnax 142:54d1543e23e5 1098 } // switch
nixnax 142:54d1543e23e5 1099
nixnax 142:54d1543e23e5 1100 // The TCP flag handling is now done
nixnax 142:54d1543e23e5 1101 // first we swap source and destination TCP addresses and insert the new ack and seq numbers
nixnax 147:0f40005cbc5f 1102 swapIpAddresses(); // swap IP source and destination addresses
nixnax 149:969d98f6fb88 1103 swapIpPorts(); // swap IP source and destination ports
nixnax 142:54d1543e23e5 1104
nixnax 158:841592aed956 1105 ppp.tcp->ackTcpR = __REV( ack_out ); // byte reversed - tcp/ip messages are big-endian (high byte first)
nixnax 158:841592aed956 1106 ppp.tcp->seqTcpR = __REV( seq_out ); // byte reversed - tcp/ip messages are big-endian (high byte first)
nixnax 160:bd701ad564cb 1107
nixnax 156:163c23249731 1108 ppp.tcp->flag.All = flagsOut; // update the TCP flags
nixnax 142:54d1543e23e5 1109
nixnax 159:4d1bf96a59cd 1110 // recalculate all the header sizes
nixnax 154:18b2bd92f557 1111 tcpSize = headerSizeTcp + dataLen; // tcp packet size
nixnax 154:18b2bd92f557 1112 int newPacketSize = headerSizeIp + tcpSize; // calculate size of the outgoing packet
nixnax 159:4d1bf96a59cd 1113 ppp.ip->lengthR = __REV16 ( newPacketSize );
nixnax 142:54d1543e23e5 1114 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 1115
nixnax 142:54d1543e23e5 1116 // the header is all set up, now do the IP and TCP checksums
nixnax 146:a45f49a2b29c 1117 IpHeaderCheckSum(); // calculate new IP header checksum
nixnax 170:3d3b2126181c 1118 checkSumPseudoHeader( tcpSize ); // get the TCP pseudo-header checksum
nixnax 159:4d1bf96a59cd 1119 ppp.tcp->checksumR = 0; // before TCP checksum calculations the checksum bytes must be set cleared
nixnax 161:d59f778bc8ab 1120 unsigned int pseudoHeaderSum=dataCheckSum(ppp.tcpStart,tcpSize, 0); // continue the TCP checksum on the whole TCP packet
nixnax 159:4d1bf96a59cd 1121 ppp.tcp->checksumR = __REV16( pseudoHeaderSum); // tcp checksum done, store it in the TCP header
nixnax 160:bd701ad564cb 1122
nixnax 170:3d3b2126181c 1123 dumpHeaderIP(1); // dump outgoing IP header before sending the frame
nixnax 170:3d3b2126181c 1124 dumpHeaderTCP(1); // dump outgoing TCP header before sending the frame
nixnax 173:6774a0c851c4 1125 dumpDataTCP(1); // dump outgoing TCP data before sending the frame
nixnax 226:4898247048c7 1126 #define WAIT_BEFORE_IP_SEND_NO
nixnax 226:4898247048c7 1127 #ifdef WAIT_BEFORE_IP_SEND_YES
nixnax 226:4898247048c7 1128 wait_ms(45); // 45 ms delay before sending frame
nixnax 226:4898247048c7 1129 #endif
nixnax 144:01d98cf7738e 1130 sendPppFrame(); // All preparation complete - send the TCP response
nixnax 226:4898247048c7 1131 #define DUMP_TRANSMITTED_PPP_NO
nixnax 226:4898247048c7 1132 #ifdef DUMP_TRANSMITTED_PPP_YES
nixnax 226:4898247048c7 1133 dumpPPPFrame(); // dump every transmitted ppp frame to debug port
nixnax 226:4898247048c7 1134 #endif
nixnax 142:54d1543e23e5 1135 memset(ppp.pkt.buf+44,0,500); // flush out traces of previous data that we may scan for
nixnax 142:54d1543e23e5 1136 }
nixnax 142:54d1543e23e5 1137
nixnax 142:54d1543e23e5 1138 /// handle an incoming TCP packet
nixnax 142:54d1543e23e5 1139 void TCPpacket()
nixnax 142:54d1543e23e5 1140 {
nixnax 173:6774a0c851c4 1141 dumpHeaderIP(0); // dump incoming packet IP header
nixnax 173:6774a0c851c4 1142 dumpHeaderTCP(0); // dump incoming packet TCP header
nixnax 173:6774a0c851c4 1143 dumpDataTCP(0); // dump incoming packet data
nixnax 142:54d1543e23e5 1144 tcpHandler();
nixnax 142:54d1543e23e5 1145 }
nixnax 142:54d1543e23e5 1146
nixnax 142:54d1543e23e5 1147 /// handle the remaining IP protocols by ignoring them
nixnax 142:54d1543e23e5 1148 void otherProtocol()
nixnax 142:54d1543e23e5 1149 {
nixnax 230:ee097c486e45 1150 debugPuts("Other IP protocol");
nixnax 142:54d1543e23e5 1151 }
nixnax 142:54d1543e23e5 1152
nixnax 142:54d1543e23e5 1153 /// process an incoming IP packet
nixnax 142:54d1543e23e5 1154 void IPframe()
nixnax 142:54d1543e23e5 1155 {
nixnax 167:ff8a2d8beeb1 1156 int protocol = ppp.ip->protocol;
nixnax 142:54d1543e23e5 1157 switch (protocol) {
nixnax 142:54d1543e23e5 1158 case 1:
nixnax 142:54d1543e23e5 1159 ICMPpacket();
nixnax 142:54d1543e23e5 1160 break;
nixnax 142:54d1543e23e5 1161 case 2:
nixnax 142:54d1543e23e5 1162 IGMPpacket();
nixnax 142:54d1543e23e5 1163 break;
nixnax 142:54d1543e23e5 1164 case 17:
nixnax 142:54d1543e23e5 1165 UDPpacket();
nixnax 142:54d1543e23e5 1166 break;
nixnax 142:54d1543e23e5 1167 case 6:
nixnax 142:54d1543e23e5 1168 TCPpacket();
nixnax 142:54d1543e23e5 1169 break;
nixnax 142:54d1543e23e5 1170 default:
nixnax 142:54d1543e23e5 1171 otherProtocol();
nixnax 142:54d1543e23e5 1172 }
nixnax 142:54d1543e23e5 1173 }
nixnax 142:54d1543e23e5 1174
nixnax 227:f87156f9ef11 1175 /// process LCP packets
nixnax 142:54d1543e23e5 1176 void LCPframe()
nixnax 142:54d1543e23e5 1177 {
nixnax 175:b4e6f8a6fe00 1178 int code = ppp.lcp->code;
nixnax 142:54d1543e23e5 1179 switch (code) {
nixnax 227:f87156f9ef11 1180 case 1: // LCP configuration request
nixnax 231:6bd4523833a2 1181 debugPuts("LCP Config");
nixnax 227:f87156f9ef11 1182 if ( __REV16( ppp.lcp->lengthR ) == 4 ) {
nixnax 230:ee097c486e45 1183 debugPuts("LCP Ack\n");
nixnax 227:f87156f9ef11 1184 ppp.lcp->code=2; // acknowledge zero configuration request
nixnax 227:f87156f9ef11 1185 sendPppFrame();
nixnax 227:f87156f9ef11 1186 ppp.lcp->code=1; // request no options
nixnax 227:f87156f9ef11 1187 sendPppFrame();
nixnax 227:f87156f9ef11 1188 } else {
nixnax 230:ee097c486e45 1189 debugPuts("LCP request reject\n");
nixnax 227:f87156f9ef11 1190 ppp.lcp->code=4; // allow only "no options" which means Maximum Receive Unit (MRU) is default 1500 bytes
nixnax 227:f87156f9ef11 1191 sendPppFrame();
nixnax 227:f87156f9ef11 1192 }
nixnax 227:f87156f9ef11 1193 break;
nixnax 227:f87156f9ef11 1194 case 2: // LCP configuration acknowledge
nixnax 230:ee097c486e45 1195 debugPuts("LCP Ack\n"); // don't do anything, i.e. ignore
nixnax 227:f87156f9ef11 1196 break;
nixnax 227:f87156f9ef11 1197 case 5: // LCP end
nixnax 227:f87156f9ef11 1198 ppp.lcp->code=6; // acknowledge
nixnax 227:f87156f9ef11 1199 sendPppFrame(); // acknowledge
nixnax 227:f87156f9ef11 1200 ppp.online=0; // start hunting for connect string again
nixnax 227:f87156f9ef11 1201 pppInitStruct(); // flush the receive buffer
nixnax 230:ee097c486e45 1202 debugPuts("LCP End (Disconnect from host)\n");
nixnax 142:54d1543e23e5 1203 break; // end connection
nixnax 142:54d1543e23e5 1204 default:
nixnax 231:6bd4523833a2 1205 debugPuts("LCP Other\n");
nixnax 142:54d1543e23e5 1206 }
nixnax 142:54d1543e23e5 1207 }
nixnax 142:54d1543e23e5 1208
nixnax 142:54d1543e23e5 1209 /// discard packets that are not IP, IPCP, or LCP
nixnax 142:54d1543e23e5 1210 void discardedFrame()
nixnax 142:54d1543e23e5 1211 {
nixnax 142:54d1543e23e5 1212 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 1213 }
nixnax 142:54d1543e23e5 1214
nixnax 142:54d1543e23e5 1215 /// determine the packet type (IP, IPCP or LCP) of incoming packets
nixnax 142:54d1543e23e5 1216 void determinePacketType()
nixnax 142:54d1543e23e5 1217 {
nixnax 167:ff8a2d8beeb1 1218 if ( ppp.ppp->address != 0xff ) {
nixnax 231:6bd4523833a2 1219 debugPuts("Unexpected: PPP address != ff\n");
nixnax 142:54d1543e23e5 1220 return;
nixnax 142:54d1543e23e5 1221 }
nixnax 167:ff8a2d8beeb1 1222 if ( ppp.ppp->control != 3 ) {
nixnax 231:6bd4523833a2 1223 debugPuts("Unexpected: PPP control != 3\n");
nixnax 142:54d1543e23e5 1224 return;
nixnax 142:54d1543e23e5 1225 }
nixnax 167:ff8a2d8beeb1 1226 unsigned int protocol = __REV16( ppp.ppp->protocolR );
nixnax 167:ff8a2d8beeb1 1227 switch ( protocol ) {
nixnax 167:ff8a2d8beeb1 1228 case 0xc021:
nixnax 142:54d1543e23e5 1229 LCPframe();
nixnax 142:54d1543e23e5 1230 break; // link control
nixnax 167:ff8a2d8beeb1 1231 case 0x8021:
nixnax 142:54d1543e23e5 1232 IPCPframe();
nixnax 142:54d1543e23e5 1233 break; // IP control
nixnax 167:ff8a2d8beeb1 1234 case 0x0021:
nixnax 142:54d1543e23e5 1235 IPframe();
nixnax 142:54d1543e23e5 1236 break; // IP itself
nixnax 142:54d1543e23e5 1237 default:
nixnax 142:54d1543e23e5 1238 discardedFrame();
nixnax 142:54d1543e23e5 1239 }
nixnax 142:54d1543e23e5 1240 }
nixnax 142:54d1543e23e5 1241
nixnax 265:b05d78e226a9 1242
nixnax 265:b05d78e226a9 1243 /// SHA1 library
nixnax 265:b05d78e226a9 1244 /*
nixnax 265:b05d78e226a9 1245 SHA-1 in C
nixnax 265:b05d78e226a9 1246 By Steve Reid <steve@edmweb.com>
nixnax 265:b05d78e226a9 1247 100% Public Domain
nixnax 265:b05d78e226a9 1248 */
nixnax 265:b05d78e226a9 1249
nixnax 265:b05d78e226a9 1250 #define SHA1HANDSOFF
nixnax 265:b05d78e226a9 1251
nixnax 265:b05d78e226a9 1252 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
nixnax 265:b05d78e226a9 1253
nixnax 265:b05d78e226a9 1254 /* blk0() and blk() perform the initial expand. */
nixnax 265:b05d78e226a9 1255 /* I got the idea of expanding during the round function from SSLeay */
nixnax 265:b05d78e226a9 1256 #if BYTE_ORDER == LITTLE_ENDIAN
nixnax 265:b05d78e226a9 1257 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
nixnax 265:b05d78e226a9 1258 |(rol(block->l[i],8)&0x00FF00FF))
nixnax 265:b05d78e226a9 1259 #elif BYTE_ORDER == BIG_ENDIAN
nixnax 265:b05d78e226a9 1260 #define blk0(i) block->l[i]
nixnax 265:b05d78e226a9 1261 #else
nixnax 265:b05d78e226a9 1262 #error "Endianness not defined!"
nixnax 265:b05d78e226a9 1263 #endif
nixnax 265:b05d78e226a9 1264 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
nixnax 265:b05d78e226a9 1265 ^block->l[(i+2)&15]^block->l[i&15],1))
nixnax 265:b05d78e226a9 1266
nixnax 265:b05d78e226a9 1267 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
nixnax 265:b05d78e226a9 1268 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
nixnax 265:b05d78e226a9 1269 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
nixnax 265:b05d78e226a9 1270 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
nixnax 265:b05d78e226a9 1271 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
nixnax 265:b05d78e226a9 1272 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
nixnax 265:b05d78e226a9 1273
nixnax 265:b05d78e226a9 1274
nixnax 265:b05d78e226a9 1275 /* Hash a single 512-bit block. This is the core of the algorithm. */
nixnax 265:b05d78e226a9 1276
nixnax 265:b05d78e226a9 1277 void SHA1Transform(
nixnax 265:b05d78e226a9 1278 uint32_t state[5],
nixnax 265:b05d78e226a9 1279 const unsigned char buffer[64]
nixnax 265:b05d78e226a9 1280 )
nixnax 265:b05d78e226a9 1281 {
nixnax 265:b05d78e226a9 1282 uint32_t a, b, c, d, e;
nixnax 265:b05d78e226a9 1283
nixnax 275:bb2d582c20d6 1284 typedef union {
nixnax 265:b05d78e226a9 1285 unsigned char c[64];
nixnax 265:b05d78e226a9 1286 uint32_t l[16];
nixnax 265:b05d78e226a9 1287 } CHAR64LONG16;
nixnax 265:b05d78e226a9 1288
nixnax 265:b05d78e226a9 1289 #ifdef SHA1HANDSOFF
nixnax 265:b05d78e226a9 1290 CHAR64LONG16 block[1]; /* use array to appear as a pointer */
nixnax 265:b05d78e226a9 1291
nixnax 265:b05d78e226a9 1292 memcpy(block, buffer, 64);
nixnax 265:b05d78e226a9 1293 #else
nixnax 265:b05d78e226a9 1294 /* The following had better never be used because it causes the
nixnax 265:b05d78e226a9 1295 * pointer-to-const buffer to be cast into a pointer to non-const.
nixnax 265:b05d78e226a9 1296 * And the result is written through. I threw a "const" in, hoping
nixnax 265:b05d78e226a9 1297 * this will cause a diagnostic.
nixnax 265:b05d78e226a9 1298 */
nixnax 265:b05d78e226a9 1299 CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
nixnax 265:b05d78e226a9 1300 #endif
nixnax 265:b05d78e226a9 1301 /* Copy context->state[] to working vars */
nixnax 265:b05d78e226a9 1302 a = state[0];
nixnax 265:b05d78e226a9 1303 b = state[1];
nixnax 265:b05d78e226a9 1304 c = state[2];
nixnax 265:b05d78e226a9 1305 d = state[3];
nixnax 265:b05d78e226a9 1306 e = state[4];
nixnax 265:b05d78e226a9 1307 /* 4 rounds of 20 operations each. Loop unrolled. */
nixnax 265:b05d78e226a9 1308 R0(a, b, c, d, e, 0);
nixnax 265:b05d78e226a9 1309 R0(e, a, b, c, d, 1);
nixnax 265:b05d78e226a9 1310 R0(d, e, a, b, c, 2);
nixnax 265:b05d78e226a9 1311 R0(c, d, e, a, b, 3);
nixnax 265:b05d78e226a9 1312 R0(b, c, d, e, a, 4);
nixnax 265:b05d78e226a9 1313 R0(a, b, c, d, e, 5);
nixnax 265:b05d78e226a9 1314 R0(e, a, b, c, d, 6);
nixnax 265:b05d78e226a9 1315 R0(d, e, a, b, c, 7);
nixnax 265:b05d78e226a9 1316 R0(c, d, e, a, b, 8);
nixnax 265:b05d78e226a9 1317 R0(b, c, d, e, a, 9);
nixnax 265:b05d78e226a9 1318 R0(a, b, c, d, e, 10);
nixnax 265:b05d78e226a9 1319 R0(e, a, b, c, d, 11);
nixnax 265:b05d78e226a9 1320 R0(d, e, a, b, c, 12);
nixnax 265:b05d78e226a9 1321 R0(c, d, e, a, b, 13);
nixnax 265:b05d78e226a9 1322 R0(b, c, d, e, a, 14);
nixnax 265:b05d78e226a9 1323 R0(a, b, c, d, e, 15);
nixnax 265:b05d78e226a9 1324 R1(e, a, b, c, d, 16);
nixnax 265:b05d78e226a9 1325 R1(d, e, a, b, c, 17);
nixnax 265:b05d78e226a9 1326 R1(c, d, e, a, b, 18);
nixnax 265:b05d78e226a9 1327 R1(b, c, d, e, a, 19);
nixnax 265:b05d78e226a9 1328 R2(a, b, c, d, e, 20);
nixnax 265:b05d78e226a9 1329 R2(e, a, b, c, d, 21);
nixnax 265:b05d78e226a9 1330 R2(d, e, a, b, c, 22);
nixnax 265:b05d78e226a9 1331 R2(c, d, e, a, b, 23);
nixnax 265:b05d78e226a9 1332 R2(b, c, d, e, a, 24);
nixnax 265:b05d78e226a9 1333 R2(a, b, c, d, e, 25);
nixnax 265:b05d78e226a9 1334 R2(e, a, b, c, d, 26);
nixnax 265:b05d78e226a9 1335 R2(d, e, a, b, c, 27);
nixnax 265:b05d78e226a9 1336 R2(c, d, e, a, b, 28);
nixnax 265:b05d78e226a9 1337 R2(b, c, d, e, a, 29);
nixnax 265:b05d78e226a9 1338 R2(a, b, c, d, e, 30);
nixnax 265:b05d78e226a9 1339 R2(e, a, b, c, d, 31);
nixnax 265:b05d78e226a9 1340 R2(d, e, a, b, c, 32);
nixnax 265:b05d78e226a9 1341 R2(c, d, e, a, b, 33);
nixnax 265:b05d78e226a9 1342 R2(b, c, d, e, a, 34);
nixnax 265:b05d78e226a9 1343 R2(a, b, c, d, e, 35);
nixnax 265:b05d78e226a9 1344 R2(e, a, b, c, d, 36);
nixnax 265:b05d78e226a9 1345 R2(d, e, a, b, c, 37);
nixnax 265:b05d78e226a9 1346 R2(c, d, e, a, b, 38);
nixnax 265:b05d78e226a9 1347 R2(b, c, d, e, a, 39);
nixnax 265:b05d78e226a9 1348 R3(a, b, c, d, e, 40);
nixnax 265:b05d78e226a9 1349 R3(e, a, b, c, d, 41);
nixnax 265:b05d78e226a9 1350 R3(d, e, a, b, c, 42);
nixnax 265:b05d78e226a9 1351 R3(c, d, e, a, b, 43);
nixnax 265:b05d78e226a9 1352 R3(b, c, d, e, a, 44);
nixnax 265:b05d78e226a9 1353 R3(a, b, c, d, e, 45);
nixnax 265:b05d78e226a9 1354 R3(e, a, b, c, d, 46);
nixnax 265:b05d78e226a9 1355 R3(d, e, a, b, c, 47);
nixnax 265:b05d78e226a9 1356 R3(c, d, e, a, b, 48);
nixnax 265:b05d78e226a9 1357 R3(b, c, d, e, a, 49);
nixnax 265:b05d78e226a9 1358 R3(a, b, c, d, e, 50);
nixnax 265:b05d78e226a9 1359 R3(e, a, b, c, d, 51);
nixnax 265:b05d78e226a9 1360 R3(d, e, a, b, c, 52);
nixnax 265:b05d78e226a9 1361 R3(c, d, e, a, b, 53);
nixnax 265:b05d78e226a9 1362 R3(b, c, d, e, a, 54);
nixnax 265:b05d78e226a9 1363 R3(a, b, c, d, e, 55);
nixnax 265:b05d78e226a9 1364 R3(e, a, b, c, d, 56);
nixnax 265:b05d78e226a9 1365 R3(d, e, a, b, c, 57);
nixnax 265:b05d78e226a9 1366 R3(c, d, e, a, b, 58);
nixnax 265:b05d78e226a9 1367 R3(b, c, d, e, a, 59);
nixnax 265:b05d78e226a9 1368 R4(a, b, c, d, e, 60);
nixnax 265:b05d78e226a9 1369 R4(e, a, b, c, d, 61);
nixnax 265:b05d78e226a9 1370 R4(d, e, a, b, c, 62);
nixnax 265:b05d78e226a9 1371 R4(c, d, e, a, b, 63);
nixnax 265:b05d78e226a9 1372 R4(b, c, d, e, a, 64);
nixnax 265:b05d78e226a9 1373 R4(a, b, c, d, e, 65);
nixnax 265:b05d78e226a9 1374 R4(e, a, b, c, d, 66);
nixnax 265:b05d78e226a9 1375 R4(d, e, a, b, c, 67);
nixnax 265:b05d78e226a9 1376 R4(c, d, e, a, b, 68);
nixnax 265:b05d78e226a9 1377 R4(b, c, d, e, a, 69);
nixnax 265:b05d78e226a9 1378 R4(a, b, c, d, e, 70);
nixnax 265:b05d78e226a9 1379 R4(e, a, b, c, d, 71);
nixnax 265:b05d78e226a9 1380 R4(d, e, a, b, c, 72);
nixnax 265:b05d78e226a9 1381 R4(c, d, e, a, b, 73);
nixnax 265:b05d78e226a9 1382 R4(b, c, d, e, a, 74);
nixnax 265:b05d78e226a9 1383 R4(a, b, c, d, e, 75);
nixnax 265:b05d78e226a9 1384 R4(e, a, b, c, d, 76);
nixnax 265:b05d78e226a9 1385 R4(d, e, a, b, c, 77);
nixnax 265:b05d78e226a9 1386 R4(c, d, e, a, b, 78);
nixnax 265:b05d78e226a9 1387 R4(b, c, d, e, a, 79);
nixnax 265:b05d78e226a9 1388 /* Add the working vars back into context.state[] */
nixnax 265:b05d78e226a9 1389 state[0] += a;
nixnax 265:b05d78e226a9 1390 state[1] += b;
nixnax 265:b05d78e226a9 1391 state[2] += c;
nixnax 265:b05d78e226a9 1392 state[3] += d;
nixnax 265:b05d78e226a9 1393 state[4] += e;
nixnax 265:b05d78e226a9 1394 /* Wipe variables */
nixnax 265:b05d78e226a9 1395 a = b = c = d = e = 0;
nixnax 265:b05d78e226a9 1396 #ifdef SHA1HANDSOFF
nixnax 265:b05d78e226a9 1397 memset(block, '\0', sizeof(block));
nixnax 265:b05d78e226a9 1398 #endif
nixnax 265:b05d78e226a9 1399 }
nixnax 265:b05d78e226a9 1400
nixnax 265:b05d78e226a9 1401
nixnax 265:b05d78e226a9 1402 /* SHA1Init - Initialize new context */
nixnax 265:b05d78e226a9 1403
nixnax 265:b05d78e226a9 1404 void SHA1Init(
nixnax 265:b05d78e226a9 1405 SHA1_CTX * context
nixnax 265:b05d78e226a9 1406 )
nixnax 265:b05d78e226a9 1407 {
nixnax 265:b05d78e226a9 1408 /* SHA1 initialization constants */
nixnax 265:b05d78e226a9 1409 context->state[0] = 0x67452301;
nixnax 265:b05d78e226a9 1410 context->state[1] = 0xEFCDAB89;
nixnax 265:b05d78e226a9 1411 context->state[2] = 0x98BADCFE;
nixnax 265:b05d78e226a9 1412 context->state[3] = 0x10325476;
nixnax 265:b05d78e226a9 1413 context->state[4] = 0xC3D2E1F0;
nixnax 265:b05d78e226a9 1414 context->count[0] = context->count[1] = 0;
nixnax 265:b05d78e226a9 1415 }
nixnax 265:b05d78e226a9 1416
nixnax 265:b05d78e226a9 1417
nixnax 265:b05d78e226a9 1418 /* Run your data through this. */
nixnax 265:b05d78e226a9 1419
nixnax 265:b05d78e226a9 1420 void SHA1Update(
nixnax 265:b05d78e226a9 1421 SHA1_CTX * context,
nixnax 265:b05d78e226a9 1422 const unsigned char *data,
nixnax 265:b05d78e226a9 1423 uint32_t len
nixnax 265:b05d78e226a9 1424 )
nixnax 265:b05d78e226a9 1425 {
nixnax 265:b05d78e226a9 1426 uint32_t i;
nixnax 265:b05d78e226a9 1427
nixnax 265:b05d78e226a9 1428 uint32_t j;
nixnax 265:b05d78e226a9 1429
nixnax 265:b05d78e226a9 1430 j = context->count[0];
nixnax 265:b05d78e226a9 1431 if ((context->count[0] += len << 3) < j)
nixnax 265:b05d78e226a9 1432 context->count[1]++;
nixnax 265:b05d78e226a9 1433 context->count[1] += (len >> 29);
nixnax 265:b05d78e226a9 1434 j = (j >> 3) & 63;
nixnax 275:bb2d582c20d6 1435 if ((j + len) > 63) {
nixnax 265:b05d78e226a9 1436 memcpy(&context->buffer[j], data, (i = 64 - j));
nixnax 265:b05d78e226a9 1437 SHA1Transform(context->state, context->buffer);
nixnax 275:bb2d582c20d6 1438 for (; i + 63 < len; i += 64) {
nixnax 265:b05d78e226a9 1439 SHA1Transform(context->state, &data[i]);
nixnax 265:b05d78e226a9 1440 }
nixnax 265:b05d78e226a9 1441 j = 0;
nixnax 275:bb2d582c20d6 1442 } else
nixnax 265:b05d78e226a9 1443 i = 0;
nixnax 265:b05d78e226a9 1444 memcpy(&context->buffer[j], &data[i], len - i);
nixnax 265:b05d78e226a9 1445 }
nixnax 265:b05d78e226a9 1446
nixnax 265:b05d78e226a9 1447
nixnax 265:b05d78e226a9 1448 /* Add padding and return the message digest. */
nixnax 265:b05d78e226a9 1449
nixnax 265:b05d78e226a9 1450 void SHA1Final(
nixnax 265:b05d78e226a9 1451 unsigned char digest[20],
nixnax 265:b05d78e226a9 1452 SHA1_CTX * context
nixnax 265:b05d78e226a9 1453 )
nixnax 265:b05d78e226a9 1454 {
nixnax 265:b05d78e226a9 1455 unsigned i;
nixnax 265:b05d78e226a9 1456
nixnax 265:b05d78e226a9 1457 unsigned char finalcount[8];
nixnax 265:b05d78e226a9 1458
nixnax 265:b05d78e226a9 1459 unsigned char c;
nixnax 265:b05d78e226a9 1460
nixnax 275:bb2d582c20d6 1461 for (i = 0; i < 8; i++) {
nixnax 265:b05d78e226a9 1462 finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
nixnax 265:b05d78e226a9 1463 }
nixnax 265:b05d78e226a9 1464
nixnax 265:b05d78e226a9 1465 c = 0200;
nixnax 265:b05d78e226a9 1466 SHA1Update(context, &c, 1);
nixnax 275:bb2d582c20d6 1467 while ((context->count[0] & 504) != 448) {
nixnax 265:b05d78e226a9 1468 c = 0000;
nixnax 265:b05d78e226a9 1469 SHA1Update(context, &c, 1);
nixnax 265:b05d78e226a9 1470 }
nixnax 265:b05d78e226a9 1471 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
nixnax 275:bb2d582c20d6 1472 for (i = 0; i < 20; i++) {
nixnax 265:b05d78e226a9 1473 digest[i] = (unsigned char)
nixnax 275:bb2d582c20d6 1474 ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
nixnax 265:b05d78e226a9 1475 }
nixnax 265:b05d78e226a9 1476 /* Wipe variables */
nixnax 265:b05d78e226a9 1477 memset(context, '\0', sizeof(*context));
nixnax 265:b05d78e226a9 1478 memset(&finalcount, '\0', sizeof(finalcount));
nixnax 265:b05d78e226a9 1479 }
nixnax 265:b05d78e226a9 1480
nixnax 265:b05d78e226a9 1481 void sha1(
nixnax 265:b05d78e226a9 1482 char *hash_out,
nixnax 265:b05d78e226a9 1483 const char *str,
nixnax 265:b05d78e226a9 1484 int len)
nixnax 265:b05d78e226a9 1485 {
nixnax 265:b05d78e226a9 1486 SHA1_CTX ctx;
nixnax 265:b05d78e226a9 1487 unsigned int ii;
nixnax 265:b05d78e226a9 1488
nixnax 265:b05d78e226a9 1489 SHA1Init(&ctx);
nixnax 265:b05d78e226a9 1490 for (ii=0; ii<len; ii+=1)
nixnax 265:b05d78e226a9 1491 SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
nixnax 265:b05d78e226a9 1492 SHA1Final((unsigned char *)hash_out, &ctx);
nixnax 265:b05d78e226a9 1493 // hash_out[20] = '\0';
nixnax 265:b05d78e226a9 1494 }
nixnax 265:b05d78e226a9 1495
nixnax 265:b05d78e226a9 1496
nixnax 146:a45f49a2b29c 1497 /// a sniffer tool to assist in figuring out where in the code we are having characters in the input buffer
nixnax 146:a45f49a2b29c 1498 void sniff()
nixnax 146:a45f49a2b29c 1499 {
nixnax 221:1d7c32d8256b 1500 if ( pc.readable() ) debugPuts( "Sniff - Char available!\n" ); // if this prints anything it means there is a character in the serial receive buffer
nixnax 146:a45f49a2b29c 1501 }
nixnax 146:a45f49a2b29c 1502
nixnax 142:54d1543e23e5 1503 /// scan the PPP serial input stream for frame start markers
nixnax 143:c5019f856a56 1504 void waitForPppFrame()
nixnax 142:54d1543e23e5 1505 {
nixnax 142:54d1543e23e5 1506 while(1) {
nixnax 266:47eea4b910ad 1507 if ( ppp.rx.head != ppp.rx.tail ) { // check for something in the receive buffer
nixnax 142:54d1543e23e5 1508 int oldTail = ppp.rx.tail; // remember where the character is located in the buffer
nixnax 142:54d1543e23e5 1509 int rx = pc_getBuf(); // get the character
nixnax 238:f39289ed5a2e 1510 if (rx==0x7e) { // check for frame start/end character 0x7e
nixnax 260:2c9a01c9d03e 1511 if (ppp.firstFrame) { // is this the start of the first frame
nixnax 142:54d1543e23e5 1512 ppp.rx.rtail = ppp.rx.tail; // update real-time tail with the virtual tail
nixnax 142:54d1543e23e5 1513 ppp.hdlc.frameStartIndex = ppp.rx.tail; // remember where first frame started
nixnax 181:bcfe7fe2142d 1514 ppp.firstFrame=0; // clear first frame flag
nixnax 142:54d1543e23e5 1515 } else {
nixnax 142:54d1543e23e5 1516 ppp.hdlc.frameEndIndex=oldTail; // mark the frame end character
nixnax 142:54d1543e23e5 1517 processPPPFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); // process the frame
nixnax 142:54d1543e23e5 1518 ppp.rx.rtail = ppp.rx.tail; // update real-time tail with the virtual tail
nixnax 142:54d1543e23e5 1519 ppp.hdlc.frameStartIndex = ppp.rx.tail; // remember where next frame started
nixnax 142:54d1543e23e5 1520 break;
nixnax 142:54d1543e23e5 1521 }
nixnax 142:54d1543e23e5 1522 }
nixnax 142:54d1543e23e5 1523 }
nixnax 210:a2a651249a9a 1524 // change below to YES to enable the serial to UDP demo
nixnax 210:a2a651249a9a 1525 #define SERIAL_TO_UDP_NO
nixnax 210:a2a651249a9a 1526 #ifdef SERIAL_TO_UDP_YES
nixnax 210:a2a651249a9a 1527 sendUdpData(); // demo that sends characters received on the DEBUG serial port via UDP to another host
nixnax 210:a2a651249a9a 1528 #endif
nixnax 142:54d1543e23e5 1529 }
nixnax 142:54d1543e23e5 1530 }
nixnax 142:54d1543e23e5 1531
nixnax 265:b05d78e226a9 1532
nixnax 209:aa514cd631ee 1533 /// Wait for a dial-up modem connect command ("CLIENT") from the host PC, if found, we set ppp.online to true, which starts the IP packet scanner.
nixnax 209:aa514cd631ee 1534 // Note: a 0x7E in the input stream (ppp start of frame character) will also set ppp.online to true - see the code in pppReceiveHandler()
nixnax 153:7993def8663f 1535 void waitForPcConnectString()
nixnax 142:54d1543e23e5 1536 {
nixnax 142:54d1543e23e5 1537 while(ppp.online == 0) {
nixnax 142:54d1543e23e5 1538 // search for Windows Dialup Networking "Direct Connection Between Two Computers" expected connect string
nixnax 142:54d1543e23e5 1539 char * found1 = strstr( (char *)ppp.rx.buf, "CLIENT" );
nixnax 142:54d1543e23e5 1540 if (found1 != NULL) {
nixnax 230:ee097c486e45 1541 if (v0) debugPuts("Connected: Found connect string \"CLIENT\", sent \"CLIENTSERVER\"\n");
nixnax 208:73086736610a 1542 // respond with Windows Dialup networking expected "Direct Connection Between Two Computers" response string
nixnax 142:54d1543e23e5 1543 pc.puts("CLIENTSERVER");
nixnax 142:54d1543e23e5 1544 ppp.online=1; // we are connected - set flag so we stop looking for the connect string
nixnax 142:54d1543e23e5 1545 }
nixnax 142:54d1543e23e5 1546 }
nixnax 142:54d1543e23e5 1547 }
nixnax 142:54d1543e23e5 1548
nixnax 144:01d98cf7738e 1549 /// Initialize PPP data structure and set serial port(s) baud rate(s)
nixnax 144:01d98cf7738e 1550 void initializePpp()
nixnax 142:54d1543e23e5 1551 {
nixnax 210:a2a651249a9a 1552 #ifdef SERIAL_PORT_MONITOR_YES
nixnax 144:01d98cf7738e 1553 debugBaudRate(115200); // baud rate for (optional) debug serial port
nixnax 230:ee097c486e45 1554 debugPuts("\x1b[2J\x1b[H\x1b[30m"); // VT100 codes for clear_screen, home, black_text - Tera Term is a handy VT100 terminal
nixnax 184:4133f557d4d4 1555 wait_ms(200); // a brief wait so a human can see the reset event
nixnax 258:1eb9abd66922 1556 debugPuts("mbed PPP-Blinky HTTP & WebSocket server ready :)\n");
nixnax 210:a2a651249a9a 1557 #endif
nixnax 179:ba2b2ddd0da5 1558 pppInitStruct(); // initialize all the variables/properties/buffers
nixnax 179:ba2b2ddd0da5 1559 pc.baud(115200); // pc serial port acting as a dial-up modem - for PPP traffic
nixnax 179:ba2b2ddd0da5 1560 pc.attach(&pppReceiveHandler, RawSerial::RxIrq); // set up serial port receive interrupt handler
nixnax 143:c5019f856a56 1561 }
nixnax 265:b05d78e226a9 1562