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

source code of qtbase/src/network/kernel/qhostaddress.h