⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp.lst

📁 Keil_C51程序,C8051实现的TCP/IP功能源码
💻 LST
📖 第 1 页 / 共 3 页
字号:
 226   5                  }
 227   4               }
 228   3            }
 229   2         }
 230   1      }
 231          
 232          
 233          
 234          
 235          //------------------------------------------------------------------------
 236          // This runs every 0.5 seconds.  If the connection has had no activity
 237          // it initiates closing the connection.
 238          // 这个程序每0.5秒运行。如果连接没有活动,就关闭连接。
 239          //------------------------------------------------------------------------
 240          void tcp_inactivity(void)
 241          {
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 5   

 242   1              UCHAR idata nr;
 243   1         
 244   1              // Look for active connections in the established state
 245   1              for (nr = 0; nr < 5; nr++)
 246   1              {
 247   2              if ((conxn[nr].ipaddr != 0) && 
 248   2                (conxn[nr].state == STATE_ESTABLISHED) &&
 249   2                (conxn[nr].inactivity))
 250   2              {
 251   3                      // Decrement the timer and see if it hit 0
 252   3                      conxn[nr].inactivity--;
 253   3                      if (conxn[nr].inactivity == 0)
 254   3                      {
 255   4                  // Inactivity timer has just timed out.
 256   4                  // Initiate close of connection
 257   4                      tcp_send((FLG_ACK | FLG_FIN), 20, nr);
 258   4                      conxn[nr].my_sequence++;    // For my FIN
 259   4                      conxn[nr].state = STATE_FIN_WAIT_1;
 260   4                      if (debug) SendCommString("TCP: Entered FIN_WAIT_1 state\r");   
 261   4                      }
 262   3              }
 263   2              }
 264   1      }
 265          
 266          
 267          //------------------------------------------------------------------------
 268          // This handles incoming TCP messages and manages the TCP state machine
 269          // Note - both the SYN and FIN flags consume a sequence number.
 270          // See "TCP/IP Illustrated, Volume 1" Sect 18.6 for info on TCP states
 271          // See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
 272          // 处理收到的TCP信息和管理TCP状态机。
 273          //------------------------------------------------------------------------
 274          void tcp_rcve(UCHAR xdata * inbuf, UINT len)
 275          {
 276   1              UCHAR idata i, j, nr;
 277   1              UINT idata result, header_len, data_len;
 278   1              TCP_HEADER xdata * tcp;
 279   1              IP_HEADER xdata * ip;
 280   1              ULONG idata sum;
 281   1         
 282   1              // IP header is always 20 bytes so message starts at index 34      
 283   1              tcp = (TCP_HEADER xdata *)(inbuf + 34);
 284   1              ip = (IP_HEADER xdata *)(inbuf + 14);
 285   1                                         
 286   1              // Compute TCP checksum including 12 byte pseudoheader
 287   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message 
 288   1              sum = (ULONG)cksum(inbuf + 26, 8 + len);
 289   1                      
 290   1              // Add in the rest of pseudoheader which is
 291   1              // protocol id and TCP segment length
 292   1              sum += (ULONG)0x0006;     
 293   1              sum += (ULONG)len;
 294   1      
 295   1              // In case there was a carry, add it back around
 296   1              result = (UINT)(sum + (sum >> 16));
 297   1                      
 298   1              if (result != 0xFFFF)
 299   1              {
 300   2                      if (debug) SendCommString("TCP: Error, bad cksum\r");
 301   2                      return;
 302   2              }
 303   1      
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 6   

 304   1              if (debug) SendCommString("TCP: Msg rcvd with good cksum\r");
 305   1         
 306   1              // See if message is for http server
 307   1              // 需要HTTP服务,
 308   1              if (tcp->dest_port != HTTP_PORT)        
 309   1              {
 310   2              if (debug)
 311   2              {
 312   3                      SendCommString("TCP: Error, msg to port ");
 313   3                      memset(text, 0, 10);
 314   3                      itoa(tcp->dest_port, text, 10);
 315   3                      SendCommString(text);
 316   3                              SendCommString("\r");
 317   3              }
 318   2              tcp_send(FLG_RST, 20, NO_CONNECTION);
 319   2              return;
 320   2              }
 321   1         
 322   1              // Capture sender's IP address and port number
 323   1              sender_ipaddr = ip->source_ipaddr;
 324   1              sender_tcpport = tcp->source_port;
 325   1         
 326   1              // See if the TCP segment is from someone we are already
 327   1              // connected to. 
 328   1              for (i=0; i < 5; i++)
 329   1              {
 330   2              if ((ip->source_ipaddr == conxn[i].ipaddr) &&
 331   2               (tcp->source_port == conxn[i].port))
 332   2              {   
 333   3                      nr = i;
 334   3                      if (debug) SendCommString("TCP: Rcvd msg from existing conxn\r");
 335   3                      break;
 336   3              }       
 337   2              }       
 338   1         
 339   1              // If i = 5, we are not connected. If it is a SYN then assign
 340   1              // a temporary conection  to it for processing
 341   1              if (i == 5)
 342   1              {
 343   2              if (tcp->flags & FLG_SYN)
 344   2              {
 345   3                      // Find first unused connection (one with IP = 0) 
 346   3                      for (j=0; j < 5; j++)
 347   3                      {
 348   4                      if (conxn[j].ipaddr == 0)
 349   4                      {
 350   5                              nr = j;
 351   5                      // Initialize new connection
 352   5                              conxn[nr].state = STATE_LISTEN;
 353   5                              break;
 354   5                      }
 355   4                      }
 356   3            
 357   3                      // If all connections are used then drop msg
 358   3                      if (j == 5) return;
 359   3               
 360   3                      if (debug)
 361   3                      {
 362   4                      SendCommString("TCP: New connection ");
 363   4                      memset(text, 0, 10);
 364   4                      itoa((UINT)nr, text, 10);
 365   4                      SendCommString(text);
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 7   

 366   4                                      SendCommString("\r");
 367   4                      }
 368   3              }
 369   2              }
 370   1      
 371   1      
 372   1              // By now we should have a connection number in range of 0-4
 373   1              // Do a check to avoid any chance of exceeding size of struct
 374   1              if (nr > 4)
 375   1              {
 376   2              if (debug) SendCommString("TCP: Error in assigning conxn number\r");
 377   2              return;
 378   2              }
 379   1      
 380   1              // Eventually put in protection against wrapping sequence
 381   1              // numbers, for now make the client start over if his
 382   1              // sequence number is close to wrapping
 383   1              // 保护
 384   1              if (tcp->sequence > 0xFFFFFF00L) 
 385   1              {
 386   2              if (debug) SendCommString("TCP: Rcvd a high sequence number\r");
 387   2                      conxn[nr].ipaddr = 0;                   
 388   2                      tcp_send(FLG_RST, 20, NO_CONNECTION);
 389   2                      return;         
 390   2              }
 391   1                 
 392   1              // Handle messages whose action is mostly independent of state
 393   1              // such as RST, SYN, and segment with no ACK.  That way the
 394   1              // state machine below does not need to worry about it.
 395   1              if (tcp->flags & FLG_RST)
 396   1              {
 397   2                      // An RST does not depend on state at all.  And it does
 398   2              // not count as data so do not send an ACK here.  Close
 399   2              // connection
 400   2                      if (debug) SendCommString("TCP: Rcvd a reset\r");
 401   2              conxn[nr].ipaddr = 0;
 402   2              return;
 403   2              }
 404   1              
 405   1              else if (tcp->flags & FLG_SYN)
 406   1              {
 407   2                      // A SYN segment only makes sense if connection is in LISTEN 
 408   2                      if ((conxn[nr].state != STATE_LISTEN) && (conxn[nr].state != STATE_CLOSED))
 409   2                      {
 410   3                              if (debug) SendCommString("TCP: Error, rcvd bogus SYN\r");
 411   3                              conxn[nr].ipaddr = 0;                   
 412   3                              tcp_send(FLG_RST, 20, NO_CONNECTION);
 413   3                              return;         
 414   3                      }
 415   2              }
 416   1              
 417   1              else if ((tcp->flags & FLG_ACK) == 0)
 418   1              {
 419   2                      // Incoming segments except SYN or RST must have ACK bit set
 420   2                      // See TCP/IP Illustrated, Vol 2, Page 965
 421   2            // Drop segment but do not send a reset
 422   2                      if (debug) SendCommString("TCP: Error, rcvd segment has no ACK\r");
 423   2                      return;
 424   2              }
 425   1                 
 426   1              // Compute length of header including options, and from that
 427   1              // compute length of actual data
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 8   

 428   1              header_len =  (tcp->flags & 0xF000) >> 10;
 429   1              data_len = len - header_len;
 430   1      
 431   1      
 432   1               
 433   1              // Handle TCP state machine for this connection
 434   1              switch (conxn[nr].state)
 435   1              {
 436   2              case STATE_CLOSED://
 437   2              case STATE_LISTEN://
 438   2                  
 439   2              // If incoming segment contains SYN and no ACK, then handle 
 440   2              if ((tcp->flags & FLG_SYN) && ((tcp->flags & FLG_ACK) == 0))
 441   2              {
 442   3                      // Capture his starting sequence number and generate
 443   3                      // my starting sequence number
 444   3                      // Fill in connection information
 445   3                      conxn[nr].ipaddr = ip->source_ipaddr;
 446   3                      conxn[nr].port = tcp->source_port;
 447   3                      conxn[nr].state = STATE_LISTEN;
 448   3                      conxn[nr].his_sequence = 1 + tcp->sequence;
 449   3                      conxn[nr].his_ack = tcp->ack_number;
 450   3               
 451   3                      // Use system clock for initial sequence number
 452   3                      EA = 0;
 453   3                      conxn[nr].my_sequence = initial_sequence_nr;
 454   3                      initial_sequence_nr += 64000L;
 455   3                      EA = 1;                  
 456   3                        
 457   3                      // Send header options with the next message

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -