1 |
/*********************************************************************
|
2 |
*
|
3 |
* DHCP Module for Microchip TCP/IP Stack
|
4 |
*
|
5 |
*********************************************************************
|
6 |
* FileName: DHCP.c
|
7 |
* Dependencies: StackTsk.h
|
8 |
* UDP.h
|
9 |
* Processor: PIC18
|
10 |
* Complier: MCC18 v1.00.50 or higher
|
11 |
* HITECH PICC-18 V8.10PL1 or higher
|
12 |
* Company: Microchip Technology, Inc.
|
13 |
*
|
14 |
* Software License Agreement
|
15 |
*
|
16 |
* The software supplied herewith by Microchip Technology Incorporated
|
17 |
* (the “Company”) for its PICmicro® Microcontroller is intended and
|
18 |
* supplied to you, the Company’s customer, for use solely and
|
19 |
* exclusively on Microchip PICmicro Microcontroller products. The
|
20 |
* software is owned by the Company and/or its supplier, and is
|
21 |
* protected under applicable copyright laws. All rights are reserved.
|
22 |
* Any use in violation of the foregoing restrictions may subject the
|
23 |
* user to criminal sanctions under applicable laws, as well as to
|
24 |
* civil liability for the breach of the terms and conditions of this
|
25 |
* license.
|
26 |
*
|
27 |
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
|
28 |
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
|
29 |
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
30 |
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
|
31 |
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
|
32 |
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
|
33 |
*
|
34 |
* HiTech PICC18 Compiler Options excluding device selection:
|
35 |
* -FAKELOCAL -G -Zg -E -C
|
36 |
*
|
37 |
* Author Date Comment
|
38 |
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
39 |
* Nilesh Rajbharti 3/21/01 Original (Rev 1.0)
|
40 |
* Nilesh Rajbharti 7/10/02 Explicitly initialized tempIPAddress
|
41 |
* (Rev 2.11)
|
42 |
* Nilesh Rajbharti 5/16/03 Increased DHCP_TIMEOUT to 2 seconds.
|
43 |
* Nilesh Rajbharti 5/16/03 Fixed SM_DHCP_BROADCAST logic
|
44 |
* where UDPPut was called before setting
|
45 |
* active socket.
|
46 |
* Robert Sloan 5/29/03 Improved DHCP State machine to handle
|
47 |
* NAK and renew existing IP address.
|
48 |
* Nilesh Rajbharti 8/15/03 Modified _DHCPRecieve() to check for
|
49 |
* chaddr field before accpting the packet.
|
50 |
* Fixed DHCPTask() where it would not
|
51 |
* reply to first OFFER.
|
52 |
********************************************************************/
|
53 |
#define THIS_IS_DHCP
|
54 |
|
55 |
#include "stacktsk.h"
|
56 |
#include "dhcp.h"
|
57 |
#include "udp.h"
|
58 |
#include "tick.h"
|
59 |
|
60 |
#if !defined(STACK_USE_DHCP)
|
61 |
#error DHCP module is not enabled.
|
62 |
#error If you do not want DHCP module, remove this file from your
|
63 |
#error project to reduce your code size.
|
64 |
#error If you do want DHCP module, make sure that STACK_USE_DHCP
|
65 |
#error is defined in StackTsk.h file.
|
66 |
#endif
|
67 |
|
68 |
#if defined(STACK_USE_SIIP)
|
69 |
#error DHCP module is not available when SLIP is used.
|
70 |
#endif
|
71 |
|
72 |
|
73 |
#define DHCP_TIMEOUT (TICK)(2L * TICK_SECOND)
|
74 |
|
75 |
|
76 |
#define DHCP_CLIENT_PORT (68)
|
77 |
#define DHCP_SERVER_PORT (67)
|
78 |
|
79 |
#define BOOT_REQUEST (1)
|
80 |
#define BOOT_REPLY (2)
|
81 |
#define HW_TYPE (1)
|
82 |
#define LEN_OF_HW_TYPE (6)
|
83 |
|
84 |
#define DHCP_MESSAGE_TYPE (53)
|
85 |
#define DHCP_MESSAGE_TYPE_LEN (1)
|
86 |
|
87 |
#define DHCP_UNKNOWN_MESSAGE (0)
|
88 |
|
89 |
#define DHCP_DISCOVER_MESSAGE (1)
|
90 |
#define DHCP_OFFER_MESSAGE (2)
|
91 |
#define DHCP_REQUEST_MESSAGE (3)
|
92 |
#define DHCP_DECLINE_MESSAGE (4)
|
93 |
#define DHCP_ACK_MESSAGE (5)
|
94 |
#define DHCP_NAK_MESSAGE (6)
|
95 |
#define DHCP_RELEASE_MESSAGE (7)
|
96 |
|
97 |
#define DHCP_SERVER_IDENTIFIER (54)
|
98 |
#define DHCP_SERVER_IDENTIFIER_LEN (4)
|
99 |
|
100 |
#define DHCP_PARAM_REQUEST_LIST (55)
|
101 |
#define DHCP_PARAM_REQUEST_LIST_LEN (2)
|
102 |
#define DHCP_PARAM_REQUEST_IP_ADDRESS (50)
|
103 |
#define DHCP_PARAM_REQUEST_IP_ADDRESS_LEN (4)
|
104 |
#define DHCP_SUBNET_MASK (1)
|
105 |
#define DHCP_ROUTER (3)
|
106 |
#define DHCP_IP_LEASE_TIME (51)
|
107 |
#define DHCP_END_OPTION (255)
|
108 |
|
109 |
#define HALF_HOUR (WORD)((WORD)60 * (WORD)30)
|
110 |
|
111 |
SM_DHCP smDHCPState = SM_DHCP_INIT;
|
112 |
static UDP_SOCKET DHCPSocket = INVALID_UDP_SOCKET;
|
113 |
|
114 |
|
115 |
DHCP_STATE DHCPState = { 0x00 };
|
116 |
|
117 |
static DWORD_VAL DHCPServerID;
|
118 |
static DWORD_VAL DHCPLeaseTime;
|
119 |
|
120 |
static IP_ADDR tempIPAddress;
|
121 |
static IP_ADDR tempGateway;
|
122 |
static IP_ADDR tempMask;
|
123 |
|
124 |
static BYTE _DHCPReceive(void);
|
125 |
static void _DHCPSend(BYTE messageType);
|
126 |
|
127 |
BYTE DHCPBindCount = 0;
|
128 |
|
129 |
|
130 |
/*
|
131 |
* Uncomment following line if DHCP transactions are to be displayed on
|
132 |
* RS-232 - for debug purpose only.
|
133 |
*/
|
134 |
//#define DHCP_DEBUG_MODE
|
135 |
|
136 |
#if defined(DHCP_DEBUG_MODE)
|
137 |
static USARTPut(BYTE c)
|
138 |
{
|
139 |
while( !TXSTA_TRMT);
|
140 |
TXREG = c;
|
141 |
}
|
142 |
#else
|
143 |
|
144 |
#define USARTPut(a)
|
145 |
|
146 |
#endif
|
147 |
|
148 |
|
149 |
void DHCPReset(void)
|
150 |
{
|
151 |
// Do not reset DHCP if it was previously disabled.
|
152 |
if ( smDHCPState == SM_DHCP_DISABLED )
|
153 |
return;
|
154 |
|
155 |
if ( DHCPSocket != INVALID_UDP_SOCKET )
|
156 |
UDPClose(DHCPSocket);
|
157 |
DHCPSocket = INVALID_UDP_SOCKET;
|
158 |
|
159 |
smDHCPState = SM_DHCP_INIT;
|
160 |
DHCPBindCount = 0;
|
161 |
}
|
162 |
|
163 |
/*********************************************************************
|
164 |
* Function: void DHCPTask(void)
|
165 |
*
|
166 |
* PreCondition: DHCPInit() is already called AND
|
167 |
* IPGetHeader() is called with
|
168 |
* IPFrameType == IP_PROT_UDP
|
169 |
*
|
170 |
* Input: None
|
171 |
*
|
172 |
* Output: None
|
173 |
*
|
174 |
* Side Effects: None
|
175 |
*
|
176 |
* Overview: Fetches pending UDP packet from MAC receive buffer
|
177 |
* and dispatches it appropriate UDP socket.
|
178 |
* If not UDP socket is matched, UDP packet is
|
179 |
* silently discarded.
|
180 |
*
|
181 |
* Note: Caller must make sure that MAC receive buffer
|
182 |
* access pointer is set to begining of UDP packet.
|
183 |
* Required steps before calling this function is:
|
184 |
*
|
185 |
* If ( MACIsRxReady() )
|
186 |
* {
|
187 |
* MACGetHeader()
|
188 |
* If MACFrameType == IP
|
189 |
* IPGetHeader()
|
190 |
* if ( IPFrameType == IP_PROT_UDP )
|
191 |
* Call DHCPTask()
|
192 |
* ...
|
193 |
********************************************************************/
|
194 |
void DHCPTask(void)
|
195 |
{
|
196 |
NODE_INFO DHCPServerNode;
|
197 |
static TICK lastTryTick;
|
198 |
BYTE DHCPRecvReturnValue;
|
199 |
|
200 |
switch(smDHCPState)
|
201 |
{
|
202 |
case SM_DHCP_INIT:
|
203 |
DHCPServerNode.MACAddr.v[0] = 0xff;
|
204 |
DHCPServerNode.MACAddr.v[1] = 0xff;
|
205 |
DHCPServerNode.MACAddr.v[2] = 0xff;
|
206 |
DHCPServerNode.MACAddr.v[3] = 0xff;
|
207 |
DHCPServerNode.MACAddr.v[4] = 0xff;
|
208 |
DHCPServerNode.MACAddr.v[5] = 0xff;
|
209 |
DHCPServerNode.IPAddr.Val = 0xffffffff;
|
210 |
tempIPAddress.Val = 0x0;
|
211 |
DHCPSocket = UDPOpen(DHCP_CLIENT_PORT,
|
212 |
&DHCPServerNode,
|
213 |
DHCP_SERVER_PORT);
|
214 |
lastTryTick = TickGet();
|
215 |
smDHCPState = SM_DHCP_RESET_WAIT;
|
216 |
/* No break */
|
217 |
|
218 |
case SM_DHCP_RESET_WAIT:
|
219 |
if ( TickGetDiff(TickGet(), lastTryTick) >= (TICK_SECOND/(TICK)5) )
|
220 |
smDHCPState = SM_DHCP_BROADCAST;
|
221 |
break;
|
222 |
|
223 |
case SM_DHCP_BROADCAST:
|
224 |
/*
|
225 |
* If we have already obtained some IP address, renew it.
|
226 |
*/
|
227 |
if ( tempIPAddress.Val != 0x00000 )
|
228 |
{
|
229 |
smDHCPState = SM_DHCP_REQUEST;
|
230 |
}
|
231 |
else if ( UDPIsPutReady(DHCPSocket) )
|
232 |
{
|
233 |
/*
|
234 |
* To minimize code requirement, user must make sure that
|
235 |
* above call will be successful by making at least one
|
236 |
* UDP socket available.
|
237 |
* Usually this will be the case, given that DHCP will be
|
238 |
* the first one to use UDP socket.
|
239 |
*
|
240 |
* Also, we will not check for transmitter readiness,
|
241 |
* we assume it to be ready.
|
242 |
*/
|
243 |
|
244 |
_DHCPSend(DHCP_DISCOVER_MESSAGE);
|
245 |
|
246 |
// DEBUG
|
247 |
USARTPut('\n');
|
248 |
USARTPut('\r');
|
249 |
USARTPut('D');
|
250 |
|
251 |
lastTryTick = TickGet();
|
252 |
smDHCPState = SM_DHCP_DISCOVER;
|
253 |
}
|
254 |
|
255 |
break;
|
256 |
|
257 |
|
258 |
case SM_DHCP_DISCOVER:
|
259 |
if ( TickGetDiff(TickGet(), lastTryTick) >= DHCP_TIMEOUT )
|
260 |
{
|
261 |
smDHCPState = SM_DHCP_BROADCAST;
|
262 |
//return;
|
263 |
}
|
264 |
|
265 |
if ( UDPIsGetReady(DHCPSocket) )
|
266 |
{
|
267 |
// DEBUG
|
268 |
USARTPut('R');
|
269 |
|
270 |
if ( _DHCPReceive() == DHCP_OFFER_MESSAGE )
|
271 |
{
|
272 |
// DEBUG
|
273 |
USARTPut('O');
|
274 |
|
275 |
smDHCPState = SM_DHCP_REQUEST;
|
276 |
}
|
277 |
else
|
278 |
break;
|
279 |
}
|
280 |
else
|
281 |
break;
|
282 |
|
283 |
|
284 |
|
285 |
case SM_DHCP_REQUEST:
|
286 |
if ( UDPIsPutReady(DHCPSocket) )
|
287 |
{
|
288 |
_DHCPSend(DHCP_REQUEST_MESSAGE);
|
289 |
|
290 |
lastTryTick = TickGet();
|
291 |
smDHCPState = SM_DHCP_BIND;
|
292 |
}
|
293 |
break;
|
294 |
|
295 |
case SM_DHCP_BIND:
|
296 |
if ( UDPIsGetReady(DHCPSocket) )
|
297 |
{
|
298 |
DHCPRecvReturnValue = _DHCPReceive();
|
299 |
if ( DHCPRecvReturnValue == DHCP_NAK_MESSAGE )
|
300 |
{
|
301 |
// (RSS) NAK recieved. DHCP server didn't like our DHCP Request format
|
302 |
USARTPut('n');
|
303 |
smDHCPState = SM_DHCP_REQUEST; // Request again
|
304 |
}
|
305 |
else if ( DHCPRecvReturnValue == DHCP_ACK_MESSAGE )
|
306 |
{
|
307 |
// DEBUG
|
308 |
USARTPut('B');
|
309 |
|
310 |
/*
|
311 |
* Once DCHP is successful, release the UDP socket
|
312 |
* This will ensure that UDP layer discards any further
|
313 |
* DHCP related packets.
|
314 |
*/
|
315 |
UDPClose(DHCPSocket);
|
316 |
DHCPSocket = INVALID_UDP_SOCKET;
|
317 |
|
318 |
lastTryTick = TickGet();
|
319 |
smDHCPState = SM_DHCP_BOUND;
|
320 |
|
321 |
MY_IP_BYTE1 = tempIPAddress.v[0];
|
322 |
MY_IP_BYTE2 = tempIPAddress.v[1];
|
323 |
MY_IP_BYTE3 = tempIPAddress.v[2];
|
324 |
MY_IP_BYTE4 = tempIPAddress.v[3];
|
325 |
|
326 |
MY_MASK_BYTE1 = tempMask.v[0];
|
327 |
MY_MASK_BYTE2 = tempMask.v[1];
|
328 |
MY_MASK_BYTE3 = tempMask.v[2];
|
329 |
MY_MASK_BYTE4 = tempMask.v[3];
|
330 |
|
331 |
MY_GATE_BYTE1 = tempGateway.v[0];
|
332 |
MY_GATE_BYTE2 = tempGateway.v[1];
|
333 |
MY_GATE_BYTE3 = tempGateway.v[2];
|
334 |
MY_GATE_BYTE4 = tempGateway.v[3];
|
335 |
|
336 |
DHCPState.bits.bIsBound = TRUE;
|
337 |
|
338 |
DHCPBindCount++;
|
339 |
|
340 |
return;
|
341 |
}
|
342 |
}
|
343 |
else if ( TickGetDiff(TickGet(), lastTryTick) >= DHCP_TIMEOUT )
|
344 |
{
|
345 |
USARTPut('t');
|
346 |
smDHCPState = SM_DHCP_BROADCAST;
|
347 |
}
|
348 |
break;
|
349 |
|
350 |
case SM_DHCP_BOUND:
|
351 |
/*
|
352 |
* Keep track of how long we use this IP configuration.
|
353 |
* When lease period expires, renew the configuration.
|
354 |
*/
|
355 |
if ( TickGetDiff(TickGet(), lastTryTick) >= TICK_SECOND )
|
356 |
{
|
357 |
DHCPLeaseTime.Val -= 1;
|
358 |
if ( DHCPLeaseTime.Val == 0 )
|
359 |
smDHCPState = SM_DHCP_INIT;
|
360 |
lastTryTick = TickGet();
|
361 |
}
|
362 |
}
|
363 |
|
364 |
}
|
365 |
|
366 |
|
367 |
|
368 |
/*********************************************************************
|
369 |
* Function: void DHCPAbort(void)
|
370 |
*
|
371 |
* PreCondition: DHCPTask() must have been called at least once.
|
372 |
*
|
373 |
* Input: None
|
374 |
*
|
375 |
* Output: None
|
376 |
*
|
377 |
* Side Effects: None
|
378 |
*
|
379 |
* Overview: Forgets about any previous DHCP attempts and
|
380 |
* closes DHCPSocket.
|
381 |
*
|
382 |
* Note:
|
383 |
********************************************************************/
|
384 |
void DHCPAbort(void)
|
385 |
{
|
386 |
smDHCPState = SM_DHCP_ABORTED;
|
387 |
UDPClose(DHCPSocket);
|
388 |
}
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
/*********************************************************************
|
395 |
DHCP PACKET FORMAT AS PER RFC 1541
|
396 |
|
397 |
0 1 2 3
|
398 |
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
399 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
400 |
| op (1) | htype (1) | hlen (1) | hops (1) |
|
401 |
+---------------+---------------+---------------+---------------+
|
402 |
| xid (4) |
|
403 |
+-------------------------------+-------------------------------+
|
404 |
| secs (2) | flags (2) |
|
405 |
+-------------------------------+-------------------------------+
|
406 |
| ciaddr (4) |
|
407 |
+---------------------------------------------------------------+
|
408 |
| yiaddr (4) |
|
409 |
+---------------------------------------------------------------+
|
410 |
| siaddr (4) |
|
411 |
+---------------------------------------------------------------+
|
412 |
| giaddr (4) |
|
413 |
+---------------------------------------------------------------+
|
414 |
| |
|
415 |
| chaddr (16) |
|
416 |
| |
|
417 |
| |
|
418 |
+---------------------------------------------------------------+
|
419 |
| |
|
420 |
| sname (64) |
|
421 |
+---------------------------------------------------------------+
|
422 |
| |
|
423 |
| file (128) |
|
424 |
+---------------------------------------------------------------+
|
425 |
| |
|
426 |
| options (312) |
|
427 |
+---------------------------------------------------------------+
|
428 |
|
429 |
********************************************************************/
|
430 |
static BYTE _DHCPReceive(void)
|
431 |
{
|
432 |
BYTE v;
|
433 |
BYTE i;
|
434 |
BYTE type;
|
435 |
BOOL lbDone;
|
436 |
DWORD_VAL tempServerID;
|
437 |
|
438 |
|
439 |
// Assume unknown message until proven otherwise.
|
440 |
type = DHCP_UNKNOWN_MESSAGE;
|
441 |
|
442 |
/*
|
443 |
* Assume default IP Lease time of 60 seconds.
|
444 |
* This should be minimum possible to make sure that if
|
445 |
* server did not specify lease time, we try again after
|
446 |
* this minimum time.
|
447 |
*/
|
448 |
DHCPLeaseTime.Val = 60;
|
449 |
|
450 |
UDPGet(&v); // op
|
451 |
/*
|
452 |
* Make sure this is BOOT_REPLY.
|
453 |
*/
|
454 |
if ( v == BOOT_REPLY )
|
455 |
{
|
456 |
/*
|
457 |
* Discard htype, hlen, hops, xid, secs, flags, ciaddr.
|
458 |
*/
|
459 |
for ( i = 0; i < 15; i++ )
|
460 |
UDPGet(&v);
|
461 |
|
462 |
/*
|
463 |
* Save offered IP address until we know for sure that
|
464 |
* we have it.
|
465 |
*/
|
466 |
UDPGet(&tempIPAddress.v[0]);
|
467 |
UDPGet(&tempIPAddress.v[1]);
|
468 |
UDPGet(&tempIPAddress.v[2]);
|
469 |
UDPGet(&tempIPAddress.v[3]);
|
470 |
|
471 |
/*
|
472 |
* Ignore siaddr, giaddr
|
473 |
*/
|
474 |
for ( i = 0; i < 8; i++ )
|
475 |
UDPGet(&v);
|
476 |
|
477 |
/*
|
478 |
* Check to see if chaddr (Client Hardware Address) belongs to us.
|
479 |
*/
|
480 |
for ( i = 0; i < 6; i++ )
|
481 |
{
|
482 |
UDPGet(&v);
|
483 |
if ( v != AppConfig.MyMACAddr.v[i])
|
484 |
goto UDPInvalid;
|
485 |
}
|
486 |
|
487 |
/*
|
488 |
* Ignore part of chaddr, sname, file, magic cookie.
|
489 |
*/
|
490 |
for ( i = 0; i < 206; i++ )
|
491 |
UDPGet(&v);
|
492 |
|
493 |
lbDone = FALSE;
|
494 |
do
|
495 |
{
|
496 |
UDPGet(&v);
|
497 |
|
498 |
switch(v)
|
499 |
{
|
500 |
case DHCP_MESSAGE_TYPE:
|
501 |
UDPGet(&v); // Skip len
|
502 |
// Len must be 1.
|
503 |
if ( v == 1 )
|
504 |
{
|
505 |
UDPGet(&type); // Get type
|
506 |
}
|
507 |
else
|
508 |
goto UDPInvalid;
|
509 |
break;
|
510 |
|
511 |
case DHCP_SUBNET_MASK:
|
512 |
UDPGet(&v); // Skip len
|
513 |
// Len must be 4.
|
514 |
if ( v == 4 )
|
515 |
{
|
516 |
UDPGet(&tempMask.v[0]);
|
517 |
UDPGet(&tempMask.v[1]);
|
518 |
UDPGet(&tempMask.v[2]);
|
519 |
UDPGet(&tempMask.v[3]);
|
520 |
}
|
521 |
else
|
522 |
goto UDPInvalid;
|
523 |
break;
|
524 |
|
525 |
case DHCP_ROUTER:
|
526 |
UDPGet(&v);
|
527 |
// Len must be >= 4.
|
528 |
if ( v >= 4 )
|
529 |
{
|
530 |
UDPGet(&tempGateway.v[0]);
|
531 |
UDPGet(&tempGateway.v[1]);
|
532 |
UDPGet(&tempGateway.v[2]);
|
533 |
UDPGet(&tempGateway.v[3]);
|
534 |
}
|
535 |
else
|
536 |
goto UDPInvalid;
|
537 |
|
538 |
/*
|
539 |
* Discard any other router addresses.
|
540 |
*/
|
541 |
v -= 4;
|
542 |
while(v--)
|
543 |
UDPGet(&i);
|
544 |
break;
|
545 |
|
546 |
case DHCP_SERVER_IDENTIFIER:
|
547 |
UDPGet(&v); // Get len
|
548 |
// Len must be 4.
|
549 |
if ( v == 4 )
|
550 |
{
|
551 |
UDPGet(&UPPER_MSB(tempServerID)); // Get the id
|
552 |
UDPGet(&UPPER_LSB(tempServerID));
|
553 |
UDPGet(&LOWER_MSB(tempServerID));
|
554 |
UDPGet(&LOWER_LSB(tempServerID));
|
555 |
}
|
556 |
else
|
557 |
goto UDPInvalid;
|
558 |
break;
|
559 |
|
560 |
case DHCP_END_OPTION:
|
561 |
lbDone = TRUE;
|
562 |
break;
|
563 |
|
564 |
case DHCP_IP_LEASE_TIME:
|
565 |
UDPGet(&v); // Get len
|
566 |
// Len must be 4.
|
567 |
if ( v == 4 )
|
568 |
{
|
569 |
UDPGet(&UPPER_MSB(DHCPLeaseTime));
|
570 |
UDPGet(&UPPER_LSB(DHCPLeaseTime));
|
571 |
UDPGet(&LOWER_MSB(DHCPLeaseTime));
|
572 |
UDPGet(&LOWER_LSB(DHCPLeaseTime));
|
573 |
|
574 |
/*
|
575 |
* Due to possible timing delays, consider actual lease
|
576 |
* time less by half hour.
|
577 |
*/
|
578 |
if ( DHCPLeaseTime.Val > HALF_HOUR )
|
579 |
DHCPLeaseTime.Val = DHCPLeaseTime.Val - HALF_HOUR;
|
580 |
|
581 |
}
|
582 |
else
|
583 |
goto UDPInvalid;
|
584 |
break;
|
585 |
|
586 |
default:
|
587 |
// Ignore all unsupport tags.
|
588 |
UDPGet(&v); // Get option len
|
589 |
while( v-- ) // Ignore option values
|
590 |
UDPGet(&i);
|
591 |
}
|
592 |
} while( !lbDone );
|
593 |
}
|
594 |
|
595 |
/*
|
596 |
* If this is an OFFER message, remember current server id.
|
597 |
*/
|
598 |
if ( type == DHCP_OFFER_MESSAGE )
|
599 |
{
|
600 |
DHCPServerID.Val = tempServerID.Val;
|
601 |
}
|
602 |
else
|
603 |
{
|
604 |
/*
|
605 |
* For other types of messages, make sure that received
|
606 |
* server id matches with our previous one.
|
607 |
*/
|
608 |
if ( DHCPServerID.Val != tempServerID.Val )
|
609 |
type = DHCP_UNKNOWN_MESSAGE;
|
610 |
}
|
611 |
|
612 |
UDPDiscard(); // We are done with this packet
|
613 |
return type;
|
614 |
|
615 |
UDPInvalid:
|
616 |
UDPDiscard();
|
617 |
return DHCP_UNKNOWN_MESSAGE;
|
618 |
|
619 |
}
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
static void _DHCPSend(BYTE messageType)
|
626 |
{
|
627 |
BYTE i;
|
628 |
|
629 |
UDPPut(BOOT_REQUEST); // op
|
630 |
UDPPut(HW_TYPE); // htype
|
631 |
UDPPut(LEN_OF_HW_TYPE); // hlen
|
632 |
UDPPut(0); // hops
|
633 |
UDPPut(0x12); // xid[0]
|
634 |
UDPPut(0x23); // xid[1]
|
635 |
UDPPut(0x34); // xid[2]
|
636 |
UDPPut(0x56); // xid[3]
|
637 |
UDPPut(0); // secs[0]
|
638 |
UDPPut(0); // secs[1]
|
639 |
UDPPut(0x80); // flags[0] with BF set
|
640 |
UDPPut(0); // flags[1]
|
641 |
|
642 |
|
643 |
/*
|
644 |
* If this is DHCP REQUEST message, use previously allocated
|
645 |
* IP address.
|
646 |
*/
|
647 |
#if 0
|
648 |
if ( messageType == DHCP_REQUEST_MESSAGE )
|
649 |
{
|
650 |
UDPPut(tempIPAddress.v[0]);
|
651 |
UDPPut(tempIPAddress.v[1]);
|
652 |
UDPPut(tempIPAddress.v[2]);
|
653 |
UDPPut(tempIPAddress.v[3]);
|
654 |
}
|
655 |
else
|
656 |
#endif
|
657 |
{
|
658 |
UDPPut(0x00);
|
659 |
UDPPut(0x00);
|
660 |
UDPPut(0x00);
|
661 |
UDPPut(0x00);
|
662 |
}
|
663 |
|
664 |
/*
|
665 |
* Set yiaddr, siaddr, giaddr as zeros,
|
666 |
*/
|
667 |
for ( i = 0; i < 12; i++ )
|
668 |
UDPPut(0x00);
|
669 |
|
670 |
|
671 |
|
672 |
/*
|
673 |
* Load chaddr - Client hardware address.
|
674 |
*/
|
675 |
UDPPut(MY_MAC_BYTE1);
|
676 |
UDPPut(MY_MAC_BYTE2);
|
677 |
UDPPut(MY_MAC_BYTE3);
|
678 |
UDPPut(MY_MAC_BYTE4);
|
679 |
UDPPut(MY_MAC_BYTE5);
|
680 |
UDPPut(MY_MAC_BYTE6);
|
681 |
|
682 |
/*
|
683 |
* Set chaddr[6..15], sname and file as zeros.
|
684 |
*/
|
685 |
for ( i = 0; i < 202; i++ )
|
686 |
UDPPut(0);
|
687 |
|
688 |
/*
|
689 |
* Load magic cookie as per RFC 1533.
|
690 |
*/
|
691 |
UDPPut(99);
|
692 |
UDPPut(130);
|
693 |
UDPPut(83);
|
694 |
UDPPut(99);
|
695 |
|
696 |
/*
|
697 |
* Load message type.
|
698 |
*/
|
699 |
UDPPut(DHCP_MESSAGE_TYPE);
|
700 |
UDPPut(DHCP_MESSAGE_TYPE_LEN);
|
701 |
UDPPut(messageType);
|
702 |
|
703 |
if ( messageType != DHCP_DISCOVER_MESSAGE && tempIPAddress.Val != 0x0000 )
|
704 |
{
|
705 |
/*
|
706 |
* DHCP REQUEST message may include server identifier,
|
707 |
* to identify the server we are talking to.
|
708 |
* DHCP ACK may include it too. To simplify logic,
|
709 |
* we will include server identifier in DHCP ACK message
|
710 |
* too.
|
711 |
* _DHCPReceive() would populate "serverID" when it
|
712 |
* receives DHCP OFFER message. We will simply use that
|
713 |
* when we are replying to server.
|
714 |
*
|
715 |
* If this is a renwal request, do not include server id.
|
716 |
*/
|
717 |
UDPPut(DHCP_SERVER_IDENTIFIER);
|
718 |
UDPPut(DHCP_SERVER_IDENTIFIER_LEN);
|
719 |
UDPPut(UPPER_MSB(DHCPServerID));
|
720 |
UDPPut(UPPER_LSB(DHCPServerID));
|
721 |
UDPPut(LOWER_MSB(DHCPServerID));
|
722 |
UDPPut(LOWER_LSB(DHCPServerID));
|
723 |
}
|
724 |
|
725 |
/*
|
726 |
* Load our interested parameters
|
727 |
* This is hardcoded list. If any new parameters are desired,
|
728 |
* new lines must be added here.
|
729 |
*/
|
730 |
UDPPut(DHCP_PARAM_REQUEST_LIST);
|
731 |
UDPPut(DHCP_PARAM_REQUEST_LIST_LEN);
|
732 |
UDPPut(DHCP_SUBNET_MASK);
|
733 |
UDPPut(DHCP_ROUTER);
|
734 |
|
735 |
// Add requested IP address to DHCP Request Message
|
736 |
if ( messageType == DHCP_REQUEST_MESSAGE )
|
737 |
{
|
738 |
UDPPut(DHCP_PARAM_REQUEST_IP_ADDRESS);
|
739 |
UDPPut(DHCP_PARAM_REQUEST_IP_ADDRESS_LEN);
|
740 |
|
741 |
UDPPut(tempIPAddress.v[0]);
|
742 |
UDPPut(tempIPAddress.v[1]);
|
743 |
UDPPut(tempIPAddress.v[2]);
|
744 |
UDPPut(tempIPAddress.v[3]);
|
745 |
}
|
746 |
|
747 |
/*
|
748 |
* Add any new paramter request here.
|
749 |
*/
|
750 |
|
751 |
/*
|
752 |
* End of Options.
|
753 |
*/
|
754 |
UDPPut(DHCP_END_OPTION);
|
755 |
|
756 |
UDPFlush();
|
757 |
}
|
758 |
|