1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QHOSTADDRESSPRIVATE_H |
5 | #define QHOSTADDRESSPRIVATE_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists for the convenience |
12 | // of the QHostAddress and QNetworkInterface classes. This header file may change from |
13 | // version to version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
19 | #include "qhostaddress.h" |
20 | #include "qabstractsocket.h" |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | enum AddressClassification { |
25 | LoopbackAddress = 1, |
26 | LocalNetAddress, // RFC 1122 |
27 | LinkLocalAddress, // RFC 4291 (v6), RFC 3927 (v4) |
28 | MulticastAddress, // RFC 4291 (v6), RFC 3171 (v4) |
29 | BroadcastAddress, // RFC 919, 922 |
30 | |
31 | GlobalAddress = 16, |
32 | TestNetworkAddress, // RFC 3849 (v6), RFC 5737 (v4), |
33 | PrivateNetworkAddress, // RFC 1918 |
34 | UniqueLocalAddress, // RFC 4193 |
35 | SiteLocalAddress, // RFC 4291 (deprecated by RFC 3879, should be treated as global) |
36 | |
37 | UnknownAddress = 0 // unclassified or reserved |
38 | }; |
39 | |
40 | class QNetmask |
41 | { |
42 | // stores 0-32 for IPv4, 0-128 for IPv6, or 255 for invalid |
43 | quint8 length; |
44 | public: |
45 | constexpr QNetmask() : length(255) {} |
46 | |
47 | bool setAddress(const QHostAddress &address); |
48 | QHostAddress address(QAbstractSocket::NetworkLayerProtocol protocol) const; |
49 | |
50 | int prefixLength() const { return length == 255 ? -1 : length; } |
51 | void setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int len) |
52 | { |
53 | int maxlen = -1; |
54 | if (proto == QAbstractSocket::IPv4Protocol) |
55 | maxlen = 32; |
56 | else if (proto == QAbstractSocket::IPv6Protocol) |
57 | maxlen = 128; |
58 | if (len > maxlen || len < 0) |
59 | length = 255U; |
60 | else |
61 | length = unsigned(len); |
62 | } |
63 | |
64 | friend bool operator==(QNetmask n1, QNetmask n2) |
65 | { return n1.length == n2.length; } |
66 | }; |
67 | |
68 | class QHostAddressPrivate : public QSharedData |
69 | { |
70 | public: |
71 | QHostAddressPrivate(); |
72 | |
73 | void setAddress(quint32 a_ = 0); |
74 | void setAddress(const quint8 *a_); |
75 | void setAddress(const Q_IPV6ADDR &a_); |
76 | |
77 | bool parse(const QString &ipString); |
78 | void clear(); |
79 | |
80 | QString scopeId; |
81 | |
82 | union { |
83 | Q_IPV6ADDR a6; // IPv6 address |
84 | struct { quint64 c[2]; } a6_64; |
85 | struct { quint32 c[4]; } a6_32; |
86 | }; |
87 | quint32 a; // IPv4 address |
88 | qint8 protocol; |
89 | |
90 | AddressClassification classify() const; |
91 | static AddressClassification classify(const QHostAddress &address) |
92 | { return address.d->classify(); } |
93 | |
94 | friend class QHostAddress; |
95 | }; |
96 | |
97 | QT_END_NAMESPACE |
98 | |
99 | #endif |
100 | |