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

📄 tcp1.lst

📁 cf8020+cp2200(网络)的驱动实现
💻 LST
字号:
C51 COMPILER V7.09   TCP1                                                                  06/28/2007 09:51:21 PAGE 1   


C51 COMPILER V7.09, COMPILATION OF MODULE TCP1
OBJECT MODULE PLACED IN TCP1.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\TCP1.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\TCP1.lst) OBJECT(TCP1.ob
                    -j)

line level    source

   1          //-----------------------------------------------------------------------------
   2          // Copyright (c) 2002 Jim Brady
   3          // Do not use commercially without author's permission
   4          // Last revised August 2002
   5          // Net TCP.C
   6          //
   7          // This module handles TCP segments
   8          // Refer to RFC 793, 896, 1122, 1323, 2018, 2581
   9          //
  10          // A "connection" is a unique combination of 4 items:  His IP address,
  11          // his port number, my IP address, and my port number.
  12          //
  13          // Note that a SYN and a FIN count as a byte of data, but a RST does
  14          // not count. Neither do any of the other flags.
  15          // See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
  16          //-----------------------------------------------------------------------------
  17          
  18          #include <stdlib.h>
  19          #include <ctype.h>              // toupper
  20          #include "net1.h"
  21          #include "cksum.h"
  22          #include "serial1.h"
  23          #include "ip1.h"
  24          #include "http1.h"
  25          #include "tcp1.h"
  26          #include "utils.h"
  27          
  28          
  29          // These structures keep track of connection information
  30          CONNECTION  conxn[5];
  31          
  32          ulong  initial_sequence_nr;
  33          uint  sender_tcpport;
  34          static ulong  sender_ipaddr;
  35          UCHAR  just_closed; // Keeps track of when a conxn closed
  36          
  37          extern ulong  my_ipaddr;
  38          extern UCHAR  debug;
  39          extern char  text[];
  40          
  41          // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes) 
  42          UCHAR opt[10] = {
  43          0x02, 0x04, 0x05, 0xB4,
  44          0x01, 0x01,
  45          0x04, 0x02};
  46          
  47          
  48          
  49          
  50          //------------------------------------------------------------------------
  51          //  Initialize variables declared in this module
  52          //
  53          //------------------------------------------------------------------------
  54          void init_tcp(void)
