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

📄 tcp.lst

📁 Keil_C51程序,C8051实现的TCP/IP功能源码
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 1   


C51 COMPILER V7.20, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN Tcp.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Tcp.c OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND

line level    source

   1          //-----------------------------------------------------------------------------
   2          // Net TCP.C
   3          //
   4          // This module handles TCP segments
   5          // Refer to RFC 793, 896, 1122, 1323, 2018, 2581
   6          //
   7          // A "connection" is a unique combination of 4 items:  His IP address,
   8          // his port number, my IP address, and my port number.
   9          //
  10          // Note that a SYN and a FIN count as a byte of data, but a RST does
  11          // not count. Neither do any of the other flags.
  12          // See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
  13          //-----------------------------------------------------------------------------
  14          #include <string.h>
  15          #include <stdlib.h>
  16          #include <ctype.h>              // toupper
  17          #include "C8051f.h"
  18          #include "net.h"
  19          #include "cksum.h"
  20          #include "serial.h"
  21          #include "ip.h"
  22          #include "http.h"
  23          #include "tcp.h"
  24          
  25          
  26          // These structures keep track of connection information
  27          CONNECTION xdata conxn[5];
  28          
  29          ULONG   idata initial_sequence_nr;
  30          UINT    xdata sender_tcpport;
  31          static  ULONG xdata sender_ipaddr;
  32          UCHAR   idata just_closed; // Keeps track of when a conxn closed
  33          
  34          extern ULONG code my_ipaddr;
  35          extern UCHAR idata debug;
  36          extern char xdata text[];
  37          
  38          // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes) 
  39          UCHAR code opt[10] = {
  40          0x02, 0x04, 0x05, 0xB4,
  41          0x01, 0x01,
  42          0x04, 0x02};
  43          
  44          
  45          
  46          //------------------------------------------------------------------------
  47          // Initialize variables declared in this module
  48          // 初始化tcp 
  49          //------------------------------------------------------------------------
  50          void init_tcp(void)
  51          {
  52   1         memset(conxn, 0, sizeof(conxn));
  53   1         just_closed = FALSE;
  54   1         initial_sequence_nr = 1;
  55   1      }
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 2   

  56          
  57          //------------------------------------------------------------------------
  58          // This sends a TCP segments that do not include any data.
  59          // http_send() in the HTTP module is used to send data.
  60          // See "TCP/IP Illustrated, Volume 1" Sect 17.3
  61          // 发送一个TCP段,不包括任何数据。http_send()用于发送数据。
  62          //------------------------------------------------------------------------
  63          void tcp_send(UINT flags, UINT hdr_len, UCHAR nr)
  64          {
  65   1              ULONG idata sum, dest;
  66   1              UINT idata result;
  67   1              UCHAR xdata * outbuf;
  68   1              TCP_HEADER xdata * tcp;
  69   1              IP_HEADER xdata * ip;
  70   1                
  71   1         
  72   1              // Allocate memory for entire outgoing message including
  73   1              // eth & IP headers 
  74   1              outbuf = (UCHAR xdata *)malloc(34 + hdr_len);
  75   1              if (outbuf == NULL)
  76   1              {
  77   2              if (debug) SendCommString("TCP: Oops, out of memory\r");
  78   2              return;
  79   2              }
  80   1      
  81   1                 
  82   1              tcp = (TCP_HEADER xdata *)(outbuf + 34);
  83   1              ip = (IP_HEADER xdata *)(outbuf + 14);
  84   1      
  85   1              // If no connection, then message is probably a reset
  86   1              // message which goes back to the sender
  87   1              // Otherwise, use information from the connection.
  88   1              if (nr == NO_CONNECTION)
  89   1              {
  90   2              tcp->source_port = HTTP_PORT;
  91   2              tcp->dest_port = sender_tcpport;
  92   2              tcp->sequence = 0;
  93   2              tcp->ack_number = 0;
  94   2              dest = sender_ipaddr;
  95   2              }
  96   1              else if (nr < 5)
  97   1              {
  98   2              // This message is to connected port
  99   2              tcp->source_port = HTTP_PORT;
 100   2              tcp->dest_port = conxn[nr].port;
 101   2              tcp->sequence = conxn[nr].my_sequence;
 102   2              tcp->ack_number = conxn[nr].his_sequence;
 103   2              dest = conxn[nr].ipaddr;
 104   2              }
 105   1              else
 106   1              {
 107   2              if (debug) SendCommString("TCP: Oops, sock nr out of range\r");
 108   2                      free(outbuf);
 109   2                      return;
 110   2              }
 111   1      
 112   1              // Total segment length = header length
 113   1            
 114   1              // Insert header len
 115   1              tcp->flags = (hdr_len << 10) | flags;
 116   1              tcp->window = 1024;
 117   1              tcp->checksum = 0;
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 3   

 118   1              tcp->urgent_ptr = 0;//
 119   1         
 120   1              // Sending SYN with header options
 121   1              if (hdr_len == 28)
 122   1              {
 123   2              memcpy(&tcp->options, opt, 8);
 124   2              }   
 125   1         
 126   1              // Compute checksum including 12 bytes of pseudoheader
 127   1              // Must pre-fill 2 items in ip header to do this
 128   1              ip->dest_ipaddr = dest;
 129   1              ip->source_ipaddr = my_ipaddr;
 130   1                      
 131   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message 
 132   1              sum = (ULONG)cksum(outbuf + 26, 8 + hdr_len);
 133   1                                      
 134   1              // Add in the rest of pseudoheader which is
 135   1              // protocol id and TCP segment length
 136   1              sum += (ULONG)0x0006;
 137   1              sum += (ULONG)hdr_len;
 138   1      
 139   1              // In case there was a carry, add it back around
 140   1              result = (UINT)(sum + (sum >> 16));
 141   1              tcp->checksum = ~result;
 142   1         
 143   1              if (debug) SendCommString("TCP: Sending msg to IP layer\r");
 144   1              ip_send(outbuf, dest, TCP_TYPE, hdr_len);
 145   1      
 146   1              // (Re)start TCP retransmit timer
 147   1              conxn[nr].timer = TCP_TIMEOUT;
 148   1      }
 149          
 150          
 151          //------------------------------------------------------------------------
 152          // This runs every 0.5 seconds.  If the other end has not ACK'd
 153          // everyting we have sent, it re-sends it.  To save RAM space, we 
 154          // regenerate a segment rather than keeping a bunch of segments 
 155          // hanging around eating up RAM.  A connection should not be in an
 156          // opening or closing state when this timer expires, so we simply
 157          // send a reset.
 158          // 每0.5秒运行一次。如果我们这端发送后另外一端没有没有回复的,将重发。
 159          // 为了保证RAM的空间,我们另开了一段RAM,防止互相重叠。当超时时,
 160          // 一次连接不应该是开或者关状态,因此我们简单的重发一次。
 161          // If a connection is in the ESTABLISHED state when the timer expires
 162          // then we have just sent a web page so re-send the page
 163          // 在连接状态时,当超时,我们发送网页,更新它。
 164          //------------------------------------------------------------------------
 165          void tcp_retransmit(void)
 166          {
 167   1              static UCHAR idata retries = 0;//重试
 168   1              UCHAR idata nr;
 169   1      
 170   1         // Scan through all active connections 
 171   1              for (nr = 0; nr < 5; nr++)
 172   1              {
 173   2                      if ((conxn[nr].ipaddr != 0) && (conxn[nr].timer))
 174   2              {
 175   3                      // Decrement the timer and see if it hit 0
 176   3                      conxn[nr].timer--;
 177   3                      if (conxn[nr].timer == 0)
 178   3                      {
 179   4                  // Socket just timed out. If we are not in ESTABLISHED state
C51 COMPILER V7.20   TCP                                                                   03/07/2006 14:49:13 PAGE 4   

 180   4                  // something is amiss so send reset and close connection
 181   4                      if (conxn[nr].state != STATE_ESTABLISHED)
 182   4                      {
 183   5                      // Send reset and close connection
 184   5                              if (debug) SendCommString("TCP: Timeout, sending reset\r");
 185   5                              tcp_send(FLG_RST, 20, nr);
 186   5                              conxn[nr].ipaddr = 0;
 187   5                              return;
 188   5                      }
 189   4                      else
 190   4                      {
 191   5                      // Socket is in ESTABLISHED state. First make sure his
 192   5                      // ack number is not bogus.
 193   5                      if (conxn[nr].his_ack > conxn[nr].my_sequence)
 194   5                      {
 195   6                              // Send reset and close connection
 196   6                              if (debug) SendCommString("TCP: Timeout, sending reset\r");
 197   6                              tcp_send(FLG_RST, 20, nr);
 198   6                              conxn[nr].ipaddr = 0;
 199   6                              return;
 200   6                      }
 201   5                     
 202   5                      // We always increment our sequence number immediately
 203   5                                      // after sending, so the ack number from the other end
 204   5                                      // should be equal to our sequence number.  If it is less,
 205   5                                      // it means he lost some of our data.
 206   5                      if (conxn[nr].his_ack < conxn[nr].my_sequence)
 207   5                                      {
 208   6                              retries++;
 209   6                                              if (retries <= 2)
 210   6                                              {
 211   7                              // The only thing we send is a web page, and it looks
 212   7                              // like other end did not get it, so resend
 213   7                              // but do not increase my sequence number
 214   7                                      if (debug) SendCommString("TCP: Timeout, resending data\r");
 215   7                                      http_server(conxn[nr].query, 0, nr, 1);
 216   7                                              conxn[nr].inactivity = INACTIVITY_TIME;
 217   7                              }
 218   6                                              else
 219   6                                              {
 220   7                                                      if (debug) SendCommString("TCP: Giving up, sending reset\r");
 221   7                                                      // Send reset and close connection
 222   7                                      tcp_send(FLG_RST, 20, nr);
 223   7                                      conxn[nr].ipaddr = 0;
 224   7                              }
 225   6                      }

⌨️ 快捷键说明

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