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