From 1a4037f232f72480d7b54d6d3d1f17fa806b9317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 14 Nov 2025 21:47:35 +0100 Subject: [PATCH] ping.cpp: resolve DNS failures and cleanup --- libnymea/network/ping.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libnymea/network/ping.cpp b/libnymea/network/ping.cpp index cfee144f..714affda 100644 --- a/libnymea/network/ping.cpp +++ b/libnymea/network/ping.cpp @@ -191,6 +191,13 @@ void Ping::performPing(PingReply *reply) // Get host ip address struct hostent *hostname = gethostbyname(reply->targetHostAddress().toString().toLocal8Bit().constData()); + if (!hostname) { + m_error = PingReply::ErrorHostNameLookupFailed; + qCWarning(dcPing()) << "Failed to resolve host" << reply->targetHostAddress().toString() << hstrerror(h_errno); + finishReply(reply, m_error); + return; + } + struct sockaddr_in pingAddress; memset(&pingAddress, 0, sizeof(pingAddress)); pingAddress.sin_family = hostname->h_addrtype; @@ -421,6 +428,12 @@ void Ping::cleanUpReply(PingReply *reply) QHostInfo::abortHostLookup(lookupId); m_pendingHostNameLookups.remove(lookupId); } + + if (m_pendingHostAddressLookups.values().contains(reply)) { + int lookupId = m_pendingHostAddressLookups.key(reply); + QHostInfo::abortHostLookup(lookupId); + m_pendingHostAddressLookups.remove(lookupId); + } } void Ping::onSocketReadyRead(int socketDescriptor) @@ -493,6 +506,8 @@ void Ping::onSocketReadyRead(int socketDescriptor) << "Time:" << reply->duration() << "[ms]"; if (reply->doHostLookup()) { + // Prevent timeout while waiting for the hostname lookup to finish + reply->m_timer->stop(); // Note: due to a Qt bug < 5.9 we need to use old SLOT style and cannot make use of lambda here int lookupId = QHostInfo::lookupHost(senderAddress.toString(), this, SLOT(onHostLookupFinished(QHostInfo))); m_pendingHostNameLookups.insert(lookupId, reply);