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
qhostinfo_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
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 QHOSTINFO_P_H
6#define QHOSTINFO_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of the QHostInfo class. This header file may change from
14// version to version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtNetwork/private/qtnetworkglobal_p.h>
20#include "QtCore/qcoreapplication.h"
21#include "private/qcoreapplication_p.h"
22#include "private/qmetaobject_p.h"
23#include "QtNetwork/qhostinfo.h"
24#include "QtCore/qmutex.h"
25#include "QtCore/qwaitcondition.h"
26#include "QtCore/qobject.h"
27#include "QtCore/qpointer.h"
28#include "QtCore/qthread.h"
29#if QT_CONFIG(thread)
30#include "QtCore/qthreadpool.h"
31#endif
32#include "QtCore/qrunnable.h"
33#include "QtCore/qlist.h"
34#include "QtCore/qqueue.h"
35#include <QElapsedTimer>
36#include <QCache>
37
38#include <atomic>
39#include <memory>
40
41QT_BEGIN_NAMESPACE
42
43
44class QHostInfoResult : public QObject
45{
46 Q_OBJECT
47public:
48 explicit QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot);
49 ~QHostInfoResult() override;
50
51 void postResultsReady(const QHostInfo &info);
52
53Q_SIGNALS:
54 void resultsReady(const QHostInfo &info);
55
56private Q_SLOTS:
57 void finalizePostResultsReady(const QHostInfo &info);
58
59private:
60 QHostInfoResult(QHostInfoResult *other)
61 : receiver(other->receiver.get() != other ? other->receiver.get() : this),
62 slotObj{std::move(other->slotObj)}
63 {
64 // cleanup if the application terminates before results are delivered
65 connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
66 this, &QObject::deleteLater);
67 // maintain thread affinity
68 moveToThread(other->thread());
69 }
70
71 // receiver is either a QObject provided by the user,
72 // or it's set to `this` (to emulate the behavior of the contextless connect())
73 QPointer<const QObject> receiver = nullptr;
74 QtPrivate::SlotObjUniquePtr slotObj;
75};
76
78{
79public:
80 static QHostInfo fromName(const QString &hostName);
81 static QHostInfo lookup(const QString &hostName);
82 static QHostInfo reverseLookup(const QHostAddress &address);
83};
84
86{
87public:
90 errorStr(QLatin1StringView(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))),
91 lookupId(0)
92 {
93 }
94 static int lookupHostImpl(const QString &name,
95 const QObject *receiver,
96 QtPrivate::QSlotObjectBase *slotObj,
97 const char *member);
98
104};
105
106// These functions are outside of the QHostInfo class and strictly internal.
107// Do NOT use them outside of QAbstractSocket.
108QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
109void Q_AUTOTEST_EXPORT qt_qhostinfo_clear_cache();
110void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e);
111void Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(const QString &hostname, const QHostInfo &resolution);
112
114{
115public:
116#if defined(QT_BUILD_INTERNAL) || QT_CONFIG(hostinfocache)
117 // unit tests require the ability to manipulate the cache
119 const int max_age; // seconds
120
121 QHostInfo get(const QString &name, bool *valid);
122 void put(const QString &name, const QHostInfo &info);
123 void clear();
124
125 bool isEnabled() { return enabled.load(std::memory_order_relaxed); }
126 // this function is currently only used for the auto tests
127 // and not usable by public API
129private:
130 std::atomic<bool> enabled;
131 struct QHostInfoCacheElement {
134 };
137#else
138 QHostInfo get(const QString &, bool *valid) { *valid = false; return QHostInfo(); }
139 void put(const QString &, const QHostInfo &) { }
140 void clear() {}
141 bool isEnabled() { return false; }
142 void setEnabled(bool) { }
143#endif
144};
145
146// the following classes are used for the (normal) case: We use multiple threads to lookup DNS
147
149{
150public:
151 explicit QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
152 QtPrivate::SlotObjUniquePtr slotObj);
154
155 void run() override;
156
158 int id;
160};
161
162
164{
165public:
168
169 void clear();
170
171 // called from QHostInfo
173 void abortLookup(int id);
174
175 // called from QHostInfoRunnable
177 bool wasAborted(int id);
178
179#if __has_attribute(no_unique_address) && __cplusplus >= 201803L
181#endif
183
184 friend class QHostInfoRunnable;
185protected:
186#if QT_CONFIG(thread)
187 QList<QHostInfoRunnable*> currentLookups; // in progress
188 QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
189#endif
192 QList<int> abortedLookups; // ids of aborted lookups
193
194#if QT_CONFIG(thread)
196#endif
198
200
201private:
202 void rescheduleWithMutexHeld();
203};
204
205QT_END_NAMESPACE
206
207#endif // QHOSTINFO_P_H
static QHostInfo lookup(const QString &hostName)
static QHostInfo reverseLookup(const QHostAddress &address)
static QHostInfo fromName(const QString &hostName)
void setEnabled(bool)
QHostInfo get(const QString &, bool *valid)
void put(const QString &, const QHostInfo &)
QQueue< QHostInfoRunnable * > scheduledLookups
void lookupFinished(QHostInfoRunnable *r)
void scheduleLookup(QHostInfoRunnable *r)
QList< int > abortedLookups
QHostInfoCache cache
QList< QHostInfoRunnable * > finishedLookups
QList< QHostAddress > addrs
void run() override
Implement this pure virtual function in your subclass.
std::unique_ptr< QHostInfoResult > resultEmitter
~QHostInfoRunnable() override
The QHostInfo class provides static functions for host name lookups.
Definition qhostinfo.h:22
#define __has_attribute(x)
void qt_qhostinfo_clear_cache()
static int nextId()
QHostInfo qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id)