summaryrefslogtreecommitdiffstats
path: root/src/network/kernel/qdnslookup_p.h
blob: da4721411bdc265920f1332ee95ee40baa626eb4 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
// Copyright (C) 2023 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QDNSLOOKUP_P_H
#define QDNSLOOKUP_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of the QDnsLookup class.  This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "QtCore/qmutex.h"
#include "QtCore/qrunnable.h"
#if QT_CONFIG(thread)
#include "QtCore/qthreadpool.h"
#endif
#include "QtNetwork/qdnslookup.h"
#include "QtNetwork/qhostaddress.h"
#include "private/qobject_p.h"
#include "private/qurl_p.h"

QT_REQUIRE_CONFIG(dnslookup);

QT_BEGIN_NAMESPACE

//#define QDNSLOOKUP_DEBUG

constexpr qsizetype MaxDomainNameLength = 255;
constexpr quint16 DnsPort = 53;

class QDnsLookupRunnable;
QDebug operator<<(QDebug &, QDnsLookupRunnable *);

class QDnsLookupReply
{
public:
    QDnsLookup::Error error = QDnsLookup::NoError;
    QString errorString;

    QList<QDnsDomainNameRecord> canonicalNameRecords;
    QList<QDnsHostAddressRecord> hostAddressRecords;
    QList<QDnsMailExchangeRecord> mailExchangeRecords;
    QList<QDnsDomainNameRecord> nameServerRecords;
    QList<QDnsDomainNameRecord> pointerRecords;
    QList<QDnsServiceRecord> serviceRecords;
    QList<QDnsTextRecord> textRecords;

    // helper methods
    void setError(QDnsLookup::Error err, QString &&msg)
    {
        error = err;
        errorString = std::move(msg);
    }

    void makeResolverSystemError(int code = -1)
    {
        Q_ASSERT(allAreEmpty());
        setError(QDnsLookup::ResolverError, qt_error_string(code));
    }

    void makeTimeoutError()
    {
        Q_ASSERT(allAreEmpty());
        setError(QDnsLookup::TimeoutError, QDnsLookup::tr("Request timed out"));
    }

    void makeDnsRcodeError(quint8 rcode)
    {
        Q_ASSERT(allAreEmpty());
        switch (rcode) {
        case 1:     // FORMERR
            error = QDnsLookup::InvalidRequestError;
            errorString = QDnsLookup::tr("Server could not process query");
            return;
        case 2:     // SERVFAIL
        case 4:     // NOTIMP
            error = QDnsLookup::ServerFailureError;
            errorString = QDnsLookup::tr("Server failure");
            return;
        case 3:     // NXDOMAIN
            error = QDnsLookup::NotFoundError;
            errorString = QDnsLookup::tr("Non existent domain");
            return;
        case 5:     // REFUSED
            error = QDnsLookup::ServerRefusedError;
            errorString = QDnsLookup::tr("Server refused to answer");
            return;
        default:
            error = QDnsLookup::InvalidReplyError;
            errorString = QDnsLookup::tr("Invalid reply received (rcode %1)")
                    .arg(rcode);
            return;
        }
    }

    void makeInvalidReplyError(QString &&msg = QString())
    {
        if (msg.isEmpty())
            msg = QDnsLookup::tr("Invalid reply received");
        else
            msg = QDnsLookup::tr("Invalid reply received (%1)").arg(std::move(msg));
        *this = QDnsLookupReply();  // empty our lists
        setError(QDnsLookup::InvalidReplyError, std::move(msg));
    }

private:
    bool allAreEmpty() const
    {
        return canonicalNameRecords.isEmpty()
                && hostAddressRecords.isEmpty()
                && mailExchangeRecords.isEmpty()
                && nameServerRecords.isEmpty()
                && pointerRecords.isEmpty()
                && serviceRecords.isEmpty()
                && textRecords.isEmpty();
    }
};

class QDnsLookupPrivate : public QObjectPrivate
{
public:
    QDnsLookupPrivate()
        : type(QDnsLookup::A)
        , port(DnsPort)
    { }

    void nameChanged()
    {
        emit q_func()->nameChanged(name);
    }
    Q_OBJECT_BINDABLE_PROPERTY(QDnsLookupPrivate, QString, name,
                               &QDnsLookupPrivate::nameChanged);

    void nameserverChanged()
    {
        emit q_func()->nameserverChanged(nameserver);
    }
    Q_OBJECT_BINDABLE_PROPERTY(QDnsLookupPrivate, QHostAddress, nameserver,
                               &QDnsLookupPrivate::nameserverChanged);

    void typeChanged()
    {
        emit q_func()->typeChanged(type);
    }

    Q_OBJECT_BINDABLE_PROPERTY(QDnsLookupPrivate, QDnsLookup::Type,
                               type, &QDnsLookupPrivate::typeChanged);

    void nameserverPortChanged()
    {
        emit q_func()->nameserverPortChanged(port);
    }

    Q_OBJECT_BINDABLE_PROPERTY(QDnsLookupPrivate, quint16,
                               port, &QDnsLookupPrivate::nameserverPortChanged);


    QDnsLookupReply reply;
    QDnsLookupRunnable *runnable = nullptr;
    bool isFinished = false;

    Q_DECLARE_PUBLIC(QDnsLookup)
};

class QDnsLookupRunnable : public QObject, public QRunnable
{
    Q_OBJECT

public:
#ifdef Q_OS_WIN
    using EncodedLabel = QString;
#else
    using EncodedLabel = QByteArray;
#endif

    QDnsLookupRunnable(const QDnsLookupPrivate *d);
    void run() override;

signals:
    void finished(const QDnsLookupReply &reply);

private:
    template <typename T> static QString decodeLabel(T encodedLabel)
    {
        return qt_ACE_do(encodedLabel.toString(), NormalizeAce, ForbidLeadingDot);
    }
    void query(QDnsLookupReply *reply);

    EncodedLabel requestName;
    QHostAddress nameserver;
    QDnsLookup::Type requestType;
    quint16 port;
    friend QDebug operator<<(QDebug &, QDnsLookupRunnable *);
};

class QDnsRecordPrivate : public QSharedData
{
public:
    QDnsRecordPrivate()
        : timeToLive(0)
    { }

    QString name;
    quint32 timeToLive;
};

class QDnsDomainNameRecordPrivate : public QDnsRecordPrivate
{
public:
    QDnsDomainNameRecordPrivate()
    { }

    QString value;
};

class QDnsHostAddressRecordPrivate : public QDnsRecordPrivate
{
public:
    QDnsHostAddressRecordPrivate()
    { }

    QHostAddress value;
};

class QDnsMailExchangeRecordPrivate : public QDnsRecordPrivate
{
public:
    QDnsMailExchangeRecordPrivate()
        : preference(0)
    { }

    QString exchange;
    quint16 preference;
};

class QDnsServiceRecordPrivate : public QDnsRecordPrivate
{
public:
    QDnsServiceRecordPrivate()
        : port(0),
          priority(0),
          weight(0)
    { }

    QString target;
    quint16 port;
    quint16 priority;
    quint16 weight;
};

class QDnsTextRecordPrivate : public QDnsRecordPrivate
{
public:
    QDnsTextRecordPrivate()
    { }

    QList<QByteArray> values;
};

QT_END_NAMESPACE

#endif // QDNSLOOKUP_P_H