QNetworkInterface: prefer SIOCGIFINDEX over if_nametoindex
authorThiago Macieira <thiago.macieira@intel.com>
Tue, 11 Aug 2015 22:21:30 +0000 (15:21 -0700)
committerThiago Macieira <thiago.macieira@intel.com>
Sun, 16 Aug 2015 18:12:39 +0000 (18:12 +0000)
On Linux (on a bad system without getifaddrs), the ioctl for
SIOCGIFINDEX should be faster than if_nametoindex. The ioctl on the
already open socket will require one syscall, while if_nametoindex will
require at least one more (to open the socket), probably more.

Change-Id: I7de033f80b0e4431b7f1ffff13f9888bf2044105
Reviewed-by: Richard J. Moore <rich@kde.org>
src/network/kernel/qnetworkinterface_unix.cpp

index 04e1ce7edd757f21666363233b067bc1e15f92c6..38cddd8f7ab139582915882945ae5f457e1b5710 100644 (file)
@@ -185,9 +185,14 @@ static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfa
     QNetworkInterfacePrivate *iface = 0;
     int ifindex = 0;
 
-#ifndef QT_NO_IPV6IFNAME
+#if !defined(QT_NO_IPV6IFNAME) || defined(SIOCGIFINDEX)
     // Get the interface index
+#  ifdef SIOCGIFINDEX
+    if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
+        ifindex = req.ifr_ifindex;
+#  else
     ifindex = if_nametoindex(req.ifr_name);
+#  endif
 
     // find the interface data
     QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();