📄 tcp.lst
字号:
C51 COMPILER V6.12 TCP 05/19/2009 14:55:30 PAGE 1
C51 COMPILER V6.12, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN .\TCP.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ..\TCPIP\TCP.c BROWSE DEBUG OBJECTEXTEND PRINT(.\TCP.lst) OBJECT(.\TCP.obj)
-
stmt level source
1 /*
2 * Copyright (c) 2003 Electric Application Laboratory of NAN KAI University
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * Author: Li Zhanglin <wzzlin@nankai.edu.cn>
28 *
29 */
30
31 #include "..\GloblDef\GloblDef.h"
32 #include "..\TCPIP\TCPIPmem.h"
33 #include "..\TCPIP\IP.h"
34 #include "..\TCPIP\Netif.h"
35 #include "..\TCPIP\TCP.h"
36
37 struct STCB DT_XDATA TCBPool[TCP_CONNECTION_MAX_NUM];
38 struct STCB DT_XDATA * DT_XDATA TCBFreeList; /* free list */
39 struct STCB DT_XDATA * DT_XDATA TCBList; /* tcb in use */
40
41 struct SPacketQueue DT_XDATA QPool[TCP_QUEUE_MAX_NUM];
42 struct SPacketQueue DT_XDATA * DT_XDATA QFreeList;
43
44 struct STCB DT_XDATA *TCPGetTCB() REENTRANT_SIG
45 {
46 1 struct STCB DT_XDATA * pTCB;
47 1 if((pTCB = TCBFreeList) != NULL)
48 1 {
49 2 TCBFreeList = TCBFreeList->pNext;
50 2 }
51 1 return pTCB;
52 1 }
53
54 void TCPInsertTCB(struct STCB DT_XDATA *pTCB) REENTRANT_SIG
C51 COMPILER V6.12 TCP 05/19/2009 14:55:30 PAGE 2
55 {
56 1 pTCB->pNext = TCBList;
57 1 TCBList = pTCB;
58 1 }
59
60 struct SPacketQueue DT_XDATA * TCPGetQ() REENTRANT_SIG
61 {
62 1 struct SPacketQueue DT_XDATA * pQ;
63 1 if((pQ = QFreeList) != NULL)
64 1 {
65 2 QFreeList = QFreeList->pNext;
66 2 }
67 1 return pQ;
68 1 }
69
70 /* insert to the head of *ppQ. Q is a double link chain */
71 BOOL TCPInsertQ(struct SPacketQueue DT_XDATA * DT_XDATA * ppQ,struct SMemHead DT_XDATA *MemHead,
72 DWORD Seq) REENTRANT_SIG
73 {
74 1 struct SPacketQueue DT_XDATA *pNewQ;
75 1 struct SPacketQueue DT_XDATA *pQ;
76 1
77 1 /* allocate a queue to it */
78 1 if((pNewQ = TCPGetQ()) == NULL)
79 1 return FALSE;
80 1
81 1 /* write content */
82 1 pNewQ->Seq = Seq;
83 1 pNewQ->MemHead = MemHead;
84 1
85 1 /*
86 1 * link in the queue
87 1 */
88 1
89 1 /* if is a empty queue */
90 1 if(*ppQ == NULL)
91 1 {
92 2 *ppQ = pNewQ;
93 2
94 2 /* pNext pPre point to itself when no others in queue */
95 2 pNewQ->pNext = pNewQ;
96 2 pNewQ->pPre = pNewQ;
97 2 }
98 1 else
99 1 {
100 2 pQ = *ppQ;
101 2
102 2 /* pNext link */
103 2 pNewQ->pNext = pQ->pNext;
104 2 pQ->pNext = pNewQ;
105 2
106 2 /* pPre link */
107 2 pNewQ->pNext->pPre = pNewQ;
108 2 pNewQ->pPre = pQ;
109 2 }
110 1 return TRUE;
111 1 }
112
113 /* move the last unit in queue outof queue,if the queue
114 is empty return FALSE.actrually last unit is *ppQ */
115 struct SPacketQueue DT_XDATA * TCPOutQ(struct SPacketQueue DT_XDATA * DT_XDATA * ppQ) REENTRANT_SIG
116 {
C51 COMPILER V6.12 TCP 05/19/2009 14:55:30 PAGE 3
117 1 struct SPacketQueue DT_XDATA *pQ;
118 1
119 1 /* a empty queue? */
120 1 if((pQ = *ppQ) == NULL)
121 1 return NULL;
122 1
123 1 /* after remove it, the queue is empty? */
124 1 if(pQ->pNext == pQ)
125 1 *ppQ = NULL;
126 1 else
127 1 {
128 2 /* relink */
129 2 pQ->pPre->pNext = pQ->pNext;
130 2 pQ->pNext->pPre = pQ->pPre;
131 2
132 2 /* and the queue head *ppQ point to pQ->pPre */
133 2 *ppQ = pQ->pPre;
134 2 }
135 1
136 1 /* relaim it. link to QFreeList */
137 1 pQ->pNext = QFreeList;
138 1 QFreeList = pQ;
139 1 return pQ;
140 1 }
141
142 void TCPInit() REENTRANT_MUL
143 {
144 1 WORD i;
145 1
146 1 /* move TCBPool to TCBFreeList */
147 1 for(i = 0, TCBFreeList = NULL; i<TCP_CONNECTION_MAX_NUM; i++)
148 1 {
149 2 TCBPool[i].pNext = TCBFreeList;
150 2 TCBFreeList = &TCBPool[i];
151 2 }
152 1
153 1 /* move QPool to QFreeList */
154 1 for(i = 0, QFreeList = NULL; i<TCP_QUEUE_MAX_NUM; i++)
155 1 {
156 2 QPool[i].pNext = QFreeList;
157 2 QFreeList = &QPool[i];
158 2 }
159 1
160 1 TCBList = NULL;
161 1 }
162
163
164 /* tcp check sum. return check sum. TCPSize = TCPDataSize + TCPHeadSize*/
165 WORD TCPCheckSum(struct SIPHead DT_XDATA * pIPHead,WORD TCPSize) REENTRANT_SIG
166 {
167 1 DWORD sum = 0;
168 1 WORD DT_XDATA * p;
169 1 BYTE i;
170 1
171 1 /* clac pseudo-header CheckSum. pseudo-header is:
172 1 source ip, destination ip, pad 8 bits, protocol, TCP lendth */
173 1 sum = 0;
174 1
175 1 /* source ip and dest ip */
176 1 p = (WORD DT_XDATA *)(&(pIPHead->IPScr));
177 1 for(i=0; i < sizeof(DWORD)/sizeof(WORD)*2; i++,p++)
178 1 sum += *p;
C51 COMPILER V6.12 TCP 05/19/2009 14:55:30 PAGE 4
179 1
180 1 /* pad 8 and protocol */
181 1 sum += pIPHead->Protocol;
182 1
183 1 /* tcp lendth */
184 1 sum += TCPSize;
185 1
186 1 return CheckSum((WORD DT_XDATA *)((BYTE DT_XDATA *)pIPHead + IP_HEAD_MIN_LEN),TCPSize,sum);
187 1 }
188
189 /* this funtion should be called periodically */
190 void TCPTimer() REENTRANT_MUL
191 {
192 1 struct STCB DT_XDATA *pTCB;
193 1
194 1 /* go through all tcbs to see if any time out */
195 1 for(pTCB = TCBList; pTCB != NULL; pTCB = pTCB->pNext)
196 1 {
197 2 /* delayed ack need send now? */
198 2 if(pTCB->bNeedAck == TRUE)
199 2 {
200 3 if(pTCB->DelayAckTimer == 0)
201 3 {
202 4 /* send a ack. bNeedAck will set FLASE in TCPOutput*/
203 4 TCPSendSeg(pTCB,TCPAllocate(0),TCP_ACK);
204 4 }
205 3 else
206 3 pTCB->DelayAckTimer--;
207 3 }
208 2
209 2 /* TCP_STATE_LASTACK TCP_STATE_TIMEWAIT state time out? */
210 2 if(pTCB->TCPState == TCP_STATE_LASTACK ||
211 2 pTCB->TCPState == TCP_STATE_TIMEWAIT)
212 2 {
213 3 if(pTCB->LastAckTimer == 0)
214 3 {
215 4 pTCB->TCPState = TCP_STATE_CLOSED;
216 4
217 4 /* release buf queue and call user close */
218 4 TCPRelease(pTCB);
219 4 /* let pTCB->close(); to be call when they send a fin when we at established */
220 4 }
221 3 else
222 3 pTCB->LastAckTimer--;
223 3 }
224 2
225 2 /* if retransmit timer out? */
226 2 if(pTCB->QUnacked != NULL)
227 2 {
228 3 if(pTCB->RetranTimer == 0)
229 3 {
230 4 /* retransmit,pStart set to tcpdata */
231 4 IPOutput(pTCB->QUnacked->MemHead);
232 4
233 4 /* timer restart and retransmit times inc */
234 4 if(pTCB->RetranTimes == TCP_RETRAN_MAX_TIME)
235 4 {
236 5 pTCB->TCPState = TCP_STATE_CLOSED;
237 5
238 5 /* closed by countpart shut down */
239 5 TCPRelease(pTCB);
240 5 }
C51 COMPILER V6.12 TCP 05/19/2009 14:55:30 PAGE 5
241 4 else
242 4 {
243 5 pTCB->RetranTimes++;
244 5 pTCB->RetranTimer = TCP_RETRAN_TIME_OUT;
245 5 }
246 4 }
247 3 else
248 3 pTCB->RetranTimer--;
249 3 }
250 2 }
251 1 }
252 /* when a TCP close, send too much packet but no replay,
253 connection fail. TCPIP will call TCPRelease to release packet
254 and queue, but will not reclaim TCB. in other word user
255 can use this socket again. */
256 void TCPRelease(struct STCB DT_XDATA *pTCB) REENTRANT_SIG
257 {
258 1 struct SPacketQueue DT_XDATA *pQ;
259 1
260 1 /* reclaim Q, and free packet in queue */
261 1 while(pQ = TCPOutQ(&(pTCB->QExceedSeq)))
262 1 MemFree(pQ->MemHead);
263 1 while(pQ = TCPOutQ(&(pTCB->QUnacked)))
264 1 MemFree(pQ->MemHead);
265 1 while(pQ = TCPOutQ(&(pTCB->QUnSend)))
266 1 MemFree(pQ->MemHead);
267 1 }
268
269 /* fill a segment and send it,NOTE MemHead->pStart point to TCPData */
270 BOOL TCPSendSeg(struct STCB DT_XDATA *pTCB,struct SMemHead DT_XDATA *MemHead,BYTE TCPFlag) REENTRANT_SIG
271 {
272 1 struct STCPHead DT_XDATA *pTCPHead;
273 1 struct SIPHead DT_XDATA *pIPHead;
274 1 WORD SeqInc;
275 1
276 1 /* mem insufficient? */
277 1 if(MemHead == NULL)
278 1 return FALSE;
279 1
280 1 /* SeqMine increasment */
281 1 if((TCPFlag & TCP_SYN) || (TCPFlag & TCP_FIN))
282 1 {
283 2 SeqInc = MemHead->pEnd - MemHead->pStart + 1;
284 2 }
285 1 else
286 1 {
287 2 SeqInc = MemHead->pEnd - MemHead->pStart;
288 2 }
289 1
290 1 pTCPHead = (struct STCPHead DT_XDATA *)(MemHead->pStart - sizeof(struct STCPHead));
291 1
292 1 /* fill tcp header */
293 1 pTCPHead->PortDest = pTCB->PortDest;
294 1 pTCPHead->PortScr = pTCB->PortScr;
295 1 pTCPHead->Seq = htonl(pTCB->SeqMine);
296 1 pTCPHead->AckSeq = htonl(pTCB->SeqHis);
297 1 pTCPHead->TCPHeadLen = (BYTE)(((BYTE)sizeof(struct STCPHead)/4)<<4);
298 1 pTCPHead->flag = TCPFlag;
299 1 pTCPHead->WndSize = htons(pTCB->WndMine = MemFreeSize());
300 1 pTCPHead->CheckSum = 0;
301 1 pTCPHead->UrgentPoint = 0;
302 1
C51 COMPILER V6.12 TCP 05/19/2009 14:55:30 PAGE 6
303 1 /* fill some of IPHead. it will be used to calculate TCPChecksum
304 1 and as augument passed to IPlayer */
305 1 pIPHead = (struct SIPHead DT_XDATA *)((BYTE DT_XDATA *)pTCPHead - IP_HEAD_MIN_LEN);
306 1 pIPHead->IPDest = pTCB->IPDest;
307 1 pIPHead->IPScr = pTCB->IPScr;
308 1 pIPHead->Protocol = IP_PROTOCOL_TCP;
309 1 pIPHead->TotalLen = htons(MemHead->pEnd -
310 1 MemHead->pStart + TCP_HEAD_MIN_LEN + IP_HEAD_MIN_LEN); /* pStart point to TCP data */
311 1 pTCPHead->CheckSum = htons(TCPCheckSum(pIPHead,MemHead->pEnd - MemHead->pStart + TCP_HEAD_MIN_LEN));
312 1
313 1 /* send packet */
314 1 MemHead->pStart = (BYTE DT_XDATA *)pIPHead; /* dec pStart */
315 1 IPOutput(MemHead);
316 1
317 1 /*
318 1 * renew tcb
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -