QNetworkInterface: make the name lookup search numbers in string forms
authorThiago Macieira <thiago.macieira@intel.com>
Tue, 11 Aug 2015 23:42:34 +0000 (16:42 -0700)
committerThiago Macieira <thiago.macieira@intel.com>
Sun, 16 Aug 2015 18:12:44 +0000 (18:12 +0000)
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 <rich@kde.org>
src/network/kernel/qhostaddress.cpp
src/network/kernel/qnetworkinterface.cpp

index 579567023e505b258e4069ab058a645b6f200477..5087576b44cd2f3174d01d908673e06c43f7eca4 100644 (file)
@@ -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)
 {
index 2fbbf56e0fa0ad5ec5d7870938f32e68395884c5..4a527052d1bc656cbb6d7561f243af23d610cf72 100644 (file)
@@ -86,9 +86,16 @@ QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interface
 {
     QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces();
     QList<QSharedDataPointer<QNetworkInterfacePrivate> >::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<QNetworkAddressEntry> 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)