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 uint qHash(const QHostAddress &key, uint 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(quint8 *ip6Addr); // ### Qt 6: remove me
97     explicit QHostAddress(const quint8 *ip6Addr);
98     explicit QHostAddress(const Q_IPV6ADDR &ip6Addr);
99     explicit QHostAddress(const sockaddr *address);
100     explicit QHostAddress(const QString &address);
101     QHostAddress(const QHostAddress &copy);
102     QHostAddress(SpecialAddress address);
103     ~QHostAddress();
104 
105     QHostAddress &operator=(QHostAddress &&other) noexcept
106     { swap(other); return *this; }
107     QHostAddress &operator=(const QHostAddress &other);
108 #if QT_DEPRECATED_SINCE(5, 8)
109     QT_DEPRECATED_X("use = QHostAddress(string) instead")
110     QHostAddress &operator=(const QString &address);
111 #endif
112     QHostAddress &operator=(SpecialAddress address);
113 
swap(QHostAddress & other)114     void swap(QHostAddress &other) noexcept { d.swap(other.d); }
115 
116     void setAddress(quint32 ip4Addr);
117     void setAddress(quint8 *ip6Addr);   // ### Qt 6: remove me
118     void setAddress(const quint8 *ip6Addr);
119     void setAddress(const Q_IPV6ADDR &ip6Addr);
120     void setAddress(const sockaddr *address);
121     bool setAddress(const QString &address);
122     void setAddress(SpecialAddress address);
123 
124     QAbstractSocket::NetworkLayerProtocol protocol() const;
125     quint32 toIPv4Address() const; // ### Qt6: merge with next overload
126     quint32 toIPv4Address(bool *ok) const;
127     Q_IPV6ADDR toIPv6Address() const;
128 
129     QString toString() const;
130 
131     QString scopeId() const;
132     void setScopeId(const QString &id);
133 
134     bool isEqual(const QHostAddress &address, ConversionMode mode = TolerantConversion) const;
135     bool operator ==(const QHostAddress &address) const;
136     bool operator ==(SpecialAddress address) const;
137     inline bool operator !=(const QHostAddress &address) const
138     { return !operator==(address); }
139     inline bool operator !=(SpecialAddress address) const
140     { return !operator==(address); }
141     bool isNull() const;
142     void clear();
143 
144     bool isInSubnet(const QHostAddress &subnet, int netmask) const;
145     bool isInSubnet(const QPair<QHostAddress, int> &subnet) const;
146 
147     bool isLoopback() const;
148     bool isGlobal() const;
149     bool isLinkLocal() const;
150     bool isSiteLocal() const;
151     bool isUniqueLocalUnicast() const;
152     bool isMulticast() const;
153     bool isBroadcast() const;
154 
155     static QPair<QHostAddress, int> parseSubnet(const QString &subnet);
156 
157     friend Q_NETWORK_EXPORT uint qHash(const QHostAddress &key, uint seed) noexcept;
158 protected:
159     friend class QHostAddressPrivate;
160     QExplicitlySharedDataPointer<QHostAddressPrivate> d;
161 };
162 Q_DECLARE_OPERATORS_FOR_FLAGS(QHostAddress::ConversionMode)
163 Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QHostAddress)
164 
165 inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddress &address2)
166 { return address2 == address1; }
167 inline bool operator!=(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs)
168 { return rhs != lhs; }
169 
170 #ifndef QT_NO_DEBUG_STREAM
171 Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &);
172 #endif
173 
174 #ifndef QT_NO_DATASTREAM
175 Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &);
176 Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &);
177 #endif
178 
179 QT_END_NAMESPACE
180 
181 #endif // QHOSTADDRESS_H
182