Small Internet Protocol Stack using a standard serial port.

Dependencies:   mbed

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

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

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

Describe PPP-Blinky in Three Sentences

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

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

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

No Ethernet Port Required

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

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

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

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

Webserver

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

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

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

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

WebSocket Service

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

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

Trying PPP-Blinky on your mbed board

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

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

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

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

Creating a Dial-up Connection in Windows

/media/uploads/nixnax/modem.jpg

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

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

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

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

Connecting to PPP-Blinky from your PC

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

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

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

One Million Pings!

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

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

Monitoring PPP-Blinky Packets

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

Responses from PPP-Blinky are shown in blue.

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

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

Debug Output

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

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

Creating a Dial-Up Connection in Linux

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

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

Caveats

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

Committer:
nixnax
Date:
Sun Jul 30 02:22:59 2017 +0000
Revision:
100:3f3a017684c5
Parent:
99:3f56162e703e
Child:
101:0a15de2ca623
Remove unused ppp.seq

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nixnax 93:9675adc36882 1 // PPP-Blinky - "The Most Basic Internet Of Things"
nixnax 0:2cf4880c312a 2
nixnax 93:9675adc36882 3 // A Tiny Webserver Using Windows XP/7/8/10/Linux Dial-Up Networking Over A Serial Port.
nixnax 0:2cf4880c312a 4
nixnax 41:e58a5a09f411 5 // Also receives UDP packets and responds to ping (ICMP Echo requests)
nixnax 4:a469050d5b80 6
nixnax 93:9675adc36882 7 // 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 81:9ede60e9a2c8 8
nixnax 41:e58a5a09f411 9 // Notes and Instructions
nixnax 41:e58a5a09f411 10 // http://bit.ly/PPP-Blinky-Instructions
nixnax 0:2cf4880c312a 11
nixnax 41:e58a5a09f411 12 // Handy reading material
nixnax 6:fba4c2e817b8 13 // https://technet.microsoft.com/en-us/library/cc957992.aspx
nixnax 9:0992486d4a30 14 // https://en.wikibooks.org/wiki/Serial_Programming/IP_Over_Serial_Connections
nixnax 39:b90183d35f1e 15 // http://bit.ly/dialup777error - how to solve Dial Up Error 777 in Windows 7/8/10
nixnax 41:e58a5a09f411 16 // http://atari.kensclassics.org/wcomlog.htm
nixnax 6:fba4c2e817b8 17
nixnax 29:30de79d658f6 18 // Handy tools
nixnax 44:d0c61ae49ea5 19 // 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 92:cb962b365cce 20 // https://www.microsoft.com/en-us/download/details.aspx?id=4865 - Microsoft network monitor - real-time monitoring of PPP packets -
nixnax 29:30de79d658f6 21 // http://pingtester.net/ - nice tool for high rate ping testing
nixnax 95:40af49390daf 22 // 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 29:30de79d658f6 23 // https://technet.microsoft.com/en-us/sysinternals/pstools.aspx - psping for fast testing of ICMP ping function
nixnax 41:e58a5a09f411 24 // https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky
nixnax 91:5141ae9fba53 25 // 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 41:e58a5a09f411 26
nixnax 92:cb962b365cce 27 // Connecting PPP-Blinky to Linux
nixnax 93:9675adc36882 28 // PPP-Blinky can be made to talk to Linux - tested on Fedora - the following command, which uses pppd, works:
nixnax 93:9675adc36882 29 // pppd /dev/ttyACM0 115200 debug dump local passive noccp novj nodetach nocrtscts 172.10.10.1:172.10.10.2
nixnax 92:cb962b365cce 30 // in the above command 172.10.10.1 is the adapter IP, and 172.10.10.2 is the IP of PPP-Blinky.
nixnax 93:9675adc36882 31 // See also https://en.wikipedia.org/wiki/Point-to-Point_Protocol_daemon
nixnax 93:9675adc36882 32
nixnax 93:9675adc36882 33 // Ok, enough talking, time to check out some code!!
nixnax 92:cb962b365cce 34
nixnax 81:9ede60e9a2c8 35 #include "mbed.h"
nixnax 66:f005b9fdf4d1 36
nixnax 93:9675adc36882 37 // The #define below enables/disables a second (OPTIONAL) serial port that prints out interesting diagnostic messages.
nixnax 66:f005b9fdf4d1 38 // 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 94:8ee3eec2a2bb 39 #define SERIAL_PORT_MONITOR_NO /* change to SERIAL_PORT_MONITOR_YES for debug messages */
nixnax 0:2cf4880c312a 40
nixnax 41:e58a5a09f411 41 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 85:53e57ff1cf05 42
nixnax 93:9675adc36882 43 // here we define the OPTIONAL, second debug serial port for the various target boards
nixnax 93:9675adc36882 44 // insert your target board's port here if it's not in yet - if it works, please send it to me - thanks!!!
nixnax 88:b4a71242837c 45 #if defined(TARGET_LPC1768)
nixnax 85:53e57ff1cf05 46 Serial 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 88:b4a71242837c 47 #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_L152RE)
nixnax 85:53e57ff1cf05 48 Serial 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 85:53e57ff1cf05 49 #else
nixnax 85:53e57ff1cf05 50 #error Add your target board's second serial port here if you want to use debugging - or choose SERIAL_PORT_MONITOR_NO
nixnax 85:53e57ff1cf05 51 #endif
nixnax 85:53e57ff1cf05 52
nixnax 93:9675adc36882 53 #define debug(x...) xx.printf (x)
nixnax 85:53e57ff1cf05 54
nixnax 41:e58a5a09f411 55 #else
nixnax 42:4de44be70bfd 56 #define debug(x...) {}
nixnax 41:e58a5a09f411 57 #endif
nixnax 41:e58a5a09f411 58
nixnax 93:9675adc36882 59 // verbosity flag used in debug printouts - change to 1 to see more debug info. Lots of interesting info.
nixnax 90:55e0f243a7ce 60 #define v0 0
nixnax 93:9675adc36882 61 // verbosity flag used in debug printouts - change to 1 to see more debug info. Lots of interesting info.
nixnax 81:9ede60e9a2c8 62 #define v1 0
nixnax 93:9675adc36882 63 // verbosity flag used in debug printouts - change to 1 to see more debug info. Lots of interesting info.
nixnax 77:abf92baebb42 64 #define v2 0
nixnax 29:30de79d658f6 65
nixnax 93:9675adc36882 66 // this is the webpage we serve when we get an HTTP request to root (/)
nixnax 93:9675adc36882 67 // keep size under ~900 bytes to fit into a single PPP packet
nixnax 93:9675adc36882 68 const static char rootWebPage[] = "\
nixnax 66:f005b9fdf4d1 69 <!DOCTYPE html>\
nixnax 66:f005b9fdf4d1 70 <html>\
nixnax 66:f005b9fdf4d1 71 <head>\
nixnax 66:f005b9fdf4d1 72 <title>mbed-PPP-Blinky</title>\
nixnax 66:f005b9fdf4d1 73 <script>\
nixnax 66:f005b9fdf4d1 74 window.onload=function(){\
nixnax 66:f005b9fdf4d1 75 setInterval(function(){function x(){return document.getElementById('w');};\
nixnax 66:f005b9fdf4d1 76 x().textContent = parseInt(x().textContent)+1;},100);};\
nixnax 66:f005b9fdf4d1 77 </script>\
nixnax 66:f005b9fdf4d1 78 </head>\
nixnax 66:f005b9fdf4d1 79 <body style=\"font-family: sans-serif; font-size:30px; color:#807070\">\
nixnax 66:f005b9fdf4d1 80 <h1>mbed PPP-Blinky Up and Running</h1>\
nixnax 66:f005b9fdf4d1 81 <h1 id=\"w\" style=\"text-align:center;\">0</h1>\
nixnax 66:f005b9fdf4d1 82 <h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\
nixnax 66:f005b9fdf4d1 83 </body>\
nixnax 69:23f560087c16 84 </html>"; // around 464 bytes long
nixnax 69:23f560087c16 85
nixnax 73:2f56ec87dbe9 86 // The serial port on your mbed hardware. Your PC should be configured to view this port as a standard dial-up networking modem.
nixnax 73:2f56ec87dbe9 87 // On Windows the model type of the modem should be selected as "Communications cable between two computers"
nixnax 72:ad3d12753acf 88 // The modem baud rate should be set to 115200 baud
nixnax 72:ad3d12753acf 89 // See instructions at the top.
nixnax 73:2f56ec87dbe9 90 // 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 73:2f56ec87dbe9 91 Serial pc(USBTX, USBRX); // usb virtual com port for mbed hardware
nixnax 29:30de79d658f6 92
nixnax 29:30de79d658f6 93 DigitalOut led1(LED1); // this led toggles when a packet is received
nixnax 4:a469050d5b80 94
nixnax 66:f005b9fdf4d1 95 // the standard hdlc frame start/end character. It's the tilde character "~"
nixnax 4:a469050d5b80 96 #define FRAME_7E (0x7e)
nixnax 29:30de79d658f6 97
nixnax 29:30de79d658f6 98 // a structure to keep all our ppp globals in
nixnax 29:30de79d658f6 99 struct pppType {
nixnax 38:ab582987926e 100 int online; // we hunt for a PPP connection if this is zero
nixnax 87:9f5ac1fabd95 101 unsigned int ident; // our IP ident value
nixnax 38:ab582987926e 102 int crc; // for calculating IP and TCP CRCs
nixnax 38:ab582987926e 103 int ledState; // state of LED1
nixnax 98:3babad0d1bd4 104 int httpPageCount;
nixnax 4:a469050d5b80 105 struct {
nixnax 81:9ede60e9a2c8 106 #define RXBUFLEN (1<<14)
nixnax 93:9675adc36882 107 // the serial port receive buffer and packet buffer, size is RXBUFLEN (currently 8192 bytes)
nixnax 85:53e57ff1cf05 108 char buf[RXBUFLEN]; // RXBUFLEN MUST be a power of two because we use & operator for fast wrap-around in ring buffer
nixnax 93:9675adc36882 109 int head;
nixnax 93:9675adc36882 110 int tail;
nixnax 95:40af49390daf 111 int rtail;
nixnax 93:9675adc36882 112 int buflevel;
nixnax 38:ab582987926e 113 } rx; // serial port objects
nixnax 4:a469050d5b80 114 struct {
nixnax 38:ab582987926e 115 int len; // number of bytes in buffer
nixnax 38:ab582987926e 116 int crc; // PPP CRC (frame check)
nixnax 77:abf92baebb42 117 #define TCP_max_size 3300
nixnax 77:abf92baebb42 118 char buf[TCP_max_size]; // send and receive buffer large enough for unstuffed (decoded) hdlc frames
nixnax 38:ab582987926e 119 } pkt; // ppp buffer objects
nixnax 50:ad4e7c3c88e5 120 struct {
nixnax 50:ad4e7c3c88e5 121 int frameStartIndex; // frame start marker
nixnax 50:ad4e7c3c88e5 122 int frameEndIndex; // frame end marker
nixnax 81:9ede60e9a2c8 123 int frameFound; // we have found at least one start of frame already
nixnax 50:ad4e7c3c88e5 124 } hdlc; // hdlc frame objects
nixnax 29:30de79d658f6 125 };
nixnax 31:e000c1b9c565 126
nixnax 29:30de79d658f6 127 pppType ppp; // our global - definitely not thread safe
nixnax 0:2cf4880c312a 128
nixnax 93:9675adc36882 129 // Initialize our global structure, clear the buffer, etc.
nixnax 29:30de79d658f6 130 void pppInitStruct()
nixnax 29:30de79d658f6 131 {
nixnax 81:9ede60e9a2c8 132 memset( ppp.rx.buf, 0, RXBUFLEN);
nixnax 29:30de79d658f6 133 ppp.online=0;
nixnax 29:30de79d658f6 134 ppp.rx.tail=0;
nixnax 93:9675adc36882 135 ppp.rx.rtail=0;
nixnax 29:30de79d658f6 136 ppp.rx.head=0;
nixnax 81:9ede60e9a2c8 137 ppp.rx.buflevel=0;
nixnax 29:30de79d658f6 138 ppp.pkt.len=0;
nixnax 83:cdcb81d1910f 139 ppp.ident=1000;
nixnax 29:30de79d658f6 140 ppp.ledState=0;
nixnax 81:9ede60e9a2c8 141 ppp.hdlc.frameFound=0;
nixnax 81:9ede60e9a2c8 142 ppp.hdlc.frameStartIndex=0;
nixnax 98:3babad0d1bd4 143 ppp.httpPageCount=0;
nixnax 29:30de79d658f6 144 }
nixnax 26:11f4eb2663a7 145
nixnax 43:aa57db08995d 146 void led1Toggle()
nixnax 43:aa57db08995d 147 {
nixnax 43:aa57db08995d 148 ppp.ledState = ppp.ledState? 0 : 1;
nixnax 93:9675adc36882 149 led1 = ppp.ledState; // toggle led
nixnax 43:aa57db08995d 150 }
nixnax 43:aa57db08995d 151
nixnax 29:30de79d658f6 152 void crcReset()
nixnax 29:30de79d658f6 153 {
nixnax 29:30de79d658f6 154 ppp.crc=0xffff; // crc restart
nixnax 29:30de79d658f6 155 }
nixnax 4:a469050d5b80 156
nixnax 29:30de79d658f6 157 void crcDo(int x) // cumulative crc
nixnax 29:30de79d658f6 158 {
nixnax 29:30de79d658f6 159 for (int i=0; i<8; i++) {
nixnax 29:30de79d658f6 160 ppp.crc=((ppp.crc&1)^(x&1))?(ppp.crc>>1)^0x8408:ppp.crc>>1; // crc calculator
nixnax 29:30de79d658f6 161 x>>=1;
nixnax 29:30de79d658f6 162 }
nixnax 29:30de79d658f6 163 }
nixnax 29:30de79d658f6 164
nixnax 29:30de79d658f6 165 int crcBuf(char * buf, int size) // crc on an entire block of memory
nixnax 29:30de79d658f6 166 {
nixnax 31:e000c1b9c565 167 crcReset();
nixnax 31:e000c1b9c565 168 for(int i=0; i<size; i++)crcDo(*buf++);
nixnax 29:30de79d658f6 169 return ppp.crc;
nixnax 29:30de79d658f6 170 }
nixnax 0:2cf4880c312a 171
nixnax 85:53e57ff1cf05 172
nixnax 85:53e57ff1cf05 173 // fill our own receive buffer with characters from the PPP serial port
nixnax 85:53e57ff1cf05 174 void fillbuf()
nixnax 0:2cf4880c312a 175 {
nixnax 85:53e57ff1cf05 176 if ( pc.readable() ) {
nixnax 85:53e57ff1cf05 177 int hd = (ppp.rx.head+1)&(RXBUFLEN-1); // increment/wrap head index
nixnax 93:9675adc36882 178 if ( hd == ppp.rx.rtail ) {
nixnax 85:53e57ff1cf05 179 debug("\nReceive buffer full\n");
nixnax 85:53e57ff1cf05 180 return;
nixnax 83:cdcb81d1910f 181 }
nixnax 17:4918c893d802 182 ppp.rx.buf[ppp.rx.head]=pc.getc(); // insert in rx buffer
nixnax 20:5db9b77b38a6 183 ppp.rx.head = hd; // update head pointer
nixnax 81:9ede60e9a2c8 184 ppp.rx.buflevel++;
nixnax 15:b0154c910143 185 }
nixnax 0:2cf4880c312a 186 }
nixnax 0:2cf4880c312a 187
nixnax 85:53e57ff1cf05 188 int rxbufNotEmpty() // check if rx buffer has data
nixnax 0:2cf4880c312a 189 {
nixnax 43:aa57db08995d 190 int emptyStatus = (ppp.rx.head==ppp.rx.tail) ? 0 : 1 ;
nixnax 43:aa57db08995d 191 return emptyStatus;
nixnax 0:2cf4880c312a 192 }
nixnax 0:2cf4880c312a 193
nixnax 85:53e57ff1cf05 194 int pc_getBuf() // get one character from the buffer
nixnax 0:2cf4880c312a 195 {
nixnax 63:9253b0e1b7d8 196 int x = ppp.rx.buf[ ppp.rx.tail ];
nixnax 81:9ede60e9a2c8 197 ppp.rx.tail=(ppp.rx.tail+1)&(RXBUFLEN-1);
nixnax 81:9ede60e9a2c8 198 ppp.rx.buflevel--;
nixnax 63:9253b0e1b7d8 199 return x;
nixnax 0:2cf4880c312a 200 }
nixnax 0:2cf4880c312a 201
nixnax 95:40af49390daf 202 // Note - the hex output of dumpPPPFrame() can be imported into WireShark
nixnax 95:40af49390daf 203 // Capture the frame's hex output in your terminal program and save as a text file
nixnax 95:40af49390daf 204 // In WireShark, use "Import Hex File". Options are: Offset=None, Protocol=PPP.
nixnax 95:40af49390daf 205 void dumpPPPFrame()
nixnax 95:40af49390daf 206 {
nixnax 95:40af49390daf 207 for(int i=0; i<ppp.pkt.len; i++) debug("%02x ", ppp.pkt.buf[i]);
nixnax 95:40af49390daf 208 debug(" CRC=%04x Len=%d\n", ppp.pkt.crc, ppp.pkt.len);
nixnax 95:40af49390daf 209 }
nixnax 95:40af49390daf 210
nixnax 50:ad4e7c3c88e5 211 void processHDLCFrame(int start, int end) // process received frame
nixnax 29:30de79d658f6 212 {
nixnax 38:ab582987926e 213 led1Toggle(); // change led1 state on every frame we receive
nixnax 29:30de79d658f6 214 if(start==end) {
nixnax 29:30de79d658f6 215 return;
nixnax 29:30de79d658f6 216 }
nixnax 9:0992486d4a30 217 crcReset();
nixnax 9:0992486d4a30 218 char * dest = ppp.pkt.buf;
nixnax 9:0992486d4a30 219 ppp.pkt.len=0;
nixnax 9:0992486d4a30 220 int unstuff=0;
nixnax 17:4918c893d802 221 int idx = start;
nixnax 17:4918c893d802 222 while(1) {
nixnax 9:0992486d4a30 223 if (unstuff==0) {
nixnax 72:ad3d12753acf 224 if (ppp.rx.buf[idx]==0x7d) unstuff=1;
nixnax 29:30de79d658f6 225 else {
nixnax 72:ad3d12753acf 226 *dest = ppp.rx.buf[idx];
nixnax 29:30de79d658f6 227 ppp.pkt.len++;
nixnax 29:30de79d658f6 228 dest++;
nixnax 72:ad3d12753acf 229 crcDo(ppp.rx.buf[idx]);
nixnax 29:30de79d658f6 230 }
nixnax 66:f005b9fdf4d1 231 } else { // unstuff characters prefixed with 0x7d
nixnax 72:ad3d12753acf 232 *dest = ppp.rx.buf[idx]^0x20;
nixnax 29:30de79d658f6 233 ppp.pkt.len++;
nixnax 29:30de79d658f6 234 dest++;
nixnax 72:ad3d12753acf 235 crcDo(ppp.rx.buf[idx]^0x20);
nixnax 9:0992486d4a30 236 unstuff=0;
nixnax 9:0992486d4a30 237 }
nixnax 81:9ede60e9a2c8 238 idx = (idx+1) & (RXBUFLEN-1);
nixnax 17:4918c893d802 239 if (idx == end) break;
nixnax 9:0992486d4a30 240 }
nixnax 29:30de79d658f6 241 ppp.pkt.crc = ppp.crc & 0xffff;
nixnax 9:0992486d4a30 242 if (ppp.pkt.crc == 0xf0b8) { // check for good CRC
nixnax 16:cb0b80c24ba2 243 void determinePacketType(); // declaration only
nixnax 9:0992486d4a30 244 determinePacketType();
nixnax 90:55e0f243a7ce 245 } else {
nixnax 95:40af49390daf 246 if (0) { // don't normally report FCS because windows early-terminates packets, and early-terminated packets all have FCS errors.
nixnax 95:40af49390daf 247 debug("\nPPP 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 95:40af49390daf 248 }
nixnax 9:0992486d4a30 249 }
nixnax 9:0992486d4a30 250 }
nixnax 9:0992486d4a30 251
nixnax 29:30de79d658f6 252 void hdlcPut(int ch) // do hdlc handling of special (flag) characters
nixnax 29:30de79d658f6 253 {
nixnax 29:30de79d658f6 254 if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) ) {
nixnax 29:30de79d658f6 255 pc.putc(0x7d);
nixnax 66:f005b9fdf4d1 256 pc.putc(ch^0x20); // these characters need special handling
nixnax 29:30de79d658f6 257 } else {
nixnax 29:30de79d658f6 258 pc.putc(ch);
nixnax 29:30de79d658f6 259 }
nixnax 11:f58998c24f0b 260 }
nixnax 9:0992486d4a30 261
nixnax 93:9675adc36882 262 void send_pppFrame() // send a PPP frame in HDLC format
nixnax 29:30de79d658f6 263 {
nixnax 17:4918c893d802 264 int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc
nixnax 12:db0dc91f0231 265 ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc)
nixnax 12:db0dc91f0231 266 ppp.pkt.buf[ ppp.pkt.len-1 ] = (~crc>>8); // fcs hi (crc)
nixnax 16:cb0b80c24ba2 267 pc.putc(0x7e); // hdlc start-of-frame "flag"
nixnax 95:40af49390daf 268 for(int i=0; i<ppp.pkt.len; i++) {
nixnax 93:9675adc36882 269 hdlcPut( ppp.pkt.buf[i] ); // send a character
nixnax 95:40af49390daf 270
nixnax 93:9675adc36882 271 if((i&0x7f)==0) fillbuf(); // handle received characters very every 128 sent
nixnax 93:9675adc36882 272 }
nixnax 16:cb0b80c24ba2 273 pc.putc(0x7e); // hdlc end-of-frame "flag"
nixnax 9:0992486d4a30 274 }
nixnax 9:0992486d4a30 275
nixnax 93:9675adc36882 276 void ipcpConfigRequestHandler()
nixnax 29:30de79d658f6 277 {
nixnax 93:9675adc36882 278 debug("Their IPCP Config Req, Our Ack\n");
nixnax 92:cb962b365cce 279 ppp.pkt.buf[4]=2; // change code to ack
nixnax 93:9675adc36882 280 send_pppFrame(); // simply acknowledge everything they ask for
nixnax 93:9675adc36882 281 debug("Our IPCP Ask\n");
nixnax 92:cb962b365cce 282 // send our own request now
nixnax 92:cb962b365cce 283 ppp.pkt.buf[4]=1; // change code to request
nixnax 92:cb962b365cce 284 ppp.pkt.buf[5]++; // increment sequence number
nixnax 92:cb962b365cce 285 ppp.pkt.buf[7]=4; // no options requested
nixnax 93:9675adc36882 286 send_pppFrame(); // this is our request
nixnax 92:cb962b365cce 287 return;
nixnax 9:0992486d4a30 288 }
nixnax 9:0992486d4a30 289
nixnax 93:9675adc36882 290 void ipcpAckHandler()
nixnax 29:30de79d658f6 291 {
nixnax 93:9675adc36882 292 debug("Their IPCP Grant\n");
nixnax 29:30de79d658f6 293 }
nixnax 9:0992486d4a30 294
nixnax 93:9675adc36882 295 void ipcpNackHandler()
nixnax 29:30de79d658f6 296 {
nixnax 93:9675adc36882 297 debug("Their IPCP Nack\n");
nixnax 29:30de79d658f6 298 }
nixnax 9:0992486d4a30 299
nixnax 93:9675adc36882 300 void ipcpDefaultHandler()
nixnax 29:30de79d658f6 301 {
nixnax 93:9675adc36882 302 debug("Their IPCP Other\n");
nixnax 29:30de79d658f6 303 }
nixnax 29:30de79d658f6 304
nixnax 29:30de79d658f6 305 void IPCPframe()
nixnax 29:30de79d658f6 306 {
nixnax 9:0992486d4a30 307 int code = ppp.pkt.buf[4]; // packet type is here
nixnax 9:0992486d4a30 308 switch (code) {
nixnax 29:30de79d658f6 309 case 1:
nixnax 93:9675adc36882 310 ipcpConfigRequestHandler();
nixnax 29:30de79d658f6 311 break;
nixnax 29:30de79d658f6 312 case 2:
nixnax 93:9675adc36882 313 ipcpAckHandler();
nixnax 29:30de79d658f6 314 break;
nixnax 29:30de79d658f6 315 case 3:
nixnax 93:9675adc36882 316 ipcpNackHandler();
nixnax 29:30de79d658f6 317 break;
nixnax 29:30de79d658f6 318 default:
nixnax 93:9675adc36882 319 ipcpDefaultHandler();
nixnax 9:0992486d4a30 320 }
nixnax 29:30de79d658f6 321 }
nixnax 9:0992486d4a30 322
nixnax 29:30de79d658f6 323 void UDPpacket()
nixnax 29:30de79d658f6 324 {
nixnax 12:db0dc91f0231 325 char * udpPkt = ppp.pkt.buf+4; // udp packet start
nixnax 16:cb0b80c24ba2 326 int headerSizeIP = (( udpPkt[0]&0xf)*4);
nixnax 29:30de79d658f6 327 char * udpBlock = udpPkt + headerSizeIP; // udp info start
nixnax 63:9253b0e1b7d8 328 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 12:db0dc91f0231 329 char * udpSrc = udpBlock; // source port
nixnax 12:db0dc91f0231 330 char * udpDst = udpBlock+2; // destination port
nixnax 63:9253b0e1b7d8 331 #endif
nixnax 12:db0dc91f0231 332 char * udpLen = udpBlock+4; // udp data length
nixnax 12:db0dc91f0231 333 char * udpInf = udpBlock+8; // actual start of info
nixnax 63:9253b0e1b7d8 334 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 12:db0dc91f0231 335 int srcPort = (udpSrc[0]<<8) | udpSrc[1];
nixnax 12:db0dc91f0231 336 int dstPort = (udpDst[0]<<8) | udpDst[1];
nixnax 12:db0dc91f0231 337 char * srcIP = udpPkt+12; // udp src addr
nixnax 12:db0dc91f0231 338 char * dstIP = udpPkt+16; // udp dst addr
nixnax 55:43faae812be3 339 #endif
nixnax 29:30de79d658f6 340 #define UDP_HEADER_SIZE 8
nixnax 12:db0dc91f0231 341 int udpLength = ((udpLen[0]<<8) | udpLen[1]) - UDP_HEADER_SIZE; // size of the actual udp data
nixnax 81:9ede60e9a2c8 342 if(v0) debug("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],srcPort);
nixnax 81:9ede60e9a2c8 343 if(v0) debug("%d.%d.%d.%d:%d ", dstIP[0],dstIP[1],dstIP[2],dstIP[3],dstPort);
nixnax 83:cdcb81d1910f 344 if(v0) debug("Len %03d", udpLength);
nixnax 29:30de79d658f6 345 int printSize = udpLength;
nixnax 29:30de79d658f6 346 if (printSize > 20) printSize = 20; // print only first 20 characters
nixnax 81:9ede60e9a2c8 347 if (v1) {
nixnax 29:30de79d658f6 348 for (int i=0; i<printSize; i++) {
nixnax 29:30de79d658f6 349 char ch = udpInf[i];
nixnax 29:30de79d658f6 350 if (ch>31 && ch<127) {
nixnax 42:4de44be70bfd 351 debug("%c", ch);
nixnax 29:30de79d658f6 352 } else {
nixnax 64:677b9713a120 353 debug("_");
nixnax 29:30de79d658f6 354 }
nixnax 29:30de79d658f6 355 }
nixnax 29:30de79d658f6 356 }
nixnax 83:cdcb81d1910f 357 if (v0) debug("\n");
nixnax 12:db0dc91f0231 358 }
nixnax 11:f58998c24f0b 359
nixnax 76:00e208cceb8b 360 unsigned int dataCheckSum(unsigned char * ptr, int len)
nixnax 29:30de79d658f6 361 {
nixnax 77:abf92baebb42 362
nixnax 75:0d513869231f 363 unsigned int sum=0;
nixnax 76:00e208cceb8b 364 unsigned char placeHolder;
nixnax 29:30de79d658f6 365 if (len&1) {
nixnax 76:00e208cceb8b 366 placeHolder = ptr[len]; // when length is odd stuff in a zero byte
nixnax 76:00e208cceb8b 367 ptr[len]=0;
nixnax 29:30de79d658f6 368 }
nixnax 29:30de79d658f6 369 for (int i=0; i<len/2; i++) {
nixnax 76:00e208cceb8b 370 unsigned int hi = *ptr;
nixnax 29:30de79d658f6 371 ptr++;
nixnax 76:00e208cceb8b 372 unsigned int lo = *ptr;
nixnax 29:30de79d658f6 373 ptr++;
nixnax 76:00e208cceb8b 374 unsigned int val = ( (hi<<8) | lo );
nixnax 11:f58998c24f0b 375 sum = sum + val;
nixnax 11:f58998c24f0b 376 }
nixnax 29:30de79d658f6 377 if (len&1) {
nixnax 76:00e208cceb8b 378 ptr[len] = placeHolder; // restore the last byte for odd lengths
nixnax 29:30de79d658f6 379 }
nixnax 76:00e208cceb8b 380 sum = (sum & 0xffff) + (sum>>16);
nixnax 76:00e208cceb8b 381 sum = (sum & 0xffff) + (sum>>16); // sum one more time to catch any carry from the carry
nixnax 12:db0dc91f0231 382 return ~sum;
nixnax 29:30de79d658f6 383 }
nixnax 11:f58998c24f0b 384
nixnax 29:30de79d658f6 385 void headerCheckSum()
nixnax 29:30de79d658f6 386 {
nixnax 11:f58998c24f0b 387 int len =(ppp.pkt.buf[4]&0xf)*4; // length of header in bytes
nixnax 11:f58998c24f0b 388 char * ptr = ppp.pkt.buf+4; // start of ip packet
nixnax 11:f58998c24f0b 389 int sum=0;
nixnax 11:f58998c24f0b 390
nixnax 29:30de79d658f6 391 for (int i=0; i<len/2; i++) {
nixnax 29:30de79d658f6 392 int hi = *ptr;
nixnax 29:30de79d658f6 393 ptr++;
nixnax 29:30de79d658f6 394 int lo = *ptr;
nixnax 29:30de79d658f6 395 ptr++;
nixnax 11:f58998c24f0b 396 int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 );
nixnax 11:f58998c24f0b 397 sum = sum + val;
nixnax 11:f58998c24f0b 398 }
nixnax 11:f58998c24f0b 399 sum = sum + (sum>>16);
nixnax 11:f58998c24f0b 400 sum = ~sum;
nixnax 11:f58998c24f0b 401 ppp.pkt.buf[14]= (sum>>8);
nixnax 11:f58998c24f0b 402 ppp.pkt.buf[15]= (sum );
nixnax 29:30de79d658f6 403 }
nixnax 9:0992486d4a30 404
nixnax 29:30de79d658f6 405 void ICMPpacket() // internet control message protocol
nixnax 29:30de79d658f6 406 {
nixnax 12:db0dc91f0231 407 char * ipPkt = ppp.pkt.buf+4; // ip packet start
nixnax 12:db0dc91f0231 408 char * pktLen = ipPkt+2;
nixnax 12:db0dc91f0231 409 int packetLength = (pktLen[0]<<8) | pktLen[1]; // icmp packet length
nixnax 16:cb0b80c24ba2 410 int headerSizeIP = (( ipPkt[0]&0xf)*4);
nixnax 16:cb0b80c24ba2 411 char * icmpType = ipPkt + headerSizeIP; // icmp data start
nixnax 13:d882b8a042b4 412 char * icmpSum = icmpType+2; // icmp checksum
nixnax 29:30de79d658f6 413 #define ICMP_TYPE_PING_REQUEST 8
nixnax 29:30de79d658f6 414 if ( icmpType[0] == ICMP_TYPE_PING_REQUEST ) {
nixnax 12:db0dc91f0231 415 char * ipTTL = ipPkt+8; // time to live
nixnax 12:db0dc91f0231 416 ipTTL[0]--; // decrement time to live
nixnax 12:db0dc91f0231 417 char * srcAdr = ipPkt+12;
nixnax 12:db0dc91f0231 418 char * dstAdr = ipPkt+16;
nixnax 63:9253b0e1b7d8 419 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 18:3e35de1bc877 420 int icmpIdent = (icmpType[4]<<8)|icmpType[5];
nixnax 29:30de79d658f6 421 int icmpSequence = (icmpType[6]<<8)|icmpType[7];
nixnax 63:9253b0e1b7d8 422 #endif
nixnax 81:9ede60e9a2c8 423 if(v0) 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 81:9ede60e9a2c8 424 if(v0) debug("Ident %04x Sequence %04d ",icmpIdent,icmpSequence);
nixnax 29:30de79d658f6 425 char src[4];
nixnax 29:30de79d658f6 426 char dst[4];
nixnax 12:db0dc91f0231 427 memcpy(src, srcAdr,4);
nixnax 12:db0dc91f0231 428 memcpy(dst, dstAdr,4);
nixnax 12:db0dc91f0231 429 memcpy(srcAdr, dst,4);
nixnax 12:db0dc91f0231 430 memcpy(dstAdr, src,4); // swap src & dest ip
nixnax 12:db0dc91f0231 431 char * chkSum = ipPkt+10;
nixnax 29:30de79d658f6 432 chkSum[0]=0;
nixnax 29:30de79d658f6 433 chkSum[1]=0;
nixnax 12:db0dc91f0231 434 headerCheckSum(); // new ip header checksum
nixnax 29:30de79d658f6 435 #define ICMP_TYPE_ECHO_REPLY 0
nixnax 16:cb0b80c24ba2 436 icmpType[0]=ICMP_TYPE_ECHO_REPLY; // icmp echo reply
nixnax 29:30de79d658f6 437 icmpSum[0]=0;
nixnax 29:30de79d658f6 438 icmpSum[1]=0; // zero the checksum for recalculation
nixnax 16:cb0b80c24ba2 439 int icmpLength = packetLength - headerSizeIP; // length of ICMP data portion
nixnax 76:00e208cceb8b 440 unsigned int sum = dataCheckSum( (unsigned char *)icmpType, icmpLength); // this checksum on icmp data portion
nixnax 76:00e208cceb8b 441 icmpSum[0]=(sum>>8)&0xff;
nixnax 76:00e208cceb8b 442 icmpSum[1]=(sum )&0xff; // new checksum for ICMP data portion
nixnax 16:cb0b80c24ba2 443
nixnax 16:cb0b80c24ba2 444 int printSize = icmpLength-8; // exclude size of icmp header
nixnax 25:0b0450e1b08b 445 char * icmpData = icmpType+8; // the actual payload data is after the header
nixnax 25:0b0450e1b08b 446 if (printSize > 10) printSize = 10; // print up to 20 characters
nixnax 29:30de79d658f6 447 if (v0) {
nixnax 29:30de79d658f6 448 for (int i=0; i<printSize; i++) {
nixnax 29:30de79d658f6 449 char ch = icmpData[i];
nixnax 29:30de79d658f6 450 if (ch>31 && ch<127) {
nixnax 42:4de44be70bfd 451 debug("%c",ch);
nixnax 29:30de79d658f6 452 } else {
nixnax 64:677b9713a120 453 debug("_");
nixnax 29:30de79d658f6 454 }
nixnax 29:30de79d658f6 455 }
nixnax 64:677b9713a120 456 debug("\n");
nixnax 29:30de79d658f6 457 }
nixnax 93:9675adc36882 458 send_pppFrame(); // reply to the ping
nixnax 29:30de79d658f6 459
nixnax 12:db0dc91f0231 460 } else {
nixnax 29:30de79d658f6 461 if (v0) {
nixnax 42:4de44be70bfd 462 debug("ICMP type=%d \n", icmpType[0]);
nixnax 29:30de79d658f6 463 }
nixnax 11:f58998c24f0b 464 }
nixnax 11:f58998c24f0b 465 }
nixnax 11:f58998c24f0b 466
nixnax 29:30de79d658f6 467 void IGMPpacket() // internet group management protocol
nixnax 29:30de79d658f6 468 {
nixnax 29:30de79d658f6 469 if (v0) {
nixnax 42:4de44be70bfd 470 debug("IGMP type=%d \n", ppp.pkt.buf[28]);
nixnax 29:30de79d658f6 471 }
nixnax 29:30de79d658f6 472 }
nixnax 11:f58998c24f0b 473
nixnax 29:30de79d658f6 474 void dumpHeaderIP ()
nixnax 29:30de79d658f6 475 {
nixnax 95:40af49390daf 476 if (v0) {
nixnax 95:40af49390daf 477 char * ipPkt = ppp.pkt.buf+4; // ip packet start
nixnax 63:9253b0e1b7d8 478 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 95:40af49390daf 479 char * version = ipPkt; // top 4 bits
nixnax 95:40af49390daf 480 char * ihl = ipPkt; // bottom 4 bits
nixnax 95:40af49390daf 481 char * dscp = ipPkt+1; // top 6 bits
nixnax 95:40af49390daf 482 char * ecn = ipPkt+1; // lower 2 bits
nixnax 95:40af49390daf 483 char * pktLen = ipPkt+2; // 2 bytes
nixnax 95:40af49390daf 484 char * ident = ipPkt+4; // 2 bytes
nixnax 95:40af49390daf 485 char * flags = ipPkt+6; // 2 bits
nixnax 95:40af49390daf 486 char * ttl = ipPkt+8; // 1 byte
nixnax 95:40af49390daf 487 char * protocol = ipPkt+9; // 1 byte
nixnax 95:40af49390daf 488 char * headercheck= ipPkt+10; // 2 bytes
nixnax 63:9253b0e1b7d8 489 #endif
nixnax 95:40af49390daf 490 char * srcAdr = ipPkt+12; // 4 bytes
nixnax 95:40af49390daf 491 char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes
nixnax 29:30de79d658f6 492
nixnax 55:43faae812be3 493 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 95:40af49390daf 494 int versionIP = (version[0]>>4)&0xf;
nixnax 95:40af49390daf 495 int headerSizeIP = (ihl[0]&0xf)*4;
nixnax 95:40af49390daf 496 int dscpIP = (dscp[0]>>2)&0x3f;
nixnax 95:40af49390daf 497 int ecnIP = ecn[0]&3;
nixnax 95:40af49390daf 498 int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length
nixnax 95:40af49390daf 499 int identIP = (ident[0]<<8)|ident[1];
nixnax 95:40af49390daf 500 int flagsIP = flags[0]>>14&3;
nixnax 95:40af49390daf 501 int ttlIP = ttl[0];
nixnax 95:40af49390daf 502 int protocolIP = protocol[0];
nixnax 95:40af49390daf 503 unsigned int checksumIP = (headercheck[0]<<8)|headercheck[1];
nixnax 55:43faae812be3 504 #endif
nixnax 55:43faae812be3 505
nixnax 95:40af49390daf 506 char srcIP [16];
nixnax 95:40af49390daf 507 snprintf(srcIP,16, "%d.%d.%d.%d", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3]);
nixnax 95:40af49390daf 508 char dstIP [16];
nixnax 95:40af49390daf 509 snprintf(dstIP,16, "%d.%d.%d.%d", dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]);
nixnax 95:40af49390daf 510 debug("IP %s %s v%d h%d d%d e%d L%03d ",srcIP,dstIP,versionIP,headerSizeIP,dscpIP,ecnIP,packetLength);
nixnax 95:40af49390daf 511 debug("i%04x f%d t%d p%d C%04x\n",identIP,flagsIP,ttlIP,protocolIP,checksumIP);
nixnax 95:40af49390daf 512 }
nixnax 29:30de79d658f6 513 }
nixnax 26:11f4eb2663a7 514
nixnax 29:30de79d658f6 515 void dumpHeaderTCP()
nixnax 29:30de79d658f6 516 {
nixnax 83:cdcb81d1910f 517 if( v0 ) {
nixnax 81:9ede60e9a2c8 518
nixnax 81:9ede60e9a2c8 519 int headerSizeIP = (ppp.pkt.buf[4]&0xf)*4; // header size of ip portion
nixnax 81:9ede60e9a2c8 520 char * tcpStart = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet
nixnax 63:9253b0e1b7d8 521 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 81:9ede60e9a2c8 522 char * seqtcp = tcpStart + 4; // 4 bytes
nixnax 81:9ede60e9a2c8 523 char * acktcp = tcpStart + 8; // 4 bytes
nixnax 63:9253b0e1b7d8 524 #endif
nixnax 81:9ede60e9a2c8 525 char * flagbitstcp = tcpStart + 12; // 9 bits
nixnax 63:9253b0e1b7d8 526 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 81:9ede60e9a2c8 527 unsigned int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
nixnax 81:9ede60e9a2c8 528 unsigned int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
nixnax 63:9253b0e1b7d8 529 #endif
nixnax 81:9ede60e9a2c8 530 int flags = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1];
nixnax 10:74f8233f72c0 531
nixnax 81:9ede60e9a2c8 532 char flagInfo[10]; // text string presentating the TCP flags
nixnax 81:9ede60e9a2c8 533 memset(flagInfo,'.', 9); // fill string with "........."
nixnax 81:9ede60e9a2c8 534 memset(flagInfo+9,0,1); // null terminate string
nixnax 66:f005b9fdf4d1 535
nixnax 81:9ede60e9a2c8 536 if (flags & (1<<0)) flagInfo[0]='F';
nixnax 81:9ede60e9a2c8 537 if (flags & (1<<1)) flagInfo[1]='S';
nixnax 81:9ede60e9a2c8 538 if (flags & (1<<2)) flagInfo[2]='R';
nixnax 81:9ede60e9a2c8 539 if (flags & (1<<3)) flagInfo[3]='P';
nixnax 81:9ede60e9a2c8 540 if (flags & (1<<4)) flagInfo[4]='A';
nixnax 81:9ede60e9a2c8 541 if (flags & (1<<5)) flagInfo[5]='U';
nixnax 81:9ede60e9a2c8 542 if (flags & (1<<6)) flagInfo[6]='E';
nixnax 81:9ede60e9a2c8 543 if (flags & (1<<7)) flagInfo[7]='C';
nixnax 81:9ede60e9a2c8 544 if (flags & (1<<8)) flagInfo[8]='N';
nixnax 81:9ede60e9a2c8 545 debug("TCP Flags %s Seq %u Ack %u\n", flagInfo, seq, ack); // show the flags in debug
nixnax 29:30de79d658f6 546 }
nixnax 29:30de79d658f6 547 }
nixnax 29:30de79d658f6 548
nixnax 68:0b74763ae67f 549 int httpResponse(char * dataStart)
nixnax 67:a63e3486bcda 550 {
nixnax 67:a63e3486bcda 551 int n=0; // number of bytes we have printed so far
nixnax 77:abf92baebb42 552 int nHeader; // byte size of HTTP header
nixnax 83:cdcb81d1910f 553 int contentLengthStart; // index where HTML starts
nixnax 93:9675adc36882 554 int xFetch, httpGetRoot; // temporary storage of strncmp results
nixnax 85:53e57ff1cf05 555
nixnax 98:3babad0d1bd4 556 ppp.httpPageCount++; // increment the number of frames we have made
nixnax 85:53e57ff1cf05 557
nixnax 98:3babad0d1bd4 558 httpGetRoot = strncmp(dataStart, "GET / HTTP/1.", 13); // found GET, respond to both HTTP/1.<anything>
nixnax 99:3f56162e703e 559 xFetch = strncmp(dataStart, "GET /x", 6); // found GET /x , respond to both HTTP/1.<anything>
nixnax 99:3f56162e703e 560 // for example, in linux, xFetch can be used as: echo GET /x | nc 172.10.10.2
nixnax 93:9675adc36882 561 if( (httpGetRoot==0) || (xFetch==0) ) {
nixnax 89:2c8dd0c2a426 562 n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: mbed-PPP-Blinky\r\n"); // 200 OK header
nixnax 83:cdcb81d1910f 563 } else {
nixnax 89:2c8dd0c2a426 564 n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\nServer: mbed-PPP-Blinky\r\n"); // 404 header
nixnax 83:cdcb81d1910f 565 }
nixnax 83:cdcb81d1910f 566 n=n+sprintf(n+dataStart,"Content-Length: "); // http header
nixnax 83:cdcb81d1910f 567 contentLengthStart = n; // remember where Content-Length is in buffer
nixnax 83:cdcb81d1910f 568 n=n+sprintf(n+dataStart,"?????\r\n"); // leave five spaces for content length - will be updated later
nixnax 83:cdcb81d1910f 569 n=n+sprintf(n+dataStart,"Connection: close\r\n"); // close connection immediately
nixnax 83:cdcb81d1910f 570 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 83:cdcb81d1910f 571 nHeader=n; // size of HTTP header
nixnax 93:9675adc36882 572 if( httpGetRoot == 0 ) {
nixnax 83:cdcb81d1910f 573 // this is where we insert our web page into the buffer
nixnax 93:9675adc36882 574 //n=n+sprintf(n+dataStart,"%s", ourWebPage);
nixnax 93:9675adc36882 575 memcpy(n+dataStart,rootWebPage,sizeof(rootWebPage));
nixnax 93:9675adc36882 576 n = n + sizeof(rootWebPage);
nixnax 85:53e57ff1cf05 577 } else {
nixnax 89:2c8dd0c2a426 578 if (xFetch == 0) {
nixnax 89:2c8dd0c2a426 579
nixnax 89:2c8dd0c2a426 580 #define W3C_COMPLIANT_RESPONSE_NO
nixnax 89:2c8dd0c2a426 581 // change the above to W3C_COMPLIANT_RESPONSE_YES if you want a W3C.org compliant HTTP response
nixnax 89:2c8dd0c2a426 582 #ifdef W3C_COMPLIANT_RESPONSE_YES
nixnax 89:2c8dd0c2a426 583 n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed-ppp-blinky</title>"); // html title (W3C.org required element)
nixnax 98:3babad0d1bd4 584 n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.httpPageCount); // body = the http frame count
nixnax 89:2c8dd0c2a426 585 #else
nixnax 98:3babad0d1bd4 586 #define BENCHMARK_USING_BROWSER_NO /* set to _YES if you want to use your browser as a benchmark tool of http fetches */
nixnax 98:3babad0d1bd4 587 #ifndef BENCHMARK_USING_BROWSER_NO
nixnax 98:3babad0d1bd4 588 // a small script that reloads the page after 10 ms - handy for benchmarking using your web browser
nixnax 98:3babad0d1bd4 589 n=n+sprintf(n+dataStart, "<script>setTimeout(function(){location.reload();},10);</script><body>%d</body>",ppp.httpPageCount);
nixnax 98:3babad0d1bd4 590 #else
nixnax 98:3babad0d1bd4 591 n=n+sprintf(n+dataStart,"%d",ppp.httpPageCount); // not valid html but fast, most browsers and curl are ok with it
nixnax 98:3babad0d1bd4 592 #endif
nixnax 89:2c8dd0c2a426 593 #endif
nixnax 89:2c8dd0c2a426 594
nixnax 89:2c8dd0c2a426 595 } else {
nixnax 89:2c8dd0c2a426 596 // all other requests get 404 Not Found response with a http frame count - nice for debugging
nixnax 89:2c8dd0c2a426 597 n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>ppp-blinky-mbed</title>"); // html title (required element)
nixnax 89:2c8dd0c2a426 598 n=n+sprintf(n+dataStart,"<body>Not Found</body>"); // not found message
nixnax 89:2c8dd0c2a426 599 }
nixnax 83:cdcb81d1910f 600 }
nixnax 87:9f5ac1fabd95 601 while( (n%4)!= 2) n=n+sprintf(n+dataStart," "); // insert spaces until n is exactly two away from a multiple of four
nixnax 87:9f5ac1fabd95 602 n=n+sprintf(n+dataStart,"\r\n"); // add the last two characters (\r\n) - n is now an exact multiple of four
nixnax 87:9f5ac1fabd95 603
nixnax 83:cdcb81d1910f 604 #define CONTENTLENGTHSIZE 5
nixnax 83:cdcb81d1910f 605 char contentLengthString[CONTENTLENGTHSIZE+1];
nixnax 83:cdcb81d1910f 606 snprintf(contentLengthString,CONTENTLENGTHSIZE+1,"%*d",CONTENTLENGTHSIZE,n-nHeader); // print Content-Length with leading spaces and fixed width equal to csize
nixnax 83:cdcb81d1910f 607 memcpy(dataStart+contentLengthStart, contentLengthString, CONTENTLENGTHSIZE); // copy Content-Length to it's place in the send buffer
nixnax 67:a63e3486bcda 608
nixnax 77:abf92baebb42 609 if (v2) {
nixnax 77:abf92baebb42 610 debug("HTTP Response: HTTP-header %d HTTP-content %d HTTP-total %d\n",nHeader,n-nHeader,n);
nixnax 77:abf92baebb42 611 }
nixnax 67:a63e3486bcda 612 return n; // total byte size of our response
nixnax 67:a63e3486bcda 613 }
nixnax 67:a63e3486bcda 614
nixnax 98:3babad0d1bd4 615 // if not an http response we just report the number of bytes received
nixnax 98:3babad0d1bd4 616 // this is handy when you for example want to use netcat (nc.exe) to talk to PPP-Blinky
nixnax 98:3babad0d1bd4 617 int tcpResponse(char * dataStart, int len)
nixnax 98:3babad0d1bd4 618 {
nixnax 98:3babad0d1bd4 619 int n=0; // number of bytes we have printed so far
nixnax 98:3babad0d1bd4 620
nixnax 98:3babad0d1bd4 621 n=n+sprintf(n+dataStart,"Got %04d bytes.\n",len); // report the number of bytes received
nixnax 98:3babad0d1bd4 622 while( (n%4)!= 0) n=n+sprintf(n+dataStart,"*"); // insert spaces until n is exactly two away from a multiple of four
nixnax 98:3babad0d1bd4 623 if (v2) {
nixnax 98:3babad0d1bd4 624 debug("TCP response %d byes\n",n);
nixnax 98:3babad0d1bd4 625 }
nixnax 98:3babad0d1bd4 626 return n; // total byte size of our response
nixnax 98:3babad0d1bd4 627 }
nixnax 98:3babad0d1bd4 628
nixnax 98:3babad0d1bd4 629
nixnax 98:3babad0d1bd4 630
nixnax 98:3babad0d1bd4 631
nixnax 98:3babad0d1bd4 632
nixnax 67:a63e3486bcda 633
nixnax 29:30de79d658f6 634 void tcpHandler()
nixnax 29:30de79d658f6 635 {
nixnax 87:9f5ac1fabd95 636 // IP header
nixnax 87:9f5ac1fabd95 637
nixnax 26:11f4eb2663a7 638 char * ipPkt = ppp.pkt.buf+4; // ip packet start
nixnax 26:11f4eb2663a7 639 char * ihl = ipPkt; // bottom 4 bits
nixnax 83:cdcb81d1910f 640 char * pktLen = ipPkt+2; // 2 bytes
nixnax 26:11f4eb2663a7 641 char * ident = ipPkt+4; // 2 bytes
nixnax 28:1aa629be05e7 642 char * protocol = ipPkt+9; // 1 byte
nixnax 83:cdcb81d1910f 643 char * headercheck= ipPkt+10; // 2 bytes
nixnax 26:11f4eb2663a7 644 char * srcAdr = ipPkt+12; // 4 bytes
nixnax 26:11f4eb2663a7 645 char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes
nixnax 26:11f4eb2663a7 646 int headerSizeIP = (ihl[0]&0xf)*4;
nixnax 26:11f4eb2663a7 647 int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length
nixnax 26:11f4eb2663a7 648
nixnax 87:9f5ac1fabd95 649 // TCP header
nixnax 87:9f5ac1fabd95 650
nixnax 87:9f5ac1fabd95 651 char * tcp = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet
nixnax 87:9f5ac1fabd95 652 char * srctcp = tcp + 0; // 2 bytes
nixnax 87:9f5ac1fabd95 653 char * dsttcp = tcp + 2; // 2 bytes
nixnax 87:9f5ac1fabd95 654 char * seqtcp = tcp + 4; // 4 bytes
nixnax 87:9f5ac1fabd95 655 char * acktcp = tcp + 8; // 4 bytes
nixnax 87:9f5ac1fabd95 656 char * offset = tcp + 12; // 4 bits
nixnax 87:9f5ac1fabd95 657 char * flagbitstcp = tcp + 12; // 9 bits
nixnax 87:9f5ac1fabd95 658 char * windowsizetcp = tcp + 14; // 2 bytes
nixnax 87:9f5ac1fabd95 659 char * checksumtcp = tcp + 16; // 2 bytes
nixnax 26:11f4eb2663a7 660
nixnax 26:11f4eb2663a7 661 int tcpSize = packetLength - headerSizeIP;
nixnax 35:e7068df4d971 662 int headerSizeTCP = ((offset[0]>>4)&0x0f)*4; // size of tcp header only
nixnax 61:b3c1a04efd0a 663 int protocolIP = protocol[0];
nixnax 29:30de79d658f6 664
nixnax 87:9f5ac1fabd95 665 char * tcpDataIn = tcp + headerSizeTCP; // start of data block after TCP header
nixnax 67:a63e3486bcda 666 int tcpDataSize = tcpSize - headerSizeTCP; // size of data block after TCP header
nixnax 87:9f5ac1fabd95 667 char * tcpDataOut = tcp + 20; // start of outgoing data
nixnax 81:9ede60e9a2c8 668
nixnax 85:53e57ff1cf05 669 unsigned int seq_in = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
nixnax 85:53e57ff1cf05 670 unsigned int ack_in = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
nixnax 85:53e57ff1cf05 671
nixnax 85:53e57ff1cf05 672 unsigned int ack_out = seq_in + tcpDataSize;
nixnax 87:9f5ac1fabd95 673 unsigned int seq_out = ack_in; // use their version of our current sequence number
nixnax 81:9ede60e9a2c8 674
nixnax 28:1aa629be05e7 675
nixnax 29:30de79d658f6 676 #define TCP_FLAG_ACK (1<<4)
nixnax 29:30de79d658f6 677 #define TCP_FLAG_SYN (1<<1)
nixnax 29:30de79d658f6 678 #define TCP_FLAG_PSH (1<<3)
nixnax 29:30de79d658f6 679 #define TCP_FLAG_RST (1<<2)
nixnax 29:30de79d658f6 680 #define TCP_FLAG_FIN (1<<0)
nixnax 28:1aa629be05e7 681
nixnax 87:9f5ac1fabd95 682 // first we shorten the TCP response header to only 20 bytes.
nixnax 87:9f5ac1fabd95 683 // this means we ignore all TCP option requests
nixnax 87:9f5ac1fabd95 684
nixnax 87:9f5ac1fabd95 685 tcpSize = 20; // shorten total TCP packet size to 20 bytes (no data)
nixnax 87:9f5ac1fabd95 686 headerSizeTCP = 20; // shorten outgoing TCP header size 20 bytes
nixnax 87:9f5ac1fabd95 687 offset[0] = (headerSizeTCP/4)<<4; // shorten tcp header size to 20 bytes
nixnax 87:9f5ac1fabd95 688 packetLength = 40; // shorten total packet size to 40 bytes (20 ip + 20 tcp)
nixnax 87:9f5ac1fabd95 689 pktLen[1] = 40; // set total packet size to 40 bytes (20 ip + 20 tcp)
nixnax 87:9f5ac1fabd95 690 pktLen[0] = 0; // set total packet size to 40 bytes (20 ip + 20 tcp)
nixnax 87:9f5ac1fabd95 691
nixnax 35:e7068df4d971 692 int dataLen = 0; // most of our responses will have zero TCP data, only a header
nixnax 38:ab582987926e 693 int flagsOut = TCP_FLAG_ACK; // the default case is an ACK packet
nixnax 87:9f5ac1fabd95 694 int flagsTCP = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; // the tcp flags we received
nixnax 92:cb962b365cce 695
nixnax 97:bdf885e146dc 696 windowsizetcp[0]=2; // tco window size = 700
nixnax 97:bdf885e146dc 697 windowsizetcp[1]=0xbc; // tcp windows size = 700
nixnax 38:ab582987926e 698
nixnax 93:9675adc36882 699 int doFin = 0; // flag to see if we have to send an extra FIN message to shut down the link
nixnax 93:9675adc36882 700
nixnax 69:23f560087c16 701 // A sparse TCP flag interpreter that implements simple TCP connections from a single source
nixnax 69:23f560087c16 702 // Clients are allowed ONE push packet, after which the link is closed with a FIN flag in the ACK packet
nixnax 69:23f560087c16 703 // This strategy allows web browsers, netcat and curl to work ok while keeping the state machine simple
nixnax 77:abf92baebb42 704
nixnax 69:23f560087c16 705 switch ( flagsTCP ) {
nixnax 69:23f560087c16 706 case TCP_FLAG_SYN:
nixnax 87:9f5ac1fabd95 707 flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // something wants to connect - acknowledge it
nixnax 87:9f5ac1fabd95 708 seq_out = seq_in+0x1000U; // create a new sequence number using their sequence as a base
nixnax 85:53e57ff1cf05 709 ack_out++; // for SYN flag we have to increase the sequence by 1
nixnax 69:23f560087c16 710 break;
nixnax 77:abf92baebb42 711 case TCP_FLAG_ACK | TCP_FLAG_PSH:
nixnax 98:3babad0d1bd4 712 if ( strncmp(tcpDataIn, "GET /", 5) == 0) { // check for an http GET command
nixnax 98:3babad0d1bd4 713 flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set the FIN flag to start closing this TCP connection
nixnax 87:9f5ac1fabd95 714 dataLen = httpResponse(tcpDataOut); // send an http response
nixnax 98:3babad0d1bd4 715 } else {
nixnax 98:3babad0d1bd4 716 dataLen = tcpResponse(tcpDataOut,tcpDataSize); // not a web request, just report number of received bytes
nixnax 69:23f560087c16 717 }
nixnax 69:23f560087c16 718 break;
nixnax 98:3babad0d1bd4 719 case TCP_FLAG_FIN | TCP_FLAG_PSH | TCP_FLAG_ACK:
nixnax 69:23f560087c16 720 case TCP_FLAG_FIN | TCP_FLAG_ACK:
nixnax 93:9675adc36882 721 case TCP_FLAG_RST:
nixnax 85:53e57ff1cf05 722 ack_out++; // for FIN flag we always have to increase sequence by 1
nixnax 69:23f560087c16 723 break;
nixnax 98:3babad0d1bd4 724 case TCP_FLAG_FIN:
nixnax 98:3babad0d1bd4 725 flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set the FIN flag to start closing the connection
nixnax 98:3babad0d1bd4 726 break;
nixnax 77:abf92baebb42 727 default:
nixnax 69:23f560087c16 728 return; // ignore remaining packets
nixnax 93:9675adc36882 729 } // switch
nixnax 77:abf92baebb42 730
nixnax 69:23f560087c16 731 // The TCP flag handling is now done
nixnax 95:40af49390daf 732
nixnax 93:9675adc36882 733 // first we swap sourc and destination TCP addresses and insert the new ack and seq numbers
nixnax 95:40af49390daf 734
nixnax 65:23b17c43aa0f 735 char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header
nixnax 60:2b770949c911 736 memcpy(tempHold, srcAdr,4);
nixnax 65:23b17c43aa0f 737 memcpy(srcAdr, dstAdr,4);
nixnax 60:2b770949c911 738 memcpy(dstAdr, tempHold,4); // swap ip address source/dest
nixnax 60:2b770949c911 739
nixnax 60:2b770949c911 740 memcpy(tempHold, srctcp,2);
nixnax 65:23b17c43aa0f 741 memcpy(srctcp, dsttcp,2);
nixnax 60:2b770949c911 742 memcpy(dsttcp, tempHold,2); // swap ip port source/dest
nixnax 95:40af49390daf 743
nixnax 93:9675adc36882 744 sendTCP:
nixnax 83:cdcb81d1910f 745
nixnax 85:53e57ff1cf05 746 acktcp[0]=ack_out>>24;
nixnax 85:53e57ff1cf05 747 acktcp[1]=ack_out>>16;
nixnax 85:53e57ff1cf05 748 acktcp[2]=ack_out>>8;
nixnax 85:53e57ff1cf05 749 acktcp[3]=ack_out>>0; // save ack 32-bit integer
nixnax 38:ab582987926e 750
nixnax 85:53e57ff1cf05 751 seqtcp[0]=seq_out>>24;
nixnax 85:53e57ff1cf05 752 seqtcp[1]=seq_out>>16;
nixnax 85:53e57ff1cf05 753 seqtcp[2]=seq_out>>8;
nixnax 85:53e57ff1cf05 754 seqtcp[3]=seq_out>>0; // save seq 32-bit integer
nixnax 38:ab582987926e 755
nixnax 93:9675adc36882 756 flagbitstcp[1] = flagsOut; // update the TCP flags
nixnax 93:9675adc36882 757
nixnax 93:9675adc36882 758 // increment the ip ident number
nixnax 38:ab582987926e 759
nixnax 93:9675adc36882 760 ppp.ident++; // get next ident number for our packet
nixnax 93:9675adc36882 761 ident[0] = ppp.ident>>8;
nixnax 93:9675adc36882 762 ident[1] = ppp.ident>>0; // insert OUR ident
nixnax 93:9675adc36882 763
nixnax 93:9675adc36882 764 // Now we recalculate all the header sizes, and do the IP and TCP checksums
nixnax 38:ab582987926e 765 int newPacketSize = headerSizeIP + headerSizeTCP + dataLen; // calculate size of the outgoing packet
nixnax 36:2a9b457f8276 766 pktLen[0] = (newPacketSize>>8);
nixnax 36:2a9b457f8276 767 pktLen[1]=newPacketSize; // ip total packet size
nixnax 36:2a9b457f8276 768 ppp.pkt.len = newPacketSize+6; // ppp packet length
nixnax 36:2a9b457f8276 769 tcpSize = headerSizeTCP + dataLen; // tcp packet size
nixnax 36:2a9b457f8276 770
nixnax 38:ab582987926e 771 // the header is all set up, now do the IP and TCP checksums
nixnax 36:2a9b457f8276 772
nixnax 61:b3c1a04efd0a 773 headercheck[0]=0; // IP header checksum
nixnax 61:b3c1a04efd0a 774 headercheck[1]=0; // IP header checksum
nixnax 61:b3c1a04efd0a 775 headerCheckSum(); // calculate the IP header checksum
nixnax 63:9253b0e1b7d8 776
nixnax 64:677b9713a120 777 // 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 778 // 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 779
nixnax 87:9f5ac1fabd95 780 char * pseudoHeader = tcp-12; // mark the start of the TCP pseudo-header
nixnax 67:a63e3486bcda 781 memcpy(tempHold, pseudoHeader, 12); // preserve the 12 bytes of the IP header where the TCP pseudo-Header will be built
nixnax 61:b3c1a04efd0a 782 memcpy( pseudoHeader+0, srcAdr, 8); // IP source and destination addresses from IP header
nixnax 61:b3c1a04efd0a 783 memset( pseudoHeader+8, 0, 1); // reserved, set to zero
nixnax 61:b3c1a04efd0a 784 memset( pseudoHeader+9, protocolIP, 1); // protocol from IP header
nixnax 62:f192926e42f1 785 memset( pseudoHeader+10, tcpSize>>8, 1); // size of IP data (TCP packet size)
nixnax 62:f192926e42f1 786 memset( pseudoHeader+11, tcpSize, 1); // size of IP data (TCP packet size)
nixnax 38:ab582987926e 787
nixnax 61:b3c1a04efd0a 788 // pseudo-header built, now we can calculate TCP checksum
nixnax 63:9253b0e1b7d8 789
nixnax 29:30de79d658f6 790 checksumtcp[0]=0;
nixnax 29:30de79d658f6 791 checksumtcp[1]=0;
nixnax 76:00e208cceb8b 792 unsigned int pseudoHeaderSum=dataCheckSum((unsigned char *)pseudoHeader,tcpSize+12); // calculate the TCP checksum starting at the pseudo-header
nixnax 61:b3c1a04efd0a 793 checksumtcp[0]=pseudoHeaderSum>>8;
nixnax 61:b3c1a04efd0a 794 checksumtcp[1]=pseudoHeaderSum;
nixnax 87:9f5ac1fabd95 795 memcpy( tcp-12, tempHold, 12); // restore the 12 bytes that the pseudo-header overwrote
nixnax 95:40af49390daf 796
nixnax 93:9675adc36882 797 send_pppFrame(); // All preparation complete - send the TCP response
nixnax 60:2b770949c911 798
nixnax 93:9675adc36882 799 if (doFin==1) { // they want to shut down the link, so we have to send another packet to close our side of the link
nixnax 93:9675adc36882 800 doFin=0;
nixnax 93:9675adc36882 801 flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // tell them we are also finished
nixnax 93:9675adc36882 802 goto sendTCP; // send our final packet for this conversation.
nixnax 93:9675adc36882 803 }
nixnax 95:40af49390daf 804
nixnax 83:cdcb81d1910f 805 dumpHeaderIP();
nixnax 95:40af49390daf 806 dumpHeaderTCP();
nixnax 93:9675adc36882 807
nixnax 29:30de79d658f6 808 }
nixnax 26:11f4eb2663a7 809
nixnax 29:30de79d658f6 810 void dumpDataTCP()
nixnax 29:30de79d658f6 811 {
nixnax 26:11f4eb2663a7 812 int ipPktLen = (ppp.pkt.buf[6]<<8)|ppp.pkt.buf[7]; // overall length of ip packet
nixnax 26:11f4eb2663a7 813 int ipHeaderLen = (ppp.pkt.buf[4]&0xf)*4; // length of ip header
nixnax 35:e7068df4d971 814 int headerSizeTCP = ((ppp.pkt.buf[4+ipHeaderLen+12]>>4)&0xf)*4;; // length of tcp header
nixnax 35:e7068df4d971 815 int dataLen = ipPktLen - ipHeaderLen - headerSizeTCP; // data is what's left after the two headers
nixnax 29:30de79d658f6 816 if (v1) {
nixnax 42:4de44be70bfd 817 debug("TCP %d ipHeader %d tcpHeader %d Data %d\n", ipPktLen, ipHeaderLen, headerSizeTCP, dataLen); // 1 for more verbose
nixnax 29:30de79d658f6 818 }
nixnax 29:30de79d658f6 819 if (dataLen > 0) {
nixnax 47:00a5ca075f8f 820 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 821 debug("%s\n",ppp.pkt.buf+4+ipHeaderLen+headerSizeTCP); // show the data
nixnax 29:30de79d658f6 822 }
nixnax 29:30de79d658f6 823 }
nixnax 26:11f4eb2663a7 824
nixnax 29:30de79d658f6 825 void TCPpacket()
nixnax 29:30de79d658f6 826 {
nixnax 83:cdcb81d1910f 827 dumpHeaderIP();
nixnax 81:9ede60e9a2c8 828 dumpHeaderTCP();
nixnax 77:abf92baebb42 829 if (v2) {
nixnax 77:abf92baebb42 830 dumpDataTCP();
nixnax 77:abf92baebb42 831 }
nixnax 26:11f4eb2663a7 832 tcpHandler();
nixnax 11:f58998c24f0b 833 }
nixnax 11:f58998c24f0b 834
nixnax 29:30de79d658f6 835 void otherProtocol()
nixnax 29:30de79d658f6 836 {
nixnax 64:677b9713a120 837 debug("Other IP protocol");
nixnax 29:30de79d658f6 838 }
nixnax 26:11f4eb2663a7 839
nixnax 29:30de79d658f6 840 void IPframe()
nixnax 29:30de79d658f6 841 {
nixnax 10:74f8233f72c0 842 int protocol = ppp.pkt.buf[13];
nixnax 10:74f8233f72c0 843 switch (protocol) {
nixnax 29:30de79d658f6 844 case 1:
nixnax 29:30de79d658f6 845 ICMPpacket();
nixnax 29:30de79d658f6 846 break;
nixnax 29:30de79d658f6 847 case 2:
nixnax 29:30de79d658f6 848 IGMPpacket();
nixnax 29:30de79d658f6 849 break;
nixnax 29:30de79d658f6 850 case 17:
nixnax 29:30de79d658f6 851 UDPpacket();
nixnax 29:30de79d658f6 852 break;
nixnax 29:30de79d658f6 853 case 6:
nixnax 29:30de79d658f6 854 TCPpacket();
nixnax 29:30de79d658f6 855 break;
nixnax 29:30de79d658f6 856 default:
nixnax 29:30de79d658f6 857 otherProtocol();
nixnax 29:30de79d658f6 858 }
nixnax 29:30de79d658f6 859 }
nixnax 9:0992486d4a30 860
nixnax 29:30de79d658f6 861 void LCPconfReq()
nixnax 29:30de79d658f6 862 {
nixnax 64:677b9713a120 863 debug("LCP Config ");
nixnax 9:0992486d4a30 864 if (ppp.pkt.buf[7] != 4) {
nixnax 85:53e57ff1cf05 865 ppp.pkt.buf[4]=4; // allow only "no options" which means Maximum Receive Unit (MRU) is 1500 bytes
nixnax 64:677b9713a120 866 debug("Reject\n");
nixnax 93:9675adc36882 867 send_pppFrame();
nixnax 9:0992486d4a30 868 } else {
nixnax 9:0992486d4a30 869 ppp.pkt.buf[4]=2; // ack zero conf
nixnax 64:677b9713a120 870 debug("Ack\n");
nixnax 93:9675adc36882 871 send_pppFrame();
nixnax 64:677b9713a120 872 debug("LCP Ask\n");
nixnax 11:f58998c24f0b 873 ppp.pkt.buf[4]=1; // request no options
nixnax 93:9675adc36882 874 send_pppFrame();
nixnax 9:0992486d4a30 875 }
nixnax 9:0992486d4a30 876 }
nixnax 9:0992486d4a30 877
nixnax 29:30de79d658f6 878 void LCPconfAck()
nixnax 29:30de79d658f6 879 {
nixnax 64:677b9713a120 880 debug("LCP Ack\n");
nixnax 29:30de79d658f6 881 }
nixnax 9:0992486d4a30 882
nixnax 29:30de79d658f6 883 void LCPend()
nixnax 29:30de79d658f6 884 {
nixnax 29:30de79d658f6 885 ppp.pkt.buf[4]=6;
nixnax 93:9675adc36882 886 send_pppFrame(); // acknowledge
nixnax 81:9ede60e9a2c8 887 ppp.online=0; // start hunting for connect string again
nixnax 81:9ede60e9a2c8 888 pppInitStruct(); // flush the receive buffer
nixnax 81:9ede60e9a2c8 889 debug("LCP End\n");
nixnax 9:0992486d4a30 890 }
nixnax 9:0992486d4a30 891
nixnax 29:30de79d658f6 892 void LCPother()
nixnax 29:30de79d658f6 893 {
nixnax 64:677b9713a120 894 debug("LCP Other\n");
nixnax 95:40af49390daf 895 dumpPPPFrame();
nixnax 29:30de79d658f6 896 }
nixnax 29:30de79d658f6 897
nixnax 29:30de79d658f6 898 void LCPframe()
nixnax 29:30de79d658f6 899 {
nixnax 29:30de79d658f6 900 int code = ppp.pkt.buf[4];
nixnax 29:30de79d658f6 901 switch (code) {
nixnax 29:30de79d658f6 902 case 1:
nixnax 29:30de79d658f6 903 LCPconfReq();
nixnax 29:30de79d658f6 904 break; // config request
nixnax 29:30de79d658f6 905 case 2:
nixnax 29:30de79d658f6 906 LCPconfAck();
nixnax 29:30de79d658f6 907 break; // config ack
nixnax 29:30de79d658f6 908 case 5:
nixnax 29:30de79d658f6 909 LCPend();
nixnax 29:30de79d658f6 910 break; // end connection
nixnax 29:30de79d658f6 911 default:
nixnax 29:30de79d658f6 912 LCPother();
nixnax 29:30de79d658f6 913 }
nixnax 9:0992486d4a30 914 }
nixnax 9:0992486d4a30 915
nixnax 29:30de79d658f6 916 void discardedFrame()
nixnax 29:30de79d658f6 917 {
nixnax 29:30de79d658f6 918 if (v0) {
nixnax 71:965619fedb3a 919 debug("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 29:30de79d658f6 920 }
nixnax 9:0992486d4a30 921 }
nixnax 9:0992486d4a30 922
nixnax 29:30de79d658f6 923 void determinePacketType()
nixnax 29:30de79d658f6 924 {
nixnax 29:30de79d658f6 925 if ( ppp.pkt.buf[0] != 0xff ) {
nixnax 64:677b9713a120 926 debug("byte0 != ff\n");
nixnax 29:30de79d658f6 927 return;
nixnax 29:30de79d658f6 928 }
nixnax 29:30de79d658f6 929 if ( ppp.pkt.buf[1] != 3 ) {
nixnax 64:677b9713a120 930 debug("byte1 != 3\n");
nixnax 29:30de79d658f6 931 return;
nixnax 29:30de79d658f6 932 }
nixnax 29:30de79d658f6 933 if ( ppp.pkt.buf[3] != 0x21 ) {
nixnax 64:677b9713a120 934 debug("byte2 != 21\n");
nixnax 29:30de79d658f6 935 return;
nixnax 29:30de79d658f6 936 }
nixnax 9:0992486d4a30 937 int packetType = ppp.pkt.buf[2];
nixnax 9:0992486d4a30 938 switch (packetType) {
nixnax 29:30de79d658f6 939 case 0xc0:
nixnax 29:30de79d658f6 940 LCPframe();
nixnax 29:30de79d658f6 941 break; // link control
nixnax 29:30de79d658f6 942 case 0x80:
nixnax 29:30de79d658f6 943 IPCPframe();
nixnax 29:30de79d658f6 944 break; // IP control
nixnax 29:30de79d658f6 945 case 0x00:
nixnax 29:30de79d658f6 946 IPframe();
nixnax 29:30de79d658f6 947 break; // IP itself
nixnax 29:30de79d658f6 948 default:
nixnax 29:30de79d658f6 949 discardedFrame();
nixnax 9:0992486d4a30 950 }
nixnax 29:30de79d658f6 951 }
nixnax 9:0992486d4a30 952
nixnax 50:ad4e7c3c88e5 953 void wait_for_HDLC_frame()
nixnax 50:ad4e7c3c88e5 954 {
nixnax 85:53e57ff1cf05 955 while(1) {
nixnax 93:9675adc36882 956 fillbuf(); // handle received characters
nixnax 81:9ede60e9a2c8 957 if ( rxbufNotEmpty() ) {
nixnax 81:9ede60e9a2c8 958 int oldTail = ppp.rx.tail; // remember where the character is located in the buffer
nixnax 81:9ede60e9a2c8 959 int rx = pc_getBuf(); // get the character
nixnax 50:ad4e7c3c88e5 960 if (rx==FRAME_7E) {
nixnax 81:9ede60e9a2c8 961 if (ppp.hdlc.frameFound == 0) { // we are still waiting for a frame start
nixnax 81:9ede60e9a2c8 962 ppp.hdlc.frameFound = 1; // we found our first frame start
nixnax 81:9ede60e9a2c8 963 ppp.hdlc.frameStartIndex=ppp.rx.tail; // remember where the frame character is in the buffer
nixnax 51:a86d56844324 964 } else {
nixnax 81:9ede60e9a2c8 965 // we have previously found a frame start
nixnax 81:9ede60e9a2c8 966 ppp.hdlc.frameEndIndex=oldTail; // mark the frame end character
nixnax 81:9ede60e9a2c8 967 processHDLCFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); // process the frame
nixnax 93:9675adc36882 968 ppp.rx.rtail = ppp.rx.tail;
nixnax 81:9ede60e9a2c8 969 ppp.hdlc.frameStartIndex = ppp.rx.tail; // where next frame will start
nixnax 81:9ede60e9a2c8 970 break;
nixnax 50:ad4e7c3c88e5 971 }
nixnax 50:ad4e7c3c88e5 972 }
nixnax 50:ad4e7c3c88e5 973 }
nixnax 85:53e57ff1cf05 974 }
nixnax 50:ad4e7c3c88e5 975 }
nixnax 50:ad4e7c3c88e5 976
nixnax 29:30de79d658f6 977 void scanForConnectString()
nixnax 29:30de79d658f6 978 {
nixnax 81:9ede60e9a2c8 979 while(ppp.online == 0) {
nixnax 85:53e57ff1cf05 980 fillbuf();
nixnax 81:9ede60e9a2c8 981 // search for Windows Dialup Networking "Direct Connection Between Two Computers" expected connect string
nixnax 81:9ede60e9a2c8 982 char * found1 = strstr( (char *)ppp.rx.buf, "CLIENTCLIENT" );
nixnax 81:9ede60e9a2c8 983 // also search for HDLC frame start character 0x7e
nixnax 81:9ede60e9a2c8 984 void * found2 = memchr( (char *)ppp.rx.buf, 0x7e, RXBUFLEN );
nixnax 81:9ede60e9a2c8 985 if( (found1 != NULL) | (found2 != NULL) ) {
nixnax 81:9ede60e9a2c8 986 if (found1 != NULL) {
nixnax 81:9ede60e9a2c8 987 // respond with Windows Dialup networking expected "Direct Connection Between Two Computers" response string
nixnax 81:9ede60e9a2c8 988 pc.puts("CLIENTSERVER");
nixnax 81:9ede60e9a2c8 989 if (v0) debug("Found connect string \"CLIENTCLIENT\"\n");
nixnax 81:9ede60e9a2c8 990 }
nixnax 81:9ede60e9a2c8 991 if (found2 != NULL) {
nixnax 81:9ede60e9a2c8 992 if (v0) debug("Found HDLC frame start (7E)\n");
nixnax 81:9ede60e9a2c8 993 }
nixnax 71:965619fedb3a 994 ppp.online=1; // we are connected, so stop looking for the string
nixnax 9:0992486d4a30 995 }
nixnax 9:0992486d4a30 996 }
nixnax 81:9ede60e9a2c8 997
nixnax 9:0992486d4a30 998 }
nixnax 9:0992486d4a30 999
nixnax 0:2cf4880c312a 1000 int main()
nixnax 0:2cf4880c312a 1001 {
nixnax 14:c65831c25aaa 1002 pc.baud(115200); // USB virtual serial port
nixnax 41:e58a5a09f411 1003 #ifndef SERIAL_PORT_MONITOR_NO
nixnax 49:2213f9c132b2 1004 xx.baud(115200); // second serial port for debug messages
nixnax 9:0992486d4a30 1005 xx.puts("\x1b[2J\x1b[HReady\n"); // VT100 code for clear screen & home
nixnax 41:e58a5a09f411 1006 #endif
nixnax 9:0992486d4a30 1007 pppInitStruct(); // initialize all the PPP properties
nixnax 81:9ede60e9a2c8 1008
nixnax 0:2cf4880c312a 1009 while(1) {
nixnax 51:a86d56844324 1010 scanForConnectString(); // respond to connect command from windows dial up networking
nixnax 81:9ede60e9a2c8 1011 while(ppp.online) {
nixnax 81:9ede60e9a2c8 1012 wait_for_HDLC_frame();
nixnax 81:9ede60e9a2c8 1013 }
nixnax 7:ab147f5e97ac 1014 }
nixnax 7:ab147f5e97ac 1015 }