Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only define DNS_ReadNameField when cache is used #1201

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 87 additions & 84 deletions source/FreeRTOS_DNS_Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#if ( ipconfigUSE_DNS != 0 )


#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
ActoryOu marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Read the Name field out of a DNS response packet.
*
Expand All @@ -56,110 +58,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 */

/**
* @brief Simple routine that jumps over the NAME field of a resource record.
Expand Down