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
47
49{
50public:
52 bool authenticData = false;
54
63
64#if QT_CONFIG(ssl)
66#endif
67
68 // helper methods
69 void setError(QDnsLookup::Error err, QString &&msg)
70 {
71 error = err;
72 errorString = std::move(msg);
73 }
74
75 void makeResolverSystemError(int code = -1)
76 {
77 Q_ASSERT(allAreEmpty());
78 setError(QDnsLookup::ResolverError, qt_error_string(code));
79 }
80
82 {
83 Q_ASSERT(allAreEmpty());
84 setError(QDnsLookup::TimeoutError, QDnsLookup::tr("Request timed out"));
85 }
86
87 void makeDnsRcodeError(quint8 rcode)
88 {
89 Q_ASSERT(allAreEmpty());
90 switch (rcode) {
91 case 1: // FORMERR
92 error = QDnsLookup::InvalidRequestError;
93 errorString = QDnsLookup::tr("Server could not process query");
94 return;
95 case 2: // SERVFAIL
96 case 4: // NOTIMP
97 error = QDnsLookup::ServerFailureError;
98 errorString = QDnsLookup::tr("Server failure");
99 return;
100 case 3: // NXDOMAIN
101 error = QDnsLookup::NotFoundError;
102 errorString = QDnsLookup::tr("Non existent domain");
103 return;
104 case 5: // REFUSED
105 error = QDnsLookup::ServerRefusedError;
106 errorString = QDnsLookup::tr("Server refused to answer");
107 return;
108 default:
109 error = QDnsLookup::InvalidReplyError;
110 errorString = QDnsLookup::tr("Invalid reply received (rcode %1)")
111 .arg(rcode);
112 return;
113 }
114 }
115
116 void makeInvalidReplyError(QString &&msg = QString())
117 {
118 if (msg.isEmpty())
119 msg = QDnsLookup::tr("Invalid reply received");
120 else
121 msg = QDnsLookup::tr("Invalid reply received (%1)").arg(std::move(msg));
122 *this = QDnsLookupReply(); // empty our lists
123 setError(QDnsLookup::InvalidReplyError, std::move(msg));
124 }
125
126private:
127 bool allAreEmpty() const
128 {
129 return canonicalNameRecords.isEmpty()
130 && hostAddressRecords.isEmpty()
131 && mailExchangeRecords.isEmpty()
132 && nameServerRecords.isEmpty()
133 && pointerRecords.isEmpty()
134 && serviceRecords.isEmpty()
135 && tlsAssociationRecords.isEmpty()
136 && textRecords.isEmpty();
137 }
138};
139
141{
142public:
144 : type(QDnsLookup::A)
145 , port(0)
147 { }
148
150 {
151 emit q_func()->nameChanged(name);
152 }
154 &QDnsLookupPrivate::nameChanged);
155
157 {
158 emit q_func()->nameserverChanged(nameserver);
159 }
160 Q_OBJECT_BINDABLE_PROPERTY(QDnsLookupPrivate, QHostAddress, nameserver,
161 &QDnsLookupPrivate::nameserverChanged);
162
164 {
165 emit q_func()->typeChanged(type);
166 }
167
169 type, &QDnsLookupPrivate::typeChanged);
170
172 {
173 emit q_func()->nameserverPortChanged(port);
174 }
175
177 port, &QDnsLookupPrivate::nameserverPortChanged);
178
180 {
181 emit q_func()->nameserverProtocolChanged(protocol);
182 }
183
185 protocol, &QDnsLookupPrivate::nameserverProtocolChanged);
186
189 bool isFinished = false;
190
191#if QT_CONFIG(ssl)
193#endif
194
195 Q_DECLARE_PUBLIC(QDnsLookup)
196};
197
199{
201
202public:
203#ifdef Q_OS_WIN
204 using EncodedLabel = QString;
205#else
207#endif
208 // minimum IPv6 MTU (1280) minus the IPv6 (40) and UDP headers (8)
209 static constexpr qsizetype ReplyBufferSize = 1280 - 40 - 8;
211
213 void run() override;
214 bool sendDnsOverTls(QDnsLookupReply *reply, QSpan<unsigned char> query, ReplyBuffer &response);
215
216signals:
218
219private:
220 template <typename T> static QString decodeLabel(T encodedLabel)
221 {
222 return qt_ACE_do(encodedLabel.toString(), NormalizeAce, ForbidLeadingDot);
223 }
224 void query(QDnsLookupReply *reply);
225
226 EncodedLabel requestName;
227 QHostAddress nameserver;
229 quint16 port;
231
232#if QT_CONFIG(ssl)
234#endif
235 friend QDebug operator<<(QDebug &, QDnsLookupRunnable *);
236};
237
239{
240public:
242 : timeToLive(0)
243 { }
244
247};
248
257
259{
260public:
263
264 QHostAddress value;
265};
266
277
292
301
310
311QT_END_NAMESPACE
312
313#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.
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,...)