]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/FreeRTOS_DHCP.c
Improve DCHP handling by removing the yiaddr field from outgoing DHCP packets and...
[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         #define dhcpBROADCAST   0x0080\r
139 #else\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
145 \r
146 #include "pack_struct_start.h"\r
147 struct xDHCPMessage\r
148 {\r
149         uint8_t ucOpcode;\r
150         uint8_t ucAddressType;\r
151         uint8_t ucAddressLength;\r
152         uint8_t ucHops;\r
153         uint32_t ulTransactionID;\r
154         uint16_t usElapsedTime;\r
155         uint16_t usFlags;\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
165 }\r
166 #include "pack_struct_end.h"\r
167 typedef struct xDHCPMessage xDHCPMessage_t;\r
168 \r
169 /* DHCP state machine states. */\r
170 typedef enum\r
171 {\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
177 } eDHCPState_t;\r
178 \r
179 /*\r
180  * Generate a DHCP discover message and send it on the DHCP socket.\r
181  */\r
182 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress );\r
183 \r
184 /*\r
185  * Interpret message received on the DHCP socket.\r
186  */\r
187 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing );\r
188 \r
189 /*\r
190  * Generate a DHCP request packet, and send it on the DHCP socket.\r
191  */\r
192 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress );\r
193 \r
194 /*\r
195  * Prepare to start a DHCP transaction.  This initialises some state variables\r
196  * and creates the DHCP socket if necessary.\r
197  */\r
198 static void prvInitialiseDHCP( void );\r
199 \r
200 /*\r
201  * Creates the part of outgoing DHCP messages that are common to all outgoing\r
202  * DHCP messages.\r
203  */\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
205 \r
206 /*\r
207  * Create the DHCP socket, if it has not been created already.\r
208  */\r
209 static void prvCreateDHCPSocket( void );\r
210 \r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /* The timer used to drive the DHCP state machine. */\r
214 static xTimerHandle xDHCPTimer = NULL;\r
215 \r
216 /* The UDP socket used for all incoming and outgoing DHCP traffic. */\r
217 static xSocket_t xDHCPSocket = NULL;\r
218 \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
221 \r
222 /* Hold information on the current timer state. */\r
223 static portTickType xDHCPTxTime = 0x00, xDHCPTxPeriod = 0x00;\r
224 \r
225 /* Maintains the DHCP state machine state. */\r
226 static eDHCPState_t eDHCPState = eWaitingSendFirstDiscover;\r
227 \r
228 /*-----------------------------------------------------------*/\r
229 \r
230 void vDHCPProcess( portBASE_TYPE xReset, xMACAddress_t *pxMACAddress, uint32_t *pulIPAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )\r
231 {\r
232         if( xReset != pdFALSE )\r
233         {\r
234                 eDHCPState = eWaitingSendFirstDiscover;\r
235         }\r
236 \r
237         switch( eDHCPState )\r
238         {\r
239                 case eWaitingSendFirstDiscover :\r
240 \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
245 \r
246                         /* Send the first discover request. */\r
247                         if( xDHCPSocket != NULL )\r
248                         {\r
249                                 xDHCPTxTime = xTaskGetTickCount();\r
250                                 prvSendDHCPDiscover( pxMACAddress );\r
251                                 eDHCPState = eWaitingOffer;\r
252                         }\r
253                         break;\r
254 \r
255                 case eWaitingOffer :\r
256 \r
257                         /* Look for offers coming in. */\r
258                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER, pxMACAddress, pxNetworkAddressing ) == pdPASS )\r
259                         {\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
265                         }\r
266                         else\r
267                         {\r
268                                 /* Is it time to send another Discover? */\r
269                                 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )\r
270                                 {\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
275                                         {\r
276                                                 ulTransactionId++;\r
277                                                 xDHCPTxTime = xTaskGetTickCount();\r
278                                                 prvSendDHCPDiscover( pxMACAddress );\r
279                                         }\r
280                                         else\r
281                                         {\r
282                                                 /* Revert to static IP address. */\r
283                                                 taskENTER_CRITICAL();\r
284                                                 {\r
285                                                         *pulIPAddress = pxNetworkAddressing->ulDefaultIPAddress;\r
286                                                         iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( pxNetworkAddressing->ulDefaultIPAddress );\r
287                                                 }\r
288                                                 taskEXIT_CRITICAL();\r
289                                                 eDHCPState = eNotUsingLeasedAddress;\r
290                                                 xTimerStop( xDHCPTimer, ( portTickType ) 0 );\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                                                 /* Static configuration is being used, so the network is now up. */\r
299                                                 #if ipconfigFREERTOS_PLUS_NABTO == 1\r
300                                                 {\r
301                                                         /* Return value is used in configASSERT() inside the \r
302                                                         function. */\r
303                                                         ( void ) xStartNabtoTask();\r
304                                                 }\r
305                                                 #endif /* ipconfigFREERTOS_PLUS_NABTO */\r
306 \r
307                                                 /* Close socket to ensure packets don't queue on it. */\r
308                                                 FreeRTOS_closesocket( xDHCPSocket );\r
309                                                 xDHCPSocket = NULL;\r
310                                         }\r
311                                 }\r
312                         }\r
313                         break;\r
314 \r
315                 case eWaitingAcknowledge :\r
316 \r
317                         /* Look for acks coming in. */\r
318                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK, pxMACAddress, pxNetworkAddressing ) == pdPASS )\r
319                         {\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
324 \r
325                                 #if ipconfigUSE_NETWORK_EVENT_HOOK == 1\r
326                                 {\r
327                                         vApplicationIPNetworkEventHook( eNetworkUp );\r
328                                 }\r
329                                 #endif\r
330 \r
331                                 /* Static configuration is being used, so the network is now \r
332                                 up. */\r
333                                 #if ipconfigFREERTOS_PLUS_NABTO == 1\r
334                                 {\r
335                                         /* Return value is used in configASSERT() inside the \r
336                                         function. */\r
337                                         ( void ) xStartNabtoTask();\r
338                                 }\r
339                                 #endif /* ipconfigFREERTOS_PLUS_NABTO */\r
340 \r
341                                 /* Close socket to ensure packets don't queue on it. */\r
342                                 FreeRTOS_closesocket( xDHCPSocket );\r
343                                 xDHCPSocket = NULL;\r
344 \r
345                                 if( ulLeaseTime == 0UL )\r
346                                 {\r
347                                         ulLeaseTime = dhcpDEFAULT_LEASE_TIME;\r
348                                 }\r
349                                 else if( ulLeaseTime < dhcpMINIMUM_LEASE_TIME )\r
350                                 {\r
351                                         ulLeaseTime = dhcpMINIMUM_LEASE_TIME;\r
352                                 }\r
353                                 else\r
354                                 {\r
355                                         /* The lease time is already valid. */\r
356                                 }\r
357 \r
358                                 xTimerChangePeriod( xDHCPTimer, ulLeaseTime, portMAX_DELAY );\r
359                         }\r
360                         else\r
361                         {\r
362                                 /* Is it time to send another Discover? */\r
363                                 if( ( xTaskGetTickCount() - xDHCPTxTime ) > xDHCPTxPeriod )\r
364                                 {\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
369                                         {\r
370                                                 xDHCPTxTime = xTaskGetTickCount();\r
371                                                 prvSendDHCPRequest( pxMACAddress );\r
372                                         }\r
373                                         else\r
374                                         {\r
375                                                 /* Give up, start again. */\r
376                                                 eDHCPState = eWaitingSendFirstDiscover;\r
377                                         }\r
378                                 }\r
379                         }\r
380                         break;\r
381 \r
382                 case eLeasedAddress :\r
383 \r
384                         /* Resend the request at the appropriate time to renew the lease. */\r
385                         prvCreateDHCPSocket();\r
386                         if( xDHCPSocket != NULL )\r
387                         {\r
388                                 xDHCPTxTime = xTaskGetTickCount();\r
389                                 xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
390                                 prvSendDHCPRequest( pxMACAddress );\r
391                                 eDHCPState = eWaitingAcknowledge;\r
392                         }\r
393                         xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );\r
394                         break;\r
395 \r
396                 case eNotUsingLeasedAddress:\r
397                         xTimerStop( xDHCPTimer, ( portTickType ) 0 );\r
398                         break;\r
399         }\r
400 }\r
401 /*-----------------------------------------------------------*/\r
402 \r
403 static void prvCreateDHCPSocket( void )\r
404 {\r
405 struct freertos_sockaddr xAddress;\r
406 portBASE_TYPE xReturn;\r
407 portTickType xTimeoutTime = 0;\r
408 \r
409         /* Create the socket, if it has not already been created. */\r
410         if( xDHCPSocket == NULL )\r
411         {\r
412                 xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
413                 configASSERT( ( xDHCPSocket != FREERTOS_INVALID_SOCKET ) );\r
414 \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
419 \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
424 \r
425                 /* Remove compiler warnings if configASSERT() is not defined. */\r
426                 ( void ) xReturn;\r
427         }\r
428 }\r
429 /*-----------------------------------------------------------*/\r
430 \r
431 static void prvInitialiseDHCP( void )\r
432 {\r
433 extern void vIPFunctionsTimerCallback( xTimerHandle xTimer );\r
434 \r
435         /* Initialise the parameters that will be set by the DHCP process. */\r
436         if( ulTransactionId == 0 )\r
437         {\r
438                 ulTransactionId = ipconfigRAND32();\r
439         }\r
440         else\r
441         {\r
442                 ulTransactionId++;\r
443         }\r
444         ulOfferedIPAddress = 0UL;\r
445         ulDHCPServerAddress = 0UL;\r
446         xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
447 \r
448         /* Create the DHCP socket if it has not already been created. */\r
449         prvCreateDHCPSocket();\r
450 \r
451         if( xDHCPTimer == NULL )\r
452         {\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
456         }\r
457         else\r
458         {\r
459                 xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );\r
460         }\r
461 }\r
462 /*-----------------------------------------------------------*/\r
463 \r
464 static portBASE_TYPE prvProcessDHCPReplies( uint8_t ucExpectedMessageType, xMACAddress_t *pxMACAddress, xNetworkAddressingParameters_t *pxNetworkAddressing )\r
465 {\r
466 uint8_t *pucUDPPayload, *pucLastByte;\r
467 struct freertos_sockaddr xClient;\r
468 uint32_t xClientLength = sizeof( xClient );\r
469 int32_t lBytes;\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
475 \r
476         lBytes = FreeRTOS_recvfrom( xDHCPSocket, ( void * ) &pucUDPPayload, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );\r
477 \r
478         if( lBytes > 0 )\r
479         {\r
480                 /* Map a DHCP structure onto the received data. */\r
481                 pxDHCPMessage = ( xDHCPMessage_t * ) ( pucUDPPayload );\r
482 \r
483                 /* Sanity check. */\r
484                 if( ( pxDHCPMessage->ulDHCPCookie == dhcpCOOKIE ) && ( pxDHCPMessage->ucOpcode == dhcpREPLY_OPCODE ) && ( pxDHCPMessage->ulTransactionID == ulTransactionId ) )\r
485                 {\r
486                         if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), ( void * ) pxMACAddress, sizeof( xMACAddress_t ) ) == 0 )\r
487                         {\r
488                                 /* None of the essential options have been processed yet. */\r
489                                 ulProcessed = 0;\r
490 \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
496                                 {\r
497                                         ucOptionCode = *pucByte;\r
498                                         pucByte++;\r
499                                         ucLength = *pucByte;\r
500                                         pucByte++;\r
501 \r
502                                         switch( ucOptionCode )\r
503                                         {\r
504                                                 case dhcpMESSAGE_TYPE_OPTION_CODE       :\r
505 \r
506                                                         if( *pucByte == ucExpectedMessageType )\r
507                                                         {\r
508                                                                 /* The message type is the message type the\r
509                                                                 state machine is expecting. */\r
510                                                                 ulProcessed++;\r
511                                                         }\r
512                                                         else if( *pucByte == dhcpMESSAGE_TYPE_NACK )\r
513                                                         {\r
514                                                                 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_ACK )\r
515                                                                 {\r
516                                                                         /* Start again. */\r
517                                                                         eDHCPState = eWaitingSendFirstDiscover;\r
518                                                                 }\r
519                                                         }\r
520                                                         else\r
521                                                         {\r
522                                                                 /* Don't process other message types. */\r
523                                                         }\r
524                                                         break;\r
525 \r
526                                                 case dhcpSUBNET_MASK_OPTION_CODE :\r
527 \r
528                                                         if( ucLength == sizeof( uint32_t ) )\r
529                                                         {\r
530                                                                 memcpy( ( void * ) &( pxNetworkAddressing->ulNetMask ), ( void * ) pucByte, ( size_t ) ucLength );\r
531                                                         }\r
532                                                         break;\r
533 \r
534                                                 case dhcpGATEWAY_OPTION_CODE :\r
535 \r
536                                                         if( ucLength == sizeof( uint32_t ) )\r
537                                                         {\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
541                                                         }\r
542                                                         break;\r
543 \r
544                                                 case hdcpDNS_SERVER_OPTIONS_CODE :\r
545 \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
550                                                         break;\r
551 \r
552                                                 case dhcpSERVER_IP_ADDRESS_OPTION_CODE :\r
553 \r
554                                                         if( ucLength == sizeof( uint32_t ) )\r
555                                                         {\r
556                                                                 if( ucExpectedMessageType == dhcpMESSAGE_TYPE_OFFER )\r
557                                                                 {\r
558                                                                         /* Offers state the replying server. */\r
559                                                                         ulProcessed++;\r
560                                                                         memcpy( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength );\r
561                                                                 }\r
562                                                                 else\r
563                                                                 {\r
564                                                                         /* The ack must come from the expected server. */\r
565                                                                         if( memcmp( ( void * ) &ulDHCPServerAddress, ( void * ) pucByte, ( size_t ) ucLength ) == 0 )\r
566                                                                         {\r
567                                                                                 ulProcessed++;\r
568                                                                         }\r
569                                                                 }\r
570                                                         }\r
571                                                         break;\r
572 \r
573                                                 case dhcpLEASE_TIME_OPTION_CODE :\r
574 \r
575                                                         if( ucLength == sizeof( &ulLeaseTime ) )\r
576                                                         {\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
581 \r
582                                                                 /* Convert the lease time to milliseconds\r
583                                                                 (*1000) then ticks (/portTICK_RATE_MS). */\r
584                                                                 ulLeaseTime *= ( 1000UL / portTICK_RATE_MS );\r
585 \r
586                                                                 /* Divide the lease time to ensure a renew\r
587                                                                 request is sent before the lease actually\r
588                                                                 expires. */\r
589                                                                 ulLeaseTime >>= 1UL;\r
590                                                         }\r
591                                                         break;\r
592 \r
593                                                 default :\r
594 \r
595                                                         /* Not interested in this field. */\r
596 \r
597                                                         break;\r
598                                         }\r
599 \r
600                                         /* Jump over the data to find the next option code. */\r
601                                         if( ucLength == 0 )\r
602                                         {\r
603                                                 break;\r
604                                         }\r
605                                         else\r
606                                         {\r
607                                                 pucByte += ucLength;\r
608                                         }\r
609                                 }\r
610 \r
611                                 /* Were all the mandatory options received? */\r
612                                 if( ulProcessed == ulMandatoryOptions )\r
613                                 {\r
614                                         ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;\r
615                                         xReturn = pdPASS;\r
616                                 }\r
617                         }\r
618                 }\r
619 \r
620                 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );\r
621         }\r
622 \r
623         return xReturn;\r
624 }\r
625 /*-----------------------------------------------------------*/\r
626 \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
628 {\r
629 xDHCPMessage_t *pxDHCPMessage;\r
630 const size_t xRequiredBufferSize = sizeof( xDHCPMessage_t ) + xOptionsArraySize;\r
631 uint8_t *pucUDPPayloadBuffer;\r
632 \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
635         test. */\r
636         do\r
637         {\r
638         }while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );\r
639 \r
640         pxDHCPMessage = ( xDHCPMessage_t * ) pucUDPPayloadBuffer;\r
641 \r
642         /* Most fields need to be zero. */\r
643         memset( ( void * ) pxDHCPMessage, 0x00, sizeof( xDHCPMessage_t ) );\r
644 \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
653 \r
654         /* Copy in the const part of the options options. */\r
655         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, xOptionsArraySize );\r
656 \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
659 \r
660         /* Set the addressing. */\r
661         pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;\r
662         pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;\r
663 \r
664         return pucUDPPayloadBuffer;\r
665 }\r
666 /*-----------------------------------------------------------*/\r
667 \r
668 static void prvSendDHCPRequest( xMACAddress_t *pxMACAddress )\r
669 {\r
670 uint8_t *pucUDPPayloadBuffer;\r
671 struct freertos_sockaddr xAddress;\r
672 static const uint8_t ucDHCPRequestOptions[] =\r
673 {\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
682 };\r
683 \r
684         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPRequestOptions, sizeof( ucDHCPRequestOptions ) );\r
685 \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
688 \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
691 \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
694         {\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
698         }\r
699 }\r
700 /*-----------------------------------------------------------*/\r
701 \r
702 static void prvSendDHCPDiscover( xMACAddress_t *pxMACAddress )\r
703 {\r
704 uint8_t *pucUDPPayloadBuffer;\r
705 struct freertos_sockaddr xAddress;\r
706 static const uint8_t ucDHCPDiscoverOptions[] =\r
707 {\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
713 };\r
714 \r
715         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, pxMACAddress, dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, sizeof( ucDHCPDiscoverOptions ) );\r
716 \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
719         {\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
723         }\r
724 }\r
725 /*-----------------------------------------------------------*/\r
726 \r
727 #endif /* ipconfigUSE_DHCP != 0 */\r
728 \r
729 \r