Coverage Report

Created: 2026-10-28 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/network/kernel/qdnslookup.h
Line
Count
Source
1
// Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#ifndef QDNSLOOKUP_H
6
#define QDNSLOOKUP_H
7
8
#include <QtNetwork/qtnetworkglobal.h>
9
#include <QtCore/qlist.h>
10
#include <QtCore/qobject.h>
11
#include <QtCore/qshareddata.h>
12
#include <QtCore/qstring.h>
13
#include <QtCore/qproperty.h>
14
15
QT_REQUIRE_CONFIG(dnslookup);
16
17
QT_BEGIN_NAMESPACE
18
19
class QHostAddress;
20
class QDnsLookupPrivate;
21
class QDnsDomainNameRecordPrivate;
22
class QDnsHostAddressRecordPrivate;
23
class QDnsMailExchangeRecordPrivate;
24
class QDnsServiceRecordPrivate;
25
class QDnsTextRecordPrivate;
26
class QDnsTlsAssociationRecordPrivate;
27
class QSslConfiguration;
28
29
QT_DECLARE_QESDP_SPECIALIZATION_DTOR(QDnsTlsAssociationRecordPrivate)
30
31
class Q_NETWORK_EXPORT QDnsDomainNameRecord
32
{
33
public:
34
    QDnsDomainNameRecord();
35
    QDnsDomainNameRecord(const QDnsDomainNameRecord &other);
36
0
    QDnsDomainNameRecord &operator=(QDnsDomainNameRecord &&other) noexcept { swap(other); return *this; }
37
    QDnsDomainNameRecord &operator=(const QDnsDomainNameRecord &other);
38
    ~QDnsDomainNameRecord();
39
40
0
    void swap(QDnsDomainNameRecord &other) noexcept { d.swap(other.d); }
41
42
    QString name() const;
43
    quint32 timeToLive() const;
44
    QString value() const;
45
46
private:
47
    QSharedDataPointer<QDnsDomainNameRecordPrivate> d;
48
    friend class QDnsLookupRunnable;
49
};
50
51
Q_DECLARE_SHARED(QDnsDomainNameRecord)
52
53
class Q_NETWORK_EXPORT QDnsHostAddressRecord
54
{
55
public:
56
    QDnsHostAddressRecord();
57
    QDnsHostAddressRecord(const QDnsHostAddressRecord &other);
58
0
    QDnsHostAddressRecord &operator=(QDnsHostAddressRecord &&other) noexcept { swap(other); return *this; }
59
    QDnsHostAddressRecord &operator=(const QDnsHostAddressRecord &other);
60
    ~QDnsHostAddressRecord();
61
62
0
    void swap(QDnsHostAddressRecord &other) noexcept { d.swap(other.d); }
63
64
    QString name() const;
65
    quint32 timeToLive() const;
66
    QHostAddress value() const;
67
68
private:
69
    QSharedDataPointer<QDnsHostAddressRecordPrivate> d;
70
    friend class QDnsLookupRunnable;
71
};
72
73
Q_DECLARE_SHARED(QDnsHostAddressRecord)
74
75
class Q_NETWORK_EXPORT QDnsMailExchangeRecord
76
{
77
public:
78
    QDnsMailExchangeRecord();
79
    QDnsMailExchangeRecord(const QDnsMailExchangeRecord &other);
80
0
    QDnsMailExchangeRecord &operator=(QDnsMailExchangeRecord &&other) noexcept { swap(other); return *this; }
81
    QDnsMailExchangeRecord &operator=(const QDnsMailExchangeRecord &other);
82
    ~QDnsMailExchangeRecord();
83
84
0
    void swap(QDnsMailExchangeRecord &other) noexcept { d.swap(other.d); }
85
86
    QString exchange() const;
87
    QString name() const;
88
    quint16 preference() const;
89
    quint32 timeToLive() const;
90
91
private:
92
    QSharedDataPointer<QDnsMailExchangeRecordPrivate> d;
93
    friend class QDnsLookupRunnable;
94
};
95
96
Q_DECLARE_SHARED(QDnsMailExchangeRecord)
97
98
class Q_NETWORK_EXPORT QDnsServiceRecord
99
{
100
public:
101
    QDnsServiceRecord();
102
    QDnsServiceRecord(const QDnsServiceRecord &other);
103
0
    QDnsServiceRecord &operator=(QDnsServiceRecord &&other) noexcept { swap(other); return *this; }
104
    QDnsServiceRecord &operator=(const QDnsServiceRecord &other);
105
    ~QDnsServiceRecord();
106
107
0
    void swap(QDnsServiceRecord &other) noexcept { d.swap(other.d); }
108
109
    QString name() const;
110
    quint16 port() const;
111
    quint16 priority() const;
112
    QString target() const;
113
    quint32 timeToLive() const;
114
    quint16 weight() const;
115
116
private:
117
    QSharedDataPointer<QDnsServiceRecordPrivate> d;
118
    friend class QDnsLookupRunnable;
119
};
120
121
Q_DECLARE_SHARED(QDnsServiceRecord)
122
123
class Q_NETWORK_EXPORT QDnsTextRecord
124
{
125
public:
126
    QDnsTextRecord();
127
    QDnsTextRecord(const QDnsTextRecord &other);
128
0
    QDnsTextRecord &operator=(QDnsTextRecord &&other) noexcept { swap(other); return *this; }
129
    QDnsTextRecord &operator=(const QDnsTextRecord &other);
130
    ~QDnsTextRecord();
131
132
0
    void swap(QDnsTextRecord &other) noexcept { d.swap(other.d); }
133
134
    QString name() const;
135
    quint32 timeToLive() const;
136
    QList<QByteArray> values() const;
137
138
private:
139
    QSharedDataPointer<QDnsTextRecordPrivate> d;
140
    friend class QDnsLookupRunnable;
141
};
142
143
Q_DECLARE_SHARED(QDnsTextRecord)
144
145
class QDnsTlsAssociationRecord
146
{
147
    Q_GADGET_EXPORT(Q_NETWORK_EXPORT)
148
    Q_CLASSINFO("RegisterEnumClassesUnscoped", "false")
149
public:
150
    enum class CertificateUsage : quint8 {
151
        // https://www.iana.org/assignments/dane-parameters/dane-parameters.xhtml#certificate-usages
152
        // RFC 6698
153
        CertificateAuthorityConstrait = 0,
154
        ServiceCertificateConstraint = 1,
155
        TrustAnchorAssertion = 2,
156
        DomainIssuedCertificate = 3,
157
        PrivateUse = 255,
158
159
        // Aliases by RFC 7218
160
        PKIX_TA = 0,
161
        PKIX_EE = 1,
162
        DANE_TA = 2,
163
        DANE_EE = 3,
164
        PrivCert = 255,
165
    };
166
    Q_ENUM(CertificateUsage)
167
168
    enum class Selector : quint8 {
169
        // https://www.iana.org/assignments/dane-parameters/dane-parameters.xhtml#selectors
170
        // RFC 6698
171
        FullCertificate = 0,
172
        SubjectPublicKeyInfo = 1,
173
        PrivateUse = 255,
174
175
        // Aliases by RFC 7218
176
        Cert = FullCertificate,
177
        SPKI = SubjectPublicKeyInfo,
178
        PrivSel = PrivateUse,
179
    };
180
    Q_ENUM(Selector)
181
182
    enum class MatchingType : quint8 {
183
        // https://www.iana.org/assignments/dane-parameters/dane-parameters.xhtml#matching-types
184
        // RFC 6698
185
        Exact = 0,
186
        Sha256 = 1,
187
        Sha512 = 2,
188
        PrivateUse = 255,
189
        PrivMatch = PrivateUse,
190
    };
191
    Q_ENUM(MatchingType)
192
193
    Q_NETWORK_EXPORT QDnsTlsAssociationRecord();
194
    Q_NETWORK_EXPORT QDnsTlsAssociationRecord(const QDnsTlsAssociationRecord &other);
195
0
    QDnsTlsAssociationRecord(QDnsTlsAssociationRecord &&other) noexcept = default;
196
    QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDnsTlsAssociationRecord)
