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:
Wed Nov 14 08:44:44 2018 +0000
Revision:
233:1b590d78ec9c
Parent:
231:6bd4523833a2
Child:
237:796851817c26
restoring RXBUFLEN to 1500

Who changed what in which revision?

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