diff options
author | Shane Kearns <shane.kearns@accenture.com> | 2011-10-24 11:01:09 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-10-24 13:56:41 +0200 |
commit | 3736dcb65e92ea3d6f1af827a52b09ebd1250f7e (patch) | |
tree | 6dfe2acfabd55ac35135dfd19c4ada764001e475 /src/network/kernel | |
parent | 29c30a20bab4c4ea892b95c08c71bb5f136bb82c (diff) | |
download | qtbase-3736dcb65e92ea3d6f1af827a52b09ebd1250f7e.tar.gz |
Network - Add QHostAddress::isLoopback API.
The standard IPv4 loopback address is 127.0.0.1, however anything in
the 127.0.0.0/8 range is also a loopback address.
isLoopback returns true for any address that is in the IPv4 loopback
address range, or is the single IPv6 loopback address ::1
Task-number: QTBUG-22246
Change-Id: Ic39100e2e97a52db700e01b109998a1cfd4335e3
Reviewed-by: Thiago Macieira (Intel) <thiago.macieira@intel.com>
Diffstat (limited to 'src/network/kernel')
-rw-r--r-- | src/network/kernel/qhostaddress.cpp | 22 | ||||
-rw-r--r-- | src/network/kernel/qhostaddress.h | 2 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index d66d471206..cbd804c44b 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -1109,6 +1109,28 @@ QPair<QHostAddress, int> QHostAddress::parseSubnet(const QString &subnet) return qMakePair(QHostAddress(addr), netmask); } +/*! + \since 5.0 + + returns true if the address is the IPv6 loopback address, or any + of the IPv4 loopback addresses. +*/ +bool QHostAddress::isLoopback() const +{ + QT_ENSURE_PARSED(this); + if ((d->a & 0xFF000000) == 0x7F000000) + return true; // v4 range (including IPv6 wrapped IPv4 addresses) + if (d->protocol == QAbstractSocket::IPv6Protocol) { + if (d->a6.c[15] != 1) + return false; + for (int i = 0; i < 15; i++) + if (d->a6[i] != 0) + return false; + return true; + } + return false; +} + #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 3d364de6e6..83e8a7ce3c 100644 --- a/src/network/kernel/qhostaddress.h +++ b/src/network/kernel/qhostaddress.h @@ -120,6 +120,8 @@ public: bool isInSubnet(const QHostAddress &subnet, int netmask) const; bool isInSubnet(const QPair<QHostAddress, int> &subnet) const; + bool isLoopback() const; + static QPair<QHostAddress, int> parseSubnet(const QString &subnet); protected: |