Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Commit

Permalink
Fix DNS lookup problems #161
Browse files Browse the repository at this point in the history
  • Loading branch information
librehat committed Feb 3, 2018
1 parent fd724ad commit 49af99a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
49 changes: 33 additions & 16 deletions lib/types/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,28 @@
#include "address.h"
#include "util/common.h"

#include <QDnsLookup>
#include <memory>

namespace QSS {

void DnsLookup::lookup(const QString& hostname)
{
QHostInfo::lookupHost(hostname, this, SLOT(lookedUp(QHostInfo)));
}

const QList<QHostAddress> DnsLookup::iplist() const
{
return m_ips;
}

void DnsLookup::lookedUp(const QHostInfo &info)
{
if (info.error() != QHostInfo::NoError) {
qWarning("DNS lookup failed: %s", info.errorString().toStdString().data());
} else {
m_ips = info.addresses();
}
emit finished();
}

Address::Address(const std::string &a, uint16_t p)
{
data.second = p;
Expand Down Expand Up @@ -75,19 +92,19 @@ void Address::lookUp(Address::LookUpCallback cb)
if (isIPValid()) {
return cb(true);
}
std::shared_ptr<QDnsLookup> dns(new QDnsLookup(QDnsLookup::Type::ANY, QString::fromStdString(data.first)));
QObject::connect(dns.get(), &QDnsLookup::finished,
[cb, dns, this]() {
if (dns->error() != QDnsLookup::NoError) {
qDebug("Failed to look up host address: %s", dns->errorString().toStdString().data());
cb(false);
} else {
ipAddrList.clear();
for (const QDnsHostAddressRecord& record : dns->hostAddressRecords()) {
ipAddrList.push_back(record.value());
}
cb(true);
}});

if (dns) {
// DNS lookup is in-progress
return;
}

dns = std::make_shared<DnsLookup>();
QObject::connect(dns.get(), &DnsLookup::finished, [cb, this]() {
ipAddrList = dns->iplist().toVector().toStdVector();
cb(!ipAddrList.empty());
dns.reset();
});
dns->lookup(QString::fromStdString(data.first));
}

bool Address::blockingLookUp()
Expand Down
23 changes: 22 additions & 1 deletion lib/types/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,32 @@
#include <QString>
#include <QHostAddress>
#include <QHostInfo>
#include <vector>

#include <functional>
#include <memory>
#include <vector>
#include "util/export.h"

namespace QSS {

class QSS_EXPORT DnsLookup : public QObject
{
// A simple wrapper class to provide asynchronous DNS lookup
Q_OBJECT
public:
void lookup(const QString& hostname);
const QList<QHostAddress> iplist() const;

signals:
void finished();

private slots:
void lookedUp(const QHostInfo& info);

private:
QList<QHostAddress> m_ips;
};

class QSS_EXPORT Address
{
public:
Expand Down Expand Up @@ -118,6 +138,7 @@ class QSS_EXPORT Address
private:
std::pair<std::string, uint16_t> data;//first: address string; second: port
std::vector<QHostAddress> ipAddrList;
std::shared_ptr<DnsLookup> dns;
};

}
Expand Down
13 changes: 13 additions & 0 deletions test/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private Q_SLOTS:
void testSetAddress();
void testSetIPAddress();
void testSetPort();
void testLookup();
};

void Address::testConstructor1()
Expand Down Expand Up @@ -80,5 +81,17 @@ void Address::testSetPort()
QCOMPARE(a.getPort(), port);
}

void Address::testLookup()
{
QSS::Address a("www.google.com", 443);
a.lookUp([&a](bool success) {
if (success) {
QVERIFY(a.isIPValid());
} else {
QVERIFY(!a.isIPValid());
}
});
}

QTEST_MAIN(Address)
#include "address.moc"

0 comments on commit 49af99a

Please sign in to comment.