📄 tcp.lst
字号:
124 {
125 // Send reset and close connection
126 // if (debug) serial_send("TCP: Timeout, sending reset\r");
127 tcp_ReBack(FLG_RST, 20, nr);
128 StrConnection_buf[nr].ipaddr = 0;
129 return;
130 }
131 else
132 {
133 // Socket is in ESTABLISHED state. First make sure his
134 // ack number is not bogus.
135 if (StrConnection_buf[nr].his_TRUECODE > StrConnection_buf[nr].My_SERIESNUM)
136 {
137 // Send reset and close connection
138 // if (debug) serial_send("TCP: Timeout, sending reset\r");
139 tcp_ReBack(FLG_RST, 20, nr);
140 StrConnection_buf[nr].ipaddr = 0;
141 return;
142 }
143
144 // We always increment our sequence number immediately
145 // after sending, so the ack number from the other end
146 // should be equal to our sequence number. If it is less,
147 // it means he lost some of our data.
148 if (StrConnection_buf[nr].his_TRUECODE < StrConnection_buf[nr].My_SERIESNUM)
149 {
150 retries++;
151 if (retries <= 2)
152 {
153 // The only thing we send is a web page, and it looks
154 // like other end did not get it, so resend
155 // but do not increase my sequence number
156 // if (debug) serial_send("TCP: Timeout, resending data\r");
157 // http_server(StrConnection_buf[nr].query, 0, nr, 1);
158 StrConnection_buf[nr].inactivity = INACTIVITY_TIME;
159 }
160 else
161 {
162 // if (debug) serial_send("TCP: Giving up, sending reset\r");
163 // Send reset and close connection
164 tcp_ReBack(FLG_RST, 20, nr);
165 StrConnection_buf[nr].ipaddr = 0;
166 }
167 }
168 }
169 }
170 }
171 }
172 }
173 */
174 /*********************************************************************
175 函数名: void IpReceive(void)
176 功能: Ip处理,因为是接收一帧处理一帧,不用返回成功失败
177 输入: 接收的帧是Ip帧
178 输出: 处理IP分组
C51 COMPILER V8.18 TCP 08/02/2010 19:52:46 PAGE 4
179 返回: None
180 日期: 2004/12/20
181 *********************************************************************/
182 UWORK16 cksum(UWORK8 xdata *check,UWORK16 length) //计算校验和
183 {
184 1 UWORK32 sum=0;
185 1 UWORK16 i;
186 1 UWORK16 xdata *ptr;
187 1 ptr=(UWORK16 xdata *)check;
188 1 for (i=0;i<(length)/2;i++)
189 1 {
190 2 sum+=*ptr++;
191 2 }
192 1 if (length&0x01)//表示长度为单数
193 1 {
194 2 sum=sum+((*ptr)&0xff00);
195 2 }
196 1 sum=(sum&0xffff)+((sum>>16)&0xffff);//高16位和低16位相加
197 1 if(sum&0xffff0000)
198 1 {//表示有进位
199 2 sum++;
200 2 }
201 1 return ( (UWORK16)((sum)&0xffff));
202 1 // return ( (UWORK16)(~((sum)&0xffff)));
203 1 }
204
205 /*********************************************************************
206 函数名: void tcp_ReBack(UWORK16 flags, UWORK16 hdr_len, UWORK8 nr)
207 功能: 发送TCP
208 输入: flags: TCP的标志位, hdr_len: TCP的长度,nr:是接收的次数
209 输出: None
210 返回: None
211 日期: 2004/02/04
212 *********************************************************************/
213 void tcp_ReBack(UWORK16 flags, UWORK16 TCPHead_len, UWORK8 nr)
214 {
215 1 ST_TCP_FORMAT *strTcp;
216 1
217 1 strTcp = (ST_TCP_FORMAT *)&NetSend_buf[20];
218 1
219 1 strTcp->usSourcePort = SourcePort; //源端口
220 1 strTcp->usDesPort = StrConnection_buf[nr].usDesPort;
221 1 strTcp->ucSERIESNUM = StrConnection_buf[nr].My_SERIESNUM; //序号为0
222 1 strTcp->ucTRUECODE = StrConnection_buf[nr].His_SERIESNUM; //确认号为0
223 1 memcpy(&IPDestAddress_buf[0],(UWORK8 *)&StrConnection_buf[nr].ipaddr, 4); //发送的目标IP地址在TCP接收中已
-附值
224 1
225 1 strTcp->ucMOVENUM = (TCPHead_len << 10) | flags; //记算TCP的头长度及相关的标志位
226 1 strTcp->ucWINDOWBIG = 1024; //设置窗口大小
227 1 strTcp->ucTCPCHECK = 0; //先设置校验位为0
228 1 strTcp->ucMUSGPOINT = 0; //紧急指针为0
229 1
230 1 if (nr == NO_CONNECTION)
231 1 {
232 2 strTcp->ucSERIESNUM = 0;
233 2 strTcp->ucTRUECODE = 0;
234 2 }
235 1 if (TCPHead_len == 28) //如果发送的字节为28,那么多余的部分为TCP的很选项
236 1 {
237 2 memcpy(&strTcp->options[0], options_buf, 8); //把默认的TCP选择附值过去
238 2 }
239 1
C51 COMPILER V8.18 TCP 08/02/2010 19:52:46 PAGE 5
240 1 //下面是给IP打包头
241 1 gstIphead.ucVerAndLen = 0x45; //版本号和长度,各占4位
242 1 gstIphead.ucDs = 0; //区分服务
243 1 gstIphead.usTotalLen = IP_HEAD_LEN + TCPHead_len; //头加后面的数据
244 1 gstIphead.usID = ++LocalIpID;
245 1 gstIphead.usSegOffset = 0;
246 1
247 1 gstIphead.ucTTL = 0; // max hops
248 1 gstIphead.ucprotocol = TCP;
249 1 gstIphead.usCheckSum = TCPHead_len; //在计算TCP的校验位时,TCP的长度
-要算两次,此时是借IP的校验位来做第二个TCP的长度
250 1 memcpy(&gstIphead.ucDestIP[0],&IPDestAddress_buf[0],IP_LEN); // 目的IP
251 1 memcpy(&gstIphead.ucSourceIP[0],&IPLocalAddress_buf[0],IP_LEN); // 源IP
252 1 memcpy(&NetSend_buf[0],&gstIphead,20); //把IP的头传给发送缓存区
253 1 strTcp->ucTCPCHECK = 0;
254 1 strTcp->ucTCPCHECK = CheckSum((UWORK16 *)&NetSend_buf[8],TCPHead_len + 12); // 20 = 12个字节伪头 + 8个
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -