]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/FreeRTOS_DHCP.c
Slight modification to license blub text in header comments.
[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  * This file is part of the FreeRTOS+UDP distribution.  The FreeRTOS+UDP license\r
5  * terms are different to the FreeRTOS license terms.\r
6  *\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
13  *\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
20  *\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
27  *\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
34  *\r
35  * 1 tab == 4 spaces!\r
36  *\r
37  * http://www.FreeRTOS.org\r
38  * http://www.FreeRTOS.org/udp\r
39  *\r
40  */\r
41 \r
42 /* Standard includes. */\r
43 #include <stdint.h>\r
44 \r
45 /* FreeRTOS includes. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 #include "queue.h"\r
49 #include "timers.h"\r
50 \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
58 \r
59 /* Exclude the entire file if DHCP is not enabled. */\r
60 #if ipconfigUSE_DHCP != 0\r
61 \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
66 #endif\r
67 \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
72 \r
73 /* Timer parameters.  Windows simulator times are much shorter because simulated\r
74 time is not real time. */\r
75 #ifdef _WINDOWS_\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
79 #else\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
84 \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
95 \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
102 \r
103 /* Offsets into the transmitted DHCP options fields at which various parameters\r
104 are located. */\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
108 \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
114 \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
117 \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
120 \r
121 /* Marks the end of the variable length options field in the DHCP packet. */\r
122 #define dhcpOPTION_END_BYTE 0xff\r
123 \r
124 /* Offset into a DHCP message at which the first byte of the options is\r
125 located. */\r
126 #define dhcpFIRST_OPTION_BYTE_OFFSET                    ( 0xf0 )\r
127 \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
132 \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 #else\r
139         #define dhcpCLIENT_PORT 0x0044\r
140         #define dhcpSERVER_PORT 0x0043\r
141         #define dhcpCOOKIE              0x63825363\r
142 #endif /* ipconfigBYTE_ORDER */\r
143 \r
144 #include "pack_struct_start.h"\r
145 struct xDHCPMessage\r
146 {\r
147         uint8_t ucOpcode;\r
148         uint8_t ucAddressType;\r
149         uint8_t ucAddressLength;\r
150         uint8_t ucHops;\r
151         uint32_t ulTransactionID;\r
152         uint16_t usElapsedTime;\r
153         uint16_t usFlags;\r
154         uint32_t ulClientIPAddress_ciaddr;\r
155         uint32_t ulYourIPAddress_yiaddr;\r
156         uint32_t ulServerIPAddress_siaddr;\r
157         uint32_t ulRelayAgentIPAddress_giaddr;\r
158         uint8_t ucClientHardwareAddress[ dhcpCLIENT_HARDWARE_ADDRESS_LENGTH ];\r
159         uint8_t ucServerHostName[ dhcpSERVER_HOST_NAME_LENGTH ];\r
160         uint8_t ucBootFileName[ dhcpBOOT_FILE_NAME_LENGTH ];\r
161         uint32_t ulDHCPCookie;\r
162         uint8_t ucFirstOptionByte;\r
163 }\r
164 #include "pack_struct_end.h"\r
165 typedef struct xDHCPMessage xDHCPMessage_t;\r
166 \r
167 /* DHCP state machine states. */\r
168 typedef enum\r
169 {\r
170         eWaitingSendFirstDiscover = 0,  /* Initial state.  Send a discover the first time it is called, and reset all timers. */\r
171         eWaitingOffer,                                  /* Either resend the discover, or, if the offer is forthcoming, send a request. */\r
172         eWaitingAcknowledge,                    /* Either resend the request. */\r
173         eLeasedAddress,                                 /* Resend the request at the appropriate time to renew the lease. */\r
174         eNotUsingLeasedAddress                  /* DHCP failed, and a default IP address is being used. */\r
175 } eDHCPState_t;\r
176 \r
177 /*\r
178  * Generate a DHCP discover message and send it on the DHCP socket.\r
179  */\r
180 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress );\r
181 \r
182 /*\r
183  * Interpret message received on the DHCP socket.\r
184  */\r
185 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing );\r
186 \r
187 /*\r
188  * Generate a DHCP request packet, and send it on the DHCP socket.\r
189  */\r
190 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress );\r
191 \r
192 /*\r
193  * Prepare to start a DHCP transaction.  This initialises some state variables\r
194  * and creates the DHCP socket if necessary.\r
195  */\r
196 static void prvInitialiseDHCP( void );\r
197 \r
198 /*\r
199  * Creates the part of outgoing DHCP messages that are common to all outgoing\r
200  * DHCP messages.\r
201  */\r
202 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize );\r
203 \r
204 /*\r
205  * Create the DHCP socket, if it has not been created already.\r
206  */\r
207 static void prvCreateDHCPSocket( void );\r
208 \r
209 /*-----------------------------------------------------------*/\r
210 \r
211 /* The timer used to drive the DHCP state machine. */\r
212 static xTimerHandle xDHCPTimer = NULL;\r
213 \r
214 /* The UDP socket used for all incoming and outgoing DHCP traffic. */\r
215 static xSocket_t xDHCPSocket = NULL;\r
216 \r
217 /* Hold information in between steps in the DHCP state machine. */\r
218 static uint32_t ulTransactionId = 0UL, ulOfferedIPAddress = 0UL, ulDHCPServerAddress = 0UL, ulLeaseTime = 0;\r
219 \r
220 /* Hold information on the current timer state. */\r
221 static portTickType xDHCPTxTime = 0x00, xDHCPTxPeriod = 0x00;\r
222 \r
223 /* Maintains the DHCP state machine state. */\r
224 static eDHCPState_t eDHCPState = eWaitingSendFirstDiscover;\r
225 \r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vDHCPProcess( portBASE_TYPE xReset, xMACAddress_t *pxMACAddress, uint32_t *pulIPAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )\r
229 {\r
230         if( xReset != pdFALSE )\r
231         {\r
232                 eDHCPState = eWaitingSendFirstDiscover;\r
233         }\r
234 \r
235         switch( eDHCPState )\r
236         {\r
237                 case eWaitingSendFirstDiscover :\r
238 \r
239                         /* Initial state.  Create the DHCP socket, timer, etc. if they\r
240                         have not already been created. */\r
241                         prvInitialiseDHCP();\r
242                         *pulIPAddress = 0UL;\r
243 \r
244                         /* Send the first discover request. */\r
245                         if( xDHCPSocket != NULL )\r
246                         {\r
247                                 xDHCPTxTime = xTaskGetTickCount();\r
248                                 prvSendDHCPDiscover( pxMACAddress );\r
249                                 eDHCPState = eWaitingOffer;\r
250                         }\r
251                         break;\r
252 \r
253                 case eWaitingOffer :\r
254 \r
255                         /* Look for offers coming in. */\r
256                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER, pxMACAddress, pxNetworkAddressing ) == pdPASS )\r
257                         {\r
258                                 /* An offer has been made, generate the request. */\r
259                                 xDHCPTxTime = xTaskGetTickCount();\r
260                                 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
261                                 prvSendDHCPRequest( pxMACAddress );\r
262                                 eDHCPState = eWaitingAcknowledge;\r
263                         }\r
264                         else\r
265                         {\r
266                                 /* Is it time to send another Discover? */\r
267                                 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )\r
268                                 {\r
269                                         /* Increase the time period, and if it has not got to the\r
270                                         point of giving up - send another discovery. */\r
271                                         xDHCPTxPeriod <<= 1;\r
272                                         if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )\r
273                                         {\r
274                                                 ulTransactionId++;\r
275                                                 xDHCPTxTime = xTaskGetTickCount();\r
276                                                 prvSendDHCPDiscover( pxMACAddress );\r
277                                         }\r
278                                         else\r
279                                         {\r
280                                                 /* Revert to static IP address. */\r
281                                                 taskENTER_CRITICAL();\r
282                                                 {\r
283                                                         *pulIPAddress = pxNetworkAddressing->ulDefaultIPAddress;\r
284                                                         iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( pxNetworkAddressing->ulDefaultIPAddress );\r
285                                                 }\r
286                                                 taskEXIT_CRITICAL();\r
287                                                 eDHCPState = eNotUsingLeasedAddress;\r
288                                                 xTimerStop( xDHCPTimer, ( portTickType ) 0 );\r
289 \r
290                                                 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1\r
291                                                 {\r
292                                                         vApplicationIPNetworkEventHook( eNetworkUp );\r
293                                                 }\r
294                                                 #endif\r
295 \r
296                                                 /* Static configuration is being used, so the network is now up. */\r
297                                                 #if ipconfigFREERTOS_PLUS_NABTO == 1\r
298                                                 {\r
299                                                         vStartNabtoTask();\r
300                                                 }\r
301                                                 #endif /* ipconfigFREERTOS_PLUS_NABTO */\r
302 \r
303                                                 /* Close socket to ensure packets don't queue on it. */\r
304                                                 FreeRTOS_closesocket( xDHCPSocket );\r
305                                                 xDHCPSocket = NULL;\r
306                                         }\r
307                                 }\r
308                         }\r
309                         break;\r
310 \r
311                 case eWaitingAcknowledge :\r
312 \r
313                         /* Look for acks coming in. */\r
314                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK, pxMACAddress, pxNetworkAddressing ) == pdPASS )\r
315                         {\r
316                                 /* DHCP completed.  The IP address can now be used, and the\r
317                                 timer set to the lease timeout time. */\r
318                                 *pulIPAddress = ulOfferedIPAddress;\r
319                                 eDHCPState = eLeasedAddress;\r
320 \r
321                                 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1\r
322                                 {\r
323                                         vApplicationIPNetworkEventHook( eNetworkUp );\r
324                                 }\r
325                                 #endif\r
326 \r
327                                 /* Static configuration is being used, so the network is now up. */\r
328                                 #if ipconfigFREERTOS_PLUS_NABTO == 1\r
329                                 {\r
330                                         vStartNabtoTask();\r
331                                 }\r
332                                 #endif /* ipconfigFREERTOS_PLUS_NABTO */\r
333 \r
334                                 /* Close socket to ensure packets don't queue on it. */\r
335                                 FreeRTOS_closesocket( xDHCPSocket );\r
336                                 xDHCPSocket = NULL;\r
337 \r
338                                 if( ulLeaseTime == 0UL )\r
339                                 {\r
340                                         ulLeaseTime = dhcpDEFAULT_LEASE_TIME;\r
341                                 }\r
342                                 else if( ulLeaseTime < dhcpMINIMUM_LEASE_TIME )\r
343                                 {\r
344                                         ulLeaseTime = dhcpMINIMUM_LEASE_TIME;\r
345                                 }\r
346                                 else\r
347                                 {\r
348                                         /* The lease time is already valid. */\r
349                                 }\r
350 \r
351                                 xTimerChangePeriod( xDHCPTimer, ulLeaseTime, portMAX_DELAY );\r
352                         }\r
353                         else\r
354                         {\r
355                                 /* Is it time to send another Discover? */\r
356                                 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )\r
357                                 {\r
358                                         /* Increase the time period, and if it has not got to the\r
359                                         point of giving up - send another request. */\r
360                                         xDHCPTxPeriod <<= 1;\r
361                                         if( xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )\r
362                                         {\r
363                                                 xDHCPTxTime = xTaskGetTickCount();\r
364                                                 prvSendDHCPRequest( pxMACAddress );\r
365                                         }\r
366                                         else\r
367                                         {\r
368                                                 /* Give up, start again. */\r
369                                                 eDHCPState = eWaitingSendFirstDiscover;\r
370                                         }\r
371                                 }\r
372                         }\r
373                         break;\r
374 \r
375                 case eLeasedAddress :\r
376 \r
377                         /* Resend the request at the appropriate time to renew the lease. */\r
378                         prvCreateDHCPSocket();\r
379                         if( xDHCPSocket != NULL )\r
380                         {\r
381                                 xDHCPTxTime = xTaskGetTickCount();\r
382                                 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
383                                 prvSendDHCPRequest( pxMACAddress );\r
384                                 eDHCPState = eWaitingAcknowledge;\r
385                         }\r
386                         xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );\r
387                         break;\r
388 \r
389                 case eNotUsingLeasedAddress:\r
390                         xTimerStop( xDHCPTimer, ( portTickType ) 0 );\r
391                         break;\r
392         }\r
393 }\r
394 /*-----------------------------------------------------------*/\r
395 \r
396 static void prvCreateDHCPSocket( void )\r
397 {\r
398 struct freertos_sockaddr xAddress;\r
399 portBASE_TYPE xReturn;\r
400 portTickType xTimeoutTime = 0;\r
401 \r
402         /* Create the socket, if it has not already been created. */\r
403         if( xDHCPSocket == NULL )\r
404         {\r
405                 xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
406                 configASSERT( ( xDHCPSocket != FREERTOS_INVALID_SOCKET ) );\r
407 \r
408                 /* Ensure the Rx and Tx timeouts are zero as the DHCP executes in the\r
409                 context of the IP task. */\r
410                 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );\r
411                 FreeRTOS_setsockopt( xDHCPSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( portTickType ) );\r
412 \r
413                 /* Bind to the standard DHCP client port. */\r
414                 xAddress.sin_port = dhcpCLIENT_PORT;\r
415                 xReturn = FreeRTOS_bind( xDHCPSocket, &xAddress, sizeof( xAddress ) );\r
416                 configASSERT( xReturn == 0 );\r
417 \r
418                 /* Remove compiler warnings if configASSERT() is not defined. */\r
419                 ( void ) xReturn;\r
420         }\r
421 }\r
422 /*-----------------------------------------------------------*/\r
423 \r
424 static void prvInitialiseDHCP( void )\r
425 {\r
426 extern void vIPFunctionsTimerCallback( xTimerHandle xTimer );\r
427 \r
428         /* Initialise the parameters that will be set by the DHCP process. */\r
429         if( ulTransactionId == 0 )\r
430         {\r
431                 ulTransactionId = ipconfigRAND32();\r
432         }\r
433         else\r
434         {\r
435                 ulTransactionId++;\r
436         }\r
437         ulOfferedIPAddress = 0UL;\r
438         ulDHCPServerAddress = 0UL;\r
439         xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
440 \r
441         /* Create the DHCP socket if it has not already been created. */\r
442         prvCreateDHCPSocket();\r
443 \r
444         if( xDHCPTimer == NULL )\r
445         {\r
446                 xDHCPTimer = xTimerCreate( ( const signed char * const ) "DHCP", dhcpINITIAL_TIMER_PERIOD, pdTRUE, ( void * ) eDHCPEvent, vIPFunctionsTimerCallback );\r
447                 configASSERT( xDHCPTimer );\r
448                 xTimerStart( xDHCPTimer, portMAX_DELAY );\r
449         }\r
450         else\r
451         {\r
452                 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );\r
453         }\r
454 }\r
455 /*-----------------------------------------------------------*/\r
456 \r
457 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )\r
458 {\r
459 uint8_t *pucUDPPayload, *pucLastByte;\r
460 struct freertos_sockaddr xClient;\r
461 uint32_t xClientLength = sizeof( xClient );\r
462 int32_t lBytes;\r
463 xDHCPMessage_t *pxDHCPMessage;\r
464 uint8_t *pucByte, ucOptionCode, ucLength;\r
465 uint32_t ulProcessed;\r
466 portBASE_TYPE xReturn = pdFALSE;\r
467 const uint32_t ulMandatoryOptions = 2; /* DHCP server address, and the correct DHCP message type must be present in the options. */\r
468 \r
469         lBytes = FreeRTOS_recvfrom( xDHCPSocket, ( void * ) &pucUDPPayload, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );\r
470 \r
471         if( lBytes > 0 )\r
472         {\r
473                 /* Map a DHCP structure onto the received data. */\r
474                 pxDHCPMessage = ( xDHCPMessage_t * ) ( pucUDPPayload );\r
475 \r
476                 /* Sanity check. */\r
477                 if( ( pxDHCPMessage->ulDHCPCookie == dhcpCOOKIE ) && ( pxDHCPMessage->ucOpcode == dhcpREPLY_OPCODE ) && ( pxDHCPMessage->ulTransactionID == ulTransactionId ) )\r
478                 {\r
479                         if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) ) == 0 )\r
480                         {\r
481                                 /* None of the essential options have been processed yet. */\r
482                                 ulProcessed = 0;\r
483 \r
484                                 /* Walk through the options until the dhcpOPTION_END_BYTE byte\r
485                                 is found, taking care not to walk off the end of the options. */\r
486                                 pucByte = &( pxDHCPMessage->ucFirstOptionByte );\r
487                                 pucLastByte = &( pucUDPPayload[ lBytes - dhcpMAX_OPTION_LENGTH_OF_INTEREST ] );\r
488                                 while( ( *pucByte != dhcpOPTION_END_BYTE ) && ( pucByte < pucLastByte ) )\r
489                                 {\r
490                                         ucOptionCode = *pucByte;\r
491                                         pucByte++;\r
492                                         ucLength = *pucByte;\r
493                                         pucByte++;\r
494 \r
495                                         switch( ucOptionCode )\r
496                                         {\r
497                                                 case dhcpMESSAGE_TYPE_OPTION_CODE       :\r
498 \r
499                                                         if( *pucByte == ucExpectedMessageType )\r
500                                                         {\r
501                                                                 /* The message type is the message type the\r
502                                                                 state machine is expecting. */\r
503                                                                 ulProcessed++;\r
504                                                         }\r
505                                                         else if( *pucByte == dhcpMESSAGE_TYPE_NACK )\r
506                                                         {\r
507                                                                 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_ACK )\r
508                                                                 {\r
509                                                                         /* Start again. */\r
510                                                                         eDHCPState = eWaitingSendFirstDiscover;\r
511                                                                 }\r
512                                                         }\r
513                                                         else\r
514                                                         {\r
515                                                                 /* Don't process other message types. */\r
516                                                         }\r
517                                                         break;\r
518 \r
519                                                 case dhcpSUBNET_MASK_OPTION_CODE :\r
520 \r
521                                                         if( ucLength == sizeof( uint32_t ) )\r
522                                                         {\r
523                                                                 memcpy( ( void * ) &( pxNetworkAddressing->ulNetMask ), ( void * ) pucByte, ( size_t ) ucLength );\r
524                                                         }\r
525                                                         break;\r
526 \r
527                                                 case dhcpGATEWAY_OPTION_CODE :\r
528 \r
529                                                         if( ucLength == sizeof( uint32_t ) )\r
530                                                         {\r
531                                                                 /* ulProcessed is not incremented in this case\r
532                                                                 because the gateway is not essential. */\r
533                                                                 memcpy( ( void * ) &( pxNetworkAddressing->ulGatewayAddress ), ( void * ) pucByte, ( size_t ) ucLength );\r
534                                                         }\r
535                                                         break;\r
536 \r
537                                                 case hdcpDNS_SERVER_OPTIONS_CODE :\r
538 \r
539                                                         /* ulProcessed is not incremented in this case\r
540                                                         because the DNS server is not essential.  Only the\r
541                                                         first DNS server address is taken. */\r
542                                                         memcpy( ( void * ) &( pxNetworkAddressing->ulDNSServerAddress ), ( void * ) pucByte, sizeof( uint32_t ) );\r
543                                                         break;\r
544 \r
545                                                 case dhcpSERVER_IP_ADDRESS_OPTION_CODE :\r
546 \r
547                                                         if( ucLength == sizeof( uint32_t ) )\r
548                                                         {\r
549                                                                 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_OFFER )\r
550                                                                 {\r
551                                                                         /* Offers state the replying server. */\r
552                                                                         ulProcessed++;\r
553                                                                         memcpy( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength );\r
554                                                                 }\r
555                                                                 else\r
556                                                                 {\r
557                                                                         /* The ack must come from the expected server. */\r
558                                                                         if( memcmp( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength ) == 0 )\r
559                                                                         {\r
560                                                                                 ulProcessed++;\r
561                                                                         }\r
562                                                                 }\r
563                                                         }\r
564                                                         break;\r
565 \r
566                                                 case dhcpLEASE_TIME_OPTION_CODE :\r
567 \r
568                                                         if( ucLength == sizeof( &ulLeaseTime ) )\r
569                                                         {\r
570                                                                 /* ulProcessed is not incremented in this case\r
571                                                                 because the lease time is not essential. */\r
572                                                                 memcpy( ( void * ) &ulLeaseTime, ( void * ) pucByte, ( size_t ) ucLength );\r
573                                                                 ulLeaseTime = FreeRTOS_ntohl( ulLeaseTime );\r
574 \r
575                                                                 /* Convert the lease time to milliseconds\r
576                                                                 (*1000) then ticks (/portTICK_RATE_MS). */\r
577                                                                 ulLeaseTime *= ( 1000UL / portTICK_RATE_MS );\r
578 \r
579                                                                 /* Divide the lease time to ensure a renew\r
580                                                                 request is sent before the lease actually\r
581                                                                 expires. */\r
582                                                                 ulLeaseTime >>= 1UL;\r
583                                                         }\r
584                                                         break;\r
585 \r
586                                                 default :\r
587 \r
588                                                         /* Not interested in this field. */\r
589 \r
590                                                         break;\r
591                                         }\r
592 \r
593                                         /* Jump over the data to find the next option code. */\r
594                                         if( ucLength == 0 )\r
595                                         {\r
596                                                 break;\r
597                                         }\r
598                                         else\r
599                                         {\r
600                                                 pucByte += ucLength;\r
601                                         }\r
602                                 }\r
603 \r
604                                 /* Were all the mandatory options received? */\r
605                                 if( ulProcessed == ulMandatoryOptions )\r
606                                 {\r
607                                         ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;\r
608                                         xReturn = pdPASS;\r
609                                 }\r
610                         }\r
611                 }\r
612 \r
613                 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );\r
614         }\r
615 \r
616         return xReturn;\r
617 }\r
618 /*-----------------------------------------------------------*/\r
619 \r
620 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, xMACAddress_t *pxMACAddress, uint8_t ucOpcode, const uint8_t * const pucOptionsArray, size_t xOptionsArraySize )\r
621 {\r
622 xDHCPMessage_t *pxDHCPMessage;\r
623 const size_t xRequiredBufferSize = sizeof( xDHCPMessage_t ) + xOptionsArraySize;\r
624 uint8_t *pucUDPPayloadBuffer;\r
625 \r
626         /* Get a buffer.  This uses a maximum delay, but the delay will be capped\r
627         to ipconfigMAX_SEND_BLOCK_TIME_TICKS so the return value still needs to be\r
628         test. */\r
629         do\r
630         {\r
631         }while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );\r
632 \r
633         pxDHCPMessage = ( xDHCPMessage_t * ) pucUDPPayloadBuffer;\r
634 \r
635         /* Most fields need to be zero. */\r
636         memset( ( void * ) pxDHCPMessage, 0x00, sizeof( xDHCPMessage_t ) );\r
637 \r
638         /* Create the message. */\r
639         pxDHCPMessage->ucOpcode = ucOpcode;\r
640         pxDHCPMessage->ucAddressType = dhcpADDRESS_TYPE_ETHERNET;\r
641         pxDHCPMessage->ucAddressLength = dhcpETHERNET_ADDRESS_LENGTH;\r
642         pxDHCPMessage->ulTransactionID = ulTransactionId;\r
643         pxDHCPMessage->ulYourIPAddress_yiaddr = ulOfferedIPAddress;\r
644         pxDHCPMessage->ulDHCPCookie = dhcpCOOKIE;\r
645         memcpy( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress[ 0 ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );\r
646 \r
647         /* Copy in the const part of the options options. */\r
648         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, xOptionsArraySize );\r
649 \r
650         /* Map in the client identifier. */\r
651         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpCLIENT_IDENTIFIER_OFFSET ] ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) );\r
652 \r
653         /* Set the addressing. */\r
654         pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;\r
655         pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;\r
656 \r
657         return pucUDPPayloadBuffer;\r
658 }\r
659 /*-----------------------------------------------------------*/\r
660 \r
661 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress )\r
662 {\r
663 uint8_t *pucUDPPayloadBuffer;\r
664 struct freertos_sockaddr xAddress;\r
665 static const uint8_t ucDHCPRequestOptions[] =\r
666 {\r
667         /* Do not change the ordering without also changing\r
668         dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and\r
669         dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */\r
670         dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_REQUEST,              /* Message type option. */\r
671         dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0,                 /* Client identifier. */\r
672         dhcpREQUEST_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0,                              /* The IP address being requested. */\r
673         dhcpSERVER_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0,                               /* The IP address of the DHCP server. */\r
674         dhcpOPTION_END_BYTE\r
675 };\r
676 \r
677         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPRequestOptions, sizeof( ucDHCPRequestOptions ) );\r
678 \r
679         /* Copy in the IP address being requested. */\r
680         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpREQUESTED_IP_ADDRESS_OFFSET ] ), ( void * ) &ulOfferedIPAddress, sizeof( ulOfferedIPAddress ) );\r
681 \r
682         /* Copy in the address of the DHCP server being used. */\r
683         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ] ), ( void * ) &ulDHCPServerAddress, sizeof( ulDHCPServerAddress ) );\r
684 \r
685         iptraceSENDING_DHCP_REQUEST();\r
686         if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPRequestOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
687         {\r
688                 /* The packet was not successfully queued for sending and must be\r
689                 returned to the stack. */\r
690                 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
691         }\r
692 }\r
693 /*-----------------------------------------------------------*/\r
694 \r
695 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress )\r
696 {\r
697 uint8_t *pucUDPPayloadBuffer;\r
698 struct freertos_sockaddr xAddress;\r
699 static const uint8_t ucDHCPDiscoverOptions[] =\r
700 {\r
701         /* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */\r
702         dhcpMESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_DISCOVER,                                     /* Message type option. */\r
703         dhcpCLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0,                                         /* Client identifier. */\r
704         dhcpPARAMETER_REQUEST_OPTION_CODE, 3, dhcpSUBNET_MASK_OPTION_CODE, dhcpGATEWAY_OPTION_CODE, hdcpDNS_SERVER_OPTIONS_CODE,        /* Parameter request option. */\r
705         dhcpOPTION_END_BYTE\r
706 };\r
707 \r
708         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, sizeof( ucDHCPDiscoverOptions ) );\r
709 \r
710         iptraceSENDING_DHCP_DISCOVER();\r
711         if( FreeRTOS_sendto( xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( xDHCPMessage_t ) + sizeof( ucDHCPDiscoverOptions ) ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
712         {\r
713                 /* The packet was not successfully queued for sending and must be\r
714                 returned to the stack. */\r
715                 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
716         }\r
717 }\r
718 /*-----------------------------------------------------------*/\r
719 \r
720 #endif /* ipconfigUSE_DHCP != 0 */\r
721 \r
722 \r