Skip to content

Commit

Permalink
Only define DNS_ReadNameField when cache is used (FreeRTOS#1201)
Browse files Browse the repository at this point in the history
* Fix RX network interface to create task only once

* Only define DNS_ReadNameField when cache is used

* Uncrustify: triggered by comment.

* EnDefineNS pcName buffer when MDNS/LLMNR enabled.

* Fix spelling and unit-test

---------

Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: ActoryOu <jay2002824@gmail.com>
Co-authored-by: ActoryOu <ousc@amazon.com>
  • Loading branch information
4 people authored Oct 31, 2024
1 parent 96c6f3a commit 4f67761
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 90 deletions.
1 change: 1 addition & 0 deletions .github/.cSpellWords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ BLXNS
bmcr
BMSR
BPDG
BPIALL
brgintclr
brginten
brgintstat
Expand Down
3 changes: 3 additions & 0 deletions source/FreeRTOS_DNS.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB

/* 'xFamily' might not be used when IPv6 is disabled. */
( void ) xFamily;
/* 'pcName' might not be used when DNS cache is disabled. */
( void ) pcName;

pvBuffer = pvPortMalloc( sizeof( *pxAddrInfo ) );

if( pvBuffer != NULL )
Expand Down
180 changes: 91 additions & 89 deletions source/FreeRTOS_DNS_Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#if ( ipconfigUSE_DNS != 0 )

#if ( ( ipconfigUSE_DNS_CACHE != 0 ) || ( ipconfigDNS_USE_CALLBACKS != 0 ) || ( ipconfigUSE_MDNS != 0 ) || ( ipconfigUSE_LLMNR != 0 ) )

/**
* @brief Read the Name field out of a DNS response packet.
Expand All @@ -56,110 +57,111 @@
*
* @return If a fully formed name was found, then return the number of bytes processed in pucByte.
*/
size_t DNS_ReadNameField( ParseSet_t * pxSet,
size_t uxDestLen )
{
size_t uxNameLen = 0U;
size_t uxIndex = 0U;
size_t uxSourceLen = pxSet->uxSourceBytesRemaining;
const uint8_t * pucByte = pxSet->pucByte;

/* uxCount gets the values from pucByte and counts down to 0.
* No need to have a different type than that of pucByte */
size_t uxCount;

if( uxSourceLen == ( size_t ) 0U )
size_t DNS_ReadNameField( ParseSet_t * pxSet,
size_t uxDestLen )
{
/* Return 0 value in case of error. */
uxIndex = 0U;
}
size_t uxNameLen = 0U;
size_t uxIndex = 0U;
size_t uxSourceLen = pxSet->uxSourceBytesRemaining;
const uint8_t * pucByte = pxSet->pucByte;

/* Determine if the name is the fully coded name, or an offset to the name
* elsewhere in the message. */
else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
if( uxSourceLen > sizeof( uint16_t ) )
{
uxIndex += sizeof( uint16_t );
}
else
/* uxCount gets the values from pucByte and counts down to 0.
* No need to have a different type than that of pucByte */
size_t uxCount;

if( uxSourceLen == ( size_t ) 0U )
{
/* Return 0 value in case of error. */
uxIndex = 0U;
}
}
else
{
/* 'uxIndex' points to the full name. Walk over the string. */
while( ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] != ( uint8_t ) 0x00U ) )
{
/* If this is not the first time through the loop, then add a
* separator in the output. */
if( ( uxNameLen > 0U ) )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = '.';
uxNameLen++;
}

/* Process the first/next sub-string. */
uxCount = ( size_t ) pucByte[ uxIndex ];

/* uxIndex should point to the first character now, unless uxCount
* is an offset field. */
uxIndex++;

if( ( uxIndex + uxCount ) > uxSourceLen )
/* Determine if the name is the fully coded name, or an offset to the name
* elsewhere in the message. */
else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
if( uxSourceLen > sizeof( uint16_t ) )
{
uxIndex = 0U;
break;
uxIndex += sizeof( uint16_t );
}

if( ( uxNameLen + uxCount ) >= uxDestLen )
else
{
uxIndex = 0U;
break;
}

while( uxCount-- != 0U )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = ( char ) pucByte[ uxIndex ];
uxNameLen++;
uxIndex++;
}
}

