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

📄 tcp.lst

📁 基于51单片机和RTL8019以太网控制器的嵌入式以太网控制程序。
💻 LST
📖 第 1 页 / 共 5 页
字号:
C51 COMPILER V8.02   TCP                                                                   09/21/2006 20:16:32 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN TCP.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE TCP.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #include <general.h>
   2          
   3          extern xdata union IP_address temp_ip_address;                             //用于存放临时IP地址
   4          extern xdata union Ethernet_address my_ethernet_address;           //本机的以太网地址
   5          extern xdata union IP_address my_ip_address;                               //本机的ip地址
   6          extern xdata union IP_address mask_ip_address;  
   7          extern xdata union IP_address gateway_ip_address;
   8          extern unsigned int frameindex;
   9          extern xdata union netcard rxdnet;
  10          xdata union netcard TCPSend;                                               // 用于TCP发送缓冲区                                            //IP包的序列号
  11          xdata union IP_address my_ServerIP;
  12          //-------------跟超时重发有关的设置------------------------
  13          
  14          TCPBUF xdata Queen[QUEENLEN];                           //允许有QUEENLEN个数据包在队列里
  15          unsigned char xdata  TCPBuf[BUFLEN];            //缓冲区
  16          
  17          // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes)
  18          unsigned char code opt[10] = {
  19          0x02, 0x04, 0x05, 0xB4,
  20          0x01, 0x01,
  21          0x04, 0x02};
  22          
  23          
  24          // 最大5个连接
  25          CONNECTION xdata  conxn[NO_CONNECTION];
  26          
  27          //初始化序号,根据时间在改变
  28          unsigned long  xdata initial_sequence_nr;
  29          unsigned char idata just_closed; // Keeps track of when a conxn closed
  30          
  31          
  32          xdata struct wait arpwait;              //用于等待ARP.
  33          
  34          
  35          xdata union sw Server_PORT;
  36          //#define HTTP_PORT                                     3330
  37          
  38          
  39          xdata union IP_address sender_ipaddr;                              //保存发送者的IP地址
  40          unsigned int xdata sender_tcpport;                                         //保存发送者端口
  41          unsigned char idata debug;                              //用于调试
  42          
  43          
  44          extern unsigned char data WriteBuf;     //写内容
  45          extern unsigned char data addr0,addr1;  //地址
  46          sbit P11=P1^1;
  47          //------------------------------------------------------------------------
  48          //函数功能:生成TCP包CRC校验
  49          //
  50          //入参:发送区指针,TCP包的长度(包括头部)
  51          //
  52          //
  53          //
  54          //
  55          //作者:
C51 COMPILER V8.02   TCP                                                                   09/21/2006 20:16:32 PAGE 2   

  56          //
  57          //注意:
  58          //
  59          //
  60          //注释:        Mingtree
  61          //日期:        2004-11-10
  62          //------------------------------------------------------------------------
  63          void createtcpcrc(union netcard xdata *pTxdnet,unsigned int len)//生成TCP包CRC校验
  64          {
  65   1      
  66   1              unsigned long sum;
  67   1              unsigned int result;
  68   1          unsigned int result2;
  69   1              unsigned char hdr_len;
  70   1      
  71   1      
  72   1              //计算检验和,包括伪头部,TCP头部,数据
  73   1              // Compute checksum including 12 bytes of pseudoheader
  74   1              // Must pre-fill 2 items in ip header to do this
  75   1              //计算TCP头部长度
  76   1              hdr_len=(pTxdnet->tcpframe.offset)>>2;
  77   1      
  78   1      
  79   1              //-------------------------------------------------------------
  80   1              //                              算法1
  81   1      
  82   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message
  83   1              sum = (unsigned long)checksum(&(pTxdnet->ippacket.ippacket[6]) , 8 + len);
  84   1      
  85   1              // Add in the rest of pseudoheader which is
  86   1              // protocol id and TCP segment length
  87   1              sum += (unsigned long)0x0006;
  88   1              sum += (unsigned long)len;
  89   1      
  90   1              // In case there was a carry, add it back around
  91   1              result2 = (unsigned int)(sum + (sum >> 16));
  92   1      //      pTxdnet->tcpframe.crc = ~result;
  93   1          result2=~result2;
  94   1      
  95   1              //-----------------------------------------------------------
  96   1      
  97   1      
  98   1              //-----------------------------------------------------------
  99   1              //                算法2
 100   1      
 101   1              pTxdnet->ipframe.ttl=0;
 102   1              pTxdnet->ipframe.crc=len;
 103   1              pTxdnet->ipframe.protocal=0x0006;
 104   1      
 105   1              result=checksum(&(pTxdnet->ippacket.ippacket[4]),12+ len);
 106   1              pTxdnet->tcpframe.crc=result;
 107   1              //-----------------------------------------------------------
 108   1      
 109   1      }
 110          //------------------------------------------------------------------------
 111          //函数功能:对tcp头进行校验,错误返回0,正确返回1
 112          //
 113          //入参:        无
 114          //
 115          //
 116          //
 117          //