197
    Q_NETWORK_EXPORT QDnsTlsAssociationRecord &operator=(const QDnsTlsAssociationRecord &other);
198
    Q_NETWORK_EXPORT ~QDnsTlsAssociationRecord();
199
200
0
    void swap(QDnsTlsAssociationRecord &other) noexcept { d.swap(other.d); }
201
202
    Q_NETWORK_EXPORT QString name() const;
203
    Q_NETWORK_EXPORT quint32 timeToLive() const;
204
    Q_NETWORK_EXPORT CertificateUsage usage() const;
205
    Q_NETWORK_EXPORT Selector selector() const;
206
    Q_NETWORK_EXPORT MatchingType matchType() const;
207
    Q_NETWORK_EXPORT QByteArray value() const;
208
209
private:
210
    QExplicitlySharedDataPointer<QDnsTlsAssociationRecordPrivate> d;
211
    friend class QDnsLookupRunnable;
212
};
213
214
Q_DECLARE_SHARED(QDnsTlsAssociationRecord)
215
216
class Q_NETWORK_EXPORT QDnsLookup : public QObject
217
{
218
    Q_OBJECT
219
    Q_PROPERTY(Error error READ error NOTIFY finished)
220
    Q_PROPERTY(bool authenticData READ isAuthenticData NOTIFY finished)
221
    Q_PROPERTY(QString errorString READ errorString NOTIFY finished)
222
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged BINDABLE bindableName)
223
    Q_PROPERTY(Type type READ type WRITE setType NOTIFY typeChanged BINDABLE bindableType)
224
    Q_PROPERTY(QHostAddress nameserver READ nameserver WRITE setNameserver NOTIFY nameserverChanged
225
               BINDABLE bindableNameserver)
