Skip to content

Commit

Permalink
Add macro checks for DNS Cache to fix build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kar-rahul-aws committed Oct 28, 2024
1 parent 57c1967 commit dfb94b4
Showing 1 changed file with 109 additions and 97 deletions.
206 changes: 109 additions & 97 deletions source/FreeRTOS_DNS_Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,110 +56,114 @@
*
* @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 ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) )

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 == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) ) */

/**
* @brief Simple routine that jumps over the NAME field of a resource record.
Expand Down Expand Up @@ -456,19 +460,23 @@
#if ( ipconfigUSE_IPv6 != 0 )
{
/*logging*/
FreeRTOS_printf( ( "prvParseDNS_HandleLLMNRRequest[%s]: type %04X\n", xSet.pcName, xSet.usType ) );
#if ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) )
FreeRTOS_printf( ( "prvParseDNS_HandleLLMNRRequest[%s]: type %04X\n", xSet.pcName, xSet.usType ) );
#endif /* ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) ) */

xEndPoint.usDNSType = ( uint8_t ) xSet.usType;
}
#endif /* ( ipconfigUSE_IPv6 != 0 ) */

/* If this is not a reply to our DNS request, it might be an mDNS or an LLMNR
* request. Ask the application if it uses the name. */
#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 )
xDNSHookReturn = xApplicationDNSQueryHook( xSet.pcName );
#else
xDNSHookReturn = xApplicationDNSQueryHook_Multi( &xEndPoint, xSet.pcName );
#endif
#if ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) )
#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 )
xDNSHookReturn = xApplicationDNSQueryHook( xSet.pcName );
#else
xDNSHookReturn = xApplicationDNSQueryHook_Multi( &xEndPoint, xSet.pcName );
#endif /* ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 ) */
#endif /* ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) ) */

/* During the early stages of boot or after a DHCP lease expires, our end-point
* may have an IP address of 0.0.0.0. Do not respond to name queries with that address. */
Expand Down Expand Up @@ -735,10 +743,12 @@
&( pxSet->pucByte[ sizeof( DNSAnswerRecord_t ) ] ),
ipSIZE_OF_IPv6_ADDRESS );

if( ppxAddressInfo != NULL )
{
pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET6, xIP_Address.xIPAddress.xIP_IPv6.ucBytes );
}
#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
if( ppxAddressInfo != NULL )
{
pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET6, xIP_Address.xIPAddress.xIP_IPv6.ucBytes );
}
#endif /* ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) */

xIP_Address.xIs_IPv6 = pdTRUE;

Expand All @@ -763,12 +773,14 @@
pvCopyDest = &( pxSet->ulIPAddress );
( void ) memcpy( pvCopyDest, pvCopySource, pxSet->uxAddressLength );

if( ppxAddressInfo != NULL )
{
const uint8_t * ucBytes = ( uint8_t * ) &( pxSet->ulIPAddress );
#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
if( ppxAddressInfo != NULL )
{
const uint8_t * ucBytes = ( uint8_t * ) &( pxSet->ulIPAddress );

pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET4, ucBytes );
}
pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET4, ucBytes );
}
#endif /* ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) */

xIP_Address.xIPAddress.ulIP_IPv4 = pxSet->ulIPAddress;
xIP_Address.xIs_IPv6 = pdFALSE;
Expand Down

0 comments on commit dfb94b4

Please sign in to comment.