QNetworkInterface: don't include addresses from inactive interfaces

Incidentally, we can fix a typo.

[ChangeLog][QtNetwork][QNetworkInterface] Changed allAddresses() to not
include addresses found in inactive interfaces, matching the user
expectations of this function. If those addresses are needed for some
purpose, the application will need to call allInterfaces() and obtain
the addresses in each interface.

Task-number: QTBUG-51922
Change-Id: Iaf4157b7efa2416d898cfffd14d969c963ec0a2a
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Thiago Macieira 2017-08-09 23:57:44 -07:00
parent 28a6529c2a
commit 0a98f790bf

View File

@ -355,9 +355,9 @@ void QNetworkAddressEntry::setBroadcast(const QHostAddress &newBroadcast)
contain zero or more IP addresses, each of which is optionally contain zero or more IP addresses, each of which is optionally
associated with a netmask and/or a broadcast address. The list of associated with a netmask and/or a broadcast address. The list of
such trios can be obtained with addressEntries(). Alternatively, such trios can be obtained with addressEntries(). Alternatively,
when the netmask or the broadcast addresses aren't necessary, use when the netmask or the broadcast addresses or other information aren't
the allAddresses() convenience function to obtain just the IP necessary, use the allAddresses() convenience function to obtain just the
addresses. IP addresses of the active interfaces.
QNetworkInterface also reports the interface's hardware address with QNetworkInterface also reports the interface's hardware address with
hardwareAddress(). hardwareAddress().
@ -516,9 +516,9 @@ QString QNetworkInterface::hardwareAddress() const
Returns the list of IP addresses that this interface possesses Returns the list of IP addresses that this interface possesses
along with their associated netmasks and broadcast addresses. along with their associated netmasks and broadcast addresses.
If the netmask or broadcast address information is not necessary, If the netmask or broadcast address or other information is not necessary,
you can call the allAddresses() function to obtain just the IP you can call the allAddresses() function to obtain just the IP addresses of
addresses. the active interfaces.
*/ */
QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const
{ {
@ -624,16 +624,21 @@ QList<QNetworkInterface> QNetworkInterface::allInterfaces()
} }
/*! /*!
This convenience function returns all IP addresses found on the This convenience function returns all IP addresses found on the host
host machine. It is equivalent to calling addressEntries() on all the machine. It is equivalent to calling addressEntries() on all the objects
objects returned by allInterfaces() to obtain lists of QHostAddress returned by allInterfaces() that are in the QNetworkInterface::IsUp state
objects then calling QHostAddress::ip() on each of these. to obtain lists of QNetworkAddressEntry objects then calling
QNetworkAddressEntry::ip() on each of these.
*/ */
QList<QHostAddress> QNetworkInterface::allAddresses() QList<QHostAddress> QNetworkInterface::allAddresses()
{ {
const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces(); const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
QList<QHostAddress> result; QList<QHostAddress> result;
for (const auto &p : privs) { for (const auto &p : privs) {
// skip addresses if the interface isn't up
if ((p->flags & QNetworkInterface::IsUp) == 0)
continue;
for (const QNetworkAddressEntry &entry : qAsConst(p->addressEntries)) for (const QNetworkAddressEntry &entry : qAsConst(p->addressEntries))
result += entry.ip(); result += entry.ip();
} }