Small Internet Protocol Stack using a standard serial port.
PPP-Blinky - TCP/IP Networking Over a Serial Port
Note: The source code is at the bottom of this page.
A Windows desktop showing PPP-Blinky in the network connections list.
Describe PPP-Blinky in Three Sentences
PPP-Blinky is a tiny library that enables Internet protocols (IPv4) to any mbed target hardware by using only a serial port.
The code runs on processors with as little as 8k RAM, for example the Nucleo-L053R8 board.
PPP-Blinky uses the industry-standard PPP (Point-to-Point) Protocol and a tiny "stateless" TCP/IP stack.
No Ethernet Port Required
No ethernet port is required - PPP-Blinky uses a serial port to send IP packets to your PC.
PPP-Blinky emulates a standard dial-up modem and therefore connects to Windows, Linux or Adroid machines.
The code runs on most ARM mbed platforms such as the LPC11U24 shown in the picture below:
mbed LPC11u24 acting as a webserver to a Windows laptop.
Webserver
The Webserver and WebSocket functions are ideal for building browser-based GUIs on mbed-enabled hardware.
PPP-Blinky's HTTP webserver works with most web clients such as Internet Explorer, Mozilla Firefox, Google Chrome, Safari, Curl, wget and Lynx as well as Microsoft Powershell Invoke-Webrequest command.
In the image below Firefox web browser displays the main web page embedded into PPP-Blinky's code:
Firefox web browser displays a web page embedded into PPP-Blinky's code
WebSocket Service
WebSocket is the most popular protocol standard for real-time bidirectional TCP/IP communication between clients and servers.
In the image below a small Internet Explorer script has connected to PPP-Blinky's WebSocket Service.
A websocket message was then sent by the browser and was echoed back by the WebSocket, triggering the onmessage event in the script.
The WebSocket service enables bidirectional real-time interaction between PPP-Blinky and any element in the browser DOM via JavaScript.
If you already have PPP-Blinky up and running you can test your WebSocket service using this: http://jsfiddle.net/d26cyuh2/112/embedded/result
Websockets are ideal for building browser-based GUIs for mbed hardware.
Trying PPP-Blinky on your mbed board
You will need an mbed-enabled hardware board: https://developer.mbed.org/platforms/
Establish a serial port connection between your host PC and your mbed board.
The easiest way is to use mbed hardware with a USB serial debug port. I've tried the ST-Micro Nucleo-L476RG, Nucleo-L152RE, Nucleo-F401RE, Nucleo-L432KC, Nucleo-L053R8, mbed-LPC11U24 and mbed-LPC1768 boards and they all work out of the box. Use the mbed online compiler to compile the software for your target board. Save the compiled binary to your hardware.
Before establishing a network connection, you can verify the operation of the code by opening a terminal program such as Tera Term, and setting the baud rate of the COM port on your mbed board to 115200 baud. LED1 should toggle for every two 0x7E (~
) (i.e. tilde) characters you type, as 0x7E is the PPP frame start/end marker. Don't forget to close the port when your'e done testing, or else Windows Dial-up Networking will report that the COM port is in use by another program when you try to connect.
Once you are certain that the serial port and firmware is working, proceed to creating a new network connection on your PC -see below.
Creating a Dial-up Connection in Windows
Setting up Dial-Up Networking (DUN) on your Windows 7 or 8 PC is essentially a two-step process: First, you create a new modem device, because PPP-blinky partially emulates a standard Windows serial port modem device. Second, you create a new Internet connection (in practice, a new network adapter) which is associated with your new "modem".
Step-by-step description of how to configure Windows for PPP-Blinky here:
/users/nixnax/code/PPP-Blinky/wiki/Configuring-Windows-Dial-Up-Networking
There is also a screen on how to set up Linux dial-up networking near the bottom of this page.
Connecting to PPP-Blinky from your PC
Once Windows networking is configured you can establish a dial-up connection to your mbed board over the USB virtual com port.
The IP address you manually assigned to the new dial-up network adapter (172.10.10.1) functions as a gateway to any valid IP address on that subnet. In the screen capture below, I'm sending pings from the Windows 8 command line to my ST-Micro Nucleo-L476RG board over the USB virtual serial Port. I'm also using a second serial port and Tera Term to capture the debug output from a second serial port on the hardware. The optional debug output from the board prints out the IP source and destination address and the first few bytes of the data payload. Note that the source is the adapter IP address, (172.10.10.1 in this case) and the destination is some other address on that subnet - all packets to the subnet are sent to our mbed hardware. For example, you could also ping 172.10.10.123 or, if your PPP-Blinky is running, simply click on this link: http://172.10.10.123
One Million Pings!
In the image below the ICMP ("ping") echo reply service was tested by sending one million pings to ppp-Blinky. This took over two hours.
The ping tool used on the Windows 8 PC was psping.exe from PsTools by Mark Russinovich - http://bit.ly/PingFast
The average reply time for a short ping (1 byte of payload data) was 11 milliseconds at 115200 baud on the $10 Nucleo-L053R8 board - barely enough time for 130 bytes to be sent over the port!
Monitoring PPP-Blinky Packets
The image below is from a Microsoft Network Monitor 3.4 capture session.
Responses from PPP-Blinky are shown in blue.
Frame 2 - Internet Explorer at IP 172.10.10.1 (the Dial-Up Adapter IP) requests a TCP connection by sending an S (SYN) flag.
Frame 3 - PPP-Blinky at IP 172.10.10.2 responds with an ACK in frame 3. One direction of the link is now established.
Frame 4 - The PC acknowledges the SYN sent by PPP-Blinky in frame 3. The TCP link is now fully established.
Frame 5 - The browser "pushes" (P flag is set) an HTTP GET request to PPP-Blinky.
Frame 6 - PPP-Blinky responds with a standard HTTP response "pushes" (P flag set) back a small web page. It also sets the A (ACK) flag to acknowledge the message sent in frame 6.
Frame 7 - The PC acknowledges reception of the HTTP payload.
Frame 8 - The PC starts to shut down the TCP connection by sending a FIN flag.
Frame 9 - PPP-Blinky acknowledges the FIN request - the connection is now closed in one direction. It also sets a FIN flag in the response to request closure of the opposite direction of the connection.
Frame 10 - The PC acknowledges the FIN request. The closing of the TCP connection is now confirmed in both directions.
Debug Output
PPP-Blinky can output handy debug information to an optional second serial port.
The image below shows the debug output (Ident, Source, Destination, TCP Flags) for a complete HTTP conversation.
The PC messages are displayed in black. PPP-Blinky messages are blue.
Notice how PPP-blinky automatically inserts a blank line after each full HTTP conversation.
Creating a Dial-Up Connection in Linux
The screen below shows the required pppd command to connect to PPP-Blinky from a Linux machine. This was much simpler than Windows! The USB serial port of the mbed LPC1768 board registered as /dev/ttyACM0 on my Linux box. Do a websearch on pppd if you want to learn more about pppd, the Linux PPP handler. Near the bottom of the screen below, two webpages are fetched (/ and /y) by using the curl command on the command line. Gnome Webkit and Firefox work fine, too. Also try echo GET / HTTP/1.1 | nc 172.10.10.2 which uses netcat, the "Swiss army knife" of networking tools. PPP-Blinky was also tested with ApacheBench, the Apache server benchmark software. After 100000 fetches, the mean page fetch rate was reported as 6 page fetches per second for a small page.
Caveats
PPP Blinky is an extremely sparse implementation (1.5k lines) of HTTP,WebSocket,TCP, UDP, ICMP, IPCP and LCP over PPP, requiring around 8kB of RAM. The minimum functionality required to establish connectivity is implemented. These are often acceptable tradeoffs for embedded projects as well as a handy tool to learn the practical details of everyday networking implementations.
main.cpp@29:30de79d658f6, 2017-01-04 (annotated)
- Committer:
- nixnax
- Date:
- Wed Jan 04 23:52:46 2017 +0000
- Revision:
- 29:30de79d658f6
- Parent:
- 28:1aa629be05e7
- Child:
- 30:273431bccb02
HTTP now servers a good web page; Added verbosity debug options
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nixnax | 0:2cf4880c312a | 1 | #include "mbed.h" |
nixnax | 0:2cf4880c312a | 2 | |
nixnax | 29:30de79d658f6 | 3 | // Copyright 2016 Nicolas Nackel aka Nixnax. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
nixnax | 12:db0dc91f0231 | 4 | |
nixnax | 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 | 29:30de79d658f6 | 50 | int online; |
nixnax | 26:11f4eb2663a7 | 51 | int ident; |
nixnax | 28:1aa629be05e7 | 52 | int sync; |
nixnax | 28:1aa629be05e7 | 53 | int seq; |
nixnax | 29:30de79d658f6 | 54 | int crc; |
nixnax | 29:30de79d658f6 | 55 | int ledState; |
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 | 9:0992486d4a30 | 60 | int total; |
nixnax | 4:a469050d5b80 | 61 | } rx; // serial port buffer |
nixnax | 4:a469050d5b80 | 62 | struct { |
nixnax | 6:fba4c2e817b8 | 63 | int id; |
nixnax | 4:a469050d5b80 | 64 | int len; |
nixnax | 4:a469050d5b80 | 65 | int crc; |
nixnax | 4:a469050d5b80 | 66 | char * buf; |
nixnax | 4:a469050d5b80 | 67 | } pkt; // ppp buffer |
nixnax | 29:30de79d658f6 | 68 | }; |
nixnax | 29:30de79d658f6 | 69 | |
nixnax | 29:30de79d658f6 | 70 | pppType ppp; // our global - definitely not thread safe |
nixnax | 0:2cf4880c312a | 71 | |
nixnax | 29:30de79d658f6 | 72 | // intitialize our globals |
nixnax | 29:30de79d658f6 | 73 | void pppInitStruct() |
nixnax | 29:30de79d658f6 | 74 | { |
nixnax | 29:30de79d658f6 | 75 | ppp.online=0; |
nixnax | 29:30de79d658f6 | 76 | ppp.rx.buf=rxbuf; |
nixnax | 29:30de79d658f6 | 77 | ppp.rx.tail=0; |
nixnax | 29:30de79d658f6 | 78 | ppp.rx.head=0; |
nixnax | 29:30de79d658f6 | 79 | ppp.rx.total=0; |
nixnax | 29:30de79d658f6 | 80 | ppp.pkt.buf=frbuf; |
nixnax | 29:30de79d658f6 | 81 | ppp.pkt.len=0; |
nixnax | 29:30de79d658f6 | 82 | ppp.ident=0; |
nixnax | 29:30de79d658f6 | 83 | ppp.sync=0; |
nixnax | 29:30de79d658f6 | 84 | ppp.seq=77; |
nixnax | 29:30de79d658f6 | 85 | ppp.ledState=0; |
nixnax | 29:30de79d658f6 | 86 | } |
nixnax | 26:11f4eb2663a7 | 87 | |
nixnax | 29:30de79d658f6 | 88 | void crcReset() |
nixnax | 29:30de79d658f6 | 89 | { |
nixnax | 29:30de79d658f6 | 90 | ppp.crc=0xffff; // crc restart |
nixnax | 29:30de79d658f6 | 91 | } |
nixnax | 4:a469050d5b80 | 92 | |
nixnax | 29:30de79d658f6 | 93 | void crcDo(int x) // cumulative crc |
nixnax | 29:30de79d658f6 | 94 | { |
nixnax | 29:30de79d658f6 | 95 | for (int i=0; i<8; i++) { |
nixnax | 29:30de79d658f6 | 96 | ppp.crc=((ppp.crc&1)^(x&1))?(ppp.crc>>1)^0x8408:ppp.crc>>1; // crc calculator |
nixnax | 29:30de79d658f6 | 97 | x>>=1; |
nixnax | 29:30de79d658f6 | 98 | } |
nixnax | 29:30de79d658f6 | 99 | } |
nixnax | 29:30de79d658f6 | 100 | |
nixnax | 29:30de79d658f6 | 101 | int crcBuf(char * buf, int size) // crc on an entire block of memory |
nixnax | 29:30de79d658f6 | 102 | { |
nixnax | 29:30de79d658f6 | 103 | crcReset(); |
nixnax | 29:30de79d658f6 | 104 | for(int i=0; i<size; i++)crcDo(*buf++); |
nixnax | 29:30de79d658f6 | 105 | return ppp.crc; |
nixnax | 29:30de79d658f6 | 106 | } |
nixnax | 0:2cf4880c312a | 107 | |
nixnax | 0:2cf4880c312a | 108 | void rxHandler() // serial port receive interrupt handler |
nixnax | 0:2cf4880c312a | 109 | { |
nixnax | 17:4918c893d802 | 110 | while ( pc.readable() ) { |
nixnax | 20:5db9b77b38a6 | 111 | int hd = (ppp.rx.head+1)&(BUFLEN-1); // increment/wrap |
nixnax | 20:5db9b77b38a6 | 112 | if ( hd == ppp.rx.tail ) break; // watch for buffer full |
nixnax | 17:4918c893d802 | 113 | ppp.rx.buf[ppp.rx.head]=pc.getc(); // insert in rx buffer |
nixnax | 20:5db9b77b38a6 | 114 | ppp.rx.head = hd; // update head pointer |
nixnax | 15:b0154c910143 | 115 | } |
nixnax | 0:2cf4880c312a | 116 | } |
nixnax | 0:2cf4880c312a | 117 | |
nixnax | 29:30de79d658f6 | 118 | void led1Toggle() |
nixnax | 29:30de79d658f6 | 119 | { |
nixnax | 29:30de79d658f6 | 120 | ppp.ledState = ppp.ledState? 0 : 1; |
nixnax | 29:30de79d658f6 | 121 | led1 = ppp.ledState; |
nixnax | 14:c65831c25aaa | 122 | } |
nixnax | 14:c65831c25aaa | 123 | |
nixnax | 22:00df34cd4d7e | 124 | int rxbufNotEmpty() // check if rx buffer has data |
nixnax | 0:2cf4880c312a | 125 | { |
nixnax | 22:00df34cd4d7e | 126 | __disable_irq(); // critical section start |
nixnax | 29:30de79d658f6 | 127 | int notEmpty = (ppp.rx.head==ppp.rx.tail) ? 0 : 1 ; |
nixnax | 22:00df34cd4d7e | 128 | __enable_irq(); // critical section end |
nixnax | 22:00df34cd4d7e | 129 | return notEmpty; |
nixnax | 0:2cf4880c312a | 130 | } |
nixnax | 0:2cf4880c312a | 131 | |
nixnax | 0:2cf4880c312a | 132 | int pc_getBuf() // get one character from the buffer |
nixnax | 0:2cf4880c312a | 133 | { |
nixnax | 22:00df34cd4d7e | 134 | if ( rxbufNotEmpty() ) { |
nixnax | 4:a469050d5b80 | 135 | int x = ppp.rx.buf[ ppp.rx.tail ]; |
nixnax | 20:5db9b77b38a6 | 136 | __disable_irq(); // critical section start |
nixnax | 29:30de79d658f6 | 137 | ppp.rx.tail=(ppp.rx.tail+1)&(BUFLEN-1); |
nixnax | 20:5db9b77b38a6 | 138 | __enable_irq(); // critical section end |
nixnax | 0:2cf4880c312a | 139 | return x; |
nixnax | 20:5db9b77b38a6 | 140 | } else return -1; |
nixnax | 0:2cf4880c312a | 141 | } |
nixnax | 0:2cf4880c312a | 142 | |
nixnax | 4:a469050d5b80 | 143 | void scanForConnectString(); // scan for connect attempts from pc |
nixnax | 1:9e03798d4367 | 144 | |
nixnax | 29:30de79d658f6 | 145 | void processFrame(int start, int end) // process received frame |
nixnax | 29:30de79d658f6 | 146 | { |
nixnax | 14:c65831c25aaa | 147 | led1Toggle(); // change led1 state when frames are received |
nixnax | 29:30de79d658f6 | 148 | if(start==end) { |
nixnax | 29:30de79d658f6 | 149 | pc.putc(0x7e); |
nixnax | 29:30de79d658f6 | 150 | return; |
nixnax | 29:30de79d658f6 | 151 | } |
nixnax | 9:0992486d4a30 | 152 | crcReset(); |
nixnax | 9:0992486d4a30 | 153 | char * dest = ppp.pkt.buf; |
nixnax | 9:0992486d4a30 | 154 | ppp.pkt.len=0; |
nixnax | 9:0992486d4a30 | 155 | int unstuff=0; |
nixnax | 17:4918c893d802 | 156 | int idx = start; |
nixnax | 17:4918c893d802 | 157 | while(1) { |
nixnax | 9:0992486d4a30 | 158 | if (unstuff==0) { |
nixnax | 29:30de79d658f6 | 159 | if (rxbuf[idx]==0x7d) unstuff=1; |
nixnax | 29:30de79d658f6 | 160 | else { |
nixnax | 29:30de79d658f6 | 161 | *dest = rxbuf[idx]; |
nixnax | 29:30de79d658f6 | 162 | ppp.pkt.len++; |
nixnax | 29:30de79d658f6 | 163 | dest++; |
nixnax | 29:30de79d658f6 | 164 | crcDo(rxbuf[idx]); |
nixnax | 29:30de79d658f6 | 165 | } |
nixnax | 12:db0dc91f0231 | 166 | } else { // unstuff |
nixnax | 29:30de79d658f6 | 167 | *dest = rxbuf[idx]^0x20; |
nixnax | 29:30de79d658f6 | 168 | ppp.pkt.len++; |
nixnax | 29:30de79d658f6 | 169 | dest++; |
nixnax | 29:30de79d658f6 | 170 | crcDo(rxbuf[idx]^0x20); |
nixnax | 9:0992486d4a30 | 171 | unstuff=0; |
nixnax | 9:0992486d4a30 | 172 | } |
nixnax | 17:4918c893d802 | 173 | idx = (idx+1) & (BUFLEN-1); |
nixnax | 17:4918c893d802 | 174 | if (idx == end) break; |
nixnax | 9:0992486d4a30 | 175 | } |
nixnax | 29:30de79d658f6 | 176 | ppp.pkt.crc = ppp.crc & 0xffff; |
nixnax | 9:0992486d4a30 | 177 | if (ppp.pkt.crc == 0xf0b8) { // check for good CRC |
nixnax | 16:cb0b80c24ba2 | 178 | void determinePacketType(); // declaration only |
nixnax | 9:0992486d4a30 | 179 | determinePacketType(); |
nixnax | 29:30de79d658f6 | 180 | } else if (v0) { |
nixnax | 29:30de79d658f6 | 181 | debug(("PPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len)); |
nixnax | 9:0992486d4a30 | 182 | } |
nixnax | 9:0992486d4a30 | 183 | } |
nixnax | 9:0992486d4a30 | 184 | |
nixnax | 29:30de79d658f6 | 185 | // Note - the hex output of dumpframe can be imported into WireShark |
nixnax | 29:30de79d658f6 | 186 | // Copy the frame's hex output from your terminal program and save as a text file |
nixnax | 29:30de79d658f6 | 187 | // In WireShark, use "Import Hex File". Options are No Offset, protocol=PPP. |
nixnax | 29:30de79d658f6 | 188 | void dumpFrame() |
nixnax | 29:30de79d658f6 | 189 | { |
nixnax | 29:30de79d658f6 | 190 | for(int i=0; i<ppp.pkt.len; i++) debug(("%02x ", ppp.pkt.buf[i])); |
nixnax | 16:cb0b80c24ba2 | 191 | debug((" C=%02x %02x L=%d\n", ppp.pkt.crc&0xff, (ppp.pkt.crc>>8)&0xff, ppp.pkt.len)); |
nixnax | 16:cb0b80c24ba2 | 192 | } |
nixnax | 16:cb0b80c24ba2 | 193 | |
nixnax | 29:30de79d658f6 | 194 | void hdlcPut(int ch) // do hdlc handling of special (flag) characters |
nixnax | 29:30de79d658f6 | 195 | { |
nixnax | 29:30de79d658f6 | 196 | if ( (ch<0x20) || (ch==0x7d) || (ch==0x7e) ) { |
nixnax | 29:30de79d658f6 | 197 | pc.putc(0x7d); |
nixnax | 29:30de79d658f6 | 198 | pc.putc(ch^0x20); |
nixnax | 29:30de79d658f6 | 199 | } else { |
nixnax | 29:30de79d658f6 | 200 | pc.putc(ch); |
nixnax | 29:30de79d658f6 | 201 | } |
nixnax | 11:f58998c24f0b | 202 | } |
nixnax | 9:0992486d4a30 | 203 | |
nixnax | 29:30de79d658f6 | 204 | void sendFrame() |
nixnax | 29:30de79d658f6 | 205 | { |
nixnax | 17:4918c893d802 | 206 | int crc = crcBuf(ppp.pkt.buf, ppp.pkt.len-2); // update crc |
nixnax | 12:db0dc91f0231 | 207 | ppp.pkt.buf[ ppp.pkt.len-2 ] = (~crc>>0); // fcs lo (crc) |
nixnax | 12:db0dc91f0231 | 208 | ppp.pkt.buf[ ppp.pkt.len-1 ] = (~crc>>8); // fcs hi (crc) |
nixnax | 16:cb0b80c24ba2 | 209 | pc.putc(0x7e); // hdlc start-of-frame "flag" |
nixnax | 29:30de79d658f6 | 210 | for(int i=0; i<ppp.pkt.len; i++) hdlcPut( ppp.pkt.buf[i] ); |
nixnax | 16:cb0b80c24ba2 | 211 | pc.putc(0x7e); // hdlc end-of-frame "flag" |
nixnax | 9:0992486d4a30 | 212 | } |
nixnax | 9:0992486d4a30 | 213 | |
nixnax | 29:30de79d658f6 | 214 | void ipRequestHandler() |
nixnax | 29:30de79d658f6 | 215 | { |
nixnax | 15:b0154c910143 | 216 | debug(("IPCP Conf ")); |
nixnax | 9:0992486d4a30 | 217 | if ( ppp.pkt.buf[7] != 4 ) { |
nixnax | 15:b0154c910143 | 218 | debug(("Rej\n")); // reject if any options are requested |
nixnax | 9:0992486d4a30 | 219 | ppp.pkt.buf[4]=4; |
nixnax | 9:0992486d4a30 | 220 | sendFrame(); |
nixnax | 9:0992486d4a30 | 221 | } else { |
nixnax | 15:b0154c910143 | 222 | debug(("Ack\n")); |
nixnax | 9:0992486d4a30 | 223 | ppp.pkt.buf[4]=2; // ack the minimum |
nixnax | 9:0992486d4a30 | 224 | sendFrame(); // acknowledge |
nixnax | 29:30de79d658f6 | 225 | debug(("IPCP Ask\n")); |
nixnax | 9:0992486d4a30 | 226 | // send our own request now |
nixnax | 12:db0dc91f0231 | 227 | ppp.pkt.buf[4]=1; // request no options |
nixnax | 9:0992486d4a30 | 228 | ppp.pkt.buf[5]++; // next sequence |
nixnax | 9:0992486d4a30 | 229 | sendFrame(); // this is our request |
nixnax | 9:0992486d4a30 | 230 | } |
nixnax | 9:0992486d4a30 | 231 | } |
nixnax | 9:0992486d4a30 | 232 | |
nixnax | 29:30de79d658f6 | 233 | void ipAckHandler() |
nixnax | 29:30de79d658f6 | 234 | { |
nixnax | 29:30de79d658f6 | 235 | debug(("IPCP Grant\n")); |
nixnax | 29:30de79d658f6 | 236 | } |
nixnax | 9:0992486d4a30 | 237 | |
nixnax | 29:30de79d658f6 | 238 | void ipNackHandler() |
nixnax | 29:30de79d658f6 | 239 | { |
nixnax | 29:30de79d658f6 | 240 | debug(("IPCP Nack\n")); |
nixnax | 29:30de79d658f6 | 241 | } |
nixnax | 9:0992486d4a30 | 242 | |
nixnax | 29:30de79d658f6 | 243 | void ipDefaultHandler() |
nixnax | 29:30de79d658f6 | 244 | { |
nixnax | 29:30de79d658f6 | 245 | debug(("IPCP Other\n")); |
nixnax | 29:30de79d658f6 | 246 | } |
nixnax | 29:30de79d658f6 | 247 | |
nixnax | 29:30de79d658f6 | 248 | void IPCPframe() |
nixnax | 29:30de79d658f6 | 249 | { |
nixnax | 9:0992486d4a30 | 250 | int code = ppp.pkt.buf[4]; // packet type is here |
nixnax | 9:0992486d4a30 | 251 | switch (code) { |
nixnax | 29:30de79d658f6 | 252 | case 1: |
nixnax | 29:30de79d658f6 | 253 | ipRequestHandler(); |
nixnax | 29:30de79d658f6 | 254 | break; |
nixnax | 29:30de79d658f6 | 255 | case 2: |
nixnax | 29:30de79d658f6 | 256 | ipAckHandler(); |
nixnax | 29:30de79d658f6 | 257 | break; |
nixnax | 29:30de79d658f6 | 258 | case 3: |
nixnax | 29:30de79d658f6 | 259 | ipNackHandler(); |
nixnax | 29:30de79d658f6 | 260 | break; |
nixnax | 29:30de79d658f6 | 261 | default: |
nixnax | 29:30de79d658f6 | 262 | ipDefaultHandler(); |
nixnax | 9:0992486d4a30 | 263 | } |
nixnax | 29:30de79d658f6 | 264 | } |
nixnax | 9:0992486d4a30 | 265 | |
nixnax | 29:30de79d658f6 | 266 | void UDPpacket() |
nixnax | 29:30de79d658f6 | 267 | { |
nixnax | 12:db0dc91f0231 | 268 | char * udpPkt = ppp.pkt.buf+4; // udp packet start |
nixnax | 16:cb0b80c24ba2 | 269 | int headerSizeIP = (( udpPkt[0]&0xf)*4); |
nixnax | 29:30de79d658f6 | 270 | char * udpBlock = udpPkt + headerSizeIP; // udp info start |
nixnax | 12:db0dc91f0231 | 271 | char * udpSrc = udpBlock; // source port |
nixnax | 12:db0dc91f0231 | 272 | char * udpDst = udpBlock+2; // destination port |
nixnax | 12:db0dc91f0231 | 273 | char * udpLen = udpBlock+4; // udp data length |
nixnax | 12:db0dc91f0231 | 274 | char * udpInf = udpBlock+8; // actual start of info |
nixnax | 12:db0dc91f0231 | 275 | int srcPort = (udpSrc[0]<<8) | udpSrc[1]; |
nixnax | 12:db0dc91f0231 | 276 | int dstPort = (udpDst[0]<<8) | udpDst[1]; |
nixnax | 12:db0dc91f0231 | 277 | char * srcIP = udpPkt+12; // udp src addr |
nixnax | 12:db0dc91f0231 | 278 | char * dstIP = udpPkt+16; // udp dst addr |
nixnax | 29:30de79d658f6 | 279 | #define UDP_HEADER_SIZE 8 |
nixnax | 12:db0dc91f0231 | 280 | int udpLength = ((udpLen[0]<<8) | udpLen[1]) - UDP_HEADER_SIZE; // size of the actual udp data |
nixnax | 29:30de79d658f6 | 281 | if(v1) debug(("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],srcPort)); |
nixnax | 29:30de79d658f6 | 282 | if(v1) debug(("%d.%d.%d.%d:%d ", dstIP[1],dstIP[1],dstIP[1],dstIP[1],dstPort)); |
nixnax | 15:b0154c910143 | 283 | debug(("Len %d ", udpLength)); |
nixnax | 29:30de79d658f6 | 284 | int printSize = udpLength; |
nixnax | 29:30de79d658f6 | 285 | if (printSize > 20) printSize = 20; // print only first 20 characters |
nixnax | 29:30de79d658f6 | 286 | if (v0) { |
nixnax | 29:30de79d658f6 | 287 | for (int i=0; i<printSize; i++) { |
nixnax | 29:30de79d658f6 | 288 | char ch = udpInf[i]; |
nixnax | 29:30de79d658f6 | 289 | if (ch>31 && ch<127) { |
nixnax | 29:30de79d658f6 | 290 | debug(("%c", ch)); |
nixnax | 29:30de79d658f6 | 291 | } else { |
nixnax | 29:30de79d658f6 | 292 | debug(("_")); |
nixnax | 29:30de79d658f6 | 293 | } |
nixnax | 29:30de79d658f6 | 294 | } |
nixnax | 29:30de79d658f6 | 295 | debug(("\n")); |
nixnax | 29:30de79d658f6 | 296 | } |
nixnax | 12:db0dc91f0231 | 297 | } |
nixnax | 11:f58998c24f0b | 298 | |
nixnax | 29:30de79d658f6 | 299 | int dataCheckSum(char * ptr, int len) |
nixnax | 29:30de79d658f6 | 300 | { |
nixnax | 29:30de79d658f6 | 301 | int sum=0; |
nixnax | 29:30de79d658f6 | 302 | int placeHolder; |
nixnax | 29:30de79d658f6 | 303 | if (len&1) { |
nixnax | 29:30de79d658f6 | 304 | placeHolder = ptr[len-1]; // when length is odd zero stuff |
nixnax | 29:30de79d658f6 | 305 | ptr[len-1]=0; |
nixnax | 29:30de79d658f6 | 306 | } |
nixnax | 29:30de79d658f6 | 307 | for (int i=0; i<len/2; i++) { |
nixnax | 29:30de79d658f6 | 308 | int hi = *ptr; |
nixnax | 29:30de79d658f6 | 309 | ptr++; |
nixnax | 29:30de79d658f6 | 310 | int lo = *ptr; |
nixnax | 29:30de79d658f6 | 311 | ptr++; |
nixnax | 11:f58998c24f0b | 312 | int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 ); |
nixnax | 11:f58998c24f0b | 313 | sum = sum + val; |
nixnax | 11:f58998c24f0b | 314 | } |
nixnax | 11:f58998c24f0b | 315 | sum = sum + (sum>>16); |
nixnax | 29:30de79d658f6 | 316 | if (len&1) { |
nixnax | 29:30de79d658f6 | 317 | ptr[len-1] = placeHolder; // restore the last byte for odd lengths |
nixnax | 29:30de79d658f6 | 318 | } |
nixnax | 12:db0dc91f0231 | 319 | return ~sum; |
nixnax | 29:30de79d658f6 | 320 | } |
nixnax | 11:f58998c24f0b | 321 | |
nixnax | 29:30de79d658f6 | 322 | void headerCheckSum() |
nixnax | 29:30de79d658f6 | 323 | { |
nixnax | 11:f58998c24f0b | 324 | int len =(ppp.pkt.buf[4]&0xf)*4; // length of header in bytes |
nixnax | 11:f58998c24f0b | 325 | char * ptr = ppp.pkt.buf+4; // start of ip packet |
nixnax | 11:f58998c24f0b | 326 | int sum=0; |
nixnax | 11:f58998c24f0b | 327 | |
nixnax | 29:30de79d658f6 | 328 | for (int i=0; i<len/2; i++) { |
nixnax | 29:30de79d658f6 | 329 | int hi = *ptr; |
nixnax | 29:30de79d658f6 | 330 | ptr++; |
nixnax | 29:30de79d658f6 | 331 | int lo = *ptr; |
nixnax | 29:30de79d658f6 | 332 | ptr++; |
nixnax | 11:f58998c24f0b | 333 | int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 ); |
nixnax | 11:f58998c24f0b | 334 | sum = sum + val; |
nixnax | 11:f58998c24f0b | 335 | } |
nixnax | 11:f58998c24f0b | 336 | sum = sum + (sum>>16); |
nixnax | 11:f58998c24f0b | 337 | sum = ~sum; |
nixnax | 11:f58998c24f0b | 338 | ppp.pkt.buf[14]= (sum>>8); |
nixnax | 11:f58998c24f0b | 339 | ppp.pkt.buf[15]= (sum ); |
nixnax | 29:30de79d658f6 | 340 | } |
nixnax | 9:0992486d4a30 | 341 | |
nixnax | 29:30de79d658f6 | 342 | void ICMPpacket() // internet control message protocol |
nixnax | 29:30de79d658f6 | 343 | { |
nixnax | 12:db0dc91f0231 | 344 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 12:db0dc91f0231 | 345 | char * pktLen = ipPkt+2; |
nixnax | 12:db0dc91f0231 | 346 | int packetLength = (pktLen[0]<<8) | pktLen[1]; // icmp packet length |
nixnax | 16:cb0b80c24ba2 | 347 | int headerSizeIP = (( ipPkt[0]&0xf)*4); |
nixnax | 16:cb0b80c24ba2 | 348 | char * icmpType = ipPkt + headerSizeIP; // icmp data start |
nixnax | 13:d882b8a042b4 | 349 | char * icmpSum = icmpType+2; // icmp checksum |
nixnax | 29:30de79d658f6 | 350 | #define ICMP_TYPE_PING_REQUEST 8 |
nixnax | 29:30de79d658f6 | 351 | if ( icmpType[0] == ICMP_TYPE_PING_REQUEST ) { |
nixnax | 12:db0dc91f0231 | 352 | char * ipTTL = ipPkt+8; // time to live |
nixnax | 12:db0dc91f0231 | 353 | ipTTL[0]--; // decrement time to live |
nixnax | 12:db0dc91f0231 | 354 | char * srcAdr = ipPkt+12; |
nixnax | 12:db0dc91f0231 | 355 | char * dstAdr = ipPkt+16; |
nixnax | 18:3e35de1bc877 | 356 | int icmpIdent = (icmpType[4]<<8)|icmpType[5]; |
nixnax | 29:30de79d658f6 | 357 | int icmpSequence = (icmpType[6]<<8)|icmpType[7]; |
nixnax | 25:0b0450e1b08b | 358 | 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 | 359 | debug(("Ident %04x Sequence %04d ",icmpIdent,icmpSequence)); |
nixnax | 29:30de79d658f6 | 360 | char src[4]; |
nixnax | 29:30de79d658f6 | 361 | char dst[4]; |
nixnax | 12:db0dc91f0231 | 362 | memcpy(src, srcAdr,4); |
nixnax | 12:db0dc91f0231 | 363 | memcpy(dst, dstAdr,4); |
nixnax | 12:db0dc91f0231 | 364 | memcpy(srcAdr, dst,4); |
nixnax | 12:db0dc91f0231 | 365 | memcpy(dstAdr, src,4); // swap src & dest ip |
nixnax | 12:db0dc91f0231 | 366 | char * chkSum = ipPkt+10; |
nixnax | 29:30de79d658f6 | 367 | chkSum[0]=0; |
nixnax | 29:30de79d658f6 | 368 | chkSum[1]=0; |
nixnax | 12:db0dc91f0231 | 369 | headerCheckSum(); // new ip header checksum |
nixnax | 29:30de79d658f6 | 370 | #define ICMP_TYPE_ECHO_REPLY 0 |
nixnax | 16:cb0b80c24ba2 | 371 | icmpType[0]=ICMP_TYPE_ECHO_REPLY; // icmp echo reply |
nixnax | 29:30de79d658f6 | 372 | icmpSum[0]=0; |
nixnax | 29:30de79d658f6 | 373 | icmpSum[1]=0; // zero the checksum for recalculation |
nixnax | 16:cb0b80c24ba2 | 374 | int icmpLength = packetLength - headerSizeIP; // length of ICMP data portion |
nixnax | 16:cb0b80c24ba2 | 375 | int sum = dataCheckSum( icmpType, icmpLength); // this checksum on icmp data portion |
nixnax | 29:30de79d658f6 | 376 | icmpSum[0]=sum>>8; |
nixnax | 29:30de79d658f6 | 377 | icmpSum[1]=sum; // new checksum for ICMP data portion |
nixnax | 16:cb0b80c24ba2 | 378 | |
nixnax | 16:cb0b80c24ba2 | 379 | int printSize = icmpLength-8; // exclude size of icmp header |
nixnax | 25:0b0450e1b08b | 380 | char * icmpData = icmpType+8; // the actual payload data is after the header |
nixnax | 25:0b0450e1b08b | 381 | if (printSize > 10) printSize = 10; // print up to 20 characters |
nixnax | 29:30de79d658f6 | 382 | if (v0) { |
nixnax | 29:30de79d658f6 | 383 | for (int i=0; i<printSize; i++) { |
nixnax | 29:30de79d658f6 | 384 | char ch = icmpData[i]; |
nixnax | 29:30de79d658f6 | 385 | if (ch>31 && ch<127) { |
nixnax | 29:30de79d658f6 | 386 | debug(("%c",ch)); |
nixnax | 29:30de79d658f6 | 387 | } else { |
nixnax | 29:30de79d658f6 | 388 | debug(("_")); |
nixnax | 29:30de79d658f6 | 389 | } |
nixnax | 29:30de79d658f6 | 390 | } |
nixnax | 29:30de79d658f6 | 391 | debug(("\n")); |
nixnax | 29:30de79d658f6 | 392 | } |
nixnax | 15:b0154c910143 | 393 | sendFrame(); // reply to the ping |
nixnax | 29:30de79d658f6 | 394 | |
nixnax | 12:db0dc91f0231 | 395 | } else { |
nixnax | 29:30de79d658f6 | 396 | if (v0) { |
nixnax | 29:30de79d658f6 | 397 | debug(("ICMP type=%d \n", icmpType[0])); |
nixnax | 29:30de79d658f6 | 398 | } |
nixnax | 11:f58998c24f0b | 399 | } |
nixnax | 11:f58998c24f0b | 400 | } |
nixnax | 11:f58998c24f0b | 401 | |
nixnax | 29:30de79d658f6 | 402 | void IGMPpacket() // internet group management protocol |
nixnax | 29:30de79d658f6 | 403 | { |
nixnax | 29:30de79d658f6 | 404 | if (v0) { |
nixnax | 29:30de79d658f6 | 405 | debug(("IGMP type=%d \n", ppp.pkt.buf[28])); |
nixnax | 29:30de79d658f6 | 406 | } |
nixnax | 29:30de79d658f6 | 407 | } |
nixnax | 11:f58998c24f0b | 408 | |
nixnax | 29:30de79d658f6 | 409 | void dumpHeaderIP () |
nixnax | 29:30de79d658f6 | 410 | { |
nixnax | 26:11f4eb2663a7 | 411 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 26:11f4eb2663a7 | 412 | char * version = ipPkt; // top 4 bits |
nixnax | 26:11f4eb2663a7 | 413 | char * ihl = ipPkt; // bottom 4 bits |
nixnax | 26:11f4eb2663a7 | 414 | char * dscp = ipPkt+1; // top 6 bits |
nixnax | 26:11f4eb2663a7 | 415 | char * ecn = ipPkt+1; // lower 2 bits |
nixnax | 26:11f4eb2663a7 | 416 | char * pktLen = ipPkt+2; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 417 | char * ident = ipPkt+4; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 418 | char * flags = ipPkt+6; // 2 bits |
nixnax | 26:11f4eb2663a7 | 419 | char * ttl = ipPkt+8; // 1 byte |
nixnax | 26:11f4eb2663a7 | 420 | char * protocol = ipPkt+9; // 1 byte |
nixnax | 29:30de79d658f6 | 421 | char * headercheck= ipPkt+10; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 422 | char * srcAdr = ipPkt+12; // 4 bytes |
nixnax | 26:11f4eb2663a7 | 423 | char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes |
nixnax | 29:30de79d658f6 | 424 | |
nixnax | 29:30de79d658f6 | 425 | int versionIP = (version[0]>>4)&0xf; |
nixnax | 26:11f4eb2663a7 | 426 | int headerSizeIP = (ihl[0]&0xf)*4; |
nixnax | 26:11f4eb2663a7 | 427 | int dscpIP = (dscp[0]>>2)&0x3f; |
nixnax | 26:11f4eb2663a7 | 428 | int ecnIP = ecn[0]&3; |
nixnax | 26:11f4eb2663a7 | 429 | int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length |
nixnax | 26:11f4eb2663a7 | 430 | int identIP = (ident[0]<<8)|ident[1]; |
nixnax | 26:11f4eb2663a7 | 431 | int flagsIP = flags[0]>>14&3; |
nixnax | 26:11f4eb2663a7 | 432 | int ttlIP = ttl[0]; |
nixnax | 26:11f4eb2663a7 | 433 | int protocolIP = protocol[0]; |
nixnax | 26:11f4eb2663a7 | 434 | int checksumIP = (headercheck[0]<<8)|headercheck[1]; |
nixnax | 29:30de79d658f6 | 435 | char srcIP [16]; |
nixnax | 29:30de79d658f6 | 436 | snprintf(srcIP,16, "%d.%d.%d.%d", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3]); |
nixnax | 29:30de79d658f6 | 437 | char dstIP [16]; |
nixnax | 29:30de79d658f6 | 438 | snprintf(dstIP,16, "%d.%d.%d.%d", dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]); |
nixnax | 26:11f4eb2663a7 | 439 | debug(("IP %s %s v%d h%d d%d e%d L%d ",srcIP,dstIP,versionIP,headerSizeIP,dscpIP,ecnIP,packetLength)); |
nixnax | 29:30de79d658f6 | 440 | if (v1) debug(("i%04x f%d t%d p%d C%04x\n",identIP,flagsIP,ttlIP,protocolIP,checksumIP)); |
nixnax | 29:30de79d658f6 | 441 | } |
nixnax | 26:11f4eb2663a7 | 442 | |
nixnax | 29:30de79d658f6 | 443 | void dumpHeaderTCP() |
nixnax | 29:30de79d658f6 | 444 | { |
nixnax | 29:30de79d658f6 | 445 | int headerSizeIP = (ppp.pkt.buf[4]&0xf)*4; // header size of ip portion |
nixnax | 29:30de79d658f6 | 446 | char * tcpStart = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet |
nixnax | 29:30de79d658f6 | 447 | char * seqtcp = tcpStart + 4; // 4 bytes |
nixnax | 29:30de79d658f6 | 448 | char * acktcp = tcpStart + 8; // 4 bytes |
nixnax | 29:30de79d658f6 | 449 | char * flagbitstcp = tcpStart + 12; // 9 bits |
nixnax | 26:11f4eb2663a7 | 450 | int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]); |
nixnax | 26:11f4eb2663a7 | 451 | int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]); |
nixnax | 26:11f4eb2663a7 | 452 | int flags = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; |
nixnax | 10:74f8233f72c0 | 453 | |
nixnax | 29:30de79d658f6 | 454 | char flagInfo[10]; |
nixnax | 29:30de79d658f6 | 455 | memset(flagInfo,'.',10); // text presentation of TCP flags |
nixnax | 29:30de79d658f6 | 456 | if (flags & (1<<0)) flagInfo[0]='F'; |
nixnax | 29:30de79d658f6 | 457 | if (flags & (1<<1)) flagInfo[1]='S'; |
nixnax | 29:30de79d658f6 | 458 | if (flags & (1<<2)) flagInfo[2]='R'; |
nixnax | 29:30de79d658f6 | 459 | if (flags & (1<<3)) flagInfo[3]='P'; |
nixnax | 29:30de79d658f6 | 460 | if (flags & (1<<4)) flagInfo[4]='A'; |
nixnax | 29:30de79d658f6 | 461 | if (flags & (1<<5)) flagInfo[5]='U'; |
nixnax | 29:30de79d658f6 | 462 | if (flags & (1<<6)) flagInfo[6]='E'; |
nixnax | 29:30de79d658f6 | 463 | if (flags & (1<<7)) flagInfo[7]='C'; |
nixnax | 29:30de79d658f6 | 464 | if (flags & (1<<8)) flagInfo[8]='N'; |
nixnax | 29:30de79d658f6 | 465 | flagInfo[9]=0; // null terminate string |
nixnax | 29:30de79d658f6 | 466 | if (v0) { |
nixnax | 29:30de79d658f6 | 467 | debug(("Flags %s Seq %d Ack %d", flagInfo, seq, ack)); |
nixnax | 29:30de79d658f6 | 468 | } |
nixnax | 29:30de79d658f6 | 469 | } |
nixnax | 29:30de79d658f6 | 470 | |
nixnax | 29:30de79d658f6 | 471 | void tcpHandler() |
nixnax | 29:30de79d658f6 | 472 | { |
nixnax | 26:11f4eb2663a7 | 473 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 29:30de79d658f6 | 474 | char * headercheck= ipPkt+10; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 475 | char * ihl = ipPkt; // bottom 4 bits |
nixnax | 26:11f4eb2663a7 | 476 | char * ident = ipPkt+4; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 477 | char * pktLen = ipPkt+2; // 2 bytes |
nixnax | 28:1aa629be05e7 | 478 | char * protocol = ipPkt+9; // 1 byte |
nixnax | 26:11f4eb2663a7 | 479 | char * srcAdr = ipPkt+12; // 4 bytes |
nixnax | 26:11f4eb2663a7 | 480 | char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes |
nixnax | 26:11f4eb2663a7 | 481 | int headerSizeIP = (ihl[0]&0xf)*4; |
nixnax | 26:11f4eb2663a7 | 482 | int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length |
nixnax | 26:11f4eb2663a7 | 483 | |
nixnax | 29:30de79d658f6 | 484 | ident[0] = ppp.ident>>8; |
nixnax | 29:30de79d658f6 | 485 | ident[1] = ppp.ident>>0; // insert OUR ident |
nixnax | 29:30de79d658f6 | 486 | |
nixnax | 29:30de79d658f6 | 487 | char * s = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet |
nixnax | 26:11f4eb2663a7 | 488 | char * srctcp = s + 0; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 489 | char * dsttcp = s + 2; // 2 bytes |
nixnax | 28:1aa629be05e7 | 490 | char * seqtcp = s + 4; // 4 bytes |
nixnax | 28:1aa629be05e7 | 491 | char * acktcp = s + 8; // 4 bytes |
nixnax | 28:1aa629be05e7 | 492 | char * offset = s + 12; // 4 bits |
nixnax | 26:11f4eb2663a7 | 493 | char * flagbitstcp = s + 12; // 9 bits |
nixnax | 26:11f4eb2663a7 | 494 | char * checksumtcp = s + 16; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 495 | |
nixnax | 26:11f4eb2663a7 | 496 | int tcpSize = packetLength - headerSizeIP; |
nixnax | 28:1aa629be05e7 | 497 | int tcpHeaderLen = ((offset[0]>>4)&0x0f)*4; // size of tcp header only |
nixnax | 28:1aa629be05e7 | 498 | int dataLen = tcpSize - tcpHeaderLen; // data is what's left after the header |
nixnax | 29:30de79d658f6 | 499 | |
nixnax | 28:1aa629be05e7 | 500 | int seq = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]); |
nixnax | 28:1aa629be05e7 | 501 | int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]); |
nixnax | 29:30de79d658f6 | 502 | |
nixnax | 28:1aa629be05e7 | 503 | char * dataStart = s + tcpHeaderLen; // start of data |
nixnax | 29:30de79d658f6 | 504 | |
nixnax | 26:11f4eb2663a7 | 505 | int flagsTCP = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; |
nixnax | 28:1aa629be05e7 | 506 | |
nixnax | 29:30de79d658f6 | 507 | #define TCP_FLAG_ACK (1<<4) |
nixnax | 29:30de79d658f6 | 508 | #define TCP_FLAG_SYN (1<<1) |
nixnax | 29:30de79d658f6 | 509 | #define TCP_FLAG_PSH (1<<3) |
nixnax | 29:30de79d658f6 | 510 | #define TCP_FLAG_RST (1<<2) |
nixnax | 29:30de79d658f6 | 511 | #define TCP_FLAG_FIN (1<<0) |
nixnax | 28:1aa629be05e7 | 512 | |
nixnax | 29:30de79d658f6 | 513 | // A sparse TCP flag interpreter that implements basic TCP connections from a single source |
nixnax | 29:30de79d658f6 | 514 | |
nixnax | 29:30de79d658f6 | 515 | int incomingLen = dataLen; // remember length of incoming packet |
nixnax | 29:30de79d658f6 | 516 | dataLen = 0; // most of our responses will have zero TCP data, only a header |
nixnax | 28:1aa629be05e7 | 517 | |
nixnax | 28:1aa629be05e7 | 518 | if ( ((flagsTCP & ~TCP_FLAG_ACK) == 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) { |
nixnax | 29:30de79d658f6 | 519 | if (incomingLen > 0) { // they sent data in the ack |
nixnax | 29:30de79d658f6 | 520 | ack = seq + incomingLen; // acknowledge the number of bytes they sent |
nixnax | 29:30de79d658f6 | 521 | seq = ppp.seq; // confirm our current sequence position |
nixnax | 29:30de79d658f6 | 522 | } else { |
nixnax | 29:30de79d658f6 | 523 | if (ack <= ppp.seq) return; // their idea of our sequence is larger |
nixnax | 29:30de79d658f6 | 524 | int temp = ack; // remember what they thought |
nixnax | 29:30de79d658f6 | 525 | ack = seq; // ack their sequence |
nixnax | 29:30de79d658f6 | 526 | ppp.seq = temp; // adopt their calculation of our sequence |
nixnax | 29:30de79d658f6 | 527 | seq = ppp.seq; |
nixnax | 29:30de79d658f6 | 528 | } |
nixnax | 29:30de79d658f6 | 529 | } else if ( (flagsTCP & TCP_FLAG_FIN) != 0 ) { // got FIN |
nixnax | 29:30de79d658f6 | 530 | flagbitstcp[1] |= TCP_FLAG_ACK; // do a fin-ack |
nixnax | 28:1aa629be05e7 | 531 | ack = seq; |
nixnax | 28:1aa629be05e7 | 532 | seq = ppp.seq; |
nixnax | 29:30de79d658f6 | 533 | } else if ( (flagsTCP & TCP_FLAG_SYN) != 0 ) { // got SYN |
nixnax | 28:1aa629be05e7 | 534 | flagbitstcp[1] |= TCP_FLAG_ACK; // do a syn-ack |
nixnax | 29:30de79d658f6 | 535 | ack = seq + 1; // when you see SYN you increment their sequence |
nixnax | 29:30de79d658f6 | 536 | seq = ppp.seq-1; // we cheat - our sequence will be one higher hereafter |
nixnax | 29:30de79d658f6 | 537 | } else if ( (flagsTCP & TCP_FLAG_PSH) != 0 ) { // they are pushing data |
nixnax | 29:30de79d658f6 | 538 | |
nixnax | 29:30de79d658f6 | 539 | // Ok, we are done with figuring out the flags. Now we start preparing to respond |
nixnax | 29:30de79d658f6 | 540 | |
nixnax | 29:30de79d658f6 | 541 | //flagbitstcp[1] = TCP_FLAG_ACK; // if we hare here we have to ack |
nixnax | 29:30de79d658f6 | 542 | int temp = ack; |
nixnax | 29:30de79d658f6 | 543 | ack = seq + incomingLen; // acknowledge the number of bytes they sent |
nixnax | 29:30de79d658f6 | 544 | ppp.seq = temp; // adopt their idea of our sequence |
nixnax | 29:30de79d658f6 | 545 | seq = ppp.seq; |
nixnax | 29:30de79d658f6 | 546 | // let's check incoming text for an HTTP home page GET request |
nixnax | 29:30de79d658f6 | 547 | if ( strncmp(dataStart, "GET / HTTP/1.1", 14) == 0) { |
nixnax | 29:30de79d658f6 | 548 | dataLen = 15*32; // this block has to hold the web page below, but keep it under 1k |
nixnax | 29:30de79d658f6 | 549 | memset(dataStart,'x', dataLen ); // initialize the block |
nixnax | 29:30de79d658f6 | 550 | int n=0; // number of bytes we have printed so far |
nixnax | 29:30de79d658f6 | 551 | n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: PPP-Blinky\r\n"); // http header |
nixnax | 29:30de79d658f6 | 552 | n=n+sprintf(n+dataStart,"Content-Length: 378\r\n"); // http header |
nixnax | 29:30de79d658f6 | 553 | n=n+sprintf(n+dataStart,"Content-Type: text/html;charset=us-ascii\r\n\r\n"); // http header |
nixnax | 29:30de79d658f6 | 554 | int nHeader=n; // byte total of all headers |
nixnax | 29:30de79d658f6 | 555 | n=n+sprintf(n+dataStart,"<html><head><title>mbed-PPP-Blinky</title><script>window.onload=function()"); // html |
nixnax | 29:30de79d658f6 | 556 | n=n+sprintf(n+dataStart,"{setInterval(function(){function x(){return document.getElementById('w');};"); // html |
nixnax | 29:30de79d658f6 | 557 | n=n+sprintf(n+dataStart,"x().innerText = parseInt(x().innerText)+1;},100);};</script></head><body>"); // html |
nixnax | 29:30de79d658f6 | 558 | n=n+sprintf(n+dataStart,"<h1>mbed-PPP-Blinky Up and Running</h1><h1 id=\"w\" style=\"text-align:"); // html |
nixnax | 29:30de79d658f6 | 559 | n=n+sprintf(n+dataStart," center\";>0</h1><h1><a href=\"http://bit.ly/pppBlinky\">Source on mbed</h1></body></html>"); // html |
nixnax | 29:30de79d658f6 | 560 | int contentLength = dataLen-nHeader; // this is how to calculate Content-Length, but using curl -v is easier |
nixnax | 29:30de79d658f6 | 561 | contentLength = contentLength+0; // get around unreferenced variable warning |
nixnax | 29:30de79d658f6 | 562 | if (v0) { |
nixnax | 29:30de79d658f6 | 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-1)); |
nixnax | 29:30de79d658f6 | 564 | } |
nixnax | 28:1aa629be05e7 | 565 | } |
nixnax | 29:30de79d658f6 | 566 | } |
nixnax | 29:30de79d658f6 | 567 | |
nixnax | 29:30de79d658f6 | 568 | // now we have to recalculate all the header sizes |
nixnax | 29:30de79d658f6 | 569 | |
nixnax | 28:1aa629be05e7 | 570 | int newPacketSize = headerSizeIP + tcpHeaderLen + dataLen; |
nixnax | 29:30de79d658f6 | 571 | pktLen[0] = (newPacketSize>>8); |
nixnax | 29:30de79d658f6 | 572 | pktLen[1]=newPacketSize; // ip total packet size |
nixnax | 28:1aa629be05e7 | 573 | ppp.pkt.len = newPacketSize+6; // ppp packet length |
nixnax | 28:1aa629be05e7 | 574 | tcpSize = tcpHeaderLen + dataLen; // tcp packet size |
nixnax | 29:30de79d658f6 | 575 | |
nixnax | 28:1aa629be05e7 | 576 | // redo all the header stuff |
nixnax | 29:30de79d658f6 | 577 | |
nixnax | 29:30de79d658f6 | 578 | acktcp[0]=ack>>24; |
nixnax | 29:30de79d658f6 | 579 | acktcp[1]=ack>>16; |
nixnax | 29:30de79d658f6 | 580 | acktcp[2]=ack>>8; |
nixnax | 29:30de79d658f6 | 581 | acktcp[3]=ack>>0; // save ack |
nixnax | 29:30de79d658f6 | 582 | seqtcp[0]=seq>>24; |
nixnax | 29:30de79d658f6 | 583 | seqtcp[1]=seq>>16; |
nixnax | 29:30de79d658f6 | 584 | seqtcp[2]=seq>>8; |
nixnax | 29:30de79d658f6 | 585 | seqtcp[3]=seq>>0; // save seq |
nixnax | 26:11f4eb2663a7 | 586 | |
nixnax | 29:30de79d658f6 | 587 | char src[4]; |
nixnax | 29:30de79d658f6 | 588 | char dst[4]; |
nixnax | 29:30de79d658f6 | 589 | memcpy(src, srcAdr,4); |
nixnax | 29:30de79d658f6 | 590 | memcpy(dst, dstAdr,4); |
nixnax | 29:30de79d658f6 | 591 | memcpy(srcAdr, dst,4); |
nixnax | 29:30de79d658f6 | 592 | memcpy(dstAdr, src,4); // swap ip address source/dest |
nixnax | 29:30de79d658f6 | 593 | char psrc[2]; |
nixnax | 29:30de79d658f6 | 594 | char pdst[2]; |
nixnax | 29:30de79d658f6 | 595 | memcpy(psrc, srctcp,2); |
nixnax | 29:30de79d658f6 | 596 | memcpy(pdst, dsttcp,2); |
nixnax | 29:30de79d658f6 | 597 | memcpy(srctcp, pdst,2); |
nixnax | 29:30de79d658f6 | 598 | memcpy(dsttcp, psrc,2); // swap ip port source/dest |
nixnax | 29:30de79d658f6 | 599 | |
nixnax | 29:30de79d658f6 | 600 | headercheck[0]=0; |
nixnax | 29:30de79d658f6 | 601 | headercheck[1]=0; |
nixnax | 29:30de79d658f6 | 602 | headerCheckSum(); // redo the ip header checksum |
nixnax | 29:30de79d658f6 | 603 | char pseudoHeader[12]; |
nixnax | 29:30de79d658f6 | 604 | int sum; |
nixnax | 29:30de79d658f6 | 605 | char temp[12]; // for the terrible pseudoheader checksum |
nixnax | 26:11f4eb2663a7 | 606 | memcpy( pseudoHeader+0, srcAdr, 8); // source and destination addresses. |
nixnax | 29:30de79d658f6 | 607 | pseudoHeader[8]=0; |
nixnax | 29:30de79d658f6 | 608 | pseudoHeader[9]=protocol[0]; |
nixnax | 29:30de79d658f6 | 609 | pseudoHeader[10]=tcpSize>>8; |
nixnax | 29:30de79d658f6 | 610 | pseudoHeader[11]=tcpSize; |
nixnax | 26:11f4eb2663a7 | 611 | memcpy(temp, s-12, 12); // keep a copy |
nixnax | 26:11f4eb2663a7 | 612 | memcpy( s-12, pseudoHeader, 12); // put the header on the tcp packet |
nixnax | 29:30de79d658f6 | 613 | checksumtcp[0]=0; |
nixnax | 29:30de79d658f6 | 614 | checksumtcp[1]=0; |
nixnax | 29:30de79d658f6 | 615 | sum=dataCheckSum(s-12,tcpSize+12); // update TCP checksum |
nixnax | 29:30de79d658f6 | 616 | checksumtcp[0]=sum>>8; |
nixnax | 29:30de79d658f6 | 617 | checksumtcp[1]=sum; |
nixnax | 26:11f4eb2663a7 | 618 | memcpy( s-12, temp, 12); // overwrite the pseudoheader |
nixnax | 26:11f4eb2663a7 | 619 | sendFrame(); // return the TCP packet |
nixnax | 29:30de79d658f6 | 620 | } |
nixnax | 26:11f4eb2663a7 | 621 | |
nixnax | 29:30de79d658f6 | 622 | void dumpDataTCP() |
nixnax | 29:30de79d658f6 | 623 | { |
nixnax | 26:11f4eb2663a7 | 624 | int ipPktLen = (ppp.pkt.buf[6]<<8)|ppp.pkt.buf[7]; // overall length of ip packet |
nixnax | 26:11f4eb2663a7 | 625 | int ipHeaderLen = (ppp.pkt.buf[4]&0xf)*4; // length of ip header |
nixnax | 26:11f4eb2663a7 | 626 | int tcpHeaderLen = ((ppp.pkt.buf[4+ipHeaderLen+12]>>4)&0xf)*4;; // length of tcp header |
nixnax | 26:11f4eb2663a7 | 627 | int dataLen = ipPktLen - ipHeaderLen - tcpHeaderLen; // data is what's left after the two headers |
nixnax | 29:30de79d658f6 | 628 | if (v1) { |
nixnax | 29:30de79d658f6 | 629 | debug(("TCP %d ipHeader %d tcpHeader %d Data %d\n", ipPktLen, ipHeaderLen, tcpHeaderLen, dataLen)); // 1 for more verbose |
nixnax | 29:30de79d658f6 | 630 | } |
nixnax | 29:30de79d658f6 | 631 | if (dataLen > 0) { |
nixnax | 29:30de79d658f6 | 632 | debug(("%s\n",ppp.pkt.buf+4+ipHeaderLen+tcpHeaderLen)); // show the data |
nixnax | 29:30de79d658f6 | 633 | } |
nixnax | 29:30de79d658f6 | 634 | } |
nixnax | 26:11f4eb2663a7 | 635 | |
nixnax | 29:30de79d658f6 | 636 | void TCPpacket() |
nixnax | 29:30de79d658f6 | 637 | { |
nixnax | 26:11f4eb2663a7 | 638 | char * ipPkt = ppp.pkt.buf+4; // ip packet start |
nixnax | 26:11f4eb2663a7 | 639 | char * version = ipPkt; // top 4 bits |
nixnax | 26:11f4eb2663a7 | 640 | char * ihl = ipPkt; // bottom 4 bits |
nixnax | 26:11f4eb2663a7 | 641 | char * dscp = ipPkt+1; // top 6 bits |
nixnax | 26:11f4eb2663a7 | 642 | char * ecn = ipPkt+1; // lower 2 bits |
nixnax | 26:11f4eb2663a7 | 643 | char * pktLen = ipPkt+2; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 644 | char * ident = ipPkt+4; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 645 | char * flags = ipPkt+6; // 2 bits |
nixnax | 26:11f4eb2663a7 | 646 | char * ttl = ipPkt+8; // 1 byte |
nixnax | 26:11f4eb2663a7 | 647 | char * protocol = ipPkt+9; // 1 byte |
nixnax | 29:30de79d658f6 | 648 | char * headercheck= ipPkt+10; // 2 bytes |
nixnax | 26:11f4eb2663a7 | 649 | char * srcAdr = ipPkt+12; // 4 bytes |
nixnax | 26:11f4eb2663a7 | 650 | char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes |
nixnax | 29:30de79d658f6 | 651 | |
nixnax | 29:30de79d658f6 | 652 | int versionIP = (version[0]>>4)&0xf; |
nixnax | 26:11f4eb2663a7 | 653 | int headerSizeIP = (ihl[0]&0xf)*4; |
nixnax | 26:11f4eb2663a7 | 654 | int dscpIP = (dscp[0]>>2)&0x3f; |
nixnax | 26:11f4eb2663a7 | 655 | int ecnIP = ecn[0]&3; |
nixnax | 26:11f4eb2663a7 | 656 | int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length |
nixnax | 26:11f4eb2663a7 | 657 | int identIP = (ident[0]<<8)|ident[1]; |
nixnax | 26:11f4eb2663a7 | 658 | int flagsIP = flags[0]>>14&3; |
nixnax | 26:11f4eb2663a7 | 659 | int ttlIP = ttl[0]; |
nixnax | 26:11f4eb2663a7 | 660 | int protocolIP = protocol[0]; |
nixnax | 26:11f4eb2663a7 | 661 | int checksumIP = (headercheck[0]<<8)|headercheck[1]; |
nixnax | 29:30de79d658f6 | 662 | char srcIP [16]; |
nixnax | 29:30de79d658f6 | 663 | snprintf(srcIP,16, "%d.%d.%d.%d", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3]); |
nixnax | 29:30de79d658f6 | 664 | char dstIP [16]; |
nixnax | 29:30de79d658f6 | 665 | snprintf(dstIP,16, "%d.%d.%d.%d", dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3]); |
nixnax | 29:30de79d658f6 | 666 | if (v0) { |
nixnax | 29:30de79d658f6 | 667 | debug(("IP %s %s v%d h%d d%d e%d L%d ",srcIP,dstIP,versionIP,headerSizeIP,dscpIP,ecnIP,packetLength)); |
nixnax | 29:30de79d658f6 | 668 | } |
nixnax | 29:30de79d658f6 | 669 | if (v0) { |
nixnax | 29:30de79d658f6 | 670 | debug(("i%04x f%d t%d p%d C%04x\n",identIP,flagsIP,ttlIP,protocolIP,checksumIP)); |
nixnax | 29:30de79d658f6 | 671 | } |
nixnax | 28:1aa629be05e7 | 672 | dumpHeaderTCP(); |
nixnax | 26:11f4eb2663a7 | 673 | dumpDataTCP(); |
nixnax | 26:11f4eb2663a7 | 674 | tcpHandler(); |
nixnax | 11:f58998c24f0b | 675 | } |
nixnax | 11:f58998c24f0b | 676 | |
nixnax | 29:30de79d658f6 | 677 | void otherProtocol() |
nixnax | 29:30de79d658f6 | 678 | { |
nixnax | 29:30de79d658f6 | 679 | debug(("Other IP protocol")); |
nixnax | 29:30de79d658f6 | 680 | } |
nixnax | 26:11f4eb2663a7 | 681 | |
nixnax | 29:30de79d658f6 | 682 | void IPframe() |
nixnax | 29:30de79d658f6 | 683 | { |
nixnax | 10:74f8233f72c0 | 684 | int protocol = ppp.pkt.buf[13]; |
nixnax | 10:74f8233f72c0 | 685 | switch (protocol) { |
nixnax | 29:30de79d658f6 | 686 | case 1: |
nixnax | 29:30de79d658f6 | 687 | ICMPpacket(); |
nixnax | 29:30de79d658f6 | 688 | break; |
nixnax | 29:30de79d658f6 | 689 | case 2: |
nixnax | 29:30de79d658f6 | 690 | IGMPpacket(); |
nixnax | 29:30de79d658f6 | 691 | break; |
nixnax | 29:30de79d658f6 | 692 | case 17: |
nixnax | 29:30de79d658f6 | 693 | UDPpacket(); |
nixnax | 29:30de79d658f6 | 694 | break; |
nixnax | 29:30de79d658f6 | 695 | case 6: |
nixnax | 29:30de79d658f6 | 696 | TCPpacket(); |
nixnax | 29:30de79d658f6 | 697 | break; |
nixnax | 29:30de79d658f6 | 698 | default: |
nixnax | 29:30de79d658f6 | 699 | otherProtocol(); |
nixnax | 29:30de79d658f6 | 700 | } |
nixnax | 29:30de79d658f6 | 701 | } |
nixnax | 9:0992486d4a30 | 702 | |
nixnax | 29:30de79d658f6 | 703 | void LCPconfReq() |
nixnax | 29:30de79d658f6 | 704 | { |
nixnax | 15:b0154c910143 | 705 | debug(("LCP Config ")); |
nixnax | 9:0992486d4a30 | 706 | if (ppp.pkt.buf[7] != 4) { |
nixnax | 11:f58998c24f0b | 707 | ppp.pkt.buf[4]=4; // allow only no options |
nixnax | 15:b0154c910143 | 708 | debug(("Reject\n")); |
nixnax | 29:30de79d658f6 | 709 | sendFrame(); |
nixnax | 9:0992486d4a30 | 710 | } else { |
nixnax | 9:0992486d4a30 | 711 | ppp.pkt.buf[4]=2; // ack zero conf |
nixnax | 15:b0154c910143 | 712 | debug(("Ack\n")); |
nixnax | 9:0992486d4a30 | 713 | sendFrame(); |
nixnax | 15:b0154c910143 | 714 | debug(("LCP Ask\n")); |
nixnax | 11:f58998c24f0b | 715 | ppp.pkt.buf[4]=1; // request no options |
nixnax | 9:0992486d4a30 | 716 | sendFrame(); |
nixnax | 9:0992486d4a30 | 717 | } |
nixnax | 9:0992486d4a30 | 718 | } |
nixnax | 9:0992486d4a30 | 719 | |
nixnax | 29:30de79d658f6 | 720 | void LCPconfAck() |
nixnax | 29:30de79d658f6 | 721 | { |
nixnax | 15:b0154c910143 | 722 | debug(("LCP Ack\n")); |
nixnax | 29:30de79d658f6 | 723 | } |
nixnax | 9:0992486d4a30 | 724 | |
nixnax | 29:30de79d658f6 | 725 | void LCPend() |
nixnax | 29:30de79d658f6 | 726 | { |
nixnax | 29:30de79d658f6 | 727 | debug(("LCP End\n")); |
nixnax | 29:30de79d658f6 | 728 | ppp.online=0; // start hunting for connect string again |
nixnax | 29:30de79d658f6 | 729 | ppp.pkt.buf[4]=6; |
nixnax | 29:30de79d658f6 | 730 | sendFrame(); // acknowledge |
nixnax | 9:0992486d4a30 | 731 | } |
nixnax | 9:0992486d4a30 | 732 | |
nixnax | 29:30de79d658f6 | 733 | void LCPother() |
nixnax | 29:30de79d658f6 | 734 | { |
nixnax | 29:30de79d658f6 | 735 | debug(("LCP Other\n")); |
nixnax | 29:30de79d658f6 | 736 | dumpFrame(); |
nixnax | 29:30de79d658f6 | 737 | } |
nixnax | 29:30de79d658f6 | 738 | |
nixnax | 29:30de79d658f6 | 739 | void LCPframe() |
nixnax | 29:30de79d658f6 | 740 | { |
nixnax | 29:30de79d658f6 | 741 | int code = ppp.pkt.buf[4]; |
nixnax | 29:30de79d658f6 | 742 | switch (code) { |
nixnax | 29:30de79d658f6 | 743 | case 1: |
nixnax | 29:30de79d658f6 | 744 | LCPconfReq(); |
nixnax | 29:30de79d658f6 | 745 | break; // config request |
nixnax | 29:30de79d658f6 | 746 | case 2: |
nixnax | 29:30de79d658f6 | 747 | LCPconfAck(); |
nixnax | 29:30de79d658f6 | 748 | break; // config ack |
nixnax | 29:30de79d658f6 | 749 | case 5: |
nixnax | 29:30de79d658f6 | 750 | LCPend(); |
nixnax | 29:30de79d658f6 | 751 | break; // end connection |
nixnax | 29:30de79d658f6 | 752 | default: |
nixnax | 29:30de79d658f6 | 753 | LCPother(); |
nixnax | 29:30de79d658f6 | 754 | } |
nixnax | 9:0992486d4a30 | 755 | } |
nixnax | 9:0992486d4a30 | 756 | |
nixnax | 29:30de79d658f6 | 757 | void discardedFrame() |
nixnax | 29:30de79d658f6 | 758 | { |
nixnax | 29:30de79d658f6 | 759 | if (v0) { |
nixnax | 29:30de79d658f6 | 760 | 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 | 761 | } |
nixnax | 9:0992486d4a30 | 762 | } |
nixnax | 9:0992486d4a30 | 763 | |
nixnax | 29:30de79d658f6 | 764 | void determinePacketType() |
nixnax | 29:30de79d658f6 | 765 | { |
nixnax | 29:30de79d658f6 | 766 | if ( ppp.pkt.buf[0] != 0xff ) { |
nixnax | 29:30de79d658f6 | 767 | debug(("byte0 != ff\n")); |
nixnax | 29:30de79d658f6 | 768 | return; |
nixnax | 29:30de79d658f6 | 769 | } |
nixnax | 29:30de79d658f6 | 770 | if ( ppp.pkt.buf[1] != 3 ) { |
nixnax | 29:30de79d658f6 | 771 | debug(("byte1 != 3\n")); |
nixnax | 29:30de79d658f6 | 772 | return; |
nixnax | 29:30de79d658f6 | 773 | } |
nixnax | 29:30de79d658f6 | 774 | if ( ppp.pkt.buf[3] != 0x21 ) { |
nixnax | 29:30de79d658f6 | 775 | debug(("byte2 != 21\n")); |
nixnax | 29:30de79d658f6 | 776 | return; |
nixnax | 29:30de79d658f6 | 777 | } |
nixnax | 9:0992486d4a30 | 778 | int packetType = ppp.pkt.buf[2]; |
nixnax | 9:0992486d4a30 | 779 | switch (packetType) { |
nixnax | 29:30de79d658f6 | 780 | case 0xc0: |
nixnax | 29:30de79d658f6 | 781 | LCPframe(); |
nixnax | 29:30de79d658f6 | 782 | break; // link control |
nixnax | 29:30de79d658f6 | 783 | case 0x80: |
nixnax | 29:30de79d658f6 | 784 | IPCPframe(); |
nixnax | 29:30de79d658f6 | 785 | break; // IP control |
nixnax | 29:30de79d658f6 | 786 | case 0x00: |
nixnax | 29:30de79d658f6 | 787 | IPframe(); |
nixnax | 29:30de79d658f6 | 788 | break; // IP itself |
nixnax | 29:30de79d658f6 | 789 | default: |
nixnax | 29:30de79d658f6 | 790 | discardedFrame(); |
nixnax | 9:0992486d4a30 | 791 | } |
nixnax | 29:30de79d658f6 | 792 | } |
nixnax | 9:0992486d4a30 | 793 | |
nixnax | 29:30de79d658f6 | 794 | void scanForConnectString() |
nixnax | 29:30de79d658f6 | 795 | { |
nixnax | 9:0992486d4a30 | 796 | if ( ppp.online==0 ) { |
nixnax | 15:b0154c910143 | 797 | char * clientFound = strstr( (char *)rxbuf, "CLIENTCLIENT" ); // look for PC string |
nixnax | 29:30de79d658f6 | 798 | if( clientFound ) { |
nixnax | 9:0992486d4a30 | 799 | strcpy( clientFound, "FOUND!FOUND!" ); // overwrite so we don't get fixated |
nixnax | 9:0992486d4a30 | 800 | pc.printf("CLIENTSERVER"); // respond to PC |
nixnax | 9:0992486d4a30 | 801 | ppp.online=1; // we can stop looking for the string |
nixnax | 15:b0154c910143 | 802 | debug(("Connect string found\n")); |
nixnax | 9:0992486d4a30 | 803 | } |
nixnax | 9:0992486d4a30 | 804 | } |
nixnax | 9:0992486d4a30 | 805 | } |
nixnax | 9:0992486d4a30 | 806 | |
nixnax | 0:2cf4880c312a | 807 | int main() |
nixnax | 0:2cf4880c312a | 808 | { |
nixnax | 14:c65831c25aaa | 809 | pc.baud(115200); // USB virtual serial port |
nixnax | 15:b0154c910143 | 810 | xx.baud(115200); // second serial port for debug(((((((( messages |
nixnax | 9:0992486d4a30 | 811 | xx.puts("\x1b[2J\x1b[HReady\n"); // VT100 code for clear screen & home |
nixnax | 29:30de79d658f6 | 812 | |
nixnax | 9:0992486d4a30 | 813 | pppInitStruct(); // initialize all the PPP properties |
nixnax | 4:a469050d5b80 | 814 | |
nixnax | 9:0992486d4a30 | 815 | pc.attach(&rxHandler,Serial::RxIrq); // start the receive handler |
nixnax | 4:a469050d5b80 | 816 | |
nixnax | 29:30de79d658f6 | 817 | int frameStartIndex, frameEndIndex; |
nixnax | 29:30de79d658f6 | 818 | int frameBusy=0; |
nixnax | 4:a469050d5b80 | 819 | |
nixnax | 0:2cf4880c312a | 820 | while(1) { |
nixnax | 9:0992486d4a30 | 821 | if ( ppp.online==0 ) scanForConnectString(); // try to connect |
nixnax | 22:00df34cd4d7e | 822 | while ( rxbufNotEmpty() ) { |
nixnax | 1:9e03798d4367 | 823 | int rx = pc_getBuf(); |
nixnax | 29:30de79d658f6 | 824 | if (frameBusy) { |
nixnax | 4:a469050d5b80 | 825 | if (rx==FRAME_7E) { |
nixnax | 4:a469050d5b80 | 826 | frameBusy=0; // done gathering frame |
nixnax | 4:a469050d5b80 | 827 | frameEndIndex=ppp.rx.tail-1; // remember where frame ends |
nixnax | 4:a469050d5b80 | 828 | processFrame(frameStartIndex, frameEndIndex); |
nixnax | 4:a469050d5b80 | 829 | } |
nixnax | 29:30de79d658f6 | 830 | } else { |
nixnax | 4:a469050d5b80 | 831 | if (rx==FRAME_7E) { |
nixnax | 4:a469050d5b80 | 832 | frameBusy=1; // start gathering frame |
nixnax | 9:0992486d4a30 | 833 | frameStartIndex=ppp.rx.tail; // remember where frame started |
nixnax | 4:a469050d5b80 | 834 | } |
nixnax | 0:2cf4880c312a | 835 | } |
nixnax | 4:a469050d5b80 | 836 | } |
nixnax | 7:ab147f5e97ac | 837 | } |
nixnax | 7:ab147f5e97ac | 838 | } |