1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #ifndef QHOSTADDRESS_H |
43 | #define QHOSTADDRESS_H |
44 | |
45 | #include <QtCore/qpair.h> |
46 | #include <QtCore/qstring.h> |
47 | #include <QtCore/qscopedpointer.h> |
48 | #include <QtNetwork/qabstractsocket.h> |
49 | |
50 | struct sockaddr; |
51 | |
52 | QT_BEGIN_HEADER |
53 | |
54 | QT_BEGIN_NAMESPACE |
55 | |
56 | QT_MODULE(Network) |
57 | |
58 | class QHostAddressPrivate; |
59 | |
60 | class Q_NETWORK_EXPORT QIPv6Address |
61 | { |
62 | public: |
63 | inline quint8 &operator [](int index) { return c[index]; } |
64 | inline quint8 operator [](int index) const { return c[index]; } |
65 | quint8 c[16]; |
66 | }; |
67 | |
68 | typedef QIPv6Address Q_IPV6ADDR; |
69 | |
70 | class Q_NETWORK_EXPORT QHostAddress |
71 | { |
72 | public: |
73 | enum SpecialAddress { |
74 | Null, |
75 | Broadcast, |
76 | LocalHost, |
77 | LocalHostIPv6, |
78 | Any, |
79 | AnyIPv6 |
80 | }; |
81 | |
82 | QHostAddress(); |
83 | explicit QHostAddress(quint32 ip4Addr); |
84 | explicit QHostAddress(quint8 *ip6Addr); |
85 | explicit QHostAddress(const Q_IPV6ADDR &ip6Addr); |
86 | explicit QHostAddress(const sockaddr *sockaddr); |
87 | explicit QHostAddress(const QString &address); |
88 | QHostAddress(const QHostAddress ©); |
89 | QHostAddress(SpecialAddress address); |
90 | ~QHostAddress(); |
91 | |
92 | QHostAddress &operator=(const QHostAddress &other); |
93 | QHostAddress &operator=(const QString &address); |
94 | |
95 | void setAddress(quint32 ip4Addr); |
96 | void setAddress(quint8 *ip6Addr); |
97 | void setAddress(const Q_IPV6ADDR &ip6Addr); |
98 | void setAddress(const sockaddr *sockaddr); |
99 | bool setAddress(const QString &address); |
100 | |
101 | QAbstractSocket::NetworkLayerProtocol protocol() const; |
102 | quint32 toIPv4Address() 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 operator ==(const QHostAddress &address) const; |
111 | bool operator ==(SpecialAddress address) const; |
112 | inline bool operator !=(const QHostAddress &address) const |
113 | { return !operator==(address); } |
114 | inline bool operator !=(SpecialAddress address) const |
115 | { return !operator==(address); } |
116 | bool isNull() const; |
117 | void clear(); |
118 | |
119 | #ifdef QT3_SUPPORT |
120 | inline QT3_SUPPORT quint32 ip4Addr() const { return toIPv4Address(); } |
121 | inline QT3_SUPPORT bool isIPv4Address() const { return protocol() == QAbstractSocket::IPv4Protocol |
122 | || protocol() == QAbstractSocket::UnknownNetworkLayerProtocol; } |
123 | inline QT3_SUPPORT bool isIp4Addr() const { return protocol() == QAbstractSocket::IPv4Protocol |
124 | || protocol() == QAbstractSocket::UnknownNetworkLayerProtocol; } |
125 | inline QT3_SUPPORT bool isIPv6Address() const { return protocol() == QAbstractSocket::IPv6Protocol; } |
126 | #endif |
127 | |
128 | bool isInSubnet(const QHostAddress &subnet, int netmask) const; |
129 | bool isInSubnet(const QPair<QHostAddress, int> &subnet) const; |
130 | |
131 | static QPair<QHostAddress, int> parseSubnet(const QString &subnet); |
132 | |
133 | protected: |
134 | QScopedPointer<QHostAddressPrivate> d; |
135 | }; |
136 | |
137 | inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddress &address2) |
138 | { return address2 == address1; } |
139 | |
140 | #ifndef QT_NO_DEBUG_STREAM |
141 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &); |
142 | #endif |
143 | |
144 | |
145 | Q_NETWORK_EXPORT uint qHash(const QHostAddress &key); |
146 | |
147 | #ifndef QT_NO_DATASTREAM |
148 | Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &); |
149 | Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &); |
150 | #endif |
151 | |
152 | QT_END_NAMESPACE |
153 | |
154 | QT_END_HEADER |
155 | #endif // QHOSTADDRESS_H |
156 | |