Add QHostAddress::isMulticast

This complements QHostAddress::isLoopback. The only missing check now is
for the "Any" address types, though operator== is quite fast nowadays.

Change-Id: Iee8cbc07c4434ce9b560ffff13cc2691e15014b6
Reviewed-by: Richard J. Moore <rich@kde.org>
This commit is contained in:
Thiago Macieira 2015-03-16 18:59:49 -07:00
parent 060b7ffe5b
commit 3bfba054cb
3 changed files with 66 additions and 1 deletions

View File

@ -1090,6 +1090,22 @@ bool QHostAddress::isLoopback() const
return false;
}
/*!
\since 5.6
Returns \c true if the address is an IPv4 or IPv6 multicast address, \c
false otherwise.
*/
bool QHostAddress::isMulticast() const
{
QT_ENSURE_PARSED(this);
if ((d->a & 0xF0000000) == 0xE0000000)
return true; // 224.0.0.0-239.255.255.255 (including v4-mapped IPv6 addresses)
if (d->protocol == QAbstractSocket::IPv6Protocol)
return d->a6.c[0] == 0xff;
return false;
}
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QHostAddress &address)
{

View File

@ -124,6 +124,7 @@ public:
bool isInSubnet(const QPair<QHostAddress, int> &subnet) const;
bool isLoopback() const;
bool isMulticast() const;
static QPair<QHostAddress, int> parseSubnet(const QString &subnet);

View File

@ -83,6 +83,8 @@ private slots:
void isInSubnet();
void isLoopback_data();
void isLoopback();
void isMulticast_data();
void isMulticast();
void convertv4v6_data();
void convertv4v6();
};
@ -619,6 +621,9 @@ void tst_QHostAddress::isLoopback_data()
QTest::addColumn<QHostAddress>("address");
QTest::addColumn<bool>("result");
QTest::newRow("default") << QHostAddress() << false;
QTest::newRow("invalid") << QHostAddress("&&&") << false;
QTest::newRow("ipv6_loop") << QHostAddress(QHostAddress::LocalHostIPv6) << true;
QTest::newRow("::1") << QHostAddress("::1") << true;
@ -627,7 +632,6 @@ void tst_QHostAddress::isLoopback_data()
QTest::newRow("127.0.0.2") << QHostAddress("127.0.0.2") << true;
QTest::newRow("127.3.2.1") << QHostAddress("127.3.2.1") << true;
QTest::newRow("default") << QHostAddress() << false;
QTest::newRow("1.2.3.4") << QHostAddress("1.2.3.4") << false;
QTest::newRow("10.0.0.4") << QHostAddress("10.0.0.4") << false;
QTest::newRow("192.168.3.4") << QHostAddress("192.168.3.4") << false;
@ -652,6 +656,50 @@ void tst_QHostAddress::isLoopback()
QCOMPARE(address.isLoopback(), result);
}
void tst_QHostAddress::isMulticast_data()
{
QTest::addColumn<QHostAddress>("address");
QTest::addColumn<bool>("result");
QTest::newRow("default") << QHostAddress() << false;
QTest::newRow("invalid") << QHostAddress("&&&") << false;
QTest::newRow("ipv6_loop") << QHostAddress(QHostAddress::LocalHostIPv6) << false;
QTest::newRow("::1") << QHostAddress("::1") << false;
QTest::newRow("ipv4_loop") << QHostAddress(QHostAddress::LocalHost) << false;
QTest::newRow("127.0.0.1") << QHostAddress("127.0.0.1") << false;
QTest::newRow("::") << QHostAddress("::") << false;
QTest::newRow("Any") << QHostAddress(QHostAddress::Any) << false;
QTest::newRow("AnyIPv4") << QHostAddress(QHostAddress::AnyIPv4) << false;
QTest::newRow("AnyIPv6") << QHostAddress(QHostAddress::AnyIPv6) << false;
QTest::newRow("Broadcast") << QHostAddress(QHostAddress::Broadcast) << false;
QTest::newRow("Null") << QHostAddress(QHostAddress::Null) << false;
QTest::newRow("223.255.255.255") << QHostAddress("223.255.255.255") << false;
QTest::newRow("224.0.0.0") << QHostAddress("224.0.0.0") << true;
QTest::newRow("239.255.255.255") << QHostAddress("239.255.255.255") << true;
QTest::newRow("240.0.0.0") << QHostAddress("240.0.0.0") << false;
QTest::newRow("::ffff:223.255.255.255") << QHostAddress("::ffff:223.255.255.255") << false;
QTest::newRow("::ffff:224.0.0.0") << QHostAddress("::ffff:224.0.0.0") << true;
QTest::newRow("::ffff:239.255.255.255") << QHostAddress("::ffff:239.255.255.255") << true;
QTest::newRow("::ffff:240.0.0.0") << QHostAddress("::ffff:240.0.0.0") << false;
QTest::newRow("fc00::") << QHostAddress("fc00::") << false;
QTest::newRow("fe80::") << QHostAddress("fe80::") << false;
QTest::newRow("fec0::") << QHostAddress("fec0::") << false;
QTest::newRow("ff00::") << QHostAddress("ff00::") << true;
QTest::newRow("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") << true;
}
void tst_QHostAddress::isMulticast()
{
QFETCH(QHostAddress, address);
QFETCH(bool, result);
QCOMPARE(address.isMulticast(), result);
}
void tst_QHostAddress::convertv4v6_data()
{
QTest::addColumn<QHostAddress>("source");