1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2016 Intel Corporation. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtNetwork module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | #ifndef QHOSTADDRESS_H |
42 | #define QHOSTADDRESS_H |
43 | |
44 | #include <QtNetwork/qtnetworkglobal.h> |
45 | #include <QtCore/qpair.h> |
46 | #include <QtCore/qstring.h> |
47 | #include <QtCore/qshareddata.h> |
48 | #include <QtNetwork/qabstractsocket.h> |
49 | |
50 | struct sockaddr; |
51 | |
52 | QT_BEGIN_NAMESPACE |
53 | |
54 | |
55 | class QHostAddressPrivate; |
56 | |
57 | class Q_NETWORK_EXPORT QIPv6Address |
58 | { |
59 | public: |
60 | inline quint8 &operator [](int index) { return c[index]; } |
61 | inline quint8 operator [](int index) const { return c[index]; } |
62 | quint8 c[16]; |
63 | }; |
64 | |
65 | typedef QIPv6Address Q_IPV6ADDR; |
66 | |
67 | class QHostAddress; |
68 | // qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4) |
69 | Q_NETWORK_EXPORT size_t qHash(const QHostAddress &key, size_t seed = 0) noexcept; |
70 | |
71 | class Q_NETWORK_EXPORT QHostAddress |
72 | { |
73 | public: |
74 | enum SpecialAddress { |
75 | Null, |
76 | Broadcast, |
77 | LocalHost, |
78 | LocalHostIPv6, |
79 | Any, |
80 | AnyIPv6, |
81 | AnyIPv4 |
82 | }; |
83 | enum ConversionModeFlag { |
84 | ConvertV4MappedToIPv4 = 1, |
85 | ConvertV4CompatToIPv4 = 2, |
86 | ConvertUnspecifiedAddress = 4, |
87 | ConvertLocalHost = 8, |
88 | TolerantConversion = 0xff, |
89 | |
90 | StrictConversion = 0 |
91 | }; |
92 | Q_DECLARE_FLAGS(ConversionMode, ConversionModeFlag) |
93 | |
94 | QHostAddress(); |
95 | explicit QHostAddress(quint32 ip4Addr); |
96 | explicit QHostAddress(const quint8 *ip6Addr); |
97 | explicit QHostAddress(const Q_IPV6ADDR &ip6Addr); |
98 | explicit QHostAddress(const sockaddr *address); |
99 | explicit QHostAddress(const QString &address); |
100 | QHostAddress(const QHostAddress ©); |
101 | QHostAddress(SpecialAddress address); |
102 | ~QHostAddress(); |
103 | |
104 | QHostAddress &operator=(QHostAddress &&other) noexcept |
105 | { swap(other); return *this; } |
106 | QHostAddress &operator=(const QHostAddress &other); |
107 | QHostAddress &operator=(SpecialAddress address); |
108 | |
109 | void swap(QHostAddress &other) noexcept { d.swap(other.d); } |
110 | |
111 | void setAddress(quint32 ip4Addr); |
112 | void setAddress(const quint8 *ip6Addr); |
113 | void setAddress(const Q_IPV6ADDR &ip6Addr); |
114 | void setAddress(const sockaddr *address); |
115 | bool setAddress(const QString &address); |
116 | void setAddress(SpecialAddress address); |
117 | |
118 | QAbstractSocket::NetworkLayerProtocol protocol() const; |
119 | quint32 toIPv4Address(bool *ok = nullptr) const; |
120 | Q_IPV6ADDR toIPv6Address() const; |
121 | |
122 | QString toString() const; |
123 | |
124 | QString scopeId() const; |
125 | void setScopeId(const QString &id); |
126 | |
127 | bool isEqual(const QHostAddress &address, ConversionMode mode = TolerantConversion) const; |
128 | bool operator ==(const QHostAddress &address) const; |
129 | bool operator ==(SpecialAddress address) const; |
130 | inline bool operator !=(const QHostAddress &address) const |
131 | { return !operator==(address); } |
132 | inline bool operator !=(SpecialAddress address) const |
133 | { return !operator==(address); } |
134 | bool isNull() const; |
135 | void clear(); |
136 | |
137 | bool isInSubnet(const QHostAddress &subnet, int netmask) const; |
138 | bool isInSubnet(const QPair<QHostAddress, int> &subnet) const; |
139 | |
140 | bool isLoopback() const; |
141 | bool isGlobal() const; |
142 | bool isLinkLocal() const; |
143 | bool isSiteLocal() const; |
144 | bool isUniqueLocalUnicast() const; |
145 | bool isMulticast() const; |
146 | bool isBroadcast() const; |
147 | |
148 | static QPair<QHostAddress, int> parseSubnet(const QString &subnet); |
149 | |
150 | friend Q_NETWORK_EXPORT size_t qHash(const QHostAddress &key, size_t seed) noexcept; |
151 | protected: |
152 | friend class QHostAddressPrivate; |
153 | QExplicitlySharedDataPointer<QHostAddressPrivate> d; |
154 | }; |
155 | Q_DECLARE_OPERATORS_FOR_FLAGS(QHostAddress::ConversionMode) |
156 | Q_DECLARE_SHARED(QHostAddress) |
157 | |
158 | inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddress &address2) |
159 | { return address2 == address1; } |
160 | inline bool operator!=(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs) |
161 | { return rhs != lhs; } |
162 | |
163 | #ifndef QT_NO_DEBUG_STREAM |
164 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &); |
165 | #endif |
166 | |
167 | #ifndef QT_NO_DATASTREAM |
168 | Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &); |
169 | Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &); |
170 | #endif |
171 | |
172 | QT_END_NAMESPACE |
173 | |
174 | #endif // QHOSTADDRESS_H |
175 | |