RealtimeCompLab2
Dependencies: mbed
Fork of PPP-Blinky by
ppp-blinky.h
00001 /// ppp-blinky.h 00002 00003 #include "mbed.h" 00004 #include "sha1.h" 00005 00006 void initializePpp(); 00007 int connectedPpp(); 00008 void waitForPcConnectString(); 00009 void waitForPppFrame(); 00010 void determinePacketType(); 00011 void sendUdpData(); 00012 00013 /// PPP header 00014 typedef struct { // [ff 03 00 21] 00015 unsigned int address : 8; // always 0xff 00016 unsigned int control : 8; // always 03 00017 unsigned int protocolR : 16; // byte reversed, 0x0021 for ip 00018 } pppHeaderType; 00019 00020 /// LCP and IPCP header 00021 typedef struct { 00022 // ppp part 00023 unsigned int address : 8; // always 0xff 00024 unsigned int control : 8; // always 03 00025 unsigned int protocolR : 16; // byte reversed, 0x0021 for ip 00026 00027 // ipcp and lcp part 00028 unsigned int code : 8; // IPCP and LCP contain a code field which identifies the requested action or response 00029 unsigned int identifier : 8 ; 00030 unsigned int lengthR : 16; 00031 char request [0]; 00032 } ipcpHeaderType; 00033 00034 /// IP header 00035 typedef struct { 00036 unsigned int headerLength : 4; // ip headerlength / 4 00037 unsigned int version : 4; // ip version number 00038 unsigned int ect : 1; // ecn capable transport 00039 unsigned int ce : 1; // ecn-ce 00040 unsigned int dscp : 6; // differentiated services 00041 unsigned int lengthR : 16; // ip packet length (byte-reversed) 00042 00043 unsigned int identR : 16; // ident, byte reversed 00044 unsigned int fragmentOffsHi : 5; 00045 unsigned int lastFragment : 1; 00046 unsigned int dontFragment : 1; 00047 unsigned int reservedIP : 1; 00048 unsigned int fragmentOffsLo : 8; 00049 00050 unsigned int ttl : 8; 00051 unsigned int protocol : 8; // next protocol 00052 unsigned int checksumR : 16; // ip checksum, byte reversed 00053 union { 00054 unsigned int srcAdrR; // source IP address 00055 char srcAdrPtr [0]; // so we also have a char * to srcAdrR 00056 }; 00057 union { 00058 unsigned int dstAdrR; // destination IP address 00059 char dstAdrPtr [0]; // so we also have a char * to dstAdrR 00060 }; 00061 } ipHeaderType; 00062 00063 /// IP pseudoheader. Used in TCP/UDP checksum calculations. 00064 typedef struct { 00065 union { 00066 char start [0]; // a char * to avoid type conversions 00067 unsigned int srcAdrR; // source IP address 00068 }; 00069 unsigned int dstAdrR; // destination IP address 00070 unsigned int zero : 8; 00071 unsigned int protocol : 8; 00072 unsigned int lengthR : 16; // byte reversed 00073 } pseudoIpHeaderType; 00074 00075 /// TCP header 00076 typedef struct { 00077 unsigned int srcPortR : 16; // byte reversed 00078 unsigned int dstPortR : 16; // byte reversed 00079 unsigned int seqTcpR; // byte reversed 00080 unsigned int ackTcpR; // byte reversed 00081 unsigned int resvd1 : 4; // reserved 00082 unsigned int offset : 4; // tcp header length [5..15] 00083 union { 00084 unsigned char All; // all 8 flag bits 00085 struct { // individual flag bits 00086 unsigned char fin: 1, // fin 00087 syn : 1, // syn 00088 rst : 1, // rst 00089 psh : 1, // psh 00090 ack : 1, // ack 00091 urg : 1, // urg 00092 ece : 1, // ece 00093 cwr : 1; // cwr 00094 }; 00095 } flag; 00096 unsigned int windowR : 16; // byte reversed 00097 unsigned int checksumR : 16; // byte reversed 00098 unsigned int urgentPointerR : 16; // byte reversed; 00099 unsigned int tcpOptions[10]; // up to 10 words of options possible 00100 } tcpHeaderType; 00101 00102 /// UDP header. 00103 typedef struct { 00104 unsigned int srcPortR : 16; // byte reversed 00105 unsigned int dstPortR : 16; // byte reversed 00106 unsigned int lengthR : 16; // byte reversed 00107 unsigned int checksumR : 16; // byte reversed 00108 char data [0]; // data area 00109 } udpHeaderType; 00110 00111 /// ICMP header. 00112 typedef struct { 00113 unsigned int type : 8; 00114 unsigned int code : 8; 00115 unsigned int checkSumR : 16; // byte reversed 00116 unsigned int idR : 16; // byte reversed 00117 unsigned int sequenceR : 16; // byte reversed 00118 char data [0]; // data area 00119 } icmpHeaderType; 00120 00121 /// Structure to manage all ppp variables. 00122 typedef struct pppType { 00123 union { 00124 pppHeaderType * ppp; // pointer to ppp structure 00125 ipcpHeaderType * ipcp; // pointer to ipcp structure 00126 ipcpHeaderType * lcp; // pointer to lcp structure (same as ipcp) 00127 }; 00128 union { 00129 ipHeaderType * ip; // pointer to ip header struct 00130 char * ipStart; // char pointer to ip header struct (need a char pointer for byte offset calculations) 00131 }; 00132 union { // a union for the packet type contained in the IP packet 00133 tcpHeaderType * tcp; // pointer to tcp header struct 00134 udpHeaderType * udp; // pointer to udp header struct 00135 icmpHeaderType * icmp; // pointer to udp header struct 00136 char * tcpStart; // char pointer to tcp header struct (need a char pointer for byte offset calculations) 00137 char * udpStart; // char pointer to udp header struct (need a char pointer for byte offset calculations) 00138 char * icmpStart; // char pointer to icmp header struct (need a char pointer for byte offset calculations) 00139 }; 00140 char * tcpData; // char pointer to where tcp data starts 00141 int online; // we hunt for a PPP connection if this is zero 00142 int hostIP; // ip address of host 00143 int fcs; // PPP "frame check sequence" - a 16-bit HDLC-like checksum used in all PPP frames 00144 int ledState; // state of LED1 00145 int responseCounter; 00146 int firstFrame; // cleared after first frame 00147 unsigned int sum; // a checksum used in headers 00148 struct { 00149 #define RXBUFLEN (1<<11) 00150 // the serial port receive buffer and packet buffer, size is RXBUFLEN (currently 2048 bytes) 00151 char buf[RXBUFLEN]; // RXBUFLEN MUST be a power of two because we use & operator for fast wrap-around in ring buffer 00152 volatile int head; // declared volatile so user code knows this variable changes in the interrupt handler 00153 int tail; 00154 int rtail; 00155 int buflevel; 00156 } rx; // serial port objects 00157 struct { 00158 int len; // number of bytes in buffer 00159 int crc; // PPP CRC (frame check) 00160 #define PPP_max_size 1600 00161 // we are assuming 100 bytes more than MTU size of 1500 00162 char buf[PPP_max_size]; // send and receive buffer large enough for largest IP packet 00163 } pkt; // ppp buffer objects 00164 struct { 00165 int frameStartIndex; // frame start marker 00166 int frameEndIndex; // frame end marker 00167 } hdlc; // hdlc frame objects 00168 struct { 00169 unsigned int ident; // our IP ident value (outgoing frame count) 00170 } ipData; // ip related object 00171 } pppVariables; 00172 00173 void t_print(int i, char* process); 00174 void l_off(void);void l_on(void); 00175 void led_flash(void);
Generated on Wed Jul 13 2022 22:24:08 by 1.7.2