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