/* Confirm that a fully formed name was found. */
if( uxIndex > 0U )
else
{
/* Here, there is no need to check for pucByte[ uxindex ] == 0 because:
* When we break out of the above while loop, uxIndex is made 0 thereby
* failing above check. Whenever we exit the loop otherwise, either
* pucByte[ uxIndex ] == 0 (which makes the check here unnecessary) or
* uxIndex >= uxSourceLen (which makes sure that we do not go in the 'if'
* case).
*/
if( uxIndex < uxSourceLen )
/* 'uxIndex' points to the full name. Walk over the string. */
while( ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] != ( uint8_t ) 0x00U ) )
{
pxSet->pcName[ uxNameLen ] = '\0';
/* If this is not the first time through the loop, then add a
* separator in the output. */
if( ( uxNameLen > 0U ) )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = '.';
uxNameLen++;
}

/* Process the first/next sub-string. */
uxCount = ( size_t ) pucByte[ uxIndex ];

/* uxIndex should point to the first character now, unless uxCount
* is an offset field. */
uxIndex++;

if( ( uxIndex + uxCount ) > uxSourceLen )
{
uxIndex = 0U;
break;
}

if( ( uxNameLen + uxCount ) >= uxDestLen )
{
uxIndex = 0U;
break;
}

while( uxCount-- != 0U )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = ( char ) pucByte[ uxIndex ];
uxNameLen++;
uxIndex++;
}
}
else

/* Confirm that a fully formed name was found. */
if( uxIndex > 0U )
{
uxIndex = 0U;
/* Here, there is no need to check for pucByte[ uxindex ] == 0 because:
* When we break out of the above while loop, uxIndex is made 0 thereby
* failing above check. Whenever we exit the loop otherwise, either
* pucByte[ uxIndex ] == 0 (which makes the check here unnecessary) or
* uxIndex >= uxSourceLen (which makes sure that we do not go in the 'if'
* case).
*/
if( uxIndex < uxSourceLen )
{
pxSet->pcName[ uxNameLen ] = '\0';
uxIndex++;
}
else
{
uxIndex = 0U;
}
}
}
}

return uxIndex;
}
return uxIndex;
}
#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS || ipconfigUSE_MDNS || ipconfigUSE_LLMNR */

/**
* @brief Simple routine that jumps over the NAME field of a resource record.
Expand Down Expand Up @@ -285,7 +287,7 @@
* for easier access. */

/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
xSet.pxDNSMessageHeader = ( ( DNSMessage_t * )
pucUDPPayloadBuffer );
Expand Down Expand Up @@ -355,15 +357,15 @@
}
#endif

#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
#if ( ( ipconfigUSE_DNS_CACHE != 0 ) || ( ipconfigDNS_USE_CALLBACKS != 0 ) || ( ipconfigUSE_MDNS != 0 ) || ( ipconfigUSE_LLMNR != 0 ) )
if( x == 0U )
{
uxResult = DNS_ReadNameField( &xSet,
sizeof( xSet.pcName ) );
( void ) uxResult;
}
else
#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS || ipconfigUSE_MDNS || ipconfigUSE_LLMNR */
{
/* Skip the variable length pcName field. */
uxResult = DNS_SkipNameField( xSet.pucByte,
Expand Down Expand Up @@ -721,7 +723,7 @@
* fields of the structure. */

/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxDNSAnswerRecord = ( ( DNSAnswerRecord_t * ) pxSet->pucByte );

Expand Down Expand Up @@ -874,7 +876,7 @@
/* Cast the response to DNSAnswerRecord for easy access to fields of the DNS response. */

/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxDNSAnswerRecord = ( ( DNSAnswerRecord_t * ) pxSet->pucByte );

Expand Down
3 changes: 2 additions & 1 deletion source/include/FreeRTOS_DNS_Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@
uint16_t usClass; /**< Only the value 'dnsCLASS_IN' is recognised, which stands for "Internet". */
char * pcRequestedName; /**< A pointer to the full name of the host being looked up. */
#endif
#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) || ( ipconfigUSE_MDNS == 1 )

#if ( ( ipconfigUSE_DNS_CACHE != 0 ) || ( ipconfigDNS_USE_CALLBACKS != 0 ) || ( ipconfigUSE_MDNS != 0 ) || ( ipconfigUSE_LLMNR != 0 ) )
BaseType_t xDoStore; /**< Becomes true when a DNS reply was requested by this device,
* i.e. it has a matching request ID. */
char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ]; /**< A copy of the name that is mentioned in the questions. */
Expand Down

0 comments on commit 4f67761

Please sign in to comment.