diff options
Diffstat (limited to 'src/network/kernel/qhostaddress.cpp')
-rw-r--r-- | src/network/kernel/qhostaddress.cpp | 106 |
1 files changed, 45 insertions, 61 deletions
diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index 1418d42334..0330fb091b 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -1,42 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtNetwork module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qhostaddress.h" #include "qhostaddress_p.h" @@ -137,7 +101,7 @@ void QHostAddressPrivate::setAddress(const Q_IPV6ADDR &a_) static bool parseIp6(const QString &address, QIPAddressUtils::IPv6Address &addr, QString *scopeId) { QStringView tmp(address); - int scopeIdPos = tmp.lastIndexOf(QLatin1Char('%')); + qsizetype scopeIdPos = tmp.lastIndexOf(u'%'); if (scopeIdPos != -1) { *scopeId = tmp.mid(scopeIdPos + 1).toString(); tmp.chop(tmp.size() - scopeIdPos); @@ -155,7 +119,7 @@ bool QHostAddressPrivate::parse(const QString &ipString) return false; // All IPv6 addresses contain a ':', and may contain a '.'. - if (a.contains(QLatin1Char(':'))) { + if (a.contains(u':')) { quint8 maybeIp6[16]; if (parseIp6(a, maybeIp6, &scopeId)) { setAddress(maybeIp6); @@ -200,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; } @@ -709,7 +677,7 @@ QHostAddress::NetworkLayerProtocol QHostAddress::protocol() const \l{QAbstractSocket::}{IPv6Protocol}. If the protocol is \l{QAbstractSocket::}{IPv4Protocol}, - then the address is returned an an IPv4 mapped IPv6 address. (RFC4291) + then the address is returned as an IPv4 mapped IPv6 address. (RFC4291) \sa toString() */ @@ -738,7 +706,7 @@ QString QHostAddress::toString() const } else if (d->protocol == QHostAddress::IPv6Protocol) { QIPAddressUtils::toString(s, d->a6.c); if (!d->scopeId.isEmpty()) - s.append(QLatin1Char('%') + d->scopeId); + s += u'%' + d->scopeId; } return s; } @@ -820,7 +788,7 @@ bool QHostAddress::operator==(const QHostAddress &other) const Returns \c true if this host address is the same as the \a other address given; otherwise returns \c false. - The parameter \a mode controls which conversions are preformed between addresses + The parameter \a mode controls which conversions are performed between addresses of differing protocols. If no \a mode is given, \c TolerantConversion is performed by default. @@ -854,7 +822,7 @@ bool QHostAddress::isEqual(const QHostAddress &other, ConversionMode mode) const return memcmp(&d->a6, &other.d->a6, sizeof(Q_IPV6ADDR)) == 0; case QHostAddress::AnyIPProtocol: return (mode & QHostAddress::ConvertUnspecifiedAddress) - && (other.d->a6_64.c[0] == 0) && (other.d->a6_64.c[1] == 0); + && (d->a6_64.c[0] == 0) && (d->a6_64.c[1] == 0); case QHostAddress::UnknownNetworkLayerProtocol: return false; } @@ -1043,17 +1011,17 @@ QPair<QHostAddress, int> QHostAddress::parseSubnet(const QString &subnet) if (subnet.isEmpty()) return invalid; - int slash = subnet.indexOf(QLatin1Char('/')); + qsizetype slash = subnet.indexOf(u'/'); QStringView netStr(subnet); if (slash != -1) netStr.truncate(slash); int netmask = -1; - bool isIpv6 = netStr.contains(QLatin1Char(':')); + bool isIpv6 = netStr.contains(u':'); if (slash != -1) { // is the netmask given in IP-form or in bit-count form? - if (!isIpv6 && subnet.indexOf(QLatin1Char('.'), slash + 1) != -1) { + if (!isIpv6 && subnet.indexOf(u'.', slash + 1) != -1) { // IP-style, convert it to bit-count form QHostAddress mask; QNetmask parser; @@ -1089,15 +1057,15 @@ QPair<QHostAddress, int> QHostAddress::parseSubnet(const QString &subnet) return invalid; // invalid netmask // parse the address manually - auto parts = netStr.split(QLatin1Char('.')); - if (parts.isEmpty() || parts.count() > 4) + auto parts = netStr.split(u'.'); + if (parts.isEmpty() || parts.size() > 4) return invalid; // invalid IPv4 address if (parts.constLast().isEmpty()) parts.removeLast(); quint32 addr = 0; - for (int i = 0; i < parts.count(); ++i) { + for (int i = 0; i < parts.size(); ++i) { bool ok; uint byteValue = parts.at(i).toUInt(&ok); if (!ok || byteValue > 255) @@ -1106,9 +1074,9 @@ QPair<QHostAddress, int> QHostAddress::parseSubnet(const QString &subnet) addr <<= 8; addr += byteValue; } - addr <<= 8 * (4 - parts.count()); + addr <<= 8 * (4 - parts.size()); if (netmask == -1) { - netmask = 8 * parts.count(); + netmask = 8 * parts.size(); } else if (netmask == 0) { // special case here // x86's instructions "shr" and "shl" do not operate when @@ -1143,13 +1111,13 @@ bool QHostAddress::isLoopback() const Note that IPv6 unique local unicast addresses are considered global addresses (see isUniqueLocalUnicast()), as are IPv4 addresses reserved for - local networks by \l {https://tools.ietf.org/html/rfc1918}{RFC 1918}. + local networks by \l {RFC 1918}. Also note that IPv6 site-local addresses are deprecated and should be 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 { @@ -1167,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 { @@ -1190,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 { @@ -1211,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 { @@ -1224,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 { @@ -1241,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) { @@ -1370,3 +1352,5 @@ QDataStream &operator>>(QDataStream &in, QHostAddress &address) #endif //QT_NO_DATASTREAM QT_END_NAMESPACE + +#include "moc_qhostaddress.cpp" |