QNativeSocketEngine: merge duplicated code for sin6_scope_id

Let's make sure we don't accidentally have it wrong in one of the three
versions.

Change-Id: Iee8cbc07c4434ce9b560ffff13c9bcf75ba66a66
Reviewed-by: Richard J. Moore <rich@kde.org>
This commit is contained in:
Thiago Macieira 2015-03-08 22:22:07 -07:00
parent 61929c30fc
commit bc59424ed4

View File

@ -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) {