diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp index f12aedef35..fe7864d7f3 100644 --- a/src/network/socket/qnativesocketengine_unix.cpp +++ b/src/network/socket/qnativesocketengine_unix.cpp @@ -127,6 +127,21 @@ static inline void qt_socket_getPortAndAddress(const qt_sockaddr *s, quint16 *po } } +static inline uint makeScopeId(const QHostAddress &addr) +{ + QString scopeid = addr.scopeId(); + if (scopeid.isEmpty()) + return 0; + + bool ok; + uint id = scopeid.toInt(&ok); +#ifndef QT_NO_IPV6IFNAME + if (!ok) + id = ::if_nametoindex(scopeid.toLatin1()); +#endif + return id; +} + /*! \internal Creates and returns a new socket descriptor of type \a socketType @@ -382,17 +397,8 @@ bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(port); + sockAddrIPv6.sin6_scope_id = makeScopeId(addr); - QString scopeid = addr.scopeId(); - - if (!scopeid.isEmpty()) { - bool ok; - sockAddrIPv6.sin6_scope_id = scopeid.toInt(&ok); -#ifndef QT_NO_IPV6IFNAME - if (!ok) - sockAddrIPv6.sin6_scope_id = ::if_nametoindex(scopeid.toLatin1()); -#endif - } Q_IPV6ADDR ip6 = addr.toIPv6Address(); memcpy(&sockAddrIPv6.sin6_addr.s6_addr, &ip6, sizeof(ip6)); @@ -501,16 +507,8 @@ bool QNativeSocketEnginePrivate::nativeBind(const QHostAddress &address, quint16 memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(port); - QString scopeid = address.scopeId(); + sockAddrIPv6.sin6_scope_id = makeScopeId(address); - if (!scopeid.isEmpty()) { - bool ok; - sockAddrIPv6.sin6_scope_id = scopeid.toInt(&ok); -#ifndef QT_NO_IPV6IFNAME - if (!ok) - sockAddrIPv6.sin6_scope_id = ::if_nametoindex(scopeid.toLatin1()); -#endif - } Q_IPV6ADDR tmp = address.toIPv6Address(); memcpy(&sockAddrIPv6.sin6_addr.s6_addr, &tmp, sizeof(tmp)); sockAddrSize = sizeof(sockAddrIPv6); @@ -918,19 +916,10 @@ qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 l memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(port); + sockAddrIPv6.sin6_scope_id = makeScopeId(host); Q_IPV6ADDR tmp = host.toIPv6Address(); memcpy(&sockAddrIPv6.sin6_addr.s6_addr, &tmp, sizeof(tmp)); - QString scopeid = host.scopeId(); - - if (!scopeid.isEmpty()) { - bool ok; - sockAddrIPv6.sin6_scope_id = scopeid.toInt(&ok); -#ifndef QT_NO_IPV6IFNAME - if (!ok) - sockAddrIPv6.sin6_scope_id = ::if_nametoindex(scopeid.toLatin1()); -#endif - } sockAddrSize = sizeof(sockAddrIPv6); sockAddrPtr = (struct sockaddr *)&sockAddrIPv6; } else if (host.protocol() == QAbstractSocket::IPv4Protocol) {