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:
Thu Jan 05 17:25:59 2017 +0000
Revision:
39:b90183d35f1e
Parent:
38:ab582987926e
Child:
41:e58a5a09f411
Comments

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