📄 arp.lst
字号:
C51 COMPILER V7.06 ARP 12/04/2007 13:46:19 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE ARP
OBJECT MODULE PLACED IN arp.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE arp.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*==============================================*/
2 // ARP协议程序
3
4 /*==============================================*/
5
6 #include "ne2000.h"
7 #include "eth.h"
8 #include "ip.h"
9 #include "pubilc.h"
10 #include <stdio.h>
11 static unsigned char arpindex = 0; //ARP table 循环加入点
12 extern union NetNode xdata myNode;
13 /*==============================================*/
14 // 函数名称: arp动态缓冲区初始化
15 /*==============================================*/
16 void arptab_init(void)
17 {
18 1 INT8U i,j;
19 1 for(i = 0; i < MaxLenARPtable; i++)
20 1 {
21 2 for(j = 0; j < 12; j++)
22 2 {
23 3 arp_tab[i].bytes[j] = 0;
24 3 }
25 2 }
26 1 waiting_for_arp=0;
27 1 }
28
29 /*==============================================*/
30 // 函数名称: arp请求
31 /*==============================================*/
32 void arp_request(union ip_address_type ip_address)
33 {
34 1 INT8U i;
35 1 INT8U xdata arp_req[60];
36 1 union ethernet_address_type xdata castaddress;
37 1 arp_head xdata *arp;
38 1 arp=(arp_head xdata *)(arp_req+14);
39 1 for(i = 0; i < 6; i++)
40 1 {
41 2 castaddress.bytes[i] = 0xFF;
42 2 arp->sourcenodeid[i] = myNode.node.mac[i];
43 2 }
44 1
45 1 for(i = 0; i < 4; i++)
46 1 {
47 2 arp->sourceip[i] = myNode.nodebytes.ipbytes[i];
48 2 arp->destip[i] = ip_address.bytes[i];
49 2 }
50 1 arp->harewaretype=0x0001; // 0x0100;
51 1 arp->protocaltype=0x0800; //0x0008;
52 1 arp->halength=0x06;
53 1 arp->palength=0x04;
54 1 arp->operation=0x0001; //0x0100;//应答代码
55 1 for(i=42;i<60;i++)
C51 COMPILER V7.06 ARP 12/04/2007 13:46:19 PAGE 2
56 1 arp_req[i]=0x00;
57 1 eth_send(arp_req,castaddress, ARP_PACKET, 46);
58 1 waiting_for_arp=1;
59 1 }
60
61 /*==============================================*/
62 // 函数名称: arp应答
63 /*==============================================*/
64 void arp_answer(INT8U xdata *inbuf)
65 {
66 1 INT16U i;
67 1 union ethernet_address_type answeraddress;
68 1 unsigned char xdata arp_outbuf[60];
69 1 arp_head xdata *arp_in,*arp_out;
70 1 arp_in=(arp_head xdata *)(inbuf+14);
71 1 arp_out=(arp_head xdata *)(arp_outbuf+14);
72 1 // union ethernet_address_type answeraddress;
73 1 if( (arp_in->destip[0] == myNode.nodebytes.ipbytes[0])
74 1 && (arp_in->destip[1] == myNode.nodebytes.ipbytes[1])
75 1 && (arp_in->destip[2] == myNode.nodebytes.ipbytes[2])
76 1 && (arp_in->destip[3] == myNode.nodebytes.ipbytes[3]))
77 1 { //表示是向我这个ip地址的请求
78 2 for(i = 12; i < 60; i++)
79 2 { //复制arp到发送缓冲区
80 3 arp_outbuf[i]=inbuf[i];
81 3 }
82 2 for(i = 0; i < 6; i++)
83 2 { //复制对方网卡地址或网关地址
84 3 answeraddress.bytes[i]=arp_in->sourcenodeid[i];
85 3 arp_out->sourcenodeid[i]=myNode.node.mac[i];
86 3 arp_out->destnodeid[i]=arp_in->sourcenodeid[i];
87 3 }
88 2 for(i = 0; i < 4; i++)
89 2 {
90 3 arp_out->destip[i]=arp_in->sourceip[i];
91 3 arp_out->sourceip[i]=arp_in->destip[i];
92 3 }
93 2 arp_out->operation=0x0002; //0x0200;//响应代码
94 2 eth_send (arp_outbuf,answeraddress,ARP_PACKET, 46);
95 2 }
96 1 else
97 1 {
98 2 return;
99 2 }
100 1 }
101
102 /*==============================================*/
103 // 函数名称: arp应答处理
104 /*==============================================*/
105 void arp_process(INT8U xdata *inbuf)
106 {
107 1 INT8U i,j;
108 1 union ip_address_type xdata rx_ip;
109 1 union ethernet_address_type xdata rx_hwaddr;
110 1 arp_head xdata *arp;
111 1 arp=(arp_head xdata *)(inbuf+14);
112 1 for(i=0;i<4;i++)
113 1 rx_ip.bytes[i]=arp->sourceip[i];
114 1 for(i=0;i<6;i++)
115 1 rx_hwaddr.bytes[i]=arp->sourcenodeid[i];
116 1 if ((waiting_for_arp) && (wait.ipaddr == rx_ip.dwords))
117 1 {
C51 COMPILER V7.06 ARP 12/04/2007 13:46:19 PAGE 3
118 2 waiting_for_arp = 0;
119 2 eth_send(wait.bytebuf,rx_hwaddr,wait.proto_id ,wait.len);
120 2 }
121 1 for(i = 0; i < MaxLenARPtable; i++)
122 1 {
123 2 {
124 3 if((arp_tab[i].arp.ip_address.bytes[0] == arp->sourceip[0])
125 3 && (arp_tab[i].arp.ip_address.bytes[1] == arp->sourceip[1])
126 3 && (arp_tab[i].arp.ip_address.bytes[2] == arp->sourceip[2])
127 3 && (arp_tab[i].arp.ip_address.bytes[3] == arp->sourceip[3]))
128 3 {
129 4 arp_tab[i].arp.ttl=0x80;
130 4 arp_tab[i].arp.status = 1;
131 4 for(j = 0; j < 4; j++)
132 4 arp_tab[i].arp.ip_address.bytes[j]=arp->sourceip[j];
133 4 for(j = 0; j < 6; j++)
134 4 arp_tab[i].arp.ethernet_address.bytes[j]=arp->sourcenodeid[j];
135 4 return;
136 4 }
137 3 }
138 2
139 2
140 2 }
141 1 arp_tab[arpindex].arp.status = 1; //write arp package to some location.
142 1 arp_tab[arpindex].arp.ttl = 0x80;
143 1 for(j = 0; j < 4; j++)
144 1 arp_tab[arpindex].arp.ip_address.bytes[j] = arp->sourceip[j];
145 1 for(j = 0; j < 6; j++)
146 1 arp_tab[arpindex].arp.ethernet_address.bytes[j] = arp->sourcenodeid[j];
147 1 arpindex++;
148 1 if(arpindex == MaxLenARPtable)
149 1 arpindex = 0;
150 1 }
151
152 /*==============================================*/
153 // 函数名称: 更新ARP缓存
154 /*==============================================*/
155 void updatearptab(void)
156 {
157 1 INT8U i;
158 1
159 1 for(i = 0; i < MaxLenARPtable; i++)
160 1 {
161 2 if(arp_tab[i].arp.status == 1)
162 2 {
163 3 if(arp_tab[i].arp.ttl == 0)
164 3 {
165 4 arp_tab[i].arp.status=0;
166 4 }
167 3 else
168 3 {
169 4 arp_tab[i].arp.ttl--;
170 4 }
171 3 }
172 2 }
173 1 }
174
175 /*==============================================*/
176 // 函数名称: 在ARP缓存中查找指定IP/MAC映射对
177 /*==============================================*/
178 unsigned char arp_find_mac(union ip_address_type ip,union ethernet_address_type xdata *macadr)
179 {
C51 COMPILER V7.06 ARP 12/04/2007 13:46:19 PAGE 4
180 1 INT8U i,j;
181 1
182 1 for(i = 0; i < MaxLenARPtable; i++)
183 1 {
184 2 if(arp_tab[i].arp.status == 1)
185 2 {
186 3 if((arp_tab[i].arp.ip_address.bytes[0] == ip.bytes[0])
187 3 && (arp_tab[i].arp.ip_address.bytes[1] == ip.bytes[1])
188 3 && (arp_tab[i].arp.ip_address.bytes[2] == ip.bytes[2])
189 3 && (arp_tab[i].arp.ip_address.bytes[3] == ip.bytes[3]))
190 3 {
191 4 for(j = 0; j < 6; j++)
192 4 macadr->bytes[j] = arp_tab[i].arp.ethernet_address.bytes[j];
193 4 return 1;
194 4 }
195 3 }
196 2 }
197 1 return 0;
198 1 }
199
200 /*==============================================*/
201 // 函数名称: 在ARP缓存中直接加入IP/MAC映射对
202 /*==============================================*/
203 void arp_ip_mac(INT8U xdata *inbuf, union ethernet_address_type hard_addr)
204 {
205 1 INT8U i,j;
206 1 ip_head *ip;
207 1 ip=(ip_head *)(inbuf+14);
208 1 for(i = 0; i < MaxLenARPtable; i++)
209 1 {
210 2 {
211 3 if((arp_tab[i].arp.ip_address.bytes[0] == ip->sourceip[0])
212 3 && (arp_tab[i].arp.ip_address.bytes[1] == ip->sourceip[1])
213 3 && (arp_tab[i].arp.ip_address.bytes[2] == ip->sourceip[2])
214 3 && (arp_tab[i].arp.ip_address.bytes[3] == ip->sourceip[3]))
215 3 {
216 4 arp_tab[i].arp.ttl=0x80;
217 4 arp_tab[i].arp.status = 1;
218 4 for(j = 0; j < 4; j++)
219 4 arp_tab[i].arp.ip_address.bytes[j] = ip->sourceip[j];
220 4 for(j = 0; j < 6; j++)
221 4 arp_tab[i].arp.ethernet_address.bytes[j] = hard_addr.bytes[j];
222 4 return;
223 4 }
224 3 }
225 2
226 2 }
227 1 arp_tab[arpindex].arp.status = 1; //write arp package to some location.
228 1 arp_tab[arpindex].arp.ttl = 0x80;
229 1 for(j = 0; j < 4; j++)
230 1 arp_tab[arpindex].arp.ip_address.bytes[j] = ip->sourceip[j];
231 1 for(j = 0; j < 6; j++)
232 1 arp_tab[arpindex].arp.ethernet_address.bytes[j] = hard_addr.bytes[j];
233 1 arpindex++;
234 1 if(arpindex == MaxLenARPtable)
235 1 arpindex = 0;
236 1 }
237 void arp_rcve(const INT8U xdata *inbuf)
238 {
239 1
240 1 arp_head xdata *arp;
241 1 arp=(arp_head xdata *)(inbuf+14);
C51 COMPILER V7.06 ARP 12/04/2007 13:46:19 PAGE 5
242 1 switch(arp->operation)
243 1 {
244 2 case 0x0001:
245 2 arp_answer(inbuf);
246 2 break;
247 2 case 0x0002:
248 2 arp_process(inbuf);
249 2 break;
250 2 default:
251 2 break;
252 2 }
253 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 2161 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- 136
PDATA SIZE = ---- ----
DATA SIZE = 1 40
IDATA SIZE = ---- ----
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 + -