2 * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.
\r
4 * This file is part of the FreeRTOS+UDP distribution. The FreeRTOS+UDP license
\r
5 * terms are different to the FreeRTOS license terms.
\r
7 * FreeRTOS+UDP uses a dual license model that allows the software to be used
\r
8 * under a pure GPL open source license (as opposed to the modified GPL license
\r
9 * under which FreeRTOS is distributed) or a commercial license. Details of
\r
10 * both license options follow:
\r
12 * - Open source licensing -
\r
13 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
14 * distributed without charge provided the user adheres to version two of the
\r
15 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
16 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
17 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
19 * - Commercial licensing -
\r
20 * Businesses and individuals that for commercial or other reasons cannot comply
\r
21 * with the terms of the GPL V2 license must obtain a commercial license before
\r
22 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
23 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
24 * and do not require any source files to be changed.
\r
26 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
27 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
28 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
29 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
30 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
31 * implied, expressed, or statutory.
\r
33 * 1 tab == 4 spaces!
\r
35 * http://www.FreeRTOS.org
\r
36 * http://www.FreeRTOS.org/udp
\r
40 /* Standard includes. */
\r
43 /* FreeRTOS includes. */
\r
44 #include "FreeRTOS.h"
\r
49 /* FreeRTOS+UDP includes. */
\r
50 #include "FreeRTOS_UDP_IP.h"
\r
51 #include "FreeRTOS_IP_Private.h"
\r
52 #include "FreeRTOS_DHCP.h"
\r
53 #include "FreeRTOS_Sockets.h"
\r
54 #include "NetworkInterface.h"
\r
55 #include "IPTraceMacroDefaults.h"
\r
57 /* Exclude the entire file if DHCP is not enabled. */
\r
58 #if ipconfigUSE_DHCP != 0
\r
60 #if ( ipconfigUSE_DHCP != 0 ) && ( ipconfigNETWORK_MTU < 586 )
\r
61 /* DHCP must be able to receive an options field of 312 bytes, the fixed
\r
62 part of the DHCP packet is 240 bytes, and the IP/UDP headers take 28 bytes. */
\r
63 #error ipconfigNETWORK_MTU needs to be at least 586 to use DHCP (588 if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS is set to 1)
\r
66 /* Parameter widths in the DHCP packet. */
\r
67 #define dhcpCLIENT_HARDWARE_ADDRESS_LENGTH 16
\r
68 #define dhcpSERVER_HOST_NAME_LENGTH 64
\r
69 #define dhcpBOOT_FILE_NAME_LENGTH 128
\r
71 /* Timer parameters. Windows simulator times are much shorter because simulated
\r
72 time is not real time. */
\r
74 #define dhcpINITIAL_DHCP_TX_PERIOD ( 100 / portTICK_RATE_MS )
\r
75 #define dhcpINITIAL_TIMER_PERIOD ( 10 / portTICK_RATE_MS )
\r
76 #define dhcpMAX_TIME_TO_WAIT_FOR_ACK ( 200 / portTICK_RATE_MS )
\r
78 #define dhcpINITIAL_DHCP_TX_PERIOD ( 5000 / portTICK_RATE_MS )
\r
79 #define dhcpINITIAL_TIMER_PERIOD ( 250 / portTICK_RATE_MS )
\r
80 #define dhcpMAX_TIME_TO_WAIT_FOR_ACK ( 5000 / portTICK_RATE_MS )
\r
81 #endif /* _WINDOWS_ */
\r
83 /* Codes of interest found in the DHCP options field. */
\r
84 #define dhcpSUBNET_MASK_OPTION_CODE ( 1 )
\r
85 #define dhcpGATEWAY_OPTION_CODE ( 3 )
\r
86 #define hdcpDNS_SERVER_OPTIONS_CODE ( 6 )
\r
87 #define dhcpMESSAGE_TYPE_OPTION_CODE ( 53 )
\r
88 #define dhcpLEASE_TIME_OPTION_CODE ( 51 )
\r
89 #define dhcpCLIENT_IDENTIFIER_OPTION_CODE ( 61 )
\r
90 #define dhcpPARAMETER_REQUEST_OPTION_CODE ( 55 )
\r
91 #define dhcpREQUEST_IP_ADDRESS_OPTION_CODE ( 50 )
\r
92 #define dhcpSERVER_IP_ADDRESS_OPTION_CODE ( 54 )
\r
94 /* The four DHCP message types of interest. */
\r
95 #define dhcpMESSAGE_TYPE_DISCOVER ( 1 )
\r
96 #define dhcpMESSAGE_TYPE_OFFER ( 2 )
\r
97 #define dhcpMESSAGE_TYPE_REQUEST ( 3 )
\r
98 #define dhcpMESSAGE_TYPE_ACK ( 5 )
\r
99 #define dhcpMESSAGE_TYPE_NACK ( 6 )
\r
101 /* Offsets into the transmitted DHCP options fields at which various parameters
\r
103 #define dhcpCLIENT_IDENTIFIER_OFFSET ( 5 )
\r
104 #define dhcpREQUESTED_IP_ADDRESS_OFFSET ( 13 )
\r
105 #define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ( 19 )
\r
107 /* Values used in the DHCP packets. */
\r
108 #define dhcpREQUEST_OPCODE ( 1 )
\r
109 #define dhcpREPLY_OPCODE ( 2 )
\r
110 #define dhcpADDRESS_TYPE_ETHERNET ( 1 )
\r
111 #define dhcpETHERNET_ADDRESS_LENGTH ( 6 )
\r
113 /* If a lease time is not received, use the default of two days. */
\r
114 #define dhcpDEFAULT_LEASE_TIME ( ( 48UL * 60UL * 60UL * 1000UL ) / portTICK_RATE_MS ) /* 48 hours in ticks. */
\r
116 /* Don't allow the lease time to be too short. */
\r
117 #define dhcpMINIMUM_LEASE_TIME ( 60000UL / portTICK_RATE_MS ) /* 60 seconds in ticks. */
\r
119 /* Marks the end of the variable length options field in the DHCP packet. */
\r
120 #define dhcpOPTION_END_BYTE 0xff
\r
122 /* Offset into a DHCP message at which the first byte of the options is
\r
124 #define dhcpFIRST_OPTION_BYTE_OFFSET ( 0xf0 )
\r
126 /* When walking the variable length options field, the following value is used
\r
127 to ensure the walk has not gone past the end of the valid options. 2 bytes is
\r
128 made up of the length byte, and minimum one byte value. */
\r
129 #define dhcpMAX_OPTION_LENGTH_OF_INTEREST ( 2L )
\r
131 /* Standard DHCP port numbers and magic cookie value. */
\r
132 #if( ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN )
\r
133 #define dhcpCLIENT_PORT 0x4400
\r
134 #define dhcpSERVER_PORT 0x4300
\r
135 #define dhcpCOOKIE 0x63538263
\r
137 #define dhcpCLIENT_PORT 0x0044
\r
138 #define dhcpSERVER_PORT 0x0043
\r
139 #define dhcpCOOKIE 0x63825363
\r
140 #endif /* ipconfigBYTE_ORDER */
\r
142 #include "pack_struct_start.h"
\r
143 struct xDHCPMessage
\r
146 uint8_t ucAddressType;
\r
147 uint8_t ucAddressLength;
\r
149 uint32_t ulTransactionID;
\r
150 uint16_t usElapsedTime;
\r
152 uint32_t ulClientIPAddress_ciaddr;
\r
153 uint32_t ulYourIPAddress_yiaddr;
\r
154 uint32_t ulServerIPAddress_siaddr;
\r
155 uint32_t ulRelayAgentIPAddress_giaddr;
\r
156 uint8_t ucClientHardwareAddress[ dhcpCLIENT_HARDWARE_ADDRESS_LENGTH ];
\r
157 uint8_t ucServerHostName[ dhcpSERVER_HOST_NAME_LENGTH ];
\r
158 uint8_t ucBootFileName[ dhcpBOOT_FILE_NAME_LENGTH ];
\r
159 uint32_t ulDHCPCookie;
\r
160 uint8_t ucFirstOptionByte;
\r
162 #include "pack_struct_end.h"
\r
163 typedef struct xDHCPMessage xDHCPMessage_t;
\r
165 /* DHCP state machine states. */
\r
168 eWaitingSendFirstDiscover = 0, /* Initial state. Send a discover the first time it is called, and reset all timers. */
\r
169 eWaitingOffer, /* Either resend the discover, or, if the offer is forthcoming, send a request. */
\r
170 eWaitingAcknowledge, /* Either resend the request. */
\r
171 eLeasedAddress, /* Resend the request at the appropriate time to renew the lease. */
\r
172 eNotUsingLeasedAddress /* DHCP failed, and a default IP address is being used. */
\r
176 * Generate a DHCP discover message and send it on the DHCP socket.
\r
178 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress );
\r
181 * Interpret message received on the DHCP socket.
\r
183 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing );
\r
186 * Generate a DHCP request packet, and send it on the DHCP socket.
\r
188 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress );
\r
191 * Prepare to start a DHCP transaction. This initialises some state variables
\r
192 * and creates the DHCP socket if necessary.
\r
194 static void prvInitialiseDHCP( void );
\r
197 * Creates the part of outgoing DHCP messages that are common to all outgoing
\r
200 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize );
\r
203 * Create the DHCP socket, if it has not been created already.
\r
205 static void prvCreateDHCPSocket( void );
\r
207 /*-----------------------------------------------------------*/
\r
209 /* The timer used to drive the DHCP state machine. */
\r
210 static xTimerHandle xDHCPTimer = NULL;
\r
212 /* The UDP socket used for all incoming and outgoing DHCP traffic. */
\r
213 static xSocket_t xDHCPSocket = NULL;
\r
215 /* Hold information in between steps in the DHCP state machine. */
\r
216 static uint32_t ulTransactionId = 0UL, ulOfferedIPAddress = 0UL, ulDHCPServerAddress = 0UL, ulLeaseTime = 0;
\r
218 /* Hold information on the current timer state. */
\r
219 static portTickType xDHCPTxTime = 0x00, xDHCPTxPeriod = 0x00;
\r
221 /* Maintains the DHCP state machine state. */
\r
222 static eDHCPState_t eDHCPState = eWaitingSendFirstDiscover;
\r
224 /*-----------------------------------------------------------*/
\r
226 void vDHCPProcess( portBASE_TYPE xReset, xMACAddress_t *pxMACAddress, uint32_t *pulIPAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )
\r
228 if( xReset != pdFALSE )
\r
230 eDHCPState = eWaitingSendFirstDiscover;
\r
233 switch( eDHCPState )
\r
235 case eWaitingSendFirstDiscover :
\r
237 /* Initial state. Create the DHCP socket, timer, etc. if they
\r
238 have not already been created. */
\r
239 prvInitialiseDHCP();
\r
240 *pulIPAddress = 0UL;
\r
242 /* Send the first discover request. */
\r
243 if( xDHCPSocket != NULL )
\r
245 xDHCPTxTime = xTaskGetTickCount();
\r
246 prvSendDHCPDiscover( pxMACAddress );
\r
247 eDHCPState = eWaitingOffer;
\r
251 case eWaitingOffer :
\r
253 /* Look for offers coming in. */
\r
254 if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER, pxMACAddress, pxNetworkAddressing ) == pdPASS )
\r
256 /* An offer has been made, generate the request. */
\r
257 xDHCPTxTime = xTaskGetTickCount();
\r
258 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
259 prvSendDHCPRequest( pxMACAddress );
\r
260 eDHCPState = eWaitingAcknowledge;
\r
264 /* Is it time to send another Discover? */
\r
265 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )
\r
267 /* Increase the time period, and if it has not got to the
\r
268 point of giving up - send another discovery. */
\r
269 xDHCPTxPeriod <<= 1;
\r
270 if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
\r
273 xDHCPTxTime = xTaskGetTickCount();
\r
274 prvSendDHCPDiscover( pxMACAddress );
\r
278 /* Revert to static IP address. */
\r
279 taskENTER_CRITICAL();
\r
281 *pulIPAddress = pxNetworkAddressing->ulDefaultIPAddress;
\r
282 iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( pxNetworkAddressing->ulDefaultIPAddress );
\r
284 taskEXIT_CRITICAL();
\r
285 eDHCPState = eNotUsingLeasedAddress;
\r
286 xTimerStop( xDHCPTimer, ( portTickType ) 0 );
\r
288 #if ipconfigFREERTOS_PLUS_NABTO == 1
\r
292 #endif /* ipconfigFREERTOS_PLUS_NABTO */
\r
294 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1
\r
296 vApplicationIPNetworkEventHook( eNetworkUp );
\r
300 /* Close socket to ensure packets don't queue on it. */
\r
301 FreeRTOS_closesocket( xDHCPSocket );
\r
302 xDHCPSocket = NULL;
\r
308 case eWaitingAcknowledge :
\r
310 /* Look for acks coming in. */
\r
311 if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK, pxMACAddress, pxNetworkAddressing ) == pdPASS )
\r
313 /* DHCP completed. The IP address can now be used, and the
\r
314 timer set to the lease timeout time. */
\r
315 *pulIPAddress = ulOfferedIPAddress;
\r
316 eDHCPState = eLeasedAddress;
\r
318 #if ipconfigFREERTOS_PLUS_NABTO == 1
\r
322 #endif /* ipconfigFREERTOS_PLUS_NABTO */
\r
324 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1
\r
326 vApplicationIPNetworkEventHook( eNetworkUp );
\r
330 /* Close socket to ensure packets don't queue on it. */
\r
331 FreeRTOS_closesocket( xDHCPSocket );
\r
332 xDHCPSocket = NULL;
\r
334 if( ulLeaseTime == 0UL )
\r
336 ulLeaseTime = dhcpDEFAULT_LEASE_TIME;
\r
338 else if( ulLeaseTime < dhcpMINIMUM_LEASE_TIME )
\r
340 ulLeaseTime = dhcpMINIMUM_LEASE_TIME;
\r
344 /* The lease time is already valid. */
\r
347 xTimerChangePeriod( xDHCPTimer, ulLeaseTime, portMAX_DELAY );
\r
351 /* Is it time to send another Discover? */
\r
352 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )
\r
354 /* Increase the time period, and if it has not got to the
\r
355 point of giving up - send another request. */
\r
356 xDHCPTxPeriod <<= 1;
\r
357 if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
\r
359 xDHCPTxTime = xTaskGetTickCount();
\r
360 prvSendDHCPRequest( pxMACAddress );
\r
364 /* Give up, start again. */
\r
365 eDHCPState = eWaitingSendFirstDiscover;
\r
371 case eLeasedAddress :
\r
373 /* Resend the request at the appropriate time to renew the lease. */
\r
374 prvCreateDHCPSocket();
\r
375 if( xDHCPSocket != NULL )
\r
377 xDHCPTxTime = xTaskGetTickCount();
\r
378 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
379 prvSendDHCPRequest( pxMACAddress );
\r
380 eDHCPState = eWaitingAcknowledge;
\r
382 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );
\r
385 case eNotUsingLeasedAddress:
\r
386 xTimerStop( xDHCPTimer, ( portTickType ) 0 );
\r
390 /*-----------------------------------------------------------*/
\r
392 static void prvCreateDHCPSocket( void )
\r
394 struct freertos_sockaddr xAddress;
\r
395 portBASE_TYPE xReturn;
\r
396 portTickType xTimeoutTime = 0;
\r
398 /* Create the socket, if it has not already been created. */
\r
399 if( xDHCPSocket == NULL )
\r
401 xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
402 configASSERT( ( xDHCPSocket != FREERTOS_INVALID_SOCKET ) );
\r
404 /* Ensure the Rx and Tx timeouts are zero as the DHCP executes in the
\r
405 context of the IP task. */
\r
406 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );
\r
407 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );
\r
409 /* Bind to the standard DHCP client port. */
\r
410 xAddress.sin_port = dhcpCLIENT_PORT;
\r
411 xReturn = FreeRTOS_bind( xDHCPSocket, &xAddress, sizeof( xAddress ) );
\r
412 configASSERT( xReturn == 0 );
\r
414 /* Remove compiler warnings if configASSERT() is not defined. */
\r
418 /*-----------------------------------------------------------*/
\r
420 static void prvInitialiseDHCP( void )
\r
422 extern void vIPFunctionsTimerCallback( xTimerHandle xTimer );
\r
424 /* Initialise the parameters that will be set by the DHCP process. */
\r
425 if( ulTransactionId == 0 )
\r
427 ulTransactionId = ipconfigRAND32();
\r
433 ulOfferedIPAddress = 0UL;
\r
434 ulDHCPServerAddress = 0UL;
\r
435 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
437 /* Create the DHCP socket if it has not already been created. */
\r
438 prvCreateDHCPSocket();
\r
440 if( xDHCPTimer == NULL )
\r
442 xDHCPTimer = xTimerCreate( ( const signed char * const ) "DHCP", dhcpINITIAL_TIMER_PERIOD, pdTRUE, ( void * ) eDHCPEvent, vIPFunctionsTimerCallback );
\r
443 configASSERT( xDHCPTimer );
\r
444 xTimerStart( xDHCPTimer, portMAX_DELAY );
\r
448 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );
\r
451 /*-----------------------------------------------------------*/
\r
453 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )
\r
455 uint8_t *pucUDPPayload, *pucLastByte;
\r
456 struct freertos_sockaddr xClient;
\r
457 uint32_t xClientLength = sizeof( xClient );
\r
459 xDHCPMessage_t *pxDHCPMessage;
\r
460 uint8_t *pucByte, ucOptionCode, ucLength;
\r
461 uint32_t ulProcessed;
\r
462 portBASE_TYPE xReturn = pdFALSE;
\r
463 const uint32_t ulMandatoryOptions = 2; /* DHCP server address, and the correct DHCP message type must be present in the options. */
\r
465 lBytes = FreeRTOS_recvfrom( xDHCPSocket, ( void * ) &pucUDPPayload, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\r
469 /* Map a DHCP structure onto the received data. */
\r
470 pxDHCPMessage = ( xDHCPMessage_t * ) ( pucUDPPayload );
\r
472 /* Sanity check. */
\r
473 if( ( pxDHCPMessage->ulDHCPCookie == dhcpCOOKIE ) && ( pxDHCPMessage->ucOpcode == dhcpREPLY_OPCODE ) && ( pxDHCPMessage->ulTransactionID == ulTransactionId ) )
\r
475 if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) ) == 0 )
\r
477 /* None of the essential options have been processed yet. */
\r
480 /* Walk through the options until the dhcpOPTION_END_BYTE byte
\r
481 is found, taking care not to walk off the end of the options. */
\r
482 pucByte = &( pxDHCPMessage->ucFirstOptionByte );
\r
483 pucLastByte = &( pucUDPPayload[ lBytes - dhcpMAX_OPTION_LENGTH_OF_INTEREST ] );
\r
484 while( ( *pucByte != dhcpOPTION_END_BYTE ) && ( pucByte < pucLastByte ) )
\r
486 ucOptionCode = *pucByte;
\r
488 ucLength = *pucByte;
\r
491 switch( ucOptionCode )
\r
493 case dhcpMESSAGE_TYPE_OPTION_CODE :
\r
495 if( *pucByte == ucExpectedMessageType )
\r
497 /* The message type is the message type the
\r
498 state machine is expecting. */
\r
501 else if( *pucByte == dhcpMESSAGE_TYPE_NACK )
\r
503 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_ACK )
\r
506 eDHCPState = eWaitingSendFirstDiscover;
\r
511 /* Don't process other message types. */
\r
515 case dhcpSUBNET_MASK_OPTION_CODE :
\r
517 if( ucLength == sizeof( uint32_t ) )
\r
519 memcpy( ( void * ) &( pxNetworkAddressing->ulNetMask ), ( void * ) pucByte, ( size_t ) ucLength );
\r
523 case dhcpGATEWAY_OPTION_CODE :
\r
525 if( ucLength == sizeof( uint32_t ) )
\r
527 /* ulProcessed is not incremented in this case
\r
528 because the gateway is not essential. */
\r
529 memcpy( ( void * ) &( pxNetworkAddressing->ulGatewayAddress ), ( void * ) pucByte, ( size_t ) ucLength );
\r
533 case hdcpDNS_SERVER_OPTIONS_CODE :
\r
535 /* ulProcessed is not incremented in this case
\r
536 because the DNS server is not essential. Only the
\r
537 first DNS server address is taken. */
\r
538 memcpy( ( void * ) &( pxNetworkAddressing->ulDNSServerAddress ), ( void * ) pucByte, sizeof( uint32_t ) );
\r
541 case dhcpSERVER_IP_ADDRESS_OPTION_CODE :
\r
543 if( ucLength == sizeof( uint32_t ) )
\r
545 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_OFFER )
\r
547 /* Offers state the replying server. */
\r
549 memcpy( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength );
\r
553 /* The ack must come from the expected server. */
\r
554 if( memcmp( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength ) == 0 )
\r
562 case dhcpLEASE_TIME_OPTION_CODE :
\r
564 if( ucLength == sizeof( &ulLeaseTime ) )
\r
566 /* ulProcessed is not incremented in this case
\r
567 because the lease time is not essential. */
\r
568 memcpy( ( void * ) &ulLeaseTime, ( void * ) pucByte, ( size_t ) ucLength );
\r
569 ulLeaseTime = FreeRTOS_ntohl( ulLeaseTime );
\r
571 /* Convert the lease time to milliseconds
\r
572 (*1000) then ticks (/portTICK_RATE_MS). */
\r
573 ulLeaseTime *= ( 1000UL / portTICK_RATE_MS );
\r
575 /* Divide the lease time to ensure a renew
\r
576 request is sent before the lease actually
\r
578 ulLeaseTime >>= 1UL;
\r
584 /* Not interested in this field. */
\r
589 /* Jump over the data to find the next option code. */
\r
590 if( ucLength == 0 )
\r
596 pucByte += ucLength;
\r
600 /* Were all the mandatory options received? */
\r
601 if( ulProcessed == ulMandatoryOptions )
\r
603 ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;
\r
609 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );
\r
614 /*-----------------------------------------------------------*/
\r
616 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize )
\r
618 xDHCPMessage_t *pxDHCPMessage;
\r
619 const size_t xRequiredBufferSize = sizeof( xDHCPMessage_t ) + xOptionsArraySize;
\r
620 uint8_t *pucUDPPayloadBuffer;
\r
622 /* Get a buffer. This uses a maximum delay, but the delay will be capped
\r
623 to ipconfigMAX_SEND_BLOCK_TIME_TICKS so the return value still needs to be
\r
627 }while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );
\r
629 pxDHCPMessage = ( xDHCPMessage_t * ) pucUDPPayloadBuffer;
\r
631 /* Most fields need to be zero. */
\r
632 memset( ( void * ) pxDHCPMessage, 0x00, sizeof( xDHCPMessage_t ) );
\r
634 /* Create the message. */
\r
635 pxDHCPMessage->ucOpcode = ucOpcode;
\r
636 pxDHCPMessage->ucAddressType = dhcpADDRESS_TYPE_ETHERNET;
\r
637 pxDHCPMessage->ucAddressLength = dhcpETHERNET_ADDRESS_LENGTH;
\r
638 pxDHCPMessage->ulTransactionID = ulTransactionId;
\r
639 pxDHCPMessage->ulYourIPAddress_yiaddr = ulOfferedIPAddress;
\r
640 pxDHCPMessage->ulDHCPCookie = dhcpCOOKIE;
\r
641 memcpy( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress[ 0 ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );
\r
643 /* Copy in the const part of the options options. */
\r
644 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, xOptionsArraySize );
\r
646 /* Map in the client identifier. */
\r
647 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpCLIENT_IDENTIFIER_OFFSET ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );
\r
649 /* Set the addressing. */
\r
650 pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;
\r
651 pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;
\r
653 return pucUDPPayloadBuffer;
\r
655 /*-----------------------------------------------------------*/
\r
657 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress )
\r
659 uint8_t *pucUDPPayloadBuffer;
\r
660 struct freertos_sockaddr xAddress;
\r
661 static const uint8_t ucDHCPRequestOptions[] =
\r
663 /* Do not change the ordering without also changing
\r
664 dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and
\r
665 dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */
\r
666 dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_REQUEST, /* Message type option. */
\r
667 dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
\r
668 dhcpREQUEST_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address being requested. */
\r
669 dhcpSERVER_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address of the DHCP server. */
\r
670 dhcpOPTION_END_BYTE
\r
673 pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPRequestOptions, sizeof( ucDHCPRequestOptions ) );
\r
675 /* Copy in the IP address being requested. */
\r
676 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpREQUESTED_IP_ADDRESS_OFFSET ] ), ( void * ) &ulOfferedIPAddress, sizeof( ulOfferedIPAddress ) );
\r
678 /* Copy in the address of the DHCP server being used. */
\r
679 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ] ), ( void * ) &ulDHCPServerAddress, sizeof( ulDHCPServerAddress ) );
\r
681 iptraceSENDING_DHCP_REQUEST();
\r
682 if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPRequestOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
\r
684 /* The packet was not successfully queued for sending and must be
\r
685 returned to the stack. */
\r
686 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
689 /*-----------------------------------------------------------*/
\r
691 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress )
\r
693 uint8_t *pucUDPPayloadBuffer;
\r
694 struct freertos_sockaddr xAddress;
\r
695 static const uint8_t ucDHCPDiscoverOptions[] =
\r
697 /* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */
\r
698 dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_DISCOVER, /* Message type option. */
\r
699 dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
\r
700 dhcpPARAMETER_REQUEST_OPTION_CODE, 3, dhcpSUBNET_MASK_OPTION_CODE, dhcpGATEWAY_OPTION_CODE, hdcpDNS_SERVER_OPTIONS_CODE, /* Parameter request option. */
\r
701 dhcpOPTION_END_BYTE
\r
704 pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, sizeof( ucDHCPDiscoverOptions ) );
\r
706 iptraceSENDING_DHCP_DISCOVER();
\r
707 if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPDiscoverOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
\r
709 /* The packet was not successfully queued for sending and must be
\r
710 returned to the stack. */
\r
711 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
714 /*-----------------------------------------------------------*/
\r
716 #endif /* ipconfigUSE_DHCP != 0 */
\r