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

📄 tcp.lst

📁 一个通过8051来控制8019上网的程序 该程序已经通过并成功用于键盘
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V7.06   TCP                                                                   06/26/2004 13:41:26 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN TCP.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE TCP.C LARGE BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          
   2          #include <string.h>
   3          #include <stdlib.h>
   4          #include <ctype.h>              // toupper
   5          #include <reg89C58.h>
   6          #include <stdio.h>
   7          #include "net.h"
   8          #include "cksum.h"
   9          #include "ip.h"
  10          #include "tcp.h"
  11          
  12          
  13          // These structures keep track of connection information
  14          CONNECTION xdata conxn[5];
  15          
  16          ULONG idata initial_sequence_nr;
  17          UINT xdata sender_tcpport;
  18          static ULONG xdata sender_ipaddr;
  19          UCHAR idata just_closed; // Keeps track of when a conxn closed
  20          
  21          extern ULONG data my_ipaddr;
  22          extern UCHAR idata debug;
  23          extern char xdata text[];
  24          
  25          // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes) 
  26          UCHAR code opt[10] = {
  27          0x02, 0x04, 0x05, 0xB4,
  28          0x01, 0x01,
  29          0x04, 0x02};
  30          
  31          
  32          
  33          //------------------------------------------------------------------------
  34          //  Initialize variables declared in this module
  35          //
  36          //------------------------------------------------------------------------
  37          void init_tcp(void)
  38          {
  39   1         memset(conxn, 0, sizeof(conxn));
  40   1         just_closed = FALSE;
  41   1         initial_sequence_nr = 1;
  42   1      }
  43          
  44          
  45          
  46          //------------------------------------------------------------------------
  47          // This sends a TCP segments that do not include any data.
  48          // http_send() in the HTTP module is used to send data.
  49          // See "TCP/IP Illustrated, Volume 1" Sect 17.3
  50          //------------------------------------------------------------------------
  51          void tcp_send(UINT flags, UINT hdr_len, UCHAR nr)
  52          {
  53   1         ULONG idata sum, dest;
  54   1              UINT idata result;
  55   1         UCHAR xdata * outbuf;
C51 COMPILER V7.06   TCP                                                                   06/26/2004 13:41:26 PAGE 2   

  56   1         TCP_HEADER xdata * tcp;
  57   1         IP_HEADER xdata * ip;
  58   1                
  59   1         
  60   1         // Allocate memory for entire outgoing message including
  61   1         // eth & IP headers 
  62   1         outbuf = (UCHAR xdata *)malloc(34 + hdr_len);
  63   1         if (outbuf == NULL)
  64   1         {
  65   2       //     if (debug) printf("TCP: Oops, out of memory\n");
  66   2            return;
  67   2         }
  68   1      
  69   1                 
  70   1         tcp = (TCP_HEADER xdata *)(outbuf + 34);
  71   1         ip = (IP_HEADER xdata *)(outbuf + 14);
  72   1      
  73   1         // If no connection, then message is probably a reset
  74   1         // message which goes back to the sender
  75   1         // Otherwise, use information from the connection.
  76   1         if (nr == NO_CONNECTION)
  77   1         {
  78   2            tcp->source_port = HTTP_PORT;
  79   2            tcp->dest_port = sender_tcpport;
  80   2            tcp->sequence = 0;
  81   2            tcp->ack_number = 0;
  82   2            dest = sender_ipaddr;
  83   2         }
  84   1         else if (nr < 5)
  85   1         {
  86   2            // This message is to connected port
  87   2            tcp->source_port = HTTP_PORT;
  88   2            tcp->dest_port = conxn[nr].port;
  89   2            tcp->sequence = conxn[nr].my_sequence;
  90   2            tcp->ack_number = conxn[nr].his_sequence;
  91   2            dest = conxn[nr].ipaddr;
  92   2         }
  93   1         else
  94   1         {
  95   2       //     if (debug) printf("TCP: Oops, sock nr out of range\n");
  96   2                      free(outbuf);
  97   2                      return;
  98   2              }
  99   1      
 100   1         // Total segment length = header length
 101   1            
 102   1         // Insert header len
 103   1         tcp->flags = (hdr_len << 10) | flags;
 104   1         tcp->window = 1024;
 105   1         tcp->checksum = 0;
 106   1         tcp->urgent_ptr = 0;
 107   1         
 108   1         // Sending SYN with header options
 109   1         if (hdr_len == 28)
 110   1         {
 111   2            memcpy(&tcp->options, opt, 8);
 112   2         }   
 113   1         
 114   1         // Compute checksum including 12 bytes of pseudoheader
 115   1              // Must pre-fill 2 items in ip header to do this
 116   1              ip->dest_ipaddr = dest;
 117   1              ip->source_ipaddr = my_ipaddr;
C51 COMPILER V7.06   TCP                                                                   06/26/2004 13:41:26 PAGE 3   

 118   1                      
 119   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message 
 120   1              sum = (ULONG)cksum(outbuf + 26, 8 + hdr_len);
 121   1                                      
 122   1              // Add in the rest of pseudoheader which is
 123   1              // protocol id and TCP segment length
 124   1              sum += (ULONG)0x0006;
 125   1              sum += (ULONG)hdr_len;
 126   1      
 127   1              // In case there was a carry, add it back around
 128   1              result = (UINT)(sum + (sum >> 16));
 129   1              tcp->checksum = ~result;
 130   1         
 131   1      //   if (debug) printf("TCP: Sending msg to IP layer\n");
 132   1              ip_send(outbuf, dest, TCP_TYPE, hdr_len);
 133   1      
 134   1         // (Re)start TCP retransmit timer
 135   1         conxn[nr].timer = TCP_TIMEOUT;
 136   1      }
 137          
 138          
 139          
 140          
 141          //------------------------------------------------------------------------
 142          // This runs every 0.5 seconds.  If the other end has not ACK'd
 143          // everyting we have sent, it re-sends it.  To save RAM space, we 
 144          // regenerate a segment rather than keeping a bunch of segments 
 145          // hanging around eating up RAM.  A connection should not be in an
 146          // opening or closing state when this timer expires, so we simply
 147          // send a reset.
 148          //
 149          //      If a connection is in the ESTABLISHED state when the timer expires
 150          // then we have just sent a web page so re-send the page
 151          //------------------------------------------------------------------------
 152          void tcp_retransmit(void)
 153          {
 154   1         static UCHAR idata retries = 0;
 155   1         UCHAR idata nr;
 156   1      
 157   1         // Scan through all active connections 
 158   1         for (nr = 0; nr < 5; nr++)
 159   1         {
 160   2            if ((conxn[nr].ipaddr != 0) && (conxn[nr].timer))
 161   2            {
 162   3               // Decrement the timer and see if it hit 0
 163   3               conxn[nr].timer--;
 164   3               if (conxn[nr].timer == 0)
 165   3               {
 166   4                  // Socket just timed out. If we are not in ESTABLISHED state
 167   4                  // something is amiss so send reset and close connection
 168   4                  if (conxn[nr].state != STATE_ESTABLISHED)
 169   4                  {
 170   5                     // Send reset and close connection
 171   5      //               if (debug) printf("TCP: Timeout, sending reset\n");
 172   5                     tcp_send(FLG_RST, 20, nr);
 173   5                     conxn[nr].ipaddr = 0;
 174   5                     return;
 175   5                  }
 176   4                  else
 177   4                  {
 178   5                     // Socket is in ESTABLISHED state. First make sure his
 179   5                     // ack number is not bogus.
C51 COMPILER V7.06   TCP                                                                   06/26/2004 13:41:26 PAGE 4   

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

⌨️ 快捷键说明

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