Skip to content

Commit e0189d1

Browse files
Tidying and local strncasecmp
1 parent decb55b commit e0189d1

1 file changed

Lines changed: 91 additions & 32 deletions

File tree

source/FreeRTOS_DNS_Parser.c

Lines changed: 91 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "NetworkBufferManagement.h"
4545

4646
#include <string.h>
47-
#include <strings.h>
4847

4948
#if ( ipconfigUSE_DNS != 0 )
5049

@@ -238,6 +237,46 @@
238237

239238

240239
#if ( ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_MDNS != 0 ) )
240+
/**
241+
* @brief Local implementation of posix strncasecmp
242+
*/
243+
static int local_strncasecmp( const char * s1,
244+
const char * s2,
245+
size_t n )
246+
{
247+
while( n-- != 0 )
248+
{
249+
char c1 = *s1++;
250+
char c2 = *s2++;
251+
252+
if( ( c1 >= 'A' ) && ( c1 <= 'Z' ) )
253+
{
254+
c1 += 'a' - 'A';
255+
}
256+
257+
if( ( c2 >= 'A' ) && ( c2 <= 'Z' ) )
258+
{
259+
c2 += 'a' - 'A';
260+
}
261+
262+
if( c1 > c2 )
263+
{
264+
return 1;
265+
}
266+
267+
if( c2 > c1 )
268+
{
269+
return -1;
270+
}
271+
272+
if( c1 == '\0' )
273+
{
274+
break;
275+
}
276+
}
277+
278+
return 0;
279+
}
241280

242281
/**
243282
* @brief Compare a DNS label sequence with a dot-separated name string.
@@ -349,7 +388,7 @@
349388
}
350389

351390
/* The dot string should have a segment of the same length at this point. */
352-
ulComparison = strncasecmp( ( char const * ) pcDnsSegment, pcDotSegment, uxSegmentLength );
391+
ulComparison = local_strncasecmp( ( char const * ) pcDnsSegment, pcDotSegment, uxSegmentLength );
353392

354393
if( ulComparison != 0 )
355394
{
@@ -431,8 +470,8 @@
431470
* - pxDNSRecords and uxDNSRecordCount
432471
*
433472
* @param[in,out] xSet a set of variables that are shared among the helper functions.
434-
* @param[in] pxEndPoint The end-point on which the DNS message was received.
435-
* Necessary when LLMNR/MDNS are enabled, and IPv4_BACKWARD_COMPATIBLE is 0.
473+
* @param[in] pxEndPoint The endpoint on which the DNS message was received.
474+
* Necessary when LLMNR/MDNS are enabled
436475
* Otherwise may be NULL.
437476
* @return pdTRUE if everything went okay
438477
*/
@@ -442,17 +481,13 @@
442481
UBaseType_t x;
443482
size_t uxResult;
444483

445-
( void ) pxEndPoint;
446484
#if ( ( ipconfigUSE_LLMNR == 1 ) || ( ipconfigUSE_MDNS == 1 ) )
447-
#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 )
448-
NetworkEndPoint_t xEndPoint;
449-
configASSERT( pxEndPoint != NULL );
485+
NetworkEndPoint_t xEndPoint;
486+
configASSERT( pxEndPoint != NULL );
450487

451-
/* Make a copy of the end-point because xApplicationDNSQueryHook() is allowed
452-
* to write into it. */
453-
( void ) memcpy( &( xEndPoint ), pxEndPoint, sizeof( xEndPoint ) );
454-
#else
455-
#endif
488+
/* Make a copy of the end-point because xApplicationDNSQueryHook() is allowed
489+
* to write into it. */
490+
( void ) memcpy( &( xEndPoint ), pxEndPoint, sizeof( xEndPoint ) );
456491

457492
#if ( ipconfigDNSQuery_BACKWARD_COMPATIBLE == 1 )
458493
xSet->uxDNSRecordCount = 0;
@@ -470,6 +505,8 @@
470505
#endif /* if ( ipconfigDNSQuery_BACKWARD_COMPATIBLE == 1 ) */
471506
#endif /* if ( ( ipconfigUSE_LLMNR == 1 ) || ( ipconfigUSE_MDNS == 1 ) ) */
472507

508+
( void ) pxEndPoint;
509+
473510
for( x = 0U; x < xSet->usQuestions; x++ )
474511
{
475512
#if ( ( ipconfigUSE_LLMNR == 1 ) || ( ipconfigUSE_MDNS == 1 ) )
@@ -517,11 +554,12 @@
517554
xSet->usType = usChar2u16( xSet->pucByte );
518555
xSet->usClass = usChar2u16( &( xSet->pucByte[ 2 ] ) );
519556

557+
/* This should have been set by now. */
558+
configASSERT( xSet->pxDNSRecords != NULL );
559+
520560
#if ( ( ipconfigDNSQuery_BACKWARD_COMPATIBLE == 1 ) )
521561
{
522562
( void ) i;
523-
/* This should have been set in the main DNS function */
524-
configASSERT( xSet->pxDNSRecords != NULL );
525563

526564
if( x == 0U )
527565
{
@@ -535,7 +573,10 @@
535573
{
536574
xSet->xDNSRecordsMatched = pdTRUE;
537575
#if ( ( ipconfigUSE_IPv6 != 0 ) )
538-
if( ( xSet->usType == dnsTYPE_AAAA_HOST ) || ( xSet->usType == dnsTYPE_ANY_HOST ) )
576+
if(
577+
( xEndPoint.bits.bIPv6 != pdFALSE ) &&
578+
( ( xSet->usType == dnsTYPE_AAAA_HOST ) ||
579+
( xSet->usType == dnsTYPE_ANY_HOST ) ) )
539580
{
540581
xSet->pxDNSRecords[ xSet->uxDNSRecordCount++ ] =
541582
( DNSRecord_t ) {
@@ -544,9 +585,12 @@
544585
.uxIncludeInAnswer = pdTRUE,
545586
};
546587
}
547-
#endif
588+
#endif /* if ( ( ipconfigUSE_IPv6 != 0 ) ) */
548589
#if ( ( ipconfigUSE_IPv4 != 0 ) )
549-
if( ( xSet->usType == dnsTYPE_A_HOST ) || ( xSet->usType == dnsTYPE_ANY_HOST ) )
590+
if(
591+
( xEndPoint.ipv4_settings.ulIPAddress != 0U ) &&
592+
( ( xSet->usType == dnsTYPE_A_HOST ) ||
593+
( xSet->usType == dnsTYPE_ANY_HOST ) ) )
550594
{
551595
xSet->pxDNSRecords[ xSet->uxDNSRecordCount++ ] =
552596
( DNSRecord_t ) {
@@ -555,7 +599,7 @@
555599
.uxIncludeInAnswer = pdTRUE,
556600
};
557601
}
558-
#endif
602+
#endif /* if ( ( ipconfigUSE_IPv4 != 0 ) ) */
559603
}
560604
}
561605
}
@@ -581,7 +625,7 @@
581625
break;
582626

583627
default:
584-
FreeRTOS_printf( ( "DNS_ParseDNSReply: Unsupported record type %u\n", pRecord->usRecordType ) );
628+
FreeRTOS_printf( ( "parseDNSQuestions: Unsupported record type %u\n", pRecord->usRecordType ) );
585629
/* Unsupported record type. Skip. */
586630
continue;
587631
}
@@ -595,6 +639,18 @@
595639

596640
if( ( xTypeMatch == pdTRUE ) && ( xNameMatch == pdTRUE ) )
597641
{
642+
if( ( pRecord->usRecordType == dnsTYPE_A_HOST ) && ( xEndPoint.ipv4_settings.ulIPAddress == 0U ) )
643+
{
644+
FreeRTOS_printf( ( "parseDNSQuestions: No IPV4 address, skipping A record even though it matches.\n" ) );
645+
continue;
646+
}
647+
648+
if( ( pRecord->usRecordType == dnsTYPE_AAAA_HOST ) && ( xEndPoint.bits.bIPv6 == pdFALSE ) )
649+
{
650+
FreeRTOS_printf( ( "parseDNSQuestions: No IPV6 address, skipping AAAA record even though it matches.\n" ) );
651+
continue;
652+
}
653+
598654
pRecord->uxIncludeInAnswer = pdTRUE;
599655
xSet->xDNSRecordsMatched = pdTRUE;
600656
}
@@ -777,11 +833,11 @@
777833
UBaseType_t const uxUDPOffset = ( UBaseType_t ) ( pucUDPPayloadBuffer - pxNetworkBuffer->pucEthernetBuffer );
778834
UBaseType_t uxExtraSize = 0;
779835
UBaseType_t uxDataLength;
780-
UBaseType_t pxNumAnswers = 0;
836+
UBaseType_t uxNumAnswers = 0;
781837
uint8_t * pucNewBuffer = NULL;
782838
uint8_t * start_of_dns_answers;
783839
UBaseType_t uxIsLLMNR;
784-
BaseType_t usLength;
840+
BaseType_t uxLength;
785841
configASSERT( ( uxUDPOffset == ipUDP_PAYLOAD_OFFSET_IPv4 ) || ( uxUDPOffset == ipUDP_PAYLOAD_OFFSET_IPv6 ) );
786842

787843
uxDataLength = uxBufferLength +
@@ -799,7 +855,7 @@
799855
continue;
800856
}
801857

