|
|
|
@ -28,19 +28,19 @@
@@ -28,19 +28,19 @@
|
|
|
|
|
|
|
|
|
|
#include "reverseresolution.h" |
|
|
|
|
|
|
|
|
|
#include <QDebug> |
|
|
|
|
#include <QHostAddress> |
|
|
|
|
#include <QHostInfo> |
|
|
|
|
#include <QString> |
|
|
|
|
|
|
|
|
|
const int CACHE_SIZE = 500; |
|
|
|
|
const int CACHE_SIZE = 2048; |
|
|
|
|
|
|
|
|
|
using namespace Net; |
|
|
|
|
|
|
|
|
|
namespace |
|
|
|
|
{ |
|
|
|
|
bool isUsefulHostName(const QString &hostname, const QString &ip) |
|
|
|
|
bool isUsefulHostName(const QString &hostname, const QHostAddress &ip) |
|
|
|
|
{ |
|
|
|
|
return (!hostname.isEmpty() && (hostname != ip)); |
|
|
|
|
return (!hostname.isEmpty() && (hostname != ip.toString())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -52,37 +52,40 @@ ReverseResolution::ReverseResolution(QObject *parent)
@@ -52,37 +52,40 @@ ReverseResolution::ReverseResolution(QObject *parent)
|
|
|
|
|
|
|
|
|
|
ReverseResolution::~ReverseResolution() |
|
|
|
|
{ |
|
|
|
|
qDebug("Deleting host name resolver..."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ReverseResolution::resolve(const QString &ip) |
|
|
|
|
void ReverseResolution::resolve(const QHostAddress &ip) |
|
|
|
|
{ |
|
|
|
|
if (m_cache.contains(ip)) { |
|
|
|
|
const QString &hostname = *m_cache.object(ip); |
|
|
|
|
qDebug() << "Resolved host name using cache: " << ip << " -> " << hostname; |
|
|
|
|
if (isUsefulHostName(hostname, ip)) |
|
|
|
|
emit ipResolved(ip, hostname); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
// Actually resolve the ip
|
|
|
|
|
m_lookups.insert(QHostInfo::lookupHost(ip, this, &ReverseResolution::hostResolved), ip); |
|
|
|
|
if (ip.isNull()) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
const QString *hostname = m_cache.object(ip); |
|
|
|
|
if (hostname) { |
|
|
|
|
if (hostname->isEmpty()) |
|
|
|
|
; // resolution didn't get meaningful results, so do nothing
|
|
|
|
|
else |
|
|
|
|
emit ipResolved(ip, *hostname); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// do reverse lookup: IP -> hostname
|
|
|
|
|
const int lookupId = QHostInfo::lookupHost(ip.toString(), this, &ReverseResolution::hostResolved); |
|
|
|
|
m_lookups.insert(lookupId, ip); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ReverseResolution::hostResolved(const QHostInfo &host) |
|
|
|
|
{ |
|
|
|
|
const QString ip = m_lookups.take(host.lookupId()); |
|
|
|
|
Q_ASSERT(!ip.isNull()); |
|
|
|
|
const QHostAddress ip = m_lookups.take(host.lookupId()); |
|
|
|
|
|
|
|
|
|
if (host.error() != QHostInfo::NoError) { |
|
|
|
|
qDebug() << "DNS Reverse resolution error: " << host.errorString(); |
|
|
|
|
if (host.error() != QHostInfo::NoError) |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const QString hostname = host.hostName(); |
|
|
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << ip << QString("->") << hostname; |
|
|
|
|
if (isUsefulHostName(hostname, ip)) { |
|
|
|
|
m_cache.insert(ip, new QString(hostname)); |
|
|
|
|
if (isUsefulHostName(hostname, ip)) |
|
|
|
|
emit ipResolved(ip, hostname); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
m_cache.insert(ip, new QString()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|