]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/FreeRTOS_DHCP.c
f113e56e1b90f36e7fbd1c2f48f75fac6177cda2
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / FreeRTOS_DHCP.c
1 /*\r
2  * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.\r
3  *\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
8  *\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
15  *\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
23  *\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
30  *\r
31  * 1 tab == 4 spaces!\r
32  *\r
33  * http://www.FreeRTOS.org\r
34  * http://www.FreeRTOS.org/udp\r
35  *\r
36  */\r
37 \r
38 /* Standard includes. */\r
39 #include <stdint.h>\r
40 \r
41 /* FreeRTOS includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 #include "queue.h"\r
45 #include "timers.h"\r
46 \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
54 \r
55 /* Exclude the entire file if DHCP is not enabled. */\r
56 #if ipconfigUSE_DHCP != 0\r
57 \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
62 #endif\r
63 \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
68 \r
69 /* Timer parameters.  Windows simulator times are much shorter because simulated\r
70 time is not real time. */\r
71 #ifdef _WINDOWS_\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
75 #else\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
80 \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
91 \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
98 \r
99 /* Offsets into the transmitted DHCP options fields at which various parameters\r
100 are located. */\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
104 \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
110 \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
113 \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
116 \r
117 /* Marks the end of the variable length options field in the DHCP packet. */\r
118 #define dhcpOPTION_END_BYTE 0xff\r
119 \r
120 /* Offset into a DHCP message at which the first byte of the options is\r
121 located. */\r
122 #define dhcpFIRST_OPTION_BYTE_OFFSET                    ( 0xf0 )\r
123 \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
128 \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
134 #else\r
135         #define dhcpCLIENT_PORT 0x0044\r
136         #define dhcpSERVER_PORT 0x0043\r
137         #define dhcpCOOKIE              0x63825363\r
138 #endif /* ipconfigBYTE_ORDER */\r
139 \r
140 #include "pack_struct_start.h"\r
141 struct xDHCPMessage\r
142 {\r
143         uint8_t ucOpcode;\r
144         uint8_t ucAddressType;\r
145         uint8_t ucAddressLength;\r
146         uint8_t ucHops;\r
147         uint32_t ulTransactionID;\r
148         uint16_t usElapsedTime;\r
149         uint16_t usFlags;\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
159 }\r
160 #include "pack_struct_end.h"\r
161 typedef struct xDHCPMessage xDHCPMessage_t;\r
162 \r
163 /* DHCP state machine states. */\r
164 typedef enum\r
165 {\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
171 } eDHCPState_t;\r
172 \r
173 /*\r
174  * Generate a DHCP discover message and send it on the DHCP socket.\r
175  */\r
176 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress );\r
177 \r
178 /*\r
179  * Interpret message received on the DHCP socket.\r
180  */\r
181 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing );\r
182 \r
183 /*\r
184  * Generate a DHCP request packet, and send it on the DHCP socket.\r
185  */\r
186 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress );\r
187 \r
188 /*\r
189  * Prepare to start a DHCP transaction.  This initialises some state variables\r
190  * and creates the DHCP socket if necessary.\r
191  */\r
192 static void prvInitialiseDHCP( void );\r
193 \r
194 /*\r
195  * Creates the part of outgoing DHCP messages that are common to all outgoing\r
196  * DHCP messages.\r
197  */\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
199 \r
200 /*\r
201  * Create the DHCP socket, if it has not been created already.\r
202  */\r
203 static void prvCreateDHCPSocket( void );\r
204 \r
205 /*-----------------------------------------------------------*/\r
206 \r
207 /* The timer used to drive the DHCP state machine. */\r
208 static xTimerHandle xDHCPTimer = NULL;\r
209 \r
210 /* The UDP socket used for all incoming and outgoing DHCP traffic. */\r
211 static xSocket_t xDHCPSocket = NULL;\r
212 \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
215 \r
216 /* Hold information on the current timer state. */\r
217 static portTickType xDHCPTxTime = 0x00, xDHCPTxPeriod = 0x00;\r
218 \r
219 /* Maintains the DHCP state machine state. */\r
220 static eDHCPState_t eDHCPState = eWaitingSendFirstDiscover;\r
221 \r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vDHCPProcess( portBASE_TYPE xReset, xMACAddress_t *pxMACAddress, uint32_t *pulIPAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )\r
225 {\r
226         if( xReset != pdFALSE )\r
227         {\r
228                 eDHCPState = eWaitingSendFirstDiscover;\r
229         }\r
230 \r
231         switch( eDHCPState )\r
232         {\r
233                 case eWaitingSendFirstDiscover :\r
234 \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
239 \r
240                         /* Send the first discover request. */\r
241                         if( xDHCPSocket != NULL )\r
242                         {\r
243                                 xDHCPTxTime = xTaskGetTickCount();\r
244                                 prvSendDHCPDiscover( pxMACAddress );\r
245                                 eDHCPState = eWaitingOffer;\r
246                         }\r
247                         break;\r
248 \r
249                 case eWaitingOffer :\r
250 \r
251                         /* Look for offers coming in. */\r
252                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER, pxMACAddress, pxNetworkAddressing ) == pdPASS )\r
253                         {\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
259                         }\r
260                         else\r
261                         {\r
262                                 /* Is it time to send another Discover? */\r
263                                 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )\r
264                                 {\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
269                                         {\r
270                                                 ulTransactionId++;\r
271                                                 xDHCPTxTime = xTaskGetTickCount();\r
272                                                 prvSendDHCPDiscover( pxMACAddress );\r
273                                         }\r
274                                         else\r
275                                         {\r
276                                                 /* Revert to static IP address. */\r
277                                                 taskENTER_CRITICAL();\r
278                                                 {\r
279                                                         *pulIPAddress = pxNetworkAddressing->ulDefaultIPAddress;\r
280                                                         iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( pxNetworkAddressing->ulDefaultIPAddress );\r
281                                                 }\r
282                                                 taskEXIT_CRITICAL();\r
283                                                 eDHCPState = eNotUsingLeasedAddress;\r
284                                                 xTimerStop( xDHCPTimer, ( portTickType ) 0 );\r
285 \r
286                                                 #if ipconfigFREERTOS_PLUS_NABTO == 1\r
287                                                 {\r
288                                                         vStartNabtoTask();\r
289                                                 }\r
290                                                 #endif /* ipconfigFREERTOS_PLUS_NABTO */\r
291 \r
292                                                 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1\r
293                                                 {\r
294                                                         vApplicationIPNetworkEventHook( eNetworkUp );\r
295                                                 }\r
296                                                 #endif\r
297 \r
298                                                 /* Close socket to ensure packets don't queue on it. */\r
299                                                 FreeRTOS_closesocket( xDHCPSocket );\r
300                                                 xDHCPSocket = NULL;\r
301                                         }\r
302                                 }\r
303                         }\r
304                         break;\r
305 \r
306                 case eWaitingAcknowledge :\r
307 \r
308                         /* Look for acks coming in. */\r
309                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK, pxMACAddress, pxNetworkAddressing ) == pdPASS )\r
310                         {\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
315 \r
316                                 #if ipconfigFREERTOS_PLUS_NABTO == 1\r
317                                 {\r
318                                         vStartNabtoTask();\r
319                                 }\r
320                                 #endif /* ipconfigFREERTOS_PLUS_NABTO */\r
321 \r
322                                 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1\r
323                                 {\r
324                                         vApplicationIPNetworkEventHook( eNetworkUp );\r
325                                 }\r
326                                 #endif\r
327 \r
328                                 /* Close socket to ensure packets don't queue on it. */\r
329                                 FreeRTOS_closesocket( xDHCPSocket );\r
330                                 xDHCPSocket = NULL;\r
331 \r
332                                 if( ulLeaseTime == 0UL )\r
333                                 {\r
334                                         ulLeaseTime = dhcpDEFAULT_LEASE_TIME;\r
335                                 }\r
336                                 else if( ulLeaseTime < dhcpMINIMUM_LEASE_TIME )\r
337                                 {\r
338                                         ulLeaseTime = dhcpMINIMUM_LEASE_TIME;\r
339                                 }\r
340                                 else\r
341                                 {\r
342                                         /* The lease time is already valid. */\r
343                                 }\r
344 \r
345                                 xTimerChangePeriod( xDHCPTimer, ulLeaseTime, portMAX_DELAY );\r
346                         }\r
347                         else\r
348                         {\r
349                                 /* Is it time to send another Discover? */\r
350                                 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )\r
351                                 {\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
356                                         {\r
357                                                 xDHCPTxTime = xTaskGetTickCount();\r
358                                                 prvSendDHCPRequest( pxMACAddress );\r
359                                         }\r
360                                         else\r
361                                         {\r
362                                                 /* Give up, start again. */\r
363                                                 eDHCPState = eWaitingSendFirstDiscover;\r
364                                         }\r
365                                 }\r
366                         }\r
367                         break;\r
368 \r
369                 case eLeasedAddress :\r
370 \r
371                         /* Resend the request at the appropriate time to renew the lease. */\r
372                         prvCreateDHCPSocket();\r
373                         if( xDHCPSocket != NULL )\r
374                         {\r
375                                 xDHCPTxTime = xTaskGetTickCount();\r
376                                 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
377                                 prvSendDHCPRequest( pxMACAddress );\r
378                                 eDHCPState = eWaitingAcknowledge;\r
379                         }\r
380                         xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );\r
381                         break;\r
382 \r
383                 case eNotUsingLeasedAddress:\r
384                         xTimerStop( xDHCPTimer, ( portTickType ) 0 );\r
385                         break;\r
386         }\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 static void prvCreateDHCPSocket( void )\r
391 {\r
392 struct freertos_sockaddr xAddress;\r
393 portBASE_TYPE xReturn;\r
394 portTickType xTimeoutTime = 0;\r
395 \r
396         /* Create the socket, if it has not already been created. */\r
397         if( xDHCPSocket == NULL )\r
398         {\r
399                 xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
400                 configASSERT( ( xDHCPSocket != FREERTOS_INVALID_SOCKET ) );\r
401 \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
406 \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
411 \r
412                 /* Remove compiler warnings if configASSERT() is not defined. */\r
413                 ( void ) xReturn;\r
414         }\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 static void prvInitialiseDHCP( void )\r
419 {\r
420 extern void vIPFunctionsTimerCallback( xTimerHandle xTimer );\r
421 \r
422         /* Initialise the parameters that will be set by the DHCP process. */\r
423         if( ulTransactionId == 0 )\r
424         {\r
425                 ulTransactionId = ipconfigRAND32();\r
426         }\r
427         else\r
428         {\r
429                 ulTransactionId++;\r
430         }\r
431         ulOfferedIPAddress = 0UL;\r
432         ulDHCPServerAddress = 0UL;\r
433         xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
434 \r
435         /* Create the DHCP socket if it has not already been created. */\r
436         prvCreateDHCPSocket();\r
437 \r
438         if( xDHCPTimer == NULL )\r
439         {\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
443         }\r
444         else\r
445         {\r
446                 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );\r
447         }\r
448 }\r
449 /*-----------------------------------------------------------*/\r
450 \r
451 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )\r
452 {\r
453 uint8_t *pucUDPPayload, *pucLastByte;\r
454 struct freertos_sockaddr xClient;\r
455 uint32_t xClientLength = sizeof( xClient );\r
456 int32_t lBytes;\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
462 \r
463         lBytes = FreeRTOS_recvfrom( xDHCPSocket, ( void * ) &pucUDPPayload, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );\r
464 \r
465         if( lBytes > 0 )\r
466         {\r
467                 /* Map a DHCP structure onto the received data. */\r
468                 pxDHCPMessage = ( xDHCPMessage_t * ) ( pucUDPPayload );\r
469 \r
470                 /* Sanity check. */\r
471                 if( ( pxDHCPMessage->ulDHCPCookie == dhcpCOOKIE ) && ( pxDHCPMessage->ucOpcode == dhcpREPLY_OPCODE ) && ( pxDHCPMessage->ulTransactionID == ulTransactionId ) )\r
472                 {\r
473                         if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) ) == 0 )\r
474                         {\r
475                                 /* None of the essential options have been processed yet. */\r
476                                 ulProcessed = 0;\r
477 \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
483                                 {\r
484                                         ucOptionCode = *pucByte;\r
485                                         pucByte++;\r
486                                         ucLength = *pucByte;\r
487                                         pucByte++;\r
488 \r
489                                         switch( ucOptionCode )\r
490                                         {\r
491                                                 case dhcpMESSAGE_TYPE_OPTION_CODE       :\r
492 \r
493                                                         if( *pucByte == ucExpectedMessageType )\r
494                                                         {\r
495                                                                 /* The message type is the message type the\r
496                                                                 state machine is expecting. */\r
497                                                                 ulProcessed++;\r
498                                                         }\r
499                                                         else if( *pucByte == dhcpMESSAGE_TYPE_NACK )\r
500                                                         {\r
501                                                                 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_ACK )\r
502                                                                 {\r
503                                                                         /* Start again. */\r
504                                                                         eDHCPState = eWaitingSendFirstDiscover;\r
505                                                                 }\r
506                                                         }\r
507                                                         else\r
508                                                         {\r
509                                                                 /* Don't process other message types. */\r
510                                                         }\r
511                                                         break;\r
512 \r
513                                                 case dhcpSUBNET_MASK_OPTION_CODE :\r
514 \r
515                                                         if( ucLength == sizeof( uint32_t ) )\r
516                                                         {\r
517                                                                 memcpy( ( void * ) &( pxNetworkAddressing->ulNetMask ), ( void * ) pucByte, ( size_t ) ucLength );\r
518                                                         }\r
519                                                         break;\r
520 \r
521                                                 case dhcpGATEWAY_OPTION_CODE :\r
522 \r
523                                                         if( ucLength == sizeof( uint32_t ) )\r
524                                                         {\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
528                                                         }\r
529                                                         break;\r
530 \r
531                                                 case hdcpDNS_SERVER_OPTIONS_CODE :\r
532 \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
537                                                         break;\r
538 \r
539                                                 case dhcpSERVER_IP_ADDRESS_OPTION_CODE :\r
540 \r
541                                                         if( ucLength == sizeof( uint32_t ) )\r
542                                                         {\r
543                                                                 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_OFFER )\r
544                                                                 {\r
545                                                                         /* Offers state the replying server. */\r
546                                                                         ulProcessed++;\r
547                                                                         memcpy( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength );\r
548                                                                 }\r
549                                                                 else\r
550                                                                 {\r
551                                                                         /* The ack must come from the expected server. */\r
552                                                                         if( memcmp( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength ) == 0 )\r
553                                                                         {\r
554                                                                                 ulProcessed++;\r
555                                                                         }\r
556                                                                 }\r
557                                                         }\r
558                                                         break;\r
559 \r
560                                                 case dhcpLEASE_TIME_OPTION_CODE :\r
561 \r
562                                                         if( ucLength == sizeof( &ulLeaseTime ) )\r
563                                                         {\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
568 \r
569                                                                 /* Convert the lease time to milliseconds\r
570                                                                 (*1000) then ticks (/portTICK_RATE_MS). */\r
571                                                                 ulLeaseTime *= ( 1000UL / portTICK_RATE_MS );\r
572 \r
573                                                                 /* Divide the lease time to ensure a renew \r
574                                                                 request is sent before the lease actually\r
575                                                                 expires. */\r
576                                                                 ulLeaseTime >>= 1UL;\r
577                                                         }\r
578                                                         break;\r
579 \r
580                                                 default :\r
581 \r
582                                                         /* Not interested in this field. */\r
583 \r
584                                                         break;\r
585                                         }\r
586 \r
587                                         /* Jump over the data to find the next option code. */\r
588                                         if( ucLength == 0 )\r
589                                         {\r
590                                                 break;\r
591                                         }\r
592                                         else\r
593                                         {\r
594                                                 pucByte += ucLength;\r
595                                         }\r
596                                 }\r
597 \r
598                                 /* Were all the mandatory options received? */\r
599                                 if( ulProcessed == ulMandatoryOptions )\r
600                                 {\r
601                                         ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;\r
602                                         xReturn = pdPASS;\r
603                                 }\r
604                         }\r
605                 }\r
606 \r
607                 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );\r
608         }\r
609 \r
610         return xReturn;\r
611 }\r
612 /*-----------------------------------------------------------*/\r
613 \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
615 {\r
616 xDHCPMessage_t *pxDHCPMessage;\r
617 const size_t xRequiredBufferSize = sizeof( xDHCPMessage_t ) + xOptionsArraySize;\r
618 uint8_t *pucUDPPayloadBuffer;\r
619 \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
622         test. */\r
623         do\r
624         {\r
625         }while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );\r
626 \r
627         pxDHCPMessage = ( xDHCPMessage_t * ) pucUDPPayloadBuffer;\r
628 \r
629         /* Most fields need to be zero. */\r
630         memset( ( void * ) pxDHCPMessage, 0x00, sizeof( xDHCPMessage_t ) );\r
631 \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
640 \r
641         /* Copy in the const part of the options options. */\r
642         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, xOptionsArraySize );\r
643 \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
646 \r
647         /* Set the addressing. */\r
648         pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;\r
649         pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;\r
650 \r
651         return pucUDPPayloadBuffer;\r
652 }\r
653 /*-----------------------------------------------------------*/\r
654 \r
655 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress )\r
656 {\r
657 uint8_t *pucUDPPayloadBuffer;\r
658 struct freertos_sockaddr xAddress;\r
659 static const uint8_t ucDHCPRequestOptions[] =\r
660 {\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
669 };\r
670 \r
671         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPRequestOptions, sizeof( ucDHCPRequestOptions ) );\r
672 \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
675 \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
678 \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
681         {\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
685         }\r
686 }\r
687 /*-----------------------------------------------------------*/\r
688 \r
689 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress )\r
690 {\r
691 uint8_t *pucUDPPayloadBuffer;\r
692 struct freertos_sockaddr xAddress;\r
693 static const uint8_t ucDHCPDiscoverOptions[] =\r
694 {\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
700 };\r
701 \r
702         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, sizeof( ucDHCPDiscoverOptions ) );\r
703 \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
706         {\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
710         }\r
711 }\r
712 /*-----------------------------------------------------------*/\r
713 \r
714 #endif /* ipconfigUSE_DHCP != 0 */\r
715 \r
716 \r