C51 COMPILER V7.09   TCP1                                                                  06/28/2007 09:51:21 PAGE 2   

  55          {
  56   1         memset((char*)conxn, 0, sizeof(conxn));
  57   1         just_closed = FALSE;
  58   1         initial_sequence_nr = 1;
  59   1      }
  60          
  61          
  62          
  63          //------------------------------------------------------------------------
  64          // This sends a TCP segments that do not include any data.
  65          // http_send() in the HTTP module is used to send data.
  66          // See "TCP/IP Illustrated, Volume 1" Sect 17.3
  67          //------------------------------------------------------------------------
  68          //输入参数:
  69          //flags: sun,fin,rst,ack等标志以及数据偏移等的设置;
  70          //hdr_len: TCP报文首部长度
  71          //nr: 连接号
  72          //功能:发送不包含任何数据段的TCP报文,比如请求连接或者说释放连接;
  73          //http_send发送包含数据段的TCP报文.
  74          void tcp_send(uint flags, uint hdr_len, UCHAR nr)
  75          {
  76   1         ulong  sum, dest;
  77   1         uint  result;
  78   1         UCHAR  * outbuf;
  79   1         TCP_HEADER  * tcp;
  80   1         IP_HEADER  * ip;          
  81   1         
  82   1         // Allocate memory for entire outgoing message including
  83   1         // eth & IP headers 
  84   1         //TCP层申请发送缓冲区,包括TCP和IP以及Eth首部
  85   1         outbuf = (UCHAR  *)malloc(34 + hdr_len);  //每次都申请内存,但不释放?
  86   1         if (outbuf == NULL)
  87   1         {
  88   2      //      if (debug) serial_send("TCP: Oops, out of memory\n");
  89   2            return;
  90   2         }
  91   1      
  92   1                 
  93   1         tcp = (TCP_HEADER  *)(outbuf + 34);   //IP首部20字节
  94   1         ip = (IP_HEADER  *)(outbuf + 14);     //ethernet v2首部14字节
  95   1      
  96   1         // If no connection, then message is probably a reset
  97   1         // message which goes back to the sender
  98   1         // Otherwise, use information from the connection.
  99   1         if (nr == NO_CONNECTION)
 100   1         {
 101   2            tcp->source_port = HTTP_PORT;
 102   2            tcp->dest_port = sender_tcpport;
 103   2            tcp->sequence = 0;
 104   2            tcp->ack_number = 0;
 105   2            dest = sender_ipaddr;
 106   2         }
 107   1         else if (nr < 5)
 108   1         {
 109   2            // This message is to connected port
 110   2            tcp->source_port = HTTP_PORT;     //源端口
 111   2            tcp->dest_port = conxn[nr].port;  //目的端口
 112   2            tcp->sequence = conxn[nr].my_sequence;      //序号:   本报文段所发数据的第一个字节的序号
 113   2            tcp->ack_number = conxn[nr].his_sequence;   //确认号: 期望收到对方的下一个报文段的数据的第一个字节的
             -序号
 114   2            dest = conxn[nr].ipaddr;
 115   2         }
C51 COMPILER V7.09   TCP1                                                                  06/28/2007 09:51:21 PAGE 3   

 116   1         else
 117   1         {
 118   2      //      if (debug) serial_send("TCP: Oops, sock nr out of range\n");
 119   2                      free(outbuf);
 120   2                      return;
 121   2              }
 122   1      
 123   1         // Total segment length = header length
 124   1            
 125   1         // Insert header len
 126   1         tcp->flags = (hdr_len << 10) | flags;
 127   1         tcp->window = 1024;   //窗口: 用来控制对方发送的数据量.
 128   1         tcp->checksum = 0;
 129   1         tcp->urgent_ptr = 0;  //紧急指针:             
 130   1         
 131   1         // Sending SYN with header options
 132   1         if (hdr_len == 28)
 133   1         {
 134   2            memcpy((char*)tcp->options, opt, 8);
 135   2         }   
 136   1         
 137   1         // Compute checksum including 12 bytes of pseudoheader
 138   1              // Must pre-fill 2 items in ip header to do this
 139   1              ip->dest_ipaddr = dest;
 140   1              ip->source_ipaddr = my_ipaddr;
 141   1      
 142   1      #ifdef __LITTLEENDIAN__
 143   1              tcp->flags = ntohs(tcp->flags);
 144   1              tcp->window = ntohs(tcp->window);
 145   1              tcp->source_port = ntohs(tcp->source_port);
 146   1              tcp->sequence = ntohl(tcp->sequence);   
 147   1              tcp->dest_port = ntohs(tcp->dest_port);
 148   1              tcp->ack_number = ntohl(tcp->ack_number);
 149   1              ip->source_ipaddr = ntohl(my_ipaddr);
 150   1      #endif
 151   1                      
 152   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message 
 153   1              sum = (ulong)cksum(outbuf + 26, 8 + hdr_len);
 154   1                                      
 155   1              // Add in the rest of pseudoheader which is
 156   1              // protocol id and TCP segment length
 157   1              sum += (ulong)0x0006;
 158   1      #ifdef __LITTLEENDIAN__
 159   1      //      hdr_len = ntohs(&hdr_len);
 160   1      #endif
 161   1              sum += (ulong)hdr_len;
 162   1      
 163   1              // In case there was a carry, add it back around
 164   1              result = (uint)(sum + (sum >> 16));
 165   1              tcp->checksum = ~result;
 166   1      
 167   1      #ifdef __LITTLEENDIAN__
 168   1              tcp->checksum  = ntohs(tcp->checksum);
 169   1              dest = ntohl(dest);
 170   1      //      hdr_len = ntohs(&hdr_len);
 171   1      #endif
 172   1         
 173   1      //    if (debug) serial_send("TCP: Sending msg to IP layer\n");
 174   1              
 175   1              ip_send1(outbuf, dest, TCP_TYPE, hdr_len);
 176   1      
 177   1      #ifdef  __LITTLEENDIAN__
C51 COMPILER V7.09   TCP1                                                                  06/28/2007 09:51:21 PAGE 4   

 178   1              free(outbuf);
 179   1      #endif
 180   1      
 181   1          // (Re)start TCP retransmit timer
 182   1      //    conxn[nr].timer = TCP_TIMEOUT;  //重发定时器2
 183   1              conxn[nr].timer = TCP_TIMEOUT*2;
 184   1      }
 185          
 186          
 187          
 188          
 189          //------------------------------------------------------------------------
 190          // This runs every 0.5 seconds.  If the other end has not ACK'd
 191          // everyting we have sent, it re-sends it.  To save RAM space, we 
 192          // regenerate a segment rather than keeping a bunch of segments 
 193          // hanging around eating up RAM.  A connection should not be in an
 194          // opening or closing state when this timer expires, so we simply
 195          // send a reset.
 196          //
 197          //      If a connection is in the ESTABLISHED state when the timer expires
 198          // then we have just sent a web page so re-send the page
 199          //------------------------------------------------------------------------
 200          void tcp_retransmit(void)
 201          {
 202   1      
 203   1         static UCHAR  retries = 0;
 204   1         UCHAR  nr;
 205   1      
 206   1         // Scan through all active connections 
 207   1         for (nr = 0; nr < 5; nr++)
 208   1         {
 209   2            if ((conxn[nr].ipaddr != 0) && (conxn[nr].timer))
 210   2            {
 211   3               // Decrement the timer and see if it hit 0
 212   3               conxn[nr].timer--;
 213   3                       //如果定时器到, 即2秒,  
 214   3               if (conxn[nr].timer == 0)
 215   3               {
 216   4                  // Socket just timed out. If we are not in ESTABLISHED state
 217   4                  // something is amiss so send reset and close connection
 218   4                  //不在Established阶段,发送rst要求,释放连接;
 219   4                  if (conxn[nr].state != STATE_ESTABLISHED)
 220   4                  {
 221   5                     // Send reset and close connection
 222   5      //               if (debug) serial_send("TCP: Timeout, sending reset\n");
 223   5                     tcp_send(FLG_RST, 20, nr);
 224   5                     conxn[nr].ipaddr = 0;
 225   5                     return;
 226   5                  }
 227   4                  else
 228   4                  {
 229   5                     // Socket is in ESTABLISHED state. First make sure his
 230   5                     // ack number is not bogus.
 231   5                     if (conxn[nr].his_ack > conxn[nr].my_sequence) //如果当前连接的对端确认号大于本端发送号,乱

⌨️ 快捷键说明

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