2 * FreeRTOS+TCP Labs Build 160919 (C) 2016 Real Time Engineers ltd.
\r
3 * Authors include Hein Tibosch and Richard Barry
\r
5 *******************************************************************************
\r
6 ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
\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
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
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
25 ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
\r
26 *******************************************************************************
\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
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
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
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
50 * 1 tab == 4 spaces!
\r
52 * http://www.FreeRTOS.org
\r
53 * http://www.FreeRTOS.org/plus
\r
54 * http://www.FreeRTOS.org/labs
\r
58 /* Standard includes. */
\r
61 /* FreeRTOS includes. */
\r
62 #include "FreeRTOS.h"
\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
78 /* Exclude the entire file if DNS is not enabled. */
\r
79 #if( ipconfigUSE_DNS != 0 )
\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
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
94 #endif /* ipconfigBYTE_ORDER */
\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
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
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
113 #define dnsTYPE_A_HOST 0x0001u
\r
114 #define dnsCLASS_IN 0x0001u
\r
116 /* LLMNR constants. */
\r
117 #define dnsLLMNR_TTL_VALUE 300000u
\r
118 #define dnsLLMNR_FLAGS_IS_REPONSE 0x8000u
\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
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
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
135 static Socket_t prvCreateDNSSocket( void );
\r
138 * Create the DNS message in the zero copy buffer passed in the first parameter.
\r
140 static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier );
\r
143 * Simple routine that jumps over the NAME field of a resource record.
\r
145 static uint8_t *prvSkipNameField( uint8_t *pucByte );
\r
148 * Process a response packet from a DNS server.
\r
150 static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, TickType_t xIdentifier );
\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
156 static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms );
\r
159 * The NBNS and the LLMNR protocol share this reply function.
\r
161 #if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
\r
162 static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength );
\r
165 #if( ipconfigUSE_NBNS == 1 )
\r
166 static portINLINE void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, uint32_t ulIPAddress );
\r
167 #endif /* ipconfigUSE_NBNS */
\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
173 typedef struct xDNS_CACHE_TABLE_ROW
\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
180 static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];
\r
181 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\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
187 /*-----------------------------------------------------------*/
\r
189 #include "pack_struct_start.h"
\r
192 uint16_t usIdentifier;
\r
194 uint16_t usQuestions;
\r
195 uint16_t usAnswers;
\r
196 uint16_t usAuthorityRRs;
\r
197 uint16_t usAdditionalRRs;
\r
199 #include "pack_struct_end.h"
\r
200 typedef struct xDNSMessage DNSMessage_t;
\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
206 #include "pack_struct_start.h"
\r
212 #include "pack_struct_end.h"
\r
213 typedef struct xDNSTail DNSTail_t;
\r
215 #if( ipconfigUSE_LLMNR == 1 )
\r
217 #include "pack_struct_start.h"
\r
218 struct xLLMNRAnswer
\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
225 uint16_t usDataLength;
\r
226 uint32_t ulIPAddress;
\r
228 #include "pack_struct_end.h"
\r
229 typedef struct xLLMNRAnswer LLMNRAnswer_t;
\r
231 #endif /* ipconfigUSE_LLMNR == 1 */
\r
233 #if( ipconfigUSE_NBNS == 1 )
\r
235 #include "pack_struct_start.h"
\r
236 struct xNBNSRequest
\r
238 uint16_t usRequestId;
\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
250 #include "pack_struct_end.h"
\r
251 typedef struct xNBNSRequest NBNSRequest_t;
\r
253 #include "pack_struct_start.h"
\r
259 uint16_t usDataLength;
\r
260 uint16_t usNbFlags; /* NetBIOS flags 0x6000 : IP-address, big-endian */
\r
261 uint32_t ulIPAddress;
\r
263 #include "pack_struct_end.h"
\r
264 typedef struct xNBNSAnswer NBNSAnswer_t;
\r
266 #endif /* ipconfigUSE_NBNS == 1 */
\r
268 /*-----------------------------------------------------------*/
\r
270 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
271 uint32_t FreeRTOS_dnslookup( const char *pcHostName )
\r
273 uint32_t ulIPAddress = 0UL;
\r
274 prvProcessDNSCache( pcHostName, &ulIPAddress, pdTRUE );
\r
275 return ulIPAddress;
\r
277 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\r
278 /*-----------------------------------------------------------*/
\r
280 #if( ipconfigDNS_USE_CALLBACKS != 0 )
\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
287 struct xLIST_ITEM xListItem;
\r
291 static List_t xCallbackList;
\r
293 /* Define FreeRTOS_gethostbyname() as a normal blocking call. */
\r
294 uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
\r
296 return FreeRTOS_gethostbyname_a( pcHostName, ( FOnDNSEvent ) NULL, ( void* )NULL, 0 );
\r
298 /*-----------------------------------------------------------*/
\r
300 /* Initialise the list of call-back structures. */
\r
301 void vDNSInitialise( void );
\r
302 void vDNSInitialise( void )
\r
304 vListInitialise( &xCallbackList );
\r
306 /*-----------------------------------------------------------*/
\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
313 void vDNSCheckCallBack( void *pvSearchID );
\r
314 void vDNSCheckCallBack( void *pvSearchID )
\r
316 const ListItem_t *pxIterator;
\r
317 const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
\r
321 for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
\r
322 pxIterator != ( const ListItem_t * ) xEnd;
\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
330 uxListRemove( &pxCallback->xListItem );
\r
331 vPortFree( pxCallback );
\r
333 else if( xTaskCheckForTimeOut( &pxCallback->xTimeoutState, &pxCallback->xRemaningTime ) != pdFALSE )
\r
335 pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );
\r
336 uxListRemove( &pxCallback->xListItem );
\r
337 vPortFree( ( void * ) pxCallback );
\r
343 if( listLIST_IS_EMPTY( &xCallbackList ) )
\r
345 vIPSetDnsTimerEnableState( pdFALSE );
\r
348 /*-----------------------------------------------------------*/
\r
350 void FreeRTOS_gethostbyname_cancel( void *pvSearchID )
\r
352 /* _HT_ Should better become a new API call to have the IP-task remove the callback */
\r
353 vDNSCheckCallBack( pvSearchID );
\r
355 /*-----------------------------------------------------------*/
\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
362 size_t lLength = strlen( pcHostName );
\r
363 DNSCallback_t *pxCallback = ( DNSCallback_t * )pvPortMalloc( sizeof( *pxCallback ) + lLength );
\r
365 /* Translate from ms to number of clock ticks. */
\r
366 xTimeout /= portTICK_PERIOD_MS;
\r
367 if( pxCallback != NULL )
\r
369 if( listLIST_IS_EMPTY( &xCallbackList ) )
\r
371 /* This is the first one, start the DNS timer to check for timeouts */
\r
372 vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, xTimeout ) );
\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
383 vListInsertEnd( &xCallbackList, &pxCallback->xListItem );
\r
388 /*-----------------------------------------------------------*/
\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
395 const ListItem_t *pxIterator;
\r
396 const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
\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
404 if( listGET_LIST_ITEM_VALUE( pxIterator ) == xIdentifier )
\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
412 vIPSetDnsTimerEnableState( pdFALSE );
\r
421 #endif /* ipconfigDNS_USE_CALLBACKS != 0 */
\r
422 /*-----------------------------------------------------------*/
\r
424 #if( ipconfigDNS_USE_CALLBACKS == 0 )
\r
425 uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
\r
427 uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName, FOnDNSEvent pCallback, void *pvSearchID, TickType_t xTimeout )
\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
437 /* If a DNS cache is used then check the cache before issuing another DNS
\r
439 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
441 ulIPAddress = FreeRTOS_dnslookup( pcHostName );
\r
442 if( ulIPAddress != 0 )
\r
444 FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );
\r
448 /* prvGetHostByName will be called to start a DNS lookup. */
\r
451 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\r
453 #if( ipconfigDNS_USE_CALLBACKS != 0 )
\r
455 if( pCallback != NULL )
\r
457 if( ulIPAddress == 0UL )
\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
465 /* The IP address is known, do the call-back now. */
\r
466 pCallback( pcHostName, pvSearchID, ulIPAddress );
\r
472 if( ulIPAddress == 0UL)
\r
474 ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms );
\r
477 return ulIPAddress;
\r
479 /*-----------------------------------------------------------*/
\r
481 static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms )
\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
490 size_t xPayloadLength, xExpectedPayloadLength;
\r
491 TickType_t xWriteTimeOut_ms = 100U;
\r
493 #if( ipconfigUSE_LLMNR == 1 )
\r
494 BaseType_t bHasDot = pdFALSE;
\r
495 #endif /* ipconfigUSE_LLMNR == 1 */
\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
501 const char *pucPtr;
\r
502 for( pucPtr = pcHostName; *pucPtr; pucPtr++ )
\r
504 if( *pucPtr == '.' )
\r
511 #endif /* ipconfigUSE_LLMNR == 1 */
\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
517 xDNSSocket = prvCreateDNSSocket();
\r
519 if( xDNSSocket != NULL )
\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
524 for( xAttempt = 0; xAttempt < ipconfigDNS_REQUEST_ATTEMPTS; xAttempt++ )
\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
531 if( pucUDPPayloadBuffer != NULL )
\r
533 /* Create the message in the obtained buffer. */
\r
534 xPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, xIdentifier );
\r
536 iptraceSENDING_DNS_REQUEST();
\r
538 /* Obtain the DNS server address. */
\r
539 FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulIPAddress );
\r
541 /* Send the DNS message. */
\r
542 #if( ipconfigUSE_LLMNR == 1 )
\r
543 if( bHasDot == pdFALSE )
\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
553 /* Use DNS server. */
\r
554 xAddress.sin_addr = ulIPAddress;
\r
555 xAddress.sin_port = dnsDNS_PORT;
\r
560 if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, xPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )
\r
562 /* Wait for the reply. */
\r
563 lBytes = FreeRTOS_recvfrom( xDNSSocket, &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xAddress, &ulAddressLength );
\r
567 /* The reply was received. Process it. */
\r
568 ulIPAddress = prvParseDNSReply( pucUDPPayloadBuffer, xIdentifier );
\r
570 /* Finished with the buffer. The zero copy interface
\r
571 is being used, so the buffer must be freed by the
\r
573 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
575 if( ulIPAddress != 0UL )
\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
591 /* Finished with the socket. */
\r
592 FreeRTOS_closesocket( xDNSSocket );
\r
595 return ulIPAddress;
\r
597 /*-----------------------------------------------------------*/
\r
599 static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier )
\r
601 DNSMessage_t *pxDNSMessageHeader;
\r
602 uint8_t *pucStart, *pucByte;
\r
604 static const DNSMessage_t xDefaultPartDNSHeader =
\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
614 /* Copy in the const part of the header. */
\r
615 memcpy( ( void * ) pucUDPPayloadBuffer, ( void * ) &xDefaultPartDNSHeader, sizeof( xDefaultPartDNSHeader ) );
\r
617 /* Write in a unique identifier. */
\r
618 pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
619 pxDNSMessageHeader->usIdentifier = ( uint16_t ) xIdentifier;
\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
625 /* Leave a gap for the first length bytes. */
\r
626 pucByte = pucStart + 1;
\r
628 /* Copy in the host name. */
\r
629 strcpy( ( char * ) pucByte, pcHostName );
\r
631 /* Mark the end of the string. */
\r
632 pucByte += strlen( pcHostName );
\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
644 while( ( *pucByte != 0x00 ) && ( *pucByte != '.' ) )
\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
654 pucStart = pucByte;
\r
656 } while( *pucByte != 0x00 );
\r
658 /* Finish off the record. */
\r
660 pxTail = (DNSTail_t *)( pucByte + 1 );
\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
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
669 /*-----------------------------------------------------------*/
\r
671 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
673 static uint8_t *prvReadNameField( uint8_t *pucByte, char *pcName, BaseType_t xLen )
\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
680 /* Jump over the two byte offset. */
\r
681 pucByte += sizeof( uint16_t );
\r
686 /* pucByte points to the full name. Walk over the string. */
\r
687 while( *pucByte != 0x00 )
\r
690 if( xNameLen && xNameLen < xLen - 1 )
\r
691 pcName[xNameLen++] = '.';
\r
692 for( xCount = *(pucByte++); xCount--; pucByte++ )
\r
694 if( xNameLen < xLen - 1 )
\r
695 pcName[xNameLen++] = *( ( char * ) pucByte );
\r
704 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\r
705 /*-----------------------------------------------------------*/
\r
707 static uint8_t *prvSkipNameField( uint8_t *pucByte )
\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
713 /* Jump over the two byte offset. */
\r
714 pucByte += sizeof( uint16_t );
\r
719 /* pucByte points to the full name. Walk over the string. */
\r
720 while( *pucByte != 0x00 )
\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
732 /*-----------------------------------------------------------*/
\r
734 uint32_t ulDNSHandlePacket( NetworkBufferDescriptor_t *pxNetworkBuffer )
\r
736 uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
\r
737 DNSMessage_t *pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
739 prvParseDNSReply( pucUDPPayloadBuffer, ( uint32_t ) pxDNSMessageHeader->usIdentifier );
\r
741 /* The packet was not consumed. */
\r
744 /*-----------------------------------------------------------*/
\r
746 #if( ipconfigUSE_NBNS == 1 )
\r
748 uint32_t ulNBNSHandlePacket (NetworkBufferDescriptor_t *pxNetworkBuffer )
\r
750 UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
\r
751 uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( *pxUDPPacket );
\r
753 prvTreatNBNS( pucUDPPayloadBuffer, pxUDPPacket->xIPHeader.ulSourceIPAddress );
\r
755 /* The packet was not consumed. */
\r
759 #endif /* ipconfigUSE_NBNS */
\r
760 /*-----------------------------------------------------------*/
\r
762 static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, TickType_t xIdentifier )
\r
764 DNSMessage_t *pxDNSMessageHeader;
\r
765 uint32_t ulIPAddress = 0UL;
\r
766 #if( ipconfigUSE_LLMNR == 1 )
\r
767 char *pcRequestedName = NULL;
\r
770 uint16_t x, usDataLength, usQuestions;
\r
771 #if( ipconfigUSE_LLMNR == 1 )
\r
772 uint16_t usType = 0, usClass = 0;
\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
778 pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
780 if( pxDNSMessageHeader->usIdentifier == ( uint16_t ) xIdentifier )
\r
782 /* Start at the first byte after the header. */
\r
783 pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t );
\r
785 /* Skip any question records. */
\r
786 usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );
\r
787 for( x = 0; x < usQuestions; x++ )
\r
789 #if( ipconfigUSE_LLMNR == 1 )
\r
793 pcRequestedName = ( char * ) pucByte;
\r
798 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
801 pucByte = prvReadNameField( pucByte, pcName, sizeof( pcName ) );
\r
804 #endif /* ipconfigUSE_DNS_CACHE */
\r
806 /* Skip the variable length pcName field. */
\r
807 pucByte = prvSkipNameField( pucByte );
\r
810 #if( ipconfigUSE_LLMNR == 1 )
\r
812 /* usChar2u16 returns value in host endianness. */
\r
813 usType = usChar2u16( pucByte );
\r
814 usClass = usChar2u16( pucByte + 2 );
\r
816 #endif /* ipconfigUSE_LLMNR */
\r
818 /* Skip the type and class fields. */
\r
819 pucByte += sizeof( uint32_t );
\r
822 /* Search through the answers records. */
\r
823 pxDNSMessageHeader->usAnswers = FreeRTOS_ntohs( pxDNSMessageHeader->usAnswers );
\r
825 if( ( pxDNSMessageHeader->usFlags & dnsRX_FLAGS_MASK ) == dnsEXPECTED_RX_FLAGS )
\r
827 for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )
\r
829 pucByte = prvSkipNameField( pucByte );
\r
831 /* Is the type field that of an A record? */
\r
832 if( usChar2u16( pucByte ) == dnsTYPE_A_HOST )
\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
837 pucByte += ( sizeof( uint32_t ) + sizeof( uint32_t ) + sizeof( uint8_t ) );
\r
839 /* Sanity check the data length. */
\r
840 if( ( size_t ) *pucByte == sizeof( uint32_t ) )
\r
842 /* Skip the second byte of the length. */
\r
845 /* Copy the IP address out of the record. */
\r
846 memcpy( ( void * ) &ulIPAddress, ( void * ) pucByte, sizeof( uint32_t ) );
\r
848 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
850 prvProcessDNSCache( pcName, &ulIPAddress, pdFALSE );
\r
852 #endif /* ipconfigUSE_DNS_CACHE */
\r
853 #if( ipconfigDNS_USE_CALLBACKS != 0 )
\r
855 /* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */
\r
856 vDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress );
\r
858 #endif /* ipconfigDNS_USE_CALLBACKS != 0 */
\r
865 /* Skip the type, class and time to live fields. */
\r
866 pucByte += ( sizeof( uint32_t ) + sizeof( uint32_t ) );
\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
872 /* Jump over the data length bytes, and the data itself. */
\r
873 pucByte += usDataLength + sizeof( uint16_t );
\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
880 /* If this is not a reply to our DNS request, it might an LLMNR
\r
882 if( xApplicationDNSQueryHook ( ( pcRequestedName + 1 ) ) )
\r
885 NetworkBufferDescriptor_t *pxNewBuffer = NULL;
\r
886 NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
\r
887 LLMNRAnswer_t *pxAnswer;
\r
889 if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
\r
891 BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +
\r
892 sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
\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
901 BaseType_t xOffset1, xOffset2;
\r
903 xOffset1 = ( BaseType_t ) ( pucByte - pucUDPPayloadBuffer );
\r
904 xOffset2 = ( BaseType_t ) ( ( ( uint8_t * ) pcRequestedName ) - pucUDPPayloadBuffer );
\r
906 pxNetworkBuffer = pxNewBuffer;
\r
907 pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + ipUDP_PAYLOAD_OFFSET_IPv4;
\r
909 pucByte = pucUDPPayloadBuffer + xOffset1;
\r
910 pcRequestedName = ( char * ) ( pucUDPPayloadBuffer + xOffset2 );
\r
911 pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
916 /* Just to indicate that the message may not be answered. */
\r
917 pxNetworkBuffer = NULL;
\r
920 if( pxNetworkBuffer != NULL )
\r
922 pxAnswer = (LLMNRAnswer_t *)pucByte;
\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
930 pxAnswer->ucNameCode = dnsNAME_IS_OFFSET;
\r
931 pxAnswer->ucNameOffset = ( uint8_t )( pcRequestedName - ( char * ) pucUDPPayloadBuffer );
\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
939 usLength = ( int16_t ) ( sizeof( *pxAnswer ) + ( size_t ) ( pucByte - pucUDPPayloadBuffer ) );
\r
941 prvReplyDNSMessage( pxNetworkBuffer, usLength );
\r
943 if( pxNewBuffer != NULL )
\r
945 vReleaseNetworkBufferAndDescriptor( pxNewBuffer );
\r
950 #endif /* ipconfigUSE_LLMNR == 1 */
\r
953 return ulIPAddress;
\r
955 /*-----------------------------------------------------------*/
\r
957 #if( ipconfigUSE_NBNS == 1 )
\r
959 static void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, uint32_t ulIPAddress )
\r
961 uint16_t usFlags, usType, usClass;
\r
962 uint8_t *pucSource, *pucTarget;
\r
964 uint8_t ucNBNSName[ 17 ];
\r
966 usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) );
\r
968 if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )
\r
970 usType = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
\r
971 usClass = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usClass ) );
\r
973 /* Not used for now */
\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
980 /* Start with decoding the last 2 bytes. */
\r
981 pucSource = pucUDPPayloadBuffer + ( offsetof( NBNSRequest_t, ucName ) + ( dnsNBNS_ENCODED_NAME_LENGTH - 2 ) );
\r
985 ucByte = ( uint8_t ) ( ( ( pucSource[ 0 ] - 0x41 ) << 4 ) | ( pucSource[ 1 ] - 0x41 ) );
\r
987 /* Make sure there are no trailing spaces in the name. */
\r
988 if( ( ucByte == ' ' ) && ( pucTarget[ 1 ] == '\0' ) )
\r
993 *pucTarget = ucByte;
\r
995 if( pucTarget == ucNBNSName )
\r
1004 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
1006 if( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) != 0 )
\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
1015 /* Avoid compiler warnings. */
\r
1016 ( void ) ulIPAddress;
\r
1018 #endif /* ipconfigUSE_DNS_CACHE */
\r
1020 if( ( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) == 0 ) &&
\r
1021 ( usType == dnsNBNS_TYPE_NET_BIOS ) &&
\r
1022 ( xApplicationDNSQueryHook( ( const char * ) ucNBNSName ) != pdFALSE ) )
\r
1024 uint16_t usLength;
\r
1025 DNSMessage_t *pxMessage;
\r
1026 NBNSAnswer_t *pxAnswer;
\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
1032 if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
\r
1034 NetworkBufferDescriptor_t *pxNewBuffer;
\r
1035 BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +
\r
1037 sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
\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
1046 pucUDPPayloadBuffer = pxNewBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
\r
1047 pxNetworkBuffer = pxNewBuffer;
\r
1051 /* Just prevent that a reply will be sent */
\r
1052 pxNetworkBuffer = NULL;
\r
1056 /* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */
\r
1057 if( pxNetworkBuffer != NULL )
\r
1059 pxMessage = (DNSMessage_t *)pucUDPPayloadBuffer;
\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
1069 pxAnswer = (NBNSAnswer_t *)( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
\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
1078 usLength = ( uint16_t ) ( offsetof( NBNSRequest_t, usType ) + sizeof( NBNSAnswer_t ) );
\r
1080 prvReplyDNSMessage( pxNetworkBuffer, usLength );
\r
1086 #endif /* ipconfigUSE_NBNS */
\r
1087 /*-----------------------------------------------------------*/
\r
1089 static Socket_t prvCreateDNSSocket( void )
\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
1096 /* This must be the first time this function has been called. Create
\r
1098 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
1100 /* Auto bind the port. */
\r
1101 xAddress.sin_port = 0u;
\r
1102 xReturn = FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );
\r
1104 /* Check the bind was successful, and clean up if not. */
\r
1105 if( xReturn != 0 )
\r
1107 FreeRTOS_closesocket( xSocket );
\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
1119 /*-----------------------------------------------------------*/
\r
1121 #if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
\r
1123 static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength )
\r
1125 UDPPacket_t *pxUDPPacket;
\r
1126 IPHeader_t *pxIPHeader;
\r
1127 UDPHeader_t *pxUDPHeader;
\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
1143 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
\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
1150 /* calculate the UDP checksum for outgoing package */
\r
1151 usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, pdTRUE );
\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
1158 /* This function will fill in the eth addresses and send the packet */
\r
1159 vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );
\r
1162 #endif /* ipconfigUSE_NBNS == 1 || ipconfigUSE_LLMNR == 1 */
\r
1163 /*-----------------------------------------------------------*/
\r
1165 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
1167 static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, BaseType_t xLookUp )
\r
1170 BaseType_t xFound = pdFALSE;
\r
1171 static BaseType_t xFreeEntry = 0;
\r
1173 /* For each entry in the DNS cache table. */
\r
1174 for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )
\r
1176 if( xDNSCache[ x ].pcName[ 0 ] == 0 )
\r
1181 if( strncmp( xDNSCache[ x ].pcName, pcName, sizeof( xDNSCache[ x ].pcName ) ) == 0 )
\r
1183 /* Is this function called for a lookup or to add/update an IP address? */
\r
1184 if( xLookUp != pdFALSE )
\r
1186 *pulIP = xDNSCache[ x ].ulIPAddress;
\r
1190 xDNSCache[ x ].ulIPAddress = *pulIP;
\r
1198 if( xFound == pdFALSE )
\r
1200 if( xLookUp != pdFALSE )
\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
1211 if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )
\r
1218 if( ( xLookUp == 0 ) || ( *pulIP != 0 ) )
\r
1220 FreeRTOS_debug_printf( ( "prvProcessDNSCache: %s: '%s' @ %lxip\n", xLookUp ? "look-up" : "add", pcName, FreeRTOS_ntohl( *pulIP ) ) );
\r
1224 #endif /* ipconfigUSE_DNS_CACHE */
\r
1226 #endif /* ipconfigUSE_DNS != 0 */
\r