QDnsLookup: centralize printing of warnings

Any resolution error that is caused by an invalid request, invalid reply
(only happens on Unix), or a system error can be printed as a warning,
using category "qt.network.dnslookup". Those warnings are disabled by
default.

Change-Id: I5f7f427ded124479baa6fffd175fc40b3e21c969
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Thiago Macieira 2023-05-16 16:38:58 -07:00
parent f5b584022b
commit ecaf93c366
4 changed files with 45 additions and 15 deletions

View File

@ -8,6 +8,7 @@
#include <qapplicationstatic.h> #include <qapplicationstatic.h>
#include <qcoreapplication.h> #include <qcoreapplication.h>
#include <qdatetime.h> #include <qdatetime.h>
#include <qloggingcategory.h>
#include <qrandom.h> #include <qrandom.h>
#include <qurl.h> #include <qurl.h>
@ -15,6 +16,8 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
static Q_LOGGING_CATEGORY(lcDnsLookup, "qt.network.dnslookup", QtCriticalMsg)
namespace { namespace {
struct QDnsLookupThreadPool : QThreadPool struct QDnsLookupThreadPool : QThreadPool
{ {
@ -544,6 +547,7 @@ void QDnsLookup::lookup()
d->isFinished = false; d->isFinished = false;
d->reply = QDnsLookupReply(); d->reply = QDnsLookupReply();
if (!QCoreApplication::instance()) { if (!QCoreApplication::instance()) {
// NOT qCWarning because this isn't a result of the lookup
qWarning("QDnsLookup requires a QCoreApplication"); qWarning("QDnsLookup requires a QCoreApplication");
return; return;
} }
@ -1057,20 +1061,47 @@ void QDnsLookupRunnable::run()
if (qsizetype n = requestName.size(); n > MaxDomainNameLength || n == 0) { if (qsizetype n = requestName.size(); n > MaxDomainNameLength || n == 0) {
reply.error = QDnsLookup::InvalidRequestError; reply.error = QDnsLookup::InvalidRequestError;
reply.errorString = QDnsLookup::tr("Invalid domain name"); reply.errorString = QDnsLookup::tr("Invalid domain name");
emit finished(reply); } else {
if (n) // Perform request.
qWarning("QDnsLookup: domain name being looked up is too long (%lld bytes)", n); query(&reply);
return;
// Sort results.
qt_qdnsmailexchangerecord_sort(reply.mailExchangeRecords);
qt_qdnsservicerecord_sort(reply.serviceRecords);
} }
// Perform request.
query(&reply);
// Sort results.
qt_qdnsmailexchangerecord_sort(reply.mailExchangeRecords);
qt_qdnsservicerecord_sort(reply.serviceRecords);
emit finished(reply); emit finished(reply);
// maybe print the lookup error as warning
switch (reply.error) {
case QDnsLookup::NoError:
case QDnsLookup::OperationCancelledError:
case QDnsLookup::NotFoundError:
case QDnsLookup::ServerFailureError:
case QDnsLookup::ServerRefusedError:
break; // no warning for these
case QDnsLookup::ResolverError:
case QDnsLookup::InvalidRequestError:
case QDnsLookup::InvalidReplyError:
qCWarning(lcDnsLookup()).nospace()
<< "DNS lookup failed (" << reply.error << "): "
<< qUtf16Printable(reply.errorString)
<< "; request was " << this; // continues below
}
}
inline QDebug operator<<(QDebug &d, QDnsLookupRunnable *r)
{
// continued: print the information about the request
d << r->requestName.left(MaxDomainNameLength);
if (r->requestName.size() > MaxDomainNameLength)
d << "... (truncated)";
d << " type " << r->requestType;
if (!r->nameserver.isNull())
d << " to nameserver " << qUtf16Printable(r->nameserver.toString())
<< " port " << (r->port ? r->port : DnsPort);
return d;
} }
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -7,7 +7,6 @@ QT_BEGIN_NAMESPACE
void QDnsLookupRunnable::query(QDnsLookupReply *reply) void QDnsLookupRunnable::query(QDnsLookupReply *reply)
{ {
qWarning("Not yet supported on this OS");
reply->error = QDnsLookup::ResolverError; reply->error = QDnsLookup::ResolverError;
reply->errorString = tr("Not yet supported on this OS"); reply->errorString = tr("Not yet supported on this OS");
return; return;

View File

@ -36,6 +36,7 @@ constexpr qsizetype MaxDomainNameLength = 255;
constexpr quint16 DnsPort = 53; constexpr quint16 DnsPort = 53;
class QDnsLookupRunnable; class QDnsLookupRunnable;
QDebug operator<<(QDebug &, QDnsLookupRunnable *);
class QDnsLookupReply class QDnsLookupReply
{ {
@ -179,6 +180,7 @@ private:
QHostAddress nameserver; QHostAddress nameserver;
QDnsLookup::Type requestType; QDnsLookup::Type requestType;
quint16 port; quint16 port;
friend QDebug operator<<(QDebug &, QDnsLookupRunnable *);
}; };
class QDnsRecordPrivate : public QSharedData class QDnsRecordPrivate : public QSharedData

View File

@ -109,11 +109,9 @@ void QDnsLookupRunnable::query(QDnsLookupReply *reply)
auto guard = qScopeGuard([&] { res_nclose(&state); }); auto guard = qScopeGuard([&] { res_nclose(&state); });
//Check if a nameserver was set. If so, use it //Check if a nameserver was set. If so, use it
if (!applyNameServer(&state, nameserver, port)) { if (!applyNameServer(&state, nameserver, port))
qWarning("QDnsLookup: %s", "IPv6 nameservers are currently not supported on this OS");
return reply->setError(QDnsLookup::ResolverError, return reply->setError(QDnsLookup::ResolverError,
QDnsLookup::tr("IPv6 nameservers are currently not supported on this OS")); QDnsLookup::tr("IPv6 nameservers are currently not supported on this OS"));
}
#ifdef QDNSLOOKUP_DEBUG #ifdef QDNSLOOKUP_DEBUG
state.options |= RES_DEBUG; state.options |= RES_DEBUG;
#endif #endif