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

📄 arp.lst

📁 用c8051f340基于51单片机上网
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.08   ARP                                                                   11/04/2008 18:45:33 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE ARP
OBJECT MODULE PLACED IN Arp.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.exe Arp.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 ARP.C
   6          //
   7          // This module handles ARP messages and ARP resolution and manages
   8          // the ARP cache. Refer to RFC 826 and RFC 1122
   9          //-----------------------------------------------------------------------------
  10          #include <string.h>
  11          //#include <stdlib.h>
  12          #include "global.h"
  13          #include <stdio.h>
  14          #include "C8051f340.h"
  15          ///#include "net.h"
  16          #include "ip.h"
  17          #include "arp.h"
  18          
  19          
  20          
  21          extern WAIT xdata wait;
  22          extern UCHAR xdata my_hwaddr[];
  23          extern UCHAR code broadcast_hwaddr[]; 
  24          extern ULONG code my_ipaddr;
  25          extern ULONG code my_subnet;
  26          extern ULONG code gateway_ipaddr;
  27          extern UCHAR idata debug;
  28          ARP_CACHE xdata arp_cache[CACHESIZE];
  29          UCHAR waiting_for_arp;
  30          
  31          extern char xdata outbuf1[];
  32          
  33          //-----------------------------------------------------------------------------
  34          // Exported Function Definitions
  35          //-----------------------------------------------------------------------------
  36          extern void CP220x_Send(MACADDRESS* pDestAddr, unsigned char* buffer, unsigned int buffer_length, unsigned
             - int packet_type);
  37          extern unsigned int CP220x_Receive(unsigned char* buffer, unsigned int buffer_length);
  38          
  39          
  40          void init_arp(void)
  41          {
  42   1              memset(arp_cache, 0, sizeof(arp_cache)); 
  43   1              memset(&wait, 0, sizeof(wait));
  44   1              waiting_for_arp = FALSE;
  45   1      }
  46          
  47          
  48          
  49          
  50          //------------------------------------------------------------------------
  51          //      This is called every 60 seconds to age the ARP cache
  52          // If an entry times out then it is deleted from the cache
  53          // See "TCP/IP Illustrated, Volume 1" Sect 4.3
  54          //------------------------------------------------------------------------
C51 COMPILER V8.08   ARP                                                                   11/04/2008 18:45:33 PAGE 2   

  55          void age_arp_cache(void)
  56          {
  57   1              UCHAR i;
  58   1              
  59   1         for (i=0; i < CACHESIZE; i++)
  60   1         {
  61   2            if ((arp_cache[i].ipaddr != 0) && (arp_cache[i].timer))
  62   2            {
  63   3               arp_cache[i].timer--;
  64   3                              if (arp_cache[i].timer == 0)
  65   3               {
  66   4                                      // Timed out so clear out cache entry
  67   4                                      // Do not need to zero hwaddr
  68   4                                      arp_cache[i].ipaddr = 0;
  69   4                              }
  70   3                      }
  71   2         }
  72   1      }
  73          
  74          
  75          
  76          
  77          //------------------------------------------------------------------------
  78          // This allocates memory for the entire outgoing message,
  79          // including eth and ip headers, then builds an outgoing
  80          // ARP response message
  81          // See "TCP/IP Illustrated, Volume 1" Sect 4.4
  82          //------------------------------------------------------------------------
  83          void arp_send(UCHAR * hwaddr, ULONG ipaddr, UCHAR msg_type)
  84          {
  85   1              UCHAR xdata * outbuf;
  86   1              ARP_HEADER xdata * arp;
  87   1               
  88   1         
  89   1         // Allocate memory for entire outgoing message including
  90   1         // eth header. Always 42 bytes
  91   1      //   outbuf = (UCHAR xdata *)malloc(42);
  92   1        outbuf = outbuf1;
  93   1           
  94   1         // Allow 14 bytes for the ethernet header
  95   1         arp = (ARP_HEADER xdata *)(outbuf + 14);
  96   1              
  97   1              arp->hardware_type = DIX_ETHERNET; 
  98   1         arp->protocol_type = IP_PACKET;
  99   1         arp->hwaddr_len = 6;
 100   1              arp->ipaddr_len = 4;               
 101   1         arp->message_type = (UINT)msg_type;
 102   1         
 103   1         // My hardware address and IP addresses 
 104   1         memcpy(arp->source_hwaddr, my_hwaddr, 6);
 105   1         arp->source_ipaddr = my_ipaddr;
 106   1      
 107   1         // Destination hwaddr and dest IP addr
 108   1         if (msg_type == ARP_REQUEST) memset(arp->dest_hwaddr, 0, 6);
 109   1         else memcpy(arp->dest_hwaddr, hwaddr, 6);
 110   1         
 111   1         arp->dest_ipaddr = ipaddr;
 112   1            
 113   1         // If request then the message is a brodcast, if a response then
 114   1         // send to specified hwaddr
 115   1         // ARP payload size is always 28 bytes
 116   1         if (msg_type == ARP_REQUEST) CP220x_Send(broadcast_hwaddr , outbuf, 28, ARP_PACKET);
C51 COMPILER V8.08   ARP                                                                   11/04/2008 18:45:33 PAGE 3   

*** WARNING C182 IN LINE 116 OF ARP.C: pointer to different objects
 117   1              else CP220x_Send(hwaddr , outbuf, 28, ARP_PACKET);
*** WARNING C182 IN LINE 117 OF ARP.C: pointer to different objects
 118   1      ///     if (msg_type == ARP_REQUEST) eth_send(outbuf, broadcast_hwaddr, ARP_PACKET, 28);
 119   1         ///else eth_send(outbuf, hwaddr, ARP_PACKET, 28);
 120   1      
 121   1      }
 122          
 123          
 124          
 125          //------------------------------------------------------------------------
 126          // This re-sends an ARP request if there was no response to
 127          // the first one.        It is called every 0.5 seconds.  If there
 128          // is no response after 2 re-tries, the datagram that IP was 
 129          // trying to send is deleted
 130          //-----------------------------------------------------------------------
 131          void arp_retransmit(void)
 132          {
 133   1              static UCHAR idata retries = 0; 
 134   1              
 135   1              if ((waiting_for_arp) && (wait.timer))
 136   1              {
 137   2                      wait.timer--;
 138   2                      if (wait.timer == 0)
 139   2                      {
 140   3                              retries++;
 141   3                              if (retries <= 2)
 142   3                              {
 143   4                                      arp_send(NULL, wait.ipaddr, ARP_REQUEST);
 144   4                                      wait.timer = ARP_TIMEOUT;
 145   4                              }
 146   3                              else
 147   3                              {       
 148   4                                      wait.timer = 0;
 149   4                                      waiting_for_arp = 0;
 150   4      //                              free(wait.buf);
 151   4                              }
 152   3                      }

⌨️ 快捷键说明

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