📄 arp.lst
字号:
C51 COMPILER V7.20 ARP 03/07/2006 14:49:08 PAGE 1
C51 COMPILER V7.20, COMPILATION OF MODULE ARP
OBJECT MODULE PLACED IN ARP.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ARP.C OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND
line level source
1 //-----------------------------------------------------------------------------
2 // Net ARP.C
3 //
4 // This module handles ARP messages and ARP resolution and manages
5 // the ARP cache. Refer to RFC 826 and RFC 1122
6 //-----------------------------------------------------------------------------
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include "reg52.h"
11 #include "net.h"
12 #include "eth.h"
13 #include "serial.h"
14 #include "ip.h"
15 #include "arp.h"
16
17
18 extern WAIT xdata wait;
19 extern UCHAR code my_hwaddr[];
20 extern UCHAR code broadcast_hwaddr[];
21 extern ULONG code my_ipaddr;
22 extern ULONG code my_subnet;
23 extern ULONG code gateway_ipaddr;
24 extern UCHAR idata debug;
25 ARP_CACHE xdata arp_cache[CACHESIZE];
26 UCHAR waiting_for_arp;
27
28 //初始化arp内存
29 void init_arp(void)
30 {
31 1 memset(arp_cache, 0, sizeof(arp_cache));
32 1 memset(&wait, 0, sizeof(wait));
33 1 waiting_for_arp = FALSE;
34 1 }
35
36 //------------------------------------------------------------------------
37 // This is called every 60 seconds to age the ARP cache
38 // If an entry times out then it is deleted from the cache
39 // See "TCP/IP Illustrated, Volume 1" Sect 4.3
40 //每60秒访问一次,如果生存时间超时就将他从ARP缓存中清掉
41 //------------------------------------------------------------------------
42 void age_arp_cache(void)
43 {
44 1 UCHAR i;
45 1
46 1 for (i=0; i < CACHESIZE; i++)
47 1 {
48 2 if ((arp_cache[i].ipaddr != 0) && (arp_cache[i].timer))
49 2 {
50 3 //不为空且时间不为零时
51 3 arp_cache[i].timer--;
52 3 if (arp_cache[i].timer == 0)
53 3 {
54 4 // Timed out so clear out cache entry
55 4 // Do not need to zero hwaddr
C51 COMPILER V7.20 ARP 03/07/2006 14:49:08 PAGE 2
56 4 //超时,就清空记录
57 4 arp_cache[i].ipaddr = 0;
58 4 if (debug) SendCommString("ARP: Aged out a cache entry\r");
59 4 }
60 3 }
61 2 }
62 1 }
63
64 //------------------------------------------------------------------------
65 // This allocates memory for the entire outgoing message,
66 // including eth and ip headers, then builds an outgoing
67 // ARP response message
68 // See "TCP/IP Illustrated, Volume 1" Sect 4.4
69 // ARP发送,包括MAC和IP地址。
70 //------------------------------------------------------------------------
71 void arp_send(UCHAR * hwaddr, ULONG ipaddr, UCHAR msg_type)
72 {
73 1 UCHAR xdata * outbuf;
74 1 ARP_HEADER xdata * arp;
75 1
76 1 // Allocate memory for entire outgoing message including
77 1 // eth header. Always 42 bytes
78 1 outbuf = (UCHAR xdata *)malloc(42);
79 1 if (outbuf == NULL)
80 1 {
81 2 if (debug) SendCommString("ARP: Oops, out of memory\r");
82 2 return;
83 2 }
84 1
85 1 // Allow 14 bytes for the ethernet header
86 1 arp = (ARP_HEADER xdata *)(outbuf + 14);
87 1
88 1 arp->hardware_type = DIX_ETHERNET;
89 1 arp->protocol_type = IP_PACKET;
90 1 arp->hwaddr_len = 6;
91 1 arp->ipaddr_len = 4;
92 1 arp->message_type = (UINT)msg_type;
93 1
94 1 // My hardware address and IP addresses
95 1 memcpy(arp->source_hwaddr, my_hwaddr, 6);
96 1 arp->source_ipaddr = my_ipaddr;
97 1
98 1 // Destination hwaddr and dest IP addr
99 1 if (msg_type == ARP_REQUEST) memset(arp->dest_hwaddr, 0, 6);
100 1 else memcpy(arp->dest_hwaddr, hwaddr, 6);
101 1
102 1 arp->dest_ipaddr = ipaddr;
103 1
104 1 // If request then the message is a brodcast, if a response then
105 1 // send to specified hwaddr
106 1 // ARP payload size is always 28 bytes
107 1 //负载28字符,判断是广播还是回复
108 1 if (msg_type == ARP_REQUEST) eth_send(outbuf, broadcast_hwaddr, ARP_PACKET, 28);
109 1 else eth_send(outbuf, hwaddr, ARP_PACKET, 28);
110 1 }
111
112 //------------------------------------------------------------------------
113 // This re-sends an ARP request if there was no response to
114 // the first one. It is called every 0.5 seconds. If there
115 // is no response after 2 re-tries, the datagram that IP was
116 // trying to send is deleted
117 // 如果第一次发没有回信号,ARP就重发。每0.5秒运行一次。
C51 COMPILER V7.20 ARP 03/07/2006 14:49:08 PAGE 3
118 // 重试2次后,如果还是没有数据,就删除需要发的数据。
119 //-----------------------------------------------------------------------
120 void arp_retransmit(void)
121 {
122 1 static UCHAR idata retries = 0;
123 1
124 1 if ((waiting_for_arp) && (wait.timer))
125 1 {
126 2 wait.timer--;
127 2 if (wait.timer == 0)
128 2 {
129 3 retries++;
130 3 if (retries <= 2)
131 3 {
132 4 if (debug) SendCommString("ARP: Re-sending ARP broadcast\r");
133 4 arp_send(NULL, wait.ipaddr, ARP_REQUEST);
134 4 wait.timer = ARP_TIMEOUT;
135 4 }
136 3 else
137 3 {
138 4 if (debug) SendCommString("ARP: Gave up waiting for response\r");
139 4 wait.timer = 0;
140 4 waiting_for_arp = 0;
141 4 free(wait.buf);
142 4 }
143 3 }
144 2 }
145 1 }
146
147
148 //------------------------------------------------------------------------
149 // Find the ethernet hardware address for the given ip address
150 // If destination IP is on my subnet then we want the eth
151 // address of destination, otherwise we want eth addr of gateway.
152 // Look in ARP cache first. If not found there, send ARP request.
153 // Return pointer to the hardware address or NULL if not found
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -