-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_system.cc
102 lines (93 loc) · 3.33 KB
/
dns_system.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "dns_system.h"
#include <QDnsLookup>
SystemImplementation::SystemImplementation(ushort type, const QString &name, QObject *parent) : DnsImplementation(parent) {
switch (type) {
case QDnsLookup::A:
case QDnsLookup::AAAA:
case QDnsLookup::CNAME:
case QDnsLookup::MX:
case QDnsLookup::NS:
case QDnsLookup::PTR:
case QDnsLookup::SRV:
case QDnsLookup::TXT:
dl = new QDnsLookup(static_cast<QDnsLookup::Type>(type), name, this);
connect(dl, &QDnsLookup::finished, this, [=](){
responseTime = timer.nsecsElapsed();
qDebug() << "QDnsLookup finished";
emit finished();
});
break;
default:
dl = nullptr;
errorString = tr("Qt resolver doesn't handle queries of type %1").arg(type);
}
}
QString SystemImplementation::ErrorString() const {
if (!errorString.isEmpty()) {
return errorString;
}
Q_ASSERT(dl != nullptr);
return dl->errorString();
}
QList<QHostAddress> SystemImplementation::Addresses() const {
QList<QHostAddress> ret;
if (dl == nullptr) {
return ret;
}
for (auto &i : dl->hostAddressRecords()) {
ret.append(i.value());
}
return ret;
}
void SystemImplementation::lookup() {
if (dl != nullptr) {
timer.start();
dl->lookup();
return;
}
emit finished();
}
QString SystemImplementation::PrettyResults() const {
QString error = ErrorString();
if (!error.isEmpty()) {
return error;
}
QStringList ret;
for (auto &rr : dl->canonicalNameRecords()) {
ret.append(QString("%1 %2 CNAME %3").arg(rr.name()).arg(rr.timeToLive()).arg(rr.value()));
}
for (auto &rr : dl->hostAddressRecords()) {
switch (rr.value().protocol()) {
case QAbstractSocket::IPv4Protocol:
ret.append(QString("%1 %2 A %3").arg(rr.name()).arg(rr.timeToLive()).arg(rr.value().toString()));
break;
case QAbstractSocket::IPv6Protocol:
ret.append(QString("%1 %2 AAAA %3").arg(rr.name()).arg(rr.timeToLive()).arg(rr.value().toString()));
break;
default:
ret.append(tr("Impossible protocol for %1: %2").arg(rr.name()).arg(rr.value().protocol()));
break;
}
}
for (auto &rr : dl->mailExchangeRecords()) {
ret.append(QString("%1 %2 MX %3 %4").arg(rr.name()).arg(rr.timeToLive()).arg(rr.preference()).arg(rr.exchange()));
}
for (auto &rr : dl->nameServerRecords()) {
ret.append(QString("%1 %2 NS %3").arg(rr.name()).arg(rr.timeToLive()).arg(rr.value()));
}
for (auto &rr : dl->pointerRecords()) {
ret.append(QString("%1 %2 PTR %3").arg(rr.name()).arg(rr.timeToLive()).arg(rr.value()));
}
for (auto &rr : dl->serviceRecords()) {
ret.append(QString("%1 %2 SRV %3 %4 %5 %6").arg(rr.name()).arg(rr.timeToLive()).arg(rr.priority()).arg(rr.weight()).arg(rr.port()).arg(rr.target()));
}
for (auto &rr : dl->textRecords()) {
QStringList vals;
for (auto &piece : rr.values()) {
// TODO(steve): fix escaping
vals.append(QString("\"%1\"").arg(QString::fromLatin1(piece).replace("\\", "\\\\").replace("\"", "\\\"")));
}
ret.append(QString("%1 %2 TXT %3").arg(rr.name()).arg(rr.timeToLive()).arg(vals.join(" ")));
}
return ret.join("\n");
}