| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
|---|
| 2 | // Copyright (C) 2016 Intel Corporation. | 
|---|
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
|---|
| 4 |  | 
|---|
| 5 | #ifndef QHOSTADDRESS_H | 
|---|
| 6 | #define QHOSTADDRESS_H | 
|---|
| 7 |  | 
|---|
| 8 | #include <QtNetwork/qtnetworkglobal.h> | 
|---|
| 9 | #include <QtCore/qpair.h> | 
|---|
| 10 | #include <QtCore/qstring.h> | 
|---|
| 11 | #include <QtCore/qshareddata.h> | 
|---|
| 12 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) | 
|---|
| 13 | #include <QtNetwork/qabstractsocket.h> | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | struct sockaddr; | 
|---|
| 17 |  | 
|---|
| 18 | QT_BEGIN_NAMESPACE | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | class QHostAddressPrivate; | 
|---|
| 22 | QT_DECLARE_QESDP_SPECIALIZATION_DTOR(QHostAddressPrivate) | 
|---|
| 23 |  | 
|---|
| 24 | class QT6_ONLY(Q_NETWORK_EXPORT) QIPv6Address | 
|---|
| 25 | { | 
|---|
| 26 | public: | 
|---|
| 27 | inline quint8 &operator [](int index) { return c[index]; } | 
|---|
| 28 | inline quint8 operator [](int index) const { return c[index]; } | 
|---|
| 29 | quint8 c[16]; | 
|---|
| 30 | }; | 
|---|
| 31 |  | 
|---|
| 32 | typedef QIPv6Address Q_IPV6ADDR; | 
|---|
| 33 |  | 
|---|
| 34 | class QHostAddress; | 
|---|
| 35 | // qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4) | 
|---|
| 36 | Q_NETWORK_EXPORT size_t qHash(const QHostAddress &key, size_t seed = 0) noexcept; | 
|---|
| 37 |  | 
|---|
| 38 | class Q_NETWORK_EXPORT QHostAddress | 
|---|
| 39 | { | 
|---|
| 40 | Q_GADGET | 
|---|
| 41 | public: | 
|---|
| 42 | enum SpecialAddress { | 
|---|
| 43 | Null, | 
|---|
| 44 | Broadcast, | 
|---|
| 45 | LocalHost, | 
|---|
| 46 | LocalHostIPv6, | 
|---|
| 47 | Any, | 
|---|
| 48 | AnyIPv6, | 
|---|
| 49 | AnyIPv4 | 
|---|
| 50 | }; | 
|---|
| 51 | enum ConversionModeFlag { | 
|---|
| 52 | ConvertV4MappedToIPv4 = 1, | 
|---|
| 53 | ConvertV4CompatToIPv4 = 2, | 
|---|
| 54 | ConvertUnspecifiedAddress = 4, | 
|---|
| 55 | ConvertLocalHost = 8, | 
|---|
| 56 | TolerantConversion = 0xff, | 
|---|
| 57 |  | 
|---|
| 58 | StrictConversion = 0 | 
|---|
| 59 | }; | 
|---|
| 60 | Q_DECLARE_FLAGS(ConversionMode, ConversionModeFlag) | 
|---|
| 61 |  | 
|---|
| 62 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) | 
|---|
| 63 | using NetworkLayerProtocol = QAbstractSocket::NetworkLayerProtocol; | 
|---|
| 64 | static constexpr auto IPv4Protocol = QAbstractSocket::IPv4Protocol; | 
|---|
| 65 | static constexpr auto IPv6Protocol = QAbstractSocket::IPv6Protocol; | 
|---|
| 66 | static constexpr auto AnyIPProtocol = QAbstractSocket::AnyIPProtocol; | 
|---|
| 67 | static constexpr auto UnknownNetworkLayerProtocol = QAbstractSocket::UnknownNetworkLayerProtocol; | 
|---|
| 68 | #else | 
|---|
| 69 | enum NetworkLayerProtocol { | 
|---|
| 70 | IPv4Protocol, | 
|---|
| 71 | IPv6Protocol, | 
|---|
| 72 | AnyIPProtocol, | 
|---|
| 73 | UnknownNetworkLayerProtocol = -1 | 
|---|
| 74 | }; | 
|---|
| 75 | Q_ENUM(NetworkLayerProtocol) | 
|---|
| 76 | #endif | 
|---|
| 77 |  | 
|---|
| 78 | QHostAddress(); | 
|---|
| 79 | explicit QHostAddress(quint32 ip4Addr); | 
|---|
| 80 | explicit QHostAddress(const quint8 *ip6Addr); | 
|---|
| 81 | explicit QHostAddress(const Q_IPV6ADDR &ip6Addr); | 
|---|
| 82 | explicit QHostAddress(const sockaddr *address); | 
|---|
| 83 | explicit QHostAddress(const QString &address); | 
|---|
| 84 | QHostAddress(const QHostAddress ©); | 
|---|
| 85 | QHostAddress(QHostAddress &&other) noexcept = default; | 
|---|
| 86 | QHostAddress(SpecialAddress address); | 
|---|
| 87 | ~QHostAddress(); | 
|---|
| 88 |  | 
|---|
| 89 | QHostAddress &operator=(QHostAddress &&other) noexcept | 
|---|
| 90 | { swap(other); return *this; } | 
|---|
| 91 | QHostAddress &operator=(const QHostAddress &other); | 
|---|
| 92 | QHostAddress &operator=(SpecialAddress address); | 
|---|
| 93 |  | 
|---|
| 94 | void swap(QHostAddress &other) noexcept { d.swap(other&: other.d); } | 
|---|
| 95 |  | 
|---|
| 96 | void setAddress(quint32 ip4Addr); | 
|---|
| 97 | void setAddress(const quint8 *ip6Addr); | 
|---|
| 98 | void setAddress(const Q_IPV6ADDR &ip6Addr); | 
|---|
| 99 | void setAddress(const sockaddr *address); | 
|---|
| 100 | bool setAddress(const QString &address); | 
|---|
| 101 | void setAddress(SpecialAddress address); | 
|---|
| 102 |  | 
|---|
| 103 | NetworkLayerProtocol protocol() const; | 
|---|
| 104 | quint32 toIPv4Address(bool *ok = nullptr) const; | 
|---|
| 105 | Q_IPV6ADDR toIPv6Address() const; | 
|---|
| 106 |  | 
|---|
| 107 | QString toString() const; | 
|---|
| 108 |  | 
|---|
| 109 | QString scopeId() const; | 
|---|
| 110 | void setScopeId(const QString &id); | 
|---|
| 111 |  | 
|---|
| 112 | bool isEqual(const QHostAddress &address, ConversionMode mode = TolerantConversion) const; | 
|---|
| 113 | bool operator ==(const QHostAddress &address) const; | 
|---|
| 114 | bool operator ==(SpecialAddress address) const; | 
|---|
| 115 | inline bool operator !=(const QHostAddress &address) const | 
|---|
| 116 | { return !operator==(address); } | 
|---|
| 117 | inline bool operator !=(SpecialAddress address) const | 
|---|
| 118 | { return !operator==(address); } | 
|---|
| 119 | bool isNull() const; | 
|---|
| 120 | void clear(); | 
|---|
| 121 |  | 
|---|
| 122 | bool isInSubnet(const QHostAddress &subnet, int netmask) const; | 
|---|
| 123 | bool isInSubnet(const QPair<QHostAddress, int> &subnet) const; | 
|---|
| 124 |  | 
|---|
| 125 | bool isLoopback() const; | 
|---|
| 126 | bool isGlobal() const; | 
|---|
| 127 | bool isLinkLocal() const; | 
|---|
| 128 | bool isSiteLocal() const; | 
|---|
| 129 | bool isUniqueLocalUnicast() const; | 
|---|
| 130 | bool isMulticast() const; | 
|---|
| 131 | bool isBroadcast() const; | 
|---|
| 132 | bool isPrivateUse() const; | 
|---|
| 133 |  | 
|---|
| 134 | static QPair<QHostAddress, int> parseSubnet(const QString &subnet); | 
|---|
| 135 |  | 
|---|
| 136 | friend Q_NETWORK_EXPORT size_t qHash(const QHostAddress &key, size_t seed) noexcept; | 
|---|
| 137 |  | 
|---|
| 138 | friend bool operator ==(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs) | 
|---|
| 139 | { return rhs == lhs; } | 
|---|
| 140 | friend bool operator!=(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs) | 
|---|
| 141 | { return rhs != lhs; } | 
|---|
| 142 |  | 
|---|
| 143 | protected: | 
|---|
| 144 | friend class QHostAddressPrivate; | 
|---|
| 145 | QExplicitlySharedDataPointer<QHostAddressPrivate> d; | 
|---|
| 146 | }; | 
|---|
| 147 | Q_DECLARE_OPERATORS_FOR_FLAGS(QHostAddress::ConversionMode) | 
|---|
| 148 | Q_DECLARE_SHARED(QHostAddress) | 
|---|
| 149 |  | 
|---|
| 150 | #ifndef QT_NO_DEBUG_STREAM | 
|---|
| 151 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &); | 
|---|
| 152 | #endif | 
|---|
| 153 |  | 
|---|
| 154 | #ifndef QT_NO_DATASTREAM | 
|---|
| 155 | Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &); | 
|---|
| 156 | Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &); | 
|---|
| 157 | #endif | 
|---|
| 158 |  | 
|---|
| 159 | QT_END_NAMESPACE | 
|---|
| 160 |  | 
|---|
| 161 | #endif // QHOSTADDRESS_H | 
|---|
| 162 |  | 
|---|