📄 tcp.lst
字号:
C51 COMPILER V7.07 TCP 01/14/2009 14:46:37 PAGE 1
C51 COMPILER V7.07, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN .\TCP.obj
COMPILER INVOKED BY: D:\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
55 {
C51 COMPILER V7.07 TCP 01/14/2009 14:46:37 PAGE 2
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 {
117 1 struct SPacketQueue DT_XDATA *pQ;
C51 COMPILER V7.07 TCP 01/14/2009 14:46:37 PAGE 3
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;
179 1
C51 COMPILER V7.07 TCP 01/14/2009 14:46:37 PAGE 4
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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -