]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c
Added +TCP code to main repo.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / FreeRTOS_DNS.c
1 /*\r
2  * FreeRTOS+TCP Labs Build 160919 (C) 2016 Real Time Engineers ltd.\r
3  * Authors include Hein Tibosch and Richard Barry\r
4  *\r
5  *******************************************************************************\r
6  ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***\r
7  ***                                                                         ***\r
8  ***                                                                         ***\r
9  ***   FREERTOS+TCP IS STILL IN THE LAB (mainly because the FTP and HTTP     ***\r
10  ***   demos have a dependency on FreeRTOS+FAT, which is only in the Labs    ***\r
11  ***   download):                                                            ***\r
12  ***                                                                         ***\r
13  ***   FreeRTOS+TCP is functional and has been used in commercial products   ***\r
14  ***   for some time.  Be aware however that we are still refining its       ***\r
15  ***   design, the source code does not yet quite conform to the strict      ***\r
16  ***   coding and style standards mandated by Real Time Engineers ltd., and  ***\r
17  ***   the documentation and testing is not necessarily complete.            ***\r
18  ***                                                                         ***\r
19  ***   PLEASE REPORT EXPERIENCES USING THE SUPPORT RESOURCES FOUND ON THE    ***\r
20  ***   URL: http://www.FreeRTOS.org/contact  Active early adopters may, at   ***\r
21  ***   the sole discretion of Real Time Engineers Ltd., be offered versions  ***\r
22  ***   under a license other than that described below.                      ***\r
23  ***                                                                         ***\r
24  ***                                                                         ***\r
25  ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***\r
26  *******************************************************************************\r
27  *\r
28  * FreeRTOS+TCP can be used under two different free open source licenses.  The\r
29  * license that applies is dependent on the processor on which FreeRTOS+TCP is\r
30  * executed, as follows:\r
31  *\r
32  * If FreeRTOS+TCP is executed on one of the processors listed under the Special\r
33  * License Arrangements heading of the FreeRTOS+TCP license information web\r
34  * page, then it can be used under the terms of the FreeRTOS Open Source\r
35  * License.  If FreeRTOS+TCP is used on any other processor, then it can be used\r
36  * under the terms of the GNU General Public License V2.  Links to the relevant\r
37  * licenses follow:\r
38  *\r
39  * The FreeRTOS+TCP License Information Page: http://www.FreeRTOS.org/tcp_license\r
40  * The FreeRTOS Open Source License: http://www.FreeRTOS.org/license\r
41  * The GNU General Public License Version 2: http://www.FreeRTOS.org/gpl-2.0.txt\r
42  *\r
43  * FreeRTOS+TCP is distributed in the hope that it will be useful.  You cannot\r
44  * use FreeRTOS+TCP unless you agree that you use the software 'as is'.\r
45  * FreeRTOS+TCP is provided WITHOUT ANY WARRANTY; without even the implied\r
46  * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
47  * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they\r
48  * implied, expressed, or statutory.\r
49  *\r
50  * 1 tab == 4 spaces!\r
51  *\r
52  * http://www.FreeRTOS.org\r
53  * http://www.FreeRTOS.org/plus\r
54  * http://www.FreeRTOS.org/labs\r
55  *\r
56  */\r
57 \r
58 /* Standard includes. */\r
59 #include <stdint.h>\r
60 \r
61 /* FreeRTOS includes. */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 #include "queue.h"\r
65 #include "list.h"\r
66 #include "semphr.h"\r
67 \r
68 /* FreeRTOS+TCP includes. */\r
69 #include "FreeRTOS_IP.h"\r
70 #include "FreeRTOS_Sockets.h"\r
71 #include "FreeRTOS_IP_Private.h"\r
72 #include "FreeRTOS_UDP_IP.h"\r
73 #include "FreeRTOS_DNS.h"\r
74 #include "NetworkBufferManagement.h"\r
75 #include "NetworkInterface.h"\r
76 #include "IPTraceMacroDefaults.h"\r
77 \r
78 /* Exclude the entire file if DNS is not enabled. */\r
79 #if( ipconfigUSE_DNS != 0 )\r
80 \r
81 #if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )\r
82         #define dnsDNS_PORT                                             0x3500u\r
83         #define dnsONE_QUESTION                                 0x0100u\r
84         #define dnsOUTGOING_FLAGS                               0x0001u /* Standard query. */\r
85         #define dnsRX_FLAGS_MASK                                0x0f80u /* The bits of interest in the flags field of incoming DNS messages. */\r
86         #define dnsEXPECTED_RX_FLAGS                    0x0080u /* Should be a response, without any errors. */\r
87 #else\r
88         #define dnsDNS_PORT                                             0x0035u\r
89         #define dnsONE_QUESTION                                 0x0001u\r
90         #define dnsOUTGOING_FLAGS                               0x0100u /* Standard query. */\r
91         #define dnsRX_FLAGS_MASK                                0x800fu /* The bits of interest in the flags field of incoming DNS messages. */\r
92         #define dnsEXPECTED_RX_FLAGS                    0x8000u /* Should be a response, without any errors. */\r
93 \r
94 #endif /* ipconfigBYTE_ORDER */\r
95 \r
96 /* The maximum number of times a DNS request should be sent out if a response\r
97 is not received, before giving up. */\r
98 #ifndef ipconfigDNS_REQUEST_ATTEMPTS\r
99         #define ipconfigDNS_REQUEST_ATTEMPTS            5\r
100 #endif\r
101 \r
102 /* If the top two bits in the first character of a name field are set then the\r
103 name field is an offset to the string, rather than the string itself. */\r
104 #define dnsNAME_IS_OFFSET                                       ( ( uint8_t ) 0xc0 )\r
105 \r
106 /* NBNS flags. */\r
107 #define dnsNBNS_FLAGS_RESPONSE                          0x8000u\r
108 #define dnsNBNS_FLAGS_OPCODE_MASK                       0x7800u\r
109 #define dnsNBNS_FLAGS_OPCODE_QUERY                      0x0000u\r
110 #define dnsNBNS_FLAGS_OPCODE_REGISTRATION       0x2800u\r
111 \r
112 /* Host types. */\r
113 #define dnsTYPE_A_HOST                                          0x0001u\r
114 #define dnsCLASS_IN                                                     0x0001u\r
115 \r
116 /* LLMNR constants. */\r
117 #define dnsLLMNR_TTL_VALUE                                      300000u\r
118 #define dnsLLMNR_FLAGS_IS_REPONSE                       0x8000u\r
119 \r
120 /* NBNS constants. */\r
121 #define dnsNBNS_TTL_VALUE                                       3600u /* 1 hour valid */\r
122 #define dnsNBNS_TYPE_NET_BIOS                           0x0020u\r
123 #define dnsNBNS_CLASS_IN                                        0x0001u\r
124 #define dnsNBNS_NAME_FLAGS                                      0x6000u\r
125 #define dnsNBNS_ENCODED_NAME_LENGTH                     32\r
126 \r
127 /* If the queried NBNS name matches with the device's name,\r
128 the query will be responded to with these flags: */\r
129 #define dnsNBNS_QUERY_RESPONSE_FLAGS            0x8500u\r
130 \r
131 /*\r
132  * Create a socket and bind it to the standard DNS port number.  Return the\r
133  * the created socket - or NULL if the socket could not be created or bound.\r
134  */\r
135 static Socket_t prvCreateDNSSocket( void );\r
136 \r
137 /*\r
138  * Create the DNS message in the zero copy buffer passed in the first parameter.\r
139  */\r
140 static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier );\r
141 \r
142 /*\r
143  * Simple routine that jumps over the NAME field of a resource record.\r
144  */\r
145 static uint8_t *prvSkipNameField( uint8_t *pucByte );\r
146 \r
147 /*\r
148  * Process a response packet from a DNS server.\r
149  */\r
150 static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, TickType_t xIdentifier );\r
151 \r
152 /*\r
153  * Prepare and send a message to a DNS server.  'xReadTimeOut_ms' will be passed as\r
154  * zero, in case the user has supplied a call-back function.\r
155  */\r
156 static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms );\r
157 \r
158 /*\r
159  * The NBNS and the LLMNR protocol share this reply function.\r
160  */\r
161 #if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )\r
162         static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength );\r
163 #endif\r
164 \r
165 #if( ipconfigUSE_NBNS == 1 )\r
166         static portINLINE void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, uint32_t ulIPAddress );\r
167 #endif /* ipconfigUSE_NBNS */\r
168 \r
169 #if( ipconfigUSE_DNS_CACHE == 1 )\r
170         static uint8_t *prvReadNameField( uint8_t *pucByte, char *pcName, BaseType_t xLen );\r
171         static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, BaseType_t xLookUp );\r
172 \r
173         typedef struct xDNS_CACHE_TABLE_ROW\r
174         {\r
175                 uint32_t ulIPAddress;           /* The IP address of an ARP cache entry. */\r
176                 char pcName[ipconfigDNS_CACHE_NAME_LENGTH];  /* The name of the host */\r
177                 uint8_t ucAge;                          /* A value that is periodically decremented but can also be refreshed by active communication.  The ARP cache entry is removed if the value reaches zero. */\r
178         } DNSCacheRow_t;\r
179 \r
180         static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];\r
181 #endif /* ipconfigUSE_DNS_CACHE == 1 */\r
182 \r
183 #if( ipconfigUSE_LLMNR == 1 )\r
184         const MACAddress_t xLLMNR_MacAdress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };\r
185 #endif  /* ipconfigUSE_LLMNR == 1 */\r
186 \r
187 /*-----------------------------------------------------------*/\r
188 \r
189 #include "pack_struct_start.h"\r
190 struct xDNSMessage\r
191 {\r
192         uint16_t usIdentifier;\r
193         uint16_t usFlags;\r
194         uint16_t usQuestions;\r
195         uint16_t usAnswers;\r
196         uint16_t usAuthorityRRs;\r
197         uint16_t usAdditionalRRs;\r
198 }\r
199 #include "pack_struct_end.h"\r
200 typedef struct xDNSMessage DNSMessage_t;\r
201 \r
202 /* A DNS query consists of a header, as described in 'struct xDNSMessage'\r
203 It is followed by 1 or more queries, each one consisting of a name and a tail,\r
204 with two fields: type and class\r
205 */\r
206 #include "pack_struct_start.h"\r
207 struct xDNSTail\r
208 {\r
209         uint16_t usType;\r
210         uint16_t usClass;\r
211 }\r
212 #include "pack_struct_end.h"\r
213 typedef struct xDNSTail DNSTail_t;\r
214 \r
215 #if( ipconfigUSE_LLMNR == 1 )\r
216 \r
217         #include "pack_struct_start.h"\r
218         struct xLLMNRAnswer\r
219         {\r
220                 uint8_t ucNameCode;\r
221                 uint8_t ucNameOffset;   /* The name is not repeated in the answer, only the offset is given with "0xc0 <offs>" */\r
222                 uint16_t usType;\r
223                 uint16_t usClass;\r
224                 uint32_t ulTTL;\r
225                 uint16_t usDataLength;\r
226                 uint32_t ulIPAddress;\r
227         }\r
228         #include "pack_struct_end.h"\r
229         typedef struct xLLMNRAnswer LLMNRAnswer_t;\r
230 \r
231 #endif /* ipconfigUSE_LLMNR == 1 */\r
232 \r
233 #if( ipconfigUSE_NBNS == 1 )\r
234 \r
235         #include "pack_struct_start.h"\r
236         struct xNBNSRequest\r
237         {\r
238                 uint16_t usRequestId;\r
239                 uint16_t usFlags;\r
240                 uint16_t ulRequestCount;\r
241                 uint16_t usAnswerRSS;\r
242                 uint16_t usAuthRSS;\r
243                 uint16_t usAdditionalRSS;\r
244                 uint8_t ucNameSpace;\r
245                 uint8_t ucName[ dnsNBNS_ENCODED_NAME_LENGTH ];\r
246                 uint8_t ucNameZero;\r
247                 uint16_t usType;\r
248                 uint16_t usClass;\r
249         }\r
250         #include "pack_struct_end.h"\r
251         typedef struct xNBNSRequest NBNSRequest_t;\r
252 \r
253         #include "pack_struct_start.h"\r
254         struct xNBNSAnswer\r
255         {\r
256                 uint16_t usType;\r
257                 uint16_t usClass;\r
258                 uint32_t ulTTL;\r
259                 uint16_t usDataLength;\r
260                 uint16_t usNbFlags;             /* NetBIOS flags 0x6000 : IP-address, big-endian */\r
261                 uint32_t ulIPAddress;\r
262         }\r
263         #include "pack_struct_end.h"\r
264         typedef struct xNBNSAnswer NBNSAnswer_t;\r
265 \r
266 #endif /* ipconfigUSE_NBNS == 1 */\r
267 \r
268 /*-----------------------------------------------------------*/\r
269 \r
270 #if( ipconfigUSE_DNS_CACHE == 1 )\r
271         uint32_t FreeRTOS_dnslookup( const char *pcHostName )\r
272         {\r
273         uint32_t ulIPAddress = 0UL;\r
274                 prvProcessDNSCache( pcHostName, &ulIPAddress, pdTRUE );\r
275                 return ulIPAddress;\r
276         }\r
277 #endif /* ipconfigUSE_DNS_CACHE == 1 */\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 #if( ipconfigDNS_USE_CALLBACKS != 0 )\r
281 \r
282         typedef struct xDNS_Callback {\r
283                 TickType_t xRemaningTime;               /* Timeout in ms */\r
284                 FOnDNSEvent pCallbackFunction;  /* Function to be called when the address has been found or when a timeout has beeen reached */\r
285                 TimeOut_t xTimeoutState;\r
286                 void *pvSearchID;\r
287                 struct xLIST_ITEM xListItem;\r
288                 char pcName[ 1 ];\r
289         } DNSCallback_t;\r
290 \r
291         static List_t xCallbackList;\r
292 \r
293         /* Define FreeRTOS_gethostbyname() as a normal blocking call. */\r
294         uint32_t FreeRTOS_gethostbyname( const char *pcHostName )\r
295         {\r
296                 return FreeRTOS_gethostbyname_a( pcHostName, ( FOnDNSEvent ) NULL, ( void* )NULL, 0 );\r
297         }\r
298         /*-----------------------------------------------------------*/\r
299 \r
300         /* Initialise the list of call-back structures. */\r
301         void vDNSInitialise( void );\r
302         void vDNSInitialise( void )\r
303         {\r
304                 vListInitialise( &xCallbackList );\r
305         }\r
306         /*-----------------------------------------------------------*/\r
307 \r
308         /* Iterate through the list of call-back structures and remove\r
309         old entries which have reached a timeout.\r
310         As soon as the list hase become empty, the DNS timer will be stopped\r
311         In case pvSearchID is supplied, the user wants to cancel a DNS request\r
312         */\r
313         void vDNSCheckCallBack( void *pvSearchID );\r
314         void vDNSCheckCallBack( void *pvSearchID )\r
315         {\r
316         const ListItem_t *pxIterator;\r
317         const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );\r
318 \r
319                 vTaskSuspendAll();\r
320                 {\r
321                         for( pxIterator  = ( const ListItem_t * ) listGET_NEXT( xEnd );\r
322                                  pxIterator != ( const ListItem_t * ) xEnd;\r
323                                   )\r
324                         {\r
325                                 DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );\r
326                                 /* Move to the next item because we might remove this item */\r
327                                 pxIterator  = ( const ListItem_t * ) listGET_NEXT( pxIterator );\r
328                                 if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )\r
329                                 {\r
330                                         uxListRemove( &pxCallback->xListItem );\r
331                                         vPortFree( pxCallback );\r
332                                 }\r
333                                 else if( xTaskCheckForTimeOut( &pxCallback->xTimeoutState, &pxCallback->xRemaningTime ) != pdFALSE )\r
334                                 {\r
335                                         pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );\r
336                                         uxListRemove( &pxCallback->xListItem );\r
337                                         vPortFree( ( void * ) pxCallback );\r
338                                 }\r
339                         }\r
340                 }\r
341                 xTaskResumeAll();\r
342 \r
343                 if( listLIST_IS_EMPTY( &xCallbackList ) )\r
344                 {\r
345                         vIPSetDnsTimerEnableState( pdFALSE );\r
346                 }\r
347         }\r
348         /*-----------------------------------------------------------*/\r
349 \r
350         void FreeRTOS_gethostbyname_cancel( void *pvSearchID )\r
351         {\r
352                 /* _HT_ Should better become a new API call to have the IP-task remove the callback */\r
353                 vDNSCheckCallBack( pvSearchID );\r
354         }\r
355         /*-----------------------------------------------------------*/\r
356 \r
357         /* FreeRTOS_gethostbyname_a() was called along with callback parameters.\r
358         Store them in a list for later reference. */\r
359         static void vDNSSetCallBack( const char *pcHostName, void *pvSearchID, FOnDNSEvent pCallbackFunction, TickType_t xTimeout, TickType_t xIdentifier );\r
360         static void vDNSSetCallBack( const char *pcHostName, void *pvSearchID, FOnDNSEvent pCallbackFunction, TickType_t xTimeout, TickType_t xIdentifier )\r
361         {\r
362                 size_t lLength = strlen( pcHostName );\r
363                 DNSCallback_t *pxCallback = ( DNSCallback_t * )pvPortMalloc( sizeof( *pxCallback ) + lLength );\r
364 \r
365                 /* Translate from ms to number of clock ticks. */\r
366                 xTimeout /= portTICK_PERIOD_MS;\r
367                 if( pxCallback != NULL )\r
368                 {\r
369                         if( listLIST_IS_EMPTY( &xCallbackList ) )\r
370                         {\r
371                                 /* This is the first one, start the DNS timer to check for timeouts */\r
372                                 vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, xTimeout ) );\r
373                         }\r
374                         strcpy( pxCallback->pcName, pcHostName );\r
375                         pxCallback->pCallbackFunction = pCallbackFunction;\r
376                         pxCallback->pvSearchID = pvSearchID;\r
377                         pxCallback->xRemaningTime = xTimeout;\r
378                         vTaskSetTimeOutState( &pxCallback->xTimeoutState );\r
379                         listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void* ) pxCallback );\r
380                         listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), xIdentifier );\r
381                         vTaskSuspendAll();\r
382                         {\r
383                                 vListInsertEnd( &xCallbackList, &pxCallback->xListItem );\r
384                         }\r
385                         xTaskResumeAll();\r
386                 }\r
387         }\r
388         /*-----------------------------------------------------------*/\r
389 \r
390         /* A DNS reply was received, see if there is any matching entry and\r
391         call the handler. */\r
392         static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress );\r
393         static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress )\r
394         {\r
395                 const ListItem_t *pxIterator;\r
396                 const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );\r
397 \r
398                 vTaskSuspendAll();\r
399                 {\r
400                         for( pxIterator  = ( const ListItem_t * ) listGET_NEXT( xEnd );\r
401                                  pxIterator != ( const ListItem_t * ) xEnd;\r
402                                  pxIterator  = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )\r
403                         {\r
404                                 if( listGET_LIST_ITEM_VALUE( pxIterator ) == xIdentifier )\r
405                                 {\r
406                                         DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );\r
407                                         pxCallback->pCallbackFunction( pcName, pxCallback->pvSearchID, ulIPAddress );\r
408                                         uxListRemove( &pxCallback->xListItem );\r
409                                         vPortFree( pxCallback );\r
410                                         if( listLIST_IS_EMPTY( &xCallbackList ) )\r
411                                         {\r
412                                                 vIPSetDnsTimerEnableState( pdFALSE );\r
413                                         }\r
414                                         break;\r
415                                 }\r
416                         }\r
417                 }\r
418                 xTaskResumeAll();\r
419         }\r
420 \r
421 #endif  /* ipconfigDNS_USE_CALLBACKS != 0 */\r
422 /*-----------------------------------------------------------*/\r
423 \r
424 #if( ipconfigDNS_USE_CALLBACKS == 0 )\r
425 uint32_t FreeRTOS_gethostbyname( const char *pcHostName )\r
426 #else\r
427 uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName, FOnDNSEvent pCallback, void *pvSearchID, TickType_t xTimeout )\r
428 #endif\r
429 {\r
430 uint32_t ulIPAddress = 0UL;\r
431 static uint16_t usIdentifier = 0u;\r
432 TickType_t xReadTimeOut_ms = 1200U;\r
433 /* Generate a unique identifier for this query. Keep it in a local variable\r
434  as gethostbyname() may be called from different threads */\r
435 TickType_t xIdentifier = ( TickType_t )usIdentifier++;\r
436 \r
437         /* If a DNS cache is used then check the cache before issuing another DNS\r
438         request. */\r
439         #if( ipconfigUSE_DNS_CACHE == 1 )\r
440         {\r
441                 ulIPAddress = FreeRTOS_dnslookup( pcHostName );\r
442                 if( ulIPAddress != 0 )\r
443                 {\r
444                         FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );\r
445                 }\r
446                 else\r
447                 {\r
448                         /* prvGetHostByName will be called to start a DNS lookup. */\r
449                 }\r
450         }\r
451         #endif /* ipconfigUSE_DNS_CACHE == 1 */\r
452 \r
453         #if( ipconfigDNS_USE_CALLBACKS != 0 )\r
454         {\r
455                 if( pCallback != NULL )\r
456                 {\r
457                         if( ulIPAddress == 0UL )\r
458                         {\r
459                                 /* The user has provided a callback function, so do not block on recvfrom() */\r
460                                 xReadTimeOut_ms  = 0;\r
461                                 vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t ) xIdentifier );\r
462                         }\r
463                         else\r
464                         {\r
465                                 /* The IP address is known, do the call-back now. */\r
466                                 pCallback( pcHostName, pvSearchID, ulIPAddress );\r
467                         }\r
468                 }\r
469         }\r
470         #endif\r
471 \r
472         if( ulIPAddress == 0UL)\r
473         {\r
474                 ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms );\r
475         }\r
476 \r
477         return ulIPAddress;\r
478 }\r
479 /*-----------------------------------------------------------*/\r
480 \r
481 static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms )\r
482 {\r
483 struct freertos_sockaddr xAddress;\r
484 Socket_t xDNSSocket;\r
485 uint32_t ulIPAddress = 0UL;\r
486 uint8_t *pucUDPPayloadBuffer;\r
487 static uint32_t ulAddressLength;\r
488 BaseType_t xAttempt;\r
489 int32_t lBytes;\r
490 size_t xPayloadLength, xExpectedPayloadLength;\r
491 TickType_t xWriteTimeOut_ms = 100U;\r
492 \r
493 #if( ipconfigUSE_LLMNR == 1 )\r
494         BaseType_t bHasDot = pdFALSE;\r
495 #endif /* ipconfigUSE_LLMNR == 1 */\r
496 \r
497         /* If LLMNR is being used then determine if the host name includes a '.' -\r
498         if not then LLMNR can be used as the lookup method. */\r
499         #if( ipconfigUSE_LLMNR == 1 )\r
500         {\r
501                 const char *pucPtr;\r
502                 for( pucPtr = pcHostName; *pucPtr; pucPtr++ )\r
503                 {\r
504                         if( *pucPtr == '.' )\r
505                         {\r
506                                 bHasDot = pdTRUE;\r
507                                 break;\r
508                         }\r
509                 }\r
510         }\r
511         #endif /* ipconfigUSE_LLMNR == 1 */\r
512 \r
513         /* Two is added at the end for the count of characters in the first\r
514         subdomain part and the string end byte. */\r
515         xExpectedPayloadLength = sizeof( DNSMessage_t ) + strlen( pcHostName ) + sizeof( uint16_t ) + sizeof( uint16_t ) + 2u;\r
516 \r
517         xDNSSocket = prvCreateDNSSocket();\r
518 \r
519         if( xDNSSocket != NULL )\r
520         {\r
521                 FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xWriteTimeOut_ms, sizeof( TickType_t ) );\r
522                 FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xReadTimeOut_ms,  sizeof( TickType_t ) );\r
523 \r
524                 for( xAttempt = 0; xAttempt < ipconfigDNS_REQUEST_ATTEMPTS; xAttempt++ )\r
525                 {\r
526                         /* Get a buffer.  This uses a maximum delay, but the delay will be\r
527                         capped to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value\r
528                         still needs to be tested. */\r
529                         pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xExpectedPayloadLength, portMAX_DELAY );\r
530 \r
531                         if( pucUDPPayloadBuffer != NULL )\r
532                         {\r
533                                 /* Create the message in the obtained buffer. */\r
534                                 xPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, xIdentifier );\r
535 \r
536                                 iptraceSENDING_DNS_REQUEST();\r
537 \r
538                                 /* Obtain the DNS server address. */\r
539                                 FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulIPAddress );\r
540 \r
541                                 /* Send the DNS message. */\r
542 #if( ipconfigUSE_LLMNR == 1 )\r
543                                 if( bHasDot == pdFALSE )\r
544                                 {\r
545                                         /* Use LLMNR addressing. */\r
546                                         ( ( DNSMessage_t * ) pucUDPPayloadBuffer) -> usFlags = 0;\r
547                                         xAddress.sin_addr = ipLLMNR_IP_ADDR;    /* Is in network byte order. */\r
548                                         xAddress.sin_port = FreeRTOS_ntohs( ipLLMNR_PORT );\r
549                                 }\r
550                                 else\r
551 #endif\r
552                                 {\r
553                                         /* Use DNS server. */\r
554                                         xAddress.sin_addr = ulIPAddress;\r
555                                         xAddress.sin_port = dnsDNS_PORT;\r
556                                 }\r
557 \r
558                                 ulIPAddress = 0UL;\r
559 \r
560                                 if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, xPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )\r
561                                 {\r
562                                         /* Wait for the reply. */\r
563                                         lBytes = FreeRTOS_recvfrom( xDNSSocket, &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xAddress, &ulAddressLength );\r
564 \r
565                                         if( lBytes > 0 )\r
566                                         {\r
567                                                 /* The reply was received.  Process it. */\r
568                                                 ulIPAddress = prvParseDNSReply( pucUDPPayloadBuffer, xIdentifier );\r
569 \r
570                                                 /* Finished with the buffer.  The zero copy interface\r
571                                                 is being used, so the buffer must be freed by the\r
572                                                 task. */\r
573                                                 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
574 \r
575                                                 if( ulIPAddress != 0UL )\r
576                                                 {\r
577                                                         /* All done. */\r
578                                                         break;\r
579                                                 }\r
580                                         }\r
581                                 }\r
582                                 else\r
583                                 {\r
584                                         /* The message was not sent so the stack will not be\r
585                                         releasing the zero copy - it must be released here. */\r
586                                         FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
587                                 }\r
588                         }\r
589                 }\r
590 \r
591                 /* Finished with the socket. */\r
592                 FreeRTOS_closesocket( xDNSSocket );\r
593         }\r
594 \r
595         return ulIPAddress;\r
596 }\r
597 /*-----------------------------------------------------------*/\r
598 \r
599 static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier )\r
600 {\r
601 DNSMessage_t *pxDNSMessageHeader;\r
602 uint8_t *pucStart, *pucByte;\r
603 DNSTail_t *pxTail;\r
604 static const DNSMessage_t xDefaultPartDNSHeader =\r
605 {\r
606         0,                                      /* The identifier will be overwritten. */\r
607         dnsOUTGOING_FLAGS,      /* Flags set for standard query. */\r
608         dnsONE_QUESTION,        /* One question is being asked. */\r
609         0,                                      /* No replies are included. */\r
610         0,                                      /* No authorities. */\r
611         0                                       /* No additional authorities. */\r
612 };\r
613 \r
614         /* Copy in the const part of the header. */\r
615         memcpy( ( void * ) pucUDPPayloadBuffer, ( void * ) &xDefaultPartDNSHeader, sizeof( xDefaultPartDNSHeader ) );\r
616 \r
617         /* Write in a unique identifier. */\r
618         pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
619         pxDNSMessageHeader->usIdentifier = ( uint16_t ) xIdentifier;\r
620 \r
621         /* Create the resource record at the end of the header.  First\r
622         find the end of the header. */\r
623         pucStart = pucUDPPayloadBuffer + sizeof( xDefaultPartDNSHeader );\r
624 \r
625         /* Leave a gap for the first length bytes. */\r
626         pucByte = pucStart + 1;\r
627 \r
628         /* Copy in the host name. */\r
629         strcpy( ( char * ) pucByte, pcHostName );\r
630 \r
631         /* Mark the end of the string. */\r
632         pucByte += strlen( pcHostName );\r
633         *pucByte = 0x00u;\r
634 \r
635         /* Walk the string to replace the '.' characters with byte counts.\r
636         pucStart holds the address of the byte count.  Walking the string\r
637         starts after the byte count position. */\r
638         pucByte = pucStart;\r
639 \r
640         do\r
641         {\r
642                 pucByte++;\r
643 \r
644                 while( ( *pucByte != 0x00 ) && ( *pucByte != '.' ) )\r
645                 {\r
646                         pucByte++;\r
647                 }\r
648 \r
649                 /* Fill in the byte count, then move the pucStart pointer up to\r
650                 the found byte position. */\r
651                 *pucStart = ( uint8_t ) ( ( uint32_t ) pucByte - ( uint32_t ) pucStart );\r
652                 ( *pucStart )--;\r
653 \r
654                 pucStart = pucByte;\r
655 \r
656         } while( *pucByte != 0x00 );\r
657 \r
658         /* Finish off the record. */\r
659 \r
660         pxTail = (DNSTail_t *)( pucByte + 1 );\r
661 \r
662         vSetField16( pxTail, DNSTail_t, usType, dnsTYPE_A_HOST );       /* Type A: host */\r
663         vSetField16( pxTail, DNSTail_t, usClass, dnsCLASS_IN ); /* 1: Class IN */\r
664 \r
665         /* Return the total size of the generated message, which is the space from\r
666         the last written byte to the beginning of the buffer. */\r
667         return ( ( uint32_t ) pucByte - ( uint32_t ) pucUDPPayloadBuffer + 1 ) + sizeof( *pxTail );\r
668 }\r
669 /*-----------------------------------------------------------*/\r
670 \r
671 #if( ipconfigUSE_DNS_CACHE == 1 )\r
672 \r
673         static uint8_t *prvReadNameField( uint8_t *pucByte, char *pcName, BaseType_t xLen )\r
674         {\r
675         BaseType_t xNameLen = 0;\r
676                 /* Determine if the name is the fully coded name, or an offset to the name\r
677                 elsewhere in the message. */\r
678                 if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )\r
679                 {\r
680                         /* Jump over the two byte offset. */\r
681                         pucByte += sizeof( uint16_t );\r
682 \r
683                 }\r
684                 else\r
685                 {\r
686                         /* pucByte points to the full name.  Walk over the string. */\r
687                         while( *pucByte != 0x00 )\r
688                         {\r
689                                 BaseType_t xCount;\r
690                                 if( xNameLen && xNameLen < xLen - 1 )\r
691                                         pcName[xNameLen++] = '.';\r
692                                 for( xCount = *(pucByte++); xCount--; pucByte++ )\r
693                                 {\r
694                                         if( xNameLen < xLen - 1 )\r
695                                                 pcName[xNameLen++] = *( ( char * ) pucByte );\r
696                                 }\r
697                         }\r
698 \r
699                         pucByte++;\r
700                 }\r
701 \r
702                 return pucByte;\r
703         }\r
704 #endif  /* ipconfigUSE_DNS_CACHE == 1 */\r
705 /*-----------------------------------------------------------*/\r
706 \r
707 static uint8_t *prvSkipNameField( uint8_t *pucByte )\r
708 {\r
709         /* Determine if the name is the fully coded name, or an offset to the name\r
710         elsewhere in the message. */\r
711         if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )\r
712         {\r
713                 /* Jump over the two byte offset. */\r
714                 pucByte += sizeof( uint16_t );\r
715 \r
716         }\r
717         else\r
718         {\r
719                 /* pucByte points to the full name.  Walk over the string. */\r
720                 while( *pucByte != 0x00 )\r
721                 {\r
722                         /* The number of bytes to jump for each name section is stored in the byte\r
723                         before the name section. */\r
724                         pucByte += ( *pucByte + 1 );\r
725                 }\r
726 \r
727                 pucByte++;\r
728         }\r
729 \r
730         return pucByte;\r
731 }\r
732 /*-----------------------------------------------------------*/\r
733 \r
734 uint32_t ulDNSHandlePacket( NetworkBufferDescriptor_t *pxNetworkBuffer )\r
735 {\r
736 uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );\r
737 DNSMessage_t *pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
738 \r
739         prvParseDNSReply( pucUDPPayloadBuffer, ( uint32_t ) pxDNSMessageHeader->usIdentifier );\r
740 \r
741         /* The packet was not consumed. */\r
742         return pdFAIL;\r
743 }\r
744 /*-----------------------------------------------------------*/\r
745 \r
746 #if( ipconfigUSE_NBNS == 1 )\r
747 \r
748         uint32_t ulNBNSHandlePacket (NetworkBufferDescriptor_t *pxNetworkBuffer )\r
749         {\r
750         UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;\r
751         uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( *pxUDPPacket );\r
752 \r
753                 prvTreatNBNS( pucUDPPayloadBuffer, pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
754 \r
755                 /* The packet was not consumed. */\r
756                 return pdFAIL;\r
757         }\r
758 \r
759 #endif /* ipconfigUSE_NBNS */\r
760 /*-----------------------------------------------------------*/\r
761 \r
762 static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, TickType_t xIdentifier )\r
763 {\r
764 DNSMessage_t *pxDNSMessageHeader;\r
765 uint32_t ulIPAddress = 0UL;\r
766 #if( ipconfigUSE_LLMNR == 1 )\r
767         char *pcRequestedName = NULL;\r
768 #endif\r
769 uint8_t *pucByte;\r
770 uint16_t x, usDataLength, usQuestions;\r
771 #if( ipconfigUSE_LLMNR == 1 )\r
772         uint16_t usType = 0, usClass = 0;\r
773 #endif\r
774 #if( ipconfigUSE_DNS_CACHE == 1 )\r
775         char pcName[128] = ""; /*_RB_ What is the significance of 128?  Probably too big to go on the stack for a small MCU but don't know how else it could be made re-entrant.  Might be necessary. */\r
776 #endif\r
777 \r
778         pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
779 \r
780         if( pxDNSMessageHeader->usIdentifier == ( uint16_t ) xIdentifier )\r
781         {\r
782                 /* Start at the first byte after the header. */\r
783                 pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t );\r
784 \r
785                 /* Skip any question records. */\r
786                 usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );\r
787                 for( x = 0; x < usQuestions; x++ )\r
788                 {\r
789                         #if( ipconfigUSE_LLMNR == 1 )\r
790                         {\r
791                                 if( x == 0 )\r
792                                 {\r
793                                         pcRequestedName = ( char * ) pucByte;\r
794                                 }\r
795                         }\r
796                         #endif\r
797 \r
798 #if( ipconfigUSE_DNS_CACHE == 1 )\r
799                         if( x == 0 )\r
800                         {\r
801                                 pucByte = prvReadNameField( pucByte, pcName, sizeof( pcName ) );\r
802                         }\r
803                         else\r
804 #endif /* ipconfigUSE_DNS_CACHE */\r
805                         {\r
806                                 /* Skip the variable length pcName field. */\r
807                                 pucByte = prvSkipNameField( pucByte );\r
808                         }\r
809 \r
810                         #if( ipconfigUSE_LLMNR == 1 )\r
811                         {\r
812                                 /* usChar2u16 returns value in host endianness. */\r
813                                 usType = usChar2u16( pucByte );\r
814                                 usClass = usChar2u16( pucByte + 2 );\r
815                         }\r
816                         #endif /* ipconfigUSE_LLMNR */\r
817 \r
818                         /* Skip the type and class fields. */\r
819                         pucByte += sizeof( uint32_t );\r
820                 }\r
821 \r
822                 /* Search through the answers records. */\r
823                 pxDNSMessageHeader->usAnswers = FreeRTOS_ntohs( pxDNSMessageHeader->usAnswers );\r
824 \r
825                 if( ( pxDNSMessageHeader->usFlags & dnsRX_FLAGS_MASK ) == dnsEXPECTED_RX_FLAGS )\r
826                 {\r
827                         for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )\r
828                         {\r
829                                 pucByte = prvSkipNameField( pucByte );\r
830 \r
831                                 /* Is the type field that of an A record? */\r
832                                 if( usChar2u16( pucByte ) == dnsTYPE_A_HOST )\r
833                                 {\r
834                                         /* This is the required record.  Skip the type, class, and\r
835                                         time to live fields, plus the first byte of the data\r
836                                         length. */\r
837                                         pucByte += ( sizeof( uint32_t ) + sizeof( uint32_t ) + sizeof( uint8_t ) );\r
838 \r
839                                         /* Sanity check the data length. */\r
840                                         if( ( size_t ) *pucByte == sizeof( uint32_t ) )\r
841                                         {\r
842                                                 /* Skip the second byte of the length. */\r
843                                                 pucByte++;\r
844 \r
845                                                 /* Copy the IP address out of the record. */\r
846                                                 memcpy( ( void * ) &ulIPAddress, ( void * ) pucByte, sizeof( uint32_t ) );\r
847 \r
848                                                 #if( ipconfigUSE_DNS_CACHE == 1 )\r
849                                                 {\r
850                                                         prvProcessDNSCache( pcName, &ulIPAddress, pdFALSE );\r
851                                                 }\r
852                                                 #endif /* ipconfigUSE_DNS_CACHE */\r
853                                                 #if( ipconfigDNS_USE_CALLBACKS != 0 )\r
854                                                 {\r
855                                                         /* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */\r
856                                                         vDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress );\r
857                                                 }\r
858                                                 #endif  /* ipconfigDNS_USE_CALLBACKS != 0 */\r
859                                         }\r
860 \r
861                                         break;\r
862                                 }\r
863                                 else\r
864                                 {\r
865                                         /* Skip the type, class and time to live fields. */\r
866                                         pucByte += ( sizeof( uint32_t ) + sizeof( uint32_t ) );\r
867 \r
868                                         /* Determine the length of the data in the field. */\r
869                                         memcpy( ( void * ) &usDataLength, ( void * ) pucByte, sizeof( uint16_t ) );\r
870                                         usDataLength = FreeRTOS_ntohs( usDataLength );\r
871 \r
872                                         /* Jump over the data length bytes, and the data itself. */\r
873                                         pucByte += usDataLength + sizeof( uint16_t );\r
874                                 }\r
875                         }\r
876                 }\r
877 #if( ipconfigUSE_LLMNR == 1 )\r
878                 else if( ( usQuestions != ( uint16_t )0u ) && ( usType == ( uint16_t )dnsTYPE_A_HOST ) && ( usClass == ( uint16_t )dnsCLASS_IN ) )\r
879                 {\r
880                         /* If this is not a reply to our DNS request, it might an LLMNR\r
881                         request. */\r
882                         if( xApplicationDNSQueryHook ( ( pcRequestedName + 1 ) ) )\r
883                         {\r
884                         int16_t usLength;\r
885                         NetworkBufferDescriptor_t *pxNewBuffer = NULL;\r
886                         NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );\r
887                         LLMNRAnswer_t *pxAnswer;\r
888 \r
889                                 if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )\r
890                                 {\r
891                                 BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +\r
892                                         sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );\r
893 \r
894                                         /* The field xDataLength was set to the length of the UDP payload.\r
895                                         The answer (reply) will be longer than the request, so the packet\r
896                                         must be duplicaed into a bigger buffer */\r
897                                         pxNetworkBuffer->xDataLength = xDataLength;\r
898                                         pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );\r
899                                         if( pxNewBuffer != NULL )\r
900                                         {\r
901                                         BaseType_t xOffset1, xOffset2;\r
902 \r
903                                                 xOffset1 = ( BaseType_t ) ( pucByte - pucUDPPayloadBuffer );\r
904                                                 xOffset2 = ( BaseType_t ) ( ( ( uint8_t * ) pcRequestedName ) - pucUDPPayloadBuffer );\r
905 \r
906                                                 pxNetworkBuffer = pxNewBuffer;\r
907                                                 pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + ipUDP_PAYLOAD_OFFSET_IPv4;\r
908 \r
909                                                 pucByte = pucUDPPayloadBuffer + xOffset1;\r
910                                                 pcRequestedName = ( char * ) ( pucUDPPayloadBuffer + xOffset2 );\r
911                                                 pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
912 \r
913                                         }\r
914                                         else\r
915                                         {\r
916                                                 /* Just to indicate that the message may not be answered. */\r
917                                                 pxNetworkBuffer = NULL;\r
918                                         }\r
919                                 }\r
920                                 if( pxNetworkBuffer != NULL )\r
921                                 {\r
922                                         pxAnswer = (LLMNRAnswer_t *)pucByte;\r
923 \r
924                                         /* Leave 'usIdentifier' and 'usQuestions' untouched. */\r
925                                         vSetField16( pxDNSMessageHeader, DNSMessage_t, usFlags, dnsLLMNR_FLAGS_IS_REPONSE );    /* Set the response flag */\r
926                                         vSetField16( pxDNSMessageHeader, DNSMessage_t, usAnswers, 1 );  /* Provide a single answer */\r
927                                         vSetField16( pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 );     /* No authority */\r
928                                         vSetField16( pxDNSMessageHeader, DNSMessage_t, usAdditionalRRs, 0 );    /* No additional info */\r
929 \r
930                                         pxAnswer->ucNameCode = dnsNAME_IS_OFFSET;\r
931                                         pxAnswer->ucNameOffset = ( uint8_t )( pcRequestedName - ( char * ) pucUDPPayloadBuffer );\r
932 \r
933                                         vSetField16( pxAnswer, LLMNRAnswer_t, usType, dnsTYPE_A_HOST ); /* Type A: host */\r
934                                         vSetField16( pxAnswer, LLMNRAnswer_t, usClass, dnsCLASS_IN );   /* 1: Class IN */\r
935                                         vSetField32( pxAnswer, LLMNRAnswer_t, ulTTL, dnsLLMNR_TTL_VALUE );\r
936                                         vSetField16( pxAnswer, LLMNRAnswer_t, usDataLength, 4 );\r
937                                         vSetField32( pxAnswer, LLMNRAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );\r
938 \r
939                                         usLength = ( int16_t ) ( sizeof( *pxAnswer ) + ( size_t ) ( pucByte - pucUDPPayloadBuffer ) );\r
940 \r
941                                         prvReplyDNSMessage( pxNetworkBuffer, usLength );\r
942 \r
943                                         if( pxNewBuffer != NULL )\r
944                                         {\r
945                                                 vReleaseNetworkBufferAndDescriptor( pxNewBuffer );\r
946                                         }\r
947                                 }\r
948                         }\r
949                 }\r
950 #endif /* ipconfigUSE_LLMNR == 1 */\r
951         }\r
952 \r
953         return ulIPAddress;\r
954 }\r
955 /*-----------------------------------------------------------*/\r
956 \r
957 #if( ipconfigUSE_NBNS == 1 )\r
958 \r
959         static void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, uint32_t ulIPAddress )\r
960         {\r
961                 uint16_t usFlags, usType, usClass;\r
962                 uint8_t *pucSource, *pucTarget;\r
963                 uint8_t ucByte;\r
964                 uint8_t ucNBNSName[ 17 ];\r
965 \r
966                 usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) );\r
967 \r
968                 if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )\r
969                 {\r
970                         usType  = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );\r
971                         usClass = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usClass ) );\r
972 \r
973                         /* Not used for now */\r
974                         ( void )usClass;\r
975                         /* For NBNS a name is 16 bytes long, written with capitals only.\r
976                         Make sure that the copy is terminated with a zero. */\r
977                         pucTarget = ucNBNSName + sizeof(ucNBNSName ) - 2;\r
978                         pucTarget[ 1 ] = '\0';\r
979 \r
980                         /* Start with decoding the last 2 bytes. */\r
981                         pucSource = pucUDPPayloadBuffer + ( offsetof( NBNSRequest_t, ucName ) + ( dnsNBNS_ENCODED_NAME_LENGTH - 2 ) );\r
982 \r
983                         for( ;; )\r
984                         {\r
985                                 ucByte = ( uint8_t ) ( ( ( pucSource[ 0 ] - 0x41 ) << 4 ) | ( pucSource[ 1 ] - 0x41 ) );\r
986 \r
987                                 /* Make sure there are no trailing spaces in the name. */\r
988                                 if( ( ucByte == ' ' ) && ( pucTarget[ 1 ] == '\0' ) )\r
989                                 {\r
990                                         ucByte = '\0';\r
991                                 }\r
992 \r
993                                 *pucTarget = ucByte;\r
994 \r
995                                 if( pucTarget == ucNBNSName )\r
996                                 {\r
997                                         break;\r
998                                 }\r
999 \r
1000                                 pucTarget -= 1;\r
1001                                 pucSource -= 2;\r
1002                         }\r
1003 \r
1004                         #if( ipconfigUSE_DNS_CACHE == 1 )\r
1005                         {\r
1006                                 if( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) != 0 )\r
1007                                 {\r
1008                                         /* If this is a response from another device,\r
1009                                         add the name to the DNS cache */\r
1010                                         prvProcessDNSCache( ( char * ) ucNBNSName, &ulIPAddress, pdFALSE );\r
1011                                 }\r
1012                         }\r
1013                         #else\r
1014                         {\r
1015                                 /* Avoid compiler warnings. */\r
1016                                 ( void ) ulIPAddress;\r
1017                         }\r
1018                         #endif /* ipconfigUSE_DNS_CACHE */\r
1019 \r
1020                         if( ( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) == 0 ) &&\r
1021                                 ( usType == dnsNBNS_TYPE_NET_BIOS ) &&\r
1022                                 ( xApplicationDNSQueryHook( ( const char * ) ucNBNSName ) != pdFALSE ) )\r
1023                         {\r
1024                         uint16_t usLength;\r
1025                         DNSMessage_t *pxMessage;\r
1026                         NBNSAnswer_t *pxAnswer;\r
1027 \r
1028                                 /* Someone is looking for a device with ucNBNSName,\r
1029                                 prepare a positive reply. */\r
1030                                 NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );\r
1031 \r
1032                                 if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )\r
1033                                 {\r
1034                                 NetworkBufferDescriptor_t *pxNewBuffer;\r
1035                                 BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +\r
1036 \r
1037                                         sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );\r
1038 \r
1039                                         /* The field xDataLength was set to the length of the UDP payload.\r
1040                                         The answer (reply) will be longer than the request, so the packet\r
1041                                         must be duplicated into a bigger buffer */\r
1042                                         pxNetworkBuffer->xDataLength = xDataLength;\r
1043                                         pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );\r
1044                                         if( pxNewBuffer != NULL )\r
1045                                         {\r
1046                                                 pucUDPPayloadBuffer = pxNewBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );\r
1047                                                 pxNetworkBuffer = pxNewBuffer;\r
1048                                         }\r
1049                                         else\r
1050                                         {\r
1051                                                 /* Just prevent that a reply will be sent */\r
1052                                                 pxNetworkBuffer = NULL;\r
1053                                         }\r
1054                                 }\r
1055 \r
1056                                 /* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */\r
1057                                 if( pxNetworkBuffer != NULL )\r
1058                                 {\r
1059                                         pxMessage = (DNSMessage_t *)pucUDPPayloadBuffer;\r
1060 \r
1061                                         /* As the fields in the structures are not word-aligned, we have to\r
1062                                         copy the values byte-by-byte using macro's vSetField16() and vSetField32() */\r
1063                                         vSetField16( pxMessage, DNSMessage_t, usFlags, dnsNBNS_QUERY_RESPONSE_FLAGS ); /* 0x8500 */\r
1064                                         vSetField16( pxMessage, DNSMessage_t, usQuestions, 0 );\r
1065                                         vSetField16( pxMessage, DNSMessage_t, usAnswers, 1 );\r
1066                                         vSetField16( pxMessage, DNSMessage_t, usAuthorityRRs, 0 );\r
1067                                         vSetField16( pxMessage, DNSMessage_t, usAdditionalRRs, 0 );\r
1068 \r
1069                                         pxAnswer = (NBNSAnswer_t *)( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );\r
1070 \r
1071                                         vSetField16( pxAnswer, NBNSAnswer_t, usType, usType );  /* Type */\r
1072                                         vSetField16( pxAnswer, NBNSAnswer_t, usClass, dnsNBNS_CLASS_IN );       /* Class */\r
1073                                         vSetField32( pxAnswer, NBNSAnswer_t, ulTTL, dnsNBNS_TTL_VALUE );\r
1074                                         vSetField16( pxAnswer, NBNSAnswer_t, usDataLength, 6 ); /* 6 bytes including the length field */\r
1075                                         vSetField16( pxAnswer, NBNSAnswer_t, usNbFlags, dnsNBNS_NAME_FLAGS );\r
1076                                         vSetField32( pxAnswer, NBNSAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );\r
1077 \r
1078                                         usLength = ( uint16_t ) ( offsetof( NBNSRequest_t, usType ) + sizeof( NBNSAnswer_t ) );\r
1079 \r
1080                                         prvReplyDNSMessage( pxNetworkBuffer, usLength );\r
1081                                 }\r
1082                         }\r
1083                 }\r
1084         }\r
1085 \r
1086 #endif  /* ipconfigUSE_NBNS */\r
1087 /*-----------------------------------------------------------*/\r
1088 \r
1089 static Socket_t prvCreateDNSSocket( void )\r
1090 {\r
1091 static Socket_t xSocket = NULL;\r
1092 struct freertos_sockaddr xAddress;\r
1093 BaseType_t xReturn;\r
1094 TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );\r
1095 \r
1096         /* This must be the first time this function has been called.  Create\r
1097         the socket. */\r
1098         xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
1099 \r
1100         /* Auto bind the port. */\r
1101         xAddress.sin_port = 0u;\r
1102         xReturn = FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );\r
1103 \r
1104         /* Check the bind was successful, and clean up if not. */\r
1105         if( xReturn != 0 )\r
1106         {\r
1107                 FreeRTOS_closesocket( xSocket );\r
1108                 xSocket = NULL;\r
1109         }\r
1110         else\r
1111         {\r
1112                 /* Set the send and receive timeouts. */\r
1113                 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );\r
1114                 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );\r
1115         }\r
1116 \r
1117         return xSocket;\r
1118 }\r
1119 /*-----------------------------------------------------------*/\r
1120 \r
1121 #if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )\r
1122 \r
1123         static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength )\r
1124         {\r
1125         UDPPacket_t *pxUDPPacket;\r
1126         IPHeader_t *pxIPHeader;\r
1127         UDPHeader_t *pxUDPHeader;\r
1128 \r
1129                 pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;\r
1130                 pxIPHeader = &pxUDPPacket->xIPHeader;\r
1131                 pxUDPHeader = &pxUDPPacket->xUDPHeader;\r
1132                 /* HT: started using defines like 'ipSIZE_OF_xxx' */\r
1133                 pxIPHeader->usLength               = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );\r
1134                 /* HT:endian: should not be translated, copying from packet to packet */\r
1135                 pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;\r
1136                 pxIPHeader->ulSourceIPAddress      = *ipLOCAL_IP_ADDRESS_POINTER;\r
1137                 pxIPHeader->ucTimeToLive           = ipconfigUDP_TIME_TO_LIVE;\r
1138                 pxIPHeader->usIdentification       = FreeRTOS_htons( usPacketIdentifier );\r
1139                 usPacketIdentifier++;\r
1140                 pxUDPHeader->usLength              = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );\r
1141                 vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort );\r
1142 \r
1143                 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )\r
1144                 {\r
1145                         /* calculate the IP header checksum */\r
1146                         pxIPHeader->usHeaderChecksum       = 0x00;\r
1147                         pxIPHeader->usHeaderChecksum       = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );\r
1148                         pxIPHeader->usHeaderChecksum       = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );\r
1149 \r
1150                         /* calculate the UDP checksum for outgoing package */\r
1151                         usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, pdTRUE );\r
1152                 }\r
1153                 #endif\r
1154 \r
1155                 /* Important: tell NIC driver how many bytes must be sent */\r
1156                 pxNetworkBuffer->xDataLength = ( size_t ) ( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER );\r
1157 \r
1158                 /* This function will fill in the eth addresses and send the packet */\r
1159                 vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );\r
1160         }\r
1161 \r
1162 #endif /* ipconfigUSE_NBNS == 1 || ipconfigUSE_LLMNR == 1 */\r
1163 /*-----------------------------------------------------------*/\r
1164 \r
1165 #if( ipconfigUSE_DNS_CACHE == 1 )\r
1166 \r
1167         static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, BaseType_t xLookUp )\r
1168         {\r
1169         BaseType_t x;\r
1170         BaseType_t xFound = pdFALSE;\r
1171         static BaseType_t xFreeEntry = 0;\r
1172 \r
1173                 /* For each entry in the DNS cache table. */\r
1174                 for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )\r
1175                 {\r
1176                         if( xDNSCache[ x ].pcName[ 0 ] == 0 )\r
1177                         {\r
1178                                 break;\r
1179                         }\r
1180 \r
1181                         if( strncmp( xDNSCache[ x ].pcName, pcName, sizeof( xDNSCache[ x ].pcName ) ) == 0 )\r
1182                         {\r
1183                                 /* Is this function called for a lookup or to add/update an IP address? */\r
1184                                 if( xLookUp != pdFALSE )\r
1185                                 {\r
1186                                         *pulIP = xDNSCache[ x ].ulIPAddress;\r
1187                                 }\r
1188                                 else\r
1189                                 {\r
1190                                         xDNSCache[ x ].ulIPAddress = *pulIP;\r
1191                                 }\r
1192 \r
1193                                 xFound = pdTRUE;\r
1194                                 break;\r
1195                         }\r
1196                 }\r
1197 \r
1198                 if( xFound == pdFALSE )\r
1199                 {\r
1200                         if( xLookUp != pdFALSE )\r
1201                         {\r
1202                                 *pulIP = 0;\r
1203                         }\r
1204                         else\r
1205                         {\r
1206                                 /* Called to add or update an item */\r
1207                                 strncpy( xDNSCache[ xFreeEntry ].pcName, pcName, sizeof( xDNSCache[ xFreeEntry ].pcName ) );\r
1208                                 xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP;\r
1209 \r
1210                                 xFreeEntry++;\r
1211                                 if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )\r
1212                                 {\r
1213                                         xFreeEntry = 0;\r
1214                                 }\r
1215                         }\r
1216                 }\r
1217 \r
1218                 if( ( xLookUp == 0 ) || ( *pulIP != 0 ) )\r
1219                 {\r
1220                         FreeRTOS_debug_printf( ( "prvProcessDNSCache: %s: '%s' @ %lxip\n", xLookUp ? "look-up" : "add", pcName, FreeRTOS_ntohl( *pulIP ) ) );\r
1221                 }\r
1222         }\r
1223 \r
1224 #endif /* ipconfigUSE_DNS_CACHE */\r
1225 \r
1226 #endif /* ipconfigUSE_DNS != 0 */\r
1227 \r
1228 \r