diff options
author | YAMAMOTO Atsushi <atsushi.yamamoto@signal-slot.co.jp> | 2023-02-20 11:45:47 +0900 |
---|---|---|
committer | YAMAMOTO Atsushi <atsushi.yamamoto@signal-slot.co.jp> | 2023-03-11 09:43:10 +0900 |
commit | f80b0aee604bb95630c233f8803c4d265dc5d84c (patch) | |
tree | 01aa746a5917d683944690fe2e31f9d73ea2d381 | |
parent | a25644aecf056b9b021189de5384519b6f06c1e6 (diff) |
Add test function for private network
Add a function QHostAddress::isPrivateUse to test
for IPv4 private networks and IPv6 unique local unicast addresses.
Task-number: QTBUG-111211
Change-Id: Ic8abefa7aa974fa83118aeb9f2506a5713f79f0d
Reviewed-by: Tasuku Suzuki <tasuku.suzuki@signal-slot.co.jp>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/network/kernel/qhostaddress.cpp | 32 | ||||
-rw-r--r-- | src/network/kernel/qhostaddress.h | 1 | ||||
-rw-r--r-- | tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp | 2 |
3 files changed, 28 insertions, 7 deletions
diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index b6c2e2c920..8887c4f24a 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -164,8 +164,12 @@ AddressClassification QHostAddressPrivate::classify() const return BroadcastAddress; return UnknownAddress; } + if (((a & 0xff000000U) == 0x0a000000U) // 10.0.0.0/8 + || ((a & 0xfff00000U) == 0xac100000U) // 172.16.0.0/12 + || ((a & 0xffff0000U) == 0xc0a80000U)) // 192.168.0.0/16 + return PrivateNetworkAddress; - // Not testing for PrivateNetworkAddress and TestNetworkAddress + // Not testing for TestNetworkAddress // since we don't need them yet. return GlobalAddress; } @@ -1113,7 +1117,7 @@ bool QHostAddress::isLoopback() const considered as global in new applications. This function returns true for site-local addresses too. - \sa isLoopback(), isSiteLocal(), isUniqueLocalUnicast() + \sa isLoopback(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse() */ bool QHostAddress::isGlobal() const { @@ -1131,7 +1135,7 @@ bool QHostAddress::isGlobal() const \l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml}{IANA IPv6 Address Space} registry for more information. - \sa isLoopback(), isGlobal(), isMulticast(), isSiteLocal(), isUniqueLocalUnicast() + \sa isLoopback(), isGlobal(), isMulticast(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse() */ bool QHostAddress::isLinkLocal() const { @@ -1154,7 +1158,7 @@ bool QHostAddress::isLinkLocal() const isGlobal() also returns true). Site-local addresses were replaced by Unique Local Addresses (ULA). - \sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast() + \sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse() */ bool QHostAddress::isSiteLocal() const { @@ -1175,7 +1179,7 @@ bool QHostAddress::isSiteLocal() const 4193 says that, in practice, "applications may treat these addresses like global scoped addresses." Only routers need care about the distinction. - \sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast() + \sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse() */ bool QHostAddress::isUniqueLocalUnicast() const { @@ -1188,7 +1192,7 @@ bool QHostAddress::isUniqueLocalUnicast() const Returns \c true if the address is an IPv4 or IPv6 multicast address, \c false otherwise. - \sa isLoopback(), isGlobal(), isLinkLocal(), isSiteLocal(), isUniqueLocalUnicast() + \sa isLoopback(), isGlobal(), isLinkLocal(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse() */ bool QHostAddress::isMulticast() const { @@ -1205,13 +1209,27 @@ bool QHostAddress::isMulticast() const broadcast address. For that, please use \l QNetworkInterface to obtain the broadcast addresses of the local machine. - \sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast() + \sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse() */ bool QHostAddress::isBroadcast() const { return d->classify() == BroadcastAddress; } +/*! + \since 6.6 + + Returns \c true if the address is an IPv6 unique local unicast address or + IPv4 address reserved for local networks by \l {RFC 1918}, \c false otherwise. + + \sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isBroadcast() +*/ +bool QHostAddress::isPrivateUse() const +{ + const AddressClassification classification = d->classify(); + return (classification == PrivateNetworkAddress) || (classification == UniqueLocalAddress); +} + #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug d, const QHostAddress &address) { diff --git a/src/network/kernel/qhostaddress.h b/src/network/kernel/qhostaddress.h index e71f954ac8..6aa045c959 100644 --- a/src/network/kernel/qhostaddress.h +++ b/src/network/kernel/qhostaddress.h @@ -127,6 +127,7 @@ public: bool isUniqueLocalUnicast() const; bool isMulticast() const; bool isBroadcast() const; + bool isPrivateUse() const; static QPair<QHostAddress, int> parseSubnet(const QString &subnet); diff --git a/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp index f735c3897f..1135420c47 100644 --- a/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp +++ b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp @@ -703,6 +703,7 @@ void tst_QHostAddress::classification() bool isUniqueLocalAddress = (result == UniqueLocalAddress); bool isMulticast = (result == MulticastAddress); bool isBroadcast = (result == BroadcastAddress); + bool isPrivateUse = (result == PrivateNetworkAddress || result == UniqueLocalAddress); QCOMPARE(address.isLoopback(), isLoopback); QCOMPARE(address.isGlobal(), isGlobal); @@ -711,6 +712,7 @@ void tst_QHostAddress::classification() QCOMPARE(address.isUniqueLocalUnicast(), isUniqueLocalAddress); QCOMPARE(address.isMulticast(), isMulticast); QCOMPARE(address.isBroadcast(), isBroadcast); + QCOMPARE(address.isPrivateUse(), isPrivateUse); } void tst_QHostAddress::convertv4v6_data() |