2 * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.
\r
4 * FreeRTOS+UDP is an add-on component to FreeRTOS. It is not, in itself, part
\r
5 * of the FreeRTOS kernel. FreeRTOS+UDP is licensed separately from FreeRTOS,
\r
6 * and uses a different license to FreeRTOS. FreeRTOS+UDP uses a dual license
\r
7 * model, information on which is provided below:
\r
9 * - Open source licensing -
\r
10 * FreeRTOS+UDP is a free download and may be used, modified and distributed
\r
11 * without charge provided the user adheres to version two of the GNU General
\r
12 * Public license (GPL) and does not remove the copyright notice or this text.
\r
13 * The GPL V2 text is available on the gnu.org web site, and on the following
\r
14 * URL: http://www.FreeRTOS.org/gpl-2.0.txt
\r
16 * - Commercial licensing -
\r
17 * Businesses and individuals who wish to incorporate FreeRTOS+UDP into
\r
18 * proprietary software for redistribution in any form must first obtain a
\r
19 * (very) low cost commercial license - and in-so-doing support the maintenance,
\r
20 * support and further development of the FreeRTOS+UDP product. Commercial
\r
21 * licenses can be obtained from http://shop.freertos.org and do not require any
\r
22 * source files to be changed.
\r
24 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
25 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
26 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
27 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
28 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
29 * implied, expressed, or statutory.
\r
31 * 1 tab == 4 spaces!
\r
33 * http://www.FreeRTOS.org
\r
34 * http://www.FreeRTOS.org/udp
\r
38 /* Standard includes. */
\r
41 /* FreeRTOS includes. */
\r
42 #include "FreeRTOS.h"
\r
47 /* FreeRTOS+UDP includes. */
\r
48 #include "FreeRTOS_UDP_IP.h"
\r
49 #include "FreeRTOS_IP_Private.h"
\r
50 #include "FreeRTOS_DHCP.h"
\r
51 #include "FreeRTOS_Sockets.h"
\r
52 #include "NetworkInterface.h"
\r
53 #include "IPTraceMacroDefaults.h"
\r
55 /* Exclude the entire file if DHCP is not enabled. */
\r
56 #if ipconfigUSE_DHCP != 0
\r
58 #if ( ipconfigUSE_DHCP != 0 ) && ( ipconfigNETWORK_MTU < 586 )
\r
59 /* DHCP must be able to receive an options field of 312 bytes, the fixed
\r
60 part of the DHCP packet is 240 bytes, and the IP/UDP headers take 28 bytes. */
\r
61 #error ipconfigNETWORK_MTU needs to be at least 586 to use DHCP (588 if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS is set to 1)
\r
64 /* Parameter widths in the DHCP packet. */
\r
65 #define dhcpCLIENT_HARDWARE_ADDRESS_LENGTH 16
\r
66 #define dhcpSERVER_HOST_NAME_LENGTH 64
\r
67 #define dhcpBOOT_FILE_NAME_LENGTH 128
\r
69 /* Timer parameters. Windows simulator times are much shorter because simulated
\r
70 time is not real time. */
\r
72 #define dhcpINITIAL_DHCP_TX_PERIOD ( 100 / portTICK_RATE_MS )
\r
73 #define dhcpINITIAL_TIMER_PERIOD ( 10 / portTICK_RATE_MS )
\r
74 #define dhcpMAX_TIME_TO_WAIT_FOR_ACK ( 200 / portTICK_RATE_MS )
\r
76 #define dhcpINITIAL_DHCP_TX_PERIOD ( 5000 / portTICK_RATE_MS )
\r
77 #define dhcpINITIAL_TIMER_PERIOD ( 250 / portTICK_RATE_MS )
\r
78 #define dhcpMAX_TIME_TO_WAIT_FOR_ACK ( 5000 / portTICK_RATE_MS )
\r
79 #endif /* _WINDOWS_ */
\r
81 /* Codes of interest found in the DHCP options field. */
\r
82 #define dhcpSUBNET_MASK_OPTION_CODE ( 1 )
\r
83 #define dhcpGATEWAY_OPTION_CODE ( 3 )
\r
84 #define hdcpDNS_SERVER_OPTIONS_CODE ( 6 )
\r
85 #define dhcpMESSAGE_TYPE_OPTION_CODE ( 53 )
\r
86 #define dhcpLEASE_TIME_OPTION_CODE ( 51 )
\r
87 #define dhcpCLIENT_IDENTIFIER_OPTION_CODE ( 61 )
\r
88 #define dhcpPARAMETER_REQUEST_OPTION_CODE ( 55 )
\r
89 #define dhcpREQUEST_IP_ADDRESS_OPTION_CODE ( 50 )
\r
90 #define dhcpSERVER_IP_ADDRESS_OPTION_CODE ( 54 )
\r
92 /* The four DHCP message types of interest. */
\r
93 #define dhcpMESSAGE_TYPE_DISCOVER ( 1 )
\r
94 #define dhcpMESSAGE_TYPE_OFFER ( 2 )
\r
95 #define dhcpMESSAGE_TYPE_REQUEST ( 3 )
\r
96 #define dhcpMESSAGE_TYPE_ACK ( 5 )
\r
97 #define dhcpMESSAGE_TYPE_NACK ( 6 )
\r
99 /* Offsets into the transmitted DHCP options fields at which various parameters
\r
101 #define dhcpCLIENT_IDENTIFIER_OFFSET ( 5 )
\r
102 #define dhcpREQUESTED_IP_ADDRESS_OFFSET ( 13 )
\r
103 #define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ( 19 )
\r
105 /* Values used in the DHCP packets. */
\r
106 #define dhcpREQUEST_OPCODE ( 1 )
\r
107 #define dhcpREPLY_OPCODE ( 2 )
\r
108 #define dhcpADDRESS_TYPE_ETHERNET ( 1 )
\r
109 #define dhcpETHERNET_ADDRESS_LENGTH ( 6 )
\r
111 /* If a lease time is not received, use the default of two days. */
\r
112 #define dhcpDEFAULT_LEASE_TIME ( ( 48UL * 60UL * 60UL * 1000UL ) / portTICK_RATE_MS ) /* 48 hours in ticks. */
\r
114 /* Don't allow the lease time to be too short. */
\r
115 #define dhcpMINIMUM_LEASE_TIME ( 60000UL / portTICK_RATE_MS ) /* 60 seconds in ticks. */
\r
117 /* Marks the end of the variable length options field in the DHCP packet. */
\r
118 #define dhcpOPTION_END_BYTE 0xff
\r
120 /* Offset into a DHCP message at which the first byte of the options is
\r
122 #define dhcpFIRST_OPTION_BYTE_OFFSET ( 0xf0 )
\r
124 /* When walking the variable length options field, the following value is used
\r
125 to ensure the walk has not gone past the end of the valid options. 2 bytes is
\r
126 made up of the length byte, and minimum one byte value. */
\r
127 #define dhcpMAX_OPTION_LENGTH_OF_INTEREST ( 2L )
\r
129 /* Standard DHCP port numbers and magic cookie value. */
\r
130 #if( ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN )
\r
131 #define dhcpCLIENT_PORT 0x4400
\r
132 #define dhcpSERVER_PORT 0x4300
\r
133 #define dhcpCOOKIE 0x63538263
\r
135 #define dhcpCLIENT_PORT 0x0044
\r
136 #define dhcpSERVER_PORT 0x0043
\r
137 #define dhcpCOOKIE 0x63825363
\r
138 #endif /* ipconfigBYTE_ORDER */
\r
140 #include "pack_struct_start.h"
\r
141 struct xDHCPMessage
\r
144 uint8_t ucAddressType;
\r
145 uint8_t ucAddressLength;
\r
147 uint32_t ulTransactionID;
\r
148 uint16_t usElapsedTime;
\r
150 uint32_t ulClientIPAddress_ciaddr;
\r
151 uint32_t ulYourIPAddress_yiaddr;
\r
152 uint32_t ulServerIPAddress_siaddr;
\r
153 uint32_t ulRelayAgentIPAddress_giaddr;
\r
154 uint8_t ucClientHardwareAddress[ dhcpCLIENT_HARDWARE_ADDRESS_LENGTH ];
\r
155 uint8_t ucServerHostName[ dhcpSERVER_HOST_NAME_LENGTH ];
\r
156 uint8_t ucBootFileName[ dhcpBOOT_FILE_NAME_LENGTH ];
\r
157 uint32_t ulDHCPCookie;
\r
158 uint8_t ucFirstOptionByte;
\r
160 #include "pack_struct_end.h"
\r
161 typedef struct xDHCPMessage xDHCPMessage_t;
\r
163 /* DHCP state machine states. */
\r
166 eWaitingSendFirstDiscover = 0, /* Initial state. Send a discover the first time it is called, and reset all timers. */
\r
167 eWaitingOffer, /* Either resend the discover, or, if the offer is forthcoming, send a request. */
\r
168 eWaitingAcknowledge, /* Either resend the request. */
\r
169 eLeasedAddress, /* Resend the request at the appropriate time to renew the lease. */
\r
170 eNotUsingLeasedAddress /* DHCP failed, and a default IP address is being used. */
\r
174 * Generate a DHCP discover message and send it on the DHCP socket.
\r
176 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress );
\r
179 * Interpret message received on the DHCP socket.
\r
181 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing );
\r
184 * Generate a DHCP request packet, and send it on the DHCP socket.
\r
186 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress );
\r
189 * Prepare to start a DHCP transaction. This initialises some state variables
\r
190 * and creates the DHCP socket if necessary.
\r
192 static void prvInitialiseDHCP( void );
\r
195 * Creates the part of outgoing DHCP messages that are common to all outgoing
\r
198 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize );
\r
201 * Create the DHCP socket, if it has not been created already.
\r
203 static void prvCreateDHCPSocket( void );
\r
205 /*-----------------------------------------------------------*/
\r
207 /* The timer used to drive the DHCP state machine. */
\r
208 static xTimerHandle xDHCPTimer = NULL;
\r
210 /* The UDP socket used for all incoming and outgoing DHCP traffic. */
\r
211 static xSocket_t xDHCPSocket = NULL;
\r
213 /* Hold information in between steps in the DHCP state machine. */
\r
214 static uint32_t ulTransactionId = 0UL, ulOfferedIPAddress = 0UL, ulDHCPServerAddress = 0UL, ulLeaseTime = 0;
\r
216 /* Hold information on the current timer state. */
\r
217 static portTickType xDHCPTxTime = 0x00, xDHCPTxPeriod = 0x00;
\r
219 /* Maintains the DHCP state machine state. */
\r
220 static eDHCPState_t eDHCPState = eWaitingSendFirstDiscover;
\r
222 /*-----------------------------------------------------------*/
\r
224 void vDHCPProcess( portBASE_TYPE xReset, xMACAddress_t *pxMACAddress, uint32_t *pulIPAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )
\r
226 if( xReset != pdFALSE )
\r
228 eDHCPState = eWaitingSendFirstDiscover;
\r
231 switch( eDHCPState )
\r
233 case eWaitingSendFirstDiscover :
\r
235 /* Initial state. Create the DHCP socket, timer, etc. if they
\r
236 have not already been created. */
\r
237 prvInitialiseDHCP();
\r
238 *pulIPAddress = 0UL;
\r
240 /* Send the first discover request. */
\r
241 if( xDHCPSocket != NULL )
\r
243 xDHCPTxTime = xTaskGetTickCount();
\r
244 prvSendDHCPDiscover( pxMACAddress );
\r
245 eDHCPState = eWaitingOffer;
\r
249 case eWaitingOffer :
\r
251 /* Look for offers coming in. */
\r
252 if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER, pxMACAddress, pxNetworkAddressing ) == pdPASS )
\r
254 /* An offer has been made, generate the request. */
\r
255 xDHCPTxTime = xTaskGetTickCount();
\r
256 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
257 prvSendDHCPRequest( pxMACAddress );
\r
258 eDHCPState = eWaitingAcknowledge;
\r
262 /* Is it time to send another Discover? */
\r
263 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )
\r
265 /* Increase the time period, and if it has not got to the
\r
266 point of giving up - send another discovery. */
\r
267 xDHCPTxPeriod <<= 1;
\r
268 if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
\r
271 xDHCPTxTime = xTaskGetTickCount();
\r
272 prvSendDHCPDiscover( pxMACAddress );
\r
276 /* Revert to static IP address. */
\r
277 taskENTER_CRITICAL();
\r
279 *pulIPAddress = pxNetworkAddressing->ulDefaultIPAddress;
\r
280 iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( pxNetworkAddressing->ulDefaultIPAddress );
\r
282 taskEXIT_CRITICAL();
\r
283 eDHCPState = eNotUsingLeasedAddress;
\r
284 xTimerStop( xDHCPTimer, ( portTickType ) 0 );
\r
286 #if ipconfigFREERTOS_PLUS_NABTO == 1
\r
290 #endif /* ipconfigFREERTOS_PLUS_NABTO */
\r
292 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1
\r
294 vApplicationIPNetworkEventHook( eNetworkUp );
\r
298 /* Close socket to ensure packets don't queue on it. */
\r
299 FreeRTOS_closesocket( xDHCPSocket );
\r
300 xDHCPSocket = NULL;
\r
306 case eWaitingAcknowledge :
\r
308 /* Look for acks coming in. */
\r
309 if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK, pxMACAddress, pxNetworkAddressing ) == pdPASS )
\r
311 /* DHCP completed. The IP address can now be used, and the
\r
312 timer set to the lease timeout time. */
\r
313 *pulIPAddress = ulOfferedIPAddress;
\r
314 eDHCPState = eLeasedAddress;
\r
316 #if ipconfigFREERTOS_PLUS_NABTO == 1
\r
320 #endif /* ipconfigFREERTOS_PLUS_NABTO */
\r
322 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1
\r
324 vApplicationIPNetworkEventHook( eNetworkUp );
\r
328 /* Close socket to ensure packets don't queue on it. */
\r
329 FreeRTOS_closesocket( xDHCPSocket );
\r
330 xDHCPSocket = NULL;
\r
332 if( ulLeaseTime == 0UL )
\r
334 ulLeaseTime = dhcpDEFAULT_LEASE_TIME;
\r
336 else if( ulLeaseTime < dhcpMINIMUM_LEASE_TIME )
\r
338 ulLeaseTime = dhcpMINIMUM_LEASE_TIME;
\r
342 /* The lease time is already valid. */
\r
345 xTimerChangePeriod( xDHCPTimer, ulLeaseTime, portMAX_DELAY );
\r
349 /* Is it time to send another Discover? */
\r
350 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )
\r
352 /* Increase the time period, and if it has not got to the
\r
353 point of giving up - send another request. */
\r
354 xDHCPTxPeriod <<= 1;
\r
355 if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
\r
357 xDHCPTxTime = xTaskGetTickCount();
\r
358 prvSendDHCPRequest( pxMACAddress );
\r
362 /* Give up, start again. */
\r
363 eDHCPState = eWaitingSendFirstDiscover;
\r
369 case eLeasedAddress :
\r
371 /* Resend the request at the appropriate time to renew the lease. */
\r
372 prvCreateDHCPSocket();
\r
373 if( xDHCPSocket != NULL )
\r
375 xDHCPTxTime = xTaskGetTickCount();
\r
376 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
377 prvSendDHCPRequest( pxMACAddress );
\r
378 eDHCPState = eWaitingAcknowledge;
\r
380 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );
\r
383 case eNotUsingLeasedAddress:
\r
384 xTimerStop( xDHCPTimer, ( portTickType ) 0 );
\r
388 /*-----------------------------------------------------------*/
\r
390 static void prvCreateDHCPSocket( void )
\r
392 struct freertos_sockaddr xAddress;
\r
393 portBASE_TYPE xReturn;
\r
394 portTickType xTimeoutTime = 0;
\r
396 /* Create the socket, if it has not already been created. */
\r
397 if( xDHCPSocket == NULL )
\r
399 xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
400 configASSERT( ( xDHCPSocket != FREERTOS_INVALID_SOCKET ) );
\r
402 /* Ensure the Rx and Tx timeouts are zero as the DHCP executes in the
\r
403 context of the IP task. */
\r
404 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );
\r
405 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );
\r
407 /* Bind to the standard DHCP client port. */
\r
408 xAddress.sin_port = dhcpCLIENT_PORT;
\r
409 xReturn = FreeRTOS_bind( xDHCPSocket, &xAddress, sizeof( xAddress ) );
\r
410 configASSERT( xReturn == 0 );
\r
412 /* Remove compiler warnings if configASSERT() is not defined. */
\r
416 /*-----------------------------------------------------------*/
\r
418 static void prvInitialiseDHCP( void )
\r
420 extern void vIPFunctionsTimerCallback( xTimerHandle xTimer );
\r
422 /* Initialise the parameters that will be set by the DHCP process. */
\r
423 if( ulTransactionId == 0 )
\r
425 ulTransactionId = ipconfigRAND32();
\r
431 ulOfferedIPAddress = 0UL;
\r
432 ulDHCPServerAddress = 0UL;
\r
433 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
435 /* Create the DHCP socket if it has not already been created. */
\r
436 prvCreateDHCPSocket();
\r
438 if( xDHCPTimer == NULL )
\r
440 xDHCPTimer = xTimerCreate( ( const signed char * const ) "DHCP", dhcpINITIAL_TIMER_PERIOD, pdTRUE, ( void * ) eDHCPEvent, vIPFunctionsTimerCallback );
\r
441 configASSERT( xDHCPTimer );
\r
442 xTimerStart( xDHCPTimer, portMAX_DELAY );
\r
446 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );
\r
449 /*-----------------------------------------------------------*/
\r
451 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )
\r
453 uint8_t *pucUDPPayload, *pucLastByte;
\r
454 struct freertos_sockaddr xClient;
\r
455 uint32_t xClientLength = sizeof( xClient );
\r
457 xDHCPMessage_t *pxDHCPMessage;
\r
458 uint8_t *pucByte, ucOptionCode, ucLength;
\r
459 uint32_t ulProcessed;
\r
460 portBASE_TYPE xReturn = pdFALSE;
\r
461 const uint32_t ulMandatoryOptions = 2; /* DHCP server address, and the correct DHCP message type must be present in the options. */
\r
463 lBytes = FreeRTOS_recvfrom( xDHCPSocket, ( void * ) &pucUDPPayload, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\r
467 /* Map a DHCP structure onto the received data. */
\r
468 pxDHCPMessage = ( xDHCPMessage_t * ) ( pucUDPPayload );
\r
470 /* Sanity check. */
\r
471 if( ( pxDHCPMessage->ulDHCPCookie == dhcpCOOKIE ) && ( pxDHCPMessage->ucOpcode == dhcpREPLY_OPCODE ) && ( pxDHCPMessage->ulTransactionID == ulTransactionId ) )
\r
473 if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) ) == 0 )
\r
475 /* None of the essential options have been processed yet. */
\r
478 /* Walk through the options until the dhcpOPTION_END_BYTE byte
\r
479 is found, taking care not to walk off the end of the options. */
\r
480 pucByte = &( pxDHCPMessage->ucFirstOptionByte );
\r
481 pucLastByte = &( pucUDPPayload[ lBytes - dhcpMAX_OPTION_LENGTH_OF_INTEREST ] );
\r
482 while( ( *pucByte != dhcpOPTION_END_BYTE ) && ( pucByte < pucLastByte ) )
\r
484 ucOptionCode = *pucByte;
\r
486 ucLength = *pucByte;
\r
489 switch( ucOptionCode )
\r
491 case dhcpMESSAGE_TYPE_OPTION_CODE :
\r
493 if( *pucByte == ucExpectedMessageType )
\r
495 /* The message type is the message type the
\r
496 state machine is expecting. */
\r
499 else if( *pucByte == dhcpMESSAGE_TYPE_NACK )
\r
501 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_ACK )
\r
504 eDHCPState = eWaitingSendFirstDiscover;
\r
509 /* Don't process other message types. */
\r
513 case dhcpSUBNET_MASK_OPTION_CODE :
\r
515 if( ucLength == sizeof( uint32_t ) )
\r
517 memcpy( ( void * ) &( pxNetworkAddressing->ulNetMask ), ( void * ) pucByte, ( size_t ) ucLength );
\r
521 case dhcpGATEWAY_OPTION_CODE :
\r
523 if( ucLength == sizeof( uint32_t ) )
\r
525 /* ulProcessed is not incremented in this case
\r
526 because the gateway is not essential. */
\r
527 memcpy( ( void * ) &( pxNetworkAddressing->ulGatewayAddress ), ( void * ) pucByte, ( size_t ) ucLength );
\r
531 case hdcpDNS_SERVER_OPTIONS_CODE :
\r
533 /* ulProcessed is not incremented in this case
\r
534 because the DNS server is not essential. Only the
\r
535 first DNS server address is taken. */
\r
536 memcpy( ( void * ) &( pxNetworkAddressing->ulDNSServerAddress ), ( void * ) pucByte, sizeof( uint32_t ) );
\r
539 case dhcpSERVER_IP_ADDRESS_OPTION_CODE :
\r
541 if( ucLength == sizeof( uint32_t ) )
\r
543 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_OFFER )
\r
545 /* Offers state the replying server. */
\r
547 memcpy( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength );
\r
551 /* The ack must come from the expected server. */
\r
552 if( memcmp( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength ) == 0 )
\r
560 case dhcpLEASE_TIME_OPTION_CODE :
\r
562 if( ucLength == sizeof( &ulLeaseTime ) )
\r
564 /* ulProcessed is not incremented in this case
\r
565 because the lease time is not essential. */
\r
566 memcpy( ( void * ) &ulLeaseTime, ( void * ) pucByte, ( size_t ) ucLength );
\r
567 ulLeaseTime = FreeRTOS_ntohl( ulLeaseTime );
\r
569 /* Convert the lease time to milliseconds
\r
570 (*1000) then ticks (/portTICK_RATE_MS). */
\r
571 ulLeaseTime *= ( 1000UL / portTICK_RATE_MS );
\r
573 /* Divide the lease time to ensure a renew
\r
574 request is sent before the lease actually
\r
576 ulLeaseTime >>= 1UL;
\r
582 /* Not interested in this field. */
\r
587 /* Jump over the data to find the next option code. */
\r
588 if( ucLength == 0 )
\r
594 pucByte += ucLength;
\r
598 /* Were all the mandatory options received? */
\r
599 if( ulProcessed == ulMandatoryOptions )
\r
601 ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;
\r
607 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );
\r
612 /*-----------------------------------------------------------*/
\r
614 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize )
\r
616 xDHCPMessage_t *pxDHCPMessage;
\r
617 const size_t xRequiredBufferSize = sizeof( xDHCPMessage_t ) + xOptionsArraySize;
\r
618 uint8_t *pucUDPPayloadBuffer;
\r
620 /* Get a buffer. This uses a maximum delay, but the delay will be capped
\r
621 to ipconfigMAX_SEND_BLOCK_TIME_TICKS so the return value still needs to be
\r
625 }while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );
\r
627 pxDHCPMessage = ( xDHCPMessage_t * ) pucUDPPayloadBuffer;
\r
629 /* Most fields need to be zero. */
\r
630 memset( ( void * ) pxDHCPMessage, 0x00, sizeof( xDHCPMessage_t ) );
\r
632 /* Create the message. */
\r
633 pxDHCPMessage->ucOpcode = ucOpcode;
\r
634 pxDHCPMessage->ucAddressType = dhcpADDRESS_TYPE_ETHERNET;
\r
635 pxDHCPMessage->ucAddressLength = dhcpETHERNET_ADDRESS_LENGTH;
\r
636 pxDHCPMessage->ulTransactionID = ulTransactionId;
\r
637 pxDHCPMessage->ulYourIPAddress_yiaddr = ulOfferedIPAddress;
\r
638 pxDHCPMessage->ulDHCPCookie = dhcpCOOKIE;
\r
639 memcpy( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress[ 0 ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );
\r
641 /* Copy in the const part of the options options. */
\r
642 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, xOptionsArraySize );
\r
644 /* Map in the client identifier. */
\r
645 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpCLIENT_IDENTIFIER_OFFSET ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );
\r
647 /* Set the addressing. */
\r
648 pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;
\r
649 pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;
\r
651 return pucUDPPayloadBuffer;
\r
653 /*-----------------------------------------------------------*/
\r
655 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress )
\r
657 uint8_t *pucUDPPayloadBuffer;
\r
658 struct freertos_sockaddr xAddress;
\r
659 static const uint8_t ucDHCPRequestOptions[] =
\r
661 /* Do not change the ordering without also changing
\r
662 dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and
\r
663 dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */
\r
664 dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_REQUEST, /* Message type option. */
\r
665 dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
\r
666 dhcpREQUEST_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address being requested. */
\r
667 dhcpSERVER_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address of the DHCP server. */
\r
668 dhcpOPTION_END_BYTE
\r
671 pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPRequestOptions, sizeof( ucDHCPRequestOptions ) );
\r
673 /* Copy in the IP address being requested. */
\r
674 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpREQUESTED_IP_ADDRESS_OFFSET ] ), ( void * ) &ulOfferedIPAddress, sizeof( ulOfferedIPAddress ) );
\r
676 /* Copy in the address of the DHCP server being used. */
\r
677 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ] ), ( void * ) &ulDHCPServerAddress, sizeof( ulDHCPServerAddress ) );
\r
679 iptraceSENDING_DHCP_REQUEST();
\r
680 if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPRequestOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
\r
682 /* The packet was not successfully queued for sending and must be
\r
683 returned to the stack. */
\r
684 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
687 /*-----------------------------------------------------------*/
\r
689 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress )
\r
691 uint8_t *pucUDPPayloadBuffer;
\r
692 struct freertos_sockaddr xAddress;
\r
693 static const uint8_t ucDHCPDiscoverOptions[] =
\r
695 /* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */
\r
696 dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_DISCOVER, /* Message type option. */
\r
697 dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
\r
698 dhcpPARAMETER_REQUEST_OPTION_CODE, 3, dhcpSUBNET_MASK_OPTION_CODE, dhcpGATEWAY_OPTION_CODE, hdcpDNS_SERVER_OPTIONS_CODE, /* Parameter request option. */
\r
699 dhcpOPTION_END_BYTE
\r
702 pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, sizeof( ucDHCPDiscoverOptions ) );
\r
704 iptraceSENDING_DHCP_DISCOVER();
\r
705 if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPDiscoverOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
\r
707 /* The packet was not successfully queued for sending and must be
\r
708 returned to the stack. */
\r
709 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
712 /*-----------------------------------------------------------*/
\r
714 #endif /* ipconfigUSE_DHCP != 0 */
\r