-/*\r
- * FreeRTOS+TCP V2.2.0\r
- * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
- * this software and associated documentation files (the "Software"), to deal in\r
- * the Software without restriction, including without limitation the rights to\r
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
- * the Software, and to permit persons to whom the Software is furnished to do so,\r
- * subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in all\r
- * copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- *\r
- * http://aws.amazon.com/freertos\r
- * http://www.FreeRTOS.org\r
- */\r
-\r
-/* Standard includes. */\r
-#include <stdint.h>\r
-\r
-/* FreeRTOS includes. */\r
-#include "FreeRTOS.h"\r
-#include "task.h"\r
-#include "queue.h"\r
-#include "list.h"\r
-#include "semphr.h"\r
-\r
-/* FreeRTOS+TCP includes. */\r
-#include "FreeRTOS_IP.h"\r
-#include "FreeRTOS_Sockets.h"\r
-#include "FreeRTOS_IP_Private.h"\r
-#include "FreeRTOS_UDP_IP.h"\r
-#include "FreeRTOS_DNS.h"\r
-#include "NetworkBufferManagement.h"\r
-#include "NetworkInterface.h"\r
-#include "IPTraceMacroDefaults.h"\r
-\r
-/* Exclude the entire file if DNS is not enabled. */\r
-#if( ipconfigUSE_DNS != 0 )\r
-\r
-#if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )\r
- #define dnsDNS_PORT 0x3500u\r
- #define dnsONE_QUESTION 0x0100u\r
- #define dnsOUTGOING_FLAGS 0x0001u /* Standard query. */\r
- #define dnsRX_FLAGS_MASK 0x0f80u /* The bits of interest in the flags field of incoming DNS messages. */\r
- #define dnsEXPECTED_RX_FLAGS 0x0080u /* Should be a response, without any errors. */\r
-#else\r
- #define dnsDNS_PORT 0x0035u\r
- #define dnsONE_QUESTION 0x0001u\r
- #define dnsOUTGOING_FLAGS 0x0100u /* Standard query. */\r
- #define dnsRX_FLAGS_MASK 0x800fu /* The bits of interest in the flags field of incoming DNS messages. */\r
- #define dnsEXPECTED_RX_FLAGS 0x8000u /* Should be a response, without any errors. */\r
-\r
-#endif /* ipconfigBYTE_ORDER */\r
-\r
-/* The maximum number of times a DNS request should be sent out if a response\r
-is not received, before giving up. */\r
-#ifndef ipconfigDNS_REQUEST_ATTEMPTS\r
- #define ipconfigDNS_REQUEST_ATTEMPTS 5\r
-#endif\r
-\r
-/* If the top two bits in the first character of a name field are set then the\r
-name field is an offset to the string, rather than the string itself. */\r
-#define dnsNAME_IS_OFFSET ( ( uint8_t ) 0xc0 )\r
-\r
-/* NBNS flags. */\r
-#define dnsNBNS_FLAGS_RESPONSE 0x8000u\r
-#define dnsNBNS_FLAGS_OPCODE_MASK 0x7800u\r
-#define dnsNBNS_FLAGS_OPCODE_QUERY 0x0000u\r
-#define dnsNBNS_FLAGS_OPCODE_REGISTRATION 0x2800u\r
-\r
-/* Host types. */\r
-#define dnsTYPE_A_HOST 0x01u\r
-#define dnsCLASS_IN 0x01u\r
-\r
-/* LLMNR constants. */\r
-#define dnsLLMNR_TTL_VALUE 300000uL\r
-#define dnsLLMNR_FLAGS_IS_REPONSE 0x8000u\r
-\r
-/* NBNS constants. */\r
-#define dnsNBNS_TTL_VALUE 3600uL /* 1 hour valid */\r
-#define dnsNBNS_TYPE_NET_BIOS 0x0020u\r
-#define dnsNBNS_CLASS_IN 0x01u\r
-#define dnsNBNS_NAME_FLAGS 0x6000u\r
-#define dnsNBNS_ENCODED_NAME_LENGTH 32\r
-\r
-/* If the queried NBNS name matches with the device's name,\r
-the query will be responded to with these flags: */\r
-#define dnsNBNS_QUERY_RESPONSE_FLAGS ( 0x8500u )\r
-\r
-/* Flag DNS parsing errors in situations where an IPv4 address is the return\r
-type. */\r
-#define dnsPARSE_ERROR 0uL\r
-\r
-/*\r
- * Create a socket and bind it to the standard DNS port number. Return the\r
- * the created socket - or NULL if the socket could not be created or bound.\r
- */\r
-static Socket_t prvCreateDNSSocket( void );\r
-\r
-/*\r
- * Create the DNS message in the zero copy buffer passed in the first parameter.\r
- */\r
-static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer,\r
- const char *pcHostName,\r
- TickType_t uxIdentifier );\r
-\r
-/*\r
- * Simple routine that jumps over the NAME field of a resource record.\r
- */\r
-static uint8_t * prvSkipNameField( uint8_t *pucByte,\r
- size_t uxSourceLen );\r
-\r
-/*\r
- * Process a response packet from a DNS server.\r
- * The parameter 'xExpected' indicates whether the identifier in the reply\r
- * was expected, and thus if the DNS cache may be updated with the reply.\r
- */\r
-static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer,\r
- size_t uxBufferLength,\r
- BaseType_t xExpected );\r
-\r
-/*\r
- * Prepare and send a message to a DNS server. 'uxReadTimeOut_ticks' will be passed as\r
- * zero, in case the user has supplied a call-back function.\r
- */\r
-static uint32_t prvGetHostByName( const char *pcHostName,\r
- TickType_t uxIdentifier,\r
- TickType_t uxReadTimeOut_ticks );\r
-\r
-/*\r
- * The NBNS and the LLMNR protocol share this reply function.\r
- */\r
-#if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )\r
- static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer,\r
- BaseType_t lNetLength );\r
-#endif\r
-\r
-#if( ipconfigUSE_NBNS == 1 )\r
- static portINLINE void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer,\r
- size_t uxBufferLength,\r
- uint32_t ulIPAddress );\r
-#endif /* ipconfigUSE_NBNS */\r
-\r
-\r
-#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )\r
- static uint8_t * prvReadNameField( uint8_t *pucByte,\r
- size_t uxSourceLen,\r
- char *pcName,\r
- size_t uxLen );\r
-#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */\r
-\r
-#if( ipconfigUSE_DNS_CACHE == 1 )\r
- static void prvProcessDNSCache( const char *pcName,\r
- uint32_t *pulIP,\r
- uint32_t ulTTL,\r
- BaseType_t xLookUp );\r
-\r
- typedef struct xDNS_CACHE_TABLE_ROW\r
- {\r
- uint32_t ulIPAddress; /* The IP address of an ARP cache entry. */\r
- char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ]; /* The name of the host */\r
- uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */\r
- uint32_t ulTimeWhenAddedInSeconds;\r
- } DNSCacheRow_t;\r
-\r
- static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];\r
-\r
- void FreeRTOS_dnsclear()\r
- {\r
- memset( xDNSCache, 0x0, sizeof( xDNSCache ) );\r
- }\r
-#endif /* ipconfigUSE_DNS_CACHE == 1 */\r
-\r
-#if( ipconfigUSE_LLMNR == 1 )\r
- const MACAddress_t xLLMNR_MacAdress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };\r
-#endif /* ipconfigUSE_LLMNR == 1 */\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-#include "pack_struct_start.h"\r
-struct xDNSMessage\r
-{\r
- uint16_t usIdentifier;\r
- uint16_t usFlags;\r
- uint16_t usQuestions;\r
- uint16_t usAnswers;\r
- uint16_t usAuthorityRRs;\r
- uint16_t usAdditionalRRs;\r
-}\r
-#include "pack_struct_end.h"\r
-typedef struct xDNSMessage DNSMessage_t;\r
-\r
-/* A DNS query consists of a header, as described in 'struct xDNSMessage'\r
-It is followed by 1 or more queries, each one consisting of a name and a tail,\r
-with two fields: type and class\r
-*/\r
-#include "pack_struct_start.h"\r
-struct xDNSTail\r
-{\r
- uint16_t usType;\r
- uint16_t usClass;\r
-}\r
-#include "pack_struct_end.h"\r
-typedef struct xDNSTail DNSTail_t;\r
-\r
-/* DNS answer record header. */\r
-#include "pack_struct_start.h"\r
-struct xDNSAnswerRecord\r
-{\r
- uint16_t usType;\r
- uint16_t usClass;\r
- uint32_t ulTTL;\r
- uint16_t usDataLength;\r
-}\r
-#include "pack_struct_end.h"\r
-typedef struct xDNSAnswerRecord DNSAnswerRecord_t;\r
-\r
-#if( ipconfigUSE_LLMNR == 1 )\r
-\r
- #include "pack_struct_start.h"\r
- struct xLLMNRAnswer\r
- {\r
- uint8_t ucNameCode;\r
- uint8_t ucNameOffset; /* The name is not repeated in the answer, only the offset is given with "0xc0 <offs>" */\r
- uint16_t usType;\r
- uint16_t usClass;\r
- uint32_t ulTTL;\r
- uint16_t usDataLength;\r
- uint32_t ulIPAddress;\r
- }\r
- #include "pack_struct_end.h"\r
- typedef struct xLLMNRAnswer LLMNRAnswer_t;\r
-\r
-#endif /* ipconfigUSE_LLMNR == 1 */\r
-\r
-#if( ipconfigUSE_NBNS == 1 )\r
-\r
- #include "pack_struct_start.h"\r
- struct xNBNSRequest\r
- {\r
- uint16_t usRequestId;\r
- uint16_t usFlags;\r
- uint16_t ulRequestCount;\r
- uint16_t usAnswerRSS;\r
- uint16_t usAuthRSS;\r
- uint16_t usAdditionalRSS;\r
- uint8_t ucNameSpace;\r
- uint8_t ucName[ dnsNBNS_ENCODED_NAME_LENGTH ];\r
- uint8_t ucNameZero;\r
- uint16_t usType;\r
- uint16_t usClass;\r
- }\r
- #include "pack_struct_end.h"\r
- typedef struct xNBNSRequest NBNSRequest_t;\r
-\r
- #include "pack_struct_start.h"\r
- struct xNBNSAnswer\r
- {\r
- uint16_t usType;\r
- uint16_t usClass;\r
- uint32_t ulTTL;\r
- uint16_t usDataLength;\r
- uint16_t usNbFlags; /* NetBIOS flags 0x6000 : IP-address, big-endian */\r
- uint32_t ulIPAddress;\r
- }\r
- #include "pack_struct_end.h"\r
- typedef struct xNBNSAnswer NBNSAnswer_t;\r
-\r
- #endif /* ipconfigUSE_NBNS == 1 */\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ipconfigUSE_DNS_CACHE == 1 )\r
- uint32_t FreeRTOS_dnslookup( const char *pcHostName )\r
- {\r
- uint32_t ulIPAddress = 0uL;\r
-\r
- prvProcessDNSCache( pcHostName, &ulIPAddress, 0, pdTRUE );\r
- return ulIPAddress;\r
- }\r
-#endif /* ipconfigUSE_DNS_CACHE == 1 */\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ipconfigDNS_USE_CALLBACKS == 1 )\r
-\r
- typedef struct xDNS_Callback\r
- {\r
- TickType_t uxRemaningTime; /* Timeout in ms */\r
- FOnDNSEvent pCallbackFunction; /* Function to be called when the address has been found or when a timeout has beeen reached */\r
- TimeOut_t uxTimeoutState;\r
- void *pvSearchID;\r
- struct xLIST_ITEM xListItem;\r
- char pcName[ 1 ];\r
- } DNSCallback_t;\r
-\r
- static List_t xCallbackList;\r
-\r
- /* Define FreeRTOS_gethostbyname() as a normal blocking call. */\r
- uint32_t FreeRTOS_gethostbyname( const char *pcHostName )\r
- {\r
- return FreeRTOS_gethostbyname_a( pcHostName, ( FOnDNSEvent ) NULL, ( void * ) NULL, 0 );\r
- }\r
- /*-----------------------------------------------------------*/\r
-\r
- /* Initialise the list of call-back structures. */\r
- void vDNSInitialise( void );\r
- void vDNSInitialise( void )\r
- {\r
- vListInitialise( &xCallbackList );\r
- }\r
- /*-----------------------------------------------------------*/\r
-\r
- /* Iterate through the list of call-back structures and remove\r
- old entries which have reached a timeout.\r
- As soon as the list hase become empty, the DNS timer will be stopped\r
- In case pvSearchID is supplied, the user wants to cancel a DNS request\r
- */\r
- void vDNSCheckCallBack( void *pvSearchID );\r
- void vDNSCheckCallBack( void *pvSearchID )\r
- {\r
- const ListItem_t *pxIterator;\r
- const MiniListItem_t * xEnd = ( const MiniListItem_t * ) listGET_END_MARKER( &xCallbackList );\r
-\r
- vTaskSuspendAll();\r
- {\r
- for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );\r
- pxIterator != ( const ListItem_t * ) xEnd;\r
- )\r
- {\r
- DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );\r
-\r
- /* Move to the next item because we might remove this item */\r
- pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );\r
-\r
- if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )\r
- {\r
- uxListRemove( &pxCallback->xListItem );\r
- vPortFree( pxCallback );\r
- }\r
- else if( xTaskCheckForTimeOut( &pxCallback->uxTimeoutState, &pxCallback->uxRemaningTime ) != pdFALSE )\r
- {\r
- pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );\r
- uxListRemove( &pxCallback->xListItem );\r
- vPortFree( ( void * ) pxCallback );\r
- }\r
- }\r
- }\r
- xTaskResumeAll();\r
-\r
- if( listLIST_IS_EMPTY( &xCallbackList ) )\r
- {\r
- vIPSetDnsTimerEnableState( pdFALSE );\r
- }\r
- }\r
- /*-----------------------------------------------------------*/\r
-\r
- void FreeRTOS_gethostbyname_cancel( void *pvSearchID )\r
- {\r
- /* _HT_ Should better become a new API call to have the IP-task remove the callback */\r
- vDNSCheckCallBack( pvSearchID );\r
- }\r
- /*-----------------------------------------------------------*/\r
-\r
- /* FreeRTOS_gethostbyname_a() was called along with callback parameters.\r
- Store them in a list for later reference. */\r
- static void vDNSSetCallBack( const char *pcHostName,\r
- void *pvSearchID,\r
- FOnDNSEvent pCallbackFunction,\r
- TickType_t uxTimeout,\r
- TickType_t uxIdentifier );\r
- static void vDNSSetCallBack( const char *pcHostName,\r
- void *pvSearchID,\r
- FOnDNSEvent pCallbackFunction,\r
- TickType_t uxTimeout,\r
- TickType_t uxIdentifier )\r
- {\r
- size_t lLength = strlen( pcHostName );\r
- DNSCallback_t *pxCallback = ( DNSCallback_t * ) pvPortMalloc( sizeof( *pxCallback ) + lLength );\r
-\r
- /* Translate from ms to number of clock ticks. */\r
- uxTimeout /= portTICK_PERIOD_MS;\r
-\r
- if( pxCallback != NULL )\r
- {\r
- if( listLIST_IS_EMPTY( &xCallbackList ) )\r
- {\r
- /* This is the first one, start the DNS timer to check for timeouts */\r
- vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, uxTimeout ) );\r
- }\r
-\r
- strcpy( pxCallback->pcName, pcHostName );\r
- pxCallback->pCallbackFunction = pCallbackFunction;\r
- pxCallback->pvSearchID = pvSearchID;\r
- pxCallback->uxRemaningTime = uxTimeout;\r
- vTaskSetTimeOutState( &pxCallback->uxTimeoutState );\r
- listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void * ) pxCallback );\r
- listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), uxIdentifier );\r
- vTaskSuspendAll();\r
- {\r
- vListInsertEnd( &xCallbackList, &pxCallback->xListItem );\r
- }\r
- xTaskResumeAll();\r
- }\r
- }\r
- /*-----------------------------------------------------------*/\r
-\r
- /* A DNS reply was received, see if there is any matching entry and\r
- call the handler. Returns pdTRUE if uxIdentifier was recognised. */\r
- static BaseType_t xDNSDoCallback( TickType_t uxIdentifier,\r
- const char *pcName,\r
- uint32_t ulIPAddress );\r
- static BaseType_t xDNSDoCallback( TickType_t uxIdentifier,\r
- const char *pcName,\r
- uint32_t ulIPAddress )\r
- {\r
- BaseType_t xResult = pdFALSE;\r
- const ListItem_t *pxIterator;\r
- const MiniListItem_t * xEnd = ( const MiniListItem_t * ) listGET_END_MARKER( &xCallbackList );\r
-\r
- vTaskSuspendAll();\r
- {\r
- for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );\r
- pxIterator != ( const ListItem_t * ) xEnd;\r
- pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )\r
- {\r
- /* The cast will take away the 'configLIST_VOLATILE' */\r
- if( uxIdentifier == ( TickType_t ) listGET_LIST_ITEM_VALUE( pxIterator ) )\r
- {\r
- DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );\r
-\r
- pxCallback->pCallbackFunction( pcName, pxCallback->pvSearchID, ulIPAddress );\r
- uxListRemove( &pxCallback->xListItem );\r
- vPortFree( pxCallback );\r
-\r
- if( listLIST_IS_EMPTY( &xCallbackList ) )\r
- {\r
- /* The list of outstanding requests is empty. No need for periodic polling. */\r
- vIPSetDnsTimerEnableState( pdFALSE );\r
- }\r
-\r
- xResult = pdTRUE;\r
- break;\r
- }\r
- }\r
- }\r
- xTaskResumeAll();\r
- return xResult;\r
- }\r
-\r
-#endif /* ipconfigDNS_USE_CALLBACKS == 1 */\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ipconfigDNS_USE_CALLBACKS == 0 )\r
- uint32_t FreeRTOS_gethostbyname( const char *pcHostName )\r
-#else\r
- uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName,\r
- FOnDNSEvent pCallback,\r
- void *pvSearchID,\r
- TickType_t uxTimeout )\r
-#endif\r
-{\r
-uint32_t ulIPAddress = 0uL;\r
-TickType_t uxReadTimeOut_ticks = ipconfigDNS_RECEIVE_BLOCK_TIME_TICKS;\r
-TickType_t uxIdentifier = 0u;\r
-BaseType_t xHasRandom = pdFALSE;\r
-\r
- if( pcHostName != NULL )\r
- {\r
- /* If the supplied hostname is IP address, convert it to uint32_t\r
- and return. */\r
- #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 )\r
- {\r
- ulIPAddress = FreeRTOS_inet_addr( pcHostName );\r
- }\r
- #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */\r
-\r
- /* If a DNS cache is used then check the cache before issuing another DNS\r
- request. */\r
- #if( ipconfigUSE_DNS_CACHE == 1 )\r
- {\r
- if( ulIPAddress == 0uL )\r
- {\r
- ulIPAddress = FreeRTOS_dnslookup( pcHostName );\r
-\r
- if( ulIPAddress != 0 )\r
- {\r
- FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );\r
- }\r
- else\r
- {\r
- /* prvGetHostByName will be called to start a DNS lookup. */\r
- }\r
- }\r
- }\r
- #endif /* ipconfigUSE_DNS_CACHE == 1 */\r
-\r
- /* Generate a unique identifier. */\r
- if( ulIPAddress == 0uL )\r
- {\r
- uint32_t ulNumber;\r
-\r
- xHasRandom = xApplicationGetRandomNumber( &( ulNumber ) );\r
- /* DNS identifiers are 16-bit. */\r
- uxIdentifier = ( TickType_t ) ( ulNumber & 0xffffu );\r
- /* ipconfigRAND32() may not return a non-zero value. */\r
- }\r
-\r
- #if( ipconfigDNS_USE_CALLBACKS == 1 )\r
- {\r
- if( pCallback != NULL )\r
- {\r
- if( ulIPAddress == 0uL )\r
- {\r
- /* The user has provided a callback function, so do not block on recvfrom() */\r
- if( xHasRandom != pdFALSE )\r
- {\r
- uxReadTimeOut_ticks = 0u;\r
- vDNSSetCallBack( pcHostName, pvSearchID, pCallback, uxTimeout, uxIdentifier );\r
- }\r
- }\r
- else\r
- {\r
- /* The IP address is known, do the call-back now. */\r
- pCallback( pcHostName, pvSearchID, ulIPAddress );\r
- }\r
- }\r
- }\r
- #endif /* if ( ipconfigDNS_USE_CALLBACKS == 1 ) */\r
-\r
- if( ( ulIPAddress == 0uL ) && ( xHasRandom != pdFALSE ) )\r
- {\r
- ulIPAddress = prvGetHostByName( pcHostName, uxIdentifier, uxReadTimeOut_ticks );\r
- }\r
- }\r
- return ulIPAddress;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static uint32_t prvGetHostByName( const char *pcHostName,\r
- TickType_t uxIdentifier,\r
- TickType_t uxReadTimeOut_ticks )\r
-{\r
-struct freertos_sockaddr xAddress;\r
-Socket_t xDNSSocket;\r
-uint32_t ulIPAddress = 0uL;\r
-uint8_t *pucUDPPayloadBuffer;\r
-uint32_t ulAddressLength = sizeof( struct freertos_sockaddr );\r
-BaseType_t xAttempt;\r
-int32_t lBytes;\r
-size_t uxPayloadLength, uxExpectedPayloadLength;\r
-TickType_t uxWriteTimeOut_ticks = ipconfigDNS_SEND_BLOCK_TIME_TICKS;\r
-\r
-#if( ipconfigUSE_LLMNR == 1 )\r
- BaseType_t bHasDot = pdFALSE;\r
-#endif /* ipconfigUSE_LLMNR == 1 */\r
-\r
- /* If LLMNR is being used then determine if the host name includes a '.' -\r
- if not then LLMNR can be used as the lookup method. */\r
- #if( ipconfigUSE_LLMNR == 1 )\r
- {\r
- const char *pucPtr;\r
-\r
- for( pucPtr = pcHostName; *pucPtr; pucPtr++ )\r
- {\r
- if( *pucPtr == '.' )\r
- {\r
- bHasDot = pdTRUE;\r
- break;\r
- }\r
- }\r
- }\r
- #endif /* ipconfigUSE_LLMNR == 1 */\r
-\r
- /* Two is added at the end for the count of characters in the first\r
- subdomain part and the string end byte. */\r
- uxExpectedPayloadLength = sizeof( DNSMessage_t ) + strlen( pcHostName ) + sizeof( uint16_t ) + sizeof( uint16_t ) + 2u;\r
-\r
- xDNSSocket = prvCreateDNSSocket();\r
-\r
- if( xDNSSocket != NULL )\r
- {\r
- FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &uxWriteTimeOut_ticks, sizeof( TickType_t ) );\r
- FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &uxReadTimeOut_ticks, sizeof( TickType_t ) );\r
-\r
- for( xAttempt = 0; xAttempt < ipconfigDNS_REQUEST_ATTEMPTS; xAttempt++ )\r
- {\r
- /* Get a buffer. This uses a maximum delay, but the delay will be\r
- capped to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value\r
- still needs to be tested. */\r
- pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( uxExpectedPayloadLength, portMAX_DELAY );\r
-\r
- if( pucUDPPayloadBuffer != NULL )\r
- {\r
- /* Create the message in the obtained buffer. */\r
- uxPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, uxIdentifier );\r
-\r
- iptraceSENDING_DNS_REQUEST();\r
-\r
- /* Obtain the DNS server address. */\r
- FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulIPAddress );\r
-\r
- /* Send the DNS message. */\r
-#if( ipconfigUSE_LLMNR == 1 )\r
- if( bHasDot == pdFALSE )\r
- {\r
- /* Use LLMNR addressing. */\r
- ( ( DNSMessage_t * ) pucUDPPayloadBuffer )->usFlags = 0;\r
- xAddress.sin_addr = ipLLMNR_IP_ADDR; /* Is in network byte order. */\r
- xAddress.sin_port = FreeRTOS_ntohs( ipLLMNR_PORT );\r
- }\r
- else\r
-#endif\r
- {\r
- /* Use DNS server. */\r
- xAddress.sin_addr = ulIPAddress;\r
- xAddress.sin_port = dnsDNS_PORT;\r
- }\r
-\r
- ulIPAddress = 0uL;\r
-\r
- if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, uxPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )\r
- {\r
- /* Wait for the reply. */\r
- lBytes = FreeRTOS_recvfrom( xDNSSocket, &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xAddress, &ulAddressLength );\r
-\r
- if( lBytes > 0 )\r
- {\r
- BaseType_t xExpected;\r
- DNSMessage_t *pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
-\r
- /* See if the identifiers match. */\r
- if( uxIdentifier == ( TickType_t ) pxDNSMessageHeader->usIdentifier )\r
- {\r
- xExpected = pdTRUE;\r
- }\r
- else\r
- {\r
- /* The reply was not expected. */\r
- xExpected = pdFALSE;\r
- }\r
-\r
- /* The reply was received. Process it. */\r
- #if( ipconfigDNS_USE_CALLBACKS == 0 )\r
- /* It is useless to analyse the unexpected reply\r
- unless asynchronous look-ups are enabled. */\r
- if( xExpected != pdFALSE )\r
- #endif /* ipconfigDNS_USE_CALLBACKS == 0 */\r
- {\r
- ulIPAddress = prvParseDNSReply( pucUDPPayloadBuffer, ( size_t ) lBytes, xExpected );\r
- }\r
-\r
- /* Finished with the buffer. The zero copy interface\r
- is being used, so the buffer must be freed by the\r
- task. */\r
- FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
-\r
- if( ulIPAddress != 0uL )\r
- {\r
- /* All done. */\r
- break;\r
- }\r
- }\r
- }\r
- else\r
- {\r
- /* The message was not sent so the stack will not be\r
- releasing the zero copy - it must be released here. */\r
- FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
- }\r
- }\r
-\r
- if( uxReadTimeOut_ticks == 0u )\r
- {\r
- /* This DNS lookup is asynchronous, using a call-back:\r
- send the request only once. */\r
- break;\r
- }\r
- }\r
-\r
- /* Finished with the socket. */\r
- FreeRTOS_closesocket( xDNSSocket );\r
- }\r
-\r
- return ulIPAddress;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer,\r
- const char *pcHostName,\r
- TickType_t uxIdentifier )\r
-{\r
-DNSMessage_t *pxDNSMessageHeader;\r
-uint8_t *pucStart, *pucByte;\r
-DNSTail_t *pxTail;\r
-static const DNSMessage_t xDefaultPartDNSHeader =\r
-{\r
- 0, /* The identifier will be overwritten. */\r
- dnsOUTGOING_FLAGS, /* Flags set for standard query. */\r
- dnsONE_QUESTION, /* One question is being asked. */\r
- 0, /* No replies are included. */\r
- 0, /* No authorities. */\r
- 0 /* No additional authorities. */\r
-};\r
-\r
- /* Copy in the const part of the header. */\r
- memcpy( ( void * ) pucUDPPayloadBuffer, ( void * ) &xDefaultPartDNSHeader, sizeof( xDefaultPartDNSHeader ) );\r
-\r
- /* Write in a unique identifier. */\r
- pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
- pxDNSMessageHeader->usIdentifier = ( uint16_t ) uxIdentifier;\r
-\r
- /* Create the resource record at the end of the header. First\r
- find the end of the header. */\r
- pucStart = pucUDPPayloadBuffer + sizeof( xDefaultPartDNSHeader );\r
-\r
- /* Leave a gap for the first length bytes. */\r
- pucByte = pucStart + 1;\r
-\r
- /* Copy in the host name. */\r
- strcpy( ( char * ) pucByte, pcHostName );\r
-\r
- /* Mark the end of the string. */\r
- pucByte += strlen( pcHostName );\r
- *pucByte = 0x00u;\r
-\r
- /* Walk the string to replace the '.' characters with byte counts.\r
- pucStart holds the address of the byte count. Walking the string\r
- starts after the byte count position. */\r
- pucByte = pucStart;\r
-\r
- do\r
- {\r
- pucByte++;\r
-\r
- while( ( *pucByte != 0x00 ) && ( *pucByte != '.' ) )\r
- {\r
- pucByte++;\r
- }\r
-\r
- /* Fill in the byte count, then move the pucStart pointer up to\r
- the found byte position. */\r
- *pucStart = ( uint8_t ) ( ( uint32_t ) pucByte - ( uint32_t ) pucStart );\r
- ( *pucStart )--;\r
-\r
- pucStart = pucByte;\r
- } while( *pucByte != 0x00 );\r
-\r
- /* Finish off the record. */\r
-\r
- pxTail = ( DNSTail_t * ) ( pucByte + 1 );\r
-\r
- vSetField16( pxTail, DNSTail_t, usType, dnsTYPE_A_HOST ); /* Type A: host */\r
- vSetField16( pxTail, DNSTail_t, usClass, dnsCLASS_IN ); /* 1: Class IN */\r
-\r
- /* Return the total size of the generated message, which is the space from\r
- the last written byte to the beginning of the buffer. */\r
- return ( ( uint32_t ) pucByte - ( uint32_t ) pucUDPPayloadBuffer + 1 ) + sizeof( *pxTail );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )\r
-\r
- static uint8_t * prvReadNameField( uint8_t *pucByte,\r
- size_t uxSourceLen,\r
- char *pcName,\r
- size_t uxDestLen )\r
- {\r
- size_t uxNameLen = 0;\r
- BaseType_t xCount;\r
-\r
- if( 0 == uxSourceLen )\r
- {\r
- return NULL;\r
- }\r
-\r
- /* Determine if the name is the fully coded name, or an offset to the name\r
- elsewhere in the message. */\r
- if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )\r
- {\r
- /* Jump over the two byte offset. */\r
- if( uxSourceLen > sizeof( uint16_t ) )\r
- {\r
- pucByte += sizeof( uint16_t );\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
- }\r
- else\r
- {\r
- /* pucByte points to the full name. Walk over the string. */\r
- while( ( NULL != pucByte ) && ( *pucByte != 0x00u ) && ( uxSourceLen > 1u ) )\r
- {\r
- /* If this is not the first time through the loop, then add a\r
- separator in the output. */\r
- if( ( uxNameLen > 0 ) && ( uxNameLen < ( uxDestLen - 1u ) ) )\r
- {\r
- pcName[ uxNameLen++ ] = '.';\r
- }\r
-\r
- /* Process the first/next sub-string. */\r
- for( xCount = *( pucByte++ ), uxSourceLen--;\r
- xCount-- && uxSourceLen > 1u;\r
- pucByte++, uxSourceLen-- )\r
- {\r
- if( uxNameLen < uxDestLen - 1u )\r
- {\r
- pcName[ uxNameLen++ ] = *( ( char * ) pucByte );\r
- }\r
- else\r
- {\r
- /* DNS name is too big for the provided buffer. */\r
- pucByte = NULL;\r
- break;\r
- }\r
- }\r
- }\r
-\r
- /* Confirm that a fully formed name was found. */\r
- if( NULL != pucByte )\r
- {\r
- if( 0x00 == *pucByte )\r
- {\r
- pucByte++;\r
- uxSourceLen--;\r
- pcName[ uxNameLen++ ] = '\0';\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
- }\r
- }\r
-\r
- return pucByte;\r
- }\r
-#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */\r
-/*-----------------------------------------------------------*/\r
-\r
-static uint8_t * prvSkipNameField( uint8_t *pucByte,\r
- size_t uxSourceLen )\r
-{\r
-size_t uxChunkLength;\r
-\r
- if( 0u == uxSourceLen )\r
- {\r
- return NULL;\r
- }\r
-\r
- /* Determine if the name is the fully coded name, or an offset to the name\r
- elsewhere in the message. */\r
- if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )\r
- {\r
- /* Jump over the two byte offset. */\r
- if( uxSourceLen > sizeof( uint16_t ) )\r
- {\r
- pucByte += sizeof( uint16_t );\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
- }\r
- else\r
- {\r
- /* pucByte points to the full name. Walk over the string. */\r
- while( ( *pucByte != 0x00u ) && ( uxSourceLen > 1u ) )\r
- {\r
- uxChunkLength = *pucByte + 1u;\r
-\r
- if( uxSourceLen > uxChunkLength )\r
- {\r
- uxSourceLen -= uxChunkLength;\r
- pucByte += uxChunkLength;\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- break;\r
- }\r
- }\r
-\r
- /* Confirm that a fully formed name was found. */\r
- if( NULL != pucByte )\r
- {\r
- if( 0x00u == *pucByte )\r
- {\r
- pucByte++;\r
- }\r
- else\r
- {\r
- pucByte = NULL;\r
- }\r
- }\r
- }\r
-\r
- return pucByte;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-/* The function below will only be called :\r
-when ipconfigDNS_USE_CALLBACKS == 1\r
-when ipconfigUSE_LLMNR == 1\r
-for testing purposes, by the module iot_test_freertos_tcp.c\r
-*/\r
-uint32_t ulDNSHandlePacket( NetworkBufferDescriptor_t *pxNetworkBuffer )\r
-{\r
-DNSMessage_t *pxDNSMessageHeader;\r
-size_t uxPayloadSize;\r
-\r
- /* Only proceed if the payload length indicated in the header\r
- appears to be valid. */\r
- if( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )\r
- {\r
- uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );\r
-\r
- if( uxPayloadSize >= sizeof( DNSMessage_t ) )\r
- {\r
- pxDNSMessageHeader =\r
- ( DNSMessage_t * ) ( pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t ) );\r
-\r
- /* The parameter pdFALSE indicates that the reply was not expected. */\r
- prvParseDNSReply( ( uint8_t * ) pxDNSMessageHeader,\r
- uxPayloadSize,\r
- pdFALSE );\r
- }\r
- }\r
-\r
- /* The packet was not consumed. */\r
- return pdFAIL;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ipconfigUSE_NBNS == 1 )\r
-\r
- uint32_t ulNBNSHandlePacket( NetworkBufferDescriptor_t * pxNetworkBuffer )\r
- {\r
- UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;\r
- uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );\r
- size_t uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );\r
-\r
- /* The network buffer data length has already been set to the\r
- length of the UDP payload. */\r
- prvTreatNBNS( pucUDPPayloadBuffer,\r
- uxPayloadSize,\r
- pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
-\r
- /* The packet was not consumed. */\r
- return pdFAIL;\r
- }\r
-\r
-#endif /* ipconfigUSE_NBNS */\r
-/*-----------------------------------------------------------*/\r
-\r
-static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer,\r
- size_t uxBufferLength,\r
- BaseType_t xExpected )\r
-{\r
-DNSMessage_t *pxDNSMessageHeader;\r
-DNSAnswerRecord_t *pxDNSAnswerRecord;\r
-uint32_t ulIPAddress = 0uL;\r
-#if( ipconfigUSE_LLMNR == 1 )\r
- char *pcRequestedName = NULL;\r
-#endif\r
-uint8_t *pucByte;\r
-size_t uxSourceBytesRemaining;\r
-uint16_t x, usDataLength, usQuestions;\r
-BaseType_t xDoStore = xExpected;\r
-#if( ipconfigUSE_LLMNR == 1 )\r
- uint16_t usType = 0, usClass = 0;\r
-#endif\r
-#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )\r
- char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = "";\r
-#endif\r
-\r
- /* Ensure that the buffer is of at least minimal DNS message length. */\r
- if( uxBufferLength < sizeof( DNSMessage_t ) )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
-\r
- uxSourceBytesRemaining = uxBufferLength;\r
-\r
- /* Parse the DNS message header. */\r
- pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
-\r
- /* Introduce a do {} while (0) to allow the use of breaks. */\r
- do\r
- {\r
- /* Start at the first byte after the header. */\r
- pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t );\r
- uxSourceBytesRemaining -= sizeof( DNSMessage_t );\r
-\r
- /* Skip any question records. */\r
- usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );\r
-\r
- for( x = 0; x < usQuestions; x++ )\r
- {\r
- #if( ipconfigUSE_LLMNR == 1 )\r
- {\r
- if( x == 0 )\r
- {\r
- pcRequestedName = ( char * ) pucByte;\r
- }\r
- }\r
- #endif\r
-\r
-#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )\r
- if( x == 0 )\r
- {\r
- pucByte = prvReadNameField( pucByte,\r
- uxSourceBytesRemaining,\r
- pcName,\r
- sizeof( pcName ) );\r
-\r
- /* Check for a malformed response. */\r
- if( NULL == pucByte )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
-\r
- uxSourceBytesRemaining = ( pucUDPPayloadBuffer + uxBufferLength ) - pucByte;\r
- }\r
- else\r
-#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */\r
- {\r
- /* Skip the variable length pcName field. */\r
- pucByte = prvSkipNameField( pucByte,\r
- uxSourceBytesRemaining );\r
-\r
- /* Check for a malformed response. */\r
- if( NULL == pucByte )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
-\r
- uxSourceBytesRemaining = ( size_t )\r
- ( pucUDPPayloadBuffer + uxBufferLength - pucByte );\r
- }\r
-\r
- /* Check the remaining buffer size. */\r
- if( uxSourceBytesRemaining >= sizeof( uint32_t ) )\r
- {\r
- #if( ipconfigUSE_LLMNR == 1 )\r
- {\r
- /* usChar2u16 returns value in host endianness. */\r
- usType = usChar2u16( pucByte );\r
- usClass = usChar2u16( pucByte + 2 );\r
- }\r
- #endif /* ipconfigUSE_LLMNR */\r
-\r
- /* Skip the type and class fields. */\r
- pucByte += sizeof( uint32_t );\r
- uxSourceBytesRemaining -= sizeof( uint32_t );\r
- }\r
- else\r
- {\r
- /* Malformed response. */\r
- return dnsPARSE_ERROR;\r
- }\r
- }\r
-\r
- /* Search through the answer records. */\r
- pxDNSMessageHeader->usAnswers = FreeRTOS_ntohs( pxDNSMessageHeader->usAnswers );\r
-\r
- if( ( pxDNSMessageHeader->usFlags & dnsRX_FLAGS_MASK ) == dnsEXPECTED_RX_FLAGS )\r
- {\r
- for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )\r
- {\r
- pucByte = prvSkipNameField( pucByte,\r
- uxSourceBytesRemaining );\r
-\r
- /* Check for a malformed response. */\r
- if( NULL == pucByte )\r
- {\r
- return dnsPARSE_ERROR;\r
- }\r
-\r
- uxSourceBytesRemaining = ( size_t )\r
- ( pucUDPPayloadBuffer + uxBufferLength - pucByte );\r
-\r
- /* Is there enough data for an IPv4 A record answer and, if so,\r
- is this an A record? */\r
- if( ( uxSourceBytesRemaining >= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) ) ) &&\r
- ( usChar2u16( pucByte ) == dnsTYPE_A_HOST ) )\r
- {\r
- /* This is the required record type and is of sufficient size. */\r
- pxDNSAnswerRecord = ( DNSAnswerRecord_t * ) pucByte;\r
-\r
- /* Sanity check the data length of an IPv4 answer. */\r
- if( FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ) == sizeof( uint32_t ) )\r
- {\r
- /* Copy the IP address out of the record. */\r
- memcpy( &ulIPAddress,\r
- pucByte + sizeof( DNSAnswerRecord_t ),\r
- sizeof( uint32_t ) );\r
-\r
- #if( ipconfigDNS_USE_CALLBACKS == 1 )\r
- {\r
- /* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */\r
- if( xDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress ) != pdFALSE )\r
- {\r
- /* This device has requested this DNS look-up.\r
- The result may be stored in the DNS cache. */\r
- xDoStore = pdTRUE;\r
- }\r
- }\r
- #endif /* ipconfigDNS_USE_CALLBACKS == 1 */\r
- #if( ipconfigUSE_DNS_CACHE == 1 )\r
- {\r
- /* The reply will only be stored in the DNS cache when the\r
- request was issued by this device. */\r
- if( xDoStore != pdFALSE )\r
- {\r
- prvProcessDNSCache( pcName, &ulIPAddress, pxDNSAnswerRecord->ulTTL, pdFALSE );\r
- }\r
-\r
- /* Show what has happened. */\r
- FreeRTOS_printf( ( "DNS[0x%04X]: The answer to '%s' (%xip) will%s be stored\n",\r
- ( unsigned ) pxDNSMessageHeader->usIdentifier,\r
- pcName,\r
- ( unsigned ) FreeRTOS_ntohl( ulIPAddress ),\r
- ( xDoStore != 0 ) ? "" : " NOT" ) );\r
- }\r
- #endif /* ipconfigUSE_DNS_CACHE */\r
- }\r
-\r
- pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t );\r
- uxSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) );\r
- break;\r
- }\r
- else if( uxSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) )\r
- {\r
- /* It's not an A record, so skip it. Get the header location\r
- and then jump over the header. */\r
- pxDNSAnswerRecord = ( DNSAnswerRecord_t * ) pucByte;\r
- pucByte += sizeof( DNSAnswerRecord_t );\r
- uxSourceBytesRemaining -= sizeof( DNSAnswerRecord_t );\r
-\r
- /* Determine the length of the answer data from the header. */\r
- usDataLength = FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength );\r
-\r
- /* Jump over the answer. */\r
- if( uxSourceBytesRemaining >= usDataLength )\r
- {\r
- pucByte += usDataLength;\r
- uxSourceBytesRemaining -= usDataLength;\r
- }\r
- else\r
- {\r
- /* Malformed response. */\r
- return dnsPARSE_ERROR;\r
- }\r
- }\r
- }\r
- }\r
-\r
-#if( ipconfigUSE_LLMNR == 1 )\r
- else if( usQuestions && ( usType == dnsTYPE_A_HOST ) && ( usClass == dnsCLASS_IN ) )\r
- {\r
- /* If this is not a reply to our DNS request, it might an LLMNR\r
- request. */\r
- if( xApplicationDNSQueryHook( ( pcRequestedName + 1 ) ) )\r
- {\r
- int16_t usLength;\r
- NetworkBufferDescriptor_t *pxNewBuffer = NULL;\r
- NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );\r
- LLMNRAnswer_t *pxAnswer;\r
-\r
- if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )\r
- {\r
- BaseType_t xDataLength = uxBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );\r
-\r
- /* Set the size of the outgoing packet. */\r
- pxNetworkBuffer->xDataLength = xDataLength;\r
- pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + sizeof( LLMNRAnswer_t ) );\r
-\r
- if( pxNewBuffer != NULL )\r
- {\r
- BaseType_t xOffset1, xOffset2;\r
-\r
- xOffset1 = ( BaseType_t ) ( pucByte - pucUDPPayloadBuffer );\r
- xOffset2 = ( BaseType_t ) ( ( ( uint8_t * ) pcRequestedName ) - pucUDPPayloadBuffer );\r
-\r
- pxNetworkBuffer = pxNewBuffer;\r
- pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + ipUDP_PAYLOAD_OFFSET_IPv4;\r
-\r
- pucByte = pucUDPPayloadBuffer + xOffset1;\r
- pcRequestedName = ( char * ) ( pucUDPPayloadBuffer + xOffset2 );\r
- pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
- }\r
- else\r
- {\r
- /* Just to indicate that the message may not be answered. */\r
- pxNetworkBuffer = NULL;\r
- }\r
- }\r
-\r
- if( pxNetworkBuffer != NULL )\r
- {\r
- pxAnswer = ( LLMNRAnswer_t * ) pucByte;\r
-\r
- /* We leave 'usIdentifier' and 'usQuestions' untouched */\r
- vSetField16( pxDNSMessageHeader, DNSMessage_t, usFlags, dnsLLMNR_FLAGS_IS_REPONSE ); /* Set the response flag */\r
- vSetField16( pxDNSMessageHeader, DNSMessage_t, usAnswers, 1 ); /* Provide a single answer */\r
- vSetField16( pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 ); /* No authority */\r
- vSetField16( pxDNSMessageHeader, DNSMessage_t, usAdditionalRRs, 0 ); /* No additional info */\r
-\r
- pxAnswer->ucNameCode = dnsNAME_IS_OFFSET;\r
- pxAnswer->ucNameOffset = ( uint8_t ) ( pcRequestedName - ( char * ) pucUDPPayloadBuffer );\r
-\r
- vSetField16( pxAnswer, LLMNRAnswer_t, usType, dnsTYPE_A_HOST ); /* Type A: host */\r
- vSetField16( pxAnswer, LLMNRAnswer_t, usClass, dnsCLASS_IN ); /* 1: Class IN */\r
- vSetField32( pxAnswer, LLMNRAnswer_t, ulTTL, dnsLLMNR_TTL_VALUE );\r
- vSetField16( pxAnswer, LLMNRAnswer_t, usDataLength, 4 );\r
- vSetField32( pxAnswer, LLMNRAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );\r
-\r
- usLength = ( int16_t ) ( sizeof( *pxAnswer ) + ( size_t ) ( pucByte - pucUDPPayloadBuffer ) );\r
-\r
- prvReplyDNSMessage( pxNetworkBuffer, usLength );\r
-\r
- if( pxNewBuffer != NULL )\r
- {\r
- vReleaseNetworkBufferAndDescriptor( pxNewBuffer );\r
- }\r
- }\r
- }\r
- }\r
-#endif /* ipconfigUSE_LLMNR == 1 */\r
- } while( 0 );\r
-\r
- if( xExpected == pdFALSE )\r
- {\r
- /* Do not return a valid IP-address in case the reply was not expected. */\r
- ulIPAddress = 0uL;\r
- }\r
-\r
- return ulIPAddress;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ipconfigUSE_NBNS == 1 )\r
-\r
- static void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer,\r
- size_t uxBufferLength,\r
- uint32_t ulIPAddress )\r
- {\r
- uint16_t usFlags, usType, usClass;\r
- uint8_t *pucSource, *pucTarget;\r
- uint8_t ucByte;\r
- uint8_t ucNBNSName[ 17 ];\r
-\r
- /* Check for minimum buffer size. */\r
- if( uxBufferLength < sizeof( NBNSRequest_t ) )\r
- {\r
- return;\r
- }\r
-\r
- /* Read the request flags in host endianness. */\r
- usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) );\r
-\r
- if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )\r
- {\r
- usType = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );\r
- usClass = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usClass ) );\r
-\r
- /* Not used for now */\r
- ( void ) usClass;\r
-\r
- /* For NBNS a name is 16 bytes long, written with capitals only.\r
- Make sure that the copy is terminated with a zero. */\r
- pucTarget = ucNBNSName + sizeof( ucNBNSName ) - 2;\r
- pucTarget[ 1 ] = '\0';\r
-\r
- /* Start with decoding the last 2 bytes. */\r
- pucSource = pucUDPPayloadBuffer + ( offsetof( NBNSRequest_t, ucName ) + ( dnsNBNS_ENCODED_NAME_LENGTH - 2 ) );\r
-\r
- for( ;; )\r
- {\r
- ucByte = ( uint8_t ) ( ( ( pucSource[ 0 ] - 0x41 ) << 4 ) | ( pucSource[ 1 ] - 0x41 ) );\r
-\r
- /* Make sure there are no trailing spaces in the name. */\r
- if( ( ucByte == ' ' ) && ( pucTarget[ 1 ] == '\0' ) )\r
- {\r
- ucByte = '\0';\r
- }\r
-\r
- *pucTarget = ucByte;\r
-\r
- if( pucTarget == ucNBNSName )\r
- {\r
- break;\r
- }\r
-\r
- pucTarget -= 1;\r
- pucSource -= 2;\r
- }\r
-\r
- #if( ipconfigUSE_DNS_CACHE == 1 )\r
- {\r
- if( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) != 0 )\r
- {\r
- /* If this is a response from another device,\r
- add the name to the DNS cache */\r
- prvProcessDNSCache( ( char * ) ucNBNSName, &ulIPAddress, 0, pdFALSE );\r
- }\r
- }\r
- #else\r
- {\r
- /* Avoid compiler warnings. */\r
- ( void ) ulIPAddress;\r
- }\r
- #endif /* ipconfigUSE_DNS_CACHE */\r
-\r
- if( ( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) == 0 ) &&\r
- ( usType == dnsNBNS_TYPE_NET_BIOS ) &&\r
- ( xApplicationDNSQueryHook( ( const char * ) ucNBNSName ) != pdFALSE ) )\r
- {\r
- uint16_t usLength;\r
- DNSMessage_t *pxMessage;\r
- NBNSAnswer_t *pxAnswer;\r
-\r
- /* Someone is looking for a device with ucNBNSName,\r
- prepare a positive reply. */\r
- NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );\r
-\r
- if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )\r
- {\r
- NetworkBufferDescriptor_t *pxNewBuffer;\r
-\r
- /* The field xDataLength was set to the total length of the UDP packet,\r
- i.e. the payload size plus sizeof( UDPPacket_t ). */\r
- pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, pxNetworkBuffer->xDataLength + sizeof( NBNSAnswer_t ) );\r
-\r
- if( pxNewBuffer != NULL )\r
- {\r
- pucUDPPayloadBuffer = pxNewBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );\r
- pxNetworkBuffer = pxNewBuffer;\r
- }\r
- else\r
- {\r
- /* Just prevent that a reply will be sent */\r
- pxNetworkBuffer = NULL;\r
- }\r
- }\r
-\r
- /* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */\r
- if( pxNetworkBuffer != NULL )\r
- {\r
- pxMessage = ( DNSMessage_t * ) pucUDPPayloadBuffer;\r
-\r
- /* As the fields in the structures are not word-aligned, we have to\r
- copy the values byte-by-byte using macro's vSetField16() and vSetField32() */\r
- vSetField16( pxMessage, DNSMessage_t, usFlags, dnsNBNS_QUERY_RESPONSE_FLAGS ); /* 0x8500 */\r
- vSetField16( pxMessage, DNSMessage_t, usQuestions, 0 );\r
- vSetField16( pxMessage, DNSMessage_t, usAnswers, 1 );\r
- vSetField16( pxMessage, DNSMessage_t, usAuthorityRRs, 0 );\r
- vSetField16( pxMessage, DNSMessage_t, usAdditionalRRs, 0 );\r
-\r
- pxAnswer = ( NBNSAnswer_t * ) ( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );\r
-\r
- vSetField16( pxAnswer, NBNSAnswer_t, usType, usType ); /* Type */\r
- vSetField16( pxAnswer, NBNSAnswer_t, usClass, dnsNBNS_CLASS_IN ); /* Class */\r
- vSetField32( pxAnswer, NBNSAnswer_t, ulTTL, dnsNBNS_TTL_VALUE );\r
- vSetField16( pxAnswer, NBNSAnswer_t, usDataLength, 6 ); /* 6 bytes including the length field */\r
- vSetField16( pxAnswer, NBNSAnswer_t, usNbFlags, dnsNBNS_NAME_FLAGS );\r
- vSetField32( pxAnswer, NBNSAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );\r
-\r
- usLength = ( uint16_t ) ( offsetof( NBNSRequest_t, usType ) + sizeof( NBNSAnswer_t ) );\r
-\r
- prvReplyDNSMessage( pxNetworkBuffer, usLength );\r
- }\r
- }\r
- }\r
- }\r
-\r
-#endif /* ipconfigUSE_NBNS */\r
-/*-----------------------------------------------------------*/\r
-\r
-static Socket_t prvCreateDNSSocket( void )\r
-{\r
-Socket_t xSocket = NULL;\r
-struct freertos_sockaddr xAddress;\r
-BaseType_t xReturn;\r
-\r
- /* This must be the first time this function has been called. Create\r
- the socket. */\r
- xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
-\r
- /* Auto bind the port. */\r
- xAddress.sin_port = 0u;\r
- xReturn = FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );\r
-\r
- /* Check the bind was successful, and clean up if not. */\r
- if( xReturn != 0 )\r
- {\r
- FreeRTOS_closesocket( xSocket );\r
- xSocket = NULL;\r
- }\r
- else\r
- {\r
- /* The send and receive timeouts will be set later on. */\r
- }\r
-\r
- return xSocket;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )\r
-\r
- static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer,\r
- BaseType_t lNetLength )\r
- {\r
- UDPPacket_t *pxUDPPacket;\r
- IPHeader_t *pxIPHeader;\r
- UDPHeader_t *pxUDPHeader;\r
-\r
- pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;\r
- pxIPHeader = &pxUDPPacket->xIPHeader;\r
- pxUDPHeader = &pxUDPPacket->xUDPHeader;\r
- /* HT: started using defines like 'ipSIZE_OF_xxx' */\r
- pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );\r
- /* HT:endian: should not be translated, copying from packet to packet */\r
- pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;\r
- pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;\r
- pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;\r
- pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );\r
- usPacketIdentifier++;\r
- pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );\r
- vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort );\r
-\r
- /* Important: tell NIC driver how many bytes must be sent */\r
- pxNetworkBuffer->xDataLength = ( size_t ) ( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER );\r
-\r
- #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )\r
- {\r
- /* calculate the IP header checksum */\r
- pxIPHeader->usHeaderChecksum = 0x00;\r
- pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0uL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );\r
- pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );\r
-\r
- /* calculate the UDP checksum for outgoing package */\r
- usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, pxNetworkBuffer->xDataLength, pdTRUE );\r
- }\r
- #endif\r
-\r
- /* This function will fill in the eth addresses and send the packet */\r
- vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );\r
- }\r
-\r
-#endif /* ipconfigUSE_NBNS == 1 || ipconfigUSE_LLMNR == 1 */\r
-/*-----------------------------------------------------------*/\r
-\r
-#if( ipconfigUSE_DNS_CACHE == 1 )\r
-\r
- static void prvProcessDNSCache( const char *pcName,\r
- uint32_t *pulIP,\r
- uint32_t ulTTL,\r
- BaseType_t xLookUp )\r
- {\r
- BaseType_t x;\r
- BaseType_t xFound = pdFALSE;\r
- uint32_t ulCurrentTimeSeconds = ( xTaskGetTickCount() / portTICK_PERIOD_MS ) / 1000;\r
- static BaseType_t xFreeEntry = 0;\r
- configASSERT(pcName);\r
-\r
- /* For each entry in the DNS cache table. */\r
- for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )\r
- {\r
- if( xDNSCache[ x ].pcName[ 0 ] == 0 )\r
- {\r
- continue;\r
- }\r
-\r
- if( 0 == strcmp( xDNSCache[ x ].pcName, pcName ) )\r
- {\r
- /* Is this function called for a lookup or to add/update an IP address? */\r
- if( xLookUp != pdFALSE )\r
- {\r
- /* Confirm that the record is still fresh. */\r
- if( ulCurrentTimeSeconds < ( xDNSCache[ x ].ulTimeWhenAddedInSeconds + FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) ) )\r
- {\r
- *pulIP = xDNSCache[ x ].ulIPAddress;\r
- }\r
- else\r
- {\r
- /* Age out the old cached record. */\r
- xDNSCache[ x ].pcName[ 0 ] = 0;\r
- }\r
- }\r
- else\r
- {\r
- xDNSCache[ x ].ulIPAddress = *pulIP;\r
- xDNSCache[ x ].ulTTL = ulTTL;\r
- xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;\r
- }\r
-\r
- xFound = pdTRUE;\r
- break;\r
- }\r
- }\r
-\r
- if( xFound == pdFALSE )\r
- {\r
- if( xLookUp != pdFALSE )\r
- {\r
- *pulIP = 0;\r
- }\r
- else\r
- {\r
- /* Add or update the item. */\r
- if( strlen( pcName ) < ipconfigDNS_CACHE_NAME_LENGTH )\r
- {\r
- strcpy( xDNSCache[ xFreeEntry ].pcName, pcName );\r
-\r
- xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP;\r
- xDNSCache[ xFreeEntry ].ulTTL = ulTTL;\r
- xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;\r
-\r
- xFreeEntry++;\r
-\r
- if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )\r
- {\r
- xFreeEntry = 0;\r
- }\r
- }\r
- }\r
- }\r
-\r
- if( ( xLookUp == 0 ) || ( *pulIP != 0 ) )\r
- {\r
- FreeRTOS_debug_printf( ( "prvProcessDNSCache: %s: '%s' @ %lxip\n", xLookUp ? "look-up" : "add", pcName, FreeRTOS_ntohl( *pulIP ) ) );\r
- }\r
- }\r
-\r
-#endif /* ipconfigUSE_DNS_CACHE */\r
-\r
-#endif /* ipconfigUSE_DNS != 0 */\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/* Provide access to private members for testing. */\r
-#ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS\r
- #include "iot_freertos_tcp_test_access_dns_define.h"\r
-#endif\r
-\r
+/*
+ * FreeRTOS+TCP V2.2.0
+ * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * http://aws.amazon.com/freertos
+ * http://www.FreeRTOS.org
+ */
+
+/* Standard includes. */
+#include <stdint.h>
+
+/* FreeRTOS includes. */
+#include "FreeRTOS.h"
+#include "task.h"
+#include "queue.h"
+#include "list.h"
+#include "semphr.h"
+
+/* FreeRTOS+TCP includes. */
+#include "FreeRTOS_IP.h"
+#include "FreeRTOS_Sockets.h"
+#include "FreeRTOS_IP_Private.h"
+#include "FreeRTOS_UDP_IP.h"
+#include "FreeRTOS_DNS.h"
+#include "NetworkBufferManagement.h"
+#include "NetworkInterface.h"
+#include "IPTraceMacroDefaults.h"
+
+/* Exclude the entire file if DNS is not enabled. */
+#if( ipconfigUSE_DNS != 0 )
+
+#if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
+ #define dnsDNS_PORT 0x3500u
+ #define dnsONE_QUESTION 0x0100u
+ #define dnsOUTGOING_FLAGS 0x0001u /* Standard query. */
+ #define dnsRX_FLAGS_MASK 0x0f80u /* The bits of interest in the flags field of incoming DNS messages. */
+ #define dnsEXPECTED_RX_FLAGS 0x0080u /* Should be a response, without any errors. */
+#else
+ #define dnsDNS_PORT 0x0035u
+ #define dnsONE_QUESTION 0x0001u
+ #define dnsOUTGOING_FLAGS 0x0100u /* Standard query. */
+ #define dnsRX_FLAGS_MASK 0x800fu /* The bits of interest in the flags field of incoming DNS messages. */
+ #define dnsEXPECTED_RX_FLAGS 0x8000u /* Should be a response, without any errors. */
+
+#endif /* ipconfigBYTE_ORDER */
+
+/* The maximum number of times a DNS request should be sent out if a response
+is not received, before giving up. */
+#ifndef ipconfigDNS_REQUEST_ATTEMPTS
+ #define ipconfigDNS_REQUEST_ATTEMPTS 5
+#endif
+
+/* If the top two bits in the first character of a name field are set then the
+name field is an offset to the string, rather than the string itself. */
+#define dnsNAME_IS_OFFSET ( ( uint8_t ) 0xc0 )
+
+/* NBNS flags. */
+#define dnsNBNS_FLAGS_RESPONSE 0x8000u
+#define dnsNBNS_FLAGS_OPCODE_MASK 0x7800u
+#define dnsNBNS_FLAGS_OPCODE_QUERY 0x0000u
+#define dnsNBNS_FLAGS_OPCODE_REGISTRATION 0x2800u
+
+/* Host types. */
+#define dnsTYPE_A_HOST 0x01u
+#define dnsCLASS_IN 0x01u
+
+/* LLMNR constants. */
+#define dnsLLMNR_TTL_VALUE 300000uL
+#define dnsLLMNR_FLAGS_IS_REPONSE 0x8000u
+
+/* NBNS constants. */
+#define dnsNBNS_TTL_VALUE 3600uL /* 1 hour valid */
+#define dnsNBNS_TYPE_NET_BIOS 0x0020u
+#define dnsNBNS_CLASS_IN 0x01u
+#define dnsNBNS_NAME_FLAGS 0x6000u
+#define dnsNBNS_ENCODED_NAME_LENGTH 32
+
+/* If the queried NBNS name matches with the device's name,
+the query will be responded to with these flags: */
+#define dnsNBNS_QUERY_RESPONSE_FLAGS ( 0x8500u )
+
+/* Flag DNS parsing errors in situations where an IPv4 address is the return
+type. */
+#define dnsPARSE_ERROR 0uL
+
+/*
+ * Create a socket and bind it to the standard DNS port number. Return the
+ * the created socket - or NULL if the socket could not be created or bound.
+ */
+static Socket_t prvCreateDNSSocket( void );
+
+/*
+ * Create the DNS message in the zero copy buffer passed in the first parameter.
+ */
+static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer,
+ const char *pcHostName,
+ TickType_t uxIdentifier );
+
+/*
+ * Simple routine that jumps over the NAME field of a resource record.
+ */
+static uint8_t * prvSkipNameField( uint8_t *pucByte,
+ size_t uxSourceLen );
+
+/*
+ * Process a response packet from a DNS server.
+ * The parameter 'xExpected' indicates whether the identifier in the reply
+ * was expected, and thus if the DNS cache may be updated with the reply.
+ */
+static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer,
+ size_t uxBufferLength,
+ BaseType_t xExpected );
+
+/*
+ * Prepare and send a message to a DNS server. 'uxReadTimeOut_ticks' will be passed as
+ * zero, in case the user has supplied a call-back function.
+ */
+static uint32_t prvGetHostByName( const char *pcHostName,
+ TickType_t uxIdentifier,
+ TickType_t uxReadTimeOut_ticks );
+
+/*
+ * The NBNS and the LLMNR protocol share this reply function.
+ */
+#if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
+ static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer,
+ BaseType_t lNetLength );
+#endif
+
+#if( ipconfigUSE_NBNS == 1 )
+ static portINLINE void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer,
+ size_t uxBufferLength,
+ uint32_t ulIPAddress );
+#endif /* ipconfigUSE_NBNS */
+
+
+#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
+ static uint8_t * prvReadNameField( uint8_t *pucByte,
+ size_t uxSourceLen,
+ char *pcName,
+ size_t uxLen );
+#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
+
+#if( ipconfigUSE_DNS_CACHE == 1 )
+ static void prvProcessDNSCache( const char *pcName,
+ uint32_t *pulIP,
+ uint32_t ulTTL,
+ BaseType_t xLookUp );
+
+ typedef struct xDNS_CACHE_TABLE_ROW
+ {
+ uint32_t ulIPAddress; /* The IP address of an ARP cache entry. */
+ char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ]; /* The name of the host */
+ uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */
+ uint32_t ulTimeWhenAddedInSeconds;
+ } DNSCacheRow_t;
+
+ static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];
+
+ void FreeRTOS_dnsclear()
+ {
+ memset( xDNSCache, 0x0, sizeof( xDNSCache ) );
+ }
+#endif /* ipconfigUSE_DNS_CACHE == 1 */
+
+#if( ipconfigUSE_LLMNR == 1 )
+ const MACAddress_t xLLMNR_MacAdress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
+#endif /* ipconfigUSE_LLMNR == 1 */
+
+/*-----------------------------------------------------------*/
+
+#include "pack_struct_start.h"
+struct xDNSMessage
+{
+ uint16_t usIdentifier;
+ uint16_t usFlags;
+ uint16_t usQuestions;
+ uint16_t usAnswers;
+ uint16_t usAuthorityRRs;
+ uint16_t usAdditionalRRs;
+}
+#include "pack_struct_end.h"
+typedef struct xDNSMessage DNSMessage_t;
+
+/* A DNS query consists of a header, as described in 'struct xDNSMessage'
+It is followed by 1 or more queries, each one consisting of a name and a tail,
+with two fields: type and class
+*/
+#include "pack_struct_start.h"
+struct xDNSTail
+{
+ uint16_t usType;
+ uint16_t usClass;
+}
+#include "pack_struct_end.h"
+typedef struct xDNSTail DNSTail_t;
+
+/* DNS answer record header. */
+#include "pack_struct_start.h"
+struct xDNSAnswerRecord
+{
+ uint16_t usType;
+ uint16_t usClass;
+ uint32_t ulTTL;
+ uint16_t usDataLength;
+}
+#include "pack_struct_end.h"
+typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
+
+#if( ipconfigUSE_LLMNR == 1 )
+
+ #include "pack_struct_start.h"
+ struct xLLMNRAnswer
+ {
+ uint8_t ucNameCode;
+ uint8_t ucNameOffset; /* The name is not repeated in the answer, only the offset is given with "0xc0 <offs>" */
+ uint16_t usType;
+ uint16_t usClass;
+ uint32_t ulTTL;
+ uint16_t usDataLength;
+ uint32_t ulIPAddress;
+ }
+ #include "pack_struct_end.h"
+ typedef struct xLLMNRAnswer LLMNRAnswer_t;
+
+#endif /* ipconfigUSE_LLMNR == 1 */
+
+#if( ipconfigUSE_NBNS == 1 )
+
+ #include "pack_struct_start.h"
+ struct xNBNSRequest
+ {
+ uint16_t usRequestId;
+ uint16_t usFlags;
+ uint16_t ulRequestCount;
+ uint16_t usAnswerRSS;
+ uint16_t usAuthRSS;
+ uint16_t usAdditionalRSS;
+ uint8_t ucNameSpace;
+ uint8_t ucName[ dnsNBNS_ENCODED_NAME_LENGTH ];
+ uint8_t ucNameZero;
+ uint16_t usType;
+ uint16_t usClass;
+ }
+ #include "pack_struct_end.h"
+ typedef struct xNBNSRequest NBNSRequest_t;
+
+ #include "pack_struct_start.h"
+ struct xNBNSAnswer
+ {
+ uint16_t usType;
+ uint16_t usClass;
+ uint32_t ulTTL;
+ uint16_t usDataLength;
+ uint16_t usNbFlags; /* NetBIOS flags 0x6000 : IP-address, big-endian */
+ uint32_t ulIPAddress;
+ }
+ #include "pack_struct_end.h"
+ typedef struct xNBNSAnswer NBNSAnswer_t;
+
+ #endif /* ipconfigUSE_NBNS == 1 */
+
+/*-----------------------------------------------------------*/
+
+#if( ipconfigUSE_DNS_CACHE == 1 )
+ uint32_t FreeRTOS_dnslookup( const char *pcHostName )
+ {
+ uint32_t ulIPAddress = 0uL;
+
+ prvProcessDNSCache( pcHostName, &ulIPAddress, 0, pdTRUE );
+ return ulIPAddress;
+ }
+#endif /* ipconfigUSE_DNS_CACHE == 1 */
+/*-----------------------------------------------------------*/
+
+#if( ipconfigDNS_USE_CALLBACKS == 1 )
+
+ typedef struct xDNS_Callback
+ {
+ TickType_t uxRemaningTime; /* Timeout in ms */
+ FOnDNSEvent pCallbackFunction; /* Function to be called when the address has been found or when a timeout has beeen reached */
+ TimeOut_t uxTimeoutState;
+ void *pvSearchID;
+ struct xLIST_ITEM xListItem;
+ char pcName[ 1 ];
+ } DNSCallback_t;
+
+ static List_t xCallbackList;
+
+ /* Define FreeRTOS_gethostbyname() as a normal blocking call. */
+ uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
+ {
+ return FreeRTOS_gethostbyname_a( pcHostName, ( FOnDNSEvent ) NULL, ( void * ) NULL, 0 );
+ }
+ /*-----------------------------------------------------------*/
+
+ /* Initialise the list of call-back structures. */
+ void vDNSInitialise( void );
+ void vDNSInitialise( void )
+ {
+ vListInitialise( &xCallbackList );
+ }
+ /*-----------------------------------------------------------*/
+
+ /* Iterate through the list of call-back structures and remove
+ old entries which have reached a timeout.
+ As soon as the list hase become empty, the DNS timer will be stopped
+ In case pvSearchID is supplied, the user wants to cancel a DNS request
+ */
+ void vDNSCheckCallBack( void *pvSearchID );
+ void vDNSCheckCallBack( void *pvSearchID )
+ {
+ const ListItem_t *pxIterator;
+ const MiniListItem_t * xEnd = ( const MiniListItem_t * ) listGET_END_MARKER( &xCallbackList );
+
+ vTaskSuspendAll();
+ {
+ for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
+ pxIterator != ( const ListItem_t * ) xEnd;
+ )
+ {
+ DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
+
+ /* Move to the next item because we might remove this item */
+ pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );
+
+ if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )
+ {
+ uxListRemove( &pxCallback->xListItem );
+ vPortFree( pxCallback );
+ }
+ else if( xTaskCheckForTimeOut( &pxCallback->uxTimeoutState, &pxCallback->uxRemaningTime ) != pdFALSE )
+ {
+ pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );
+ uxListRemove( &pxCallback->xListItem );
+ vPortFree( ( void * ) pxCallback );
+ }
+ }
+ }
+ xTaskResumeAll();
+
+ if( listLIST_IS_EMPTY( &xCallbackList ) )
+ {
+ vIPSetDnsTimerEnableState( pdFALSE );
+ }
+ }
+ /*-----------------------------------------------------------*/
+
+ void FreeRTOS_gethostbyname_cancel( void *pvSearchID )
+ {
+ /* _HT_ Should better become a new API call to have the IP-task remove the callback */
+ vDNSCheckCallBack( pvSearchID );
+ }
+ /*-----------------------------------------------------------*/
+
+ /* FreeRTOS_gethostbyname_a() was called along with callback parameters.
+ Store them in a list for later reference. */
+ static void vDNSSetCallBack( const char *pcHostName,
+ void *pvSearchID,
+ FOnDNSEvent pCallbackFunction,
+ TickType_t uxTimeout,
+ TickType_t uxIdentifier );
+ static void vDNSSetCallBack( const char *pcHostName,
+ void *pvSearchID,
+ FOnDNSEvent pCallbackFunction,
+ TickType_t uxTimeout,
+ TickType_t uxIdentifier )
+ {
+ size_t lLength = strlen( pcHostName );
+ DNSCallback_t *pxCallback = ( DNSCallback_t * ) pvPortMalloc( sizeof( *pxCallback ) + lLength );
+
+ /* Translate from ms to number of clock ticks. */
+ uxTimeout /= portTICK_PERIOD_MS;
+
+ if( pxCallback != NULL )
+ {
+ if( listLIST_IS_EMPTY( &xCallbackList ) )
+ {
+ /* This is the first one, start the DNS timer to check for timeouts */
+ vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, uxTimeout ) );
+ }
+
+ strcpy( pxCallback->pcName, pcHostName );
+ pxCallback->pCallbackFunction = pCallbackFunction;
+ pxCallback->pvSearchID = pvSearchID;
+ pxCallback->uxRemaningTime = uxTimeout;
+ vTaskSetTimeOutState( &pxCallback->uxTimeoutState );
+ listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void * ) pxCallback );
+ listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), uxIdentifier );
+ vTaskSuspendAll();
+ {
+ vListInsertEnd( &xCallbackList, &pxCallback->xListItem );
+ }
+ xTaskResumeAll();
+ }
+ }
+ /*-----------------------------------------------------------*/
+
+ /* A DNS reply was received, see if there is any matching entry and
+ call the handler. Returns pdTRUE if uxIdentifier was recognised. */
+ static BaseType_t xDNSDoCallback( TickType_t uxIdentifier,
+ const char *pcName,
+ uint32_t ulIPAddress );
+ static BaseType_t xDNSDoCallback( TickType_t uxIdentifier,
+ const char *pcName,
+ uint32_t ulIPAddress )
+ {
+ BaseType_t xResult = pdFALSE;
+ const ListItem_t *pxIterator;
+ const MiniListItem_t * xEnd = ( const MiniListItem_t * ) listGET_END_MARKER( &xCallbackList );
+
+ vTaskSuspendAll();
+ {
+ for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
+ pxIterator != ( const ListItem_t * ) xEnd;
+ pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
+ {
+ /* The cast will take away the 'configLIST_VOLATILE' */
+ if( uxIdentifier == ( TickType_t ) listGET_LIST_ITEM_VALUE( pxIterator ) )
+ {
+ DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
+
+ pxCallback->pCallbackFunction( pcName, pxCallback->pvSearchID, ulIPAddress );
+ uxListRemove( &pxCallback->xListItem );
+ vPortFree( pxCallback );
+
+ if( listLIST_IS_EMPTY( &xCallbackList ) )
+ {
+ /* The list of outstanding requests is empty. No need for periodic polling. */
+ vIPSetDnsTimerEnableState( pdFALSE );
+ }
+
+ xResult = pdTRUE;
+ break;
+ }
+ }
+ }
+ xTaskResumeAll();
+ return xResult;
+ }
+
+#endif /* ipconfigDNS_USE_CALLBACKS == 1 */
+/*-----------------------------------------------------------*/
+
+#if( ipconfigDNS_USE_CALLBACKS == 0 )
+ uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
+#else
+ uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName,
+ FOnDNSEvent pCallback,
+ void *pvSearchID,
+ TickType_t uxTimeout )
+#endif
+{
+uint32_t ulIPAddress = 0uL;
+TickType_t uxReadTimeOut_ticks = ipconfigDNS_RECEIVE_BLOCK_TIME_TICKS;
+TickType_t uxIdentifier = 0u;
+BaseType_t xHasRandom = pdFALSE;
+
+ if( pcHostName != NULL )
+ {
+ /* If the supplied hostname is IP address, convert it to uint32_t
+ and return. */
+ #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 )
+ {
+ ulIPAddress = FreeRTOS_inet_addr( pcHostName );
+ }
+ #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */
+
+ /* If a DNS cache is used then check the cache before issuing another DNS
+ request. */
+ #if( ipconfigUSE_DNS_CACHE == 1 )
+ {
+ if( ulIPAddress == 0uL )
+ {
+ ulIPAddress = FreeRTOS_dnslookup( pcHostName );
+
+ if( ulIPAddress != 0 )
+ {
+ FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );
+ }
+ else
+ {
+ /* prvGetHostByName will be called to start a DNS lookup. */
+ }
+ }
+ }
+ #endif /* ipconfigUSE_DNS_CACHE == 1 */
+
+ /* Generate a unique identifier. */
+ if( ulIPAddress == 0uL )
+ {
+ uint32_t ulNumber;
+
+ xHasRandom = xApplicationGetRandomNumber( &( ulNumber ) );
+ /* DNS identifiers are 16-bit. */
+ uxIdentifier = ( TickType_t ) ( ulNumber & 0xffffu );
+ /* ipconfigRAND32() may not return a non-zero value. */
+ }
+
+ #if( ipconfigDNS_USE_CALLBACKS == 1 )
+ {
+ if( pCallback != NULL )
+ {
+ if( ulIPAddress == 0uL )
+ {
+ /* The user has provided a callback function, so do not block on recvfrom() */
+ if( xHasRandom != pdFALSE )
+ {
+ uxReadTimeOut_ticks = 0u;
+ vDNSSetCallBack( pcHostName, pvSearchID, pCallback, uxTimeout, uxIdentifier );
+ }
+ }
+ else
+ {
+ /* The IP address is known, do the call-back now. */
+ pCallback( pcHostName, pvSearchID, ulIPAddress );
+ }
+ }
+ }
+ #endif /* if ( ipconfigDNS_USE_CALLBACKS == 1 ) */
+
+ if( ( ulIPAddress == 0uL ) && ( xHasRandom != pdFALSE ) )
+ {
+ ulIPAddress = prvGetHostByName( pcHostName, uxIdentifier, uxReadTimeOut_ticks );
+ }
+ }
+ return ulIPAddress;
+}
+/*-----------------------------------------------------------*/
+
+static uint32_t prvGetHostByName( const char *pcHostName,
+ TickType_t uxIdentifier,
+ TickType_t uxReadTimeOut_ticks )
+{
+struct freertos_sockaddr xAddress;
+Socket_t xDNSSocket;
+uint32_t ulIPAddress = 0uL;
+uint8_t *pucUDPPayloadBuffer;
+uint32_t ulAddressLength = sizeof( struct freertos_sockaddr );
+BaseType_t xAttempt;
+int32_t lBytes;
+size_t uxPayloadLength, uxExpectedPayloadLength;
+TickType_t uxWriteTimeOut_ticks = ipconfigDNS_SEND_BLOCK_TIME_TICKS;
+
+#if( ipconfigUSE_LLMNR == 1 )
+ BaseType_t bHasDot = pdFALSE;
+#endif /* ipconfigUSE_LLMNR == 1 */
+
+ /* If LLMNR is being used then determine if the host name includes a '.' -
+ if not then LLMNR can be used as the lookup method. */
+ #if( ipconfigUSE_LLMNR == 1 )
+ {
+ const char *pucPtr;
+
+ for( pucPtr = pcHostName; *pucPtr; pucPtr++ )
+ {
+ if( *pucPtr == '.' )
+ {
+ bHasDot = pdTRUE;
+ break;
+ }
+ }
+ }
+ #endif /* ipconfigUSE_LLMNR == 1 */
+
+ /* Two is added at the end for the count of characters in the first
+ subdomain part and the string end byte. */
+ uxExpectedPayloadLength = sizeof( DNSMessage_t ) + strlen( pcHostName ) + sizeof( uint16_t ) + sizeof( uint16_t ) + 2u;
+
+ xDNSSocket = prvCreateDNSSocket();
+
+ if( xDNSSocket != NULL )
+ {
+ FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &uxWriteTimeOut_ticks, sizeof( TickType_t ) );
+ FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &uxReadTimeOut_ticks, sizeof( TickType_t ) );
+
+ for( xAttempt = 0; xAttempt < ipconfigDNS_REQUEST_ATTEMPTS; xAttempt++ )
+ {
+ /* Get a buffer. This uses a maximum delay, but the delay will be
+ capped to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value
+ still needs to be tested. */
+ pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( uxExpectedPayloadLength, portMAX_DELAY );
+
+ if( pucUDPPayloadBuffer != NULL )
+ {
+ /* Create the message in the obtained buffer. */
+ uxPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, uxIdentifier );
+
+ iptraceSENDING_DNS_REQUEST();
+
+ /* Obtain the DNS server address. */
+ FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulIPAddress );
+
+ /* Send the DNS message. */
+#if( ipconfigUSE_LLMNR == 1 )
+ if( bHasDot == pdFALSE )
+ {
+ /* Use LLMNR addressing. */
+ ( ( DNSMessage_t * ) pucUDPPayloadBuffer )->usFlags = 0;
+ xAddress.sin_addr = ipLLMNR_IP_ADDR; /* Is in network byte order. */
+ xAddress.sin_port = FreeRTOS_ntohs( ipLLMNR_PORT );
+ }
+ else
+#endif
+ {
+ /* Use DNS server. */
+ xAddress.sin_addr = ulIPAddress;
+ xAddress.sin_port = dnsDNS_PORT;
+ }
+
+ ulIPAddress = 0uL;
+
+ if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, uxPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )
+ {
+ /* Wait for the reply. */
+ lBytes = FreeRTOS_recvfrom( xDNSSocket, &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xAddress, &ulAddressLength );
+
+ if( lBytes > 0 )
+ {
+ BaseType_t xExpected;
+ DNSMessage_t *pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
+
+ /* See if the identifiers match. */
+ if( uxIdentifier == ( TickType_t ) pxDNSMessageHeader->usIdentifier )
+ {
+ xExpected = pdTRUE;
+ }
+ else
+ {
+ /* The reply was not expected. */
+ xExpected = pdFALSE;
+ }
+
+ /* The reply was received. Process it. */
+ #if( ipconfigDNS_USE_CALLBACKS == 0 )
+ /* It is useless to analyse the unexpected reply
+ unless asynchronous look-ups are enabled. */
+ if( xExpected != pdFALSE )
+ #endif /* ipconfigDNS_USE_CALLBACKS == 0 */
+ {
+ ulIPAddress = prvParseDNSReply( pucUDPPayloadBuffer, ( size_t ) lBytes, xExpected );
+ }
+
+ /* Finished with the buffer. The zero copy interface
+ is being used, so the buffer must be freed by the
+ task. */
+ FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
+
+ if( ulIPAddress != 0uL )
+ {
+ /* All done. */
+ break;
+ }
+ }
+ }
+ else
+ {
+ /* The message was not sent so the stack will not be
+ releasing the zero copy - it must be released here. */
+ FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
+ }
+ }
+
+ if( uxReadTimeOut_ticks == 0u )
+ {
+ /* This DNS lookup is asynchronous, using a call-back:
+ send the request only once. */
+ break;
+ }
+ }
+
+ /* Finished with the socket. */
+ FreeRTOS_closesocket( xDNSSocket );
+ }
+
+ return ulIPAddress;
+}
+/*-----------------------------------------------------------*/
+
+static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer,
+ const char *pcHostName,
+ TickType_t uxIdentifier )
+{
+DNSMessage_t *pxDNSMessageHeader;
+uint8_t *pucStart, *pucByte;
+DNSTail_t *pxTail;
+static const DNSMessage_t xDefaultPartDNSHeader =
+{
+ 0, /* The identifier will be overwritten. */
+ dnsOUTGOING_FLAGS, /* Flags set for standard query. */
+ dnsONE_QUESTION, /* One question is being asked. */
+ 0, /* No replies are included. */
+ 0, /* No authorities. */
+ 0 /* No additional authorities. */
+};
+
+ /* Copy in the const part of the header. */
+ memcpy( ( void * ) pucUDPPayloadBuffer, ( void * ) &xDefaultPartDNSHeader, sizeof( xDefaultPartDNSHeader ) );
+
+ /* Write in a unique identifier. */
+ pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
+ pxDNSMessageHeader->usIdentifier = ( uint16_t ) uxIdentifier;
+
+ /* Create the resource record at the end of the header. First
+ find the end of the header. */
+ pucStart = pucUDPPayloadBuffer + sizeof( xDefaultPartDNSHeader );
+
+ /* Leave a gap for the first length bytes. */
+ pucByte = pucStart + 1;
+
+ /* Copy in the host name. */
+ strcpy( ( char * ) pucByte, pcHostName );
+
+ /* Mark the end of the string. */
+ pucByte += strlen( pcHostName );
+ *pucByte = 0x00u;
+
+ /* Walk the string to replace the '.' characters with byte counts.
+ pucStart holds the address of the byte count. Walking the string
+ starts after the byte count position. */
+ pucByte = pucStart;
+
+ do
+ {
+ pucByte++;
+
+ while( ( *pucByte != 0x00 ) && ( *pucByte != '.' ) )
+ {
+ pucByte++;
+ }
+
+ /* Fill in the byte count, then move the pucStart pointer up to
+ the found byte position. */
+ *pucStart = ( uint8_t ) ( ( uint32_t ) pucByte - ( uint32_t ) pucStart );
+ ( *pucStart )--;
+
+ pucStart = pucByte;
+ } while( *pucByte != 0x00 );
+
+ /* Finish off the record. */
+
+ pxTail = ( DNSTail_t * ) ( pucByte + 1 );
+
+ vSetField16( pxTail, DNSTail_t, usType, dnsTYPE_A_HOST ); /* Type A: host */
+ vSetField16( pxTail, DNSTail_t, usClass, dnsCLASS_IN ); /* 1: Class IN */
+
+ /* Return the total size of the generated message, which is the space from
+ the last written byte to the beginning of the buffer. */
+ return ( ( uint32_t ) pucByte - ( uint32_t ) pucUDPPayloadBuffer + 1 ) + sizeof( *pxTail );
+}
+/*-----------------------------------------------------------*/
+
+#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
+
+ static uint8_t * prvReadNameField( uint8_t *pucByte,
+ size_t uxSourceLen,
+ char *pcName,
+ size_t uxDestLen )
+ {
+ size_t uxNameLen = 0;
+ BaseType_t xCount;
+
+ if( 0 == uxSourceLen )
+ {
+ return NULL;
+ }
+
+ /* Determine if the name is the fully coded name, or an offset to the name
+ elsewhere in the message. */
+ if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
+ {
+ /* Jump over the two byte offset. */
+ if( uxSourceLen > sizeof( uint16_t ) )
+ {
+ pucByte += sizeof( uint16_t );
+ }
+ else
+ {
+ pucByte = NULL;
+ }
+ }
+ else
+ {
+ /* pucByte points to the full name. Walk over the string. */
+ while( ( NULL != pucByte ) && ( *pucByte != 0x00u ) && ( uxSourceLen > 1u ) )
+ {
+ /* If this is not the first time through the loop, then add a
+ separator in the output. */
+ if( ( uxNameLen > 0 ) && ( uxNameLen < ( uxDestLen - 1u ) ) )
+ {
+ pcName[ uxNameLen++ ] = '.';
+ }
+
+ /* Process the first/next sub-string. */
+ for( xCount = *( pucByte++ ), uxSourceLen--;
+ xCount-- && uxSourceLen > 1u;
+ pucByte++, uxSourceLen-- )
+ {
+ if( uxNameLen < uxDestLen - 1u )
+ {
+ pcName[ uxNameLen++ ] = *( ( char * ) pucByte );
+ }
+ else
+ {
+ /* DNS name is too big for the provided buffer. */
+ pucByte = NULL;
+ break;
+ }
+ }
+ }
+
+ /* Confirm that a fully formed name was found. */
+ if( NULL != pucByte )
+ {
+ if( 0x00 == *pucByte )
+ {
+ pucByte++;
+ uxSourceLen--;
+ pcName[ uxNameLen++ ] = '\0';
+ }
+ else
+ {
+ pucByte = NULL;
+ }
+ }
+ }
+
+ return pucByte;
+ }
+#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
+/*-----------------------------------------------------------*/
+
+static uint8_t * prvSkipNameField( uint8_t *pucByte,
+ size_t uxSourceLen )
+{
+size_t uxChunkLength;
+
+ if( 0u == uxSourceLen )
+ {
+ return NULL;
+ }
+
+ /* Determine if the name is the fully coded name, or an offset to the name
+ elsewhere in the message. */
+ if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
+ {
+ /* Jump over the two byte offset. */
+ if( uxSourceLen > sizeof( uint16_t ) )
+ {
+ pucByte += sizeof( uint16_t );
+ }
+ else
+ {
+ pucByte = NULL;
+ }
+ }
+ else
+ {
+ /* pucByte points to the full name. Walk over the string. */
+ while( ( *pucByte != 0x00u ) && ( uxSourceLen > 1u ) )
+ {
+ uxChunkLength = *pucByte + 1u;
+
+ if( uxSourceLen > uxChunkLength )
+ {
+ uxSourceLen -= uxChunkLength;
+ pucByte += uxChunkLength;
+ }
+ else
+ {
+ pucByte = NULL;
+ break;
+ }
+ }
+
+ /* Confirm that a fully formed name was found. */
+ if( NULL != pucByte )
+ {
+ if( 0x00u == *pucByte )
+ {
+ pucByte++;
+ }
+ else
+ {
+ pucByte = NULL;
+ }
+ }
+ }
+
+ return pucByte;
+}
+/*-----------------------------------------------------------*/
+
+/* The function below will only be called :
+when ipconfigDNS_USE_CALLBACKS == 1
+when ipconfigUSE_LLMNR == 1
+for testing purposes, by the module iot_test_freertos_tcp.c
+*/
+uint32_t ulDNSHandlePacket( NetworkBufferDescriptor_t *pxNetworkBuffer )
+{
+DNSMessage_t *pxDNSMessageHeader;
+size_t uxPayloadSize;
+
+ /* Only proceed if the payload length indicated in the header
+ appears to be valid. */
+ if( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )
+ {
+ uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );
+
+ if( uxPayloadSize >= sizeof( DNSMessage_t ) )
+ {
+ pxDNSMessageHeader =
+ ( DNSMessage_t * ) ( pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t ) );
+
+ /* The parameter pdFALSE indicates that the reply was not expected. */
+ prvParseDNSReply( ( uint8_t * ) pxDNSMessageHeader,
+ uxPayloadSize,
+ pdFALSE );
+ }
+ }
+
+ /* The packet was not consumed. */
+ return pdFAIL;
+}
+/*-----------------------------------------------------------*/
+
+#if( ipconfigUSE_NBNS == 1 )
+
+ uint32_t ulNBNSHandlePacket( NetworkBufferDescriptor_t * pxNetworkBuffer )
+ {
+ UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
+ uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
+ size_t uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );
+
+ /* The network buffer data length has already been set to the
+ length of the UDP payload. */
+ prvTreatNBNS( pucUDPPayloadBuffer,
+ uxPayloadSize,
+ pxUDPPacket->xIPHeader.ulSourceIPAddress );
+
+ /* The packet was not consumed. */
+ return pdFAIL;
+ }
+
+#endif /* ipconfigUSE_NBNS */
+/*-----------------------------------------------------------*/
+
+static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer,
+ size_t uxBufferLength,
+ BaseType_t xExpected )
+{
+DNSMessage_t *pxDNSMessageHeader;
+DNSAnswerRecord_t *pxDNSAnswerRecord;
+uint32_t ulIPAddress = 0uL;
+#if( ipconfigUSE_LLMNR == 1 )
+ char *pcRequestedName = NULL;
+#endif
+uint8_t *pucByte;
+size_t uxSourceBytesRemaining;
+uint16_t x, usDataLength, usQuestions;
+BaseType_t xDoStore = xExpected;
+#if( ipconfigUSE_LLMNR == 1 )
+ uint16_t usType = 0, usClass = 0;
+#endif
+#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
+ char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = "";
+#endif
+
+ /* Ensure that the buffer is of at least minimal DNS message length. */
+ if( uxBufferLength < sizeof( DNSMessage_t ) )
+ {
+ return dnsPARSE_ERROR;
+ }
+
+ uxSourceBytesRemaining = uxBufferLength;
+
+ /* Parse the DNS message header. */
+ pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
+
+ /* Introduce a do {} while (0) to allow the use of breaks. */
+ do
+ {
+ /* Start at the first byte after the header. */
+ pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t );
+ uxSourceBytesRemaining -= sizeof( DNSMessage_t );
+
+ /* Skip any question records. */
+ usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );
+
+ for( x = 0; x < usQuestions; x++ )
+ {
+ #if( ipconfigUSE_LLMNR == 1 )
+ {
+ if( x == 0 )
+ {
+ pcRequestedName = ( char * ) pucByte;
+ }
+ }
+ #endif
+
+#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
+ if( x == 0 )
+ {
+ pucByte = prvReadNameField( pucByte,
+ uxSourceBytesRemaining,
+ pcName,
+ sizeof( pcName ) );
+
+ /* Check for a malformed response. */
+ if( NULL == pucByte )
+ {
+ return dnsPARSE_ERROR;
+ }
+
+ uxSourceBytesRemaining = ( pucUDPPayloadBuffer + uxBufferLength ) - pucByte;
+ }
+ else
+#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
+ {
+ /* Skip the variable length pcName field. */
+ pucByte = prvSkipNameField( pucByte,
+ uxSourceBytesRemaining );
+
+ /* Check for a malformed response. */
+ if( NULL == pucByte )
+ {
+ return dnsPARSE_ERROR;
+ }
+
+ uxSourceBytesRemaining = ( size_t )
+ ( pucUDPPayloadBuffer + uxBufferLength - pucByte );
+ }
+
+ /* Check the remaining buffer size. */
+ if( uxSourceBytesRemaining >= sizeof( uint32_t ) )
+ {
+ #if( ipconfigUSE_LLMNR == 1 )
+ {
+ /* usChar2u16 returns value in host endianness. */
+ usType = usChar2u16( pucByte );
+ usClass = usChar2u16( pucByte + 2 );
+ }
+ #endif /* ipconfigUSE_LLMNR */
+
+ /* Skip the type and class fields. */
+ pucByte += sizeof( uint32_t );
+ uxSourceBytesRemaining -= sizeof( uint32_t );
+ }
+ else
+ {
+ /* Malformed response. */
+ return dnsPARSE_ERROR;
+ }
+ }
+
+ /* Search through the answer records. */
+ pxDNSMessageHeader->usAnswers = FreeRTOS_ntohs( pxDNSMessageHeader->usAnswers );
+
+ if( ( pxDNSMessageHeader->usFlags & dnsRX_FLAGS_MASK ) == dnsEXPECTED_RX_FLAGS )
+ {
+ for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )
+ {
+ pucByte = prvSkipNameField( pucByte,
+ uxSourceBytesRemaining );
+
+ /* Check for a malformed response. */
+ if( NULL == pucByte )
+ {
+ return dnsPARSE_ERROR;
+ }
+
+ uxSourceBytesRemaining = ( size_t )
+ ( pucUDPPayloadBuffer + uxBufferLength - pucByte );
+
+ /* Is there enough data for an IPv4 A record answer and, if so,
+ is this an A record? */
+ if( ( uxSourceBytesRemaining >= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) ) ) &&
+ ( usChar2u16( pucByte ) == dnsTYPE_A_HOST ) )
+ {
+ /* This is the required record type and is of sufficient size. */
+ pxDNSAnswerRecord = ( DNSAnswerRecord_t * ) pucByte;
+
+ /* Sanity check the data length of an IPv4 answer. */
+ if( FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ) == sizeof( uint32_t ) )
+ {
+ /* Copy the IP address out of the record. */
+ memcpy( &ulIPAddress,
+ pucByte + sizeof( DNSAnswerRecord_t ),
+ sizeof( uint32_t ) );
+
+ #if( ipconfigDNS_USE_CALLBACKS == 1 )
+ {
+ /* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */
+ if( xDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress ) != pdFALSE )
+ {
+ /* This device has requested this DNS look-up.
+ The result may be stored in the DNS cache. */
+ xDoStore = pdTRUE;
+ }
+ }
+ #endif /* ipconfigDNS_USE_CALLBACKS == 1 */
+ #if( ipconfigUSE_DNS_CACHE == 1 )
+ {
+ /* The reply will only be stored in the DNS cache when the
+ request was issued by this device. */
+ if( xDoStore != pdFALSE )
+ {
+ prvProcessDNSCache( pcName, &ulIPAddress, pxDNSAnswerRecord->ulTTL, pdFALSE );
+ }
+
+ /* Show what has happened. */
+ FreeRTOS_printf( ( "DNS[0x%04X]: The answer to '%s' (%xip) will%s be stored\n",
+ ( unsigned ) pxDNSMessageHeader->usIdentifier,
+ pcName,
+ ( unsigned ) FreeRTOS_ntohl( ulIPAddress ),
+ ( xDoStore != 0 ) ? "" : " NOT" ) );
+ }
+ #endif /* ipconfigUSE_DNS_CACHE */
+ }
+
+ pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t );
+ uxSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) );
+ break;
+ }
+ else if( uxSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) )
+ {
+ /* It's not an A record, so skip it. Get the header location
+ and then jump over the header. */
+ pxDNSAnswerRecord = ( DNSAnswerRecord_t * ) pucByte;
+ pucByte += sizeof( DNSAnswerRecord_t );
+ uxSourceBytesRemaining -= sizeof( DNSAnswerRecord_t );
+
+ /* Determine the length of the answer data from the header. */
+ usDataLength = FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength );
+
+ /* Jump over the answer. */
+ if( uxSourceBytesRemaining >= usDataLength )
+ {
+ pucByte += usDataLength;
+ uxSourceBytesRemaining -= usDataLength;
+ }
+ else
+ {
+ /* Malformed response. */
+ return dnsPARSE_ERROR;
+ }
+ }
+ }
+ }
+
+#if( ipconfigUSE_LLMNR == 1 )
+ else if( usQuestions && ( usType == dnsTYPE_A_HOST ) && ( usClass == dnsCLASS_IN ) )
+ {
+ /* If this is not a reply to our DNS request, it might an LLMNR
+ request. */
+ if( xApplicationDNSQueryHook( ( pcRequestedName + 1 ) ) )
+ {
+ int16_t usLength;
+ NetworkBufferDescriptor_t *pxNewBuffer = NULL;
+ NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
+ LLMNRAnswer_t *pxAnswer;
+
+ if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
+ {
+ BaseType_t xDataLength = uxBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
+
+ /* Set the size of the outgoing packet. */
+ pxNetworkBuffer->xDataLength = xDataLength;
+ pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + sizeof( LLMNRAnswer_t ) );
+
+ if( pxNewBuffer != NULL )
+ {
+ BaseType_t xOffset1, xOffset2;
+
+ xOffset1 = ( BaseType_t ) ( pucByte - pucUDPPayloadBuffer );
+ xOffset2 = ( BaseType_t ) ( ( ( uint8_t * ) pcRequestedName ) - pucUDPPayloadBuffer );
+
+ pxNetworkBuffer = pxNewBuffer;
+ pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + ipUDP_PAYLOAD_OFFSET_IPv4;
+
+ pucByte = pucUDPPayloadBuffer + xOffset1;
+ pcRequestedName = ( char * ) ( pucUDPPayloadBuffer + xOffset2 );
+ pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
+ }
+ else
+ {
+ /* Just to indicate that the message may not be answered. */
+ pxNetworkBuffer = NULL;
+ }
+ }
+
+ if( pxNetworkBuffer != NULL )
+ {
+ pxAnswer = ( LLMNRAnswer_t * ) pucByte;
+
+ /* We leave 'usIdentifier' and 'usQuestions' untouched */
+ vSetField16( pxDNSMessageHeader, DNSMessage_t, usFlags, dnsLLMNR_FLAGS_IS_REPONSE ); /* Set the response flag */
+ vSetField16( pxDNSMessageHeader, DNSMessage_t, usAnswers, 1 ); /* Provide a single answer */
+ vSetField16( pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 ); /* No authority */
+ vSetField16( pxDNSMessageHeader, DNSMessage_t, usAdditionalRRs, 0 ); /* No additional info */
+
+ pxAnswer->ucNameCode = dnsNAME_IS_OFFSET;
+ pxAnswer->ucNameOffset = ( uint8_t ) ( pcRequestedName - ( char * ) pucUDPPayloadBuffer );
+
+ vSetField16( pxAnswer, LLMNRAnswer_t, usType, dnsTYPE_A_HOST ); /* Type A: host */
+ vSetField16( pxAnswer, LLMNRAnswer_t, usClass, dnsCLASS_IN ); /* 1: Class IN */
+ vSetField32( pxAnswer, LLMNRAnswer_t, ulTTL, dnsLLMNR_TTL_VALUE );
+ vSetField16( pxAnswer, LLMNRAnswer_t, usDataLength, 4 );
+ vSetField32( pxAnswer, LLMNRAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
+
+ usLength = ( int16_t ) ( sizeof( *pxAnswer ) + ( size_t ) ( pucByte - pucUDPPayloadBuffer ) );
+
+ prvReplyDNSMessage( pxNetworkBuffer, usLength );
+
+ if( pxNewBuffer != NULL )
+ {
+ vReleaseNetworkBufferAndDescriptor( pxNewBuffer );
+ }
+ }
+ }
+ }
+#endif /* ipconfigUSE_LLMNR == 1 */
+ } while( 0 );
+
+ if( xExpected == pdFALSE )
+ {
+ /* Do not return a valid IP-address in case the reply was not expected. */
+ ulIPAddress = 0uL;
+ }
+
+ return ulIPAddress;
+}
+/*-----------------------------------------------------------*/
+
+#if( ipconfigUSE_NBNS == 1 )
+
+ static void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer,
+ size_t uxBufferLength,
+ uint32_t ulIPAddress )
+ {
+ uint16_t usFlags, usType, usClass;
+ uint8_t *pucSource, *pucTarget;
+ uint8_t ucByte;
+ uint8_t ucNBNSName[ 17 ];
+
+ /* Check for minimum buffer size. */
+ if( uxBufferLength < sizeof( NBNSRequest_t ) )
+ {
+ return;
+ }
+
+ /* Read the request flags in host endianness. */
+ usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) );
+
+ if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )
+ {
+ usType = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
+ usClass = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usClass ) );
+
+ /* Not used for now */
+ ( void ) usClass;
+
+ /* For NBNS a name is 16 bytes long, written with capitals only.
+ Make sure that the copy is terminated with a zero. */
+ pucTarget = ucNBNSName + sizeof( ucNBNSName ) - 2;
+ pucTarget[ 1 ] = '\0';
+
+ /* Start with decoding the last 2 bytes. */
+ pucSource = pucUDPPayloadBuffer + ( offsetof( NBNSRequest_t, ucName ) + ( dnsNBNS_ENCODED_NAME_LENGTH - 2 ) );
+
+ for( ;; )
+ {
+ ucByte = ( uint8_t ) ( ( ( pucSource[ 0 ] - 0x41 ) << 4 ) | ( pucSource[ 1 ] - 0x41 ) );
+
+ /* Make sure there are no trailing spaces in the name. */
+ if( ( ucByte == ' ' ) && ( pucTarget[ 1 ] == '\0' ) )
+ {
+ ucByte = '\0';
+ }
+
+ *pucTarget = ucByte;
+
+ if( pucTarget == ucNBNSName )
+ {
+ break;
+ }
+
+ pucTarget -= 1;
+ pucSource -= 2;
+ }
+
+ #if( ipconfigUSE_DNS_CACHE == 1 )
+ {
+ if( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) != 0 )
+ {
+ /* If this is a response from another device,
+ add the name to the DNS cache */
+ prvProcessDNSCache( ( char * ) ucNBNSName, &ulIPAddress, 0, pdFALSE );
+ }
+ }
+ #else
+ {
+ /* Avoid compiler warnings. */
+ ( void ) ulIPAddress;
+ }
+ #endif /* ipconfigUSE_DNS_CACHE */
+
+ if( ( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) == 0 ) &&
+ ( usType == dnsNBNS_TYPE_NET_BIOS ) &&
+ ( xApplicationDNSQueryHook( ( const char * ) ucNBNSName ) != pdFALSE ) )
+ {
+ uint16_t usLength;
+ DNSMessage_t *pxMessage;
+ NBNSAnswer_t *pxAnswer;
+
+ /* Someone is looking for a device with ucNBNSName,
+ prepare a positive reply. */
+ NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
+
+ if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
+ {
+ NetworkBufferDescriptor_t *pxNewBuffer;
+
+ /* The field xDataLength was set to the total length of the UDP packet,
+ i.e. the payload size plus sizeof( UDPPacket_t ). */
+ pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, pxNetworkBuffer->xDataLength + sizeof( NBNSAnswer_t ) );
+
+ if( pxNewBuffer != NULL )
+ {
+ pucUDPPayloadBuffer = pxNewBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
+ pxNetworkBuffer = pxNewBuffer;
+ }
+ else
+ {
+ /* Just prevent that a reply will be sent */
+ pxNetworkBuffer = NULL;
+ }
+ }
+
+ /* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */
+ if( pxNetworkBuffer != NULL )
+ {
+ pxMessage = ( DNSMessage_t * ) pucUDPPayloadBuffer;
+
+ /* As the fields in the structures are not word-aligned, we have to
+ copy the values byte-by-byte using macro's vSetField16() and vSetField32() */
+ vSetField16( pxMessage, DNSMessage_t, usFlags, dnsNBNS_QUERY_RESPONSE_FLAGS ); /* 0x8500 */
+ vSetField16( pxMessage, DNSMessage_t, usQuestions, 0 );
+ vSetField16( pxMessage, DNSMessage_t, usAnswers, 1 );
+ vSetField16( pxMessage, DNSMessage_t, usAuthorityRRs, 0 );
+ vSetField16( pxMessage, DNSMessage_t, usAdditionalRRs, 0 );
+
+ pxAnswer = ( NBNSAnswer_t * ) ( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
+
+ vSetField16( pxAnswer, NBNSAnswer_t, usType, usType ); /* Type */
+ vSetField16( pxAnswer, NBNSAnswer_t, usClass, dnsNBNS_CLASS_IN ); /* Class */
+ vSetField32( pxAnswer, NBNSAnswer_t, ulTTL, dnsNBNS_TTL_VALUE );
+ vSetField16( pxAnswer, NBNSAnswer_t, usDataLength, 6 ); /* 6 bytes including the length field */
+ vSetField16( pxAnswer, NBNSAnswer_t, usNbFlags, dnsNBNS_NAME_FLAGS );
+ vSetField32( pxAnswer, NBNSAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
+
+ usLength = ( uint16_t ) ( offsetof( NBNSRequest_t, usType ) + sizeof( NBNSAnswer_t ) );
+
+ prvReplyDNSMessage( pxNetworkBuffer, usLength );
+ }
+ }
+ }
+ }
+
+#endif /* ipconfigUSE_NBNS */
+/*-----------------------------------------------------------*/
+
+static Socket_t prvCreateDNSSocket( void )
+{
+Socket_t xSocket = NULL;
+struct freertos_sockaddr xAddress;
+BaseType_t xReturn;
+
+ /* This must be the first time this function has been called. Create
+ the socket. */
+ xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
+
+ /* Auto bind the port. */
+ xAddress.sin_port = 0u;
+ xReturn = FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );
+
+ /* Check the bind was successful, and clean up if not. */
+ if( xReturn != 0 )
+ {
+ FreeRTOS_closesocket( xSocket );
+ xSocket = NULL;
+ }
+ else
+ {
+ /* The send and receive timeouts will be set later on. */
+ }
+
+ return xSocket;
+}
+/*-----------------------------------------------------------*/
+
+#if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
+
+ static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer,
+ BaseType_t lNetLength )
+ {
+ UDPPacket_t *pxUDPPacket;
+ IPHeader_t *pxIPHeader;
+ UDPHeader_t *pxUDPHeader;
+
+ pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
+ pxIPHeader = &pxUDPPacket->xIPHeader;
+ pxUDPHeader = &pxUDPPacket->xUDPHeader;
+ /* HT: started using defines like 'ipSIZE_OF_xxx' */
+ pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );
+ /* HT:endian: should not be translated, copying from packet to packet */
+ pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
+ pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
+ pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;
+ pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );
+ usPacketIdentifier++;
+ pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );
+ vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort );
+
+ /* Important: tell NIC driver how many bytes must be sent */
+ pxNetworkBuffer->xDataLength = ( size_t ) ( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER );
+
+ #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
+ {
+ /* calculate the IP header checksum */
+ pxIPHeader->usHeaderChecksum = 0x00;
+ pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0uL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
+ pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
+
+ /* calculate the UDP checksum for outgoing package */
+ usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, pxNetworkBuffer->xDataLength, pdTRUE );
+ }
+ #endif
+
+ /* This function will fill in the eth addresses and send the packet */
+ vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );
+ }
+
+#endif /* ipconfigUSE_NBNS == 1 || ipconfigUSE_LLMNR == 1 */
+/*-----------------------------------------------------------*/
+
+#if( ipconfigUSE_DNS_CACHE == 1 )
+
+ static void prvProcessDNSCache( const char *pcName,
+ uint32_t *pulIP,
+ uint32_t ulTTL,
+ BaseType_t xLookUp )
+ {
+ BaseType_t x;
+ BaseType_t xFound = pdFALSE;
+ uint32_t ulCurrentTimeSeconds = ( xTaskGetTickCount() / portTICK_PERIOD_MS ) / 1000;
+ static BaseType_t xFreeEntry = 0;
+ configASSERT(pcName);
+
+ /* For each entry in the DNS cache table. */
+ for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )
+ {
+ if( xDNSCache[ x ].pcName[ 0 ] == 0 )
+ {
+ continue;
+ }
+
+ if( 0 == strcmp( xDNSCache[ x ].pcName, pcName ) )
+ {
+ /* Is this function called for a lookup or to add/update an IP address? */
+ if( xLookUp != pdFALSE )
+ {
+ /* Confirm that the record is still fresh. */
+ if( ulCurrentTimeSeconds < ( xDNSCache[ x ].ulTimeWhenAddedInSeconds + FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) ) )
+ {
+ *pulIP = xDNSCache[ x ].ulIPAddress;
+ }
+ else
+ {
+ /* Age out the old cached record. */
+ xDNSCache[ x ].pcName[ 0 ] = 0;
+ }
+ }
+ else
+ {
+ xDNSCache[ x ].ulIPAddress = *pulIP;
+ xDNSCache[ x ].ulTTL = ulTTL;
+ xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;
+ }
+
+ xFound = pdTRUE;
+ break;
+ }
+ }
+
+ if( xFound == pdFALSE )
+ {
+ if( xLookUp != pdFALSE )
+ {
+ *pulIP = 0;
+ }
+ else
+ {
+ /* Add or update the item. */
+ if( strlen( pcName ) < ipconfigDNS_CACHE_NAME_LENGTH )
+ {
+ strcpy( xDNSCache[ xFreeEntry ].pcName, pcName );
+
+ xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP;
+ xDNSCache[ xFreeEntry ].ulTTL = ulTTL;
+ xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;
+
+ xFreeEntry++;
+
+ if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )
+ {
+ xFreeEntry = 0;
+ }
+ }
+ }
+ }
+
+ if( ( xLookUp == 0 ) || ( *pulIP != 0 ) )
+ {
+ FreeRTOS_debug_printf( ( "prvProcessDNSCache: %s: '%s' @ %lxip\n", xLookUp ? "look-up" : "add", pcName, FreeRTOS_ntohl( *pulIP ) ) );
+ }
+ }
+
+#endif /* ipconfigUSE_DNS_CACHE */
+
+#endif /* ipconfigUSE_DNS != 0 */
+
+/*-----------------------------------------------------------*/
+
+/* Provide access to private members for testing. */
+#ifdef AMAZON_FREERTOS_ENABLE_UNIT_TESTS
+ #include "iot_freertos_tcp_test_access_dns_define.h"
+#endif
+