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 Jan 29 02:29:46 2017 +0000
Revision:
51:a86d56844324
Parent:
50:ad4e7c3c88e5
Child:
54:13f83621db80
Successful Test of 1 Million Pings

Who changed what in which revision?

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