1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QHOSTINFO_P_H
41#define QHOSTINFO_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of the QHostInfo class. This header file may change from
49// version to version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtNetwork/private/qtnetworkglobal_p.h>
55#include "QtCore/qcoreapplication.h"
56#include "private/qcoreapplication_p.h"
57#include "private/qmetaobject_p.h"
58#include "QtNetwork/qhostinfo.h"
59#include "QtCore/qmutex.h"
60#include "QtCore/qwaitcondition.h"
61#include "QtCore/qobject.h"
62#include "QtCore/qpointer.h"
63#include "QtCore/qthread.h"
64#if QT_CONFIG(thread)
65#include "QtCore/qthreadpool.h"
66#endif
67#include "QtCore/qrunnable.h"
68#include "QtCore/qlist.h"
69#include "QtCore/qqueue.h"
70#include <QElapsedTimer>
71#include <QCache>
72
73#include <QSharedPointer>
74
75#include <atomic>
76
77QT_BEGIN_NAMESPACE
78
79
80class QHostInfoResult : public QObject
81{
82 Q_OBJECT
83public:
84 QHostInfoResult(const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
85 : receiver(receiver), slotObj(slotObj),
86 withContextObject(slotObj && receiver)
87 {
88 if (receiver)
89 moveToThread(receiver->thread());
90 }
91
92 void postResultsReady(const QHostInfo &info);
93
94Q_SIGNALS:
95 void resultsReady(const QHostInfo &info);
96
97protected:
98 bool event(QEvent *event) override;
99
100private:
101 QHostInfoResult(const QHostInfoResult *other)
102 : receiver(other->receiver), slotObj(other->slotObj),
103 withContextObject(other->withContextObject)
104 {
105 // cleanup if the application terminates before results are delivered
106 connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
107 this, &QObject::deleteLater);
108 // maintain thread affinity
109 moveToThread(other->thread());
110 }
111
112 QPointer<const QObject> receiver = nullptr;
113 QtPrivate::QSlotObjectBase *slotObj = nullptr;
114 const bool withContextObject = false;
115};
116
117class QHostInfoAgent
118{
119public:
120 static QHostInfo fromName(const QString &hostName);
121private:
122 static QHostInfo lookup(const QString &hostName);
123 static QHostInfo reverseLookup(const QHostAddress &address);
124};
125
126class QHostInfoPrivate
127{
128public:
129 inline QHostInfoPrivate()
130 : err(QHostInfo::NoError),
131 errorStr(QLatin1String(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))),
132 lookupId(0)
133 {
134 }
135 static int lookupHostImpl(const QString &name,
136 const QObject *receiver,
137 QtPrivate::QSlotObjectBase *slotObj,
138 const char *member);
139
140 QHostInfo::HostInfoError err;
141 QString errorStr;
142 QList<QHostAddress> addrs;
143 QString hostName;
144 int lookupId;
145};
146
147// These functions are outside of the QHostInfo class and strictly internal.
148// Do NOT use them outside of QAbstractSocket.
149QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
150void Q_AUTOTEST_EXPORT qt_qhostinfo_clear_cache();
151void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e);
152void Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(const QString &hostname, const QHostInfo &resolution);
153
154class QHostInfoCache
155{
156public:
157 QHostInfoCache();
158 const int max_age; // seconds
159
160 QHostInfo get(const QString &name, bool *valid);
161 void put(const QString &name, const QHostInfo &info);
162 void clear();
163
164 bool isEnabled() { return enabled.load(std::memory_order_relaxed); }
165 // this function is currently only used for the auto tests
166 // and not usable by public API
167 void setEnabled(bool e) { enabled.store(e, std::memory_order_relaxed); }
168private:
169 std::atomic<bool> enabled;
170 struct QHostInfoCacheElement {
171 QHostInfo info;
172 QElapsedTimer age;
173 };
174 QCache<QString,QHostInfoCacheElement> cache;
175 QMutex mutex;
176};
177
178// the following classes are used for the (normal) case: We use multiple threads to lookup DNS
179
180class QHostInfoRunnable : public QRunnable
181{
182public:
183 QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
184 QtPrivate::QSlotObjectBase *slotObj);
185 void run() override;
186
187 QString toBeLookedUp;
188 int id;
189 QHostInfoResult resultEmitter;
190};
191
192
193class QHostInfoLookupManager
194{
195public:
196 QHostInfoLookupManager();
197 ~QHostInfoLookupManager();
198
199 void clear();
200
201 // called from QHostInfo
202 void scheduleLookup(QHostInfoRunnable *r);
203 void abortLookup(int id);
204
205 // called from QHostInfoRunnable
206 void lookupFinished(QHostInfoRunnable *r);
207 bool wasAborted(int id);
208
209 QHostInfoCache cache;
210
211 friend class QHostInfoRunnable;
212protected:
213#if QT_CONFIG(thread)
214 QList<QHostInfoRunnable*> currentLookups; // in progress
215 QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
216#endif
217 QQueue<QHostInfoRunnable*> scheduledLookups; // not yet started
218 QList<QHostInfoRunnable*> finishedLookups; // recently finished
219 QList<int> abortedLookups; // ids of aborted lookups
220
221#if QT_CONFIG(thread)
222 QThreadPool threadPool;
223#endif
224 QMutex mutex;
225
226 bool wasDeleted;
227
228private:
229 void rescheduleWithMutexHeld();
230};
231
232QT_END_NAMESPACE
233
234#endif // QHOSTINFO_P_H
235