summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c
diff options
context:
space:
mode:
Diffstat (limited to 'FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c')
-rw-r--r--FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c702
1 files changed, 418 insertions, 284 deletions
diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c
index 465bdd3f1..43b246d26 100644
--- a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c
+++ b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_DNS.c
@@ -1,5 +1,5 @@
/*
- * FreeRTOS+TCP V2.0.11
+ * 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
@@ -47,58 +47,58 @@
#if( ipconfigUSE_DNS != 0 )
#if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
- #define dnsDNS_PORT 0x3500
- #define dnsONE_QUESTION 0x0100
- #define dnsOUTGOING_FLAGS 0x0001 /* Standard query. */
- #define dnsRX_FLAGS_MASK 0x0f80 /* The bits of interest in the flags field of incoming DNS messages. */
- #define dnsEXPECTED_RX_FLAGS 0x0080 /* Should be a response, without any errors. */
+ #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 0x0035
- #define dnsONE_QUESTION 0x0001
- #define dnsOUTGOING_FLAGS 0x0100 /* Standard query. */
- #define dnsRX_FLAGS_MASK 0x800f /* The bits of interest in the flags field of incoming DNS messages. */
- #define dnsEXPECTED_RX_FLAGS 0x8000 /* Should be a response, without any errors. */
+ #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
+ #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 )
+#define dnsNAME_IS_OFFSET ( ( uint8_t ) 0xc0 )
/* NBNS flags. */
-#define dnsNBNS_FLAGS_RESPONSE 0x8000
-#define dnsNBNS_FLAGS_OPCODE_MASK 0x7800
-#define dnsNBNS_FLAGS_OPCODE_QUERY 0x0000
-#define dnsNBNS_FLAGS_OPCODE_REGISTRATION 0x2800
+#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 0x01
-#define dnsCLASS_IN 0x01
+#define dnsTYPE_A_HOST 0x01u
+#define dnsCLASS_IN 0x01u
/* LLMNR constants. */
-#define dnsLLMNR_TTL_VALUE 300000
-#define dnsLLMNR_FLAGS_IS_REPONSE 0x8000
+#define dnsLLMNR_TTL_VALUE 300000uL
+#define dnsLLMNR_FLAGS_IS_REPONSE 0x8000u
/* NBNS constants. */
-#define dnsNBNS_TTL_VALUE 3600 /* 1 hour valid */
-#define dnsNBNS_TYPE_NET_BIOS 0x0020
-#define dnsNBNS_CLASS_IN 0x01
-#define dnsNBNS_NAME_FLAGS 0x6000
-#define dnsNBNS_ENCODED_NAME_LENGTH 32
+#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 ( 0x8500 )
+#define dnsNBNS_QUERY_RESPONSE_FLAGS ( 0x8500u )
/* Flag DNS parsing errors in situations where an IPv4 address is the return
type. */
-#define dnsPARSE_ERROR 0UL
+#define dnsPARSE_ERROR 0uL
/*
* Create a socket and bind it to the standard DNS port number. Return the
@@ -109,44 +109,66 @@ 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 xIdentifier );
+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 xSourceLen );
+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 xBufferLength, TickType_t xIdentifier );
+static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer,
+ size_t uxBufferLength,
+ BaseType_t xExpected );
/*
- * Prepare and send a message to a DNS server. 'xReadTimeOut_ms' will be passed as
+ * 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 xIdentifier, TickType_t xReadTimeOut_ms );
+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 );
+ static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer,
+ BaseType_t lNetLength );
#endif
#if( ipconfigUSE_NBNS == 1 )
- static portINLINE void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, size_t xBufferLength, uint32_t ulIPAddress );
+ 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 uint8_t *prvReadNameField( uint8_t *pucByte, size_t xSourceLen, char *pcName, size_t xLen );
- static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, uint32_t ulTTL, BaseType_t xLookUp );
+ 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 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;
@@ -160,7 +182,7 @@ static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier
#if( ipconfigUSE_LLMNR == 1 )
const MACAddress_t xLLMNR_MacAdress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
-#endif /* ipconfigUSE_LLMNR == 1 */
+#endif /* ipconfigUSE_LLMNR == 1 */
/*-----------------------------------------------------------*/
@@ -208,7 +230,7 @@ typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
struct xLLMNRAnswer
{
uint8_t ucNameCode;
- uint8_t ucNameOffset; /* The name is not repeated in the answer, only the offset is given with "0xc0 <offs>" */
+ 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;
@@ -247,32 +269,34 @@ typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
uint16_t usClass;
uint32_t ulTTL;
uint16_t usDataLength;
- uint16_t usNbFlags; /* NetBIOS flags 0x6000 : IP-address, big-endian */
+ 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 */
+ #endif /* ipconfigUSE_NBNS == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_DNS_CACHE == 1 )
uint32_t FreeRTOS_dnslookup( const char *pcHostName )
{
- uint32_t ulIPAddress = 0UL;
+ uint32_t ulIPAddress = 0uL;
+
prvProcessDNSCache( pcHostName, &ulIPAddress, 0, pdTRUE );
return ulIPAddress;
}
#endif /* ipconfigUSE_DNS_CACHE == 1 */
/*-----------------------------------------------------------*/
-#if( ipconfigDNS_USE_CALLBACKS != 0 )
+#if( ipconfigDNS_USE_CALLBACKS == 1 )
- typedef struct xDNS_Callback {
- TickType_t xRemaningTime; /* Timeout in ms */
+ 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 xTimeoutState;
+ TimeOut_t uxTimeoutState;
void *pvSearchID;
struct xLIST_ITEM xListItem;
char pcName[ 1 ];
@@ -283,7 +307,7 @@ typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
/* 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 );
+ return FreeRTOS_gethostbyname_a( pcHostName, ( FOnDNSEvent ) NULL, ( void * ) NULL, 0 );
}
/*-----------------------------------------------------------*/
@@ -304,23 +328,25 @@ typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
void vDNSCheckCallBack( void *pvSearchID )
{
const ListItem_t *pxIterator;
- const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
+ 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 );
+ 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 );
+ pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );
+
if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )
{
uxListRemove( &pxCallback->xListItem );
vPortFree( pxCallback );
}
- else if( xTaskCheckForTimeOut( &pxCallback->xTimeoutState, &pxCallback->xRemaningTime ) != pdFALSE )
+ else if( xTaskCheckForTimeOut( &pxCallback->uxTimeoutState, &pxCallback->uxRemaningTime ) != pdFALSE )
{
pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );
uxListRemove( &pxCallback->xListItem );
@@ -346,28 +372,38 @@ typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
/* 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 xTimeout, TickType_t xIdentifier );
- static void vDNSSetCallBack( const char *pcHostName, void *pvSearchID, FOnDNSEvent pCallbackFunction, TickType_t xTimeout, TickType_t xIdentifier )
+ 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 );
+ size_t lLength = strlen( pcHostName );
+ DNSCallback_t *pxCallback = ( DNSCallback_t * ) pvPortMalloc( sizeof( *pxCallback ) + lLength );
/* Translate from ms to number of clock ticks. */
- xTimeout /= portTICK_PERIOD_MS;
+ 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, xTimeout ) );
+ vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, uxTimeout ) );
}
+
strcpy( pxCallback->pcName, pcHostName );
pxCallback->pCallbackFunction = pCallbackFunction;
pxCallback->pvSearchID = pvSearchID;
- pxCallback->xRemaningTime = xTimeout;
- vTaskSetTimeOutState( &pxCallback->xTimeoutState );
- listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void* ) pxCallback );
- listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), xIdentifier );
+ 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 );
@@ -378,12 +414,17 @@ typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
/*-----------------------------------------------------------*/
/* A DNS reply was received, see if there is any matching entry and
- call the handler. */
- static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress );
- static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress )
+ 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 )
{
- const ListItem_t *pxIterator;
- const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
+ BaseType_t xResult = pdFALSE;
+ const ListItem_t *pxIterator;
+ const MiniListItem_t * xEnd = ( const MiniListItem_t * ) listGET_END_MARKER( &xCallbackList );
vTaskSuspendAll();
{
@@ -391,111 +432,132 @@ typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
pxIterator != ( const ListItem_t * ) xEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
- if( listGET_LIST_ITEM_VALUE( pxIterator ) == xIdentifier )
+ /* 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 );
+ 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 != 0 */
+#endif /* ipconfigDNS_USE_CALLBACKS == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigDNS_USE_CALLBACKS == 0 )
-uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
+ uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
#else
-uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName, FOnDNSEvent pCallback, void *pvSearchID, TickType_t xTimeout )
+ uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName,
+ FOnDNSEvent pCallback,
+ void *pvSearchID,
+ TickType_t uxTimeout )
#endif
{
-uint32_t ulIPAddress = 0UL;
-TickType_t xReadTimeOut_ms = ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME;
-TickType_t xIdentifier = 0;
+uint32_t ulIPAddress = 0uL;
+TickType_t uxReadTimeOut_ticks = ipconfigDNS_RECEIVE_BLOCK_TIME_TICKS;
+TickType_t uxIdentifier = 0u;
+BaseType_t xHasRandom = pdFALSE;
- /* If the supplied hostname is IP address, convert it to uint32_t
- and return. */
- #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 )
+ if( pcHostName != NULL )
{
- ulIPAddress = FreeRTOS_inet_addr( pcHostName );
- }
- #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */
+ /* 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 )
+ /* If a DNS cache is used then check the cache before issuing another DNS
+ request. */
+ #if( ipconfigUSE_DNS_CACHE == 1 )
{
- ulIPAddress = FreeRTOS_dnslookup( pcHostName );
- if( ulIPAddress != 0 )
+ if( ulIPAddress == 0uL )
{
- FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );
- }
- else
- {
- /* prvGetHostByName will be called to start a DNS lookup */
+ 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 */
+ #endif /* ipconfigUSE_DNS_CACHE == 1 */
- /* Generate a unique identifier. */
- if( 0 == ulIPAddress )
- {
- xIdentifier = ( TickType_t )ipconfigRAND32( );
- }
+ /* Generate a unique identifier. */
+ if( ulIPAddress == 0uL )
+ {
+ uint32_t ulNumber;
- #if( ipconfigDNS_USE_CALLBACKS != 0 )
- {
- if( pCallback != NULL )
+ 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( ulIPAddress == 0UL )
+ if( pCallback != NULL )
{
- /* The user has provided a callback function, so do not block on recvfrom() */
- if( 0 != xIdentifier )
+ if( ulIPAddress == 0uL )
{
- xReadTimeOut_ms = 0;
- vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t )xIdentifier );
+ /* 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 );
}
- }
- else
- {
- /* The IP address is known, do the call-back now. */
- pCallback( pcHostName, pvSearchID, ulIPAddress );
}
}
- }
- #endif
+ #endif /* if ( ipconfigDNS_USE_CALLBACKS == 1 ) */
- if( ( ulIPAddress == 0UL ) && ( 0 != xIdentifier ) )
- {
- ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms );
+ if( ( ulIPAddress == 0uL ) && ( xHasRandom != pdFALSE ) )
+ {
+ ulIPAddress = prvGetHostByName( pcHostName, uxIdentifier, uxReadTimeOut_ticks );
+ }
}
-
return ulIPAddress;
}
/*-----------------------------------------------------------*/
-static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms )
+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;
+uint32_t ulIPAddress = 0uL;
uint8_t *pucUDPPayloadBuffer;
uint32_t ulAddressLength = sizeof( struct freertos_sockaddr );
BaseType_t xAttempt;
int32_t lBytes;
-size_t xPayloadLength, xExpectedPayloadLength;
-TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
+size_t uxPayloadLength, uxExpectedPayloadLength;
+TickType_t uxWriteTimeOut_ticks = ipconfigDNS_SEND_BLOCK_TIME_TICKS;
#if( ipconfigUSE_LLMNR == 1 )
BaseType_t bHasDot = pdFALSE;
@@ -505,7 +567,8 @@ TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
if not then LLMNR can be used as the lookup method. */
#if( ipconfigUSE_LLMNR == 1 )
{
- const char *pucPtr;
+ const char *pucPtr;
+
for( pucPtr = pcHostName; *pucPtr; pucPtr++ )
{
if( *pucPtr == '.' )
@@ -519,26 +582,26 @@ TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
/* Two is added at the end for the count of characters in the first
subdomain part and the string end byte. */
- xExpectedPayloadLength = sizeof( DNSMessage_t ) + strlen( pcHostName ) + sizeof( uint16_t ) + sizeof( uint16_t ) + 2u;
+ 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 * ) &xWriteTimeOut_ms, sizeof( TickType_t ) );
- FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xReadTimeOut_ms, sizeof( TickType_t ) );
+ 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( xExpectedPayloadLength, portMAX_DELAY );
+ pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( uxExpectedPayloadLength, portMAX_DELAY );
if( pucUDPPayloadBuffer != NULL )
{
/* Create the message in the obtained buffer. */
- xPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, xIdentifier );
+ uxPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, uxIdentifier );
iptraceSENDING_DNS_REQUEST();
@@ -550,8 +613,8 @@ TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
if( bHasDot == pdFALSE )
{
/* Use LLMNR addressing. */
- ( ( DNSMessage_t * ) pucUDPPayloadBuffer) -> usFlags = 0;
- xAddress.sin_addr = ipLLMNR_IP_ADDR; /* Is in network byte order. */
+ ( ( DNSMessage_t * ) pucUDPPayloadBuffer )->usFlags = 0;
+ xAddress.sin_addr = ipLLMNR_IP_ADDR; /* Is in network byte order. */
xAddress.sin_port = FreeRTOS_ntohs( ipLLMNR_PORT );
}
else
@@ -562,24 +625,45 @@ TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
xAddress.sin_port = dnsDNS_PORT;
}
- ulIPAddress = 0UL;
+ ulIPAddress = 0uL;
- if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, xPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )
+ 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. */
- ulIPAddress = prvParseDNSReply( pucUDPPayloadBuffer, lBytes, xIdentifier );
+ #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 )
+ if( ulIPAddress != 0uL )
{
/* All done. */
break;
@@ -593,6 +677,13 @@ TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
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. */
@@ -603,19 +694,21 @@ TickType_t xWriteTimeOut_ms = ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME;
}
/*-----------------------------------------------------------*/
-static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier )
+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. */
+ 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. */
@@ -623,7 +716,7 @@ static const DNSMessage_t xDefaultPartDNSHeader =
/* Write in a unique identifier. */
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
- pxDNSMessageHeader->usIdentifier = ( uint16_t ) xIdentifier;
+ pxDNSMessageHeader->usIdentifier = ( uint16_t ) uxIdentifier;
/* Create the resource record at the end of the header. First
find the end of the header. */
@@ -659,15 +752,14 @@ static const DNSMessage_t xDefaultPartDNSHeader =
( *pucStart )--;
pucStart = pucByte;
-
} while( *pucByte != 0x00 );
/* Finish off the record. */
- pxTail = (DNSTail_t *)( pucByte + 1 );
+ 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 */
+ 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. */
@@ -675,14 +767,17 @@ static const DNSMessage_t xDefaultPartDNSHeader =
}
/*-----------------------------------------------------------*/
-#if( ipconfigUSE_DNS_CACHE == 1 )
+#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
- static uint8_t *prvReadNameField( uint8_t *pucByte, size_t xSourceLen, char *pcName, size_t xDestLen )
+ static uint8_t * prvReadNameField( uint8_t *pucByte,
+ size_t uxSourceLen,
+ char *pcName,
+ size_t uxDestLen )
{
- size_t xNameLen = 0;
+ size_t uxNameLen = 0;
BaseType_t xCount;
- if( 0 == xSourceLen )
+ if( 0 == uxSourceLen )
{
return NULL;
}
@@ -692,7 +787,7 @@ static const DNSMessage_t xDefaultPartDNSHeader =
if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
- if( xSourceLen > sizeof( uint16_t ) )
+ if( uxSourceLen > sizeof( uint16_t ) )
{
pucByte += sizeof( uint16_t );
}
@@ -704,23 +799,23 @@ static const DNSMessage_t xDefaultPartDNSHeader =
else
{
/* pucByte points to the full name. Walk over the string. */
- while( ( NULL != pucByte ) && ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) )
+ 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( ( xNameLen > 0 ) && ( xNameLen < ( xDestLen - 1 ) ) )
+ if( ( uxNameLen > 0 ) && ( uxNameLen < ( uxDestLen - 1u ) ) )
{
- pcName[ xNameLen++ ] = '.';
+ pcName[ uxNameLen++ ] = '.';
}
/* Process the first/next sub-string. */
- for( xCount = *(pucByte++), xSourceLen--;
- xCount-- && xSourceLen > 1;
- pucByte++, xSourceLen-- )
+ for( xCount = *( pucByte++ ), uxSourceLen--;
+ xCount-- && uxSourceLen > 1u;
+ pucByte++, uxSourceLen-- )
{
- if( xNameLen < xDestLen - 1 )
+ if( uxNameLen < uxDestLen - 1u )
{
- pcName[ xNameLen++ ] = *( ( char * )pucByte );
+ pcName[ uxNameLen++ ] = *( ( char * ) pucByte );
}
else
{
@@ -737,8 +832,8 @@ static const DNSMessage_t xDefaultPartDNSHeader =
if( 0x00 == *pucByte )
{
pucByte++;
- xSourceLen--;
- pcName[ xNameLen++ ] = '\0';
+ uxSourceLen--;
+ pcName[ uxNameLen++ ] = '\0';
}
else
{
@@ -749,14 +844,15 @@ static const DNSMessage_t xDefaultPartDNSHeader =
return pucByte;
}
-#endif /* ipconfigUSE_DNS_CACHE == 1 */
+#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
/*-----------------------------------------------------------*/
-static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen )
+static uint8_t * prvSkipNameField( uint8_t *pucByte,
+ size_t uxSourceLen )
{
- size_t xChunkLength;
+size_t uxChunkLength;
- if( 0 == xSourceLen )
+ if( 0u == uxSourceLen )
{
return NULL;
}
@@ -766,7 +862,7 @@ static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen )
if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
- if( xSourceLen > sizeof( uint16_t ) )
+ if( uxSourceLen > sizeof( uint16_t ) )
{
pucByte += sizeof( uint16_t );
}
@@ -778,14 +874,14 @@ static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen )
else
{
/* pucByte points to the full name. Walk over the string. */
- while( ( *pucByte != 0x00 ) && ( xSourceLen > 1 ) )
+ while( ( *pucByte != 0x00u ) && ( uxSourceLen > 1u ) )
{
- xChunkLength = *pucByte + 1;
+ uxChunkLength = *pucByte + 1u;
- if( xSourceLen > xChunkLength )
+ if( uxSourceLen > uxChunkLength )
{
- xSourceLen -= xChunkLength;
- pucByte += xChunkLength;
+ uxSourceLen -= uxChunkLength;
+ pucByte += uxChunkLength;
}
else
{
@@ -797,7 +893,7 @@ static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen )
/* Confirm that a fully formed name was found. */
if( NULL != pucByte )
{
- if( 0x00 == *pucByte )
+ if( 0x00u == *pucByte )
{
pucByte++;
}
@@ -812,19 +908,33 @@ static uint8_t *prvSkipNameField( uint8_t *pucByte, size_t xSourceLen )
}
/*-----------------------------------------------------------*/
+/* 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;
-
- if( pxNetworkBuffer->xDataLength >= sizeof( DNSMessage_t ) )
- {
- pxDNSMessageHeader =
- ( DNSMessage_t * )( pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t ) );
-
- prvParseDNSReply( ( uint8_t * )pxDNSMessageHeader,
- pxNetworkBuffer->xDataLength,
- ( uint32_t )pxDNSMessageHeader->usIdentifier );
- }
+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;
@@ -833,15 +943,16 @@ DNSMessage_t *pxDNSMessageHeader;
#if( ipconfigUSE_NBNS == 1 )
- uint32_t ulNBNSHandlePacket (NetworkBufferDescriptor_t *pxNetworkBuffer )
+ 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
+ /* The network buffer data length has already been set to the
length of the UDP payload. */
prvTreatNBNS( pucUDPPayloadBuffer,
- pxNetworkBuffer->xDataLength,
+ uxPayloadSize,
pxUDPPacket->xIPHeader.ulSourceIPAddress );
/* The packet was not consumed. */
@@ -851,45 +962,48 @@ DNSMessage_t *pxDNSMessageHeader;
#endif /* ipconfigUSE_NBNS */
/*-----------------------------------------------------------*/
-static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, size_t xBufferLength, TickType_t xIdentifier )
+static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer,
+ size_t uxBufferLength,
+ BaseType_t xExpected )
{
DNSMessage_t *pxDNSMessageHeader;
DNSAnswerRecord_t *pxDNSAnswerRecord;
-uint32_t ulIPAddress = 0UL;
+uint32_t ulIPAddress = 0uL;
#if( ipconfigUSE_LLMNR == 1 )
char *pcRequestedName = NULL;
#endif
uint8_t *pucByte;
-size_t xSourceBytesRemaining;
+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 )
+#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( xBufferLength < sizeof( DNSMessage_t ) )
+ if( uxBufferLength < sizeof( DNSMessage_t ) )
{
return dnsPARSE_ERROR;
}
- else
- {
- xSourceBytesRemaining = xBufferLength;
- }
+
+ uxSourceBytesRemaining = uxBufferLength;
/* Parse the DNS message header. */
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
- if( pxDNSMessageHeader->usIdentifier == ( uint16_t ) xIdentifier )
+ /* 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 );
- xSourceBytesRemaining -= 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 )
@@ -901,11 +1015,11 @@ uint16_t x, usDataLength, usQuestions;
}
#endif
-#if( ipconfigUSE_DNS_CACHE == 1 )
+#if( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
if( x == 0 )
{
pucByte = prvReadNameField( pucByte,
- xSourceBytesRemaining,
+ uxSourceBytesRemaining,
pcName,
sizeof( pcName ) );
@@ -914,35 +1028,32 @@ uint16_t x, usDataLength, usQuestions;
{
return dnsPARSE_ERROR;
}
- else
- {
- xSourceBytesRemaining = ( pucUDPPayloadBuffer + xBufferLength ) - pucByte;
- }
+
+ uxSourceBytesRemaining = ( pucUDPPayloadBuffer + uxBufferLength ) - pucByte;
}
else
-#endif /* ipconfigUSE_DNS_CACHE */
+#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
{
/* Skip the variable length pcName field. */
pucByte = prvSkipNameField( pucByte,
- xSourceBytesRemaining );
+ uxSourceBytesRemaining );
/* Check for a malformed response. */
if( NULL == pucByte )
{
return dnsPARSE_ERROR;
}
- else
- {
- xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;
- }
+
+ uxSourceBytesRemaining = ( size_t )
+ ( pucUDPPayloadBuffer + uxBufferLength - pucByte );
}
/* Check the remaining buffer size. */
- if( xSourceBytesRemaining >= sizeof( uint32_t ) )
+ if( uxSourceBytesRemaining >= sizeof( uint32_t ) )
{
#if( ipconfigUSE_LLMNR == 1 )
{
- /* usChar2u16 returns value in host endianness */
+ /* usChar2u16 returns value in host endianness. */
usType = usChar2u16( pucByte );
usClass = usChar2u16( pucByte + 2 );
}
@@ -950,7 +1061,7 @@ uint16_t x, usDataLength, usQuestions;
/* Skip the type and class fields. */
pucByte += sizeof( uint32_t );
- xSourceBytesRemaining -= sizeof( uint32_t );
+ uxSourceBytesRemaining -= sizeof( uint32_t );
}
else
{
@@ -967,25 +1078,24 @@ uint16_t x, usDataLength, usQuestions;
for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )
{
pucByte = prvSkipNameField( pucByte,
- xSourceBytesRemaining );
+ uxSourceBytesRemaining );
/* Check for a malformed response. */
if( NULL == pucByte )
{
return dnsPARSE_ERROR;
}
- else
- {
- xSourceBytesRemaining = pucUDPPayloadBuffer + xBufferLength - pucByte;
- }
+
+ 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( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) &&
- usChar2u16( pucByte ) == dnsTYPE_A_HOST )
+ 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;
+ pxDNSAnswerRecord = ( DNSAnswerRecord_t * ) pucByte;
/* Sanity check the data length of an IPv4 answer. */
if( FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ) == sizeof( uint32_t ) )
@@ -995,39 +1105,56 @@ uint16_t x, usDataLength, usQuestions;
pucByte + sizeof( DNSAnswerRecord_t ),
sizeof( uint32_t ) );
- #if( ipconfigUSE_DNS_CACHE == 1 )
+ #if( ipconfigDNS_USE_CALLBACKS == 1 )
{
- prvProcessDNSCache( pcName, &ulIPAddress, pxDNSAnswerRecord->ulTTL, pdFALSE );
+ /* 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 /* ipconfigUSE_DNS_CACHE */
- #if( ipconfigDNS_USE_CALLBACKS != 0 )
+ #endif /* ipconfigDNS_USE_CALLBACKS == 1 */
+ #if( ipconfigUSE_DNS_CACHE == 1 )
{
- /* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */
- vDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress );
+ /* 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 /* ipconfigDNS_USE_CALLBACKS != 0 */
+ #endif /* ipconfigUSE_DNS_CACHE */
}
pucByte += sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t );
- xSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) );
+ uxSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + sizeof( uint32_t ) );
break;
}
- else if( xSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) )
+ 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;
+ pxDNSAnswerRecord = ( DNSAnswerRecord_t * ) pucByte;
pucByte += sizeof( DNSAnswerRecord_t );
- xSourceBytesRemaining -= 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( xSourceBytesRemaining >= usDataLength )
+ if( uxSourceBytesRemaining >= usDataLength )
{
pucByte += usDataLength;
- xSourceBytesRemaining -= usDataLength;
+ uxSourceBytesRemaining -= usDataLength;
}
else
{
@@ -1037,12 +1164,13 @@ uint16_t x, usDataLength, usQuestions;
}
}
}
+
#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 ) ) )
+ if( xApplicationDNSQueryHook( ( pcRequestedName + 1 ) ) )
{
int16_t usLength;
NetworkBufferDescriptor_t *pxNewBuffer = NULL;
@@ -1051,13 +1179,12 @@ uint16_t x, usDataLength, usQuestions;
if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
{
- BaseType_t xDataLength = xBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
+ BaseType_t xDataLength = uxBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
- /* The field xDataLength was set to the length of the UDP payload.
- The answer (reply) will be longer than the request, so the packet
- must be duplicaed into a bigger buffer */
+ /* Set the size of the outgoing packet. */
pxNetworkBuffer->xDataLength = xDataLength;
- pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );
+ pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + sizeof( LLMNRAnswer_t ) );
+
if( pxNewBuffer != NULL )
{
BaseType_t xOffset1, xOffset2;
@@ -1071,7 +1198,6 @@ uint16_t x, usDataLength, usQuestions;
pucByte = pucUDPPayloadBuffer + xOffset1;
pcRequestedName = ( char * ) ( pucUDPPayloadBuffer + xOffset2 );
pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
-
}
else
{
@@ -1079,21 +1205,22 @@ uint16_t x, usDataLength, usQuestions;
pxNetworkBuffer = NULL;
}
}
+
if( pxNetworkBuffer != NULL )
{
- pxAnswer = (LLMNRAnswer_t *)pucByte;
+ 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 */
+ 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 );
+ 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 */
+ 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 ) );
@@ -1110,6 +1237,12 @@ uint16_t x, usDataLength, usQuestions;
}
}
#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;
@@ -1118,15 +1251,17 @@ uint16_t x, usDataLength, usQuestions;
#if( ipconfigUSE_NBNS == 1 )
- static void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, size_t xBufferLength, uint32_t ulIPAddress )
+ 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 ];
+ uint16_t usFlags, usType, usClass;
+ uint8_t *pucSource, *pucTarget;
+ uint8_t ucByte;
+ uint8_t ucNBNSName[ 17 ];
/* Check for minimum buffer size. */
- if( xBufferLength < sizeof( NBNSRequest_t ) )
+ if( uxBufferLength < sizeof( NBNSRequest_t ) )
{
return;
}
@@ -1140,10 +1275,11 @@ uint16_t x, usDataLength, usQuestions;
usClass = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usClass ) );
/* Not used for now */
- ( void )usClass;
+ ( 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 = ucNBNSName + sizeof( ucNBNSName ) - 2;
pucTarget[ 1 ] = '\0';
/* Start with decoding the last 2 bytes. */
@@ -1201,14 +1337,11 @@ uint16_t x, usDataLength, usQuestions;
if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
{
NetworkBufferDescriptor_t *pxNewBuffer;
- BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +
- sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
- /* The field xDataLength was set to the length of the UDP payload.
- The answer (reply) will be longer than the request, so the packet
- must be duplicated into a bigger buffer */
- pxNetworkBuffer->xDataLength = xDataLength;
- pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );
+ /* 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 );
@@ -1224,7 +1357,7 @@ uint16_t x, usDataLength, usQuestions;
/* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */
if( pxNetworkBuffer != NULL )
{
- pxMessage = (DNSMessage_t *)pucUDPPayloadBuffer;
+ 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() */
@@ -1234,12 +1367,12 @@ uint16_t x, usDataLength, usQuestions;
vSetField16( pxMessage, DNSMessage_t, usAuthorityRRs, 0 );
vSetField16( pxMessage, DNSMessage_t, usAdditionalRRs, 0 );
- pxAnswer = (NBNSAnswer_t *)( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
+ pxAnswer = ( NBNSAnswer_t * ) ( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
- vSetField16( pxAnswer, NBNSAnswer_t, usType, usType ); /* Type */
- vSetField16( pxAnswer, NBNSAnswer_t, usClass, dnsNBNS_CLASS_IN ); /* Class */
+ 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, 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 ) );
@@ -1251,7 +1384,7 @@ uint16_t x, usDataLength, usQuestions;
}
}
-#endif /* ipconfigUSE_NBNS */
+#endif /* ipconfigUSE_NBNS */
/*-----------------------------------------------------------*/
static Socket_t prvCreateDNSSocket( void )
@@ -1259,7 +1392,6 @@ static Socket_t prvCreateDNSSocket( void )
Socket_t xSocket = NULL;
struct freertos_sockaddr xAddress;
BaseType_t xReturn;
-TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
/* This must be the first time this function has been called. Create
the socket. */
@@ -1277,9 +1409,7 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
}
else
{
- /* Set the send and receive timeouts. */
- FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
- FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
+ /* The send and receive timeouts will be set later on. */
}
return xSocket;
@@ -1288,13 +1418,14 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
#if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
- static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength )
+ static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer,
+ BaseType_t lNetLength )
{
UDPPacket_t *pxUDPPacket;
IPHeader_t *pxIPHeader;
UDPHeader_t *pxUDPHeader;
- pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;
+ pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
pxIPHeader = &pxUDPPacket->xIPHeader;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
/* HT: started using defines like 'ipSIZE_OF_xxx' */
@@ -1314,9 +1445,9 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
#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 );
+ 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 );
@@ -1332,14 +1463,16 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
#if( ipconfigUSE_DNS_CACHE == 1 )
- static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, uint32_t ulTTL, BaseType_t xLookUp )
+ 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);
-
+ configASSERT(pcName);
/* For each entry in the DNS cache table. */
for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )
@@ -1395,6 +1528,7 @@ TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;
xFreeEntry++;
+
if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )
{
xFreeEntry = 0;