]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c
8ce9e8e92dffb6c5b1d1dfd1e3bb96189fae717e
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / FreeRTOS_DHCP.c
1 /*\r
2  * FreeRTOS+TCP V2.0.3\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://aws.amazon.com/freertos\r
23  * http://www.FreeRTOS.org\r
24  */\r
25 \r
26 /* Standard includes. */\r
27 #include <stdint.h>\r
28 \r
29 /* FreeRTOS includes. */\r
30 #include "FreeRTOS.h"\r
31 #include "task.h"\r
32 #include "semphr.h"\r
33 \r
34 /* FreeRTOS+TCP includes. */\r
35 #include "FreeRTOS_IP.h"\r
36 #include "FreeRTOS_Sockets.h"\r
37 #include "FreeRTOS_IP_Private.h"\r
38 #include "FreeRTOS_UDP_IP.h"\r
39 #include "FreeRTOS_TCP_IP.h"\r
40 #include "FreeRTOS_DHCP.h"\r
41 #include "FreeRTOS_ARP.h"\r
42 #include "NetworkInterface.h"\r
43 #include "NetworkBufferManagement.h"\r
44 \r
45 /* Exclude the entire file if DHCP is not enabled. */\r
46 #if( ipconfigUSE_DHCP != 0 )\r
47 \r
48 #if ( ipconfigUSE_DHCP != 0 ) && ( ipconfigNETWORK_MTU < 586u )\r
49         /* DHCP must be able to receive an options field of 312 bytes, the fixed\r
50         part of the DHCP packet is 240 bytes, and the IP/UDP headers take 28 bytes. */\r
51         #error ipconfigNETWORK_MTU needs to be at least 586 to use DHCP\r
52 #endif\r
53 \r
54 /* Parameter widths in the DHCP packet. */\r
55 #define dhcpCLIENT_HARDWARE_ADDRESS_LENGTH              16\r
56 #define dhcpSERVER_HOST_NAME_LENGTH                             64\r
57 #define dhcpBOOT_FILE_NAME_LENGTH                               128\r
58 \r
59 /* Timer parameters */\r
60 #ifndef dhcpINITIAL_DHCP_TX_PERIOD\r
61         #define dhcpINITIAL_TIMER_PERIOD                        ( pdMS_TO_TICKS( 250 ) )\r
62         #define dhcpINITIAL_DHCP_TX_PERIOD                      ( pdMS_TO_TICKS( 5000 ) )\r
63 #endif\r
64 \r
65 /* Codes of interest found in the DHCP options field. */\r
66 #define dhcpIPv4_ZERO_PAD_OPTION_CODE                   ( 0u )\r
67 #define dhcpIPv4_SUBNET_MASK_OPTION_CODE                ( 1u )\r
68 #define dhcpIPv4_GATEWAY_OPTION_CODE                    ( 3u )\r
69 #define dhcpIPv4_DNS_SERVER_OPTIONS_CODE                ( 6u )\r
70 #define dhcpIPv4_DNS_HOSTNAME_OPTIONS_CODE              ( 12u )\r
71 #define dhcpIPv4_REQUEST_IP_ADDRESS_OPTION_CODE ( 50u )\r
72 #define dhcpIPv4_LEASE_TIME_OPTION_CODE                 ( 51u )\r
73 #define dhcpIPv4_MESSAGE_TYPE_OPTION_CODE               ( 53u )\r
74 #define dhcpIPv4_SERVER_IP_ADDRESS_OPTION_CODE  ( 54u )\r
75 #define dhcpIPv4_PARAMETER_REQUEST_OPTION_CODE  ( 55u )\r
76 #define dhcpIPv4_CLIENT_IDENTIFIER_OPTION_CODE  ( 61u )\r
77 \r
78 /* The four DHCP message types of interest. */\r
79 #define dhcpMESSAGE_TYPE_DISCOVER                               ( 1 )\r
80 #define dhcpMESSAGE_TYPE_OFFER                                  ( 2 )\r
81 #define dhcpMESSAGE_TYPE_REQUEST                                ( 3 )\r
82 #define dhcpMESSAGE_TYPE_ACK                                    ( 5 )\r
83 #define dhcpMESSAGE_TYPE_NACK                                   ( 6 )\r
84 \r
85 /* Offsets into the transmitted DHCP options fields at which various parameters\r
86 are located. */\r
87 #define dhcpCLIENT_IDENTIFIER_OFFSET                    ( 5 )\r
88 #define dhcpREQUESTED_IP_ADDRESS_OFFSET                 ( 13 )\r
89 #define dhcpDHCP_SERVER_IP_ADDRESS_OFFSET               ( 19 )\r
90 \r
91 /* Values used in the DHCP packets. */\r
92 #define dhcpREQUEST_OPCODE                                              ( 1 )\r
93 #define dhcpREPLY_OPCODE                                                ( 2 )\r
94 #define dhcpADDRESS_TYPE_ETHERNET                               ( 1 )\r
95 #define dhcpETHERNET_ADDRESS_LENGTH                             ( 6 )\r
96 \r
97 /* If a lease time is not received, use the default of two days. */\r
98 /* 48 hours in ticks.  Can not use pdMS_TO_TICKS() as integer overflow can occur. */\r
99 #define dhcpDEFAULT_LEASE_TIME                                  ( ( 48UL * 60UL * 60UL ) * configTICK_RATE_HZ )\r
100 \r
101 /* Don't allow the lease time to be too short. */\r
102 #define dhcpMINIMUM_LEASE_TIME                                  ( pdMS_TO_TICKS( 60000UL ) )    /* 60 seconds in ticks. */\r
103 \r
104 /* Marks the end of the variable length options field in the DHCP packet. */\r
105 #define dhcpOPTION_END_BYTE 0xffu\r
106 \r
107 /* Offset into a DHCP message at which the first byte of the options is\r
108 located. */\r
109 #define dhcpFIRST_OPTION_BYTE_OFFSET                    ( 0xf0 )\r
110 \r
111 /* When walking the variable length options field, the following value is used\r
112 to ensure the walk has not gone past the end of the valid options.  2 bytes is\r
113 made up of the length byte, and minimum one byte value. */\r
114 #define dhcpMAX_OPTION_LENGTH_OF_INTEREST               ( 2L )\r
115 \r
116 /* Standard DHCP port numbers and magic cookie value.\r
117 DHCPv4 uses UDP port number  68 for clients and port number  67 for servers.\r
118 */\r
119 #if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )\r
120         #define dhcpCLIENT_PORT 0x4400u\r
121         #define dhcpSERVER_PORT 0x4300u\r
122         #define dhcpCOOKIE              0x63538263ul\r
123         #define dhcpBROADCAST   0x0080u\r
124 #else\r
125         #define dhcpCLIENT_PORT 0x0044u\r
126         #define dhcpSERVER_PORT 0x0043u\r
127         #define dhcpCOOKIE              0x63825363ul\r
128         #define dhcpBROADCAST   0x8000u\r
129 #endif /* ipconfigBYTE_ORDER */\r
130 \r
131 #include "pack_struct_start.h"\r
132 struct xDHCPMessage\r
133 {\r
134         uint8_t ucOpcode;\r
135         uint8_t ucAddressType;\r
136         uint8_t ucAddressLength;\r
137         uint8_t ucHops;\r
138         uint32_t ulTransactionID;\r
139         uint16_t usElapsedTime;\r
140         uint16_t usFlags;\r
141         uint32_t ulClientIPAddress_ciaddr;\r
142         uint32_t ulYourIPAddress_yiaddr;\r
143         uint32_t ulServerIPAddress_siaddr;\r
144         uint32_t ulRelayAgentIPAddress_giaddr;\r
145         uint8_t ucClientHardwareAddress[ dhcpCLIENT_HARDWARE_ADDRESS_LENGTH ];\r
146         uint8_t ucServerHostName[ dhcpSERVER_HOST_NAME_LENGTH ];\r
147         uint8_t ucBootFileName[ dhcpBOOT_FILE_NAME_LENGTH ];\r
148         uint32_t ulDHCPCookie;\r
149         uint8_t ucFirstOptionByte;\r
150 }\r
151 #include "pack_struct_end.h"\r
152 typedef struct xDHCPMessage DHCPMessage_t;\r
153 \r
154 /* DHCP state machine states. */\r
155 typedef enum\r
156 {\r
157         eWaitingSendFirstDiscover = 0,  /* Initial state.  Send a discover the first time it is called, and reset all timers. */\r
158         eWaitingOffer,                                  /* Either resend the discover, or, if the offer is forthcoming, send a request. */\r
159         eWaitingAcknowledge,                    /* Either resend the request. */\r
160         #if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )\r
161                 eGetLinkLayerAddress,           /* When DHCP didn't respond, try to obtain a LinkLayer address 168.254.x.x. */\r
162         #endif\r
163         eLeasedAddress,                                 /* Resend the request at the appropriate time to renew the lease. */\r
164         eNotUsingLeasedAddress                  /* DHCP failed, and a default IP address is being used. */\r
165 } eDHCPState_t;\r
166 \r
167 /* Hold information in between steps in the DHCP state machine. */\r
168 struct xDHCP_DATA\r
169 {\r
170         uint32_t ulTransactionId;\r
171         uint32_t ulOfferedIPAddress;\r
172         uint32_t ulDHCPServerAddress;\r
173         uint32_t ulLeaseTime;\r
174         /* Hold information on the current timer state. */\r
175         TickType_t xDHCPTxTime;\r
176         TickType_t xDHCPTxPeriod;\r
177         /* Try both without and with the broadcast flag */\r
178         BaseType_t xUseBroadcast;\r
179         /* Maintains the DHCP state machine state. */\r
180         eDHCPState_t eDHCPState;\r
181         /* The UDP socket used for all incoming and outgoing DHCP traffic. */\r
182         Socket_t xDHCPSocket;\r
183 };\r
184 \r
185 typedef struct xDHCP_DATA DHCPData_t;\r
186 \r
187 #if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )\r
188         /* Define the Link Layer IP address: 169.254.x.x */\r
189         #define LINK_LAYER_ADDRESS_0    169\r
190         #define LINK_LAYER_ADDRESS_1    254\r
191 \r
192         /* Define the netmask used: 255.255.0.0 */\r
193         #define LINK_LAYER_NETMASK_0    255\r
194         #define LINK_LAYER_NETMASK_1    255\r
195         #define LINK_LAYER_NETMASK_2    0\r
196         #define LINK_LAYER_NETMASK_3    0\r
197 #endif\r
198 \r
199 \r
200 /*\r
201  * Generate a DHCP discover message and send it on the DHCP socket.\r
202  */\r
203 static void prvSendDHCPDiscover( void );\r
204 \r
205 /*\r
206  * Interpret message received on the DHCP socket.\r
207  */\r
208 static BaseType_t prvProcessDHCPReplies( BaseType_t xExpectedMessageType );\r
209 \r
210 /*\r
211  * Generate a DHCP request packet, and send it on the DHCP socket.\r
212  */\r
213 static void prvSendDHCPRequest( void );\r
214 \r
215 /*\r
216  * Prepare to start a DHCP transaction.  This initialises some state variables\r
217  * and creates the DHCP socket if necessary.\r
218  */\r
219 static void prvInitialiseDHCP( void );\r
220 \r
221 /*\r
222  * Creates the part of outgoing DHCP messages that are common to all outgoing\r
223  * DHCP messages.\r
224  */\r
225 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, BaseType_t xOpcode, const uint8_t * const pucOptionsArray, size_t *pxOptionsArraySize );\r
226 \r
227 /*\r
228  * Create the DHCP socket, if it has not been created already.\r
229  */\r
230 static void prvCreateDHCPSocket( void );\r
231 \r
232 /*\r
233  * After DHCP has failed to answer, prepare everything to start searching\r
234  * for (trying-out) LinkLayer IP-addresses, using the random method: Send\r
235  * a gratuitous ARP request and wait if another device responds to it.\r
236  */\r
237 #if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )\r
238         static void prvPrepareLinkLayerIPLookUp( void );\r
239 #endif\r
240 \r
241 /*-----------------------------------------------------------*/\r
242 \r
243 /* The next DHCP transaction Id to be used. */\r
244 static DHCPData_t xDHCPData;\r
245 \r
246 /*-----------------------------------------------------------*/\r
247 \r
248 BaseType_t xIsDHCPSocket( Socket_t xSocket )\r
249 {\r
250 BaseType_t xReturn;\r
251 \r
252         if( xDHCPData.xDHCPSocket == xSocket )\r
253         {\r
254                 xReturn = pdTRUE;\r
255         }\r
256         else\r
257         {\r
258                 xReturn = pdFALSE;\r
259         }\r
260 \r
261         return xReturn;\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 void vDHCPProcess( BaseType_t xReset )\r
266 {\r
267 BaseType_t xGivingUp = pdFALSE;\r
268 #if( ipconfigUSE_DHCP_HOOK != 0 )\r
269         eDHCPCallbackAnswer_t eAnswer;\r
270 #endif  /* ipconfigUSE_DHCP_HOOK */\r
271 \r
272         /* Is DHCP starting over? */\r
273         if( xReset != pdFALSE )\r
274         {\r
275                 xDHCPData.eDHCPState = eWaitingSendFirstDiscover;\r
276         }\r
277 \r
278         switch( xDHCPData.eDHCPState )\r
279         {\r
280                 case eWaitingSendFirstDiscover :\r
281                         /* Ask the user if a DHCP discovery is required. */\r
282                 #if( ipconfigUSE_DHCP_HOOK != 0 )\r
283                         eAnswer = xApplicationDHCPHook( eDHCPPhasePreDiscover, xNetworkAddressing.ulDefaultIPAddress );\r
284                         if( eAnswer == eDHCPContinue )\r
285                 #endif  /* ipconfigUSE_DHCP_HOOK */\r
286                         {\r
287                                 /* Initial state.  Create the DHCP socket, timer, etc. if they\r
288                                 have not already been created. */\r
289                                 prvInitialiseDHCP();\r
290 \r
291                                 /* See if prvInitialiseDHCP() has creates a socket. */\r
292                                 if( xDHCPData.xDHCPSocket == NULL )\r
293                                 {\r
294                                         xGivingUp = pdTRUE;\r
295                                         break;\r
296                                 }\r
297 \r
298                                 *ipLOCAL_IP_ADDRESS_POINTER = 0UL;\r
299 \r
300                                 /* Send the first discover request. */\r
301                                 if( xDHCPData.xDHCPSocket != NULL )\r
302                                 {\r
303                                         xDHCPData.xDHCPTxTime = xTaskGetTickCount();\r
304                                         prvSendDHCPDiscover( );\r
305                                         xDHCPData.eDHCPState = eWaitingOffer;\r
306                                 }\r
307                         }\r
308                 #if( ipconfigUSE_DHCP_HOOK != 0 )\r
309                         else\r
310                         {\r
311                                 if( eAnswer == eDHCPUseDefaults )\r
312                                 {\r
313                                         memcpy( &xNetworkAddressing, &xDefaultAddressing, sizeof( xNetworkAddressing ) );\r
314                                 }\r
315 \r
316                                 /* The user indicates that the DHCP process does not continue. */\r
317                                 xGivingUp = pdTRUE;\r
318                         }\r
319                 #endif  /* ipconfigUSE_DHCP_HOOK */\r
320                         break;\r
321 \r
322                 case eWaitingOffer :\r
323 \r
324                         xGivingUp = pdFALSE;\r
325 \r
326                         /* Look for offers coming in. */\r
327                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_OFFER ) == pdPASS )\r
328                         {\r
329                         #if( ipconfigUSE_DHCP_HOOK != 0 )\r
330                                 /* Ask the user if a DHCP request is required. */\r
331                                 eAnswer = xApplicationDHCPHook( eDHCPPhasePreRequest, xDHCPData.ulOfferedIPAddress );\r
332 \r
333                                 if( eAnswer == eDHCPContinue )\r
334                         #endif  /* ipconfigUSE_DHCP_HOOK */\r
335                                 {\r
336                                         /* An offer has been made, the user wants to continue,\r
337                                         generate the request. */\r
338                                         xDHCPData.xDHCPTxTime = xTaskGetTickCount();\r
339                                         xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
340                                         prvSendDHCPRequest( );\r
341                                         xDHCPData.eDHCPState = eWaitingAcknowledge;\r
342                                         break;\r
343                                 }\r
344 \r
345                         #if( ipconfigUSE_DHCP_HOOK != 0 )\r
346                                 if( eAnswer == eDHCPUseDefaults )\r
347                                 {\r
348                                         memcpy( &xNetworkAddressing, &xDefaultAddressing, sizeof( xNetworkAddressing ) );\r
349                                 }\r
350 \r
351                                 /* The user indicates that the DHCP process does not continue. */\r
352                                 xGivingUp = pdTRUE;\r
353                         #endif  /* ipconfigUSE_DHCP_HOOK */\r
354                         }\r
355                         else if( ( xTaskGetTickCount() - xDHCPData.xDHCPTxTime ) > xDHCPData.xDHCPTxPeriod )\r
356                         {\r
357                                 /* It is time to send another Discover.  Increase the time\r
358                                 period, and if it has not got to the point of giving up - send\r
359                                 another discovery. */\r
360                                 xDHCPData.xDHCPTxPeriod <<= 1;\r
361 \r
362                                 if( xDHCPData.xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )\r
363                                 {\r
364                                         xDHCPData.ulTransactionId++;\r
365                                         xDHCPData.xDHCPTxTime = xTaskGetTickCount();\r
366                                         xDHCPData.xUseBroadcast = !xDHCPData.xUseBroadcast;\r
367                                         prvSendDHCPDiscover( );\r
368                                         FreeRTOS_debug_printf( ( "vDHCPProcess: timeout %lu ticks\n", xDHCPData.xDHCPTxPeriod ) );\r
369                                 }\r
370                                 else\r
371                                 {\r
372                                         FreeRTOS_debug_printf( ( "vDHCPProcess: giving up %lu > %lu ticks\n", xDHCPData.xDHCPTxPeriod, ipconfigMAXIMUM_DISCOVER_TX_PERIOD ) );\r
373 \r
374                                         #if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )\r
375                                         {\r
376                                                 /* Only use a fake Ack if the default IP address == 0x00\r
377                                                 and the link local addressing is used.  Start searching\r
378                                                 a free LinkLayer IP-address.  Next state will be\r
379                                                 'eGetLinkLayerAddress'. */\r
380                                                 prvPrepareLinkLayerIPLookUp();\r
381 \r
382                                                 /* Setting an IP address manually so set to not using\r
383                                                 leased address mode. */\r
384                                                 xDHCPData.eDHCPState = eGetLinkLayerAddress;\r
385                                         }\r
386                                         #else\r
387                                         {\r
388                                                 xGivingUp = pdTRUE;\r
389                                         }\r
390                                         #endif /* ipconfigDHCP_FALL_BACK_AUTO_IP */\r
391                                 }\r
392                         }\r
393                         break;\r
394 \r
395                 case eWaitingAcknowledge :\r
396 \r
397                         /* Look for acks coming in. */\r
398                         if( prvProcessDHCPReplies( dhcpMESSAGE_TYPE_ACK ) == pdPASS )\r
399                         {\r
400                                 FreeRTOS_debug_printf( ( "vDHCPProcess: acked %lxip\n", FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );\r
401 \r
402                                 /* DHCP completed.  The IP address can now be used, and the\r
403                                 timer set to the lease timeout time. */\r
404                                 *ipLOCAL_IP_ADDRESS_POINTER = xDHCPData.ulOfferedIPAddress;\r
405 \r
406                                 /* Setting the 'local' broadcast address, something like\r
407                                 '192.168.1.255'. */\r
408                                 xNetworkAddressing.ulBroadcastAddress = ( xDHCPData.ulOfferedIPAddress & xNetworkAddressing.ulNetMask ) |  ~xNetworkAddressing.ulNetMask;\r
409                                 xDHCPData.eDHCPState = eLeasedAddress;\r
410 \r
411                                 iptraceDHCP_SUCCEDEED( xDHCPData.ulOfferedIPAddress );\r
412 \r
413                                 /* DHCP failed, the default configured IP-address will be used\r
414                                 Now call vIPNetworkUpCalls() to send the network-up event and\r
415                                 start the ARP timer. */\r
416                                 vIPNetworkUpCalls( );\r
417 \r
418                                 /* Close socket to ensure packets don't queue on it. */\r
419                                 vSocketClose( xDHCPData.xDHCPSocket );\r
420                                 xDHCPData.xDHCPSocket = NULL;\r
421 \r
422                                 if( xDHCPData.ulLeaseTime == 0UL )\r
423                                 {\r
424                                         xDHCPData.ulLeaseTime = dhcpDEFAULT_LEASE_TIME;\r
425                                 }\r
426                                 else if( xDHCPData.ulLeaseTime < dhcpMINIMUM_LEASE_TIME )\r
427                                 {\r
428                                         xDHCPData.ulLeaseTime = dhcpMINIMUM_LEASE_TIME;\r
429                                 }\r
430                                 else\r
431                                 {\r
432                                         /* The lease time is already valid. */\r
433                                 }\r
434 \r
435                                 /* Check for clashes. */\r
436                                 vARPSendGratuitous();\r
437                                 vIPReloadDHCPTimer( xDHCPData.ulLeaseTime );\r
438                         }\r
439                         else\r
440                         {\r
441                                 /* Is it time to send another Discover? */\r
442                                 if( ( xTaskGetTickCount() - xDHCPData.xDHCPTxTime ) > xDHCPData.xDHCPTxPeriod )\r
443                                 {\r
444                                         /* Increase the time period, and if it has not got to the\r
445                                         point of giving up - send another request. */\r
446                                         xDHCPData.xDHCPTxPeriod <<= 1;\r
447 \r
448                                         if( xDHCPData.xDHCPTxPeriod <= ipconfigMAXIMUM_DISCOVER_TX_PERIOD )\r
449                                         {\r
450                                                 xDHCPData.xDHCPTxTime = xTaskGetTickCount();\r
451                                                 prvSendDHCPRequest( );\r
452                                         }\r
453                                         else\r
454                                         {\r
455                                                 /* Give up, start again. */\r
456                                                 xDHCPData.eDHCPState = eWaitingSendFirstDiscover;\r
457                                         }\r
458                                 }\r
459                         }\r
460                         break;\r
461 \r
462         #if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )\r
463                 case eGetLinkLayerAddress:\r
464                         if( ( xTaskGetTickCount() - xDHCPData.xDHCPTxTime ) > xDHCPData.xDHCPTxPeriod )\r
465                         {\r
466                                 if( xARPHadIPClash == pdFALSE )\r
467                                 {\r
468                                         /* ARP OK. proceed. */\r
469                                         iptraceDHCP_SUCCEDEED( xDHCPData.ulOfferedIPAddress );\r
470 \r
471                                         /* Auto-IP succeeded, the default configured IP-address will\r
472                                         be used.  Now call vIPNetworkUpCalls() to send the\r
473                                         network-up event and start the ARP timer. */\r
474                                         vIPNetworkUpCalls( );\r
475                                         xDHCPData.eDHCPState = eNotUsingLeasedAddress;\r
476                                 }\r
477                                 else\r
478                                 {\r
479                                         /* ARP clashed - try another IP address. */\r
480                                         prvPrepareLinkLayerIPLookUp();\r
481 \r
482                                         /* Setting an IP address manually so set to not using leased\r
483                                         address mode. */\r
484                                         xDHCPData.eDHCPState = eGetLinkLayerAddress;\r
485                                 }\r
486                         }\r
487                         break;\r
488         #endif  /* ipconfigDHCP_FALL_BACK_AUTO_IP */\r
489 \r
490                 case eLeasedAddress :\r
491 \r
492                         /* Resend the request at the appropriate time to renew the lease. */\r
493                         prvCreateDHCPSocket();\r
494 \r
495                         if( xDHCPData.xDHCPSocket != NULL )\r
496                         {\r
497                                 xDHCPData.xDHCPTxTime = xTaskGetTickCount();\r
498                                 xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
499                                 prvSendDHCPRequest( );\r
500                                 xDHCPData.eDHCPState = eWaitingAcknowledge;\r
501 \r
502                                 /* From now on, we should be called more often */\r
503                                 vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD );\r
504                         }\r
505                         break;\r
506 \r
507                 case eNotUsingLeasedAddress:\r
508 \r
509                         vIPSetDHCPTimerEnableState( pdFALSE );\r
510                         break;\r
511 \r
512                 default:\r
513                         break;\r
514         }\r
515 \r
516         if( xGivingUp != pdFALSE )\r
517         {\r
518                 /* xGivingUp became true either because of a time-out, or because\r
519                 xApplicationDHCPHook() returned another value than 'eDHCPContinue',\r
520                 meaning that the conversion is cancelled from here. */\r
521 \r
522                 /* Revert to static IP address. */\r
523                 taskENTER_CRITICAL();\r
524                 {\r
525                         *ipLOCAL_IP_ADDRESS_POINTER = xNetworkAddressing.ulDefaultIPAddress;\r
526                         iptraceDHCP_REQUESTS_FAILED_USING_DEFAULT_IP_ADDRESS( xNetworkAddressing.ulDefaultIPAddress );\r
527                 }\r
528                 taskEXIT_CRITICAL();\r
529 \r
530                 xDHCPData.eDHCPState = eNotUsingLeasedAddress;\r
531                 vIPSetDHCPTimerEnableState( pdFALSE );\r
532 \r
533                 /* DHCP failed, the default configured IP-address will be used.  Now\r
534                 call vIPNetworkUpCalls() to send the network-up event and start the ARP\r
535                 timer. */\r
536                 vIPNetworkUpCalls( );\r
537 \r
538                 /* Test if socket was indeed created. */\r
539                 if( xDHCPData.xDHCPSocket != NULL )\r
540                 {\r
541                         /* Close socket to ensure packets don't queue on it. */\r
542                         vSocketClose( xDHCPData.xDHCPSocket );\r
543                         xDHCPData.xDHCPSocket = NULL;\r
544                 }\r
545         }\r
546 }\r
547 /*-----------------------------------------------------------*/\r
548 \r
549 static void prvCreateDHCPSocket( void )\r
550 {\r
551 struct freertos_sockaddr xAddress;\r
552 BaseType_t xReturn;\r
553 TickType_t xTimeoutTime = ( TickType_t ) 0;\r
554 \r
555         /* Create the socket, if it has not already been created. */\r
556         if( xDHCPData.xDHCPSocket == NULL )\r
557         {\r
558                 xDHCPData.xDHCPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
559                 if( xDHCPData.xDHCPSocket != FREERTOS_INVALID_SOCKET )\r
560                 {\r
561 \r
562                         /* Ensure the Rx and Tx timeouts are zero as the DHCP executes in the\r
563                         context of the IP task. */\r
564                         FreeRTOS_setsockopt( xDHCPData.xDHCPSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );\r
565                         FreeRTOS_setsockopt( xDHCPData.xDHCPSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );\r
566 \r
567                         /* Bind to the standard DHCP client port. */\r
568                         xAddress.sin_port = ( uint16_t ) dhcpCLIENT_PORT;\r
569                         xReturn = vSocketBind( xDHCPData.xDHCPSocket, &xAddress, sizeof( xAddress ), pdFALSE );\r
570                         if( xReturn != 0 )\r
571                         {\r
572                                 /* Binding failed, close the socket again. */\r
573                                 vSocketClose( xDHCPData.xDHCPSocket );\r
574                                 xDHCPData.xDHCPSocket = NULL;\r
575                         }\r
576                 }\r
577                 else\r
578                 {\r
579                         /* Change to NULL for easier testing. */\r
580                         xDHCPData.xDHCPSocket = NULL;\r
581                 }\r
582         }\r
583 }\r
584 /*-----------------------------------------------------------*/\r
585 \r
586 static void prvInitialiseDHCP( void )\r
587 {\r
588         /* Initialise the parameters that will be set by the DHCP process. */\r
589         if( xDHCPData.ulTransactionId == 0ul )\r
590         {\r
591                 xDHCPData.ulTransactionId = ipconfigRAND32();\r
592         }\r
593         else\r
594         {\r
595                 xDHCPData.ulTransactionId++;\r
596         }\r
597 \r
598         /* Check for random number generator API failure. */\r
599         if( 0 != xDHCPData.ulTransactionId )\r
600         {\r
601                 xDHCPData.xUseBroadcast = 0;\r
602                 xDHCPData.ulOfferedIPAddress = 0UL;\r
603                 xDHCPData.ulDHCPServerAddress = 0UL;\r
604                 xDHCPData.xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;\r
605 \r
606                 /* Create the DHCP socket if it has not already been created. */\r
607                 prvCreateDHCPSocket();\r
608                 FreeRTOS_debug_printf( ( "prvInitialiseDHCP: start after %lu ticks\n", dhcpINITIAL_TIMER_PERIOD ) );\r
609                 vIPReloadDHCPTimer( dhcpINITIAL_TIMER_PERIOD );\r
610         }\r
611 }\r
612 /*-----------------------------------------------------------*/\r
613 \r
614 static BaseType_t prvProcessDHCPReplies( BaseType_t xExpectedMessageType )\r
615 {\r
616 uint8_t *pucUDPPayload, *pucLastByte;\r
617 struct freertos_sockaddr xClient;\r
618 uint32_t xClientLength = sizeof( xClient );\r
619 int32_t lBytes;\r
620 DHCPMessage_t *pxDHCPMessage;\r
621 uint8_t *pucByte, ucOptionCode, ucLength;\r
622 uint32_t ulProcessed, ulParameter;\r
623 BaseType_t xReturn = pdFALSE;\r
624 const uint32_t ulMandatoryOptions = 2ul; /* DHCP server address, and the correct DHCP message type must be present in the options. */\r
625 \r
626         lBytes = FreeRTOS_recvfrom( xDHCPData.xDHCPSocket, ( void * ) &pucUDPPayload, 0ul, FREERTOS_ZERO_COPY, &xClient, &xClientLength );\r
627 \r
628         if( lBytes > 0 )\r
629         {\r
630                 /* Map a DHCP structure onto the received data. */\r
631                 pxDHCPMessage = ( DHCPMessage_t * ) ( pucUDPPayload );\r
632 \r
633                 /* Sanity check. */\r
634                 if( ( pxDHCPMessage->ulDHCPCookie == ( uint32_t ) dhcpCOOKIE ) &&\r
635                         ( pxDHCPMessage->ucOpcode == ( uint8_t ) dhcpREPLY_OPCODE ) &&\r
636                         ( pxDHCPMessage->ulTransactionID == FreeRTOS_htonl( xDHCPData.ulTransactionId ) ) )\r
637                 {\r
638                         if( memcmp( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress ), ( void * ) ipLOCAL_MAC_ADDRESS, sizeof( MACAddress_t ) ) == 0 )\r
639                         {\r
640                                 /* None of the essential options have been processed yet. */\r
641                                 ulProcessed = 0ul;\r
642 \r
643                                 /* Walk through the options until the dhcpOPTION_END_BYTE byte\r
644                                 is found, taking care not to walk off the end of the options. */\r
645                                 pucByte = &( pxDHCPMessage->ucFirstOptionByte );\r
646                                 pucLastByte = &( pucUDPPayload[ lBytes - dhcpMAX_OPTION_LENGTH_OF_INTEREST ] );\r
647 \r
648                                 while( pucByte < pucLastByte )\r
649                                 {\r
650                                         ucOptionCode = pucByte[ 0 ];\r
651                                         if( ucOptionCode == ( uint8_t ) dhcpOPTION_END_BYTE )\r
652                                         {\r
653                                                 /* Ready, the last byte has been seen. */\r
654                                                 break;\r
655                                         }\r
656                                         if( ucOptionCode == ( uint8_t ) dhcpIPv4_ZERO_PAD_OPTION_CODE )\r
657                                         {\r
658                                                 /* The value zero is used as a pad byte,\r
659                                                 it is not followed by a length byte. */\r
660                                                 pucByte += 1;\r
661                                                 continue;\r
662                                         }\r
663                                         ucLength = pucByte[ 1 ];\r
664                                         pucByte += 2;\r
665 \r
666                                         /* In most cases, a 4-byte network-endian parameter follows,\r
667                                         just get it once here and use later */\r
668                                         memcpy( ( void * ) &( ulParameter ), ( void * ) pucByte, ( size_t ) sizeof( ulParameter ) );\r
669 \r
670                                         switch( ucOptionCode )\r
671                                         {\r
672                                                 case dhcpIPv4_MESSAGE_TYPE_OPTION_CODE  :\r
673 \r
674                                                         if( *pucByte == ( uint8_t ) xExpectedMessageType )\r
675                                                         {\r
676                                                                 /* The message type is the message type the\r
677                                                                 state machine is expecting. */\r
678                                                                 ulProcessed++;\r
679                                                         }\r
680                                                         else if( *pucByte == ( uint8_t ) dhcpMESSAGE_TYPE_NACK )\r
681                                                         {\r
682                                                                 if( xExpectedMessageType == ( BaseType_t ) dhcpMESSAGE_TYPE_ACK )\r
683                                                                 {\r
684                                                                         /* Start again. */\r
685                                                                         xDHCPData.eDHCPState = eWaitingSendFirstDiscover;\r
686                                                                 }\r
687                                                         }\r
688                                                         else\r
689                                                         {\r
690                                                                 /* Don't process other message types. */\r
691                                                         }\r
692                                                         break;\r
693 \r
694                                                 case dhcpIPv4_SUBNET_MASK_OPTION_CODE :\r
695 \r
696                                                         if( ucLength == sizeof( uint32_t ) )\r
697                                                         {\r
698                                                                 xNetworkAddressing.ulNetMask = ulParameter;\r
699                                                         }\r
700                                                         break;\r
701 \r
702                                                 case dhcpIPv4_GATEWAY_OPTION_CODE :\r
703                                                         /* The DHCP server may send more than 1 gateway addresses. */\r
704                                                         if( ucLength >= sizeof( uint32_t ) )\r
705                                                         {\r
706                                                                 /* ulProcessed is not incremented in this case\r
707                                                                 because the gateway is not essential. */\r
708                                                                 xNetworkAddressing.ulGatewayAddress = ulParameter;\r
709                                                         }\r
710                                                         break;\r
711 \r
712                                                 case dhcpIPv4_DNS_SERVER_OPTIONS_CODE :\r
713 \r
714                                                         /* ulProcessed is not incremented in this case\r
715                                                         because the DNS server is not essential.  Only the\r
716                                                         first DNS server address is taken. */\r
717                                                         xNetworkAddressing.ulDNSServerAddress = ulParameter;\r
718                                                         break;\r
719 \r
720                                                 case dhcpIPv4_SERVER_IP_ADDRESS_OPTION_CODE :\r
721 \r
722                                                         if( ucLength == sizeof( uint32_t ) )\r
723                                                         {\r
724                                                                 if( xExpectedMessageType == ( BaseType_t ) dhcpMESSAGE_TYPE_OFFER )\r
725                                                                 {\r
726                                                                         /* Offers state the replying server. */\r
727                                                                         ulProcessed++;\r
728                                                                         xDHCPData.ulDHCPServerAddress = ulParameter;\r
729                                                                 }\r
730                                                                 else\r
731                                                                 {\r
732                                                                         /* The ack must come from the expected server. */\r
733                                                                         if( xDHCPData.ulDHCPServerAddress == ulParameter )\r
734                                                                         {\r
735                                                                                 ulProcessed++;\r
736                                                                         }\r
737                                                                 }\r
738                                                         }\r
739                                                         break;\r
740 \r
741                                                 case dhcpIPv4_LEASE_TIME_OPTION_CODE :\r
742 \r
743                                                         if( ucLength == sizeof( xDHCPData.ulLeaseTime ) )\r
744                                                         {\r
745                                                                 /* ulProcessed is not incremented in this case\r
746                                                                 because the lease time is not essential. */\r
747                                                                 /* The DHCP parameter is in seconds, convert\r
748                                                                 to host-endian format. */\r
749                                                                 xDHCPData.ulLeaseTime = FreeRTOS_ntohl( ulParameter );\r
750 \r
751                                                                 /* Divide the lease time by two to ensure a\r
752                                                                 renew request is sent before the lease actually\r
753                                                                 expires. */\r
754                                                                 xDHCPData.ulLeaseTime >>= 1UL;\r
755 \r
756                                                                 /* Multiply with configTICK_RATE_HZ to get clock\r
757                                                                 ticks. */\r
758                                                                 xDHCPData.ulLeaseTime = configTICK_RATE_HZ * xDHCPData.ulLeaseTime;\r
759                                                         }\r
760                                                         break;\r
761 \r
762                                                 default :\r
763 \r
764                                                         /* Not interested in this field. */\r
765 \r
766                                                         break;\r
767                                         }\r
768 \r
769                                         /* Jump over the data to find the next option code. */\r
770                                         if( ucLength == 0u )\r
771                                         {\r
772                                                 break;\r
773                                         }\r
774                                         else\r
775                                         {\r
776                                                 pucByte += ucLength;\r
777                                         }\r
778                                 }\r
779 \r
780                                 /* Were all the mandatory options received? */\r
781                                 if( ulProcessed >= ulMandatoryOptions )\r
782                                 {\r
783                                         /* HT:endian: used to be network order */\r
784                                         xDHCPData.ulOfferedIPAddress = pxDHCPMessage->ulYourIPAddress_yiaddr;\r
785                                         FreeRTOS_printf( ( "vDHCPProcess: offer %lxip\n", FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );\r
786                                         xReturn = pdPASS;\r
787                                 }\r
788                         }\r
789                 }\r
790 \r
791                 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayload );\r
792         } /* if( lBytes > 0 ) */\r
793 \r
794         return xReturn;\r
795 }\r
796 /*-----------------------------------------------------------*/\r
797 \r
798 static uint8_t *prvCreatePartDHCPMessage( struct freertos_sockaddr *pxAddress, BaseType_t xOpcode, const uint8_t * const pucOptionsArray, size_t *pxOptionsArraySize )\r
799 {\r
800 DHCPMessage_t *pxDHCPMessage;\r
801 size_t xRequiredBufferSize = sizeof( DHCPMessage_t ) + *pxOptionsArraySize;\r
802 uint8_t *pucUDPPayloadBuffer;\r
803 \r
804 #if( ipconfigDHCP_REGISTER_HOSTNAME == 1 )\r
805         const char *pucHostName = pcApplicationHostnameHook ();\r
806         size_t xNameLength = strlen( pucHostName );\r
807         uint8_t *pucPtr;\r
808 \r
809         xRequiredBufferSize += ( 2 + xNameLength );\r
810 #endif\r
811 \r
812         /* Get a buffer.  This uses a maximum delay, but the delay will be capped\r
813         to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value still needs to\r
814         be test. */\r
815         do\r
816         {\r
817         } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xRequiredBufferSize, portMAX_DELAY ) ) == NULL );\r
818 \r
819         pxDHCPMessage = ( DHCPMessage_t * ) pucUDPPayloadBuffer;\r
820 \r
821         /* Most fields need to be zero. */\r
822         memset( ( void * ) pxDHCPMessage, 0x00, sizeof( DHCPMessage_t ) );\r
823 \r
824         /* Create the message. */\r
825         pxDHCPMessage->ucOpcode = ( uint8_t ) xOpcode;\r
826         pxDHCPMessage->ucAddressType = ( uint8_t ) dhcpADDRESS_TYPE_ETHERNET;\r
827         pxDHCPMessage->ucAddressLength = ( uint8_t ) dhcpETHERNET_ADDRESS_LENGTH;\r
828 \r
829         /* ulTransactionID doesn't really need a htonl() translation, but when DHCP\r
830         times out, it is nicer to see an increasing number in this ID field */\r
831         pxDHCPMessage->ulTransactionID = FreeRTOS_htonl( xDHCPData.ulTransactionId );\r
832         pxDHCPMessage->ulDHCPCookie = ( uint32_t ) dhcpCOOKIE;\r
833         if( xDHCPData.xUseBroadcast != pdFALSE )\r
834         {\r
835                 pxDHCPMessage->usFlags = ( uint16_t ) dhcpBROADCAST;\r
836         }\r
837         else\r
838         {\r
839                 pxDHCPMessage->usFlags = 0u;\r
840         }\r
841 \r
842         memcpy( ( void * ) &( pxDHCPMessage->ucClientHardwareAddress[ 0 ] ), ( void * ) ipLOCAL_MAC_ADDRESS, sizeof( MACAddress_t ) );\r
843 \r
844         /* Copy in the const part of the options options. */\r
845         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET ] ), ( void * ) pucOptionsArray, *pxOptionsArraySize );\r
846 \r
847         #if( ipconfigDHCP_REGISTER_HOSTNAME == 1 )\r
848         {\r
849                 /* With this option, the hostname can be registered as well which makes\r
850                 it easier to lookup a device in a router's list of DHCP clients. */\r
851 \r
852                 /* Point to where the OPTION_END was stored to add data. */\r
853                 pucPtr = &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + ( *pxOptionsArraySize - 1 ) ] );\r
854                 pucPtr[ 0 ] = dhcpIPv4_DNS_HOSTNAME_OPTIONS_CODE;\r
855                 pucPtr[ 1 ] = ( uint8_t ) xNameLength;\r
856                 memcpy( ( void *) ( pucPtr + 2 ), pucHostName, xNameLength );\r
857                 pucPtr[ 2 + xNameLength ] = dhcpOPTION_END_BYTE;\r
858                 *pxOptionsArraySize += ( 2 + xNameLength );\r
859         }\r
860         #endif\r
861 \r
862         /* Map in the client identifier. */\r
863         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpCLIENT_IDENTIFIER_OFFSET ] ),\r
864                 ( void * ) ipLOCAL_MAC_ADDRESS, sizeof( MACAddress_t ) );\r
865 \r
866         /* Set the addressing. */\r
867         pxAddress->sin_addr = ipBROADCAST_IP_ADDRESS;\r
868         pxAddress->sin_port = ( uint16_t ) dhcpSERVER_PORT;\r
869 \r
870         return pucUDPPayloadBuffer;\r
871 }\r
872 /*-----------------------------------------------------------*/\r
873 \r
874 static void prvSendDHCPRequest( void )\r
875 {\r
876 uint8_t *pucUDPPayloadBuffer;\r
877 struct freertos_sockaddr xAddress;\r
878 static const uint8_t ucDHCPRequestOptions[] =\r
879 {\r
880         /* Do not change the ordering without also changing\r
881         dhcpCLIENT_IDENTIFIER_OFFSET, dhcpREQUESTED_IP_ADDRESS_OFFSET and\r
882         dhcpDHCP_SERVER_IP_ADDRESS_OFFSET. */\r
883         dhcpIPv4_MESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_REQUEST,         /* Message type option. */\r
884         dhcpIPv4_CLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0,                    /* Client identifier. */\r
885         dhcpIPv4_REQUEST_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0,                         /* The IP address being requested. */\r
886         dhcpIPv4_SERVER_IP_ADDRESS_OPTION_CODE, 4, 0, 0, 0, 0,                          /* The IP address of the DHCP server. */\r
887         dhcpOPTION_END_BYTE\r
888 };\r
889 size_t xOptionsLength = sizeof( ucDHCPRequestOptions );\r
890 \r
891         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, ( uint8_t ) dhcpREQUEST_OPCODE, ucDHCPRequestOptions, &xOptionsLength );\r
892 \r
893         /* Copy in the IP address being requested. */\r
894         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpREQUESTED_IP_ADDRESS_OFFSET ] ),\r
895                 ( void * ) &( xDHCPData.ulOfferedIPAddress ), sizeof( xDHCPData.ulOfferedIPAddress ) );\r
896 \r
897         /* Copy in the address of the DHCP server being used. */\r
898         memcpy( ( void * ) &( pucUDPPayloadBuffer[ dhcpFIRST_OPTION_BYTE_OFFSET + dhcpDHCP_SERVER_IP_ADDRESS_OFFSET ] ),\r
899                 ( void * ) &( xDHCPData.ulDHCPServerAddress ), sizeof( xDHCPData.ulDHCPServerAddress ) );\r
900 \r
901         FreeRTOS_debug_printf( ( "vDHCPProcess: reply %lxip\n", FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );\r
902         iptraceSENDING_DHCP_REQUEST();\r
903 \r
904         if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
905         {\r
906                 /* The packet was not successfully queued for sending and must be\r
907                 returned to the stack. */\r
908                 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
909         }\r
910 }\r
911 /*-----------------------------------------------------------*/\r
912 \r
913 static void prvSendDHCPDiscover( void )\r
914 {\r
915 uint8_t *pucUDPPayloadBuffer;\r
916 struct freertos_sockaddr xAddress;\r
917 static const uint8_t ucDHCPDiscoverOptions[] =\r
918 {\r
919         /* Do not change the ordering without also changing dhcpCLIENT_IDENTIFIER_OFFSET. */\r
920         dhcpIPv4_MESSAGE_TYPE_OPTION_CODE, 1, dhcpMESSAGE_TYPE_DISCOVER,                                        /* Message type option. */\r
921         dhcpIPv4_CLIENT_IDENTIFIER_OPTION_CODE, 6, 0, 0, 0, 0, 0, 0,                                            /* Client identifier. */\r
922         dhcpIPv4_PARAMETER_REQUEST_OPTION_CODE, 3, dhcpIPv4_SUBNET_MASK_OPTION_CODE, dhcpIPv4_GATEWAY_OPTION_CODE, dhcpIPv4_DNS_SERVER_OPTIONS_CODE,    /* Parameter request option. */\r
923         dhcpOPTION_END_BYTE\r
924 };\r
925 size_t xOptionsLength = sizeof( ucDHCPDiscoverOptions );\r
926 \r
927         pucUDPPayloadBuffer = prvCreatePartDHCPMessage( &xAddress, ( uint8_t ) dhcpREQUEST_OPCODE, ucDHCPDiscoverOptions, &xOptionsLength );\r
928 \r
929         FreeRTOS_debug_printf( ( "vDHCPProcess: discover\n" ) );\r
930         iptraceSENDING_DHCP_DISCOVER();\r
931 \r
932         if( FreeRTOS_sendto( xDHCPData.xDHCPSocket, pucUDPPayloadBuffer, ( sizeof( DHCPMessage_t ) + xOptionsLength ), FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) == 0 )\r
933         {\r
934                 /* The packet was not successfully queued for sending and must be\r
935                 returned to the stack. */\r
936                 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
937         }\r
938 }\r
939 /*-----------------------------------------------------------*/\r
940 \r
941 #if( ipconfigDHCP_FALL_BACK_AUTO_IP != 0 )\r
942 \r
943         static void prvPrepareLinkLayerIPLookUp( void )\r
944         {\r
945         uint8_t ucLinkLayerIPAddress[ 2 ];\r
946 \r
947                 /* After DHCP has failed to answer, prepare everything to start\r
948                 trying-out LinkLayer IP-addresses, using the random method. */\r
949                 xDHCPData.xDHCPTxTime = xTaskGetTickCount();\r
950 \r
951                 ucLinkLayerIPAddress[ 0 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32() % 0xFDu );             /* get value 1..254 for IP-address 3rd byte of IP address to try. */\r
952                 ucLinkLayerIPAddress[ 1 ] = ( uint8_t )1 + ( uint8_t )( ipconfigRAND32() % 0xFDu );             /* get value 1..254 for IP-address 4th byte of IP address to try. */\r
953 \r
954                 xNetworkAddressing.ulGatewayAddress = FreeRTOS_htonl( 0xA9FE0203 );\r
955 \r
956                 /* prepare xDHCPData with data to test. */\r
957                 xDHCPData.ulOfferedIPAddress =\r
958                         FreeRTOS_inet_addr_quick( LINK_LAYER_ADDRESS_0, LINK_LAYER_ADDRESS_1, ucLinkLayerIPAddress[ 0 ], ucLinkLayerIPAddress[ 1 ] );\r
959 \r
960                 xDHCPData.ulLeaseTime = dhcpDEFAULT_LEASE_TIME;  /*  don't care about lease time. just put anything. */\r
961 \r
962                 xNetworkAddressing.ulNetMask =\r
963                         FreeRTOS_inet_addr_quick( LINK_LAYER_NETMASK_0, LINK_LAYER_NETMASK_1, LINK_LAYER_NETMASK_2, LINK_LAYER_NETMASK_3 );\r
964 \r
965                 /* DHCP completed.  The IP address can now be used, and the\r
966                 timer set to the lease timeout time. */\r
967                 *ipLOCAL_IP_ADDRESS_POINTER = xDHCPData.ulOfferedIPAddress;\r
968 \r
969                 /* Setting the 'local' broadcast address, something like 192.168.1.255' */\r
970                 xNetworkAddressing.ulBroadcastAddress = ( xDHCPData.ulOfferedIPAddress & xNetworkAddressing.ulNetMask ) |  ~xNetworkAddressing.ulNetMask;\r
971 \r
972                 /* Close socket to ensure packets don't queue on it. not needed anymore as DHCP failed. but still need timer for ARP testing. */\r
973                 vSocketClose( xDHCPData.xDHCPSocket );\r
974                 xDHCPData.xDHCPSocket = NULL;\r
975                 xDHCPData.xDHCPTxPeriod = pdMS_TO_TICKS( 3000ul + ( ipconfigRAND32() & 0x3fful ) ); /*  do ARP test every (3 + 0-1024mS) seconds. */\r
976 \r
977                 xARPHadIPClash = pdFALSE;          /* reset flag that shows if have ARP clash. */\r
978                 vARPSendGratuitous();\r
979         }\r
980 \r
981 #endif /* ipconfigDHCP_FALL_BACK_AUTO_IP */\r
982 /*-----------------------------------------------------------*/\r
983 \r
984 #endif /* ipconfigUSE_DHCP != 0 */\r
985 \r
986 \r