1
0
mirror of https://github.com/FreeRTOS/FreeRTOS-Plus-TCP synced 2025-10-23 01:18:54 +08:00

Fix DNS to use correct DNS IP preference (#1109)

* Fix xDNS_IP_Preference to be IPv4 in the else case

* Fix UTs

* Fix DNS IP preference

* Uncrustify: triggered by comment

* Fix UTs

---------

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tony Josi
2024-02-28 13:03:52 +05:30
committed by GitHub
parent b098c07b98
commit f50a76b937
2 changed files with 44 additions and 42 deletions

View File

@@ -843,10 +843,6 @@
BaseType_t bHasLocal = pdFALSE; BaseType_t bHasLocal = pdFALSE;
const char * pcDot = ( const char * ) strchr( pcHostName, ( int32_t ) '.' ); const char * pcDot = ( const char * ) strchr( pcHostName, ( int32_t ) '.' );
#if ( ipconfigUSE_MDNS == 1 ) || ( ipconfigUSE_LLMNR == 1 )
BaseType_t xNeed_Endpoint = pdFALSE;
#endif
#if ( ipconfigUSE_LLMNR != 1 ) #if ( ipconfigUSE_LLMNR != 1 )
( void ) pcHostName; ( void ) pcHostName;
#endif #endif
@@ -892,9 +888,8 @@
pxAddress->sin_port = ipMDNS_PORT; pxAddress->sin_port = ipMDNS_PORT;
pxAddress->sin_port = FreeRTOS_ntohs( pxAddress->sin_port ); pxAddress->sin_port = FreeRTOS_ntohs( pxAddress->sin_port );
xNeed_Endpoint = pdTRUE;
switch( xDNS_IP_Preference ) switch( xDNS_IP_Preference ) /* LCOV_EXCL_BR_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
{ {
#if ( ipconfigUSE_IPv4 != 0 ) #if ( ipconfigUSE_IPv4 != 0 )
case xPreferenceIPv4: case xPreferenceIPv4:
@@ -912,11 +907,10 @@
break; break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */ #endif /* ( ipconfigUSE_IPv6 != 0 ) */
default: default: /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
/* MISRA 16.4 Compliance */ /* MISRA 16.4 Compliance */
xNeed_Endpoint = pdFALSE;
FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference \n" ) ); FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference \n" ) );
break; break; /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
} }
} }
} }
@@ -929,9 +923,8 @@
/* Use LLMNR addressing. */ /* Use LLMNR addressing. */
pxAddress->sin_port = ipLLMNR_PORT; pxAddress->sin_port = ipLLMNR_PORT;
pxAddress->sin_port = FreeRTOS_ntohs( pxAddress->sin_port ); pxAddress->sin_port = FreeRTOS_ntohs( pxAddress->sin_port );
xNeed_Endpoint = pdTRUE;
switch( xDNS_IP_Preference ) switch( xDNS_IP_Preference ) /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
{ {
#if ( ipconfigUSE_IPv4 != 0 ) #if ( ipconfigUSE_IPv4 != 0 )
case xPreferenceIPv4: case xPreferenceIPv4:
@@ -949,45 +942,41 @@
break; break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */ #endif /* ( ipconfigUSE_IPv6 != 0 ) */
default: default: /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
/* MISRA 16.4 Compliance */ /* MISRA 16.4 Compliance */
xNeed_Endpoint = pdFALSE;
FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference (LLMNR) \n" ) ); FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference (LLMNR) \n" ) );
break; break; /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
} }
} }
} }
#endif /* if ( ipconfigUSE_LLMNR == 1 ) */ #endif /* if ( ipconfigUSE_LLMNR == 1 ) */
#if ( ipconfigUSE_MDNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) #if ( ipconfigUSE_MDNS == 1 ) || ( ipconfigUSE_LLMNR == 1 )
if( xNeed_Endpoint == pdTRUE ) for( pxEndPoint = FreeRTOS_FirstEndPoint( NULL );
pxEndPoint != NULL;
pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint ) )
{ {
for( pxEndPoint = FreeRTOS_FirstEndPoint( NULL ); #if ( ipconfigUSE_IPv6 != 0 )
pxEndPoint != NULL; if( xDNS_IP_Preference == xPreferenceIPv6 )
pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint ) ) {
{ if( pxEndPoint->bits.bIPv6 != 0U )
#if ( ipconfigUSE_IPv6 != 0 )
if( xDNS_IP_Preference == xPreferenceIPv6 )
{ {
if( pxEndPoint->bits.bIPv6 != 0U ) break;
}
}
else
{
#if ( ipconfigUSE_IPv4 != 0 )
if( pxEndPoint->bits.bIPv6 == 0U )
{ {
break; break;
} }
} #endif /* if ( ipconfigUSE_IPv4 != 0 ) */
else }
{ #else /* if ( ipconfigUSE_IPv6 != 0 ) */
#if ( ipconfigUSE_IPv4 != 0 ) /* IPv6 is not included, so all end-points are IPv4. */
if( pxEndPoint->bits.bIPv6 == 0U ) break;
{ #endif /* if ( ipconfigUSE_IPv6 != 0 ) */
break;
}
#endif /* if ( ipconfigUSE_IPv4 != 0 ) */
}
#else /* if ( ipconfigUSE_IPv6 != 0 ) */
/* IPv6 is not included, so all end-points are IPv4. */
break;
#endif /* if ( ipconfigUSE_IPv6 != 0 ) */
}
} }
#endif /* if ( ipconfigUSE_MDNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) */ #endif /* if ( ipconfigUSE_MDNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) */
} }
@@ -1000,7 +989,7 @@
pxEndPoint != NULL; pxEndPoint != NULL;
pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint ) ) pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint ) )
{ {
switch( xDNS_IP_Preference ) switch( xDNS_IP_Preference ) /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
{ {
#if ( ipconfigUSE_IPv4 != 0 ) #if ( ipconfigUSE_IPv4 != 0 )
case xPreferenceIPv4: case xPreferenceIPv4:
@@ -1047,10 +1036,10 @@
break; break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */ #endif /* ( ipconfigUSE_IPv6 != 0 ) */
default: default: /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
/* MISRA 16.4 Compliance */ /* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference \n" ) ); FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference \n" ) );
break; break; /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
} }
if( xBreakLoop == pdTRUE ) if( xBreakLoop == pdTRUE )
@@ -1264,6 +1253,10 @@
{ {
xDNS_IP_Preference = xPreferenceIPv6; xDNS_IP_Preference = xPreferenceIPv6;
} }
else
{
xDNS_IP_Preference = xPreferenceIPv4;
}
#endif /* ( ipconfigUSE_IPv6 != 0 ) */ #endif /* ( ipconfigUSE_IPv6 != 0 ) */
pxEndPoint = prvFillSockAddress( &xAddress, pcHostName ); pxEndPoint = prvFillSockAddress( &xAddress, pcHostName );

View File

@@ -1699,6 +1699,12 @@ void test_FreeRTOS_getaddrinfo_a_IPv4Random_LocalDNSUnknownPreference( void )
/* In prvGetHostByName */ /* In prvGetHostByName */
DNS_CreateSocket_ExpectAnyArgsAndReturn( &xDNSSocket ); DNS_CreateSocket_ExpectAnyArgsAndReturn( &xDNSSocket );
FreeRTOS_FirstEndPoint_ExpectAndReturn( NULL, &xEndPoint );
DNS_BindSocket_ExpectAnyArgsAndReturn( 0 );
pxGetNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( &xNetworkBuffer );
DNS_SendRequest_ExpectAnyArgsAndReturn( pdPASS );
DNS_ReadReply_ExpectAnyArgsAndReturn( ipconfigNETWORK_MTU );
/* Back prvGetHostByName */ /* Back prvGetHostByName */
DNS_CloseSocket_Expect( &xDNSSocket ); DNS_CloseSocket_Expect( &xDNSSocket );
@@ -1909,8 +1915,11 @@ void test_FreeRTOS_getaddrinfo_a_IPv4Random_LLMNRDNSUnknownPreference( void )
/* In prvGetHostByName */ /* In prvGetHostByName */
DNS_CreateSocket_ExpectAnyArgsAndReturn( &xDNSSocket ); DNS_CreateSocket_ExpectAnyArgsAndReturn( &xDNSSocket );
/* In prvGetHostByNameOp */ FreeRTOS_FirstEndPoint_ExpectAndReturn( NULL, &xEndPoint );
/* In prvFillSockAddress */ DNS_BindSocket_ExpectAnyArgsAndReturn( 0 );
pxGetNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( &xNetworkBuffer );
DNS_SendRequest_ExpectAnyArgsAndReturn( pdPASS );
DNS_ReadReply_ExpectAnyArgsAndReturn( ipconfigNETWORK_MTU );
/* Back prvGetHostByName */ /* Back prvGetHostByName */
DNS_CloseSocket_Expect( &xDNSSocket ); DNS_CloseSocket_Expect( &xDNSSocket );