📄 arp.lst
字号:
C51 COMPILER V7.07 ARP 11/25/2003 15:47:43 PAGE 1
C51 COMPILER V7.07, COMPILATION OF MODULE ARP
OBJECT MODULE PLACED IN ARP.OBJ
COMPILER INVOKED BY: D:\KEIL707\C51\BIN\C51.EXE ARP.C OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND
stmt 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 <stdio.h>
13 #include "C8051f.h"
14 #include "net.h"
15 #include "eth.h"
16 #include "serial.h"
17 #include "ip.h"
18 #include "arp.h"
19
20
21
22 extern WAIT xdata wait;
23 extern UCHAR code my_hwaddr[];
24 extern UCHAR code broadcast_hwaddr[];
25 extern ULONG code my_ipaddr;
26 extern ULONG code my_subnet;
27 extern ULONG code gateway_ipaddr;
28 extern UCHAR idata debug;
29 ARP_CACHE xdata arp_cache[CACHESIZE];
30 UCHAR waiting_for_arp;
31
32
33
34 void init_arp(void)
35 {
36 1 memset(arp_cache, 0, sizeof(arp_cache));
37 1 memset(&wait, 0, sizeof(wait));
38 1 waiting_for_arp = FALSE;
39 1 }
40
41
42
43
44 //------------------------------------------------------------------------
45 // This is called every 60 seconds to age the ARP cache
46 // If an entry times out then it is deleted from the cache
47 // See "TCP/IP Illustrated, Volume 1" Sect 4.3
48 //------------------------------------------------------------------------
49 void age_arp_cache(void)
50 {
51 1 UCHAR i;
52 1
53 1 for (i=0; i < CACHESIZE; i++)
54 1 {
55 2 if ((arp_cache[i].ipaddr != 0) && (arp_cache[i].timer))
C51 COMPILER V7.07 ARP 11/25/2003 15:47:43 PAGE 2
56 2 {
57 3 arp_cache[i].timer--;
58 3 if (arp_cache[i].timer == 0)
59 3 {
60 4 // Timed out so clear out cache entry
61 4 // Do not need to zero hwaddr
62 4 arp_cache[i].ipaddr = 0;
63 4 if (debug) serial_send("ARP: Aged out a cache entry\r");
64 4 }
65 3 }
66 2 }
67 1 }
68
69
70
71
72 //------------------------------------------------------------------------
73 // This allocates memory for the entire outgoing message,
74 // including eth and ip headers, then builds an outgoing
75 // ARP response message
76 // See "TCP/IP Illustrated, Volume 1" Sect 4.4
77 //------------------------------------------------------------------------
78 void arp_send(UCHAR * hwaddr, ULONG ipaddr, UCHAR msg_type)
79 {
80 1 UCHAR xdata * outbuf;
81 1 ARP_HEADER xdata * arp;
82 1
83 1
84 1 // Allocate memory for entire outgoing message including
85 1 // eth header. Always 42 bytes
86 1 outbuf = (UCHAR xdata *)malloc(42);
87 1 if (outbuf == NULL)
88 1 {
89 2 if (debug) serial_send("ARP: Oops, out of memory\r");
90 2 return;
91 2 }
92 1
93 1 // Allow 14 bytes for the ethernet header
94 1 arp = (ARP_HEADER xdata *)(outbuf + 14);
95 1
96 1 arp->hardware_type = DIX_ETHERNET;
97 1 arp->protocol_type = IP_PACKET;
98 1 arp->hwaddr_len = 6;
99 1 arp->ipaddr_len = 4;
100 1 arp->message_type = (UINT)msg_type;
101 1
102 1 // My hardware address and IP addresses
103 1 memcpy(arp->source_hwaddr, my_hwaddr, 6);
104 1 arp->source_ipaddr = my_ipaddr;
105 1
106 1 // Destination hwaddr and dest IP addr
107 1 if (msg_type == ARP_REQUEST) memset(arp->dest_hwaddr, 0, 6);
108 1 else memcpy(arp->dest_hwaddr, hwaddr, 6);
109 1
110 1 arp->dest_ipaddr = ipaddr;
111 1
112 1 // If request then the message is a brodcast, if a response then
113 1 // send to specified hwaddr
114 1 // ARP payload size is always 28 bytes
115 1 if (msg_type == ARP_REQUEST) eth_send(outbuf, broadcast_hwaddr, ARP_PACKET, 28);
116 1 else eth_send(outbuf, hwaddr, ARP_PACKET, 28);
117 1 }
C51 COMPILER V7.07 ARP 11/25/2003 15:47:43 PAGE 3
118
119
120
121 //------------------------------------------------------------------------
122 // This re-sends an ARP request if there was no response to
123 // the first one. It is called every 0.5 seconds. If there
124 // is no response after 2 re-tries, the datagram that IP was
125 // trying to send is deleted
126 //-----------------------------------------------------------------------
127 void arp_retransmit(void)
128 {
129 1 static UCHAR idata retries = 0;
130 1
131 1 if ((waiting_for_arp) && (wait.timer))
132 1 {
133 2 wait.timer--;
134 2 if (wait.timer == 0)
135 2 {
136 3 retries++;
137 3 if (retries <= 2)
138 3 {
139 4 if (debug) serial_send("ARP: Re-sending ARP broadcast\r");
140 4 arp_send(NULL, wait.ipaddr, ARP_REQUEST);
141 4 wait.timer = ARP_TIMEOUT;
142 4 }
143 3 else
144 3 {
145 4 if (debug) serial_send("ARP: Gave up waiting for response\r");
146 4 wait.timer = 0;
147 4 waiting_for_arp = 0;
148 4 free(wait.buf);
149 4 }
150 3 }
151 2 }
152 1 }
153
154
155
156
157 //------------------------------------------------------------------------
158 // Find the ethernet hardware address for the given ip address
159 // If destination IP is on my subnet then we want the eth
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -