diff --git a/src/core/net/dnssd_server.cpp b/src/core/net/dnssd_server.cpp index 0623eaff0..1266d2c9c 100644 --- a/src/core/net/dnssd_server.cpp +++ b/src/core/net/dnssd_server.cpp @@ -366,11 +366,6 @@ Server::ResponseCode Server::Response::AddQuestionsFrom(const Request &aRequest) SuccessOrExit(Name(*aRequest.mMessage, kQueryNameOffset).AppendTo(*mMessage)); - // Check the name to include the correct domain name and determine - // the domain name offset (for DNS name compression). - - VerifyOrExit(ParseQueryName() == kErrorNone, rcode = Header::kResponseNameError); - mHeader.SetQuestionCount(aRequest.mHeader.GetQuestionCount()); offset = sizeof(Header); @@ -394,9 +389,22 @@ Server::ResponseCode Server::Response::AddQuestionsFrom(const Request &aRequest) SuccessOrExit(mMessage->Append(question)); } - rcode = Header::kResponseSuccess; + // Check the name to include the correct domain name and determine + // the domain name offset (for DNS name compression). + + rcode = (ParseQueryName() == kErrorNone) ? Header::kResponseSuccess : Header::kResponseNameError; exit: + if (rcode == Header::kResponseServerFailure) + { + // If we fail to add questions to the response message + // (no buffer), we clear the question count in the header and + // just include the header in the message. + + mHeader.SetQuestionCount(0); + IgnoreError(mMessage->SetLength(sizeof(Header))); + } + return rcode; } diff --git a/tests/unit/test_dns_client.cpp b/tests/unit/test_dns_client.cpp index 63e790a48..28a7eb5b3 100644 --- a/tests/unit/test_dns_client.cpp +++ b/tests/unit/test_dns_client.cpp @@ -238,8 +238,9 @@ void FinalizeTest(void) //--------------------------------------------------------------------------------------------------------------------- -static const char kHostName[] = "elden"; -static const char kHostFullName[] = "elden.default.service.arpa."; +static const char kHostName[] = "elden"; +static const char kHostFullName[] = "elden.default.service.arpa."; +static const char kNonExistingName[] = "noname.nodomain."; static const char kService1Name[] = "_srv._udp"; static const char kService1FullName[] = "_srv._udp.default.service.arpa."; @@ -804,6 +805,16 @@ void TestDnsClient(void) VerifyOrQuit(addresses.Contains(sAddressInfo.mHostAddresses[index])); } + //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + // Validate DNS Client `ResolveAddress()` for an invalid (non-existing) name + + sAddressInfo.Reset(); + Log("ResolveAddress(%s)", kNonExistingName); + SuccessOrQuit(dnsClient->ResolveAddress(kNonExistingName, AddressCallback, sInstance)); + AdvanceTime(100); + VerifyOrQuit(sAddressInfo.mCallbackCount == 1); + VerifyOrQuit(sAddressInfo.mError == kErrorNotFound); + //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Validate DNS Client `ResolveIp4Address()`