From c72311440b160f37d5175c131508d37249889a36 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 11 Aug 2015 16:42:34 -0700 Subject: QNetworkInterface: make the name lookup search numbers in string forms That's how QHostAddress::scopeId() stores them, so we ought to look them up the same way. Change-Id: I7de033f80b0e4431b7f1ffff13f98cf87d45ebc6 Reviewed-by: Richard J. Moore --- src/network/kernel/qhostaddress.cpp | 12 +++++++++--- src/network/kernel/qnetworkinterface.cpp | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index 579567023e..5087576b44 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -785,7 +785,7 @@ QString QHostAddress::toString() const usually the same as the interface name (e.g., "eth0", "en1") or number (e.g., "1", "2"). - \sa setScopeId() + \sa setScopeId(), QNetworkInterface, QNetworkInterface::interfaceFromName */ QString QHostAddress::scopeId() const { @@ -796,8 +796,14 @@ QString QHostAddress::scopeId() const /*! \since 4.1 - Sets the IPv6 scope ID of the address to \a id. If the address - protocol is not IPv6, this function does nothing. + Sets the IPv6 scope ID of the address to \a id. If the address protocol is + not IPv6, this function does nothing. The scope ID may be set as an + interface name (such as "eth0" or "en1") or as an integer representing the + interface index. If \a id is an interface name, QtNetwork will convert to + an interface index using QNetworkInterface::interfaceIndexFromName() before + calling the operating system networking functions. + + \sa scopeId(), QNetworkInterface, QNetworkInterface::interfaceFromName */ void QHostAddress::setScopeId(const QString &id) { diff --git a/src/network/kernel/qnetworkinterface.cpp b/src/network/kernel/qnetworkinterface.cpp index 2fbbf56e0f..4a527052d1 100644 --- a/src/network/kernel/qnetworkinterface.cpp +++ b/src/network/kernel/qnetworkinterface.cpp @@ -86,9 +86,16 @@ QSharedDataPointer QNetworkInterfaceManager::interface { QList > interfaceList = allInterfaces(); QList >::ConstIterator it = interfaceList.constBegin(); - for ( ; it != interfaceList.constEnd(); ++it) - if ((*it)->name == name) + + bool ok; + uint index = name.toUInt(&ok); + + for ( ; it != interfaceList.constEnd(); ++it) { + if (ok && (*it)->index == int(index)) return *it; + else if ((*it)->name == name) + return *it; + } return empty; } @@ -516,6 +523,9 @@ QList QNetworkInterface::addressEntries() const name. If no such interface exists, this function returns an invalid QNetworkInterface object. + The string \a name may be either an actual interface name (such as "eth0" + or "en1") or an interface index in string form ("1", "2", etc.). + \sa name(), isValid() */ QNetworkInterface QNetworkInterface::interfaceFromName(const QString &name) -- cgit v1.2.3