Fix multicastMembershipHelper to not assume addresses are IPv4

Make it iterate the addresses available looking for an IPv4 address,
when that's what it needs, instead of just assuming the first entry in
the list (when non-empty) is IPv4.

Based on a suggestion by Dmitry Pankratov.

Task-number: QTBUG-27641
Change-Id: I1920f68ade44a996ea5c2ed691a87ff3e686f35a
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2017-02-20 11:16:12 +01:00
parent 46730ca610
commit 4c367833b4
2 changed files with 20 additions and 8 deletions

View File

@ -679,10 +679,16 @@ static bool multicastMembershipHelper(QNativeSocketEnginePrivate *d,
if (interface.isValid()) { if (interface.isValid()) {
const QList<QNetworkAddressEntry> addressEntries = interface.addressEntries(); const QList<QNetworkAddressEntry> addressEntries = interface.addressEntries();
if (!addressEntries.isEmpty()) { bool found = false;
QHostAddress firstIP = addressEntries.first().ip(); for (const QNetworkAddressEntry &entry : addressEntries) {
mreq4.imr_interface.s_addr = htonl(firstIP.toIPv4Address()); const QHostAddress ip = entry.ip();
} else { if (ip.protocol() == QAbstractSocket::IPv4Protocol) {
mreq4.imr_interface.s_addr = htonl(ip.toIPv4Address());
found = true;
break;
}
}
if (!found) {
d->setError(QAbstractSocket::NetworkError, d->setError(QAbstractSocket::NetworkError,
QNativeSocketEnginePrivate::NetworkUnreachableErrorString); QNativeSocketEnginePrivate::NetworkUnreachableErrorString);
return false; return false;

View File

@ -959,10 +959,16 @@ static bool multicastMembershipHelper(QNativeSocketEnginePrivate *d,
if (iface.isValid()) { if (iface.isValid()) {
const QList<QNetworkAddressEntry> addressEntries = iface.addressEntries(); const QList<QNetworkAddressEntry> addressEntries = iface.addressEntries();
if (!addressEntries.isEmpty()) { bool found = false;
QHostAddress firstIP = addressEntries.first().ip(); for (const QNetworkAddressEntry &entry : addressEntries) {
mreq4.imr_interface.s_addr = htonl(firstIP.toIPv4Address()); const QHostAddress ip = entry.ip();
} else { if (ip.protocol() == QAbstractSocket::IPv4Protocol) {
mreq4.imr_interface.s_addr = htonl(ip.toIPv4Address());
found = true;
break;
}
}
if (!found) {
d->setError(QAbstractSocket::NetworkError, d->setError(QAbstractSocket::NetworkError,
QNativeSocketEnginePrivate::NetworkUnreachableErrorString); QNativeSocketEnginePrivate::NetworkUnreachableErrorString);
return false; return false;