|
@@ -0,0 +1,114 @@
|
|
1
|
+//
|
|
2
|
+// Created by robin on 2/12/18.
|
|
3
|
+//
|
|
4
|
+
|
|
5
|
+#include "dns-client/QDnsAxfrClient.h"
|
|
6
|
+
|
|
7
|
+namespace QDns
|
|
8
|
+{
|
|
9
|
+ namespace Client
|
|
10
|
+ {
|
|
11
|
+ QDnsAxfrClient::QDnsAxfrClient(const QString& zone, const QHostAddress& host, quint16 port, QObject *parent)
|
|
12
|
+ : QObject(parent)
|
|
13
|
+ , m_zone(zone)
|
|
14
|
+ , m_status(AxfrStatus::FirstSOA)
|
|
15
|
+ , m_udpClient(new QDns::Client::QDnsUdpClient(host, port, this))
|
|
16
|
+ , m_tcpClient(new QDns::Client::QDnsTcpClient(host, port, this))
|
|
17
|
+ {
|
|
18
|
+ connect(m_udpClient, &QDnsClient::onStateChanged, this, &QDnsAxfrClient::onClientStateChanged);
|
|
19
|
+ connect(m_tcpClient, &QDnsClient::onStateChanged, this, &QDnsAxfrClient::onClientStateChanged);
|
|
20
|
+ connect(m_udpClient, &QDnsClient::onAnswer, this, &QDnsAxfrClient::onClientAnswer);
|
|
21
|
+ connect(m_tcpClient, &QDnsClient::onAnswer, this, &QDnsAxfrClient::onClientAnswer);
|
|
22
|
+ connect(m_udpClient, &QDnsClient::onError, this, &QDnsAxfrClient::onClientError);
|
|
23
|
+ connect(m_tcpClient, &QDnsClient::onError, this, &QDnsAxfrClient::onClientError);
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ void QDnsAxfrClient::transfer()
|
|
27
|
+ {
|
|
28
|
+ m_status = AxfrStatus::FirstSOA;
|
|
29
|
+ m_records.clear();
|
|
30
|
+ m_udpClient->connectToHost();
|
|
31
|
+ m_tcpClient->connectToHost();
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ void QDnsAxfrClient::onClientStateChanged(const QDnsClient::OnStateChangedParams& params)
|
|
35
|
+ {
|
|
36
|
+ if (m_udpClient->getState() == QDnsClient::ClientState::Connected
|
|
37
|
+ && m_tcpClient->getState() == QDnsClient::ClientState::Connected)
|
|
38
|
+ {
|
|
39
|
+ auto axfrQuery = QDns::Base::QDnsPacket::makeSimpleQuery(m_zone, QDns::Base::QDnsBase::RecordType::AXFR);
|
|
40
|
+ m_tcpClient->query(axfrQuery);
|
|
41
|
+ }
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ void QDnsAxfrClient::onClientAnswer(const QDnsClient::OnAnswerParams& params)
|
|
45
|
+ {
|
|
46
|
+ processPacket(params.answer);
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ void QDnsAxfrClient::onClientError(const QDnsClient::OnErrorParams ¶ms)
|
|
50
|
+ {
|
|
51
|
+ if (params.error == QDns::Client::QDnsClient::ErrorCode::UnknownId)
|
|
52
|
+ {
|
|
53
|
+ processPacket(params.answer);
|
|
54
|
+ }
|
|
55
|
+ else
|
|
56
|
+ {
|
|
57
|
+ emit onError(OnErrorParams
|
|
58
|
+ {
|
|
59
|
+ 0,
|
|
60
|
+ ErrorCode::ClientError,
|
|
61
|
+ params
|
|
62
|
+ });
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ void QDnsAxfrClient::processPacket(const QDns::Base::QDnsPacket& packet)
|
|
67
|
+ {
|
|
68
|
+ if (m_status == AxfrStatus::FirstSOA)
|
|
69
|
+ {
|
|
70
|
+ if (packet.getHeader().getAnswerCount() == 1 &&
|
|
71
|
+ packet.getAnswerRecords().first().getType() == QDns::Base::QDnsBase::RecordType::SOA)
|
|
72
|
+ {
|
|
73
|
+ m_status = AxfrStatus::Records;
|
|
74
|
+ m_records.append(packet.getAnswerRecords());
|
|
75
|
+ }
|
|
76
|
+ else
|
|
77
|
+ {
|
|
78
|
+ emit onError(OnErrorParams
|
|
79
|
+ {
|
|
80
|
+ 0,
|
|
81
|
+ ErrorCode::NoFirstSOA
|
|
82
|
+ });
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+ else if (m_status == AxfrStatus::Records)
|
|
86
|
+ {
|
|
87
|
+ if (packet.getHeader().getAnswerCount() == 1 &&
|
|
88
|
+ packet.getAnswerRecords().first().getType() == QDns::Base::QDnsBase::RecordType::SOA)
|
|
89
|
+ {
|
|
90
|
+ m_status = AxfrStatus::LastSOA;
|
|
91
|
+ m_udpClient->disconnectFromHost();
|
|
92
|
+ m_tcpClient->disconnectFromHost();
|
|
93
|
+ emit onAnswer(OnAnswerParams
|
|
94
|
+ {
|
|
95
|
+ 0,
|
|
96
|
+ m_records
|
|
97
|
+ });
|
|
98
|
+ }
|
|
99
|
+ else
|
|
100
|
+ {
|
|
101
|
+ m_records.append(packet.getAnswerRecords());
|
|
102
|
+ }
|
|
103
|
+ }
|
|
104
|
+ else
|
|
105
|
+ {
|
|
106
|
+ emit onError(OnErrorParams
|
|
107
|
+ {
|
|
108
|
+ 0,
|
|
109
|
+ ErrorCode::ExtraRecords
|
|
110
|
+ });
|
|
111
|
+ }
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+}
|