-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnswire.cc
80 lines (67 loc) · 2.17 KB
/
dnswire.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
#include "dnswire.h"
#include "dns-storage.hh"
#include "dnsmessages.hh"
#include <sstream>
// We use tdns from https://github.com/ahupowerdns/hello-dns
// for serialization of DNS queries and replies, but this
// file is the only place it appears.
QByteArray WireRequest(ushort type, const QString &name) {
// TODO(steve): Handle IDN
DNSName dn = makeDNSName(name.toStdString());
DNSType dt = static_cast<DNSType>(type);
DNSMessageWriter dmw(dn, dt);
dmw.dh.rd = true;
dmw.randomizeID();
dmw.setEDNS(4000, false);
return QByteArray::fromStdString(dmw.serialize());
}
QString WireToPretty(const QByteArray &wire) {
if (wire.size() < 12) {
qWarning() << "Response too short: " << wire.size();
return QString("Response too short: %1").arg(wire.size());
}
DNSMessageReader dmr(wire.toStdString());
DNSSection rrsection, lastsection = DNSSection::Question;
uint32_t ttl;
DNSName dn;
DNSType dt;
dmr.getQuestion(dn, dt);
std::ostringstream str;
str << "; Question section\n" << dn << "\t" << dt << "\n";
std::unique_ptr<RRGen> rr;
while(dmr.getRR(rrsection, dn, dt, ttl, rr)) {
if (rrsection != lastsection) {
str << "; " << rrsection << " section\n";
lastsection = rrsection;
}
str << dn << "\t" << dt << "\t" << ttl << "\t" << rr->toString() << "\n";
}
return QString::fromStdString(str.str());
}
QList<QHostAddress> WireToAddresses(const QByteArray &wire) {
if (wire.size() < 12) {
qWarning() << "Response too short: " << wire.size();
return QList<QHostAddress>();
}
QList<QHostAddress> ret;
DNSMessageReader dmr(wire.toStdString());
DNSSection rrsection;
uint32_t ttl;
DNSName dn;
DNSType dt;
std::unique_ptr<RRGen> rr;
while (dmr.getRR(rrsection, dn, dt, ttl, rr)) {
if (rrsection != DNSSection::Answer) {
continue;
}
switch (rr->getType()) {
case DNSType::A:
case DNSType::AAAA:
ret.append(QHostAddress(QString::fromStdString(rr->toString())));
break;
default:
break;
}
}
return ret;
}