1 |
/****************************************************************** |
---|
2 |
***** ***** |
---|
3 |
***** Name: tcpip.h ***** |
---|
4 |
***** Ver.: 1.0 ***** |
---|
5 |
***** Date: 07/05/2001 ***** |
---|
6 |
***** Auth: Andreas Dannenberg ***** |
---|
7 |
***** HTWK Leipzig ***** |
---|
8 |
***** university of applied sciences ***** |
---|
9 |
***** Germany ***** |
---|
10 |
***** Func: header-file for tcpip.c ***** |
---|
11 |
***** ***** |
---|
12 |
******************************************************************/ |
---|
13 |
|
---|
14 |
#ifndef __TCPIP_H |
---|
15 |
#define __TCPIP_H |
---|
16 |
|
---|
17 |
// easyWEB-stack definitions |
---|
18 |
#define MYIP_1 192 // our internet protocol (IP) address |
---|
19 |
#define MYIP_2 168 |
---|
20 |
#define MYIP_3 3 |
---|
21 |
#define MYIP_4 100 |
---|
22 |
|
---|
23 |
#define SUBMASK_1 255 // subnet mask |
---|
24 |
#define SUBMASK_2 255 |
---|
25 |
#define SUBMASK_3 255 |
---|
26 |
#define SUBMASK_4 0 |
---|
27 |
|
---|
28 |
#define GWIP_1 192 // standard gateway (used if remote |
---|
29 |
#define GWIP_2 168 // IP is no part of our subnet) |
---|
30 |
#define GWIP_3 1 |
---|
31 |
#define GWIP_4 1 |
---|
32 |
|
---|
33 |
#define RETRY_TIMEOUT 8 // wait max. 8 x 262ms for an ACK (about 2 sec.) |
---|
34 |
#define FIN_TIMEOUT 2 // max. time to wait for an ACK of a FIN |
---|
35 |
// before closing TCP state-machine (about 0.5 s) |
---|
36 |
#define MAX_RETRYS 4 // nr. of resendings before reset conn. |
---|
37 |
// total nr. of transmissions = MAX_RETRYS + 1 |
---|
38 |
|
---|
39 |
#define MAX_TCP_TX_DATA_SIZE 512 // max. outgoing TCP data size (even!) |
---|
40 |
#define MAX_TCP_RX_DATA_SIZE 256 // max. incoming TCP data size (even!) |
---|
41 |
// (increasing the buffer-size dramatically |
---|
42 |
// increases the transfer-speed!) |
---|
43 |
|
---|
44 |
#define MAX_ETH_TX_DATA_SIZE 60 // 2nd buffer, used for ARP, ICMP, TCP (even!) |
---|
45 |
// enough to echo 32 byte via ICMP |
---|
46 |
|
---|
47 |
#define DEFAULT_TTL 64 // Time To Live sent with packets |
---|
48 |
|
---|
49 |
// Ethernet network layer definitions |
---|
50 |
#define ETH_DA_OFS 0 // Destination MAC address (48 Bit) |
---|
51 |
#define ETH_SA_OFS 6 // Source MAC address (48 Bit) |
---|
52 |
#define ETH_TYPE_OFS 12 // Type field (16 Bit) |
---|
53 |
#define ETH_DATA_OFS 14 // Frame Data |
---|
54 |
#define ETH_HEADER_SIZE 14 |
---|
55 |
|
---|
56 |
#define FRAME_ARP 0x0806 // frame types (stored in Type/Length field) |
---|
57 |
#define FRAME_IP 0x0800 |
---|
58 |
|
---|
59 |
// IPv4 layer definitions |
---|
60 |
#define IP_VER_IHL_TOS_OFS ETH_DATA_OFS + 0 // Version, Header Length, Type of Service |
---|
61 |
#define IP_TOTAL_LENGTH_OFS ETH_DATA_OFS + 2 // IP Frame's Total Length |
---|
62 |
#define IP_IDENT_OFS ETH_DATA_OFS + 4 // Identifying Value |
---|
63 |
#define IP_FLAGS_FRAG_OFS ETH_DATA_OFS + 6 // Flags and Fragment Offset |
---|
64 |
#define IP_TTL_PROT_OFS ETH_DATA_OFS + 8 // Frame's Time to Live, Protocol |
---|
65 |
#define IP_HEAD_CHKSUM_OFS ETH_DATA_OFS + 10 // IP Frame's Header Checksum |
---|
66 |
#define IP_SOURCE_OFS ETH_DATA_OFS + 12 // Source Address (32 Bit) |
---|
67 |
#define IP_DESTINATION_OFS ETH_DATA_OFS + 16 // Destination Address (32 Bit) |
---|
68 |
#define IP_DATA_OFS ETH_DATA_OFS + 20 // Frame Data (if no options) |
---|
69 |
#define IP_HEADER_SIZE 20 // w/o options |
---|
70 |
|
---|
71 |
#define IP_VER_IHL 0x4500 // IPv4, Header Length = 5x32 bit |
---|
72 |
#define IP_TOS_D 0x0010 // TOS low delay |
---|
73 |
#define IP_TOS_T 0x0008 // TOS high throughput |
---|
74 |
#define IP_TOS_R 0x0004 // TOS high reliability |
---|
75 |
|
---|
76 |
#define IP_FLAG_DONTFRAG 0x4000 // don't fragment IP frame |
---|
77 |
#define IP_FLAG_MOREFRAG 0x2000 // more fragments available |
---|
78 |
#define IP_FRAGOFS_MASK 0x1FFF // indicates where this fragment belongs |
---|
79 |
|
---|
80 |
#define PROT_ICMP 1 // Internet Control Message Protocol |
---|
81 |
#define PROT_TCP 6 // Transmission Control Protocol |
---|
82 |
#define PROT_UDP 17 // User Datagram Protocol |
---|
83 |
|
---|
84 |
// ARP definitions |
---|
85 |
#define ARP_HARDW_OFS ETH_DATA_OFS + 0 // Hardware address type |
---|
86 |
#define ARP_PROT_OFS ETH_DATA_OFS + 2 // Protocol |
---|
87 |
#define ARP_HLEN_PLEN_OFS ETH_DATA_OFS + 4 // byte length of each hardw. / prot. address |
---|
88 |
#define ARP_OPCODE_OFS ETH_DATA_OFS + 6 // Opcode |
---|
89 |
#define ARP_SENDER_HA_OFS ETH_DATA_OFS + 8 // Hardw. address of sender of this packet |
---|
90 |
#define ARP_SENDER_IP_OFS ETH_DATA_OFS + 14 // IP address of sender |
---|
91 |
#define ARP_TARGET_HA_OFS ETH_DATA_OFS + 18 // Hardw. address of target of this packet |
---|
92 |
#define ARP_TARGET_IP_OFS ETH_DATA_OFS + 24 // IP address of target |
---|
93 |
#define ARP_FRAME_SIZE 28 |
---|
94 |
|
---|
95 |
#define HARDW_ETH10 1 // hardware-type 10Mbps Ethernet |
---|
96 |
#define IP_HLEN_PLEN 0x0604 // MAC = 6 byte long, IP = 4 byte long |
---|
97 |
#define OP_ARP_REQUEST 1 // operations for ARP-frames |
---|
98 |
#define OP_ARP_ANSWER 2 |
---|
99 |
|
---|
100 |
// ICMP definitions |
---|
101 |
#define ICMP_TYPE_CODE_OFS IP_DATA_OFS + 0 // type of message |
---|
102 |
#define ICMP_CHKSUM_OFS IP_DATA_OFS + 2 // checksum of ICMP-message (16 bit) |
---|
103 |
#define ICMP_DATA_OFS IP_DATA_OFS + 4 |
---|
104 |
#define ICMP_HEADER_SIZE 4 |
---|
105 |
|
---|
106 |
#define ICMP_ECHO 8 // message is an echo request |
---|
107 |
#define ICMP_ECHO_REPLY 0 // message is an echo reply |
---|
108 |
|
---|
109 |
// TCP layer definitions |
---|
110 |
#define TCP_SRCPORT_OFS IP_DATA_OFS + 0 // Source Port (16 bit) |
---|
111 |
#define TCP_DESTPORT_OFS IP_DATA_OFS + 2 // Destination Port (16 bit) |
---|
112 |
#define TCP_SEQNR_OFS IP_DATA_OFS + 4 // Sequence Number (32 bit) |
---|
113 |
#define TCP_ACKNR_OFS IP_DATA_OFS + 8 // Acknowledge Number (32 bit) |
---|
114 |
#define TCP_DATA_CODE_OFS IP_DATA_OFS + 12 // Data Offset and Control Bits (16 bit) |
---|
115 |
#define TCP_WINDOW_OFS IP_DATA_OFS + 14 // Window Size (16 bit) |
---|
116 |
#define TCP_CHKSUM_OFS IP_DATA_OFS + 16 // Checksum Field (16 bit) |
---|
117 |
#define TCP_URGENT_OFS IP_DATA_OFS + 18 // Urgent Pointer (16 bit) |
---|
118 |
#define TCP_DATA_OFS IP_DATA_OFS + 20 // Frame Data (if no options) |
---|
119 |
#define TCP_HEADER_SIZE 20 // size w/o options |
---|
120 |
|
---|
121 |
#define DATA_OFS_MASK 0xF000 // number of 32 bit words in the TCP Header |
---|
122 |
|
---|
123 |
#define TCP_CODE_FIN 0x0001 |
---|
124 |
#define TCP_CODE_SYN 0x0002 |
---|
125 |
#define TCP_CODE_RST 0x0004 |
---|
126 |
#define TCP_CODE_PSH 0x0008 |
---|
127 |
#define TCP_CODE_ACK 0x0010 |
---|
128 |
#define TCP_CODE_URG 0x0020 |
---|
129 |
|
---|
130 |
#define TCP_OPT_MSS 0x0204 // Type 2, Option Length 4 (Max. Segment Size) |
---|
131 |
#define TCP_OPT_MSS_SIZE 4 |
---|
132 |
|
---|
133 |
// define some TCP standard-ports, useful for testing... |
---|
134 |
#define TCP_PORT_ECHO 7 // echo |
---|
135 |
#define TCP_PORT_DISCARD 9 // discard |
---|
136 |
#define TCP_PORT_DAYTIME 13 // daytime |
---|
137 |
#define TCP_PORT_QOTD 17 // quote of the day |
---|
138 |
#define TCP_PORT_CHARGEN 19 // character generator |
---|
139 |
#define TCP_PORT_HTTP 80 // word wide web HTTP |
---|
140 |
|
---|
141 |
// macros |
---|
142 |
#define SWAPB(Word) ((unsigned short)((Word) << 8) | ((Word) >> 8)) |
---|
143 |
// convert little <-> big endian |
---|
144 |
|
---|
145 |
// typedefs |
---|
146 |
typedef enum { // states of the TCP-state machine |
---|
147 |
CLOSED, // according to RFC793 |
---|
148 |
LISTENING, |
---|
149 |
SYN_SENT, |
---|
150 |
SYN_RECD, |
---|
151 |
ESTABLISHED, |
---|
152 |
FIN_WAIT_1, |
---|
153 |
FIN_WAIT_2, |
---|
154 |
CLOSE_WAIT, |
---|
155 |
CLOSING, |
---|
156 |
LAST_ACK, |
---|
157 |
TIME_WAIT |
---|
158 |
} TTCPStateMachine; |
---|
159 |
|
---|
160 |
typedef enum { // type of last frame sent. used |
---|
161 |
ARP_REQUEST, // for retransmissions |
---|
162 |
TCP_SYN_FRAME, |
---|
163 |
TCP_SYN_ACK_FRAME, |
---|
164 |
TCP_FIN_FRAME, |
---|
165 |
TCP_DATA_FRAME |
---|
166 |
} TLastFrameSent; |
---|
167 |
|
---|
168 |
// constants |
---|
169 |
#ifdef extern // Keil: declarations for modular programming |
---|
170 |
const unsigned short MyIP[] = // "MYIP1.MYIP2.MYIP3.MYIP4" |
---|
171 |
{ |
---|
172 |
MYIP_1 + (MYIP_2 << 8), // use 'unsigned int' to |
---|
173 |
MYIP_3 + (MYIP_4 << 8) // achieve word alignment |
---|
174 |
}; |
---|
175 |
|
---|
176 |
const unsigned short SubnetMask[] = // "SUBMASK1.SUBMASK2.SUBMASK3.SUBMASK4" |
---|
177 |
{ |
---|
178 |
SUBMASK_1 + (SUBMASK_2 << 8), // use 'unsigned int' to |
---|
179 |
SUBMASK_3 + (SUBMASK_4 << 8) // achieve word alignment |
---|
180 |
}; |
---|
181 |
|
---|
182 |
const unsigned short GatewayIP[] = // "GWIP1.GWIP2.GWIP3.GWIP4" |
---|
183 |
{ |
---|
184 |
GWIP_1 + (GWIP_2 << 8), // use 'unsigned int' to |
---|
185 |
GWIP_3 + (GWIP_4 << 8) // achieve word alignment |
---|
186 |
}; |
---|
187 |
#else |
---|
188 |
extern const unsigned short MyIP[2]; // "MYIP1.MYIP2.MYIP3.MYIP4" |
---|
189 |
extern const unsigned short SubnetMask[2]; // "SUBMASK1.SUBMASK2.SUBMASK3.SUBMASK4" |
---|
190 |
extern const unsigned short GatewayIP[2]; // "GWIP1.GWIP2.GWIP3.GWIP4" |
---|
191 |
extern const unsigned char MyMAC[6]; // "M1-M2-M3-M4-M5-M6" |
---|
192 |
#endif |
---|
193 |
|
---|
194 |
// easyWEB's internal variables |
---|
195 |
extern TTCPStateMachine TCPStateMachine; // perhaps the most important var at all ;-) |
---|
196 |
extern TLastFrameSent LastFrameSent; // retransmission type |
---|
197 |
|
---|
198 |
extern unsigned short ISNGenHigh; // upper word of our Initial Sequence Number |
---|
199 |
extern unsigned long TCPSeqNr; // next sequence number to send |
---|
200 |
extern unsigned long TCPUNASeqNr; // last unaknowledged sequence number |
---|
201 |
// incremented AFTER sending data |
---|
202 |
extern unsigned long TCPAckNr; // next seq to receive and ack to send |
---|
203 |
// incremented AFTER receiving data |
---|
204 |
extern unsigned char TCPTimer; // inc'd each 262ms |
---|
205 |
extern unsigned char RetryCounter; // nr. of retransmissions |
---|
206 |
|
---|
207 |
// properties of the just received frame |
---|
208 |
extern unsigned short RecdFrameLength; // EMAC reported frame length |
---|
209 |
extern unsigned short RecdFrameMAC[3]; // 48 bit MAC |
---|
210 |
extern unsigned short RecdFrameIP[2]; // 32 bit IP |
---|
211 |
extern unsigned short RecdIPFrameLength; // 16 bit IP packet length |
---|
212 |
|
---|
213 |
// the next 3 buffers must be word-aligned! |
---|
214 |
// (here the 'RecdIPFrameLength' above does that) |
---|
215 |
extern unsigned short _TxFrame1[(ETH_HEADER_SIZE + IP_HEADER_SIZE + TCP_HEADER_SIZE + MAX_TCP_TX_DATA_SIZE)/2]; |
---|
216 |
extern unsigned short _TxFrame2[(ETH_HEADER_SIZE + MAX_ETH_TX_DATA_SIZE)/2]; |
---|
217 |
extern unsigned short _RxTCPBuffer[MAX_TCP_RX_DATA_SIZE/2]; // space for incoming TCP-data |
---|
218 |
#define TxFrame1 ((unsigned char *)_TxFrame1) |
---|
219 |
#define TxFrame2 ((unsigned char *)_TxFrame2) |
---|
220 |
#define RxTCPBuffer ((unsigned char *)_RxTCPBuffer) |
---|
221 |
|
---|
222 |
extern unsigned short TxFrame1Size; // bytes to send in TxFrame1 |
---|
223 |
extern unsigned char TxFrame2Size; // bytes to send in TxFrame2 |
---|
224 |
|
---|
225 |
extern unsigned char TransmitControl; |
---|
226 |
#define SEND_FRAME1 0x01 |
---|
227 |
#define SEND_FRAME2 0x02 |
---|
228 |
|
---|
229 |
extern unsigned char TCPFlags; |
---|
230 |
#define TCP_ACTIVE_OPEN 0x01 // easyWEB shall initiate a connection |
---|
231 |
#define IP_ADDR_RESOLVED 0x02 // IP sucessfully resolved to MAC |
---|
232 |
#define TCP_TIMER_RUNNING 0x04 |
---|
233 |
#define TIMER_TYPE_RETRY 0x08 |
---|
234 |
#define TCP_CLOSE_REQUESTED 0x10 |
---|
235 |
|
---|
236 |
// prototypes |
---|
237 |
void DoNetworkStuff(void); |
---|
238 |
|
---|
239 |
// Handlers for incoming frames |
---|
240 |
void ProcessEthBroadcastFrame(void); |
---|
241 |
void ProcessEthIAFrame(void); |
---|
242 |
void ProcessICMPFrame(void); |
---|
243 |
void ProcessTCPFrame(void); |
---|
244 |
|
---|
245 |
// fill TX-buffers |
---|
246 |
void PrepareARP_REQUEST(void); |
---|
247 |
void PrepareARP_ANSWER(void); |
---|
248 |
void PrepareICMP_ECHO_REPLY(void); |
---|
249 |
void PrepareTCP_FRAME(unsigned short TCPCode); |
---|
250 |
void PrepareTCP_DATA_FRAME(void); |
---|
251 |
|
---|
252 |
// general help functions |
---|
253 |
void SendFrame1(void); |
---|
254 |
void SendFrame2(void); |
---|
255 |
void TCPStartRetryTimer(void); |
---|
256 |
void TCPStartTimeWaitTimer(void); |
---|
257 |
void TCPRestartTimer(void); |
---|
258 |
void TCPStopTimer(void); |
---|
259 |
void TCPHandleRetransmission(void); |
---|
260 |
void TCPHandleTimeout(void); |
---|
261 |
unsigned short CalcChecksum(void *Start, unsigned short Count, unsigned char IsTCP); |
---|
262 |
|
---|
263 |
// functions to work with big-endian numbers |
---|
264 |
unsigned short SwapBytes(unsigned short Data); |
---|
265 |
void WriteWBE(unsigned char *Add, unsigned short Data); |
---|
266 |
void WriteDWBE(unsigned char *Add, unsigned long Data); |
---|
267 |
|
---|
268 |
// easyWEB-API functions |
---|
269 |
void TCPLowLevelInit(void); // setup timer, LAN-controller, flags... |
---|
270 |
void TCPPassiveOpen(void); // listen for a connection |
---|
271 |
void TCPActiveOpen(void); // open connection |
---|
272 |
void TCPClose(void); // close connection |
---|
273 |
void TCPReleaseRxBuffer(void); // indicate to discard rec'd packet |
---|
274 |
void TCPTransmitTxBuffer(void); // initiate transfer after TxBuffer is filled |
---|
275 |
//void TCPClockHandler(void) __irq; // Keil: interrupt service routine for timer 0 |
---|
276 |
|
---|
277 |
|
---|
278 |
void TCPClockHandler(void) __attribute__ ((interrupt ("IRQ"))); |
---|
279 |
|
---|
280 |
|
---|
281 |
|
---|
282 |
// easyWEB-API global vars and flags |
---|
283 |
extern unsigned short TCPRxDataCount; // nr. of bytes rec'd |
---|
284 |
extern unsigned short TCPTxDataCount; // nr. of bytes to send |
---|
285 |
|
---|
286 |
extern unsigned short TCPLocalPort; // TCP ports |
---|
287 |
extern unsigned short TCPRemotePort; |
---|
288 |
|
---|
289 |
extern unsigned short RemoteMAC[3]; // MAC and IP of current TCP-session |
---|
290 |
extern unsigned short RemoteIP[2]; |
---|
291 |
|
---|
292 |
extern unsigned char SocketStatus; |
---|
293 |
#define SOCK_ACTIVE 0x01 // state machine NOT closed |
---|
294 |
#define SOCK_CONNECTED 0x02 // user may send & receive data |
---|
295 |
#define SOCK_DATA_AVAILABLE 0x04 // new data available |
---|
296 |
#define SOCK_TX_BUF_RELEASED 0x08 // user may fill buffer |
---|
297 |
|
---|
298 |
#define SOCK_ERROR_MASK 0xF0 // bit-mask to check for errors |
---|
299 |
#define SOCK_ERR_OK 0x00 // no error |
---|
300 |
#define SOCK_ERR_ARP_TIMEOUT 0x10 // timeout waiting for an ARP-REPLY |
---|
301 |
#define SOCK_ERR_TCP_TIMEOUT 0x20 // timeout waiting for an ACK |
---|
302 |
#define SOCK_ERR_CONN_RESET 0x30 // connection was reset by the other TCP |
---|
303 |
#define SOCK_ERR_REMOTE 0x40 // remote TCP caused fatal error |
---|
304 |
#define SOCK_ERR_ETHERNET 0x50 // network interface error (timeout) |
---|
305 |
|
---|
306 |
// easyWEB-API buffer-pointers |
---|
307 |
#define TCP_TX_BUF ((unsigned char *)TxFrame1 + ETH_HEADER_SIZE + IP_HEADER_SIZE + TCP_HEADER_SIZE) |
---|
308 |
#define TCP_RX_BUF ((unsigned char *)RxTCPBuffer) |
---|
309 |
|
---|
310 |
#endif |
---|
311 |
|
---|