summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2013-07-13 12:02:57 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-08-06 19:47:45 +0200
commitdae087b30f4b88b24ffb233624f1b3f44285a123 (patch)
tree650030676cc665301600cb33dcbf7609366f9f32 /src
parent7a3653887cc6e551bb9fc645fb32c3b682479f42 (diff)
Avoid one extra if_indextoname per address in QNetworkInterface
Given an IPv6 address associated with a network interface, there's a fairly high chance (of 100%) that any scope ID found is that of the interface. Change-Id: Id7315473f39b68ee4c169207168dc2e60fd7d570 Reviewed-by: Richard J. Moore <rich@kde.org> Reviewed-by: Peter Hartmann <phartmann@blackberry.com> Reviewed-by: Jonas Gastal <gastal@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/network/kernel/qnetworkinterface_unix.cpp14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/network/kernel/qnetworkinterface_unix.cpp b/src/network/kernel/qnetworkinterface_unix.cpp
index 80cc5db700..b090213861 100644
--- a/src/network/kernel/qnetworkinterface_unix.cpp
+++ b/src/network/kernel/qnetworkinterface_unix.cpp
@@ -81,7 +81,7 @@
QT_BEGIN_NAMESPACE
-static QHostAddress addressFromSockaddr(sockaddr *sa)
+static QHostAddress addressFromSockaddr(sockaddr *sa, int ifindex = 0, const QString &ifname = QString())
{
QHostAddress address;
if (!sa)
@@ -92,7 +92,11 @@ static QHostAddress addressFromSockaddr(sockaddr *sa)
else if (sa->sa_family == AF_INET6) {
address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr);
int scope = ((sockaddr_in6 *)sa)->sin6_scope_id;
- if (scope) {
+ if (scope && scope == ifindex) {
+ // this is the most likely scenario:
+ // a scope ID in a socket is that of the interface this address came from
+ address.setScopeId(ifname);
+ } else if (scope) {
#ifndef QT_NO_IPV6IFNAME
char scopeid[IFNAMSIZ];
if (::if_indextoname(scope, scopeid)) {
@@ -434,14 +438,14 @@ static QList<QNetworkInterfacePrivate *> interfaceListing()
}
QNetworkAddressEntry entry;
- entry.setIp(addressFromSockaddr(ptr->ifa_addr));
+ entry.setIp(addressFromSockaddr(ptr->ifa_addr, iface->index, iface->name));
if (entry.ip().isNull())
// could not parse the address
continue;
- entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask));
+ entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask, iface->index, iface->name));
if (iface->flags & QNetworkInterface::CanBroadcast)
- entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr));
+ entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr, iface->index, iface->name));
iface->addressEntries << entry;
}