ip.lst

来自「用c8051f340基于51单片机上网」· LST 代码 · 共 199 行

LST
199
字号
C51 COMPILER V8.08   IP                                                                    11/04/2008 18:45:34 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE IP
OBJECT MODULE PLACED IN Ip.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.exe Ip.c DB OE

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 IP.C
   6          //
   7          // This module is the IP layer
   8          // Refer to RFC 791, 1122, and RFC 815 (fragmentation)
   9          //-----------------------------------------------------------------------------
  10          #include <string.h>
  11          #include "C8051f340.h"
  12          #include "global.h"
  13          ///#include "net.h"
  14          #include "cksum.h"
  15          #include "arp.h"
  16          #include "icmp.h"
  17          #include "udp.h"
  18          #include "tcp.h"
  19          #include "ip.h"
  20          
  21          
  22          extern UCHAR idata debug;
  23          extern ULONG code my_ipaddr;
  24          WAIT xdata wait;
  25          
  26          //-----------------------------------------------------------------------------
  27          // Exported Function Definitions
  28          //-----------------------------------------------------------------------------
  29          extern void CP220x_Send(MACADDRESS* pDestAddr, unsigned char* buffer, unsigned int buffer_length, unsigned
             - int packet_type);
  30          extern unsigned int CP220x_Receive(unsigned char* buffer, unsigned int buffer_length);
  31          
  32          //------------------------------------------------------------------------
  33          // This handles outgoing IP datagrams.  It adds the 20 byte IP header
  34          // and checksum then forwards the IP datagram to the Ethernet layer
  35          // for sending. See "TCP/IP Illustrated, Volume 1" Sect 3.2
  36          //------------------------------------------------------------------------
  37          void ip_send(UCHAR xdata * outbuf, ULONG ipaddr, UCHAR proto_id, UINT len)
  38          {
  39   1         IP_HEADER xdata * ip;
  40   1         UCHAR xdata * hwaddr;
  41   1         static UINT ip_ident = 0;
  42   1         
  43   1         ip = (IP_HEADER xdata *)(outbuf + 14);
  44   1         ip->ver_len = 0x45;          // IPv4 with 20 byte header
  45   1         ip->type_of_service = 0;
  46   1         ip->total_length = 20 + len;
  47   1         ip->identifier = ip_ident++;     // sequential identifier
  48   1         ip->fragment_info = 0;           // not fragmented
  49   1         ip->time_to_live = 32;           // max hops
  50   1         ip->protocol_id = proto_id;      // type of payload
  51   1         ip->header_cksum = 0;
  52   1         ip->source_ipaddr = my_ipaddr;
  53   1         
  54   1         // Outgoing IP address
C51 COMPILER V8.08   IP                                                                    11/04/2008 18:45:34 PAGE 2   

  55   1         ip->dest_ipaddr = ipaddr;
  56   1      
  57   1         // Compute and insert complement of checksum of ip header
  58   1         // Outgoing ip header length is always 20 bytes
  59   1         ip->header_cksum = ~cksum(outbuf + 14, 20);
  60   1         
  61   1         // Use ARP to get hardware address to send this to
  62   1         hwaddr = arp_resolve(ip->dest_ipaddr);
  63   1              
  64   1              // Null means that the ARP resolver did not find the IP address
  65   1              // in its cache so had to send an ARP request
  66   1              if (hwaddr == NULL)
  67   1              {
  68   2                      // Fill in the destination information so ehrn the ARP response
  69   2                      // arrives we can identify it and know what to do when we get it
  70   2            wait.buf = outbuf;
  71   2                      wait.ipaddr = ip->dest_ipaddr;
  72   2                      wait.proto_id = proto_id;
  73   2                      wait.len = len;
  74   2                      wait.timer = ARP_TIMEOUT; 
  75   2            return;
  76   2              }       
  77   1              
  78   1              ///eth_send(outbuf, hwaddr, IP_PACKET, 20 + len);
  79   1              CP220x_Send(hwaddr , outbuf, 20 + len, IP_PACKET);
*** WARNING C182 IN LINE 79 OF IP.C: pointer to different objects
  80   1      }
  81          
  82          
  83          
  84          //------------------------------------------------------------------------
  85          // This handles incoming IP datagrams from the Ethernet layer
  86          // See "TCP/IP Illustrated, Volume 1" Sect 3.2
  87          //------------------------------------------------------------------------
  88          void ip_rcve(UCHAR xdata * inbuf)
  89          {
  90   1              IP_HEADER xdata * ip;
  91   1              UINT idata header_len, payload_len;
  92   1                      
  93   1         ip = (IP_HEADER xdata *)(inbuf + 14);
  94   1                  
  95   1         // Make sure it is addressed to my IP address
  96   1         if (ip->dest_ipaddr != my_ipaddr) return;
  97   1      
  98   1         // Validate checksum of ip header
  99   1              header_len = 4 * (0x0F & ip->ver_len);
 100   1              payload_len = ip->total_length - header_len;
 101   1         if (cksum(inbuf + 14, header_len) != 0xFFFF)
 102   1              {
 103   2                 return; 
 104   2         }
 105   1              
 106   1              // Make sure incoming message is IP version 4
 107   1              if ((ip->ver_len >> 4) != 0x04)
 108   1              {
 109   2              return;
 110   2              }
 111   1      
 112   1              // Make sure incoming message is not fragmented because
 113   1         // we cannot handle fragmented messages
 114   1         if ((ip->fragment_info & 0x3FFF) != 0)
 115   1         {
C51 COMPILER V8.08   IP                                                                    11/04/2008 18:45:34 PAGE 3   

 116   2                 return; 
 117   2         }
 118   1      
 119   1         // At this point we have received a valid IP datagram addressed
 120   1         // to me.  We do not use header options, and do not forward
 121   1         // messages, so in the unlikely event there are header options,
 122   1         // delete them and shift the data down. The advantage is that
 123   1         // layers such as UDP and TCP know where their data starts
 124   1              if (header_len > 20)
 125   1              {
 126   2                              
 127   2            // Use memmove because of overlap
 128   2            memmove(inbuf + 34, inbuf + 14 + header_len, payload_len);
 129   2      
 130   2                      // Adjust info to reflect the move
 131   2                      header_len = 20;
 132   2                      ip->ver_len = 0x45;
 133   2                      ip->total_length = 20 + payload_len;
 134   2              }
 135   1              
 136   1              
 137   1              // Look at protocol ID byte and call the appropriate
 138   1         // function to handle the received message.  See 
 139   1         // "TCP/IP Illustrated, Volume 1" Sect 1.7 and RFC 791
 140   1         // for values for various protocols
 141   1         switch (ip->protocol_id)
 142   1              {
 143   2                 case ICMP_TYPE:
 144   2            icmp_rcve(inbuf, payload_len);
 145   2                      break;
 146   2      
 147   2            case IGMP_TYPE:
 148   2                ;
 149   2                      // We cannot handle IGMP messages
 150   2                      break;
 151   2                        
 152   2                      case UDP_TYPE:
 153   2            udp_rcve(inbuf, payload_len);
 154   2                      break;
 155   2      
 156   2                      case TCP_TYPE:   
 157   2            tcp_rcve(inbuf, payload_len);
 158   2                      break;
 159   2      
 160   2            default:
 161   2            ;
 162   2            break;
 163   2         }
 164   1      }
 165          
 166          
 167          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    657    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =     10    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      2      15
   IDATA SIZE       =   ----       4
   BIT SIZE         =   ----    ----
C51 COMPILER V8.08   IP                                                                    11/04/2008 18:45:34 PAGE 4   

END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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