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_H
6#define QHOSTINFO_H
7
8#include <QtNetwork/qtnetworkglobal.h>
9#include <QtCore/qlist.h>
10#include <QtCore/qscopedpointer.h>
11#include <QtNetwork/qhostaddress.h>
12
13QT_BEGIN_NAMESPACE
14
15
16class QObject;
17class QHostInfoPrivate;
18
19class Q_NETWORK_EXPORT QHostInfo
20{
21 Q_GADGET
22public:
23 enum HostInfoError {
24 NoError,
25 HostNotFound,
26 UnknownError
27 };
28
29 explicit QHostInfo(int lookupId = -1);
30 QHostInfo(const QHostInfo &d);
31 QHostInfo(QHostInfo &&other) noexcept : d_ptr(std::exchange(obj&: other.d_ptr, new_val: nullptr)) {}
32 QHostInfo &operator=(const QHostInfo &d);
33 QHostInfo &operator=(QHostInfo &&other) noexcept { swap(other); return *this; }
34 ~QHostInfo();
35
36 void swap(QHostInfo &other) noexcept { qt_ptr_swap(lhs&: d_ptr, rhs&: other.d_ptr); }
37
38 QString hostName() const;
39 void setHostName(const QString &name);
40
41 QList<QHostAddress> addresses() const;
42 void setAddresses(const QList<QHostAddress> &addresses);
43
44 HostInfoError error() const;
45 void setError(HostInfoError error);
46
47 QString errorString() const;
48 void setErrorString(const QString &errorString);
49
50 void setLookupId(int id);
51 int lookupId() const;
52
53#if QT_NETWORK_REMOVED_SINCE(6, 7)
54 static int lookupHost(const QString &name, QObject *receiver, const char *member);
55#endif
56 static int lookupHost(const QString &name, const QObject *receiver, const char *member);
57 static void abortHostLookup(int lookupId);
58
59 static QHostInfo fromName(const QString &name);
60 static QString localHostName();
61 static QString localDomainName();
62
63#ifdef Q_QDOC
64 template<typename Functor>
65 static int lookupHost(const QString &name, const QObject *context, Functor functor);
66#else
67 // lookupHost to a callable (with context)
68 template <typename Functor>
69 static inline int lookupHost(const QString &name,
70 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
71 Functor &&func)
72 {
73 using Prototype = void(*)(QHostInfo);
74 QtPrivate::AssertCompatibleFunctions<Prototype, Functor>();
75 return lookupHostImpl(name, receiver,
76 slotObj: QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(func)),
77 member: nullptr);
78 }
79#endif // Q_QDOC
80
81#ifndef QT_NO_CONTEXTLESS_CONNECT
82 // lookupHost to a callable (without context)
83 template <typename Functor>
84 static inline int lookupHost(const QString &name, Functor &&slot)
85 {
86 return lookupHost(name, nullptr, std::forward<Functor>(slot));
87 }
88#endif // QT_NO_CONTEXTLESS_CONNECT
89
90private:
91 QHostInfoPrivate *d_ptr;
92 Q_DECLARE_PRIVATE(QHostInfo)
93
94 static int lookupHostImpl(const QString &name,
95 const QObject *receiver,
96 QtPrivate::QSlotObjectBase *slotObj,
97 const char *member);
98
99 friend QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver,
100 const char *member, bool *valid, int *id);
101};
102
103Q_DECLARE_SHARED(QHostInfo)
104
105QT_END_NAMESPACE
106
107QT_DECL_METATYPE_EXTERN(QHostInfo, Q_NETWORK_EXPORT)
108
109#endif // QHOSTINFO_H
110

source code of qtbase/src/network/kernel/qhostinfo.h