1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2016 The Qt Company Ltd. 4 ** Contact: https://www.qt.io/licensing/ 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 The Qt Company. For licensing terms 14 ** and conditions see https://www.qt.io/terms-conditions. For further 15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software 20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 21 ** packaging of this file. Please review the following information to 22 ** ensure the GNU Lesser General Public License version 3 requirements 23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 24 ** 25 ** GNU General Public License Usage 26 ** Alternatively, this file may be used under the terms of the GNU 27 ** General Public License version 2.0 or (at your option) the GNU General 28 ** Public license version 3 or any later version approved by the KDE Free 29 ** Qt Foundation. The licenses are as published by the Free Software 30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 31 ** included in the packaging of this file. Please review the following 32 ** information to ensure the GNU General Public License requirements will 33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and 34 ** https://www.gnu.org/licenses/gpl-3.0.html. 35 ** 36 ** $QT_END_LICENSE$ 37 ** 38 ****************************************************************************/ 39 40 #ifndef QHOSTADDRESSPRIVATE_H 41 #define QHOSTADDRESSPRIVATE_H 42 43 // 44 // W A R N I N G 45 // ------------- 46 // 47 // This file is not part of the Qt API. It exists for the convenience 48 // of the QHostAddress and QNetworkInterface classes. This header file may change from 49 // version to version without notice, or even be removed. 50 // 51 // We mean it. 52 // 53 54 #include <QtNetwork/private/qtnetworkglobal_p.h> 55 #include "qhostaddress.h" 56 #include "qabstractsocket.h" 57 58 QT_BEGIN_NAMESPACE 59 60 enum AddressClassification { 61 LoopbackAddress = 1, 62 LocalNetAddress, // RFC 1122 63 LinkLocalAddress, // RFC 4291 (v6), RFC 3927 (v4) 64 MulticastAddress, // RFC 4291 (v6), RFC 3171 (v4) 65 BroadcastAddress, // RFC 919, 922 66 67 GlobalAddress = 16, 68 TestNetworkAddress, // RFC 3849 (v6), RFC 5737 (v4), 69 PrivateNetworkAddress, // RFC 1918 70 UniqueLocalAddress, // RFC 4193 71 SiteLocalAddress, // RFC 4291 (deprecated by RFC 3879, should be treated as global) 72 73 UnknownAddress = 0 // unclassified or reserved 74 }; 75 76 class QNetmask 77 { 78 // stores 0-32 for IPv4, 0-128 for IPv6, or 255 for invalid 79 quint8 length; 80 public: QNetmask()81 Q_DECL_CONSTEXPR QNetmask() : length(255) {} 82 83 bool setAddress(const QHostAddress &address); 84 QHostAddress address(QAbstractSocket::NetworkLayerProtocol protocol) const; 85 prefixLength()86 int prefixLength() const { return length == 255 ? -1 : length; } setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto,int len)87 void setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int len) 88 { 89 int maxlen = -1; 90 if (proto == QAbstractSocket::IPv4Protocol) 91 maxlen = 32; 92 else if (proto == QAbstractSocket::IPv6Protocol) 93 maxlen = 128; 94 if (len > maxlen || len < 0) 95 length = 255U; 96 else 97 length = unsigned(len); 98 } 99 100 friend bool operator==(QNetmask n1, QNetmask n2) 101 { return n1.length == n2.length; } 102 }; 103 104 class QHostAddressPrivate : public QSharedData 105 { 106 public: 107 QHostAddressPrivate(); 108 109 void setAddress(quint32 a_ = 0); 110 void setAddress(const quint8 *a_); 111 void setAddress(const Q_IPV6ADDR &a_); 112 113 bool parse(const QString &ipString); 114 void clear(); 115 116 QString scopeId; 117 118 union { 119 Q_IPV6ADDR a6; // IPv6 address 120 struct { quint64 c[2]; } a6_64; 121 struct { quint32 c[4]; } a6_32; 122 }; 123 quint32 a; // IPv4 address 124 qint8 protocol; 125 126 AddressClassification classify() const; classify(const QHostAddress & address)127 static AddressClassification classify(const QHostAddress &address) 128 { return address.d->classify(); } 129 130 friend class QHostAddress; 131 }; 132 133 QT_END_NAMESPACE 134 135 #endif 136