From 078c6f1dc509ce341e314bfb5fb047a69756c253 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Wed, 14 May 2025 22:54:54 +0800 Subject: [PATCH] [posix] update dns `Resolver` to attempt all servers before reporting failure (#11495) This commit addresses a defect in the Resolver::Query function where it would error out if the attempt to send a DNS query to any single configured server failed. This could lead to query failures even if other DNS servers were available and operational. --- src/posix/platform/resolver.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/posix/platform/resolver.cpp b/src/posix/platform/resolver.cpp index 97b43ced9..1b86bd1fc 100644 --- a/src/posix/platform/resolver.cpp +++ b/src/posix/platform/resolver.cpp @@ -248,6 +248,8 @@ otError Resolver::SendQueryToServer(Transaction *aTxn, error = OT_ERROR_NO_ROUTE); } + LogInfo("Forwarded DNS query %p to %s", static_cast(aTxn), Ip6AddressString(&aServerAddress).AsCString()); + exit: return error; } @@ -255,9 +257,10 @@ exit: void Resolver::Query(otPlatDnsUpstreamQuery *aTxn, const otMessage *aQuery) { char packet[kMaxDnsMessageSize]; - otError error = OT_ERROR_NONE; - uint16_t length = otMessageGetLength(aQuery); - Transaction *txn = nullptr; + otError error = OT_ERROR_NONE; + uint16_t length = otMessageGetLength(aQuery); + uint32_t serverCount = 0; + Transaction *txn = nullptr; VerifyOrExit(length <= kMaxDnsMessageSize, error = OT_ERROR_NO_BUFS); VerifyOrExit(otMessageRead(aQuery, 0, &packet, sizeof(packet)) == length, error = OT_ERROR_NO_BUFS); @@ -268,22 +271,23 @@ void Resolver::Query(otPlatDnsUpstreamQuery *aTxn, const otMessage *aQuery) for (uint32_t i = 0; i < mRecursiveDnsServerCount; i++) { - SuccessOrExit(error = SendQueryToServer(txn, mRecursiveDnsServerList[i], packet, length)); - - LogInfo("Forwarded DNS query %p to %s", static_cast(aTxn), - Ip6AddressString(&mRecursiveDnsServerList[i]).AsCString()); + if (SendQueryToServer(txn, mRecursiveDnsServerList[i], packet, length) == OT_ERROR_NONE) + { + serverCount++; + } } for (uint32_t i = 0; i < mUpstreamDnsServerCount; i++) { - SuccessOrExit(error = SendQueryToServer(txn, mUpstreamDnsServerList[i], packet, length)); - - LogInfo("Forwarded DNS query %p to %s", static_cast(aTxn), - Ip6AddressString(&mUpstreamDnsServerList[i]).AsCString()); + if (SendQueryToServer(txn, mUpstreamDnsServerList[i], packet, length) == OT_ERROR_NONE) + { + serverCount++; + } } - LogInfo("Forwarded DNS query %p to %d server(s).", static_cast(aTxn), - mRecursiveDnsServerCount + mUpstreamDnsServerCount); + VerifyOrExit(serverCount > 0, error = OT_ERROR_NO_ROUTE); + + LogInfo("Forwarded DNS query %p to %u server(s).", static_cast(aTxn), serverCount); exit: if (error != OT_ERROR_NONE)