📄 tcp.lst
字号:
179 1 if((ip.dwords & mask_ip_address.dwords) != (my_ip_address.dwords & mask_ip_address.dwords))
C51 COMPILER V8.02 TCP 09/21/2006 20:16:32 PAGE 4
180 1 {
181 2 // Use ARP to get hardware address to send this to
182 2 if(findmacadr(gateway_ip_address,&temp))
183 2 {
184 3 //填写目的MAC
185 3 pTxdnet->etherframe.uDestID[0]=temp.words[0];
186 3 pTxdnet->etherframe.uDestID[1]=temp.words[1];
187 3 pTxdnet->etherframe.uDestID[2]=temp.words[2];
188 3
189 3 send_packet(pTxdnet,34+len);
190 3 }
191 2 else
192 2 {
193 3 // Fill in the destination information so ehrn the ARP response
194 3 // arrives we can identify it and know what to do when we get it
195 3 arpwait.buf = pTxdnet;
196 3 arpwait.ipaddr.dwords = ip.dwords;
197 3 arpwait.proto_id = proto_id;
198 3 arpwait.len = len;
199 3 arpwait.timer = ARP_TIMEOUT;
200 3 arpwait.wait_arp=TRUE;
201 3 arp_request(gateway_ip_address);
202 3 }
203 2 }
204 1 else
205 1 {
206 2 if(findmacadr(ip,&temp))
207 2 {
208 3 //填写目的MAC
209 3 pTxdnet->etherframe.uDestID[0]=temp.words[0];
210 3 pTxdnet->etherframe.uDestID[1]=temp.words[1];
211 3 pTxdnet->etherframe.uDestID[2]=temp.words[2];
212 3
213 3 send_packet(pTxdnet,34+len);
214 3 }
215 2 else
216 2 {
217 3 // Fill in the destination information so ehrn the ARP response
218 3 // arrives we can identify it and know what to do when we get it
219 3 arpwait.buf = pTxdnet;
220 3 arpwait.ipaddr.dwords = ip.dwords;
221 3 arpwait.proto_id = proto_id;
222 3 arpwait.len = len;
223 3 arpwait.timer = ARP_TIMEOUT;
224 3 arpwait.wait_arp=TRUE;
225 3 arp_request(ip);
226 3 }
227 2 }
228 1
229 1 }
230 //------------------------------------------------------------------------
231 //函数功能:发送TCP包
232 //
233 //入参:flags:TCP包的标志,index_conn:表示哪个连接发数据,hdr_len:整个TCP首部的长度
234 //
235 //
236 //
237 //
238 //作者:
239 //
240 //注意:
241 //
C51 COMPILER V8.02 TCP 09/21/2006 20:16:32 PAGE 5
242 //
243 //注释: Mingtree
244 //日期: 2004-11-29
245 //------------------------------------------------------------------------
246 void tcp_send(union netcard xdata *pTxdnet,unsigned char flags, unsigned char hdr_len, unsigned char index
-_conn)
247 {
248 1 union IP_address dest;
249 1 unsigned char i;
250 1 // If no connection, then message is probably a reset
251 1 // message which goes back to the sender
252 1 // Otherwise, use information from the connection.
253 1 if (index_conn == NO_CONNECTION)
254 1 {
255 2 pTxdnet->tcpframe.sourceport = Server_PORT.word;
256 2 pTxdnet->tcpframe.destport = sender_tcpport;
257 2 pTxdnet->tcpframe.seqnumber = 0;
258 2 pTxdnet->tcpframe.acknumber = 0;
259 2 dest.dwords = sender_ipaddr.dwords;
260 2 }
261 1 else if (index_conn < NO_CONNECTION)
262 1 {
263 2 // This message is to connected port
264 2 if(conxn[index_conn].socket_type==SERVER)
265 2 pTxdnet->tcpframe.sourceport = Server_PORT.word;
266 2 if(conxn[index_conn].socket_type==CLIENT)
267 2 pTxdnet->tcpframe.sourceport = LOCAL_PORT;
268 2 pTxdnet->tcpframe.destport = conxn[index_conn].port;
269 2 pTxdnet->tcpframe.seqnumber = conxn[index_conn].my_sequence;
270 2 pTxdnet->tcpframe.acknumber = conxn[index_conn].his_sequence;
271 2 dest.dwords = conxn[index_conn].ip.dwords;
272 2 }
273 1 else
274 1 {
275 2 // if (debug) PrintStr("TCP: Oops, sock nr out of range\r");
276 2 return;
277 2 }
278 1
279 1 // Total segment length = header length
280 1
281 1 // Insert header len
282 1 pTxdnet->tcpframe.offset = hdr_len << 2;
283 1 pTxdnet->tcpframe.control = flags;
284 1 pTxdnet->tcpframe.window = WNDSIZE;
285 1 pTxdnet->tcpframe.crc = 0;
286 1 pTxdnet->tcpframe.urg = 0;
287 1
288 1 // Sending SYN with header options
289 1 if (hdr_len == 28)
290 1 {
291 2 for(i=0;i<8;i++)
292 2 pTxdnet->tcpframe.tcpdata[i]=opt[i];
293 2 }
294 1 //计算检验和,包括伪头部,TCP头部,数据
295 1 // Compute checksum including 12 bytes of pseudoheader
296 1 // Must pre-fill 2 items in ip header to do this
297 1 pTxdnet->ipframe.sourceip[0] = my_ip_address.words[0];
298 1 pTxdnet->ipframe.sourceip[1] = my_ip_address.words[1];
299 1
300 1 pTxdnet->ipframe.destip[0] = dest.words[0];
301 1 pTxdnet->ipframe.destip[1] = dest.words[1];
302 1
C51 COMPILER V8.02 TCP 09/21/2006 20:16:32 PAGE 6
303 1 createtcpcrc(pTxdnet,hdr_len);
304 1
305 1 // if (debug) PrintStr("TCP: Sending msg to IP layer\r");
306 1 ip_send(pTxdnet, dest, TCP_TYPE, (unsigned int)hdr_len);
307 1 }
308
309
310 //------------------------------------------------------------------------
311 //函数功能:接收TCP包,根据接收到的包来改变连接的状态
312 //
313 //入参: 无
314 //
315 //
316 //
317 //
318 //作者:
319 //
320 //注意:
321 //
322 //
323 //注释: Mingtree
324 //日期: 2004-11-29
325 //修改:2006-02-13,假如对 对方保活探测的相应,即对对方序号小于常值-1的ack发送正常的ack
326 //------------------------------------------------------------------------
327 void tcp_rcve(union netcard xdata *pRxdnet)
328 {
329 1 unsigned int data_len;
330 1 unsigned char i,index,j,header_len;
331 1 unsigned long seq , packACK,packSeq;
332 1 //对tcp头进行校验
333 1 if(verifytcpcrc(pRxdnet))
334 1 {
335 2 // crccheck(pRxdnet);
336 2 //校验正确
337 2
338 2 //取得对方的IP地址,端口号
339 2 sender_ipaddr.words[0]=pRxdnet->ipframe.sourceip[0];
340 2 sender_ipaddr.words[1]=pRxdnet->ipframe.sourceip[1];
341 2 sender_tcpport=pRxdnet->tcpframe.sourceport;
342 2
343 2 // 判断该TCP包是否是来自已连接的机器
344 2 for(i=0;i<NO_CONNECTION;i++)
345 2 {
346 3 if ((sender_ipaddr.dwords==conxn[i].ip.dwords) &&
347 3 (sender_tcpport==conxn[i].port))
348 3 {
349 4 index=i; //保留序号
350 4 // if (debug) PrintStr("TCP: Rcvd msg from existing conxn\r");
351 4 break;
352 4 }
353 3 }
354 2 //该TCP包来自未连接的机器
355 2 if (NO_CONNECTION==i)
356 2 {
357 3 if (pRxdnet->tcpframe.control & FLG_SYN)
358 3 {
359 4 // Find first unused connection (one with IP = 0)
360 4 for (j=0; j < NO_CONNECTION; j++)
361 4 {
362 5 if (!conxn[j].bUsed)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -