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