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

📄 tcp.lst

📁 tcpip.rar 是一个51控制8019的程序,我已用于商用.还很稳定,有兴趣的可以看一下,对TCP IP UDP ICMP ARP RARP HTTP均可以实现.
💻 LST
📖 第 1 页 / 共 3 页
字号:
 227   5                  }
 228   4               }
 229   3            }
 230   2         }
 231   1      }
 232          
 233          
 234          
 235          
 236          //------------------------------------------------------------------------
 237          // This runs every 0.5 seconds.  If the connection has had no activity
 238          // it initiates closing the connection.
 239          //
 240          //------------------------------------------------------------------------
 241          void tcp_inactivity(void)
C51 COMPILER V7.06   TCP                                                                   05/19/2004 15:18:11 PAGE 5   

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

 304   1      
 305   1              if (debug) printf("TCP: Msg rcvd with good cksum\n");
 306   1         
 307   1              // See if message is for http server
 308   1              if (tcp->dest_port != HTTP_PORT)        
 309   1         {
 310   2            if (debug)
 311   2            {
 312   3               printf("TCP: Error, msg to port\n ");
 313   3               memset(text, 0, 10);
 314   3               itoa(tcp->dest_port, text, 10);
 315   3               printf(text);
 316   3                         printf("\n");
 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) printf("TCP: Rcvd msg from existing conxn\n");
 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                  printf("TCP: New connection ");
 363   4                  memset(text, 0, 10);
 364   4                  itoa((UINT)nr, text, 10);
 365   4                  printf(text);
C51 COMPILER V7.06   TCP                                                                   05/19/2004 15:18:11 PAGE 7   

 366   4                                 printf("\n");
 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) printf("TCP: Error in assigning conxn number\n");
 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         if (tcp->sequence > 0xFFFFFF00L) 
 384   1         {
 385   2            if (debug) printf("TCP: Rcvd a high sequence number\n");
 386   2                      conxn[nr].ipaddr = 0;                   
 387   2                      tcp_send(FLG_RST, 20, NO_CONNECTION);
 388   2                      return;         
 389   2         }
 390   1                 
 391   1         // Handle messages whose action is mostly independent of state
 392   1         // such as RST, SYN, and segment with no ACK.  That way the
 393   1              // state machine below does not need to worry about it.
 394   1         if (tcp->flags & FLG_RST)
 395   1         {
 396   2            // An RST does not depend on state at all.  And it does
 397   2            // not count as data so do not send an ACK here.  Close
 398   2            // connection
 399   2                      if (debug) printf("TCP: Rcvd a reset\n");
 400   2            conxn[nr].ipaddr = 0;
 401   2            return;
 402   2         }
 403   1              
 404   1              else if (tcp->flags & FLG_SYN)
 405   1              {
 406   2                 // A SYN segment only makes sense if connection is in LISTEN 
 407   2                 if ((conxn[nr].state != STATE_LISTEN) &&
 408   2                (conxn[nr].state != STATE_CLOSED))
 409   2                      {
 410   3                              if (debug) printf("TCP: Error, rcvd bogus SYN\n");
 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) printf("TCP: Error, rcvd segment has no ACK\n");
 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.06   TCP                                                                   05/19/2004 15:18:11 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
 458   3               // Since timestamps are optional and we do not use
 459   3               // them, do not have to send them 

⌨️ 快捷键说明

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