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