226
    Q_PROPERTY(quint16 nameserverPort READ nameserverPort WRITE setNameserverPort
227
               NOTIFY nameserverPortChanged BINDABLE bindableNameserverPort)
228
    Q_PROPERTY(Protocol nameserverProtocol READ nameserverProtocol WRITE setNameserverProtocol
229
               NOTIFY nameserverProtocolChanged BINDABLE bindableNameserverProtocol)
230
231
public:
232
    enum Error
233
    {
234
        NoError = 0,
235
        ResolverError,
236
        OperationCancelledError,
237
        InvalidRequestError,
238
        InvalidReplyError,
239
        ServerFailureError,
240
        ServerRefusedError,
241
        NotFoundError,
242
        TimeoutError,
243
    };
244
    Q_ENUM(Error)
245
246
    enum Type
247
    {
248
        A = 1,
249
        AAAA = 28,
250
        ANY = 255,
251
        CNAME = 5,
252
        MX = 15,
253
        NS = 2,
254
        PTR = 12,
255
        SRV = 33,
256
        TLSA = 52,
257
        TXT = 16
258
    };
259
    Q_ENUM(Type)
260
261
    enum Protocol : quint8 {
262
        Standard = 0,
263
        DnsOverTls,
264
    };
265
    Q_ENUM(Protocol)
266
267
    explicit QDnsLookup(QObject *parent = nullptr);
268
    QDnsLookup(Type type, const QString &name, QObject *parent = nullptr);
269
    QDnsLookup(Type type, const QString &name, const QHostAddress &nameserver, QObject *parent = nullptr);
270
    QDnsLookup(Type type, const QString &name, const QHostAddress &nameserver, quint16 port,
271
               QObject *parent = nullptr);
272
    QDnsLookup(Type type, const QString &name, Protocol protocol, const QHostAddress &nameserver,
273
               quint16 port = 0, QObject *parent = nullptr);
274
    ~QDnsLookup();
275
276
    bool isAuthenticData() const;
277
    Error error() const;
278
    QString errorString() const;
279
    bool isFinished() const;
280
281
    QString name() const;
282
    void setName(const QString &name);
283
    QBindable<QString> bindableName();
284
285
    Type type() const;
286
    void setType(QDnsLookup::Type);
287
    QBindable<Type> bindableType();
288
289
    QHostAddress nameserver() const;
290
    void setNameserver(const QHostAddress &nameserver);
291
    QBindable<QHostAddress> bindableNameserver();
292
    quint16 nameserverPort() const;
293
    void setNameserverPort(quint16 port);
294
    QBindable<quint16> bindableNameserverPort();
295
    Protocol nameserverProtocol() const;
296
    void setNameserverProtocol(Protocol protocol);
297
    QBindable<Protocol> bindableNameserverProtocol();
298
    void setNameserver(Protocol protocol, const QHostAddress &nameserver, quint16 port = 0);
299
    QT_NETWORK_INLINE_SINCE(6, 8)
300
    void setNameserver(const QHostAddress &nameserver, quint16 port);
301
302
    QList<QDnsDomainNameRecord> canonicalNameRecords() const;
303
    QList<QDnsHostAddressRecord> hostAddressRecords() const;
304
    QList<QDnsMailExchangeRecord> mailExchangeRecords() const;
305
    QList<QDnsDomainNameRecord> nameServerRecords() const;
306
    QList<QDnsDomainNameRecord> pointerRecords() const;
307
    QList<QDnsServiceRecord> serviceRecords() const;
308
    QList<QDnsTextRecord> textRecords() const;
309
    QList<QDnsTlsAssociationRecord> tlsAssociationRecords() const;
310
311
#if QT_CONFIG(ssl)
312
    void setSslConfiguration(const QSslConfiguration &sslConfiguration);
313
    QSslConfiguration sslConfiguration() const;
314
#endif
315
316
    static bool isProtocolSupported(Protocol protocol);
317
    Q_DECL_CONST_FUNCTION static quint16 defaultPortForProtocol(Protocol protocol) noexcept;
318
319
public Q_SLOTS:
320
    void abort();
321
    void lookup();
322
323
Q_SIGNALS:
324
    void finished();
325
    void nameChanged(const QString &name);
326
    void typeChanged(QDnsLookup::Type type);
327
    void nameserverChanged(const QHostAddress &nameserver);
328
    void nameserverPortChanged(quint16 port);
329
    void nameserverProtocolChanged(QDnsLookup::Protocol protocol);
330
331
private:
332
    Q_DECLARE_PRIVATE(QDnsLookup)
333
};
334
335
#if QT_NETWORK_INLINE_IMPL_SINCE(6, 8)
336
void QDnsLookup::setNameserver(const QHostAddress &nameserver, quint16 port)
337
0
{
338
0
    setNameserver(Standard, nameserver, port);
339
0
}
340
#endif
341
342
QT_END_NAMESPACE
343
344
#endif // QDNSLOOKUP_H