From ae302ef0163ff733ba665dad4d88d800acaabf9f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 18 Apr 2024 11:05:26 -0700 Subject: [PATCH] QDnsLookup/Unix: handle more error conditions from res_nsend() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It does set ECONNREFUSED explicitly. So let's reuse ServerRefusedError for it too (it was so far only used when we got a REFUSED DNS answer), which allows tst_QDnsLookup to handle this as an ignorable failure. For almost everything else, it sets ETIMEDOUT to indicate "no answer", which isn't useful. So let's inspect the reply to see if it has something. Change-Id: I455fe22ef4ad4b2f9b01fffd17c771ffa4a998be Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit f19e9f2521ff7784223ec34fc6794583f4faa2a5) Reviewed-by: Qt Cherry-pick Bot --- src/network/kernel/qdnslookup_unix.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/network/kernel/qdnslookup_unix.cpp b/src/network/kernel/qdnslookup_unix.cpp index 57ac430ea15..6cdd4e3adfd 100644 --- a/src/network/kernel/qdnslookup_unix.cpp +++ b/src/network/kernel/qdnslookup_unix.cpp @@ -183,14 +183,22 @@ void QDnsLookupRunnable::query(QDnsLookupReply *reply) auto attemptToSend = [&]() { std::memset(buffer.data(), 0, HFIXEDSZ); // the header is enough int responseLength = res_nsend(&state, qbuffer.data(), queryLength, buffer.data(), buffer.size()); - if (responseLength < 0) { - // network error of some sort - if (errno == ETIMEDOUT) - reply->makeTimeoutError(); - else - reply->makeResolverSystemError(); - } - return responseLength; + if (responseLength >= 0) + return responseLength; // success + + // libresolv uses ETIMEDOUT for resolver errors ("no answer") + if (errno == ECONNREFUSED) + reply->setError(QDnsLookup::ServerRefusedError, qt_error_string()); + else if (errno != ETIMEDOUT) + reply->makeResolverSystemError(); // some other error + + auto query = reinterpret_cast
(qbuffer.data()); + auto header = reinterpret_cast
(buffer.data()); + if (query->id == header->id && header->qr) + reply->makeDnsRcodeError(header->rcode); + else + reply->makeTimeoutError(); // must really be a timeout + return -1; }; // strictly use UDP, we'll deal with truncated replies ourselves