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

📄 ip.lst

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


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

line level    source

   1          //-----------------------------------------------------------------------------
   2          // Net IP.C
   3          // This module is the IP layer
   4          // Refer to RFC 791, 1122, and RFC 815 (fragmentation)
   5          //-----------------------------------------------------------------------------
   6          #include <string.h>
   7          #include "reg52.h"
   8          #include "net.h"
   9          #include "cksum.h"
  10          #include "arp.h"
  11          #include "eth.h"
  12          #include "icmp.h"
  13          #include "udp.h"
  14          #include "serial.h"
  15          #include "ip.h"
  16          #include "tcp.h"
  17          
  18          extern UCHAR idata debug;
  19          extern ULONG code my_ipaddr;
  20          WAIT xdata wait;
  21          
  22          
  23          void Delay1ms(unsigned char);
  24          //------------------------------------------------------------------------
  25          // This handles outgoing IP datagrams.  It adds the 20 byte IP header
  26          // and checksum then forwards the IP datagram to the Ethernet layer
  27          // for sending. See "TCP/IP Illustrated, Volume 1" Sect 3.2
  28          // 处理输出的ip数据
  29          //------------------------------------------------------------------------
  30          void ip_send(UCHAR xdata * outbuf, ULONG ipaddr, UCHAR proto_id, UINT len)
  31          {
  32   1              IP_HEADER xdata * ip;
  33   1              UCHAR xdata * hwaddr;
  34   1              static UINT ip_ident = 0;
  35   1         
  36   1              ip = (IP_HEADER xdata *)(outbuf + 14);
  37   1              ip->ver_len = 0x45;          // IPv4 with 20 byte header
  38   1              ip->type_of_service = 0;
  39   1              ip->total_length = 20 + len;
  40   1              ip->identifier = ip_ident++;     // sequential identifier
  41   1              ip->fragment_info = 0;           // not fragmented
  42   1              ip->time_to_live = 32;           // max hops
  43   1              ip->protocol_id = proto_id;      // type of payload
  44   1              ip->header_cksum = 0;
  45   1              ip->source_ipaddr = my_ipaddr;
  46   1         
  47   1              // Outgoing IP address
  48   1              ip->dest_ipaddr = ipaddr;
  49   1      
  50   1              // Compute and insert complement of checksum of ip header
  51   1              // Outgoing ip header length is always 20 bytes
  52   1              ip->header_cksum = ~cksum(outbuf + 14, 20);
  53   1         
  54   1              // Use ARP to get hardware address to send this to
  55   1              // arp 解析以获得地址
C51 COMPILER V7.20   IP                                                                    03/07/2006 14:49:10 PAGE 2   

  56   1              hwaddr = arp_resolve(ip->dest_ipaddr);
  57   1              
  58   1              // Null means that the ARP resolver did not find the IP address
  59   1              // in its cache so had to send an ARP request
  60   1              if (hwaddr == NULL)
  61   1              {
  62   2                      // Fill in the destination information so ehrn the ARP response
  63   2                      // arrives we can identify it and know what to do when we get it
  64   2              wait.buf = outbuf;
  65   2                      wait.ipaddr = ip->dest_ipaddr;
  66   2                      wait.proto_id = proto_id;
  67   2                      wait.len = len;
  68   2                      wait.timer = ARP_TIMEOUT; 
  69   2                  return;
  70   2              }       
  71   1              
  72   1              eth_send(outbuf, hwaddr, IP_PACKET, 20 + len);
  73   1      }
  74          
  75          
  76          //------------------------------------------------------------------------
  77          // This handles incoming IP datagrams from the Ethernet layer
  78          // See "TCP/IP Illustrated, Volume 1" Sect 3.2
  79          //------------------------------------------------------------------------
  80          void ip_rcve(UCHAR xdata * inbuf)
  81          {
  82   1              IP_HEADER xdata * ip;
  83   1              UINT idata header_len, payload_len;
  84   1                      
  85   1         ip = (IP_HEADER xdata *)(inbuf + 14);
  86   1                  
  87   1         // Make sure it is addressed to my IP address
  88   1         if (ip->dest_ipaddr != my_ipaddr) return;
  89   1      
  90   1         // Validate checksum of ip header
  91   1              header_len = 4 * (0x0F & ip->ver_len);
  92   1              payload_len = ip->total_length - header_len;
  93   1         if (cksum(inbuf + 14, header_len) != 0xFFFF)
  94   1              {
  95   2                      if (debug) SendCommString("IP:  Error, cksum bad\r");
  96   2                 return; 
  97   2         }
  98   1              
  99   1              // Make sure incoming message is IP version 4
 100   1              if ((ip->ver_len >> 4) != 0x04)
 101   1              {
 102   2                      if (debug) SendCommString("IP:  Error, not IPv4\r");
 103   2              return;
 104   2              }
 105   1      
 106   1              // Make sure incoming message is not fragmented because
 107   1         // we cannot handle fragmented messages
 108   1         if ((ip->fragment_info & 0x3FFF) != 0)
 109   1         {
 110   2            if (debug) SendCommString("IP:  Error, fragmented msg rcvd\r");
 111   2                 return; 
 112   2         }
 113   1      
 114   1         // At this point we have received a valid IP datagram addressed
 115   1         // to me.  We do not use header options, and do not forward
 116   1         // messages, so in the unlikely event there are header options,
 117   1         // delete them and shift the data down. The advantage is that
C51 COMPILER V7.20   IP                                                                    03/07/2006 14:49:10 PAGE 3   

 118   1         // layers such as UDP and TCP know where their data starts
 119   1              if (header_len > 20)
 120   1              {
 121   2                      if (debug) SendCommString("IP: Rcvd header > 20 bytes\r");
 122   2                              
 123   2            // Use memmove because of overlap
 124   2            memmove(inbuf + 34, inbuf + 14 + header_len, payload_len);
 125   2      
 126   2                      // Adjust info to reflect the move
 127   2                      header_len = 20;
 128   2                      ip->ver_len = 0x45;
 129   2                      ip->total_length = 20 + payload_len;
 130   2              }
 131   1              
 132   1              
 133   1              // Look at protocol ID byte and call the appropriate
 134   1         // function to handle the received message.  See 
 135   1         // "TCP/IP Illustrated, Volume 1" Sect 1.7 and RFC 791
 136   1         // for values for various protocols
 137   1              switch (ip->protocol_id)
 138   1              {
 139   2                      case ICMP_TYPE:
 140   2                      if (debug) SendCommString("IP:  ICMP pkt rcvd\r");
 141   2                      icmp_rcve(inbuf, payload_len);
 142   2                      break;
 143   2      
 144   2              case IGMP_TYPE:
 145   2                      // We cannot handle IGMP messages
 146   2                              if (debug) SendCommString("IP:  Error, IGMP pkt rcvd\r");
 147   2                              break;
 148   2                        
 149   2                      case UDP_TYPE:
 150   2                              if (debug) SendCommString("IP:  UDP pkt rcvd\r");
 151   2                      udp_rcve(inbuf, payload_len);
 152   2                      break;
 153   2      
 154   2                      case TCP_TYPE:   
 155   2                      if (debug) SendCommString("IP:  TCP pkt rcvd\r");
 156   2                    tcp_rcve(inbuf, payload_len);
 157   2                      break;
 158   2      
 159   2            default:
 160   2            if (debug) SendCommString("IP:  Unknown IP proto id rcvd\r");
 161   2            break;
 162   2         }
 163   1      }
 164          
 165          
 166          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    702    ----
   CONSTANT SIZE    =    222    ----
   XDATA SIZE       =     10    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      2      15
   IDATA SIZE       =   ----       4
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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