From 5a19d52ae7498ef331520c2878128079d3723032 Mon Sep 17 00:00:00 2001 From: Martin Petersson Date: Fri, 6 May 2011 13:00:53 +0200 Subject: Make QHostAddress.toString() follow RFC-5952 for IPv6 address format. Task-number: QTBUG-18426 Reviewed-by: Peter Hartmann (cherry picked from commit ebc134db484eee31491836b619aad1ee86e3070e) --- src/network/kernel/qhostaddress.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index ae7d7a1486..4e7c286d80 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -720,7 +720,8 @@ Q_IPV6ADDR QHostAddress::toIPv6Address() const Returns the address as a string. For example, if the address is the IPv4 address 127.0.0.1, the - returned string is "127.0.0.1". + returned string is "127.0.0.1". For IPv6 the string format will + follow the RFC5952 recommendation. \sa toIPv4Address() */ @@ -741,8 +742,32 @@ QString QHostAddress::toString() const ugle[i] = (quint16(d->a6[2*i]) << 8) | quint16(d->a6[2*i+1]); } QString s; - s.sprintf("%X:%X:%X:%X:%X:%X:%X:%X", - ugle[0], ugle[1], ugle[2], ugle[3], ugle[4], ugle[5], ugle[6], ugle[7]); + QString temp; + bool zeroDetected = false; + bool zeroShortened = false; + for (int i = 0; i < 8; i++) { + if ((ugle[i] != 0) || zeroShortened) { + temp.sprintf("%X", ugle[i]); + s.append(temp); + if (zeroDetected) + zeroShortened = true; + } else { + if (!zeroDetected) { + if (i<7 && (ugle[i+1] == 0)) { + s.append(QLatin1Char(':')); + zeroDetected = true; + } else { + temp.sprintf("%X", ugle[i]); + s.append(temp); + if (i<7) + s.append(QLatin1Char(':')); + } + } + } + if (i<7 && ((ugle[i] != 0) || zeroShortened || (i==0 && zeroDetected))) + s.append(QLatin1Char(':')); + } + if (!d->scopeId.isEmpty()) s.append(QLatin1Char('%') + d->scopeId); return s; -- cgit v1.2.3 From 0ebe0414d23c15cb21ebf212bb8b82d66b68c73b Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Wed, 11 May 2011 16:51:28 +1000 Subject: Fix incorrect hardware address on systems without getifaddrs() On unix systems for which QT_NO_GETIFADDRS is defined, the way that the hardware address field is extracted from the result of a call to qt_safe_ioctl() is incorrect. The address of the ifreq.ifr_addr struct is taken rather than the appropriate member within that struct, sa_data, resulting in a memory offset and subsequently the hardware address has garbage in the first two of six fields. This commit modifies the code to pass the sa_data member instead of the address of the struct as a whole. Task-number: QTBUG-19165 Merge-request: 2614 Reviewed-by: Martin Petersson (cherry picked from commit 004ad12669ef696eeba70fd57d1eb0c67c806d1d) --- src/network/kernel/qnetworkinterface_unix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/network/kernel/qnetworkinterface_unix.cpp b/src/network/kernel/qnetworkinterface_unix.cpp index 6098bdeee0..7f251a782b 100644 --- a/src/network/kernel/qnetworkinterface_unix.cpp +++ b/src/network/kernel/qnetworkinterface_unix.cpp @@ -219,7 +219,7 @@ static QNetworkInterfacePrivate *findInterface(int socket, QList= 0) { - uchar *addr = (uchar *)&req.ifr_addr; + uchar *addr = (uchar *)req.ifr_addr.sa_data; iface->hardwareAddress = iface->makeHwAddress(6, addr); } #endif -- cgit v1.2.3