Small Internet Protocol Stack using a standard serial port.
PPP-Blinky - TCP/IP Networking Over a Serial Port
Note: The source code is at the bottom of this page.
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:
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:
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.
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
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
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!
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.
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.
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.
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.
main.cpp@68:0b74763ae67f, 2017-06-05 (annotated)
- Committer:
- nixnax
- Date:
- Mon Jun 05 22:45:39 2017 +0000
- Revision:
- 68:0b74763ae67f
- Parent:
- 67:a63e3486bcda
- Child:
- 69:23f560087c16
Removed 200ms delay for faster response; Calculation of Content-Length
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nixnax | 0:2cf4880c312a | 1 | #include "mbed.h" |
nixnax | 0:2cf4880c312a | 2 | |
nixnax | 29:30de79d658f6 | 3 | // Copyright 2016 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 | 12:db0dc91f0231 | 4 | |
nixnax | 41:e58a5a09f411 | 5 | // PPP-Blinky - "My Internet Of Thing" |
nixnax | 41:e58a5a09f411 | 6 | // A Tiny Webserver Using Windows XP/7/8/10 Networking Over A Serial Port. |
nixnax | 0:2cf4880c312a | 7 | |
nixnax | 41:e58a5a09f411 | 8 | // Also receives UDP packets and responds to ping (ICMP Echo requests) |
nixnax | 4:a469050d5b80 | 9 | |
nixnax | 41:e58a5a09f411 | 10 | // Notes and Instructions |
nixnax | 41:e58a5a09f411 | 11 | // http://bit.ly/PPP-Blinky-Instructions |
nixnax | 0:2cf4880c312a | 12 | |
nixnax | 41:e58a5a09f411 | 13 | // Handy reading material |
nixnax | 6:fba4c2e817b8 | 14 | // https://technet.microsoft.com/en-us/library/cc957992.aspx |
nixnax | 9:0992486d4a30 | 15 | // https://en.wikibooks.org/wiki/Serial_Programming/IP_Over_Serial_Connections |
nixnax | 39:b90183d35f1e | 16 | // http://bit.ly/dialup777error - how to solve Dial Up Error 777 in Windows 7/8/10 |
nixnax | 41:e58a5a09f411 | 17 | // http://atari.kensclassics.org/wcomlog.htm |
nixnax | 6:fba4c2e817b8 | 18 | |
nixnax | 29:30de79d658f6 | 19 | // Handy tools |
nixnax | 44:d0c61ae49ea5 | 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 | 44:d0c61ae49ea5 | 21 | // Wireshark - can't monitor Dial-Up network packets on windows, but useful - can import our dumpFrame routine's hex output |
nixnax | 29:30de79d658f6 | 22 | // Microsoft network monitor - real-time monitoring of all our packets |
nixnax | 29:30de79d658f6 | 23 | // http://pingtester.net/ - nice tool for high rate ping testing |
nixnax | 29:30de79d658f6 | 24 | // http://www.sunshine2k.de/coding/javascript/crc/crc_js.html - Correctly calculates the 16-bit FCS (crc) on our frames (Choose CRC16_CCITT_FALSE) |
nixnax | 41:e58a5a09f411 | 25 | // The curl.exe program in Windows Powershell - use it like this to stress test the webserver: while (1) { curl 172.10.10.1 } |
nixnax | 29:30de79d658f6 | 26 | // https://technet.microsoft.com/en-us/sysinternals/pstools.aspx - psping for fast testing of ICMP ping function |
nixnax | 41:e58a5a09f411 | 27 | // https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky |
nixnax | 41:e58a5a09f411 | 28 | |
nixnax | 66:f005b9fdf4d1 | 29 | |
nixnax | 66:f005b9fdf4d1 | 30 | // The #define below enables/disables a SECOND (optional) serial port that prints out interesting diagnostic messages. |
nixnax | 66:f005b9fdf4d1 | 31 | // Change to SERIAL_PORT_MONITOR_YES to enable diagnostics messages. You need to wire a second serial port to your mbed hardware to monitor this. |
nixnax | 48:6352c1411c5f | 32 | #define SERIAL_PORT_MONITOR_NO /* or SERIAL_PORT_MONITOR_YES */ |
nixnax | 0:2cf4880c312a | 33 | |
nixnax | 41:e58a5a09f411 | 34 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 66:f005b9fdf4d1 | 35 | Serial xx(PC_10, PC_11); // Not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO |
nixnax | 42:4de44be70bfd | 36 | #define debug(x...) xx.printf (x) |
nixnax | 41:e58a5a09f411 | 37 | #else |
nixnax | 42:4de44be70bfd | 38 | #define debug(x...) {} |
nixnax | 41:e58a5a09f411 | 39 | #endif |
nixnax | 41:e58a5a09f411 | 40 | |
nixnax | 66:f005b9fdf4d1 | 41 | // verbosity flag used in debug printouts - change to 0 to see less debug info. Lots of interesting info. |
nixnax | 66:f005b9fdf4d1 | 42 | #define v0 1 |
nixnax | 66:f005b9fdf4d1 | 43 | // verbosity flag used in debug printouts - change to 0 to see less debug info. Lots of interesting info. |
nixnax | 66:f005b9fdf4d1 | 44 | #define v1 1 |
nixnax | 29:30de79d658f6 | 45 | |
nixnax | 66:f005b9fdf4d1 | 46 | // this is the webpage we serve when we get an HTTP request |
nixnax | 66:f005b9fdf4d1 | 47 | // keep size under 900 bytes to fit in a single frame |
nixnax | 66:f005b9fdf4d1 | 48 | const static char ourWebPage[] = "\ |
nixnax | 66:f005b9fdf4d1 | 49 | <!DOCTYPE html>\ |
nixnax | 66:f005b9fdf4d1 | 50 | <html>\ |
nixnax | 66:f005b9fdf4d1 | 51 | <head>\ |
nixnax | 66:f005b9fdf4d1 | 52 | <title>mbed-PPP-Blinky</title>\ |
nixnax | 66:f005b9fdf4d1 | 53 | <script>\ |
nixnax | 66:f005b9fdf4d1 | 54 | window.onload=function(){\ |
nixnax | 66:f005b9fdf4d1 | 55 | setInterval(function(){function x(){return document.getElementById('w');};\ |
nixnax | 66:f005b9fdf4d1 | 56 | x().textContent = parseInt(x().textContent)+1;},100);};\ |
nixnax | 66:f005b9fdf4d1 | 57 | </script>\ |
nixnax | 66:f005b9fdf4d1 | 58 | </head>\ |
nixnax | 66:f005b9fdf4d1 | 59 | <body style=\"font-family: sans-serif; font-size:30px; color:#807070\">\ |
nixnax | 66:f005b9fdf4d1 | 60 | <h1>mbed PPP-Blinky Up and Running</h1>\ |
nixnax | 66:f005b9fdf4d1 | 61 | <h1 id=\"w\" style=\"text-align:center;\">0</h1>\ |
nixnax | 66:f005b9fdf4d1 | 62 | <h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\ |
nixnax | 66:f005b9fdf4d1 | 63 | </body>\ |
nixnax | 68:0b74763ae67f | 64 | </html>"; // current size is approximately 470 characters |
nixnax | 66:f005b9fdf4d1 | 65 | |
nixnax | 66:f005b9fdf4d1 | 66 | // The serial port on your mbed hardware. Your PC should view this as a standard dial-up networking modem. See instructions at the top. |
nixnax | 66:f005b9fdf4d1 | 67 | // On a typical mbed hardware platform this is a USB virtual com port (VCP) |
nixnax | 66:f005b9fdf4d1 | 68 | Serial pc(USBTX, USBRX); // usb virtual com port |
nixnax | 29:30de79d658f6 | 69 | |
nixnax | 29:30de79d658f6 | 70 | DigitalOut led1(LED1); // this led toggles when a packet is received |
nixnax | 4:a469050d5b80 | 71 | |
nixnax | 66:f005b9fdf4d1 | 72 | // the standard hdlc frame start/end character. It's the tilde character "~" |
nixnax | 4:a469050d5b80 | 73 | #define FRAME_7E (0x7e) |
nixnax | 29:30de79d658f6 | 74 | |
nixnax | 29:30de79d658f6 | 75 | // the serial port receive buffer and packet buffer |
nixnax | 29:30de79d658f6 | 76 | #define BUFLEN (1<<12) |
nixnax | 66:f005b9fdf4d1 | 77 | char rxbufppp[BUFLEN]; // BUFLEN MUST be a power of two because we use & operator for fast wrap-around in rxHandler |
nixnax | 66:f005b9fdf4d1 | 78 | char hdlcBuffer[2000]; // send and receive buffer for unstuffed (decoded) hdlc frames |
nixnax | 0:2cf4880c312a | 79 | |
nixnax | 29:30de79d658f6 | 80 | // a structure to keep all our ppp globals in |
nixnax | 29:30de79d658f6 | 81 | struct pppType { |
nixnax | 38:ab582987926e | 82 | int online; // we hunt for a PPP connection if this is zero |
nixnax | 38:ab582987926e | 83 | int ident; // our IP ident value |
nixnax | 38:ab582987926e | 84 | unsigned int seq; // our TCP sequence number |
nixnax | 38:ab582987926e | 85 | int crc; // for calculating IP and TCP CRCs |
nixnax | 38:ab582987926e | 86 | int ledState; // state of LED1 |
nixnax | 4:a469050d5b80 | 87 | struct { |
nixnax | 20:5db9b77b38a6 | 88 | char * buf; |
nixnax | 29:30de79d658f6 | 89 | volatile int head; |
nixnax | 29:30de79d658f6 | 90 | volatile int tail; |
nixnax | 38:ab582987926e | 91 | } rx; // serial port objects |
nixnax | 4:a469050d5b80 | 92 | struct { |
nixnax | 38:ab582987926e | 93 | int len; // number of bytes in buffer |
nixnax | 38:ab582987926e | 94 | int crc; // PPP CRC (frame check) |
nixnax | 38:ab582987926e | 95 | char * buf; // the actual buffer |
nixnax | 38:ab582987926e | 96 | } pkt; // ppp buffer objects |
nixnax | 50:ad4e7c3c88e5 | 97 | struct { |
nixnax | 50:ad4e7c3c88e5 | 98 | int frameStartIndex; // frame start marker |
nixnax | 50:ad4e7c3c88e5 | 99 | int frameEndIndex; // frame end marker |
nixnax | 50:ad4e7c3c88e5 | 100 | int frameBusy; // busy capturing a frame |
nixnax | 50:ad4e7c3c88e5 | 101 | } hdlc; // hdlc frame objects |
nixnax | 29:30de79d658f6 | 102 | }; |
nixnax | 31:e000c1b9c565 | 103 | |
nixnax | 29:30de79d658f6 | 104 | pppType ppp; // our global - definitely not thread safe |
nixnax | 0:2cf4880c312a | 105 | |
nixnax | 60:2b770949c911 | 106 | // Initialize our globals |
nixnax | 29:30de79d658f6 | 107 | void pppInitStruct() |
nixnax | 29:30de79d658f6 | 108 | { |
nixnax | 29:30de79d658f6 | 109 | ppp.online=0; |
nixnax | 49:2213f9c132b2 | 110 | ppp.rx.buf=rxbufppp; |
nixnax | 29:30de79d658f6 | 111 | ppp.rx.tail=0; |
nixnax | 29:30de79d658f6 | 112 | ppp.rx.head=0; |
nixnax | 52:accc3026b2b0 | 113 | ppp.pkt.buf=hdlcBuffer; |
nixnax | 29:30de79d658f6 | 114 | ppp.pkt.len=0; |
nixnax | 29:30de79d658f6 | 115 | ppp.ident=0; |
nixnax | 35:e7068df4d971 | 116 | ppp.seq=1000; |
nixnax | 29:30de79d658f6 | 117 | ppp.ledState=0; |
nixnax | 50:ad4e7c3c88e5 | 118 | ppp.hdlc.frameBusy=0; |
nixnax | 29:30de79d658f6 | 119 | } |
nixnax | 26:11f4eb2663a7 | 120 | |
nixnax | 43:aa57db08995d | 121 | void led1Toggle() |
nixnax | 43:aa57db08995d | 122 | { |
nixnax | 43:aa57db08995d | 123 | ppp.ledState = ppp.ledState? 0 : 1; |
nixnax | 43:aa57db08995d | 124 | led1 = ppp.ledState; |
nixnax | 43:aa57db08995d | 125 | } |
nixnax | 43:aa57db08995d | 126 | |
nixnax | 29:30de79d658f6 | 127 | void crcReset() |
nixnax | 29:30de79d658f6 | 128 | { |
nixnax | 29:30de79d658f6 | 129 | ppp.crc=0xffff; // crc restart |
nixnax | 29:30de79d658f6 | 130 | } |
nixnax | 4:a469050d5b80 | 131 | |
nixnax | 29:30de79d658f6 | 132 | void crcDo(int x) // cumulative crc |
nixnax | 29:30de79d658f6 | 133 | { |
nixnax | 29:30de79d658f6 | 134 | for (int i=0; i<8; i++) { |
nixnax | 29:30de79d658f6 | 135 | ppp.crc=((ppp.crc&1)^(x&1))?(ppp.crc>>1)^0x8408:ppp.crc>>1; // crc calculator |
nixnax | 29:30de79d658f6 | 136 | x>>=1; |
nixnax | 29:30de79d658f6 | 137 | } |
nixnax | 29:30de79d658f6 | 138 | } |
nixnax | 29:30de79d658f6 | 139 | |
nixnax | 29:30de79d658f6 | 140 | int crcBuf(char * buf, int size) // crc on an entire block of memory |
nixnax | 29:30de79d658f6 | 141 | { |
nixnax | 31:e000c1b9c565 | 142 | crcReset(); |
nixnax | 31:e000c1b9c565 | 143 | for(int i=0; i<size; i++)crcDo(*buf++); |
nixnax | 29:30de79d658f6 | 144 | return ppp.crc; |
nixnax | 29:30de79d658f6 | 145 | } |
nixnax | 0:2cf4880c312a | 146 | |
nixnax | 0:2cf4880c312a | 147 | void rxHandler() // serial port receive interrupt handler |
nixnax | 0:2cf4880c312a | 148 | { |
nixnax | 17:4918c893d802 | 149 | while ( pc.readable() ) { |
nixnax | 20:5db9b77b38a6 | 150 | int hd = (ppp.rx.head+1)&(BUFLEN-1); // increment/wrap |
nixnax | 20:5db9b77b38a6 | 151 | if ( hd == ppp.rx.tail ) break; // watch for buffer full |
nixnax | 17:4918c893d802 | 152 | ppp.rx.buf[ppp.rx.head]=pc.getc(); // insert in rx buffer |
nixnax | 20:5db9b77b38a6 | 153 | ppp.rx.head = hd; // update head pointer |
nixnax | 15:b0154c910143 | 154 | } |
nixnax | 0:2cf4880c312a | 155 | } |
nixnax | 0:2cf4880c312a | 156 | |
nixnax | 22:00df34cd4d7e | 157 | int rxbufNotEmpty() // check if rx buffer has data |
nixnax | 0:2cf4880c312a | 158 | { |
nixnax | 22:00df34cd4d7e | 159 | __disable_irq(); // critical section start |
nixnax | 43:aa57db08995d | 160 | int emptyStatus = (ppp.rx.head==ppp.rx.tail) ? 0 : 1 ; |
nixnax | 22:00df34cd4d7e | 161 | __enable_irq(); // critical section end |
nixnax | 43:aa57db08995d | 162 | return emptyStatus; |
nixnax | 0:2cf4880c312a | 163 | } |
nixnax | 0:2cf4880c312a | 164 | |
nixnax | 0:2cf4880c312a | 165 | int pc_getBuf() // get one character from the buffer |
nixnax | 0:2cf4880c312a | 166 | { |
nixnax | 63:9253b0e1b7d8 | 167 | __disable_irq(); // critical section start |
nixnax | 63:9253b0e1b7d8 | 168 | int x = ppp.rx.buf[ ppp.rx.tail ]; |
nixnax | 63:9253b0e1b7d8 | 169 | ppp.rx.tail=(ppp.rx.tail+1)&(BUFLEN-1); |
nixnax | 63:9253b0e1b7d8 | 170 | __enable_irq(); // critical section end |
nixnax | 63:9253b0e1b7d8 | 171 | return x; |
nixnax | 0:2cf4880c312a | 172 | } |
nixnax | 0:2cf4880c312a | 173 | |
nixnax | 50:ad4e7c3c88e5 | 174 | void processHDLCFrame(int start, int end) // process received frame |
nixnax | 29:30de79d658f6 | 175 | { |
nixnax | 38:ab582987926e | 176 | led1Toggle(); // change led1 state on every frame we receive |
nixnax | 29:30de79d658f6 | 177 | if(start==end) { |
nixnax | 29:30de79d658f6 | 178 | pc.putc(0x7e); |
nixnax | 29:30de79d658f6 | 179 | return; |
nixnax | 29:30de79d658f6 | 180 | } |
nixnax | 9:0992486d4a30 | 181 | crcReset(); |
nixnax | 9:0992486d4a30 | 182 | char * dest = ppp.pkt.buf; |
nixnax | 9:0992486d4a30 | 183 | ppp.pkt.len=0; |
nixnax | 9:0992486d4a30 | 184 | int unstuff=0; |
nixnax | 17:4918c893d802 | 185 | int idx = start; |
nixnax | 17:4918c893d802 | 186 | while(1) { |
nixnax | 9:0992486d4a30 | 187 | if (unstuff==0) { |
nixnax | 49:2213f9c132b2 | 188 | if (rxbufppp[idx]==0x7d) unstuff=1; |
nixnax | 29:30de79d658f6 | 189 | else { |
nixnax | 49:2213f9c132b2 | 190 | *dest = rxbufppp[idx]; |
nixnax | 29:30de79d658f6 | 191 | ppp.pkt.len++; |
nixnax | 29:30de79d658f6 | 192 | dest++; |
nixnax | 49:2213f9c132b2 | 193 | crcDo(rxbufppp[idx]); |
nixnax | 29:30de79d658f6 | 194 | } |
nixnax | 66:f005b9fdf4d1 | 195 | } else { // unstuff characters prefixed with 0x7d |
nixnax | 49:2213f9c132b2 | 196 | *dest = rxbufppp[idx]^0x20; |
nixnax | 29:30de79d658f6 | 197 | ppp.pkt.len++; |
nixnax | 29:30de79d658f6 | 198 | dest++; |
nixnax | 49:2213f9c132b2 | 199 | crcDo(rxbufppp[idx]^0x20); |
nixnax | 9:0992486d4a30 | 200 | unstuff=0; |
nixnax | 9:0992486d4a30 | 201 | } |
nixnax | 17:4918c893d802 | 202 | idx = (idx+1) & (BUFLEN-1); |
nixnax | 17:4918c893d802 | 203 | if (idx == end) break; |
nixnax | 9:0992486d4a30 | 204 | } |
nixnax | 29:30de79d658f6 | 205 | ppp.pkt.crc = ppp.crc & 0xffff; |
nixnax | 9:0992486d4a30 | 206 | if (ppp.pkt.crc == 0xf0b8) { // check for good CRC |
nixnax | 16:cb0b80c24ba2 | 207 | void determinePacketType(); // declaration only |
nixnax | 9:0992486d4a30 | 208 | determinePacketType(); |
nixnax | 29:30de79d658f6 | 209 | } else if (v0) { |
nixnax | 66:f005b9fdf4d1 | 210 | debug("PPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // ignore packets with CRC errors but print a debug line |
nixnax | 9:0992486d4a30 | 211 | } |
nixnax | 9:0992486d4a30 | 212 | } |
nixnax | 9:0992486d4a30 | 213 | |
nixnax | 38:ab582987926e | 214 | // Note - the hex output of dumpFrame() can be imported into WireShark |
nixnax | 38:ab582987926e | 215 | // Capture the frame's hex output in your terminal program and save as a text file |
nixnax | 38:ab582987926e | 216 | // In WireShark, use "Import Hex File". Options are: Offset=None, Protocol=PPP. |
nixnax | 29:30de79d658f6 | 217 | void dumpFrame() |
nixnax | 29:30de79d658f6 | 218 | { |
nixnax | 42:4de44be70bfd | 219 | for(int i=0; i<ppp.pkt.len; i++) debug("%02x ", ppp.pkt.buf[i]); |
nixnax | 42:4de44be70bfd | 220 | debug(" C=%02x %02x L=%d\n", ppp.pkt.crc&0xff, (ppp.pkt.crc>>8)&0xff, ppp.pkt.len); |
nixnax | 16:cb0b80c24ba2 | 221 | } |
nixnax | 16:cb0b80c24ba2 | 222 | |
nixnax | 29:30de79d658f6 | 223 | void hdlcPut(int ch) // do hdlc handling of special (flag) characters |
nixnax | 29:30de79d658f6 | 224 | { |
nixnax | 29:30de79d658f6 | 225 | if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) ) { |
nixnax | 29:30de79d658f6 | 226 | pc.putc(0x7d); |
nixnax | 66:f005b9fdf4d1 | 227 | pc.putc(ch^0x20); // these characters need special handling |
nixnax | 29:30de79d658f6 | 228 | } else { |
nixnax | 29:30de79d658f6 | 229 | pc.putc(ch); |
nixnax | 29:30de79d658f6 | 230 | } |
nixnax | 11:f58998c24f0b | 231 | } |
nixnax | 9:0992486d4a30 | 232 | |
nixnax | 66:f005b9fdf4d1 | 233 | void sendFrame() // send a PPP frame in HDLC format |
nixnax | 29:30de79d658f6 | 234 | { |
nixnax | 17:4918c893d802 | 235 | int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc |
nixnax | 12:db0dc91f0231 | 236 | ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc) |
nixnax | 12:db0dc91f0231 | 237 | ppp.pkt.buf[ ppp.pkt.len-1 ] = (~crc>>8); // fcs hi (crc) |
nixnax | 16:cb0b80c24ba2 | 238 | pc.putc(0x7e); // hdlc start-of-frame "flag" |
nixnax | 29:30de79d658f6 | 239 | for(int i=0; i<ppp.pkt.len; i++) hdlcPut( ppp.pkt.buf[i] ); |
nixnax | 16:cb0b80c24ba2 | 240 | pc.putc(0x7e); // hdlc end-of-frame "flag" |
nixnax | 9:0992486d4a30 | 241 | } |
nixnax | 9:0992486d4a30 | 242 | |
nixnax | 66:f005b9fdf4d1 | 243 | void ipConfigRequestHandler() |
nixnax | 29:30de79d658f6 | 244 | { |
nixnax | 64:677b9713a120 | 245 | debug("IPCP Conf "); |
nixnax | 9:0992486d4a30 | 246 | if ( ppp.pkt.buf[7] != 4 ) { |
nixnax | 66:f005b9fdf4d1 | 247 | debug("Rej\n"); // reject any options that are requested |
nixnax | 9:0992486d4a30 | 248 | ppp.pkt.buf[4]=4; |
nixnax | 9:0992486d4a30 | 249 | sendFrame(); |
nixnax | 9:0992486d4a30 | 250 | } else { |
nixnax | 64:677b9713a120 | 251 | debug("Ack\n"); |
nixnax | 9:0992486d4a30 | 252 | ppp.pkt.buf[4]=2; // ack the minimum |
nixnax | 9:0992486d4a30 | 253 | sendFrame(); // acknowledge |
nixnax | 64:677b9713a120 | 254 | debug("IPCP Ask\n"); |
nixnax | 9:0992486d4a30 | 255 | // send our own request now |
nixnax | 12:db0dc91f0231 | 256 | ppp.pkt.buf[4]=1; // request no options |
nixnax | 9:0992486d4a30 | 257 | ppp.pkt.buf[5]++; // next sequence |
nixnax | 9:0992486d4a30 | 258 | sendFrame(); // this is our request |
nixnax | 9:0992486d4a30 | 259 | } |
nixnax | 9:0992486d4a30 | 260 | } |
nixnax | 9:0992486d4a30 | 261 | |
nixnax | 29:30de79d658f6 | 262 | void ipAckHandler() |
nixnax | 29:30de79d658f6 | 263 | { |
nixnax | 64:677b9713a120 | 264 | debug("IPCP Grant\n"); |
nixnax | 29:30de79d658f6 | 265 | } |
nixnax | 9:0992486d4a30 | 266 | |
nixnax | 29:30de79d658f6 | 267 | void ipNackHandler() |
nixnax | 29:30de79d658f6 | 268 | { |
nixnax | 64:677b9713a120 | 269 | debug("IPCP Nack\n"); |
nixnax | 29:30de79d658f6 | 270 | } |
nixnax | 9:0992486d4a30 | 271 | |
nixnax | 29:30de79d658f6 | 272 | void ipDefaultHandler() |
nixnax | 29:30de79d658f6 | 273 | { |
nixnax | 64:677b9713a120 | 274 | debug("IPCP Other\n"); |
nixnax | 29:30de79d658f6 | 275 | } |
nixnax | 29:30de79d658f6 | 276 | |
nixnax | 29:30de79d658f6 | 277 | void IPCPframe() |
nixnax | 29:30de79d658f6 | 278 | { |
nixnax | 9:0992486d4a30 | 279 | int code = ppp.pkt.buf[4]; // packet type is here |
nixnax | 9:0992486d4a30 | 280 | switch (code) { |
nixnax | 29:30de79d658f6 | 281 | case 1: |
nixnax | 66:f005b9fdf4d1 | 282 | ipConfigRequestHandler(); |
nixnax | 29:30de79d658f6 | 283 | break; |
nixnax | 29:30de79d658f6 | 284 | case 2: |
nixnax | 29:30de79d658f6 | 285 | ipAckHandler(); |
nixnax | 29:30de79d658f6 | 286 | break; |
nixnax | 29:30de79d658f6 | 287 | case 3: |
nixnax | 29:30de79d658f6 | 288 | ipNackHandler(); |
nixnax | 29:30de79d658f6 | 289 | break; |
nixnax | 29:30de79d658f6 | 290 | default: |
nixnax | 29:30de79d658f6 | 291 | ipDefaultHandler(); |
nixnax | 9:0992486d4a30 | 292 | } |
nixnax | 29:30de79d658f6 | 293 | } |
nixnax | 9:0992486d4a30 | 294 | |
nixnax | 29:30de79d658f6 | 295 | void UDPpacket() |
nixnax | 29:30de79d658f6 | 296 | { |
nixnax | 12:db0dc91f0231 | 297 | char * udpPkt = ppp.pkt.buf+4; // udp packet start |
nixnax | 16:cb0b80c24ba2 | 298 | int headerSizeIP = (( udpPkt[0]&0xf)*4); |
nixnax | 29:30de79d658f6 | 299 | char * udpBlock = udpPkt + headerSizeIP; // udp info start |
nixnax | 63:9253b0e1b7d8 | 300 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 12:db0dc91f0231 | 301 | char * udpSrc = udpBlock; // source port |
nixnax | 12:db0dc91f0231 | 302 | char * udpDst = udpBlock+2; // destination port |
nixnax | 63:9253b0e1b7d8 | 303 | #endif |
nixnax | 12:db0dc91f0231 | 304 | char * udpLen = udpBlock+4; // udp data length |
nixnax | 12:db0dc91f0231 | 305 | char * udpInf = udpBlock+8; // actual start of info |
nixnax | 63:9253b0e1b7d8 | 306 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 12:db0dc91f0231 | 307 | int srcPort = (udpSrc[0]<<8) | udpSrc[1]; |
nixnax | 12:db0dc91f0231 | 308 | int dstPort = (udpDst[0]<<8) | udpDst[1]; |
nixnax | 12:db0dc91f0231 | 309 | char * srcIP = udpPkt+12; // udp src addr |
nixnax | 12:db0dc91f0231 | 310 | char * dstIP = udpPkt+16; // udp dst addr |
nixnax | 55:43faae812be3 | 311 | #endif |
nixnax | 29:30de79d658f6 | 312 | #define UDP_HEADER_SIZE 8 |
nixnax | 12:db0dc91f0231 | 313 | int udpLength = ((udpLen[0]<<8) | udpLen[1]) - UDP_HEADER_SIZE; // size of the actual udp data |
nixnax | 42:4de44be70bfd | 314 | if(v1) debug("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],srcPort); |
nixnax | 42:4de44be70bfd | 315 | if(v1) debug("%d.%d.%d.%d:%d ", dstIP[1],dstIP[1],dstIP[1],dstIP[1],dstPort); |
nixnax | 42:4de44be70bfd | 316 | debug("Len %d ", udpLength); |
nixnax | 29:30de79d658f6 | 317 | int printSize = udpLength; |
nixnax | 29:30de79d658f6 | 318 | if (printSize > 20) printSize = 20; // print only first 20 characters |
nixnax | 29:30de79d658f6 | 319 | if (v0) { |
nixnax | 29:30de79d658f6 | 320 | for (int i=0; i<printSize; i++) { |
nixnax | 29:30de79d658f6 | 321 | char ch = udpInf[i]; |
nixnax | 29:30de79d658f6 | 322 | if (ch>31 && ch<127) { |
nixnax | 42:4de44be70bfd | 323 | debug("%c", ch); |
nixnax | 29:30de79d658f6 | 324 | } else { |
nixnax | 64:677b9713a120 | 325 | debug("_"); |
nixnax | 29:30de79d658f6 | 326 | } |
nixnax | 29:30de79d658f6 | 327 | } |
nixnax | 64:677b9713a120 | 328 | debug("\n"); |
nixnax | 29:30de79d658f6 | 329 | } |
nixnax | 12:db0dc91f0231 | 330 | } |
nixnax | 11:f58998c24f0b | 331 | |
nixnax | 29:30de79d658f6 | 332 | int dataCheckSum(char * ptr, int len) |
nixnax | 29:30de79d658f6 | 333 | { |
nixnax | 29:30de79d658f6 | 334 | int sum=0; |
nixnax | 29:30de79d658f6 | 335 | int placeHolder; |
nixnax | 29:30de79d658f6 | 336 | if (len&1) { |
nixnax | 38:ab582987926e | 337 | placeHolder = ptr[len-1]; // when length is odd stuff in a zero byte |
nixnax | 29:30de79d658f6 | 338 | ptr[len-1]=0; |
nixnax | 29:30de79d658f6 | 339 | } |
nixnax | 29:30de79d658f6 | 340 | for (int i=0; i<len/2; i++) { |
nixnax | 29:30de79d658f6 | 341 | int hi = *ptr; |
nixnax | 29:30de79d658f6 | 342 | ptr++; |
nixnax | 29:30de79d658f6 | 343 | int lo = *ptr; |
nixnax | 29:30de79d658f6 | 344 | ptr++; |
nixnax | 11:f58998c24f0b | 345 | int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 ); |
nixnax | 11:f58998c24f0b | 346 | sum = sum + val; |
nixnax | 11:f58998c24f0b | 347 | } |
nixnax | 11:f58998c24f0b | 348 | sum = sum + (sum>>16); |
nixnax | 29:30de79d658f6 | 349 | if (len&1) { |
nixnax | 29:30de79d658f6 | 350 | ptr[len-1] = placeHolder; // restore the last byte for odd lengths |
nixnax | 29:30de79d658f6 | 351 | } |
nixnax | 12:db0dc91f0231 | 352 | return ~sum; |
nixnax | 29:30de79d658f6 | 353 | } |
nixnax | 11:f58998c24f0b | 354 | |
nixnax | 29:30de79d658f6 | 355 | void headerCheckSum() |
nixnax | 29:30de79d658f6 | 356 | { |
nixnax | 11:f58998c24f0b | 357 | int len =(ppp.pkt.buf[4]&0xf)*4; // length of header in bytes |
nixnax | 11:f58998c24f0b | 358 | char * ptr = ppp.pkt.buf+4; // start of ip packet |
nixnax | 11:f58998c24f0b | 359 | int sum=0; |
nixnax | 11:f58998c24f0b | 360 | |
nixnax | 29:30de79d658f6 | 361 | for (int i=0; i<len/2; i++) { |
nixnax | 29:30de79d658f6 | 362 | int hi = *ptr; |
nixnax | 29:30de79d658f6 | 363 | ptr++; |
nixnax | 29:30de79d658f6 | 364 | int lo = *ptr; |
nixnax | 29:30de79d658f6 | 365 | ptr++; |
nixnax | 11:f58998c24f0b | 366 | int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 ); |
nixnax | 11:f58998c24f0b | 367 | sum = sum + val; |
nixnax | 11:f58998c24f0b | 368 | } |
nixnax | 11:f58998c24f0b | 369 | sum = sum + (sum>>16); |
nixnax | 11:f58998c24f0b | 370 | sum = ~sum; |
nixnax | 11:f58998c24f0b | 371 | ppp.pkt.buf[14]= (sum>>8); |
nixnax | 11:f58998c24f0b | 372 | ppp.pkt.buf[15]= (sum ); |
nixnax | 29:30de79d658f6 | 373 | } |
nixnax | 9:0992486d4a30 | 374 | |
nixnax | 29:30de79d658f6 | 375 | void ICMPpacket() // internet control message protocol |
nixnax | 29:30de79d658f6 | 376 | { |
nixnax | 12:db0dc91f0231 | 377 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 12:db0dc91f0231 | 378 | char * pktLen = ipPkt+2; |
nixnax | 12:db0dc91f0231 | 379 | int packetLength = (pktLen[0]<<8) | pktLen[1]; // icmp packet length |
nixnax | 16:cb0b80c24ba2 | 380 | int headerSizeIP = (( ipPkt[0]&0xf)*4); |
nixnax | 16:cb0b80c24ba2 | 381 | char * icmpType = ipPkt + headerSizeIP; // icmp data start |
nixnax | 13:d882b8a042b4 | 382 | char * icmpSum = icmpType+2; // icmp checksum |
nixnax | 29:30de79d658f6 | 383 | #define ICMP_TYPE_PING_REQUEST 8 |
nixnax | 29:30de79d658f6 | 384 | if ( icmpType[0] == ICMP_TYPE_PING_REQUEST ) { |
nixnax | 12:db0dc91f0231 | 385 | char * ipTTL = ipPkt+8; // time to live |
nixnax | 12:db0dc91f0231 | 386 | ipTTL[0]--; // decrement time to live |
nixnax | 12:db0dc91f0231 | 387 | char * srcAdr = ipPkt+12; |
nixnax | 12:db0dc91f0231 | 388 | char * dstAdr = ipPkt+16; |
nixnax | 63:9253b0e1b7d8 | 389 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 18:3e35de1bc877 | 390 | int icmpIdent = (icmpType[4]<<8)|icmpType[5]; |
nixnax | 29:30de79d658f6 | 391 | int icmpSequence = (icmpType[6]<<8)|icmpType[7]; |
nixnax | 63:9253b0e1b7d8 | 392 | #endif |
nixnax | 42:4de44be70bfd | 393 | debug("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 | 42:4de44be70bfd | 394 | debug("Ident %04x Sequence %04d ",icmpIdent,icmpSequence); |
nixnax | 29:30de79d658f6 | 395 | char src[4]; |
nixnax | 29:30de79d658f6 | 396 | char dst[4]; |
nixnax | 12:db0dc91f0231 | 397 | memcpy(src, srcAdr,4); |
nixnax | 12:db0dc91f0231 | 398 | memcpy(dst, dstAdr,4); |
nixnax | 12:db0dc91f0231 | 399 | memcpy(srcAdr, dst,4); |
nixnax | 12:db0dc91f0231 | 400 | memcpy(dstAdr, src,4); // swap src & dest ip |
nixnax | 12:db0dc91f0231 | 401 | char * chkSum = ipPkt+10; |
nixnax | 29:30de79d658f6 | 402 | chkSum[0]=0; |
nixnax | 29:30de79d658f6 | 403 | chkSum[1]=0; |
nixnax | 12:db0dc91f0231 | 404 | headerCheckSum(); // new ip header checksum |
nixnax | 29:30de79d658f6 | 405 | #define ICMP_TYPE_ECHO_REPLY 0 |
nixnax | 16:cb0b80c24ba2 | 406 | icmpType[0]=ICMP_TYPE_ECHO_REPLY; // icmp echo reply |
nixnax | 29:30de79d658f6 | 407 | icmpSum[0]=0; |
nixnax | 29:30de79d658f6 | 408 | icmpSum[1]=0; // zero the checksum for recalculation |
nixnax | 16:cb0b80c24ba2 | 409 | int icmpLength = packetLength - headerSizeIP; // length of ICMP data portion |
nixnax | 16:cb0b80c24ba2 | 410 | int sum = dataCheckSum( icmpType, icmpLength); // this checksum on icmp data portion |
nixnax | 29:30de79d658f6 | 411 | icmpSum[0]=sum>>8; |
nixnax | 29:30de79d658f6 | 412 | icmpSum[1]=sum; // new checksum for ICMP data portion |
nixnax | 16:cb0b80c24ba2 | 413 | |
nixnax | 16:cb0b80c24ba2 | 414 | int printSize = icmpLength-8; // exclude size of icmp header |
nixnax | 25:0b0450e1b08b | 415 | char * icmpData = icmpType+8; // the actual payload data is after the header |
nixnax | 25:0b0450e1b08b | 416 | if (printSize > 10) printSize = 10; // print up to 20 characters |
nixnax | 29:30de79d658f6 | 417 | if (v0) { |
nixnax | 29:30de79d658f6 | 418 | for (int i=0; i<printSize; i++) { |
nixnax | 29:30de79d658f6 | 419 | char ch = icmpData[i]; |
nixnax | 29:30de79d658f6 | 420 | if (ch>31 && ch<127) { |
nixnax | 42:4de44be70bfd | 421 | debug("%c",ch); |
nixnax | 29:30de79d658f6 | 422 | } else { |
nixnax | 64:677b9713a120 | 423 | debug("_"); |
nixnax | 29:30de79d658f6 | 424 | } |
nixnax | 29:30de79d658f6 | 425 | } |
nixnax | 64:677b9713a120 | 426 | debug("\n"); |
nixnax | 29:30de79d658f6 | 427 | } |
nixnax | 15:b0154c910143 | 428 | sendFrame(); // reply to the ping |
nixnax | 29:30de79d658f6 | 429 | |
nixnax | 12:db0dc91f0231 | 430 | } else { |
nixnax | 29:30de79d658f6 | 431 | if (v0) { |
nixnax | 42:4de44be70bfd | 432 | debug("ICMP type=%d \n", icmpType[0]); |
nixnax | 29:30de79d658f6 | 433 | } |
nixnax | 11:f58998c24f0b | 434 | } |
nixnax | 11:f58998c24f0b | 435 | } |
nixnax | 11:f58998c24f0b | 436 | |
nixnax | 29:30de79d658f6 | 437 | void IGMPpacket() // internet group management protocol |
nixnax | 29:30de79d658f6 | 438 | { |
nixnax | 29:30de79d658f6 | 439 | if (v0) { |
nixnax | 42:4de44be70bfd | 440 | debug("IGMP type=%d \n", ppp.pkt.buf[28]); |
nixnax | 29:30de79d658f6 | 441 | } |
nixnax | 29:30de79d658f6 | 442 | } |
nixnax | 11:f58998c24f0b | 443 | |
nixnax | 29:30de79d658f6 | 444 | void dumpHeaderIP () |
nixnax | 29:30de79d658f6 | 445 | { |
nixnax | 26:11f4eb2663a7 | 446 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 63:9253b0e1b7d8 | 447 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 26:11f4eb2663a7 | 448 | char * version = ipPkt; // top 4 bits |
nixnax | 26:11f4eb2663a7 | 449 | char * ihl = ipPkt; // bottom 4 bits |
nixnax | 26:11f4eb2663a7 | 450 | char * dscp = ipPkt+1; // top 6 bits |
nixnax | 26:11f4eb2663a7 | 451 | char * ecn = ipPkt+1; // lower 2 bits |
nixnax | 26:11f4eb2663a7 | 452 | char * pktLen = ipPkt+2; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 453 | char * ident = ipPkt+4; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 454 | char * flags = ipPkt+6; // 2 bits |
nixnax | 26:11f4eb2663a7 | 455 | char * ttl = ipPkt+8; // 1 byte |
nixnax | 26:11f4eb2663a7 | 456 | char * protocol = ipPkt+9; // 1 byte |
nixnax | 29:30de79d658f6 | 457 | char * headercheck= ipPkt+10; // 2 bytes |
nixnax | 63:9253b0e1b7d8 | 458 | #endif |
nixnax | 26:11f4eb2663a7 | 459 | char * srcAdr = ipPkt+12; // 4 bytes |
nixnax | 26:11f4eb2663a7 | 460 | char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes |
nixnax | 29:30de79d658f6 | 461 | |
nixnax | 55:43faae812be3 | 462 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 29:30de79d658f6 | 463 | int versionIP = (version[0]>>4)&0xf; |
nixnax | 26:11f4eb2663a7 | 464 | int headerSizeIP = (ihl[0]&0xf)*4; |
nixnax | 26:11f4eb2663a7 | 465 | int dscpIP = (dscp[0]>>2)&0x3f; |
nixnax | 26:11f4eb2663a7 | 466 | int ecnIP = ecn[0]&3; |
nixnax | 26:11f4eb2663a7 | 467 | int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length |
nixnax | 26:11f4eb2663a7 | 468 | int identIP = (ident[0]<<8)|ident[1]; |
nixnax | 26:11f4eb2663a7 | 469 | int flagsIP = flags[0]>>14&3; |
nixnax | 26:11f4eb2663a7 | 470 | int ttlIP = ttl[0]; |
nixnax | 26:11f4eb2663a7 | 471 | int protocolIP = protocol[0]; |
nixnax | 63:9253b0e1b7d8 | 472 | int checksumIP = (headercheck[0]<<8)|headercheck[1]; |
nixnax | 55:43faae812be3 | 473 | #endif |
nixnax | 55:43faae812be3 | 474 | |
nixnax | 29:30de79d658f6 | 475 | char srcIP [16]; |
nixnax | 29:30de79d658f6 | 476 | snprintf(srcIP,16, "%d.%d.%d.%d", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3]); |
nixnax | 29:30de79d658f6 | 477 | char dstIP [16]; |
nixnax | 29:30de79d658f6 | 478 | snprintf(dstIP,16, "%d.%d.%d.%d", dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]); |
nixnax | 42:4de44be70bfd | 479 | if (v0) debug("IP %s %s v%d h%d d%d e%d L%d ",srcIP,dstIP,versionIP,headerSizeIP,dscpIP,ecnIP,packetLength); |
nixnax | 42:4de44be70bfd | 480 | if (v0) debug("i%04x f%d t%d p%d C%04x\n",identIP,flagsIP,ttlIP,protocolIP,checksumIP); |
nixnax | 29:30de79d658f6 | 481 | } |
nixnax | 26:11f4eb2663a7 | 482 | |
nixnax | 29:30de79d658f6 | 483 | void dumpHeaderTCP() |
nixnax | 29:30de79d658f6 | 484 | { |
nixnax | 29:30de79d658f6 | 485 | int headerSizeIP = (ppp.pkt.buf[4]&0xf)*4; // header size of ip portion |
nixnax | 29:30de79d658f6 | 486 | char * tcpStart = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet |
nixnax | 63:9253b0e1b7d8 | 487 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 29:30de79d658f6 | 488 | char * seqtcp = tcpStart + 4; // 4 bytes |
nixnax | 29:30de79d658f6 | 489 | char * acktcp = tcpStart + 8; // 4 bytes |
nixnax | 63:9253b0e1b7d8 | 490 | #endif |
nixnax | 29:30de79d658f6 | 491 | char * flagbitstcp = tcpStart + 12; // 9 bits |
nixnax | 63:9253b0e1b7d8 | 492 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 31:e000c1b9c565 | 493 | unsigned int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]); |
nixnax | 31:e000c1b9c565 | 494 | unsigned int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]); |
nixnax | 63:9253b0e1b7d8 | 495 | #endif |
nixnax | 26:11f4eb2663a7 | 496 | int flags = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; |
nixnax | 10:74f8233f72c0 | 497 | |
nixnax | 66:f005b9fdf4d1 | 498 | char flagInfo[10]; // text string presentating the TCP flags |
nixnax | 66:f005b9fdf4d1 | 499 | memset(flagInfo,'.', 9); // fill string with "........." |
nixnax | 66:f005b9fdf4d1 | 500 | memset(flagInfo,0,1); // null terminate string |
nixnax | 66:f005b9fdf4d1 | 501 | |
nixnax | 29:30de79d658f6 | 502 | if (flags & (1<<0)) flagInfo[0]='F'; |
nixnax | 29:30de79d658f6 | 503 | if (flags & (1<<1)) flagInfo[1]='S'; |
nixnax | 29:30de79d658f6 | 504 | if (flags & (1<<2)) flagInfo[2]='R'; |
nixnax | 29:30de79d658f6 | 505 | if (flags & (1<<3)) flagInfo[3]='P'; |
nixnax | 29:30de79d658f6 | 506 | if (flags & (1<<4)) flagInfo[4]='A'; |
nixnax | 29:30de79d658f6 | 507 | if (flags & (1<<5)) flagInfo[5]='U'; |
nixnax | 29:30de79d658f6 | 508 | if (flags & (1<<6)) flagInfo[6]='E'; |
nixnax | 29:30de79d658f6 | 509 | if (flags & (1<<7)) flagInfo[7]='C'; |
nixnax | 29:30de79d658f6 | 510 | if (flags & (1<<8)) flagInfo[8]='N'; |
nixnax | 29:30de79d658f6 | 511 | if (v0) { |
nixnax | 66:f005b9fdf4d1 | 512 | debug("Flags %s Seq %u Ack %u", flagInfo, seq, ack); // show the flags in debug |
nixnax | 29:30de79d658f6 | 513 | } |
nixnax | 29:30de79d658f6 | 514 | } |
nixnax | 29:30de79d658f6 | 515 | |
nixnax | 68:0b74763ae67f | 516 | int httpResponse(char * dataStart) |
nixnax | 67:a63e3486bcda | 517 | { |
nixnax | 67:a63e3486bcda | 518 | int n=0; // number of bytes we have printed so far |
nixnax | 67:a63e3486bcda | 519 | if(strncmp(dataStart, "GET / HTTP/1.1", 14) == 0 ) { |
nixnax | 67:a63e3486bcda | 520 | n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: PPP-Blinky\r\n"); // http header |
nixnax | 67:a63e3486bcda | 521 | n=n+sprintf(n+dataStart,"Content-Length: "); // http header |
nixnax | 67:a63e3486bcda | 522 | int contentLengthStart = n; // remember where Content-Length is in buffer |
nixnax | 67:a63e3486bcda | 523 | n=n+sprintf(n+dataStart,"?????\r\n"); // leave five spaces for content length - will be updated later |
nixnax | 67:a63e3486bcda | 524 | 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 | 67:a63e3486bcda | 525 | int nHeader=n; // byte size of the HTTP header. Note - seems like this must be 1+(multiple of four) |
nixnax | 67:a63e3486bcda | 526 | |
nixnax | 67:a63e3486bcda | 527 | // this is where we insert our web page into the buffer |
nixnax | 67:a63e3486bcda | 528 | n=n+sprintf(n+dataStart,"%s\r\n", ourWebPage); |
nixnax | 67:a63e3486bcda | 529 | |
nixnax | 68:0b74763ae67f | 530 | #define CONTENTLENGTHSIZE 5 |
nixnax | 67:a63e3486bcda | 531 | char contentLengthString[CONTENTLENGTHSIZE+1]; // temporary buffer to create Content-Length string |
nixnax | 68:0b74763ae67f | 532 | snprintf(contentLengthString,CONTENTLENGTHSIZE+1,"%*d",CONTENTLENGTHSIZE,n-nHeader); // print Content-Length with leading spaces and fixed width equal to csize |
nixnax | 67:a63e3486bcda | 533 | memcpy(dataStart+contentLengthStart, contentLengthString, CONTENTLENGTHSIZE); // copy Content-Length to it's place in the send buffer |
nixnax | 67:a63e3486bcda | 534 | |
nixnax | 67:a63e3486bcda | 535 | if (v0) { |
nixnax | 67:a63e3486bcda | 536 | debug("HTTP GET BufferSize %d*32=%d Header %d Content-Length %d Total %d Available %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n); |
nixnax | 67:a63e3486bcda | 537 | } |
nixnax | 67:a63e3486bcda | 538 | } else { // all remaining requests get 404 Not Found response |
nixnax | 67:a63e3486bcda | 539 | n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\nServer: PPP-Blinky\r\n"); // http header |
nixnax | 67:a63e3486bcda | 540 | n=n+sprintf(n+dataStart,"Content-Length: "); // http header |
nixnax | 67:a63e3486bcda | 541 | int contentLengthStart = n; // remember where Content-Length is in buffer |
nixnax | 67:a63e3486bcda | 542 | n=n+sprintf(n+dataStart,"?????\r\n"); // leave five spaces for content length - will be updated later |
nixnax | 67:a63e3486bcda | 543 | 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 | 67:a63e3486bcda | 544 | int nHeader=n; // byte total of all headers. Note - seems like this must be 1+(multiple of four) |
nixnax | 67:a63e3486bcda | 545 | |
nixnax | 67:a63e3486bcda | 546 | n=n+sprintf(n+dataStart,"<!DOCTYPE html><html><head></head>"); // html start |
nixnax | 67:a63e3486bcda | 547 | n=n+sprintf(n+dataStart,"<body><h1>File Not Found</h1></body>"); |
nixnax | 67:a63e3486bcda | 548 | n=n+sprintf(n+dataStart,"</html>\r\n"); // html end |
nixnax | 67:a63e3486bcda | 549 | |
nixnax | 68:0b74763ae67f | 550 | char contentLengthString[CONTENTLENGTHSIZE+1]; // temporary buffer to create Content-Length string |
nixnax | 68:0b74763ae67f | 551 | snprintf(contentLengthString,CONTENTLENGTHSIZE+1,"%*d",CONTENTLENGTHSIZE,n-nHeader); // print Content-Length with leading spaces and fixed width equal to csize |
nixnax | 68:0b74763ae67f | 552 | memcpy(dataStart+contentLengthStart, contentLengthString, CONTENTLENGTHSIZE); // copy Content-Length to it's place in the send buffer |
nixnax | 67:a63e3486bcda | 553 | |
nixnax | 67:a63e3486bcda | 554 | if (v0) { |
nixnax | 67:a63e3486bcda | 555 | debug("HTTP GET BufSize %d*32=%d Header %d Content-Length %d Total %d Available %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n); |
nixnax | 67:a63e3486bcda | 556 | } |
nixnax | 67:a63e3486bcda | 557 | } |
nixnax | 67:a63e3486bcda | 558 | return n; // total byte size of our response |
nixnax | 67:a63e3486bcda | 559 | } |
nixnax | 67:a63e3486bcda | 560 | |
nixnax | 67:a63e3486bcda | 561 | |
nixnax | 29:30de79d658f6 | 562 | void tcpHandler() |
nixnax | 29:30de79d658f6 | 563 | { |
nixnax | 26:11f4eb2663a7 | 564 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 29:30de79d658f6 | 565 | char * headercheck= ipPkt+10; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 566 | char * ihl = ipPkt; // bottom 4 bits |
nixnax | 26:11f4eb2663a7 | 567 | char * ident = ipPkt+4; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 568 | char * pktLen = ipPkt+2; // 2 bytes |
nixnax | 28:1aa629be05e7 | 569 | char * protocol = ipPkt+9; // 1 byte |
nixnax | 26:11f4eb2663a7 | 570 | char * srcAdr = ipPkt+12; // 4 bytes |
nixnax | 26:11f4eb2663a7 | 571 | char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes |
nixnax | 26:11f4eb2663a7 | 572 | int headerSizeIP = (ihl[0]&0xf)*4; |
nixnax | 26:11f4eb2663a7 | 573 | int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length |
nixnax | 26:11f4eb2663a7 | 574 | |
nixnax | 29:30de79d658f6 | 575 | ident[0] = ppp.ident>>8; |
nixnax | 29:30de79d658f6 | 576 | ident[1] = ppp.ident>>0; // insert OUR ident |
nixnax | 29:30de79d658f6 | 577 | |
nixnax | 29:30de79d658f6 | 578 | char * s = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet |
nixnax | 26:11f4eb2663a7 | 579 | char * srctcp = s + 0; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 580 | char * dsttcp = s + 2; // 2 bytes |
nixnax | 28:1aa629be05e7 | 581 | char * seqtcp = s + 4; // 4 bytes |
nixnax | 28:1aa629be05e7 | 582 | char * acktcp = s + 8; // 4 bytes |
nixnax | 28:1aa629be05e7 | 583 | char * offset = s + 12; // 4 bits |
nixnax | 26:11f4eb2663a7 | 584 | char * flagbitstcp = s + 12; // 9 bits |
nixnax | 26:11f4eb2663a7 | 585 | char * checksumtcp = s + 16; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 586 | |
nixnax | 26:11f4eb2663a7 | 587 | int tcpSize = packetLength - headerSizeIP; |
nixnax | 35:e7068df4d971 | 588 | int headerSizeTCP = ((offset[0]>>4)&0x0f)*4; // size of tcp header only |
nixnax | 61:b3c1a04efd0a | 589 | int protocolIP = protocol[0]; |
nixnax | 29:30de79d658f6 | 590 | |
nixnax | 31:e000c1b9c565 | 591 | unsigned int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]); |
nixnax | 31:e000c1b9c565 | 592 | unsigned int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]); |
nixnax | 29:30de79d658f6 | 593 | |
nixnax | 35:e7068df4d971 | 594 | int flagsTCP = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; |
nixnax | 29:30de79d658f6 | 595 | |
nixnax | 35:e7068df4d971 | 596 | char * dataStart = ppp.pkt.buf + 4 + headerSizeIP + headerSizeTCP; // start of data block after TCP header |
nixnax | 67:a63e3486bcda | 597 | int tcpDataSize = tcpSize - headerSizeTCP; // size of data block after TCP header |
nixnax | 28:1aa629be05e7 | 598 | |
nixnax | 29:30de79d658f6 | 599 | #define TCP_FLAG_ACK (1<<4) |
nixnax | 29:30de79d658f6 | 600 | #define TCP_FLAG_SYN (1<<1) |
nixnax | 29:30de79d658f6 | 601 | #define TCP_FLAG_PSH (1<<3) |
nixnax | 29:30de79d658f6 | 602 | #define TCP_FLAG_RST (1<<2) |
nixnax | 29:30de79d658f6 | 603 | #define TCP_FLAG_FIN (1<<0) |
nixnax | 28:1aa629be05e7 | 604 | |
nixnax | 38:ab582987926e | 605 | // A sparse TCP flag interpreter that implements simple TCP connections from a single source |
nixnax | 38:ab582987926e | 606 | // Clients are allowed ONE push packet, after which the link is closed with a FIN flag in the ACK packet |
nixnax | 38:ab582987926e | 607 | // This strategy allows web browsers, netcat and curl to work ok while keeping the state machine simple |
nixnax | 29:30de79d658f6 | 608 | |
nixnax | 35:e7068df4d971 | 609 | int dataLen = 0; // most of our responses will have zero TCP data, only a header |
nixnax | 38:ab582987926e | 610 | int flagsOut = TCP_FLAG_ACK; // the default case is an ACK packet |
nixnax | 38:ab582987926e | 611 | int fastResponse = 0; // normally you wait 200ms before sending a packet but this can make it faster |
nixnax | 38:ab582987926e | 612 | |
nixnax | 61:b3c1a04efd0a | 613 | ppp.seq = ack; // always adopt their sequence number calculation in place of doing our own calculation |
nixnax | 35:e7068df4d971 | 614 | |
nixnax | 66:f005b9fdf4d1 | 615 | if ( flagsTCP == TCP_FLAG_ACK ) { |
nixnax | 67:a63e3486bcda | 616 | if (tcpDataSize == 0) { // ignore - just an empty ack packet |
nixnax | 38:ab582987926e | 617 | return; |
nixnax | 38:ab582987926e | 618 | } |
nixnax | 38:ab582987926e | 619 | } else if ( (flagsTCP & TCP_FLAG_SYN) != 0 ) { // got SYN flag |
nixnax | 36:2a9b457f8276 | 620 | flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // do a syn-ack |
nixnax | 38:ab582987926e | 621 | seq++; // for SYN flag we have to increase sequence by 1 |
nixnax | 38:ab582987926e | 622 | } else if ( (flagsTCP & TCP_FLAG_FIN) != 0 ) { // got FIN flag |
nixnax | 38:ab582987926e | 623 | seq++; // for FIN flag we have to increase sequence by 1 |
nixnax | 38:ab582987926e | 624 | } else if ( (flagsTCP & TCP_FLAG_PSH) != 0 ) { // got PSH flag (push) |
nixnax | 38:ab582987926e | 625 | flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // for every push we answer once AND close the link |
nixnax | 38:ab582987926e | 626 | fastResponse = 1; // we can respond fast to a push |
nixnax | 38:ab582987926e | 627 | // It's a push, so let's check the incoming data for an HTTP GET request |
nixnax | 67:a63e3486bcda | 628 | if ( strncmp(dataStart, "GET ", 4) == 0) { // do we see an http GET command |
nixnax | 67:a63e3486bcda | 629 | dataLen = httpResponse(dataStart); // send an http response |
nixnax | 29:30de79d658f6 | 630 | } |
nixnax | 28:1aa629be05e7 | 631 | } |
nixnax | 29:30de79d658f6 | 632 | |
nixnax | 38:ab582987926e | 633 | // All the TCP flag handling is now done |
nixnax | 38:ab582987926e | 634 | // Now we have to recalculate all the header sizes, swap IP address/port source and destination, and do the IP and TCP checksums |
nixnax | 26:11f4eb2663a7 | 635 | |
nixnax | 65:23b17c43aa0f | 636 | char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header |
nixnax | 60:2b770949c911 | 637 | memcpy(tempHold, srcAdr,4); |
nixnax | 65:23b17c43aa0f | 638 | memcpy(srcAdr, dstAdr,4); |
nixnax | 60:2b770949c911 | 639 | memcpy(dstAdr, tempHold,4); // swap ip address source/dest |
nixnax | 60:2b770949c911 | 640 | |
nixnax | 60:2b770949c911 | 641 | memcpy(tempHold, srctcp,2); |
nixnax | 65:23b17c43aa0f | 642 | memcpy(srctcp, dsttcp,2); |
nixnax | 60:2b770949c911 | 643 | memcpy(dsttcp, tempHold,2); // swap ip port source/dest |
nixnax | 29:30de79d658f6 | 644 | |
nixnax | 67:a63e3486bcda | 645 | ack = seq + tcpDataSize; // acknowledge the number of data bytes that they sent by adding it to "our" sequence number |
nixnax | 38:ab582987926e | 646 | seq = ppp.seq; // set up the sequence number we have to respond with |
nixnax | 38:ab582987926e | 647 | |
nixnax | 38:ab582987926e | 648 | acktcp[0]=ack>>24; |
nixnax | 38:ab582987926e | 649 | acktcp[1]=ack>>16; |
nixnax | 38:ab582987926e | 650 | acktcp[2]=ack>>8; |
nixnax | 67:a63e3486bcda | 651 | acktcp[3]=ack>>0; // save ack 32-bit integer |
nixnax | 38:ab582987926e | 652 | |
nixnax | 38:ab582987926e | 653 | seqtcp[0]=seq>>24; |
nixnax | 38:ab582987926e | 654 | seqtcp[1]=seq>>16; |
nixnax | 38:ab582987926e | 655 | seqtcp[2]=seq>>8; |
nixnax | 67:a63e3486bcda | 656 | seqtcp[3]=seq>>0; // save seq 32-bit integer |
nixnax | 38:ab582987926e | 657 | |
nixnax | 38:ab582987926e | 658 | flagbitstcp[1] = flagsOut; // set up the new flags |
nixnax | 38:ab582987926e | 659 | |
nixnax | 38:ab582987926e | 660 | int newPacketSize = headerSizeIP + headerSizeTCP + dataLen; // calculate size of the outgoing packet |
nixnax | 36:2a9b457f8276 | 661 | pktLen[0] = (newPacketSize>>8); |
nixnax | 36:2a9b457f8276 | 662 | pktLen[1]=newPacketSize; // ip total packet size |
nixnax | 36:2a9b457f8276 | 663 | ppp.pkt.len = newPacketSize+6; // ppp packet length |
nixnax | 36:2a9b457f8276 | 664 | tcpSize = headerSizeTCP + dataLen; // tcp packet size |
nixnax | 36:2a9b457f8276 | 665 | |
nixnax | 38:ab582987926e | 666 | // the header is all set up, now do the IP and TCP checksums |
nixnax | 36:2a9b457f8276 | 667 | |
nixnax | 61:b3c1a04efd0a | 668 | headercheck[0]=0; // IP header checksum |
nixnax | 61:b3c1a04efd0a | 669 | headercheck[1]=0; // IP header checksum |
nixnax | 61:b3c1a04efd0a | 670 | headerCheckSum(); // calculate the IP header checksum |
nixnax | 63:9253b0e1b7d8 | 671 | |
nixnax | 64:677b9713a120 | 672 | // now we have to build the so-called 12-byte TCP "pseudo-header" in front of the TCP header (containing some IP header values) in order to correctly calculate the TCP checksum |
nixnax | 63:9253b0e1b7d8 | 673 | // this header contains the most important parts of the IP header, i.e. source and destination address, protocol number and data length. |
nixnax | 63:9253b0e1b7d8 | 674 | |
nixnax | 62:f192926e42f1 | 675 | char * pseudoHeader = s-12; // mark the start of the TCP pseudo-header |
nixnax | 67:a63e3486bcda | 676 | memcpy(tempHold, pseudoHeader, 12); // preserve the 12 bytes of the IP header where the TCP pseudo-Header will be built |
nixnax | 61:b3c1a04efd0a | 677 | memcpy( pseudoHeader+0, srcAdr, 8); // IP source and destination addresses from IP header |
nixnax | 61:b3c1a04efd0a | 678 | memset( pseudoHeader+8, 0, 1); // reserved, set to zero |
nixnax | 61:b3c1a04efd0a | 679 | memset( pseudoHeader+9, protocolIP, 1); // protocol from IP header |
nixnax | 62:f192926e42f1 | 680 | memset( pseudoHeader+10, tcpSize>>8, 1); // size of IP data (TCP packet size) |
nixnax | 62:f192926e42f1 | 681 | memset( pseudoHeader+11, tcpSize, 1); // size of IP data (TCP packet size) |
nixnax | 38:ab582987926e | 682 | |
nixnax | 61:b3c1a04efd0a | 683 | // pseudo-header built, now we can calculate TCP checksum |
nixnax | 63:9253b0e1b7d8 | 684 | |
nixnax | 29:30de79d658f6 | 685 | checksumtcp[0]=0; |
nixnax | 29:30de79d658f6 | 686 | checksumtcp[1]=0; |
nixnax | 62:f192926e42f1 | 687 | int pseudoHeaderSum=dataCheckSum(pseudoHeader,tcpSize+12); // calculate the TCP checksum starting at the pseudo-header |
nixnax | 61:b3c1a04efd0a | 688 | checksumtcp[0]=pseudoHeaderSum>>8; |
nixnax | 61:b3c1a04efd0a | 689 | checksumtcp[1]=pseudoHeaderSum; |
nixnax | 61:b3c1a04efd0a | 690 | memcpy( s-12, tempHold, 12); // restore the 12 bytes that the pseudo-header overwrote |
nixnax | 60:2b770949c911 | 691 | |
nixnax | 36:2a9b457f8276 | 692 | if (fastResponse==1) { |
nixnax | 36:2a9b457f8276 | 693 | fastResponse=0; // reset and skip 200 ms wait |
nixnax | 36:2a9b457f8276 | 694 | } else { |
nixnax | 68:0b74763ae67f | 695 | // normally, you wait 200 ms before sending a TCP packet |
nixnax | 68:0b74763ae67f | 696 | // remove the wait to respond faster |
nixnax | 68:0b74763ae67f | 697 | // wait(0.2); |
nixnax | 36:2a9b457f8276 | 698 | } |
nixnax | 38:ab582987926e | 699 | sendFrame(); // All done! Send the TCP packet |
nixnax | 38:ab582987926e | 700 | ppp.seq = ppp.seq + dataLen; // increase OUR sequence by the outgoing data length - for the next round |
nixnax | 29:30de79d658f6 | 701 | } |
nixnax | 26:11f4eb2663a7 | 702 | |
nixnax | 29:30de79d658f6 | 703 | void dumpDataTCP() |
nixnax | 29:30de79d658f6 | 704 | { |
nixnax | 26:11f4eb2663a7 | 705 | int ipPktLen = (ppp.pkt.buf[6]<<8)|ppp.pkt.buf[7]; // overall length of ip packet |
nixnax | 26:11f4eb2663a7 | 706 | int ipHeaderLen = (ppp.pkt.buf[4]&0xf)*4; // length of ip header |
nixnax | 35:e7068df4d971 | 707 | int headerSizeTCP = ((ppp.pkt.buf[4+ipHeaderLen+12]>>4)&0xf)*4;; // length of tcp header |
nixnax | 35:e7068df4d971 | 708 | int dataLen = ipPktLen - ipHeaderLen - headerSizeTCP; // data is what's left after the two headers |
nixnax | 29:30de79d658f6 | 709 | if (v1) { |
nixnax | 42:4de44be70bfd | 710 | debug("TCP %d ipHeader %d tcpHeader %d Data %d\n", ipPktLen, ipHeaderLen, headerSizeTCP, dataLen); // 1 for more verbose |
nixnax | 29:30de79d658f6 | 711 | } |
nixnax | 29:30de79d658f6 | 712 | if (dataLen > 0) { |
nixnax | 47:00a5ca075f8f | 713 | ppp.pkt.buf[4+ipHeaderLen+headerSizeTCP+dataLen]=0; // insert a null after the data so debug printf stops printing after the data |
nixnax | 42:4de44be70bfd | 714 | debug("%s\n",ppp.pkt.buf+4+ipHeaderLen+headerSizeTCP); // show the data |
nixnax | 29:30de79d658f6 | 715 | } |
nixnax | 29:30de79d658f6 | 716 | } |
nixnax | 26:11f4eb2663a7 | 717 | |
nixnax | 29:30de79d658f6 | 718 | void TCPpacket() |
nixnax | 29:30de79d658f6 | 719 | { |
nixnax | 26:11f4eb2663a7 | 720 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 63:9253b0e1b7d8 | 721 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 26:11f4eb2663a7 | 722 | char * version = ipPkt; // top 4 bits |
nixnax | 26:11f4eb2663a7 | 723 | char * ihl = ipPkt; // bottom 4 bits |
nixnax | 26:11f4eb2663a7 | 724 | char * dscp = ipPkt+1; // top 6 bits |
nixnax | 26:11f4eb2663a7 | 725 | char * ecn = ipPkt+1; // lower 2 bits |
nixnax | 26:11f4eb2663a7 | 726 | char * pktLen = ipPkt+2; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 727 | char * ident = ipPkt+4; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 728 | char * flags = ipPkt+6; // 2 bits |
nixnax | 26:11f4eb2663a7 | 729 | char * ttl = ipPkt+8; // 1 byte |
nixnax | 26:11f4eb2663a7 | 730 | char * protocol = ipPkt+9; // 1 byte |
nixnax | 29:30de79d658f6 | 731 | char * headercheck= ipPkt+10; // 2 bytes |
nixnax | 63:9253b0e1b7d8 | 732 | #endif |
nixnax | 26:11f4eb2663a7 | 733 | char * srcAdr = ipPkt+12; // 4 bytes |
nixnax | 26:11f4eb2663a7 | 734 | char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes |
nixnax | 29:30de79d658f6 | 735 | |
nixnax | 55:43faae812be3 | 736 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 29:30de79d658f6 | 737 | int versionIP = (version[0]>>4)&0xf; |
nixnax | 26:11f4eb2663a7 | 738 | int headerSizeIP = (ihl[0]&0xf)*4; |
nixnax | 26:11f4eb2663a7 | 739 | int dscpIP = (dscp[0]>>2)&0x3f; |
nixnax | 26:11f4eb2663a7 | 740 | int ecnIP = ecn[0]&3; |
nixnax | 26:11f4eb2663a7 | 741 | int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length |
nixnax | 26:11f4eb2663a7 | 742 | int identIP = (ident[0]<<8)|ident[1]; |
nixnax | 26:11f4eb2663a7 | 743 | int flagsIP = flags[0]>>14&3; |
nixnax | 26:11f4eb2663a7 | 744 | int ttlIP = ttl[0]; |
nixnax | 26:11f4eb2663a7 | 745 | int protocolIP = protocol[0]; |
nixnax | 26:11f4eb2663a7 | 746 | int checksumIP = (headercheck[0]<<8)|headercheck[1]; |
nixnax | 63:9253b0e1b7d8 | 747 | #endif |
nixnax | 29:30de79d658f6 | 748 | char srcIP [16]; |
nixnax | 29:30de79d658f6 | 749 | snprintf(srcIP,16, "%d.%d.%d.%d", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3]); |
nixnax | 29:30de79d658f6 | 750 | char dstIP [16]; |
nixnax | 29:30de79d658f6 | 751 | snprintf(dstIP,16, "%d.%d.%d.%d", dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]); |
nixnax | 29:30de79d658f6 | 752 | if (v0) { |
nixnax | 42:4de44be70bfd | 753 | debug("IP %s %s v%d h%d d%d e%d L%d ",srcIP,dstIP,versionIP,headerSizeIP,dscpIP,ecnIP,packetLength); |
nixnax | 29:30de79d658f6 | 754 | } |
nixnax | 29:30de79d658f6 | 755 | if (v0) { |
nixnax | 42:4de44be70bfd | 756 | debug("i%04x f%d t%d p%d C%04x\n",identIP,flagsIP,ttlIP,protocolIP,checksumIP); |
nixnax | 29:30de79d658f6 | 757 | } |
nixnax | 28:1aa629be05e7 | 758 | dumpHeaderTCP(); |
nixnax | 26:11f4eb2663a7 | 759 | dumpDataTCP(); |
nixnax | 26:11f4eb2663a7 | 760 | tcpHandler(); |
nixnax | 11:f58998c24f0b | 761 | } |
nixnax | 11:f58998c24f0b | 762 | |
nixnax | 29:30de79d658f6 | 763 | void otherProtocol() |
nixnax | 29:30de79d658f6 | 764 | { |
nixnax | 64:677b9713a120 | 765 | debug("Other IP protocol"); |
nixnax | 29:30de79d658f6 | 766 | } |
nixnax | 26:11f4eb2663a7 | 767 | |
nixnax | 29:30de79d658f6 | 768 | void IPframe() |
nixnax | 29:30de79d658f6 | 769 | { |
nixnax | 10:74f8233f72c0 | 770 | int protocol = ppp.pkt.buf[13]; |
nixnax | 10:74f8233f72c0 | 771 | switch (protocol) { |
nixnax | 29:30de79d658f6 | 772 | case 1: |
nixnax | 29:30de79d658f6 | 773 | ICMPpacket(); |
nixnax | 29:30de79d658f6 | 774 | break; |
nixnax | 29:30de79d658f6 | 775 | case 2: |
nixnax | 29:30de79d658f6 | 776 | IGMPpacket(); |
nixnax | 29:30de79d658f6 | 777 | break; |
nixnax | 29:30de79d658f6 | 778 | case 17: |
nixnax | 29:30de79d658f6 | 779 | UDPpacket(); |
nixnax | 29:30de79d658f6 | 780 | break; |
nixnax | 29:30de79d658f6 | 781 | case 6: |
nixnax | 29:30de79d658f6 | 782 | TCPpacket(); |
nixnax | 29:30de79d658f6 | 783 | break; |
nixnax | 29:30de79d658f6 | 784 | default: |
nixnax | 29:30de79d658f6 | 785 | otherProtocol(); |
nixnax | 29:30de79d658f6 | 786 | } |
nixnax | 29:30de79d658f6 | 787 | } |
nixnax | 9:0992486d4a30 | 788 | |
nixnax | 29:30de79d658f6 | 789 | void LCPconfReq() |
nixnax | 29:30de79d658f6 | 790 | { |
nixnax | 64:677b9713a120 | 791 | debug("LCP Config "); |
nixnax | 9:0992486d4a30 | 792 | if (ppp.pkt.buf[7] != 4) { |
nixnax | 11:f58998c24f0b | 793 | ppp.pkt.buf[4]=4; // allow only no options |
nixnax | 64:677b9713a120 | 794 | debug("Reject\n"); |
nixnax | 29:30de79d658f6 | 795 | sendFrame(); |
nixnax | 9:0992486d4a30 | 796 | } else { |
nixnax | 9:0992486d4a30 | 797 | ppp.pkt.buf[4]=2; // ack zero conf |
nixnax | 64:677b9713a120 | 798 | debug("Ack\n"); |
nixnax | 9:0992486d4a30 | 799 | sendFrame(); |
nixnax | 64:677b9713a120 | 800 | debug("LCP Ask\n"); |
nixnax | 11:f58998c24f0b | 801 | ppp.pkt.buf[4]=1; // request no options |
nixnax | 9:0992486d4a30 | 802 | sendFrame(); |
nixnax | 9:0992486d4a30 | 803 | } |
nixnax | 9:0992486d4a30 | 804 | } |
nixnax | 9:0992486d4a30 | 805 | |
nixnax | 29:30de79d658f6 | 806 | void LCPconfAck() |
nixnax | 29:30de79d658f6 | 807 | { |
nixnax | 64:677b9713a120 | 808 | debug("LCP Ack\n"); |
nixnax | 29:30de79d658f6 | 809 | } |
nixnax | 9:0992486d4a30 | 810 | |
nixnax | 29:30de79d658f6 | 811 | void LCPend() |
nixnax | 29:30de79d658f6 | 812 | { |
nixnax | 64:677b9713a120 | 813 | debug("LCP End\n"); |
nixnax | 29:30de79d658f6 | 814 | ppp.online=0; // start hunting for connect string again |
nixnax | 29:30de79d658f6 | 815 | ppp.pkt.buf[4]=6; |
nixnax | 29:30de79d658f6 | 816 | sendFrame(); // acknowledge |
nixnax | 9:0992486d4a30 | 817 | } |
nixnax | 9:0992486d4a30 | 818 | |
nixnax | 29:30de79d658f6 | 819 | void LCPother() |
nixnax | 29:30de79d658f6 | 820 | { |
nixnax | 64:677b9713a120 | 821 | debug("LCP Other\n"); |
nixnax | 29:30de79d658f6 | 822 | dumpFrame(); |
nixnax | 29:30de79d658f6 | 823 | } |
nixnax | 29:30de79d658f6 | 824 | |
nixnax | 29:30de79d658f6 | 825 | void LCPframe() |
nixnax | 29:30de79d658f6 | 826 | { |
nixnax | 29:30de79d658f6 | 827 | int code = ppp.pkt.buf[4]; |
nixnax | 29:30de79d658f6 | 828 | switch (code) { |
nixnax | 29:30de79d658f6 | 829 | case 1: |
nixnax | 29:30de79d658f6 | 830 | LCPconfReq(); |
nixnax | 29:30de79d658f6 | 831 | break; // config request |
nixnax | 29:30de79d658f6 | 832 | case 2: |
nixnax | 29:30de79d658f6 | 833 | LCPconfAck(); |
nixnax | 29:30de79d658f6 | 834 | break; // config ack |
nixnax | 29:30de79d658f6 | 835 | case 5: |
nixnax | 29:30de79d658f6 | 836 | LCPend(); |
nixnax | 29:30de79d658f6 | 837 | break; // end connection |
nixnax | 29:30de79d658f6 | 838 | default: |
nixnax | 29:30de79d658f6 | 839 | LCPother(); |
nixnax | 29:30de79d658f6 | 840 | } |
nixnax | 9:0992486d4a30 | 841 | } |
nixnax | 9:0992486d4a30 | 842 | |
nixnax | 29:30de79d658f6 | 843 | void discardedFrame() |
nixnax | 29:30de79d658f6 | 844 | { |
nixnax | 29:30de79d658f6 | 845 | if (v0) { |
nixnax | 42:4de44be70bfd | 846 | debug("Dropping frame %02x %02x %02x %02x\n", ppp.pkt.buf[0],ppp.pkt.buf[1],ppp.pkt.buf[2],ppp.pkt.buf[3]); |
nixnax | 29:30de79d658f6 | 847 | } |
nixnax | 9:0992486d4a30 | 848 | } |
nixnax | 9:0992486d4a30 | 849 | |
nixnax | 29:30de79d658f6 | 850 | void determinePacketType() |
nixnax | 29:30de79d658f6 | 851 | { |
nixnax | 29:30de79d658f6 | 852 | if ( ppp.pkt.buf[0] != 0xff ) { |
nixnax | 64:677b9713a120 | 853 | debug("byte0 != ff\n"); |
nixnax | 29:30de79d658f6 | 854 | return; |
nixnax | 29:30de79d658f6 | 855 | } |
nixnax | 29:30de79d658f6 | 856 | if ( ppp.pkt.buf[1] != 3 ) { |
nixnax | 64:677b9713a120 | 857 | debug("byte1 != 3\n"); |
nixnax | 29:30de79d658f6 | 858 | return; |
nixnax | 29:30de79d658f6 | 859 | } |
nixnax | 29:30de79d658f6 | 860 | if ( ppp.pkt.buf[3] != 0x21 ) { |
nixnax | 64:677b9713a120 | 861 | debug("byte2 != 21\n"); |
nixnax | 29:30de79d658f6 | 862 | return; |
nixnax | 29:30de79d658f6 | 863 | } |
nixnax | 9:0992486d4a30 | 864 | int packetType = ppp.pkt.buf[2]; |
nixnax | 9:0992486d4a30 | 865 | switch (packetType) { |
nixnax | 29:30de79d658f6 | 866 | case 0xc0: |
nixnax | 29:30de79d658f6 | 867 | LCPframe(); |
nixnax | 29:30de79d658f6 | 868 | break; // link control |
nixnax | 29:30de79d658f6 | 869 | case 0x80: |
nixnax | 29:30de79d658f6 | 870 | IPCPframe(); |
nixnax | 29:30de79d658f6 | 871 | break; // IP control |
nixnax | 29:30de79d658f6 | 872 | case 0x00: |
nixnax | 29:30de79d658f6 | 873 | IPframe(); |
nixnax | 29:30de79d658f6 | 874 | break; // IP itself |
nixnax | 29:30de79d658f6 | 875 | default: |
nixnax | 29:30de79d658f6 | 876 | discardedFrame(); |
nixnax | 9:0992486d4a30 | 877 | } |
nixnax | 29:30de79d658f6 | 878 | } |
nixnax | 9:0992486d4a30 | 879 | |
nixnax | 50:ad4e7c3c88e5 | 880 | void wait_for_HDLC_frame() |
nixnax | 50:ad4e7c3c88e5 | 881 | { |
nixnax | 50:ad4e7c3c88e5 | 882 | while ( rxbufNotEmpty() ) { |
nixnax | 50:ad4e7c3c88e5 | 883 | int rx = pc_getBuf(); |
nixnax | 50:ad4e7c3c88e5 | 884 | if (ppp.hdlc.frameBusy) { |
nixnax | 50:ad4e7c3c88e5 | 885 | if (rx==FRAME_7E) { |
nixnax | 50:ad4e7c3c88e5 | 886 | ppp.hdlc.frameBusy=0; // done gathering frame |
nixnax | 50:ad4e7c3c88e5 | 887 | if (ppp.rx.tail == 0) { // did we just wrap around? |
nixnax | 50:ad4e7c3c88e5 | 888 | ppp.hdlc.frameEndIndex=BUFLEN-1; // wrap back to end of buffer |
nixnax | 51:a86d56844324 | 889 | } else { |
nixnax | 50:ad4e7c3c88e5 | 890 | ppp.hdlc.frameEndIndex=ppp.rx.tail-1; // remember where frame ends |
nixnax | 50:ad4e7c3c88e5 | 891 | } |
nixnax | 50:ad4e7c3c88e5 | 892 | processHDLCFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); |
nixnax | 50:ad4e7c3c88e5 | 893 | } |
nixnax | 50:ad4e7c3c88e5 | 894 | } else { |
nixnax | 50:ad4e7c3c88e5 | 895 | if (rx==FRAME_7E) { |
nixnax | 50:ad4e7c3c88e5 | 896 | ppp.hdlc.frameBusy=1; // start gathering frame |
nixnax | 50:ad4e7c3c88e5 | 897 | ppp.hdlc.frameStartIndex=ppp.rx.tail; // remember where frame started |
nixnax | 50:ad4e7c3c88e5 | 898 | } |
nixnax | 50:ad4e7c3c88e5 | 899 | } |
nixnax | 50:ad4e7c3c88e5 | 900 | } |
nixnax | 50:ad4e7c3c88e5 | 901 | } |
nixnax | 50:ad4e7c3c88e5 | 902 | |
nixnax | 29:30de79d658f6 | 903 | void scanForConnectString() |
nixnax | 29:30de79d658f6 | 904 | { |
nixnax | 9:0992486d4a30 | 905 | if ( ppp.online==0 ) { |
nixnax | 49:2213f9c132b2 | 906 | char * clientFound = strstr( (char *)rxbufppp, "CLIENTCLIENT" ); // look for PC string |
nixnax | 29:30de79d658f6 | 907 | if( clientFound ) { |
nixnax | 57:8deab70fef2e | 908 | strcpy( clientFound, "FOUND!FOUND!" ); // overwrite so we don't find it again |
nixnax | 9:0992486d4a30 | 909 | pc.printf("CLIENTSERVER"); // respond to PC |
nixnax | 9:0992486d4a30 | 910 | ppp.online=1; // we can stop looking for the string |
nixnax | 64:677b9713a120 | 911 | debug("Connect string found\n"); |
nixnax | 9:0992486d4a30 | 912 | } |
nixnax | 9:0992486d4a30 | 913 | } |
nixnax | 9:0992486d4a30 | 914 | } |
nixnax | 9:0992486d4a30 | 915 | |
nixnax | 0:2cf4880c312a | 916 | int main() |
nixnax | 0:2cf4880c312a | 917 | { |
nixnax | 14:c65831c25aaa | 918 | pc.baud(115200); // USB virtual serial port |
nixnax | 41:e58a5a09f411 | 919 | #ifndef SERIAL_PORT_MONITOR_NO |
nixnax | 49:2213f9c132b2 | 920 | xx.baud(115200); // second serial port for debug messages |
nixnax | 9:0992486d4a30 | 921 | xx.puts("\x1b[2J\x1b[HReady\n"); // VT100 code for clear screen & home |
nixnax | 41:e58a5a09f411 | 922 | #endif |
nixnax | 9:0992486d4a30 | 923 | pppInitStruct(); // initialize all the PPP properties |
nixnax | 9:0992486d4a30 | 924 | pc.attach(&rxHandler,Serial::RxIrq); // start the receive handler |
nixnax | 0:2cf4880c312a | 925 | while(1) { |
nixnax | 51:a86d56844324 | 926 | scanForConnectString(); // respond to connect command from windows dial up networking |
nixnax | 50:ad4e7c3c88e5 | 927 | wait_for_HDLC_frame(); |
nixnax | 7:ab147f5e97ac | 928 | } |
nixnax | 7:ab147f5e97ac | 929 | } |