RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Committer:
nixnax
Date:
Wed Sep 06 21:15:44 2017 +0000
Revision:
176:011dbb3f7d03
Parent:
174:e5a3f16421a5
Parent:
175:b4e6f8a6fe00
Child:
179:ba2b2ddd0da5
Merge

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