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