Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdnslookup_p.h
Go to the documentation of this file.
1// Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
2// Copyright (C) 2023 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QDNSLOOKUP_P_H
7#define QDNSLOOKUP_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists for the convenience
14// of the QDnsLookup class. This header file may change from
15// version to version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtNetwork/private/qtnetworkglobal_p.h>
21#include "QtCore/qmutex.h"
22#include "QtCore/qrunnable.h"
23#if QT_CONFIG(thread)
24#include "QtCore/qthreadpool.h"
25#endif
26#include "QtNetwork/qdnslookup.h"
27#include "QtNetwork/qhostaddress.h"
28#include "private/qobject_p.h"
29#include "private/qurl_p.h"
30
31#if QT_CONFIG(ssl)
32# include "qsslconfiguration.h"
33#endif
34
36
37QT_BEGIN_NAMESPACE
38
39//#define QDNSLOOKUP_DEBUG
40
41constexpr qsizetype MaxDomainNameLength = 255;
42constexpr quint16 DnsPort = 53;
43constexpr quint16 DnsOverTlsPort = 853;
44
45#if QT_CONFIG(android_dnsresolver)
46// whether android_res_nsend() is present, that is, we're on Android 10 or later
47bool qt_androidDnsResolverAvailable();
48#endif
49
52
54{
55public:
57 bool authenticData = false;
59
68
69#if QT_CONFIG(ssl)
71#endif
72
73 // helper methods
74 void setError(QDnsLookup::Error err, QString &&msg)
75 {
76 error = err;
77 errorString = std::move(msg);
78 }
79
80 void makeResolverSystemError(int code = -1)
81 {
82 Q_ASSERT(allAreEmpty());
83 setError(QDnsLookup::ResolverError, qt_error_string(code));
84 }
85
87 {
88 Q_ASSERT(allAreEmpty());
89 setError(QDnsLookup::TimeoutError, QDnsLookup::tr("Request timed out"));
90 }
91
92 void makeDnsRcodeError(quint8 rcode)
93 {
94 Q_ASSERT(allAreEmpty());
95 switch (rcode) {
96 case 1: // FORMERR
97 error = QDnsLookup::InvalidRequestError;
98 errorString = QDnsLookup::tr("Server could not process query");
99 return;
100 case 2: // SERVFAIL
101 case 4: // NOTIMP
102 error = QDnsLookup::ServerFailureError;
103 errorString = QDnsLookup::tr("Server failure");
104 return;
105 case 3: // NXDOMAIN
106 error = QDnsLookup::NotFoundError;
107 errorString = QDnsLookup::tr("Non existent domain");
108 return;
109 case 5: // REFUSED
110 error = QDnsLookup::ServerRefusedError;
111 errorString = QDnsLookup::tr("Server refused to answer");
112 return;
113 default:
114 error = QDnsLookup::InvalidReplyError;
115 errorString = QDnsLookup::tr("Invalid reply received (rcode %1)")
116 .arg(rcode);
117 return;
118 }
119 }
120
121 void makeInvalidReplyError(QString &&msg = QString())
122 {
123 if (msg.isEmpty())
124 msg = QDnsLookup::tr("Invalid reply received");
125 else
126 msg = QDnsLookup::tr("Invalid reply received (%1)").arg(std::move(msg));
127 *this = QDnsLookupReply(); // empty our lists
128 setError(QDnsLookup::InvalidReplyError, std::move(msg));
129 }
130
131private:
132 bool allAreEmpty() const
133 {
134 return canonicalNameRecords.isEmpty()
135 && hostAddressRecords.isEmpty()
136 && mailExchangeRecords.isEmpty()
137 && nameServerRecords.isEmpty()
138 && pointerRecords.isEmpty()
139 && serviceRecords.isEmpty()
140 && tlsAssociationRecords.isEmpty()
141 && textRecords.isEmpty();
142 }
143};
144
146{
147public:
149 : type(QDnsLookup::A)
150 , port(0)
152 { }
153
155 {
156 emit q_func()->nameChanged(name);
157 }
159 &QDnsLookupPrivate::nameChanged);
160
162 {
163 emit q_func()->nameserverChanged(nameserver);
164 }
165 Q_OBJECT_BINDABLE_PROPERTY(QDnsLookupPrivate, QHostAddress, nameserver,
166 &QDnsLookupPrivate::nameserverChanged);
167
169 {
170 emit q_func()->typeChanged(type);
171 }
172
174 type, &QDnsLookupPrivate::typeChanged);
175
177 {
178 emit q_func()->nameserverPortChanged(port);
179 }
180
182 port, &QDnsLookupPrivate::nameserverPortChanged);
183
185 {
186 emit q_func()->nameserverProtocolChanged(protocol);
187 }
188
190 protocol, &QDnsLookupPrivate::nameserverProtocolChanged);
191
194 bool isFinished = false;
195
196#if QT_CONFIG(ssl)
198#endif
199
200 Q_DECLARE_PUBLIC(QDnsLookup)
201};
202
204{
206
207public:
208#ifdef Q_OS_WIN
209 using EncodedLabel = QString;
210#else
212#endif
213 // minimum IPv6 MTU (1280) minus the IPv6 (40) and UDP headers (8)
214 static constexpr qsizetype ReplyBufferSize = 1280 - 40 - 8;
216
218 void run() override;
219 bool sendDnsOverTls(QDnsLookupReply *reply, QSpan<unsigned char> query, ReplyBuffer &response);
220
221signals:
223
224private:
225 template <typename T> static QString decodeLabel(T encodedLabel)
226 {
227 return qt_ACE_do(encodedLabel.toString(), NormalizeAce, ForbidLeadingDot);
228 }
229 void query(QDnsLookupReply *reply);
230#if QT_CONFIG(libresolv) || QT_CONFIG(android_dnsresolver)
231 // implemented in qdnslookup_unix.cpp, shared by both backends there
233#endif
234
235 EncodedLabel requestName;
236 QHostAddress nameserver;
238 quint16 port;
240
241#if QT_CONFIG(ssl)
243#endif
244 friend QDebug operator<<(QDebug &, QDnsLookupRunnable *);
245};
246
248{
249public:
251 : timeToLive(0)
252 { }
253
256};
257
266
268{
269public:
272
273 QHostAddress value;
274};
275
286
301
310
319
320QT_END_NAMESPACE
321
322#endif // QDNSLOOKUP_P_H
The QDnsDomainNameRecord class stores information about a domain name record.
Definition qdnslookup.h:32
The QDnsHostAddressRecord class stores information about a host address record.
Definition qdnslookup.h:54
Q_OBJECT_BINDABLE_PROPERTY(QDnsLookupPrivate, QString, name, &QDnsLookupPrivate::nameChanged)
QDnsLookupRunnable * runnable
void nameserverProtocolChanged()
void nameserverPortChanged()
QDnsLookupReply reply
void setError(QDnsLookup::Error err, QString &&msg)
QList< QDnsMailExchangeRecord > mailExchangeRecords
void makeDnsRcodeError(quint8 rcode)
QList< QDnsServiceRecord > serviceRecords
void makeInvalidReplyError(QString &&msg=QString())
QList< QDnsDomainNameRecord > canonicalNameRecords
QList< QDnsDomainNameRecord > nameServerRecords
QList< QDnsDomainNameRecord > pointerRecords
QList< QDnsHostAddressRecord > hostAddressRecords
void makeResolverSystemError(int code=-1)
QList< QDnsTlsAssociationRecord > tlsAssociationRecords
QString errorString
void makeTimeoutError()
QList< QDnsTextRecord > textRecords
QDnsLookupRunnable(const QDnsLookupPrivate *d)
void run() override
Implement this pure virtual function in your subclass.
static constexpr qsizetype ReplyBufferSize
bool sendDnsOverTls(QDnsLookupReply *reply, QSpan< unsigned char > query, ReplyBuffer &response)
The QDnsLookup class represents a DNS lookup.
Definition qdnslookup.h:217
The QDnsMailExchangeRecord class stores information about a DNS MX record.
Definition qdnslookup.h:76
The QDnsServiceRecord class stores information about a DNS SRV record.
Definition qdnslookup.h:99
QList< QByteArray > values
The QDnsTextRecord class stores information about a DNS TXT record.
Definition qdnslookup.h:124
QDnsTlsAssociationRecord::CertificateUsage usage
QDnsTlsAssociationRecord::MatchingType matchType
QDnsTlsAssociationRecord::Selector selector
The QDnsTlsAssociationRecord class stores information about a DNS TLSA record.
Definition qdnslookup.h:146
void swap(QDnsTlsAssociationRecord &other) noexcept
Definition qdnslookup.h:200
QDnsTlsAssociationRecord(QDnsTlsAssociationRecord &&other) noexcept=default
Q_NETWORK_EXPORT ~QDnsTlsAssociationRecord()
Destroys this TLS Association record object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:177
Combined button and popup list for selecting options.
QT_REQUIRE_CONFIG(animation)
#define Q_APPLICATION_STATIC(TYPE, NAME,...)
static void qt_qdnsmailexchangerecord_sort(QList< QDnsMailExchangeRecord > &records)
static void qt_qdnsservicerecord_sort(QList< QDnsServiceRecord > &records)
static bool qt_qdnsmailexchangerecord_less_than(const QDnsMailExchangeRecord &r1, const QDnsMailExchangeRecord &r2)
static bool qt_qdnsservicerecord_less_than(const QDnsServiceRecord &r1, const QDnsServiceRecord &r2)
static QDnsLookupRunnable::EncodedLabel encodeLabel(const QString &label)
QT_REQUIRE_CONFIG(dnslookup)
constexpr quint16 DnsPort
constexpr quint16 DnsOverTlsPort
QDebug operator<<(QDebug &, QDnsLookupRunnable *)
#define qCWarning(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)