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
40QT_BEGIN_NAMESPACE
41
42
43class QHostInfoResult : public QObject
44{
45 Q_OBJECT
46public:
47 explicit QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot);
48 ~QHostInfoResult() override;
49
50 void postResultsReady(const QHostInfo &info);
51
52Q_SIGNALS:
53 void resultsReady(const QHostInfo &info);
54
55private Q_SLOTS:
56 void finalizePostResultsReady(const QHostInfo &info);
57
58private:
59 QHostInfoResult(QHostInfoResult *other)
60 : receiver(other->receiver.get() != other ? other->receiver.get() : this),
61 slotObj{std::move(other->slotObj)}
62 {
63 // cleanup if the application terminates before results are delivered
64 connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
65 this, &QObject::deleteLater);
66 // maintain thread affinity
67 moveToThread(other->thread());
68 }
69
70 // receiver is either a QObject provided by the user,
71 // or it's set to `this` (to emulate the behavior of the contextless connect())
72 QPointer<const QObject> receiver = nullptr;
73 QtPrivate::SlotObjUniquePtr slotObj;
74};
75
77{
78public:
79 static QHostInfo fromName(const QString &hostName);
80 static QHostInfo lookup(const QString &hostName);
81 static QHostInfo reverseLookup(const QHostAddress &address);
82};
83
85{
86public:
89 errorStr(QLatin1StringView(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))),
90 lookupId(0)
91 {
92 }
93 static int lookupHostImpl(const QString &name,
94 const QObject *receiver,
95 QtPrivate::QSlotObjectBase *slotObj,
96 const char *member);
97
103};
104
105// These functions are outside of the QHostInfo class and strictly internal.
106// Do NOT use them outside of QAbstractSocket.
107QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
108void Q_AUTOTEST_EXPORT qt_qhostinfo_clear_cache();
109void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e);
110void Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(const QString &hostname, const QHostInfo &resolution);
111
113{
114public:
115#if defined(QT_BUILD_INTERNAL) || QT_CONFIG(hostinfocache)
116 // unit tests require the ability to manipulate the cache
118 const int max_age; // seconds
119
120 QHostInfo get(const QString &name, bool *valid);
121 void put(const QString &name, const QHostInfo &info);
122 void clear();
123
124 bool isEnabled() { return enabled.load(std::memory_order_relaxed); }
125 // this function is currently only used for the auto tests
126 // and not usable by public API
128private:
129 std::atomic<bool> enabled;
130 struct QHostInfoCacheElement {
133 };
136#else
137 QHostInfo get(const QString &, bool *valid) { *valid = false; return QHostInfo(); }
138 void put(const QString &, const QHostInfo &) { }
139 void clear() {}
140 bool isEnabled() { return false; }
141 void setEnabled(bool) { }
142#endif
143};
144
145// the following classes are used for the (normal) case: We use multiple threads to lookup DNS
146
148{
149public:
150 explicit QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
151 QtPrivate::SlotObjUniquePtr slotObj);
153
154 void run() override;
155
157 int id;
158 QHostInfoResult resultEmitter;
159};
160
161
163{
164public:
167
168 void clear();
169
170 // called from QHostInfo
172 void abortLookup(int id);
173
174 // called from QHostInfoRunnable
176 bool wasAborted(int id);
177
178#if __has_attribute(no_unique_address) && __cplusplus >= 201803L
180#endif
182
183 friend class QHostInfoRunnable;
184protected:
185#if QT_CONFIG(thread)
186 QList<QHostInfoRunnable*> currentLookups; // in progress
187 QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
188#endif
191 QList<int> abortedLookups; // ids of aborted lookups
192
193#if QT_CONFIG(thread)
195#endif
197
199
200private:
201 void rescheduleWithMutexHeld();
202};
203
204QT_END_NAMESPACE
205
206#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)
void abortLookup(int id)
QList< int > abortedLookups
QHostInfoCache cache
QList< QHostInfoRunnable * > finishedLookups
QList< QHostAddress > addrs
void run() override
Implement this pure virtual function in your subclass.
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)