802-
pxNumAnswers++;
858+
uxNumAnswers++;
803859
uxExtraSize += strlen( pRecord->pcName ) + 2; /* Name */
804860
uxExtraSize += 2; /* Type */
805861
uxExtraSize += 2; /* Class */
@@ -891,9 +947,13 @@
891947
}
892948

893949
/* We leave 'usIdentifier' and 'usQuestions' untouched */
894-
vSetField16( xSet.pxDNSMessageHeader, DNSMessage_t, usFlags, dnsLLMNR_FLAGS_IS_RESPONSE ); /* Set the response flag */
895-
vSetField16( xSet.pxDNSMessageHeader, DNSMessage_t, usAnswers, pxNumAnswers );
896-
vSetField16( xSet.pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 ); /* No authority */
950+
vSetField16(
951+
xSet.pxDNSMessageHeader,
952+
DNSMessage_t,
953+
usFlags,
954+
xSet.usPortNumber == ipLLMNR_PORT ? dnsLLMNR_FLAGS_IS_RESPONSE : dnsMDNS_FLAGS_IS_RESPONSE ); /* Set the response flag */
955+
vSetField16( xSet.pxDNSMessageHeader, DNSMessage_t, usAnswers, uxNumAnswers );
956+
vSetField16( xSet.pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 ); /* No authority */
897957
vSetField16( xSet.pxDNSMessageHeader, DNSMessage_t, usAdditionalRRs, 0 );
898958

899959
start_of_dns_answers = xSet.pucByte;
@@ -972,10 +1032,7 @@
9721032

9731033
case dnsTYPE_TXT:
9741034
{
975-
size_t xTextLength;
976-
vSetField16( middle, MDNSResponseMiddle_t, usDataLength, strlen( record->xData.pcTxtRecord ) + 1 );
977-
xSet.pucByte += sizeof( *middle );
978-
xTextLength = strlen( record->xData.pcTxtRecord );
1035+
size_t xTextLength = strlen( record->xData.pcTxtRecord );
9791036

9801037
if( xTextLength > 255 )
9811038
{
@@ -985,6 +1042,8 @@
9851042
break;
9861043
}
9871044

1045+
vSetField16( middle, MDNSResponseMiddle_t, usDataLength, strlen( record->xData.pcTxtRecord ) + 1 );
1046+
xSet.pucByte += sizeof( *middle );
9881047
*xSet.pucByte++ = ( uint8_t ) xTextLength;
9891048
memcpy( xSet.pucByte, record->xData.pcTxtRecord, xTextLength );
9901049
xSet.pucByte += xTextLength;
@@ -1021,8 +1080,8 @@
10211080
vSetField16( xSet.pxDNSMessageHeader, DNSMessage_t, usQuestions, 0 );
10221081
}
10231082

1024-
usLength = ( BaseType_t ) ( xSet.pucByte - pucNewBuffer );
1025-
prepareReplyDNSMessage( pxNetworkBuffer, usLength );
1083+
uxLength = ( BaseType_t ) ( xSet.pucByte - pucNewBuffer );
1084+
prepareReplyDNSMessage( pxNetworkBuffer, uxLength );
10261085
/* This function will fill in the eth addresses and send the packet */
10271086
vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );
10281087
}

0 commit comments

Comments
 (0)