RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Committer:
nixnax
Date:
Wed Sep 06 21:10:01 2017 +0000
Revision:
175:b4e6f8a6fe00
Parent:
173:6774a0c851c4
Child:
176:011dbb3f7d03
Moved ppp structure.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nixnax 142:54d1543e23e5 1 /// ppp-blinky.h
nixnax 142:54d1543e23e5 2
nixnax 142:54d1543e23e5 3 #include "mbed.h"
nixnax 142:54d1543e23e5 4 #include "sha1.h"
nixnax 142:54d1543e23e5 5 #include "BufferedSerial.h"
nixnax 142:54d1543e23e5 6
nixnax 144:01d98cf7738e 7 void initializePpp();
nixnax 150:3366bf3d294e 8 int connectedPpp();
nixnax 153:7993def8663f 9 void waitForPcConnectString();
nixnax 142:54d1543e23e5 10 void waitForPppFrame();
nixnax 152:025c73b6c0a9 11 void determinePacketType();
nixnax 152:025c73b6c0a9 12
nixnax 159:4d1bf96a59cd 13 /// PPP header
nixnax 153:7993def8663f 14 typedef struct { // [ff 03 00 21]
nixnax 167:ff8a2d8beeb1 15 unsigned int address : 8; // always 0xff
nixnax 167:ff8a2d8beeb1 16 unsigned int control : 8; // always 03
nixnax 167:ff8a2d8beeb1 17 unsigned int protocolR : 16; // byte reversed, 0x0021 for ip
nixnax 152:025c73b6c0a9 18 } pppHeaderType;
nixnax 152:025c73b6c0a9 19
nixnax 167:ff8a2d8beeb1 20 /// LCP and IPCP header
nixnax 167:ff8a2d8beeb1 21 typedef struct {
nixnax 167:ff8a2d8beeb1 22 // ppp part
nixnax 167:ff8a2d8beeb1 23 unsigned int address : 8; // always 0xff
nixnax 167:ff8a2d8beeb1 24 unsigned int control : 8; // always 03
nixnax 167:ff8a2d8beeb1 25 unsigned int protocolR : 16; // byte reversed, 0x0021 for ip
nixnax 167:ff8a2d8beeb1 26
nixnax 167:ff8a2d8beeb1 27 // ipcp and lcp part
nixnax 167:ff8a2d8beeb1 28 unsigned int code : 8; // IPCP and LCP contain a code field which identifies the requested action or response
nixnax 167:ff8a2d8beeb1 29 unsigned int identifier : 8 ;
nixnax 167:ff8a2d8beeb1 30 unsigned int lengthR : 16;
nixnax 173:6774a0c851c4 31 char request [0];
nixnax 167:ff8a2d8beeb1 32 } ipcpHeaderType;
nixnax 167:ff8a2d8beeb1 33
nixnax 159:4d1bf96a59cd 34 /// IP header
nixnax 152:025c73b6c0a9 35 typedef struct {
nixnax 154:18b2bd92f557 36 unsigned int headerLength : 4; // ip headerlength / 4
nixnax 154:18b2bd92f557 37 unsigned int version : 4; // ip version number
nixnax 154:18b2bd92f557 38 unsigned int ect : 1; // ecn capable transport
nixnax 154:18b2bd92f557 39 unsigned int ce : 1; // ecn-ce
nixnax 154:18b2bd92f557 40 unsigned int dscp : 6; // differentiated services
nixnax 160:bd701ad564cb 41 unsigned int lengthR : 16; // ip packet length (byte-reversed)
nixnax 167:ff8a2d8beeb1 42
nixnax 160:bd701ad564cb 43 unsigned int identR : 16; // ident, byte reversed
nixnax 160:bd701ad564cb 44 unsigned int fragmentOffsHi : 5;
nixnax 154:18b2bd92f557 45 unsigned int lastFragment : 1;
nixnax 160:bd701ad564cb 46 unsigned int dontFragment : 1;
nixnax 160:bd701ad564cb 47 unsigned int reservedIP : 1;
nixnax 160:bd701ad564cb 48 unsigned int fragmentOffsLo : 8;
nixnax 167:ff8a2d8beeb1 49
nixnax 160:bd701ad564cb 50 unsigned int ttl : 8;
nixnax 154:18b2bd92f557 51 unsigned int protocol : 8; // next protocol
nixnax 161:d59f778bc8ab 52 unsigned int checksumR : 16; // ip checksum, byte reversed
nixnax 167:ff8a2d8beeb1 53 union {
nixnax 167:ff8a2d8beeb1 54 unsigned int srcAdrR; // source IP address
nixnax 167:ff8a2d8beeb1 55 char srcAdrPtr [0]; // so we also have a char * to srcAdrR
nixnax 167:ff8a2d8beeb1 56 };
nixnax 167:ff8a2d8beeb1 57 union {
nixnax 167:ff8a2d8beeb1 58 unsigned int dstAdrR; // destination IP address
nixnax 167:ff8a2d8beeb1 59 char dstAdrPtr [0]; // so we also have a char * to dstAdrR
nixnax 167:ff8a2d8beeb1 60 };
nixnax 152:025c73b6c0a9 61 } ipHeaderType;
nixnax 152:025c73b6c0a9 62
nixnax 164:c3de3d212c4b 63 /// IP pseudoheader. Used in TCP/UDP checksum calculations.
nixnax 152:025c73b6c0a9 64 typedef struct {
nixnax 159:4d1bf96a59cd 65 union {
nixnax 167:ff8a2d8beeb1 66 char start [0]; // a char * to avoid type conversions
nixnax 160:bd701ad564cb 67 unsigned int srcAdrR; // source IP address
nixnax 159:4d1bf96a59cd 68 };
nixnax 159:4d1bf96a59cd 69 unsigned int dstAdrR; // destination IP address
nixnax 159:4d1bf96a59cd 70 unsigned int zero : 8;
nixnax 159:4d1bf96a59cd 71 unsigned int protocol : 8;
nixnax 159:4d1bf96a59cd 72 unsigned int lengthR : 16; // byte reversed
nixnax 159:4d1bf96a59cd 73 } pseudoIpHeaderType;
nixnax 159:4d1bf96a59cd 74
nixnax 159:4d1bf96a59cd 75 /// TCP header
nixnax 159:4d1bf96a59cd 76 typedef struct {
nixnax 159:4d1bf96a59cd 77 unsigned int srcPortR : 16; // byte reversed
nixnax 159:4d1bf96a59cd 78 unsigned int dstPortR : 16; // byte reversed
nixnax 155:9c6a1d249e26 79 unsigned int seqTcpR; // byte reversed
nixnax 155:9c6a1d249e26 80 unsigned int ackTcpR; // byte reversed
nixnax 156:163c23249731 81 unsigned int resvd1 : 4; // reserved
nixnax 156:163c23249731 82 unsigned int offset : 4; // tcp header length [5..15]
nixnax 152:025c73b6c0a9 83 union {
nixnax 156:163c23249731 84 unsigned char All; // all 8 flag bits
nixnax 156:163c23249731 85 struct { // individual flag bits
nixnax 156:163c23249731 86 unsigned char fin: 1, // fin
nixnax 156:163c23249731 87 syn : 1, // syn
nixnax 156:163c23249731 88 rst : 1, // rst
nixnax 156:163c23249731 89 psh : 1, // psh
nixnax 156:163c23249731 90 ack : 1, // ack
nixnax 156:163c23249731 91 urg : 1, // urg
nixnax 156:163c23249731 92 ece : 1, // ece
nixnax 156:163c23249731 93 cwr : 1; // cwr
nixnax 152:025c73b6c0a9 94 };
nixnax 156:163c23249731 95 } flag;
nixnax 156:163c23249731 96 unsigned int windowR : 16; // byte reversed
nixnax 154:18b2bd92f557 97 unsigned int checksumR : 16; // byte reversed
nixnax 154:18b2bd92f557 98 unsigned int urgentPointerR : 16; // byte reversed;
nixnax 154:18b2bd92f557 99 unsigned int tcpOptions[10]; // up to 10 words of options possible
nixnax 154:18b2bd92f557 100 } tcpHeaderType;
nixnax 159:4d1bf96a59cd 101
nixnax 164:c3de3d212c4b 102 /// UDP header.
nixnax 163:d1b4328e9f08 103 typedef struct {
nixnax 164:c3de3d212c4b 104 unsigned int srcPortR : 16; // byte reversed
nixnax 164:c3de3d212c4b 105 unsigned int dstPortR : 16; // byte reversed
nixnax 164:c3de3d212c4b 106 unsigned int lengthR : 16; // byte reversed
nixnax 170:3d3b2126181c 107 unsigned int checksumR : 16; // byte reversed
nixnax 163:d1b4328e9f08 108 char data [0]; // data area
nixnax 163:d1b4328e9f08 109 } udpHeaderType;
nixnax 165:c47826d07e0d 110
nixnax 165:c47826d07e0d 111 /// ICMP header.
nixnax 165:c47826d07e0d 112 typedef struct {
nixnax 165:c47826d07e0d 113 unsigned int type : 8;
nixnax 165:c47826d07e0d 114 unsigned int code : 8;
nixnax 165:c47826d07e0d 115 unsigned int checkSumR : 16; // byte reversed
nixnax 165:c47826d07e0d 116 unsigned int idR : 16; // byte reversed
nixnax 165:c47826d07e0d 117 unsigned int sequenceR : 16; // byte reversed
nixnax 165:c47826d07e0d 118 char data [0]; // data area
nixnax 165:c47826d07e0d 119 } icmpHeaderType;
nixnax 175:b4e6f8a6fe00 120
nixnax 175:b4e6f8a6fe00 121 /// Structure to manage all ppp variables.
nixnax 175:b4e6f8a6fe00 122 typedef struct pppType {
nixnax 175:b4e6f8a6fe00 123 union {
nixnax 175:b4e6f8a6fe00 124 pppHeaderType * ppp; // pointer to ppp structure
nixnax 175:b4e6f8a6fe00 125 ipcpHeaderType * ipcp; // pointer to ipcp structure
nixnax 175:b4e6f8a6fe00 126 ipcpHeaderType * lcp; // pointer to lcp structure (same as ipcp)
nixnax 175:b4e6f8a6fe00 127 };
nixnax 175:b4e6f8a6fe00 128 union {
nixnax 175:b4e6f8a6fe00 129 ipHeaderType * ip; // pointer to ip header struct
nixnax 175:b4e6f8a6fe00 130 char * ipStart; // char pointer to ip header struct (need a char pointer for byte offset calculations)
nixnax 175:b4e6f8a6fe00 131 };
nixnax 175:b4e6f8a6fe00 132 union { // a union for the packet type contained in the IP packet
nixnax 175:b4e6f8a6fe00 133 tcpHeaderType * tcp; // pointer to tcp header struct
nixnax 175:b4e6f8a6fe00 134 udpHeaderType * udp; // pointer to udp header struct
nixnax 175:b4e6f8a6fe00 135 icmpHeaderType * icmp; // pointer to udp header struct
nixnax 175:b4e6f8a6fe00 136 char * tcpStart; // char pointer to tcp header struct (need a char pointer for byte offset calculations)
nixnax 175:b4e6f8a6fe00 137 char * udpStart; // char pointer to udp header struct (need a char pointer for byte offset calculations)
nixnax 175:b4e6f8a6fe00 138 char * icmpStart; // char pointer to icmp header struct (need a char pointer for byte offset calculations)
nixnax 175:b4e6f8a6fe00 139 };
nixnax 175:b4e6f8a6fe00 140 char * tcpData; // char pointer to where tcp data starts
nixnax 175:b4e6f8a6fe00 141 int online; // we hunt for a PPP connection if this is zero
nixnax 175:b4e6f8a6fe00 142 int hostIP; // ip address of host
nixnax 175:b4e6f8a6fe00 143 int fcs; // PPP "frame check sequence" - a 16-bit HDLC-like checksum used in all PPP frames
nixnax 175:b4e6f8a6fe00 144 int ledState; // state of LED1
nixnax 175:b4e6f8a6fe00 145 int responseCounter;
nixnax 175:b4e6f8a6fe00 146 int firstFrame; // cleared after first frame
nixnax 175:b4e6f8a6fe00 147 unsigned int sum; // a checksum used in headers
nixnax 175:b4e6f8a6fe00 148 struct {
nixnax 175:b4e6f8a6fe00 149 #define RXBUFLEN (1<<11)
nixnax 175:b4e6f8a6fe00 150 // the serial port receive buffer and packet buffer, size is RXBUFLEN (currently 2048 bytes)
nixnax 175:b4e6f8a6fe00 151 char buf[RXBUFLEN]; // RXBUFLEN MUST be a power of two because we use & operator for fast wrap-around in ring buffer
nixnax 175:b4e6f8a6fe00 152 int head;
nixnax 175:b4e6f8a6fe00 153 int tail;
nixnax 175:b4e6f8a6fe00 154 int rtail;
nixnax 175:b4e6f8a6fe00 155 int buflevel;
nixnax 175:b4e6f8a6fe00 156 } rx; // serial port objects
nixnax 175:b4e6f8a6fe00 157 struct {
nixnax 175:b4e6f8a6fe00 158 int len; // number of bytes in buffer
nixnax 175:b4e6f8a6fe00 159 int crc; // PPP CRC (frame check)
nixnax 175:b4e6f8a6fe00 160 #define PPP_max_size 1600
nixnax 175:b4e6f8a6fe00 161 // we are assuming 100 bytes more than MTU size of 1500
nixnax 175:b4e6f8a6fe00 162 char buf[PPP_max_size]; // send and receive buffer large enough for largest IP packet
nixnax 175:b4e6f8a6fe00 163 } pkt; // ppp buffer objects
nixnax 175:b4e6f8a6fe00 164 struct {
nixnax 175:b4e6f8a6fe00 165 int frameStartIndex; // frame start marker
nixnax 175:b4e6f8a6fe00 166 int frameEndIndex; // frame end marker
nixnax 175:b4e6f8a6fe00 167 } hdlc; // hdlc frame objects
nixnax 175:b4e6f8a6fe00 168 struct {
nixnax 175:b4e6f8a6fe00 169 unsigned int ident; // our IP ident value (outgoing frame count)
nixnax 175:b4e6f8a6fe00 170 } ipData; // ip related object
nixnax 175:b4e6f8a6fe00 171 } pppVariables;
nixnax 175:b4e6f8a6fe00 172