1 |
hedin |
15 |
/*********************************************************************
|
2 |
|
|
*
|
3 |
|
|
* Address Resolution Protocol (ARP) Client and Server
|
4 |
|
|
* Module for Microchip TCP/IP Stack
|
5 |
|
|
* -Provides IP address to Ethernet MAC address translation
|
6 |
|
|
* -Reference: RFC 826
|
7 |
|
|
*
|
8 |
|
|
*********************************************************************
|
9 |
|
|
* FileName: ARP.c
|
10 |
|
|
* Dependencies: string.h
|
11 |
|
|
* StackTsk.h
|
12 |
|
|
* Helpers.h
|
13 |
|
|
* ARP.h
|
14 |
|
|
* MAC.h
|
15 |
|
|
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
|
16 |
|
|
* Complier: Microchip C18 v3.02 or higher
|
17 |
|
|
* Microchip C30 v2.01 or higher
|
18 |
|
|
* Company: Microchip Technology, Inc.
|
19 |
|
|
*
|
20 |
|
|
* Software License Agreement
|
21 |
|
|
*
|
22 |
|
|
* Copyright © 2002-2007 Microchip Technology Inc. All rights
|
23 |
|
|
* reserved.
|
24 |
|
|
*
|
25 |
|
|
* Microchip licenses to you the right to use, modify, copy, and
|
26 |
|
|
* distribute:
|
27 |
|
|
* (i) the Software when embedded on a Microchip microcontroller or
|
28 |
|
|
* digital signal controller product (“Device”) which is
|
29 |
|
|
* integrated into Licensee’s product; or
|
30 |
|
|
* (ii) ONLY the Software driver source files ENC28J60.c and
|
31 |
|
|
* ENC28J60.h ported to a non-Microchip device used in
|
32 |
|
|
* conjunction with a Microchip ethernet controller for the
|
33 |
|
|
* sole purpose of interfacing with the ethernet controller.
|
34 |
|
|
*
|
35 |
|
|
* You should refer to the license agreement accompanying this
|
36 |
|
|
* Software for additional information regarding your rights and
|
37 |
|
|
* obligations.
|
38 |
|
|
*
|
39 |
|
|
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
|
40 |
|
|
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
|
41 |
|
|
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
|
42 |
|
|
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
43 |
|
|
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
|
44 |
|
|
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
|
45 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
|
46 |
|
|
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
|
47 |
|
|
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
|
48 |
|
|
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
|
49 |
|
|
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
|
50 |
|
|
*
|
51 |
|
|
*
|
52 |
|
|
* Author Date Comment
|
53 |
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
54 |
|
|
* Nilesh Rajbharti 5/1/01 Original (Rev 1.0)
|
55 |
|
|
* Nilesh Rajbharti 2/9/02 Cleanup
|
56 |
|
|
* Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
|
57 |
|
|
* Howard Schlunder 8/17/06 Combined ARP.c and ARPTsk.c into ARP.c;
|
58 |
|
|
* rewrote some of it to look more linear
|
59 |
|
|
********************************************************************/
|
60 |
|
|
#define __ARP_C
|
61 |
|
|
|
62 |
|
|
#include "TCPIP Stack/TCPIP.h"
|
63 |
|
|
|
64 |
|
|
#if !defined(STACK_USE_SLIP)
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
// ARP Operation codes.
|
68 |
|
|
#define ARP_OPERATION_REQ 0x01u
|
69 |
|
|
#define ARP_OPERATION_RESP 0x02u
|
70 |
|
|
|
71 |
|
|
// ETHERNET packet type as defined by IEEE 802.3
|
72 |
|
|
#define HW_ETHERNET (0x0001u)
|
73 |
|
|
#define ARP_IP (0x0800u)
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
// This ARP task caches one ARP response.
|
78 |
|
|
#ifdef STACK_CLIENT_MODE
|
79 |
|
|
static NODE_INFO Cache;
|
80 |
|
|
#endif
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
// ARP packet
|
84 |
|
|
typedef struct _ARP_PACKET
|
85 |
|
|
{
|
86 |
|
|
WORD HardwareType;
|
87 |
|
|
WORD Protocol;
|
88 |
|
|
BYTE MACAddrLen;
|
89 |
|
|
BYTE ProtocolLen;
|
90 |
|
|
WORD Operation;
|
91 |
|
|
MAC_ADDR SenderMACAddr;
|
92 |
|
|
IP_ADDR SenderIPAddr;
|
93 |
|
|
MAC_ADDR TargetMACAddr;
|
94 |
|
|
IP_ADDR TargetIPAddr;
|
95 |
|
|
} ARP_PACKET;
|
96 |
|
|
|
97 |
|
|
// Helper function
|
98 |
|
|
static void SwapARPPacket(ARP_PACKET *p);
|
99 |
|
|
static BOOL ARPPut(ARP_PACKET *packet);
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
/*********************************************************************
|
104 |
|
|
* Function: BOOL ARPPut(NODE_INFO* more, BYTE opCode)
|
105 |
|
|
*
|
106 |
|
|
* PreCondition: None
|
107 |
|
|
*
|
108 |
|
|
* Input: remote - Remote node info
|
109 |
|
|
* opCode - ARP op code to send
|
110 |
|
|
*
|
111 |
|
|
* Output: TRUE - The ARP packet was generated properly
|
112 |
|
|
* FALSE - Unable to allocate a TX buffer
|
113 |
|
|
*
|
114 |
|
|
* Side Effects: None
|
115 |
|
|
*
|
116 |
|
|
* Overview: None
|
117 |
|
|
*
|
118 |
|
|
* Note: None
|
119 |
|
|
********************************************************************/
|
120 |
|
|
static BOOL ARPPut(ARP_PACKET *packet)
|
121 |
|
|
{
|
122 |
|
|
while(!MACIsTxReady());
|
123 |
|
|
MACSetWritePtr(BASE_TX_ADDR);
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
packet->HardwareType = HW_ETHERNET;
|
127 |
|
|
packet->Protocol = ARP_IP;
|
128 |
|
|
packet->MACAddrLen = sizeof(MAC_ADDR);
|
129 |
|
|
packet->ProtocolLen = sizeof(IP_ADDR);
|
130 |
|
|
// packet->SenderMACAddr = AppConfig.MyMACAddr; // HI-TECH PICC-18 compiler can't handle this statement, use memcpy() as a workaround
|
131 |
|
|
memcpy(&packet->SenderMACAddr, (void*)&AppConfig.MyMACAddr, sizeof(packet->SenderMACAddr));
|
132 |
|
|
packet->SenderIPAddr = AppConfig.MyIPAddr;
|
133 |
|
|
|
134 |
|
|
SwapARPPacket(packet);
|
135 |
|
|
|
136 |
|
|
MACPutHeader(&packet->TargetMACAddr, MAC_ARP, sizeof(*packet));
|
137 |
|
|
MACPutArray((BYTE*)packet, sizeof(*packet));
|
138 |
|
|
MACFlush();
|
139 |
|
|
|
140 |
|
|
return TRUE;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
/*********************************************************************
|
146 |
|
|
* Function: void ARPInit(void)
|
147 |
|
|
*
|
148 |
|
|
* PreCondition: None
|
149 |
|
|
*
|
150 |
|
|
* Input: None
|
151 |
|
|
*
|
152 |
|
|
* Output: ARP Cache is initialized.
|
153 |
|
|
*
|
154 |
|
|
* Side Effects: None
|
155 |
|
|
*
|
156 |
|
|
* Overview: None
|
157 |
|
|
*
|
158 |
|
|
* Note: None
|
159 |
|
|
********************************************************************/
|
160 |
|
|
#ifdef STACK_CLIENT_MODE
|
161 |
|
|
void ARPInit(void)
|
162 |
|
|
{
|
163 |
|
|
Cache.MACAddr.v[0] = 0xff;
|
164 |
|
|
Cache.MACAddr.v[1] = 0xff;
|
165 |
|
|
Cache.MACAddr.v[2] = 0xff;
|
166 |
|
|
Cache.MACAddr.v[3] = 0xff;
|
167 |
|
|
Cache.MACAddr.v[4] = 0xff;
|
168 |
|
|
Cache.MACAddr.v[5] = 0xff;
|
169 |
|
|
|
170 |
|
|
Cache.IPAddr.Val = 0x0;
|
171 |
|
|
}
|
172 |
|
|
#endif
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
/*********************************************************************
|
177 |
|
|
* Function: BOOL ARPProcess(void)
|
178 |
|
|
*
|
179 |
|
|
* PreCondition: ARP packet is ready in MAC buffer.
|
180 |
|
|
*
|
181 |
|
|
* Input: None
|
182 |
|
|
*
|
183 |
|
|
* Output: TRUE: If all processing of this ARP packet is complete. Do not call ARPProcess() again until a new ARP packet is waiting in the RX buffer.
|
184 |
|
|
* FALSE: If ARPProcess() must be called more times. We need more time to sent an ARP response
|
185 |
|
|
*
|
186 |
|
|
* Side Effects: None
|
187 |
|
|
*
|
188 |
|
|
* Overview: None
|
189 |
|
|
*
|
190 |
|
|
* Note: None
|
191 |
|
|
********************************************************************/
|
192 |
|
|
BOOL ARPProcess(void)
|
193 |
|
|
{
|
194 |
|
|
ARP_PACKET packet;
|
195 |
|
|
static NODE_INFO Target;
|
196 |
|
|
static enum
|
197 |
|
|
{
|
198 |
|
|
SM_ARP_IDLE,
|
199 |
|
|
SM_ARP_REPLY
|
200 |
|
|
} smARP = SM_ARP_IDLE;
|
201 |
|
|
|
202 |
|
|
switch(smARP)
|
203 |
|
|
{
|
204 |
|
|
case SM_ARP_IDLE:
|
205 |
|
|
// Obtain the incoming ARP packet
|
206 |
|
|
MACGetArray((BYTE*)&packet, sizeof(packet));
|
207 |
|
|
MACDiscardRx();
|
208 |
|
|
SwapARPPacket(&packet);
|
209 |
|
|
|
210 |
|
|
// Validate the ARP packet
|
211 |
|
|
if ( packet.HardwareType != HW_ETHERNET ||
|
212 |
|
|
packet.MACAddrLen != sizeof(MAC_ADDR) ||
|
213 |
|
|
packet.ProtocolLen != sizeof(IP_ADDR) )
|
214 |
|
|
{
|
215 |
|
|
return TRUE;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
// Handle incoming ARP responses
|
219 |
|
|
#ifdef STACK_CLIENT_MODE
|
220 |
|
|
if(packet.Operation == ARP_OPERATION_RESP)
|
221 |
|
|
{
|
222 |
|
|
Cache.MACAddr = packet.SenderMACAddr;
|
223 |
|
|
Cache.IPAddr = packet.SenderIPAddr;
|
224 |
|
|
return TRUE;
|
225 |
|
|
}
|
226 |
|
|
#endif
|
227 |
|
|
|
228 |
|
|
// Handle incoming ARP requests for our MAC address
|
229 |
|
|
if(packet.Operation == ARP_OPERATION_REQ)
|
230 |
|
|
{
|
231 |
|
|
if(packet.TargetIPAddr.Val != AppConfig.MyIPAddr.Val)
|
232 |
|
|
{
|
233 |
|
|
return TRUE;
|
234 |
|
|
}
|
235 |
|
|
Target.IPAddr = packet.SenderIPAddr;
|
236 |
|
|
Target.MACAddr = packet.SenderMACAddr;
|
237 |
|
|
|
238 |
|
|
smARP = SM_ARP_REPLY;
|
239 |
|
|
}
|
240 |
|
|
// Do not break. If we get down here, we need to send a reply.
|
241 |
|
|
|
242 |
|
|
case SM_ARP_REPLY:
|
243 |
|
|
packet.Operation = ARP_OPERATION_RESP;
|
244 |
|
|
packet.TargetMACAddr = Target.MACAddr;
|
245 |
|
|
packet.TargetIPAddr = Target.IPAddr;
|
246 |
|
|
|
247 |
|
|
// Send an ARP response to a previously received request
|
248 |
|
|
if(!ARPPut(&packet))
|
249 |
|
|
{
|
250 |
|
|
return FALSE;
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
// Begin listening for ARP requests again
|
254 |
|
|
smARP = SM_ARP_IDLE;
|
255 |
|
|
break;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
return TRUE;
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
/*********************************************************************
|
263 |
|
|
* Function: void ARPResolve(IP_ADDR* IPAddr)
|
264 |
|
|
*
|
265 |
|
|
* PreCondition: MACIsTxReady(TRUE) returns TRUE
|
266 |
|
|
*
|
267 |
|
|
* Input: IPAddr - IP Address to be resolved.
|
268 |
|
|
*
|
269 |
|
|
* Output: None
|
270 |
|
|
*
|
271 |
|
|
* Side Effects: None
|
272 |
|
|
*
|
273 |
|
|
* Overview: An ARP request is sent.
|
274 |
|
|
*
|
275 |
|
|
* Note: This function is available only when
|
276 |
|
|
* STACK_CLIENT_MODE is defined.
|
277 |
|
|
********************************************************************/
|
278 |
|
|
#ifdef STACK_CLIENT_MODE
|
279 |
|
|
void ARPResolve(IP_ADDR *IPAddr)
|
280 |
|
|
{
|
281 |
|
|
ARP_PACKET packet;
|
282 |
|
|
|
283 |
|
|
packet.Operation = ARP_OPERATION_REQ;
|
284 |
|
|
packet.TargetMACAddr.v[0] = 0xff;
|
285 |
|
|
packet.TargetMACAddr.v[1] = 0xff;
|
286 |
|
|
packet.TargetMACAddr.v[2] = 0xff;
|
287 |
|
|
packet.TargetMACAddr.v[3] = 0xff;
|
288 |
|
|
packet.TargetMACAddr.v[4] = 0xff;
|
289 |
|
|
packet.TargetMACAddr.v[5] = 0xff;
|
290 |
|
|
|
291 |
|
|
|
292 |
|
|
// ARP query either the IP address directly (on our subnet), or do an ARP query for our Gateway if off of our subnet
|
293 |
|
|
packet.TargetIPAddr = ((AppConfig.MyIPAddr.Val ^ IPAddr->Val) & AppConfig.MyMask.Val) ? AppConfig.MyGateway : *IPAddr;
|
294 |
|
|
|
295 |
|
|
ARPPut(&packet);
|
296 |
|
|
}
|
297 |
|
|
#endif
|
298 |
|
|
|
299 |
|
|
|
300 |
|
|
|
301 |
|
|
/*********************************************************************
|
302 |
|
|
* Function: BOOL ARPIsResolved(IP_ADDR* IPAddr,
|
303 |
|
|
* MAC_ADDR *MACAddr)
|
304 |
|
|
*
|
305 |
|
|
* PreCondition: None
|
306 |
|
|
*
|
307 |
|
|
* Input: IPAddr - IPAddress to be resolved.
|
308 |
|
|
* MACAddr - Buffer to hold corresponding
|
309 |
|
|
* MAC Address.
|
310 |
|
|
*
|
311 |
|
|
* Output: TRUE if given IP Address has been resolved.
|
312 |
|
|
* FALSE otherwise.
|
313 |
|
|
*
|
314 |
|
|
* Side Effects: None
|
315 |
|
|
*
|
316 |
|
|
* Overview: None
|
317 |
|
|
*
|
318 |
|
|
* Note: This function is available only when
|
319 |
|
|
* STACK_CLIENT_MODE is defined.
|
320 |
|
|
********************************************************************/
|
321 |
|
|
#ifdef STACK_CLIENT_MODE
|
322 |
|
|
BOOL ARPIsResolved(IP_ADDR *IPAddr, MAC_ADDR *MACAddr)
|
323 |
|
|
{
|
324 |
|
|
if((Cache.IPAddr.Val == IPAddr->Val) || ((Cache.IPAddr.Val == AppConfig.MyGateway.Val) && Cache.IPAddr.Val))
|
325 |
|
|
{
|
326 |
|
|
*MACAddr = Cache.MACAddr;
|
327 |
|
|
return TRUE;
|
328 |
|
|
}
|
329 |
|
|
return FALSE;
|
330 |
|
|
}
|
331 |
|
|
#endif
|
332 |
|
|
|
333 |
|
|
|
334 |
|
|
|
335 |
|
|
/*********************************************************************
|
336 |
|
|
* Function: static void SwapARPPacket(ARP_PACKET* p)
|
337 |
|
|
*
|
338 |
|
|
* PreCondition: None
|
339 |
|
|
*
|
340 |
|
|
* Input: p - ARP packet to be swapped.
|
341 |
|
|
*
|
342 |
|
|
* Output: None
|
343 |
|
|
*
|
344 |
|
|
* Side Effects: None
|
345 |
|
|
*
|
346 |
|
|
* Overview: None
|
347 |
|
|
*
|
348 |
|
|
* Note: None
|
349 |
|
|
********************************************************************/
|
350 |
|
|
static void SwapARPPacket(ARP_PACKET *p)
|
351 |
|
|
{
|
352 |
|
|
p->HardwareType = swaps(p->HardwareType);
|
353 |
|
|
p->Protocol = swaps(p->Protocol);
|
354 |
|
|
p->Operation = swaps(p->Operation);
|
355 |
|
|
}
|
356 |
|
|
|
357 |
|
|
#endif //#if !defined(STACK_USE_SLIP)
|