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 standard GPL open source license, or a commercial license. The
\r
9 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
10 * itself is distributed) requires that all software statically linked with
\r
11 * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.
\r
12 * Details of both license options follow:
\r
14 * - Open source licensing -
\r
15 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
16 * distributed without charge provided the user adheres to version two of the
\r
17 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
18 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
19 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
21 * - Commercial licensing -
\r
22 * Businesses and individuals that for commercial or other reasons cannot comply
\r
23 * with the terms of the GPL V2 license must obtain a commercial license before
\r
24 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
25 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
26 * and do not require any source files to be changed.
\r
28 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
29 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
30 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
31 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
32 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
33 * implied, expressed, or statutory.
\r
35 * 1 tab == 4 spaces!
\r
37 * http://www.FreeRTOS.org
\r
38 * http://www.FreeRTOS.org/udp
\r
42 /* Standard includes. */
\r
45 /* FreeRTOS includes. */
\r
46 #include "FreeRTOS.h"
\r
51 /* FreeRTOS+UDP includes. */
\r
52 #include "FreeRTOS_UDP_IP.h"
\r
53 #include "FreeRTOS_IP_Private.h"
\r
54 #include "FreeRTOS_DHCP.h"
\r
55 #include "FreeRTOS_Sockets.h"
\r
56 #include "NetworkInterface.h"
\r
57 #include "IPTraceMacroDefaults.h"
\r
59 /* Exclude the entire file if DHCP is not enabled. */
\r
60 #if ipconfigUSE_DHCP != 0
\r
62 #if ( ipconfigUSE_DHCP != 0 ) && ( ipconfigNETWORK_MTU < 586 )
\r
63 /* DHCP must be able to receive an options field of 312 bytes, the fixed
\r
64 part of the DHCP packet is 240 bytes, and the IP/UDP headers take 28 bytes. */
\r
65 #error ipconfigNETWORK_MTU needs to be at least 586 to use DHCP (588 if ipconfigCAN_FRAGMENT_OUTGOING_PACKETS is set to 1)
\r
68 /* Parameter widths in the DHCP packet. */
\r
69 #define dhcpCLIENT_HARDWARE_ADDRESS_LENGTH 16
\r
70 #define dhcpSERVER_HOST_NAME_LENGTH 64
\r
71 #define dhcpBOOT_FILE_NAME_LENGTH 128
\r
73 /* Timer parameters. Windows simulator times are much shorter because simulated
\r
74 time is not real time. */
\r
76 #define dhcpINITIAL_DHCP_TX_PERIOD ( 100 / portTICK_RATE_MS )
\r
77 #define dhcpINITIAL_TIMER_PERIOD ( 10 / portTICK_RATE_MS )
\r
78 #define dhcpMAX_TIME_TO_WAIT_FOR_ACK ( 200 / portTICK_RATE_MS )
\r
80 #define dhcpINITIAL_DHCP_TX_PERIOD ( 5000 / portTICK_RATE_MS )
\r
81 #define dhcpINITIAL_TIMER_PERIOD ( 250 / portTICK_RATE_MS )
\r
82 #define dhcpMAX_TIME_TO_WAIT_FOR_ACK ( 5000 / portTICK_RATE_MS )
\r
83 #endif /* _WINDOWS_ */
\r
85 /* Codes of interest found in the DHCP options field. */
\r
86 #define dhcpSUBNET_MASK_OPTION_CODE ( 1 )
\r
87 #define dhcpGATEWAY_OPTION_CODE ( 3 )
\r
88 #define hdcpDNS_SERVER_OPTIONS_CODE ( 6 )
\r
89 #define dhcpMESSAGE_TYPE_OPTION_CODE ( 53 )
\r
90 #define dhcpLEASE_TIME_OPTION_CODE ( 51 )
\r
91 #define dhcpCLIENT_IDENTIFIER_OPTION_CODE ( 61 )
\r
92 #define dhcpPARAMETER_REQUEST_OPTION_CODE ( 55 )
\r
93 #define dhcpREQUEST_IP_ADDRESS_OPTION_CODE ( 50 )
\r
94 #define dhcpSERVER_IP_ADDRESS_OPTION_CODE ( 54 )
\r
96 /* The four DHCP message types of interest. */
\r
97 #define dhcpMESSAGE_TYPE_DISCOVER ( 1 )
\r
98 #define dhcpMESSAGE_TYPE_OFFER ( 2 )
\r
99 #define dhcpMESSAGE_TYPE_REQUEST ( 3 )
\r
100 #define dhcpMESSAGE_TYPE_ACK ( 5 )
\r
101 #define dhcpMESSAGE_TYPE_NACK ( 6 )
\r
103 /* Offsets into the transmitted DHCP options fields at which various parameters
\r
105 #define dhcpCLIENT_IDENTIFIER_OFFSET ( 5 )
\r
106 #define dhcpREQUESTED_IP_ADDRESS_OFFSET ( 13 )
\r
107 #define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ( 19 )
\r
109 /* Values used in the DHCP packets. */
\r
110 #define dhcpREQUEST_OPCODE ( 1 )
\r
111 #define dhcpREPLY_OPCODE ( 2 )
\r
112 #define dhcpADDRESS_TYPE_ETHERNET ( 1 )
\r
113 #define dhcpETHERNET_ADDRESS_LENGTH ( 6 )
\r
115 /* If a lease time is not received, use the default of two days. */
\r
116 #define dhcpDEFAULT_LEASE_TIME ( ( 48UL * 60UL * 60UL * 1000UL ) / portTICK_RATE_MS ) /* 48 hours in ticks. */
\r
118 /* Don't allow the lease time to be too short. */
\r
119 #define dhcpMINIMUM_LEASE_TIME ( 60000UL / portTICK_RATE_MS ) /* 60 seconds in ticks. */
\r
121 /* Marks the end of the variable length options field in the DHCP packet. */
\r
122 #define dhcpOPTION_END_BYTE 0xff
\r
124 /* Offset into a DHCP message at which the first byte of the options is
\r
126 #define dhcpFIRST_OPTION_BYTE_OFFSET ( 0xf0 )
\r
128 /* When walking the variable length options field, the following value is used
\r
129 to ensure the walk has not gone past the end of the valid options. 2 bytes is
\r
130 made up of the length byte, and minimum one byte value. */
\r
131 #define dhcpMAX_OPTION_LENGTH_OF_INTEREST ( 2L )
\r
133 /* Standard DHCP port numbers and magic cookie value. */
\r
134 #if( ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN )
\r
135 #define dhcpCLIENT_PORT 0x4400
\r
136 #define dhcpSERVER_PORT 0x4300
\r
137 #define dhcpCOOKIE 0x63538263
\r
138 #define dhcpBROADCAST 0x0080
\r
140 #define dhcpCLIENT_PORT 0x0044
\r
141 #define dhcpSERVER_PORT 0x0043
\r
142 #define dhcpCOOKIE 0x63825363
\r
143 #define dhcpBROADCAST 0x8000
\r
144 #endif /* ipconfigBYTE_ORDER */
\r
146 #include "pack_struct_start.h"
\r
147 struct xDHCPMessage
\r
150 uint8_t ucAddressType;
\r
151 uint8_t ucAddressLength;
\r
153 uint32_t ulTransactionID;
\r
154 uint16_t usElapsedTime;
\r
156 uint32_t ulClientIPAddress_ciaddr;
\r
157 uint32_t ulYourIPAddress_yiaddr;
\r
158 uint32_t ulServerIPAddress_siaddr;
\r
159 uint32_t ulRelayAgentIPAddress_giaddr;
\r
160 uint8_t ucClientHardwareAddress[ dhcpCLIENT_HARDWARE_ADDRESS_LENGTH ];
\r
161 uint8_t ucServerHostName[ dhcpSERVER_HOST_NAME_LENGTH ];
\r
162 uint8_t ucBootFileName[ dhcpBOOT_FILE_NAME_LENGTH ];
\r
163 uint32_t ulDHCPCookie;
\r
164 uint8_t ucFirstOptionByte;
\r
166 #include "pack_struct_end.h"
\r
167 typedef struct xDHCPMessage xDHCPMessage_t;
\r
169 /* DHCP state machine states. */
\r
172 eWaitingSendFirstDiscover = 0, /* Initial state. Send a discover the first time it is called, and reset all timers. */
\r
173 eWaitingOffer, /* Either resend the discover, or, if the offer is forthcoming, send a request. */
\r
174 eWaitingAcknowledge, /* Either resend the request. */
\r
175 eLeasedAddress, /* Resend the request at the appropriate time to renew the lease. */
\r
176 eNotUsingLeasedAddress /* DHCP failed, and a default IP address is being used. */
\r
180 * Generate a DHCP discover message and send it on the DHCP socket.
\r
182 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress );
\r
185 * Interpret message received on the DHCP socket.
\r
187 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing );
\r
190 * Generate a DHCP request packet, and send it on the DHCP socket.
\r
192 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress );
\r
195 * Prepare to start a DHCP transaction. This initialises some state variables
\r
196 * and creates the DHCP socket if necessary.
\r
198 static void prvInitialiseDHCP( void );
\r
201 * Creates the part of outgoing DHCP messages that are common to all outgoing
\r
204 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize );
\r
207 * Create the DHCP socket, if it has not been created already.
\r
209 static void prvCreateDHCPSocket( void );
\r
211 /*-----------------------------------------------------------*/
\r
213 /* The timer used to drive the DHCP state machine. */
\r
214 static xTimerHandle xDHCPTimer = NULL;
\r
216 /* The UDP socket used for all incoming and outgoing DHCP traffic. */
\r
217 static xSocket_t xDHCPSocket = NULL;
\r
219 /* Hold information in between steps in the DHCP state machine. */
\r
220 static uint32_t ulTransactionId = 0UL, ulOfferedIPAddress = 0UL, ulDHCPServerAddress = 0UL, ulLeaseTime = 0;
\r
222 /* Hold information on the current timer state. */
\r
223 static portTickType xDHCPTxTime = 0x00, xDHCPTxPeriod = 0x00;
\r
225 /* Maintains the DHCP state machine state. */
\r
226 static eDHCPState_t eDHCPState = eWaitingSendFirstDiscover;
\r
228 /*-----------------------------------------------------------*/
\r
230 void vDHCPProcess( portBASE_TYPE xReset, xMACAddress_t *pxMACAddress, uint32_t *pulIPAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )
\r
232 if( xReset != pdFALSE )
\r
234 eDHCPState = eWaitingSendFirstDiscover;
\r
237 switch( eDHCPState )
\r
239 case eWaitingSendFirstDiscover :
\r
241 /* Initial state. Create the DHCP socket, timer, etc. if they
\r
242 have not already been created. */
\r
243 prvInitialiseDHCP();
\r
244 *pulIPAddress = 0UL;
\r
246 /* Send the first discover request. */
\r
247 if( xDHCPSocket != NULL )
\r
249 xDHCPTxTime = xTaskGetTickCount();
\r
250 prvSendDHCPDiscover( pxMACAddress );
\r
251 eDHCPState = eWaitingOffer;
\r
255 case eWaitingOffer :
\r
257 /* Look for offers coming in. */
\r
258 if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER, pxMACAddress, pxNetworkAddressing ) == pdPASS )
\r
260 /* An offer has been made, generate the request. */
\r
261 xDHCPTxTime = xTaskGetTickCount();
\r
262 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
263 prvSendDHCPRequest( pxMACAddress );
\r
264 eDHCPState = eWaitingAcknowledge;
\r
268 /* Is it time to send another Discover? */
\r
269 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )
\r
271 /* Increase the time period, and if it has not got to the
\r
272 point of giving up - send another discovery. */
\r
273 xDHCPTxPeriod <<= 1;
\r
274 if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
\r
277 xDHCPTxTime = xTaskGetTickCount();
\r
278 prvSendDHCPDiscover( pxMACAddress );
\r
282 /* Revert to static IP address. */
\r
283 taskENTER_CRITICAL();
\r
285 *pulIPAddress = pxNetworkAddressing->ulDefaultIPAddress;
\r
286 iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( pxNetworkAddressing->ulDefaultIPAddress );
\r
288 taskEXIT_CRITICAL();
\r
289 eDHCPState = eNotUsingLeasedAddress;
\r
290 xTimerStop( xDHCPTimer, ( portTickType ) 0 );
\r
292 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1
\r
294 vApplicationIPNetworkEventHook( eNetworkUp );
\r
298 /* Static configuration is being used, so the network is now up. */
\r
299 #if ipconfigFREERTOS_PLUS_NABTO == 1
\r
301 /* Return value is used in configASSERT() inside the
\r
303 ( void ) xStartNabtoTask();
\r
305 #endif /* ipconfigFREERTOS_PLUS_NABTO */
\r
307 /* Close socket to ensure packets don't queue on it. */
\r
308 FreeRTOS_closesocket( xDHCPSocket );
\r
309 xDHCPSocket = NULL;
\r
315 case eWaitingAcknowledge :
\r
317 /* Look for acks coming in. */
\r
318 if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK, pxMACAddress, pxNetworkAddressing ) == pdPASS )
\r
320 /* DHCP completed. The IP address can now be used, and the
\r
321 timer set to the lease timeout time. */
\r
322 *pulIPAddress = ulOfferedIPAddress;
\r
323 eDHCPState = eLeasedAddress;
\r
325 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1
\r
327 vApplicationIPNetworkEventHook( eNetworkUp );
\r
331 /* Static configuration is being used, so the network is now
\r
333 #if ipconfigFREERTOS_PLUS_NABTO == 1
\r
335 /* Return value is used in configASSERT() inside the
\r
337 ( void ) xStartNabtoTask();
\r
339 #endif /* ipconfigFREERTOS_PLUS_NABTO */
\r
341 /* Close socket to ensure packets don't queue on it. */
\r
342 FreeRTOS_closesocket( xDHCPSocket );
\r
343 xDHCPSocket = NULL;
\r
345 if( ulLeaseTime == 0UL )
\r
347 ulLeaseTime = dhcpDEFAULT_LEASE_TIME;
\r
349 else if( ulLeaseTime < dhcpMINIMUM_LEASE_TIME )
\r
351 ulLeaseTime = dhcpMINIMUM_LEASE_TIME;
\r
355 /* The lease time is already valid. */
\r
358 xTimerChangePeriod( xDHCPTimer, ulLeaseTime, portMAX_DELAY );
\r
362 /* Is it time to send another Discover? */
\r
363 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )
\r
365 /* Increase the time period, and if it has not got to the
\r
366 point of giving up - send another request. */
\r
367 xDHCPTxPeriod <<= 1;
\r
368 if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )
\r
370 xDHCPTxTime = xTaskGetTickCount();
\r
371 prvSendDHCPRequest( pxMACAddress );
\r
375 /* Give up, start again. */
\r
376 eDHCPState = eWaitingSendFirstDiscover;
\r
382 case eLeasedAddress :
\r
384 /* Resend the request at the appropriate time to renew the lease. */
\r
385 prvCreateDHCPSocket();
\r
386 if( xDHCPSocket != NULL )
\r
388 xDHCPTxTime = xTaskGetTickCount();
\r
389 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
390 prvSendDHCPRequest( pxMACAddress );
\r
391 eDHCPState = eWaitingAcknowledge;
\r
393 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );
\r
396 case eNotUsingLeasedAddress:
\r
397 xTimerStop( xDHCPTimer, ( portTickType ) 0 );
\r
401 /*-----------------------------------------------------------*/
\r
403 static void prvCreateDHCPSocket( void )
\r
405 struct freertos_sockaddr xAddress;
\r
406 portBASE_TYPE xReturn;
\r
407 portTickType xTimeoutTime = 0;
\r
409 /* Create the socket, if it has not already been created. */
\r
410 if( xDHCPSocket == NULL )
\r
412 xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
413 configASSERT( ( xDHCPSocket != FREERTOS_INVALID_SOCKET ) );
\r
415 /* Ensure the Rx and Tx timeouts are zero as the DHCP executes in the
\r
416 context of the IP task. */
\r
417 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );
\r
418 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );
\r
420 /* Bind to the standard DHCP client port. */
\r
421 xAddress.sin_port = dhcpCLIENT_PORT;
\r
422 xReturn = FreeRTOS_bind( xDHCPSocket, &xAddress, sizeof( xAddress ) );
\r
423 configASSERT( xReturn == 0 );
\r
425 /* Remove compiler warnings if configASSERT() is not defined. */
\r
429 /*-----------------------------------------------------------*/
\r
431 static void prvInitialiseDHCP( void )
\r
433 extern void vIPFunctionsTimerCallback( xTimerHandle xTimer );
\r
435 /* Initialise the parameters that will be set by the DHCP process. */
\r
436 if( ulTransactionId == 0 )
\r
438 ulTransactionId = ipconfigRAND32();
\r
444 ulOfferedIPAddress = 0UL;
\r
445 ulDHCPServerAddress = 0UL;
\r
446 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;
\r
448 /* Create the DHCP socket if it has not already been created. */
\r
449 prvCreateDHCPSocket();
\r
451 if( xDHCPTimer == NULL )
\r
453 xDHCPTimer = xTimerCreate( ( const signed char * const ) "DHCP", dhcpINITIAL_TIMER_PERIOD, pdTRUE, ( void * ) eDHCPEvent, vIPFunctionsTimerCallback );
\r
454 configASSERT( xDHCPTimer );
\r
455 xTimerStart( xDHCPTimer, portMAX_DELAY );
\r
459 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );
\r
462 /*-----------------------------------------------------------*/
\r
464 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )
\r
466 uint8_t *pucUDPPayload, *pucLastByte;
\r
467 struct freertos_sockaddr xClient;
\r
468 uint32_t xClientLength = sizeof( xClient );
\r
470 xDHCPMessage_t *pxDHCPMessage;
\r
471 uint8_t *pucByte, ucOptionCode, ucLength;
\r
472 uint32_t ulProcessed;
\r
473 portBASE_TYPE xReturn = pdFALSE;
\r
474 const uint32_t ulMandatoryOptions = 2; /* DHCP server address, and the correct DHCP message type must be present in the options. */
\r
476 lBytes = FreeRTOS_recvfrom( xDHCPSocket, ( void * ) &pucUDPPayload, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\r
480 /* Map a DHCP structure onto the received data. */
\r
481 pxDHCPMessage = ( xDHCPMessage_t * ) ( pucUDPPayload );
\r
483 /* Sanity check. */
\r
484 if( ( pxDHCPMessage->ulDHCPCookie == dhcpCOOKIE ) && ( pxDHCPMessage->ucOpcode == dhcpREPLY_OPCODE ) && ( pxDHCPMessage->ulTransactionID == ulTransactionId ) )
\r
486 if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) ) == 0 )
\r
488 /* None of the essential options have been processed yet. */
\r
491 /* Walk through the options until the dhcpOPTION_END_BYTE byte
\r
492 is found, taking care not to walk off the end of the options. */
\r
493 pucByte = &( pxDHCPMessage->ucFirstOptionByte );
\r
494 pucLastByte = &( pucUDPPayload[ lBytes - dhcpMAX_OPTION_LENGTH_OF_INTEREST ] );
\r
495 while( ( *pucByte != dhcpOPTION_END_BYTE ) && ( pucByte < pucLastByte ) )
\r
497 ucOptionCode = *pucByte;
\r
499 ucLength = *pucByte;
\r
502 switch( ucOptionCode )
\r
504 case dhcpMESSAGE_TYPE_OPTION_CODE :
\r
506 if( *pucByte == ucExpectedMessageType )
\r
508 /* The message type is the message type the
\r
509 state machine is expecting. */
\r
512 else if( *pucByte == dhcpMESSAGE_TYPE_NACK )
\r
514 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_ACK )
\r
517 eDHCPState = eWaitingSendFirstDiscover;
\r
522 /* Don't process other message types. */
\r
526 case dhcpSUBNET_MASK_OPTION_CODE :
\r
528 if( ucLength == sizeof( uint32_t ) )
\r
530 memcpy( ( void * ) &( pxNetworkAddressing->ulNetMask ), ( void * ) pucByte, ( size_t ) ucLength );
\r
534 case dhcpGATEWAY_OPTION_CODE :
\r
536 if( ucLength == sizeof( uint32_t ) )
\r
538 /* ulProcessed is not incremented in this case
\r
539 because the gateway is not essential. */
\r
540 memcpy( ( void * ) &( pxNetworkAddressing->ulGatewayAddress ), ( void * ) pucByte, ( size_t ) ucLength );
\r
544 case hdcpDNS_SERVER_OPTIONS_CODE :
\r
546 /* ulProcessed is not incremented in this case
\r
547 because the DNS server is not essential. Only the
\r
548 first DNS server address is taken. */
\r
549 memcpy( ( void * ) &( pxNetworkAddressing->ulDNSServerAddress ), ( void * ) pucByte, sizeof( uint32_t ) );
\r
552 case dhcpSERVER_IP_ADDRESS_OPTION_CODE :
\r
554 if( ucLength == sizeof( uint32_t ) )
\r
556 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_OFFER )
\r
558 /* Offers state the replying server. */
\r
560 memcpy( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength );
\r
564 /* The ack must come from the expected server. */
\r
565 if( memcmp( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength ) == 0 )
\r
573 case dhcpLEASE_TIME_OPTION_CODE :
\r
575 if( ucLength == sizeof( &ulLeaseTime ) )
\r
577 /* ulProcessed is not incremented in this case
\r
578 because the lease time is not essential. */
\r
579 memcpy( ( void * ) &ulLeaseTime, ( void * ) pucByte, ( size_t ) ucLength );
\r
580 ulLeaseTime = FreeRTOS_ntohl( ulLeaseTime );
\r
582 /* Convert the lease time to milliseconds
\r
583 (*1000) then ticks (/portTICK_RATE_MS). */
\r
584 ulLeaseTime *= ( 1000UL / portTICK_RATE_MS );
\r
586 /* Divide the lease time to ensure a renew
\r
587 request is sent before the lease actually
\r
589 ulLeaseTime >>= 1UL;
\r
595 /* Not interested in this field. */
\r
600 /* Jump over the data to find the next option code. */
\r
601 if( ucLength == 0 )
\r
607 pucByte += ucLength;
\r
611 /* Were all the mandatory options received? */
\r
612 if( ulProcessed == ulMandatoryOptions )
\r
614 ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;
\r
620 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );
\r
625 /*-----------------------------------------------------------*/
\r
627 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize )
\r
629 xDHCPMessage_t *pxDHCPMessage;
\r
630 const size_t xRequiredBufferSize = sizeof( xDHCPMessage_t ) + xOptionsArraySize;
\r
631 uint8_t *pucUDPPayloadBuffer;
\r
633 /* Get a buffer. This uses a maximum delay, but the delay will be capped
\r
634 to ipconfigMAX_SEND_BLOCK_TIME_TICKS so the return value still needs to be
\r
638 }while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );
\r
640 pxDHCPMessage = ( xDHCPMessage_t * ) pucUDPPayloadBuffer;
\r
642 /* Most fields need to be zero. */
\r
643 memset( ( void * ) pxDHCPMessage, 0x00, sizeof( xDHCPMessage_t ) );
\r
645 /* Create the message. */
\r
646 pxDHCPMessage->ucOpcode = ucOpcode;
\r
647 pxDHCPMessage->ucAddressType = dhcpADDRESS_TYPE_ETHERNET;
\r
648 pxDHCPMessage->ucAddressLength = dhcpETHERNET_ADDRESS_LENGTH;
\r
649 pxDHCPMessage->ulTransactionID = ulTransactionId;
\r
650 pxDHCPMessage->ulDHCPCookie = dhcpCOOKIE;
\r
651 pxDHCPMessage->usFlags = dhcpBROADCAST;
\r
652 memcpy( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress[ 0 ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );
\r
654 /* Copy in the const part of the options options. */
\r
655 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, xOptionsArraySize );
\r
657 /* Map in the client identifier. */
\r
658 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpCLIENT_IDENTIFIER_OFFSET ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );
\r
660 /* Set the addressing. */
\r
661 pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;
\r
662 pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;
\r
664 return pucUDPPayloadBuffer;
\r
666 /*-----------------------------------------------------------*/
\r
668 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress )
\r
670 uint8_t *pucUDPPayloadBuffer;
\r
671 struct freertos_sockaddr xAddress;
\r
672 static const uint8_t ucDHCPRequestOptions[] =
\r
674 /* Do not change the ordering without also changing
\r
675 dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and
\r
676 dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */
\r
677 dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_REQUEST, /* Message type option. */
\r
678 dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
\r
679 dhcpREQUEST_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address being requested. */
\r
680 dhcpSERVER_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0, /* The IP address of the DHCP server. */
\r
681 dhcpOPTION_END_BYTE
\r
684 pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPRequestOptions, sizeof( ucDHCPRequestOptions ) );
\r
686 /* Copy in the IP address being requested. */
\r
687 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpREQUESTED_IP_ADDRESS_OFFSET ] ), ( void * ) &ulOfferedIPAddress, sizeof( ulOfferedIPAddress ) );
\r
689 /* Copy in the address of the DHCP server being used. */
\r
690 memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ] ), ( void * ) &ulDHCPServerAddress, sizeof( ulDHCPServerAddress ) );
\r
692 iptraceSENDING_DHCP_REQUEST();
\r
693 if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPRequestOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
\r
695 /* The packet was not successfully queued for sending and must be
\r
696 returned to the stack. */
\r
697 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
700 /*-----------------------------------------------------------*/
\r
702 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress )
\r
704 uint8_t *pucUDPPayloadBuffer;
\r
705 struct freertos_sockaddr xAddress;
\r
706 static const uint8_t ucDHCPDiscoverOptions[] =
\r
708 /* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */
\r
709 dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_DISCOVER, /* Message type option. */
\r
710 dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0, /* Client identifier. */
\r
711 dhcpPARAMETER_REQUEST_OPTION_CODE, 3, dhcpSUBNET_MASK_OPTION_CODE, dhcpGATEWAY_OPTION_CODE, hdcpDNS_SERVER_OPTIONS_CODE, /* Parameter request option. */
\r
712 dhcpOPTION_END_BYTE
\r
715 pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, sizeof( ucDHCPDiscoverOptions ) );
\r
717 iptraceSENDING_DHCP_DISCOVER();
\r
718 if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPDiscoverOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )
\r
720 /* The packet was not successfully queued for sending and must be
\r
721 returned to the stack. */
\r
722 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
725 /*-----------------------------------------------------------*/
\r
727 #endif /* ipconfigUSE_DHCP != 0 */
\r