C51 COMPILER V8.02   TCP                                                                   09/21/2006 20:16:32 PAGE 3   

 118          //作者:
 119          //
 120          //注意:
 121          //
 122          //
 123          //注释:        Mingtree
 124          //日期:        2004-11-10
 125          //------------------------------------------------------------------------
 126          unsigned char verifytcpcrc(union netcard xdata *pRxdnet)//对tcp头进行校验,错误返回0,正确返回1
 127          {
 128   1              unsigned int crc;
 129   1      
 130   1              pRxdnet->ipframe.ttl=0;
 131   1                      pRxdnet->ipframe.protocal=0x0006;
 132   1              //将IP包的16位首部检验和替换成TCP包的长度
 133   1              pRxdnet->ipframe.crc=pRxdnet->ipframe.totallength-(pRxdnet->ipframe.verandihl&0x0f)*4;
 134   1              crc=checksum(&(pRxdnet->ippacket.ippacket[4]),pRxdnet->ipframe.crc+12);
 135   1      
 136   1              if(crc==0) return (1);
 137   1              return(0);
 138   1      }
 139          //------------------------------------------------------------------------
 140          //函数功能:发送IP包
 141          //
 142          //入参:        发送缓冲区,接收方IP地址,协议类型,低层协议数据包长度(包括头部)
 143          //
 144          //
 145          //
 146          //
 147          //作者:
 148          //
 149          //注意:
 150          //
 151          //
 152          //注释:        Mingtree
 153          //日期:        2004-11-29
 154          //------------------------------------------------------------------------
 155          void ip_send(union netcard xdata *pTxdnet,union IP_address ip, unsigned char proto_id, unsigned int len)
 156          {
 157   1              xdata union Ethernet_address  temp;
 158   1              //构建以太网包
 159   1              pTxdnet->etherframe.protocal=0x0800;
 160   1              pTxdnet->etherframe.uSourceID[0]=my_ethernet_address.words[0];
 161   1              pTxdnet->etherframe.uSourceID[1]=my_ethernet_address.words[1];
 162   1              pTxdnet->etherframe.uSourceID[2]=my_ethernet_address.words[2];
 163   1              pTxdnet->ipframe.verandihl=0x45;
 164   1              pTxdnet->ipframe.typeofserver=0x00;
 165   1              pTxdnet->ipframe.totallength=20+len;
 166   1          pTxdnet->ipframe.ttl=0x80;
 167   1              pTxdnet->ipframe.frameindex=frameindex;
 168   1              frameindex++;
 169   1              pTxdnet->ipframe.segment=0x0000;
 170   1              pTxdnet->ipframe.protocal=proto_id;
 171   1          pTxdnet->ipframe.crc=0;
 172   1          pTxdnet->ipframe.destip[0]=ip.words[0];
 173   1          pTxdnet->ipframe.destip[1]=ip.words[1];
 174   1          pTxdnet->ipframe.sourceip[0]=my_ip_address.words[0];
 175   1          pTxdnet->ipframe.sourceip[1]=my_ip_address.words[1];
 176   1          pTxdnet->ipframe.crc=createipheadcrc(pTxdnet);
 177   1      
 178   1              //判断对方IP是否在同一网段内

⌨️ 快捷键说明

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