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