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:
116 const int max_age; // seconds
117
118 QHostInfo get(const QString &name, bool *valid);
119 void put(const QString &name, const QHostInfo &info);
120 void clear();
121
122 bool isEnabled() { return enabled.load(std::memory_order_relaxed); }
123 // this function is currently only used for the auto tests
124 // and not usable by public API
125 void setEnabled(bool e) { enabled.store(e, std::memory_order_relaxed); }
126private:
127 std::atomic<bool> enabled;
128 struct QHostInfoCacheElement {
129 QHostInfo info;
130 QElapsedTimer age;
131 };
132 QCache<QString,QHostInfoCacheElement> cache;
133 QMutex mutex;
134};
135
136// the following classes are used for the (normal) case: We use multiple threads to lookup DNS
137
139{
140public:
141 explicit QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
142 QtPrivate::SlotObjUniquePtr slotObj);
144
145 void run() override;
146
148 int id;
149 QHostInfoResult resultEmitter;
150};
151
152
154{
155public:
158
159 void clear();
160
161 // called from QHostInfo
163 void abortLookup(int id);
164
165 // called from QHostInfoRunnable
167 bool wasAborted(int id);
168
170
171 friend class QHostInfoRunnable;
172protected:
173#if QT_CONFIG(thread)
174 QList<QHostInfoRunnable*> currentLookups; // in progress
175 QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
176#endif
179 QList<int> abortedLookups; // ids of aborted lookups
180
181#if QT_CONFIG(thread)
183#endif
185
187
188private:
189 void rescheduleWithMutexHeld();
190};
191
192QT_END_NAMESPACE
193
194#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 e)
QHostInfo get(const QString &name, bool *valid)
const int max_age
void put(const QString &name, const QHostInfo &info)
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:20
void qt_qhostinfo_clear_cache()
static int nextId()
QHostInfo qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id)