From ac0689a666c1372182abd392bf47c4540304a56a Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Fri, 29 Mar 2024 15:24:20 +0200 Subject: [PATCH] reimplement QHostAddress via inet_pton() and inet_ntop() both inet_pton() and inet_ntop() are supposed to be part of POSIX.1-2001 (see https://linux.die.net/man/3/inet_pton and https://linux.die.net/man/3/inet_ntop), this makes QHostAddress input requirements much more strict (e.g. whitespace in the input makes the resulting QHostAddress object not valid) thus some QHostAddress tests fail but QHostInfo tests pass. amends (such as return value checks) can be done later Signed-off-by: Ivailo Monev --- CMakeLists.txt | 2 - package/freebsd/pkg-plist | 2 - scripts/incfsck.py | 2 - scripts/namefsck.py | 2 - src/network/kernel/qhostaddress.cpp | 861 ++---------------- src/network/kernel/qhostaddress.h | 46 +- src/network/kernel/qhostaddress_p.h | 14 +- src/network/kernel/qhostinfo_unix.cpp | 17 +- src/network/kernel/qnetworkinterface.cpp | 102 +-- src/network/kernel/qnetworkinterface.h | 12 +- src/network/kernel/qnetworkinterface_p.h | 2 +- src/network/kernel/qnetworkinterface_unix.cpp | 14 +- src/network/socket/qabstractsocket.cpp | 2 +- .../socket/qabstractsocketengine_unix.cpp | 75 +- src/tools/uic/cpp/cppwriteincludes.cpp | 1 - tests/auto/qhostaddress/tst_qhostaddress.cpp | 425 +++------ tests/auto/qhostinfo/tst_qhostinfo.cpp | 14 +- 17 files changed, 285 insertions(+), 1308 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 96d0dac61..da5571b3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -548,8 +548,6 @@ katie_generate_obsolete(QIncompatibleFlag QtCore qglobal.h) katie_generate_obsolete(QInputEvent QtGui qevent.h) katie_generate_obsolete(QInternal QtCore qnamespace.h) katie_generate_obsolete(QIntValidator QtGui qvalidator.h) -katie_generate_obsolete(QIPv6Address QtNetwork qhostaddress.h) -katie_generate_obsolete(Q_IPV6ADDR QtNetwork qhostaddress.h) katie_generate_obsolete(QItemEditorCreatorBase QtGui qitemeditorfactory.h) katie_generate_obsolete(QItemEditorCreator QtGui qitemeditorfactory.h) katie_generate_obsolete(QItemSelection QtGui qitemselectionmodel.h) diff --git a/package/freebsd/pkg-plist b/package/freebsd/pkg-plist index 8a35a36c2..3717f33a7 100644 --- a/package/freebsd/pkg-plist +++ b/package/freebsd/pkg-plist @@ -814,7 +814,6 @@ include/katie/QtNetwork/QAbstractSocket include/katie/QtNetwork/QCryptographicHash include/katie/QtNetwork/QHostAddress include/katie/QtNetwork/QHostInfo -include/katie/QtNetwork/QIPv6Address include/katie/QtNetwork/QLocalServer include/katie/QtNetwork/QLocalSocket include/katie/QtNetwork/QNetworkAddressEntry @@ -822,7 +821,6 @@ include/katie/QtNetwork/QNetworkInterface include/katie/QtNetwork/QTcpServer include/katie/QtNetwork/QTcpSocket include/katie/QtNetwork/QUdpSocket -include/katie/QtNetwork/Q_IPV6ADDR include/katie/QtNetwork/QtNetwork include/katie/QtNetwork/qabstractsocket.h include/katie/QtNetwork/qcryptographichash.h diff --git a/scripts/incfsck.py b/scripts/incfsck.py index 458367b56..5725f892f 100755 --- a/scripts/incfsck.py +++ b/scripts/incfsck.py @@ -246,9 +246,7 @@ incmap = { 'QtEvents': 'qevent.h', }, 'QtNetwork': { - 'QIPv6Address': 'qhostaddress.h', 'QNetworkAddressEntry': 'qnetworkinterface.h', - 'Q_IPV6ADDR': 'qhostaddress.h', }, 'QtTest': { 'QEventSizeOfChecker': 'qtestspontaneevent.h', diff --git a/scripts/namefsck.py b/scripts/namefsck.py index 71b1e1fe0..11164424f 100755 --- a/scripts/namefsck.py +++ b/scripts/namefsck.py @@ -203,7 +203,6 @@ classlist = [ "QHostInfo", "QHoverEvent", "QIODevice", - "QIPv6Address", "QIcon", "QIconEngine", "QIdentityProxyModel", @@ -578,7 +577,6 @@ classlist = [ "QXmlStreamNotationDeclarations", "QXmlStreamReader", "QXmlStreamWriter", - "Q_IPV6ADDR", "Q_PID", "Qt", "QtAlgorithms", diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index 19d87f6e1..6248334b5 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -22,9 +22,6 @@ #include "qhostaddress.h" #include "qhostaddress_p.h" #include "qdebug.h" -#include "qplatformdefs.h" -#include "qstringlist.h" -#include "qendian.h" #include "qcorecommon_p.h" #ifndef QT_NO_DATASTREAM @@ -37,325 +34,9 @@ QT_BEGIN_NAMESPACE -#define QT_ENSURE_PARSED(a) \ - do { \ - if (!(a)->d->isParsed) \ - (a)->d->parse(); \ - } while (0) - -class QHostAddressPrivate -{ -public: - QHostAddressPrivate(); - - void setAddress(quint32 a_ = 0); - void setAddress(const quint8 *a_); - void setAddress(const Q_IPV6ADDR &a_); - - bool parse(); - void clear(); - - quint32 a; // IPv4 address - Q_IPV6ADDR a6; // IPv6 address - QAbstractSocket::NetworkLayerProtocol protocol; - - QString ipString; - bool isParsed; - QString scopeId; - - friend class QHostAddress; -}; - QHostAddressPrivate::QHostAddressPrivate() - : a(0), protocol(QAbstractSocket::UnknownNetworkLayerProtocol), isParsed(true) + : protocol(QAbstractSocket::UnknownNetworkLayerProtocol) { - memset(&a6, 0, sizeof(a6)); -} - -void QHostAddressPrivate::setAddress(quint32 a_) -{ - a = a_; - protocol = QAbstractSocket::IPv4Protocol; - isParsed = true; -} - -void QHostAddressPrivate::setAddress(const quint8 *a_) -{ - for (int i = 0; i < 16; i++) - a6[i] = a_[i]; - protocol = QAbstractSocket::IPv6Protocol; - isParsed = true; -} - -void QHostAddressPrivate::setAddress(const Q_IPV6ADDR &a_) -{ - a6 = a_; - a = 0; - protocol = QAbstractSocket::IPv6Protocol; - isParsed = true; -} - -static bool parseIp4(const QString& address, quint32 *addr) -{ - const QStringList ipv4 = address.split(QLatin1String(".")); - if (ipv4.count() != 4) - return false; - - quint32 ipv4Address = 0; - for (int i = 0; i < 4; ++i) { - bool ok = false; - uint byteValue = ipv4.at(i).toUInt(&ok); - if (!ok || byteValue > 255) - return false; - - ipv4Address <<= 8; - ipv4Address += byteValue; - } - - *addr = ipv4Address; - return true; -} - -static bool parseIp6(const QString &address, quint8 *addr, QString *scopeId) -{ - QString tmp = address; - int scopeIdPos = tmp.lastIndexOf(QLatin1Char('%')); - if (scopeIdPos != -1) { - *scopeId = tmp.mid(scopeIdPos + 1); - tmp.chop(tmp.size() - scopeIdPos); - } else { - scopeId->clear(); - } - - const QStringList ipv6 = tmp.split(QLatin1String(":")); - const int count = ipv6.count(); - if (count < 3 || count > 8) - return false; - - const int colonColon = tmp.count(QLatin1String("::")); - if(count == 8 && colonColon > 1) - return false; - - // address can be compressed with a "::", but that - // may only appear once (see RFC 1884) - // the statement below means: - // if(shortened notation is not used AND - // ((pure IPv6 notation AND less than 8 parts) OR - // ((mixed IPv4/6 notation AND less than 7 parts))) - if(colonColon != 1 && count < (tmp.contains(QLatin1Char('.')) ? 7 : 8)) - return false; - - int mc = 16; - int fillCount = 9 - count; // number of 0 words to fill in the middle - for (int i = count - 1; i >= 0; --i) { - if (mc <= 0) - return false; - - if (ipv6.at(i).isEmpty()) { - if (i == count - 1) { - // special case: ":" is last character - if (!ipv6.at(i - 1).isEmpty()) - return false; - addr[--mc] = 0; - addr[--mc] = 0; - } else if (i == 0) { - // special case: ":" is first character - if (!ipv6.at(i + 1).isEmpty()) - return false; - addr[--mc] = 0; - addr[--mc] = 0; - } else { - for (int j = 0; j < fillCount; ++j) { - if (mc <= 0) - return false; - addr[--mc] = 0; - addr[--mc] = 0; - } - } - } else { - bool ok = false; - uint byteValue = ipv6.at(i).toUInt(&ok, 16); - if (ok && byteValue <= 0xffff) { - addr[--mc] = byteValue & 0xff; - addr[--mc] = (byteValue >> 8) & 0xff; - } else { - if (i != count - 1) - return false; - - // parse the ipv4 part of a mixed type - quint32 maybeIp4; - if (!parseIp4(ipv6.at(i), &maybeIp4)) - return false; - - addr[--mc] = maybeIp4 & 0xff; - addr[--mc] = (maybeIp4 >> 8) & 0xff; - addr[--mc] = (maybeIp4 >> 16) & 0xff; - addr[--mc] = (maybeIp4 >> 24) & 0xff; - --fillCount; - } - } - } - - return true; -} - -bool QHostAddressPrivate::parse() -{ - isParsed = true; - protocol = QAbstractSocket::UnknownNetworkLayerProtocol; - const QString a = ipString.simplified(); - - // All IPv6 addresses contain a ':', and may contain a '.'. - if (a.contains(QLatin1Char(':'))) { - quint8 maybeIp6[16]; - if (parseIp6(a, maybeIp6, &scopeId)) { - setAddress(maybeIp6); - protocol = QAbstractSocket::IPv6Protocol; - return true; - } - } - - // All IPv4 addresses contain a '.'. - if (a.contains(QLatin1Char('.'))) { - quint32 maybeIp4 = 0; - if (parseIp4(a, &maybeIp4)) { - setAddress(maybeIp4); - protocol = QAbstractSocket::IPv4Protocol; - return true; - } - } - - return false; -} - -void QHostAddressPrivate::clear() -{ - a = 0; - protocol = QAbstractSocket::UnknownNetworkLayerProtocol; - isParsed = true; - memset(&a6, 0, sizeof(a6)); -} - - -bool QNetmaskAddress::setAddress(const QString &address) -{ - length = -1; - QHostAddress other; - return other.setAddress(address) && setAddress(other); -} - -bool QNetmaskAddress::setAddress(const QHostAddress &address) -{ - static const quint8 zeroes[16] = { 0 }; - union { - quint32 v4; - quint8 v6[16]; - } ip; - - int netmask = 0; - quint8 *ptr = ip.v6; - quint8 *end; - length = -1; - - QHostAddress::operator=(address); - - if (d->protocol == QAbstractSocket::IPv4Protocol) { - ip.v4 = qToBigEndian(d->a); - end = ptr + 4; - } else if (d->protocol == QAbstractSocket::IPv6Protocol) { - memcpy(ip.v6, d->a6.c, 16); - end = ptr + 16; - } else { - d->clear(); - return false; - } - - while (ptr < end) { - switch (*ptr) { - case 255: { - netmask += 8; - ++ptr; - continue; - } - // intended fall throughs - case 254: - ++netmask; - case 252: - ++netmask; - case 248: - ++netmask; - case 240: - ++netmask; - case 224: - ++netmask; - case 192: - ++netmask; - case 128: - ++netmask; - case 0: - break; - // invalid IP-style netmask - default: { - d->clear(); - return false; - } - } - break; - } - - // confirm that the rest is only zeroes - if (ptr < end && memcmp(ptr + 1, zeroes, end - ptr - 1) != 0) { - d->clear(); - return false; - } - - length = netmask; - return true; -} - -static void clearBits(quint8 *where, int start, int end) -{ - Q_ASSERT(end == 32 || end == 128); - if (start == end) - return; - - // for the byte where 'start' is, clear the lower bits only - quint8 bytemask = 256 - (1 << (8 - (start & 7))); - where[start / 8] &= bytemask; - - // for the tail part, clear everything - memset(where + (start + 7) / 8, 0, end / 8 - (start + 7) / 8); -} - -int QNetmaskAddress::prefixLength() const -{ - return length; -} - -void QNetmaskAddress::setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int newLength) -{ - length = newLength; - if (length < 0 || length > (proto == QAbstractSocket::IPv4Protocol ? 32 : - proto == QAbstractSocket::IPv6Protocol ? 128 : -1)) { - // invalid information, reject - d->protocol = QAbstractSocket::UnknownNetworkLayerProtocol; - length = -1; - return; - } - - d->protocol = proto; - if (d->protocol == QAbstractSocket::IPv4Protocol) { - if (length == 0) { - d->a = 0; - } else if (length == 32) { - d->a = quint32(0xffffffff); - } else { - d->a = quint32(0xffffffff) >> (32 - length) << (32 - length); - } - } else { - memset(d->a6.c, 0xFF, sizeof(d->a6)); - clearBits(d->a6.c, length, 128); - } } /*! @@ -398,51 +79,20 @@ void QNetmaskAddress::setPrefixLength(QAbstractSocket::NetworkLayerProtocol prot \sa clear() */ QHostAddress::QHostAddress() - : d(new QHostAddressPrivate) + : d(new QHostAddressPrivate()) { } -/*! - Constructs a host address object with the IPv4 address \a ip4Addr. -*/ -QHostAddress::QHostAddress(quint32 ip4Addr) - : d(new QHostAddressPrivate) -{ - setAddress(ip4Addr); -} - -/*! - Constructs a host address object with the IPv6 address \a ip6Addr. - - \a ip6Addr must be a 16-byte array in network byte order (big - endian). -*/ -QHostAddress::QHostAddress(quint8 *ip6Addr) - : d(new QHostAddressPrivate) -{ - setAddress(ip6Addr); -} - -/*! - Constructs a host address object with the IPv6 address \a ip6Addr. -*/ -QHostAddress::QHostAddress(const Q_IPV6ADDR &ip6Addr) - : d(new QHostAddressPrivate) -{ - setAddress(ip6Addr); -} - /*! Constructs an IPv4 or IPv6 address based on the string \a address (e.g., "127.0.0.1"). \sa setAddress() */ -QHostAddress::QHostAddress(const QString &address) - : d(new QHostAddressPrivate) +QHostAddress::QHostAddress(const QByteArray &address) + : d(new QHostAddressPrivate()) { - d->ipString = address; - d->isParsed = false; + setAddress(address); } /*! @@ -454,14 +104,9 @@ QHostAddress::QHostAddress(const QString &address) \sa setAddress() */ QHostAddress::QHostAddress(const struct sockaddr *sockaddr) - : d(new QHostAddressPrivate) + : d(new QHostAddressPrivate()) { - if (sockaddr->sa_family == AF_INET) - setAddress(htonl(((sockaddr_in *)sockaddr)->sin_addr.s_addr)); -#ifndef QT_NO_IPV6 - else if (sockaddr->sa_family == AF_INET6) - setAddress(((sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr); -#endif + setAddress(sockaddr); } /*! @@ -476,29 +121,30 @@ QHostAddress::QHostAddress(const QHostAddress &address) Constructs a QHostAddress object for \a address. */ QHostAddress::QHostAddress(SpecialAddress address) - : d(new QHostAddressPrivate) + : d(new QHostAddressPrivate()) { switch (address) { - case Null: - break; - case Broadcast: { - setAddress(QLatin1String("255.255.255.255")); + case QHostAddress::Null: { break; } - case LocalHost: { - setAddress(QLatin1String("127.0.0.1")); + case QHostAddress::Broadcast: { + setAddress(QByteArray("255.255.255.255")); break; } - case LocalHostIPv6: { - setAddress(QLatin1String("::1")); + case QHostAddress::LocalHost: { + setAddress(QByteArray("127.0.0.1")); break; } - case Any: { - setAddress(QLatin1String("0.0.0.0")); + case QHostAddress::LocalHostIPv6: { + setAddress(QByteArray("::1")); break; } - case AnyIPv6: { - setAddress(QLatin1String("::")); + case QHostAddress::Any: { + setAddress(QByteArray("0.0.0.0")); + break; + } + case QHostAddress::AnyIPv6: { + setAddress(QByteArray("::")); break; } } @@ -527,7 +173,7 @@ QHostAddress &QHostAddress::operator=(const QHostAddress &address) \sa setAddress() */ -QHostAddress &QHostAddress::operator=(const QString &address) +QHostAddress &QHostAddress::operator=(const QByteArray &address) { setAddress(address); return *this; @@ -553,38 +199,9 @@ QHostAddress &QHostAddress::operator=(const QString &address) */ void QHostAddress::clear() { - d->clear(); -} - -/*! - Set the IPv4 address specified by \a ip4Addr. -*/ -void QHostAddress::setAddress(quint32 ip4Addr) -{ - d->setAddress(ip4Addr); -} - -/*! - \overload - - Set the IPv6 address specified by \a ip6Addr. - - \a ip6Addr must be an array of 16 bytes in network byte order - (high-order byte first). -*/ -void QHostAddress::setAddress(quint8 *ip6Addr) -{ - d->setAddress(ip6Addr); -} - -/*! - \overload - - Set the IPv6 address specified by \a ip6Addr. -*/ -void QHostAddress::setAddress(const Q_IPV6ADDR &ip6Addr) -{ - d->setAddress(ip6Addr); + d->protocol = QAbstractSocket::UnknownNetworkLayerProtocol; + d->ipString.clear(); + d->scopeId.clear(); } /*! @@ -595,10 +212,26 @@ void QHostAddress::setAddress(const Q_IPV6ADDR &ip6Addr) Returns true and sets the address if the address was successfully parsed; otherwise returns false. */ -bool QHostAddress::setAddress(const QString &address) +bool QHostAddress::setAddress(const QByteArray &address) { - d->ipString = address; - return d->parse(); + int result = 0; +#ifndef QT_NO_IPV6 + struct in6_addr inaddripv6; + result = inet_pton(AF_INET6, address.constData(), &inaddripv6); + if (result == 1) { + d->protocol = QAbstractSocket::IPv6Protocol; + d->ipString = address; + return true; + } +#endif + struct in_addr inaddripv4; + result = inet_pton(AF_INET, address.constData(), &inaddripv4); + if (result == 1) { + d->protocol = QAbstractSocket::IPv4Protocol; + d->ipString = address; + return true; + } + return false; } /*! @@ -612,122 +245,50 @@ bool QHostAddress::setAddress(const QString &address) void QHostAddress::setAddress(const struct sockaddr *sockaddr) { clear(); - if (sockaddr->sa_family == AF_INET) - setAddress(htonl(((sockaddr_in *)sockaddr)->sin_addr.s_addr)); + if (sockaddr->sa_family == AF_INET) { + QSTACKARRAY(char, ntopbuffer, INET_ADDRSTRLEN + 1); + if (inet_ntop(AF_INET, &((sockaddr_in *)sockaddr)->sin_addr, ntopbuffer, INET_ADDRSTRLEN) != NULL) { + d->protocol = QAbstractSocket::IPv4Protocol; + d->ipString = ntopbuffer; + } + return; + } #ifndef QT_NO_IPV6 - else if (sockaddr->sa_family == AF_INET6) - setAddress(((sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr); + if (sockaddr->sa_family == AF_INET6) { + QSTACKARRAY(char, ntopbuffer, INET6_ADDRSTRLEN + 1); + if (inet_ntop(AF_INET6, &((sockaddr_in6 *)sockaddr)->sin6_addr, ntopbuffer, INET6_ADDRSTRLEN) != NULL) { + d->protocol = QAbstractSocket::IPv6Protocol; + d->ipString = ntopbuffer; + } + } #endif } -/*! - Returns the IPv4 address as a number. - - For example, if the address is 127.0.0.1, the returned value is - 2130706433 (i.e. 0x7f000001). - - This value is only valid if the Protocol() is - \l{QAbstractSocket::}{IPv4Protocol}. - - \sa toString() -*/ -quint32 QHostAddress::toIPv4Address() const -{ - QT_ENSURE_PARSED(this); - return d->a; -} - /*! Returns the network layer protocol of the host address. */ QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol() const { - QT_ENSURE_PARSED(this); return d->protocol; } -/*! - Returns the IPv6 address as a Q_IPV6ADDR structure. The structure - consists of 16 unsigned characters. - - \snippet doc/src/snippets/code/src_network_kernel_qhostaddress.cpp 0 - - This value is only valid if the protocol() is - \l{QAbstractSocket::}{IPv6Protocol}. - - \sa toString() -*/ -Q_IPV6ADDR QHostAddress::toIPv6Address() const -{ - QT_ENSURE_PARSED(this); - return d->a6; -} - /*! 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". For IPv6 the string format will follow the RFC5952 recommendation. - - \sa toIPv4Address() */ -QString QHostAddress::toString() const +QByteArray QHostAddress::toString() const { - QT_ENSURE_PARSED(this); - if (d->protocol == QAbstractSocket::IPv4Protocol) { - const quint32 i = toIPv4Address(); - QSTACKARRAY(char, snprintfbuf, 20); - ::snprintf(snprintfbuf, sizeof(snprintfbuf), "%d.%d.%d.%d", - (i>>24) & 0xff, (i>>16) & 0xff, (i >> 8) & 0xff, i & 0xff); - return QString::fromLatin1(snprintfbuf); - } - - if (d->protocol == QAbstractSocket::IPv6Protocol) { - QSTACKARRAY(quint16, ugle, 8); - for (int i = 0; i < 8; i++) { - ugle[i] = (quint16(d->a6[2*i]) << 8) | quint16(d->a6[2*i+1]); - } - QString s; - 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; - } - - return QString(); + return d->ipString; } /*! \since 4.1 Returns the scope ID of an IPv6 address. For IPv4 addresses, or if the - address does not contain a scope ID, an empty QString is returned. + address does not contain a scope ID, an empty QByteArray is returned. The IPv6 scope ID specifies the scope of \e reachability for non-global IPv6 addresses, limiting the area in which the address can be used. All @@ -764,10 +325,12 @@ QString QHostAddress::toString() const \sa setScopeId() */ -QString QHostAddress::scopeId() const +QByteArray QHostAddress::scopeId() const { - QT_ENSURE_PARSED(this); - return (d->protocol == QAbstractSocket::IPv6Protocol) ? d->scopeId : QString(); + if (d->protocol == QAbstractSocket::IPv6Protocol) { + return d->scopeId; + } + return QByteArray(); } /*! @@ -776,11 +339,11 @@ QString QHostAddress::scopeId() const Sets the IPv6 scope ID of the address to \a id. If the address protocol is not IPv6, this function does nothing. */ -void QHostAddress::setScopeId(const QString &id) +void QHostAddress::setScopeId(const QByteArray &id) { - QT_ENSURE_PARSED(this); - if (d->protocol == QAbstractSocket::IPv6Protocol) + if (d->protocol == QAbstractSocket::IPv6Protocol) { d->scopeId = id; + } } /*! @@ -789,35 +352,11 @@ void QHostAddress::setScopeId(const QString &id) */ bool QHostAddress::operator==(const QHostAddress &other) const { - QT_ENSURE_PARSED(this); - QT_ENSURE_PARSED(&other); - - if (d->protocol == QAbstractSocket::IPv4Protocol) - return other.d->protocol == QAbstractSocket::IPv4Protocol && d->a == other.d->a; - if (d->protocol == QAbstractSocket::IPv6Protocol) { - return other.d->protocol == QAbstractSocket::IPv6Protocol - && memcmp(&d->a6, &other.d->a6, sizeof(Q_IPV6ADDR)) == 0; - } - return d->protocol == other.d->protocol; -} - -/*! - Returns true if this host address is the same as the \a other - address given; otherwise returns false. -*/ -bool QHostAddress::operator ==(SpecialAddress other) const -{ - QT_ENSURE_PARSED(this); - QHostAddress otherAddress(other); - QT_ENSURE_PARSED(&otherAddress); - - if (d->protocol == QAbstractSocket::IPv4Protocol) - return otherAddress.d->protocol == QAbstractSocket::IPv4Protocol && d->a == otherAddress.d->a; - if (d->protocol == QAbstractSocket::IPv6Protocol) { - return otherAddress.d->protocol == QAbstractSocket::IPv6Protocol - && memcmp(&d->a6, &otherAddress.d->a6, sizeof(Q_IPV6ADDR)) == 0; - } - return int(other) == int(Null); + return ( + d->protocol == other.d->protocol + && d->ipString == other.d->ipString + && d->scopeId == other.d->scopeId + ); } /*! @@ -827,228 +366,7 @@ bool QHostAddress::operator ==(SpecialAddress other) const */ bool QHostAddress::isNull() const { - QT_ENSURE_PARSED(this); - return d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol; -} - -/*! - \fn quint32 QHostAddress::ip4Addr() const - - Use toIPv4Address() instead. -*/ - -/*! - \fn bool QHostAddress::isIp4Addr() const - - Use protocol() instead. -*/ - -/*! - \fn bool QHostAddress::isIPv4Address() const - - Use protocol() instead. -*/ - -/*! - \fn bool QHostAddress::isIPv6Address() const - - Use protocol() instead. -*/ - -/*! - \since 4.5 - - Returns true if this IP is in the subnet described by the network - prefix \a subnet and netmask \a netmask. - - An IP is considered to belong to a subnet if it is contained - between the lowest and the highest address in that subnet. In the - case of IP version 4, the lowest address is the network address, - while the highest address is the broadcast address. - - The \a subnet argument does not have to be the actual network - address (the lowest address in the subnet). It can be any valid IP - belonging to that subnet. In particular, if it is equal to the IP - address held by this object, this function will always return true - (provided the netmask is a valid value). - - \sa parseSubnet() -*/ -bool QHostAddress::isInSubnet(const QHostAddress &subnet, int netmask) const -{ - QT_ENSURE_PARSED(this); - if (subnet.protocol() != d->protocol || netmask < 0) - return false; - - union { - quint32 ip; - quint8 data[4]; - } ip4, net4; - const quint8 *ip; - const quint8 *net; - if (d->protocol == QAbstractSocket::IPv4Protocol) { - if (netmask > 32) - netmask = 32; - ip4.ip = qToBigEndian(d->a); - net4.ip = qToBigEndian(subnet.d->a); - ip = ip4.data; - net = net4.data; - } else if (d->protocol == QAbstractSocket::IPv6Protocol) { - if (netmask > 128) - netmask = 128; - ip = d->a6.c; - net = subnet.d->a6.c; - } else { - return false; - } - - if (netmask >= 8 && memcmp(ip, net, netmask / 8) != 0) - return false; - if ((netmask & 7) == 0) - return true; - - // compare the last octet now - quint8 bytemask = 256 - (1 << (8 - (netmask & 7))); - quint8 ipbyte = ip[netmask / 8]; - quint8 netbyte = net[netmask / 8]; - return (ipbyte & bytemask) == (netbyte & bytemask); -} - -/*! - \since 4.5 - \overload - - Returns true if this IP is in the subnet described by \a - subnet. The QHostAddress member of \a subnet contains the network - prefix and the int (second) member contains the netmask (prefix - length). -*/ -bool QHostAddress::isInSubnet(const QPair &subnet) const -{ - return isInSubnet(subnet.first, subnet.second); -} - - -/*! - \since 4.5 - - Parses the IP and subnet information contained in \a subnet and - returns the network prefix for that network and its prefix length. - - The IP address and the netmask must be separated by a slash - (/). - - This function supports arguments in the form: - \list - \o 123.123.123.123/n where n is any value between 0 and 32 - \o 123.123.123.123/255.255.255.255 - \o /n where n is any value between 0 and 128 - \endlist - - For IP version 4, this function accepts as well missing trailing - components (i.e., less than 4 octets, like "192.168.1"), followed - or not by a dot. If the netmask is also missing in that case, it - is set to the number of octets actually passed (in the example - above, it would be 24, for 3 octets). - - \sa isInSubnet() -*/ -QPair QHostAddress::parseSubnet(const QString &subnet) -{ - // We support subnets in the form: - // ddd.ddd.ddd.ddd/nn - // ddd.ddd.ddd/nn - // ddd.ddd/nn - // ddd/nn - // ddd.ddd.ddd. - // ddd.ddd.ddd - // ddd.ddd. - // ddd.ddd - // ddd. - // ddd - // /nn - // - // where nn can be an IPv4-style netmask for the IPv4 forms - - const QPair invalid = qMakePair(QHostAddress(), -1); - if (subnet.isEmpty()) - return invalid; - - int slash = subnet.indexOf(QLatin1Char('/')); - QString netStr = subnet; - if (slash != -1) - netStr.truncate(slash); - - int netmask = -1; - bool isIpv6 = netStr.contains(QLatin1Char(':')); - - if (slash != -1) { - // is the netmask given in IP-form or in bit-count form? - if (!isIpv6 && subnet.indexOf(QLatin1Char('.'), slash + 1) != -1) { - // IP-style, convert it to bit-count form - QNetmaskAddress parser; - if (!parser.setAddress(subnet.mid(slash + 1))) - return invalid; - netmask = parser.prefixLength(); - } else { - bool ok; - netmask = subnet.mid(slash + 1).toUInt(&ok); - if (!ok) - return invalid; // failed to parse the subnet - } - } - - if (isIpv6) { - // looks like it's an IPv6 address - if (netmask > 128) - return invalid; // invalid netmask - if (netmask < 0) - netmask = 128; - - QHostAddress net; - if (!net.setAddress(netStr)) - return invalid; // failed to parse the IP - - clearBits(net.d->a6.c, netmask, 128); - return qMakePair(net, netmask); - } - - if (netmask > 32) - return invalid; // invalid netmask - - // parse the address manually - QStringList parts = netStr.split(QLatin1Char('.')); - if (parts.isEmpty() || parts.count() > 4) - return invalid; // invalid IPv4 address - - if (parts.last().isEmpty()) - parts.removeLast(); - - quint32 addr = 0; - for (int i = 0; i < parts.count(); ++i) { - bool ok; - uint byteValue = parts.at(i).toUInt(&ok); - if (!ok || byteValue > 255) - return invalid; // invalid IPv4 address - - addr <<= 8; - addr += byteValue; - } - addr <<= 8 * (4 - parts.count()); - if (netmask == -1) { - netmask = 8 * parts.count(); - } else if (netmask == 0) { - // special case here - // x86's instructions "shr" and "shl" do not operate when - // their argument is 32, so the code below doesn't work as expected - addr = 0; - } else if (netmask != 32) { - // clear remaining bits - quint32 mask = quint32(0xffffffff) >> (32 - netmask) << (32 - netmask); - addr &= mask; - } - - return qMakePair(QHostAddress(addr), netmask); + return (d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol); } #ifndef QT_NO_DEBUG_STREAM @@ -1075,20 +393,17 @@ uint qHash(const QHostAddress &key) */ QDataStream &operator<<(QDataStream &out, const QHostAddress &address) { - qint8 prot; - prot = qint8(address.protocol()); - out << prot; + out << qint8(address.protocol()); switch (address.protocol()) { - case QAbstractSocket::UnknownNetworkLayerProtocol: + case QAbstractSocket::UnknownNetworkLayerProtocol: { break; + } case QAbstractSocket::IPv4Protocol: { - out << address.toIPv4Address(); + out << address.toString(); break; } case QAbstractSocket::IPv6Protocol: { - Q_IPV6ADDR ipv6 = address.toIPv6Address(); - for (int i = 0; i < 16; ++i) - out << ipv6[i]; + out << address.toString(); out << address.scopeId(); break; } @@ -1113,19 +428,18 @@ QDataStream &operator>>(QDataStream &in, QHostAddress &address) break; } case QAbstractSocket::IPv4Protocol: { - quint32 ipv4; + QByteArray ipv4; in >> ipv4; address.setAddress(ipv4); + address.setScopeId(QByteArray()); break; } case QAbstractSocket::IPv6Protocol: { - Q_IPV6ADDR ipv6; - for (int i = 0; i < 16; ++i) - in >> ipv6[i]; - address.setAddress(ipv6); - - QString scope; + QByteArray ipv6; + QByteArray scope; + in >> ipv6; in >> scope; + address.setAddress(ipv6); address.setScopeId(scope); break; } @@ -1136,7 +450,6 @@ QDataStream &operator>>(QDataStream &in, QHostAddress &address) } return in; } - #endif //QT_NO_DATASTREAM QT_END_NAMESPACE diff --git a/src/network/kernel/qhostaddress.h b/src/network/kernel/qhostaddress.h index 45a0ce80e..a33229e84 100644 --- a/src/network/kernel/qhostaddress.h +++ b/src/network/kernel/qhostaddress.h @@ -29,22 +29,10 @@ struct sockaddr; - QT_BEGIN_NAMESPACE - class QHostAddressPrivate; -class Q_NETWORK_EXPORT QIPv6Address -{ -public: - inline quint8 &operator [](int index) { return c[index]; } - inline quint8 operator [](int index) const { return c[index]; } - quint8 c[16]; -}; - -typedef QIPv6Address Q_IPV6ADDR; - class Q_NETWORK_EXPORT QHostAddress { public: @@ -58,61 +46,39 @@ public: }; QHostAddress(); - explicit QHostAddress(quint32 ip4Addr); - explicit QHostAddress(quint8 *ip6Addr); - explicit QHostAddress(const Q_IPV6ADDR &ip6Addr); explicit QHostAddress(const sockaddr *sockaddr); - explicit QHostAddress(const QString &address); + explicit QHostAddress(const QByteArray &address); QHostAddress(const QHostAddress ©); QHostAddress(SpecialAddress address); ~QHostAddress(); QHostAddress &operator=(const QHostAddress &other); - QHostAddress &operator=(const QString &address); + QHostAddress &operator=(const QByteArray &address); - void setAddress(quint32 ip4Addr); - void setAddress(quint8 *ip6Addr); - void setAddress(const Q_IPV6ADDR &ip6Addr); void setAddress(const sockaddr *sockaddr); - bool setAddress(const QString &address); + bool setAddress(const QByteArray &address); QAbstractSocket::NetworkLayerProtocol protocol() const; - quint32 toIPv4Address() const; - Q_IPV6ADDR toIPv6Address() const; - QString toString() const; + QByteArray toString() const; - QString scopeId() const; - void setScopeId(const QString &id); + QByteArray scopeId() const; + void setScopeId(const QByteArray &id); bool operator ==(const QHostAddress &address) const; - bool operator ==(SpecialAddress address) const; inline bool operator !=(const QHostAddress &address) const { return !operator==(address); } - inline bool operator !=(SpecialAddress address) const - { return !operator==(address); } bool isNull() const; void clear(); - - bool isInSubnet(const QHostAddress &subnet, int netmask) const; - bool isInSubnet(const QPair &subnet) const; - - static QPair parseSubnet(const QString &subnet); - private: - friend class QNetmaskAddress; QScopedPointer d; }; -inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddress &address2) -{ return address2 == address1; } - #ifndef QT_NO_DEBUG_STREAM Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &); #endif - Q_NETWORK_EXPORT uint qHash(const QHostAddress &key); #ifndef QT_NO_DATASTREAM diff --git a/src/network/kernel/qhostaddress_p.h b/src/network/kernel/qhostaddress_p.h index 96c28bfd5..1fd5576bb 100644 --- a/src/network/kernel/qhostaddress_p.h +++ b/src/network/kernel/qhostaddress_p.h @@ -38,20 +38,18 @@ QT_BEGIN_NAMESPACE #include "qhostaddress.h" #include "qabstractsocket.h" -class QNetmaskAddress: public QHostAddress +class QHostAddressPrivate { - int length; public: - QNetmaskAddress() : QHostAddress(), length(-1) { } + QHostAddressPrivate(); - bool setAddress(const QString &address); - bool setAddress(const QHostAddress &address); + QAbstractSocket::NetworkLayerProtocol protocol; + QByteArray ipString; + QByteArray scopeId; - int prefixLength() const; - void setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int len); + friend class QHostAddress; }; - QT_END_NAMESPACE #endif diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp index 83fbc55d3..36745b6c2 100644 --- a/src/network/kernel/qhostinfo_unix.cpp +++ b/src/network/kernel/qhostinfo_unix.cpp @@ -65,7 +65,8 @@ QHostInfo QHostInfoPrivate::fromName(const QString &hostName) results.d->errorStr = QCoreApplication::translate("QHostInfo", "Unknown error"); QHostAddress address; - if (address.setAddress(hostName)) { + if (address.setAddress(hostName.toLatin1())) { + const QByteArray addressStr = address.toString(); #if defined(QHOSTINFO_DEBUG) qDebug("QHostInfoPrivate::fromName(%s) looking up address...", hostName.toLatin1().constData()); @@ -83,7 +84,9 @@ QHostInfo QHostInfoPrivate::fromName(const QString &hostName) saSize = sizeof(sa4); ::memset(&sa4, 0, sizeof(sa4)); sa4.sin_family = AF_INET; - sa4.sin_addr.s_addr = htonl(address.toIPv4Address()); + struct in_addr ia; + inet_pton(AF_INET, addressStr.constData(), &ia); + sa4.sin_addr = ia; } #ifndef QT_NO_IPV6 else { @@ -91,7 +94,9 @@ QHostInfo QHostInfoPrivate::fromName(const QString &hostName) saSize = sizeof(sa6); ::memset(&sa6, 0, sizeof(sa6)); sa6.sin6_family = AF_INET6; - ::memcpy(sa6.sin6_addr.s6_addr, address.toIPv6Address().c, sizeof(sa6.sin6_addr.s6_addr)); + struct in6_addr ia6; + inet_pton(AF_INET6, addressStr.constData(), &ia6); + sa6.sin6_addr = ia6; } #endif @@ -184,16 +189,16 @@ QHostInfo QHostInfoPrivate::fromName(const QString &hostName) qDebug() << "getaddrinfo node: flags:" << node->ai_flags << "family:" << node->ai_family << "ai_socktype:" << node->ai_socktype << "ai_protocol:" << node->ai_protocol << "ai_addrlen:" << node->ai_addrlen; #endif if (node->ai_family == AF_INET) { - QHostAddress addr(ntohl(((sockaddr_in *) node->ai_addr)->sin_addr.s_addr)); + QHostAddress addr(node->ai_addr); if (!addresses.contains(addr)) addresses.append(addr); } #ifndef QT_NO_IPV6 else if (node->ai_family == AF_INET6) { sockaddr_in6 *sa6 = (sockaddr_in6 *) node->ai_addr; - QHostAddress addr(sa6->sin6_addr.s6_addr); + QHostAddress addr(node->ai_addr); if (sa6->sin6_scope_id) - addr.setScopeId(QString::number(sa6->sin6_scope_id)); + addr.setScopeId(QByteArray::number(sa6->sin6_scope_id)); if (!addresses.contains(addr)) addresses.append(addr); } diff --git a/src/network/kernel/qnetworkinterface.cpp b/src/network/kernel/qnetworkinterface.cpp index fb4fd45f5..6a8942162 100644 --- a/src/network/kernel/qnetworkinterface.cpp +++ b/src/network/kernel/qnetworkinterface.cpp @@ -30,35 +30,6 @@ QT_BEGIN_NAMESPACE -static QList postProcess(QList list) -{ - // Some platforms report a netmask but don't report a broadcast address - // Go through all available addresses and calculate the broadcast address - // from the IP and the netmask - // - // This is an IPv4-only thing -- IPv6 has no concept of broadcasts - // The math is: - // broadcast = IP | ~netmask - - QList::Iterator it = list.begin(); - for ( ; it != list.end(); ++it) { - QList::Iterator addr_it = (*it)->addressEntries.begin(); - const QList::Iterator addr_end = (*it)->addressEntries.end(); - for ( ; addr_it != addr_end; ++addr_it) { - if (addr_it->ip().protocol() != QAbstractSocket::IPv4Protocol) - continue; - - if (!addr_it->netmask().isNull() && addr_it->broadcast().isNull()) { - QHostAddress bcast = addr_it->ip(); - bcast = QHostAddress(bcast.toIPv4Address() | ~addr_it->netmask().toIPv4Address()); - addr_it->setBroadcast(bcast); - } - } - } - - return list; -} - /*! \class QNetworkAddressEntry \brief The QNetworkAddressEntry class stores one IP address @@ -138,15 +109,6 @@ QHostAddress QNetworkAddressEntry::ip() const return d->address; } -/*! - Sets the IP address the QNetworkAddressEntry object contains to \a - newIp. -*/ -void QNetworkAddressEntry::setIp(const QHostAddress &newIp) -{ - d->address = newIp; -} - /*! Returns the netmask associated with the IP address. The netmask is expressed in the form of an IP address, such as @@ -165,58 +127,6 @@ QHostAddress QNetworkAddressEntry::netmask() const return d->netmask; } -/*! - Sets the netmask that this QNetworkAddressEntry object contains to - \a newNetmask. Setting the netmask also sets the prefix length to - match the new netmask. - - \sa setPrefixLength() -*/ -void QNetworkAddressEntry::setNetmask(const QHostAddress &newNetmask) -{ - if (newNetmask.protocol() != ip().protocol()) { - d->netmask = QNetmaskAddress(); - return; - } - - d->netmask.setAddress(newNetmask); -} - -/*! - \since 4.5 - Returns the prefix length of this IP address. The prefix length - matches the number of bits set to 1 in the netmask (see - netmask()). For IPv4 addresses, the value is between 0 and 32. For - IPv6 addresses, it's contained between 0 and 128 and is the - preferred form of representing addresses. - - This function returns -1 if the prefix length could not be - determined (i.e., netmask() returns a null QHostAddress()). - - \sa netmask() -*/ -int QNetworkAddressEntry::prefixLength() const -{ - return d->netmask.prefixLength(); -} - -/*! - \since 4.5 - Sets the prefix length of this IP address to \a length. The value - of \a length must be valid for this type of IP address: between 0 - and 32 for IPv4 addresses, between 0 and 128 for IPv6 - addresses. Setting to any invalid value is equivalent to setting - to -1, which means "no prefix length". - - Setting the prefix length also sets the netmask (see netmask()). - - \sa setNetmask() -*/ -void QNetworkAddressEntry::setPrefixLength(int length) -{ - d->netmask.setPrefixLength(d->address.protocol(), length); -} - /*! Returns the broadcast address associated with the IPv4 address and netmask. It can usually be derived from those two by @@ -235,15 +145,6 @@ QHostAddress QNetworkAddressEntry::broadcast() const return d->broadcast; } -/*! - Sets the broadcast IP address of this QNetworkAddressEntry object - to \a newBroadcast. -*/ -void QNetworkAddressEntry::setBroadcast(const QHostAddress &newBroadcast) -{ - d->broadcast = newBroadcast; -} - /*! \class QNetworkInterface \brief The QNetworkInterface class provides a listing of the host's IP @@ -446,8 +347,7 @@ QNetworkInterface QNetworkInterface::interfaceFromIndex(int index) QList QNetworkInterface::allInterfaces() { QList result; - QList list = postProcess(QNetworkInterfacePrivate::scan()); - foreach (QNetworkInterfacePrivate* priv, list) { + foreach (QNetworkInterfacePrivate* priv, QNetworkInterfacePrivate::scan()) { QNetworkInterface iface; iface.d = QSharedDataPointer(priv); result << iface; diff --git a/src/network/kernel/qnetworkinterface.h b/src/network/kernel/qnetworkinterface.h index e938a7588..c02acafbc 100644 --- a/src/network/kernel/qnetworkinterface.h +++ b/src/network/kernel/qnetworkinterface.h @@ -33,6 +33,8 @@ QT_BEGIN_NAMESPACE template class QList; class QNetworkAddressEntryPrivate; +class QNetworkInterfacePrivate; + class Q_NETWORK_EXPORT QNetworkAddressEntry { public: @@ -45,21 +47,15 @@ public: { return !(*this == other); } QHostAddress ip() const; - void setIp(const QHostAddress &newIp); - QHostAddress netmask() const; - void setNetmask(const QHostAddress &newNetmask); - int prefixLength() const; - void setPrefixLength(int length); - QHostAddress broadcast() const; - void setBroadcast(const QHostAddress &newBroadcast); private: + friend QNetworkInterfacePrivate; + QScopedPointer d; }; -class QNetworkInterfacePrivate; class Q_NETWORK_EXPORT QNetworkInterface { public: diff --git a/src/network/kernel/qnetworkinterface_p.h b/src/network/kernel/qnetworkinterface_p.h index 567e23d57..ce169faf9 100644 --- a/src/network/kernel/qnetworkinterface_p.h +++ b/src/network/kernel/qnetworkinterface_p.h @@ -48,7 +48,7 @@ class QNetworkAddressEntryPrivate { public: QHostAddress address; - QNetmaskAddress netmask; + QHostAddress netmask; QHostAddress broadcast; }; diff --git a/src/network/kernel/qnetworkinterface_unix.cpp b/src/network/kernel/qnetworkinterface_unix.cpp index 2600d092c..94422c4ff 100644 --- a/src/network/kernel/qnetworkinterface_unix.cpp +++ b/src/network/kernel/qnetworkinterface_unix.cpp @@ -64,19 +64,19 @@ static QHostAddress addressFromSockaddr(sockaddr *sa) return address; if (sa->sa_family == AF_INET) - address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr)); + address.setAddress(sa); #ifndef QT_NO_IPV6 else if (sa->sa_family == AF_INET6) { - address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr); + address.setAddress(sa); int scope = ((sockaddr_in6 *)sa)->sin6_scope_id; if (scope) { #ifndef QT_NO_IPV6IFNAME QSTACKARRAY(char, scopeid, IFNAMSIZ); if (::if_indextoname(scope, scopeid) != 0) { - address.setScopeId(QString::fromLatin1(scopeid)); + address.setScopeId(QByteArray(scopeid)); } else #endif - address.setScopeId(QString::number(scope)); + address.setScopeId(QByteArray::number(scope)); } } #endif @@ -127,14 +127,14 @@ QList QNetworkInterfacePrivate::scan() } QNetworkAddressEntry entry; - entry.setIp(addressFromSockaddr(ifiter->ifa_addr)); + entry.d->address = addressFromSockaddr(ifiter->ifa_addr); if (entry.ip().isNull()) // could not parse the address continue; - entry.setNetmask(addressFromSockaddr(ifiter->ifa_netmask)); + entry.d->netmask = addressFromSockaddr(ifiter->ifa_netmask); if (iface->flags & QNetworkInterface::CanBroadcast) - entry.setBroadcast(addressFromSockaddr(ifiter->ifa_broadaddr)); + entry.d->broadcast = addressFromSockaddr(ifiter->ifa_broadaddr); iface->addressEntries << entry; diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index 52a76c9ca..cc7f16dfe 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -1080,7 +1080,7 @@ void QAbstractSocket::connectToHost(const QString &hostName, quint16 port, emit stateChanged(d->state); QHostAddress temp; - if (temp.setAddress(hostName)) { + if (temp.setAddress(hostName.toLatin1())) { QHostInfo info; info.d->err = QHostInfo::NoError; info.d->errorStr = QCoreApplication::translate("QHostInfo", "Unknown error"); diff --git a/src/network/socket/qabstractsocketengine_unix.cpp b/src/network/socket/qabstractsocketengine_unix.cpp index af8cd4099..0d353d643 100644 --- a/src/network/socket/qabstractsocketengine_unix.cpp +++ b/src/network/socket/qabstractsocketengine_unix.cpp @@ -56,16 +56,16 @@ static inline void qt_socket_getPortAndAddress(const struct sockaddr_storage *ss { #if !defined(QT_NO_IPV6) if (ss->ss_family == AF_INET6) { - struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)ss; + const struct sockaddr_in6 *si6 = (const struct sockaddr_in6 *)ss; if (addr) { - addr->setAddress(si6->sin6_addr.s6_addr); + addr->setAddress((const struct sockaddr *) ss); #ifndef QT_NO_IPV6IFNAME QSTACKARRAY(char, scopeid, IFNAMSIZ); if (::if_indextoname(si6->sin6_scope_id, scopeid)) { - addr->setScopeId(QString::fromLatin1(scopeid)); + addr->setScopeId(QByteArray(scopeid)); } else #endif - addr->setScopeId(QString::number(si6->sin6_scope_id)); + addr->setScopeId(QByteArray::number(si6->sin6_scope_id)); } if (port) *port = ntohs(si6->sin6_port); @@ -73,11 +73,11 @@ static inline void qt_socket_getPortAndAddress(const struct sockaddr_storage *ss } #endif - struct sockaddr_in *si4 = (struct sockaddr_in *)ss; + const struct sockaddr_in *si4 = (const struct sockaddr_in *)ss; if (port) *port = ntohs(si4->sin_port); if (addr) { - addr->setAddress(ntohl(si4->sin_addr.s_addr)); + addr->setAddress((const struct sockaddr *) ss); } } @@ -282,6 +282,8 @@ bool QAbstractSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint qDebug("QAbstractSocketEnginePrivate::nativeConnect() : %d ", socketDescriptor); #endif + const QByteArray addrStr = addr.toString(); + struct sockaddr_in sockAddrIPv4; struct sockaddr *sockAddrPtr = 0; QT_SOCKLEN_T sockAddrSize = 0; @@ -295,14 +297,15 @@ bool QAbstractSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint sockAddrIPv6.sin6_port = htons(port); QString scopeid = addr.scopeId(); - bool ok; + bool ok = false; 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)); + struct in6_addr inAddrIPv6; + inet_pton(AF_INET6, addrStr.constData(), &inAddrIPv6); + sockAddrIPv6.sin6_addr = inAddrIPv6; sockAddrSize = sizeof(sockAddrIPv6); sockAddrPtr = (struct sockaddr *) &sockAddrIPv6; @@ -312,7 +315,9 @@ bool QAbstractSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); sockAddrIPv4.sin_family = AF_INET; sockAddrIPv4.sin_port = htons(port); - sockAddrIPv4.sin_addr.s_addr = htonl(addr.toIPv4Address()); + struct in_addr inAddrIPv4; + inet_pton(AF_INET, addrStr.constData(), &inAddrIPv4); + sockAddrIPv4.sin_addr = inAddrIPv4; sockAddrSize = sizeof(sockAddrIPv4); sockAddrPtr = (struct sockaddr *) &sockAddrIPv4; @@ -387,6 +392,8 @@ bool QAbstractSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint bool QAbstractSocketEnginePrivate::nativeBind(const QHostAddress &address, quint16 port) { + const QByteArray addrStr = address.toString(); + struct sockaddr_in sockAddrIPv4; struct sockaddr *sockAddrPtr = 0; QT_SOCKLEN_T sockAddrSize = 0; @@ -395,16 +402,18 @@ bool QAbstractSocketEnginePrivate::nativeBind(const QHostAddress &address, quint struct sockaddr_in6 sockAddrIPv6; if (address.protocol() == QAbstractSocket::IPv6Protocol) { + const QByteArray scopeid = address.scopeId(); memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(port); #ifndef QT_NO_IPV6IFNAME - sockAddrIPv6.sin6_scope_id = ::if_nametoindex(address.scopeId().toLatin1().data()); + sockAddrIPv6.sin6_scope_id = ::if_nametoindex(scopeid.constData()); #else - sockAddrIPv6.sin6_scope_id = address.scopeId().toInt(); + sockAddrIPv6.sin6_scope_id = scopeid.toInt(); #endif - Q_IPV6ADDR tmp = address.toIPv6Address(); - memcpy(&sockAddrIPv6.sin6_addr.s6_addr, &tmp, sizeof(tmp)); + struct in6_addr inAddrIPv6; + inet_pton(AF_INET6, addrStr.constData(), &inAddrIPv6); + sockAddrIPv6.sin6_addr = inAddrIPv6; sockAddrSize = sizeof(sockAddrIPv6); sockAddrPtr = (struct sockaddr *) &sockAddrIPv6; } else @@ -413,7 +422,9 @@ bool QAbstractSocketEnginePrivate::nativeBind(const QHostAddress &address, quint memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); sockAddrIPv4.sin_family = AF_INET; sockAddrIPv4.sin_port = htons(port); - sockAddrIPv4.sin_addr.s_addr = htonl(address.toIPv4Address()); + struct in_addr inAddrIPv4; + inet_pton(AF_INET, addrStr.constData(), &inAddrIPv4); + sockAddrIPv4.sin_addr = inAddrIPv4; sockAddrSize = sizeof(sockAddrIPv4); sockAddrPtr = (struct sockaddr *) &sockAddrIPv4; } @@ -509,6 +520,8 @@ static bool multicastMembershipHelper(QAbstractSocketEnginePrivate *d, const QHostAddress &groupAddress, const QNetworkInterface &interface) { + const QByteArray groupAddressStr = groupAddress.toString(); + int level = 0; int sockOpt = 0; void *sockArg; @@ -524,9 +537,10 @@ static bool multicastMembershipHelper(QAbstractSocketEnginePrivate *d, sockArg = &mreq6; sockArgSize = sizeof(mreq6); memset(&mreq6, 0, sizeof(mreq6)); - Q_IPV6ADDR ip6 = groupAddress.toIPv6Address(); - memcpy(&mreq6.ipv6mr_multiaddr, &ip6, sizeof(ip6)); mreq6.ipv6mr_interface = interface.index(); + struct in6_addr ia6; + inet_pton(AF_INET6, groupAddressStr.constData(), &ia6); + mreq6.ipv6mr_multiaddr = ia6; } else #endif if (groupAddress.protocol() == QAbstractSocket::IPv4Protocol) { @@ -535,13 +549,18 @@ static bool multicastMembershipHelper(QAbstractSocketEnginePrivate *d, sockArg = &mreq4; sockArgSize = sizeof(mreq4); memset(&mreq4, 0, sizeof(mreq4)); - mreq4.imr_multiaddr.s_addr = htonl(groupAddress.toIPv4Address()); + struct in_addr ia; + inet_pton(AF_INET, groupAddressStr.constData(), &ia); + mreq4.imr_multiaddr = ia; if (interface.isValid()) { QList addressEntries = interface.addressEntries(); if (!addressEntries.isEmpty()) { QHostAddress firstIP = addressEntries.first().ip(); - mreq4.imr_interface.s_addr = htonl(firstIP.toIPv4Address()); + const QByteArray firstIPStr = firstIP.toString(); + struct in_addr ia; + inet_pton(AF_INET, firstIPStr.constData(), &ia); + mreq4.imr_interface = ia; } else { d->setError(QAbstractSocket::NetworkError, QAbstractSocketEnginePrivate::NetworkUnreachableErrorString); @@ -623,7 +642,9 @@ QNetworkInterface QAbstractSocketEnginePrivate::nativeMulticastInterface() const if (::getsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, &v, &sizeofv) == -1) return QNetworkInterface(); if (v.s_addr != 0 && sizeofv >= sizeof(v)) { - QHostAddress ipv4(ntohl(v.s_addr)); + QSTACKARRAY(char, ntopbuffer, INET_ADDRSTRLEN + 1); + inet_ntop(AF_INET, &v.s_addr, ntopbuffer, INET_ADDRSTRLEN); + QHostAddress ipv4(ntopbuffer); QList ifaces = QNetworkInterface::allInterfaces(); for (int i = 0; i < ifaces.count(); ++i) { const QNetworkInterface &iface = ifaces.at(i); @@ -654,7 +675,8 @@ bool QAbstractSocketEnginePrivate::nativeSetMulticastInterface(const QNetworkInt const QNetworkAddressEntry &entry = entries.at(i); const QHostAddress &ip = entry.ip(); if (ip.protocol() == QAbstractSocket::IPv4Protocol) { - v.s_addr = htonl(ip.toIPv4Address()); + const QByteArray ipStr = ip.toString(); + inet_pton(AF_INET, ipStr.constData(), &v); int r = ::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, &v, sizeof(v)); if (r != -1) return true; @@ -765,6 +787,8 @@ qint64 QAbstractSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 ma qint64 QAbstractSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 len, const QHostAddress &host, quint16 port) { + const QByteArray hostStr = host.toString(); + struct sockaddr_in sockAddrIPv4; struct sockaddr *sockAddrPtr = 0; QT_SOCKLEN_T sockAddrSize = 0; @@ -776,8 +800,9 @@ qint64 QAbstractSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(port); - Q_IPV6ADDR tmp = host.toIPv6Address(); - memcpy(&sockAddrIPv6.sin6_addr.s6_addr, &tmp, sizeof(tmp)); + struct in6_addr ia6; + inet_pton(AF_INET6, hostStr.constData(), &ia6); + sockAddrIPv6.sin6_addr = ia6; QString scopeid = host.scopeId(); bool ok; sockAddrIPv6.sin6_scope_id = scopeid.toInt(&ok); @@ -793,7 +818,9 @@ qint64 QAbstractSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); sockAddrIPv4.sin_family = AF_INET; sockAddrIPv4.sin_port = htons(port); - sockAddrIPv4.sin_addr.s_addr = htonl(host.toIPv4Address()); + struct in_addr ia; + inet_pton(AF_INET, hostStr.constData(), &ia); + sockAddrIPv4.sin_addr = ia; sockAddrSize = sizeof(sockAddrIPv4); sockAddrPtr = (struct sockaddr *)&sockAddrIPv4; } diff --git a/src/tools/uic/cpp/cppwriteincludes.cpp b/src/tools/uic/cpp/cppwriteincludes.cpp index f5bbf7ff3..d9a393605 100644 --- a/src/tools/uic/cpp/cppwriteincludes.cpp +++ b/src/tools/uic/cpp/cppwriteincludes.cpp @@ -231,7 +231,6 @@ static const struct ClassTblData { { QLatin1String("QHostInfo"), QLatin1String("QtNetwork/qhostinfo.h") }, { QLatin1String("QHoverEvent"), QLatin1String("QtGui/qevent.h") }, { QLatin1String("QIODevice"), QLatin1String("QtCore/qiodevice.h") }, - { QLatin1String("QIPv6Address"), QLatin1String("QtNetwork/qhostaddress.h") }, { QLatin1String("QIcon"), QLatin1String("QtGui/qicon.h") }, { QLatin1String("QIconEngine"), QLatin1String("QtGui/qiconengine.h") }, { QLatin1String("QIdentityProxyModel"), QLatin1String("QtGui/qidentityproxymodel.h") }, diff --git a/tests/auto/qhostaddress/tst_qhostaddress.cpp b/tests/auto/qhostaddress/tst_qhostaddress.cpp index 8de69a43e..f2a98648c 100644 --- a/tests/auto/qhostaddress/tst_qhostaddress.cpp +++ b/tests/auto/qhostaddress/tst_qhostaddress.cpp @@ -62,10 +62,6 @@ private slots: void hashKey(); void streaming_data(); void streaming(); - void parseSubnet_data(); - void parseSubnet(); - void isInSubnet_data(); - void isInSubnet(); }; QT_BEGIN_NAMESPACE @@ -75,7 +71,7 @@ namespace QTest { { if (addr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol) return qstrdup(""); - return qstrdup(addr.toString().toLatin1()); + return qstrdup(addr.toString()); } } QT_END_NAMESPACE @@ -107,7 +103,7 @@ void tst_QHostAddress::constructor_QString_data() void tst_QHostAddress::constructor_QString() { - QFETCH(QString, address); + QFETCH(QByteArray, address); QFETCH(bool, ok); QFETCH(int, protocol); @@ -136,87 +132,88 @@ void tst_QHostAddress::constructor_QString() void tst_QHostAddress::setAddress_QString_data() { - QTest::addColumn("address"); + QTest::addColumn("address"); QTest::addColumn("ok"); - QTest::addColumn("resAddr"); + QTest::addColumn("resAddr"); QTest::addColumn("protocol"); // 4: IPv4, 6: IPv6, other: undefined //next we fill it with data - QTest::newRow("ip4_00") << QString("127.0.0.1") << (bool)true << QString("127.0.0.1") << 4; - QTest::newRow("ip4_01") << QString("255.3.2.1") << (bool)true << QString("255.3.2.1") << 4; - QTest::newRow("ip4_03") << QString(" 255.3.2.1") << (bool)true << QString("255.3.2.1") << 4; - QTest::newRow("ip4_04") << QString("255.3.2.1\r ") << (bool)true << QString("255.3.2.1") << 4; - QTest::newRow("ip4_05") << QString("0.0.0.0") << (bool)true << QString("0.0.0.0") << 4; + QTest::newRow("ip4_00") << QByteArray("127.0.0.1") << (bool)true << QByteArray("127.0.0.1") << 4; + QTest::newRow("ip4_01") << QByteArray("255.3.2.1") << (bool)true << QByteArray("255.3.2.1") << 4; + QTest::newRow("ip4_03") << QByteArray(" 255.3.2.1") << (bool)true << QByteArray("255.3.2.1") << 4; + QTest::newRow("ip4_04") << QByteArray("255.3.2.1\r ") << (bool)true << QByteArray("255.3.2.1") << 4; + QTest::newRow("ip4_05") << QByteArray("0.0.0.0") << (bool)true << QByteArray("0.0.0.0") << 4; // for the format of IPv6 addresses see also RFC 5952 - QTest::newRow("ip6_00") << QString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << (bool)true << QString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << 6; - QTest::newRow("ip6_01") << QString("1080:0000:0000:0000:0008:0800:200C:417A") << (bool)true << QString("1080::8:800:200C:417A") << 6; - QTest::newRow("ip6_02") << QString("1080:0:0:0:8:800:200C:417A") << (bool)true << QString("1080::8:800:200C:417A") << 6; - QTest::newRow("ip6_03") << QString("1080::8:800:200C:417A") << (bool)true << QString("1080::8:800:200C:417A") << 6; - QTest::newRow("ip6_04") << QString("FF01::43") << (bool)true << QString("FF01::43") << 6; - QTest::newRow("ip6_05") << QString("::1") << (bool)true << QString("::1") << 6; - QTest::newRow("ip6_06") << QString("1::") << (bool)true << QString("1::") << 6; - QTest::newRow("ip6_07") << QString("::") << (bool)true << QString("::") << 6; - QTest::newRow("ip6_08") << QString("0:0:0:0:0:0:13.1.68.3") << (bool)true << QString("::D01:4403") << 6; - QTest::newRow("ip6_09") << QString("::13.1.68.3") << (bool)true << QString("::D01:4403") << 6; - QTest::newRow("ip6_10") << QString("0:0:0:0:0:FFFF:129.144.52.38") << (bool)true << QString("::FFFF:8190:3426") << 6; - QTest::newRow("ip6_11") << QString("::FFFF:129.144.52.38") << (bool)true << QString("::FFFF:8190:3426") << 6; - QTest::newRow("ip6_12") << QString("1::FFFF:129.144.52.38") << (bool)true << QString("1::FFFF:8190:3426") << 6; - QTest::newRow("ip6_13") << QString("A:B::D:E") << (bool)true << QString("A:B::D:E") << 6; - QTest::newRow("ip6_14") << QString("1080:0:1:0:8:800:200C:417A") << (bool)true << QString("1080:0:1:0:8:800:200C:417A") << 6; - QTest::newRow("ip6_15") << QString("1080:0:1:0:8:800:200C:0") << (bool)true << QString("1080:0:1:0:8:800:200C:0") << 6; - QTest::newRow("ip6_16") << QString("1080:0:1:0:8:800:0:0") << (bool)true << QString("1080:0:1:0:8:800::") << 6; - QTest::newRow("ip6_17") << QString("1080:0:0:0:8:800:0:0") << (bool)true << QString("1080::8:800:0:0") << 6; - QTest::newRow("ip6_18") << QString("0:1:1:1:8:800:0:0") << (bool)true << QString("0:1:1:1:8:800::") << 6; - QTest::newRow("ip6_19") << QString("0:1:1:1:8:800:0:1") << (bool)true << QString("0:1:1:1:8:800:0:1") << 6; + QTest::newRow("ip6_00") << QByteArray("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << (bool)true << QByteArray("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << 6; + QTest::newRow("ip6_01") << QByteArray("1080:0000:0000:0000:0008:0800:200C:417A") << (bool)true << QByteArray("1080::8:800:200C:417A") << 6; + QTest::newRow("ip6_02") << QByteArray("1080:0:0:0:8:800:200C:417A") << (bool)true << QByteArray("1080::8:800:200C:417A") << 6; + QTest::newRow("ip6_03") << QByteArray("1080::8:800:200C:417A") << (bool)true << QByteArray("1080::8:800:200C:417A") << 6; + QTest::newRow("ip6_04") << QByteArray("FF01::43") << (bool)true << QByteArray("FF01::43") << 6; + QTest::newRow("ip6_05") << QByteArray("::1") << (bool)true << QByteArray("::1") << 6; + QTest::newRow("ip6_06") << QByteArray("1::") << (bool)true << QByteArray("1::") << 6; + QTest::newRow("ip6_07") << QByteArray("::") << (bool)true << QByteArray("::") << 6; + QTest::newRow("ip6_08") << QByteArray("0:0:0:0:0:0:13.1.68.3") << (bool)true << QByteArray("::D01:4403") << 6; + QTest::newRow("ip6_09") << QByteArray("::13.1.68.3") << (bool)true << QByteArray("::D01:4403") << 6; + QTest::newRow("ip6_10") << QByteArray("0:0:0:0:0:FFFF:129.144.52.38") << (bool)true << QByteArray("::FFFF:8190:3426") << 6; + QTest::newRow("ip6_11") << QByteArray("::FFFF:129.144.52.38") << (bool)true << QByteArray("::FFFF:8190:3426") << 6; + QTest::newRow("ip6_12") << QByteArray("1::FFFF:129.144.52.38") << (bool)true << QByteArray("1::FFFF:8190:3426") << 6; + QTest::newRow("ip6_13") << QByteArray("A:B::D:E") << (bool)true << QByteArray("A:B::D:E") << 6; + QTest::newRow("ip6_14") << QByteArray("1080:0:1:0:8:800:200C:417A") << (bool)true << QByteArray("1080:0:1:0:8:800:200C:417A") << 6; + QTest::newRow("ip6_15") << QByteArray("1080:0:1:0:8:800:200C:0") << (bool)true << QByteArray("1080:0:1:0:8:800:200C:0") << 6; + QTest::newRow("ip6_16") << QByteArray("1080:0:1:0:8:800:0:0") << (bool)true << QByteArray("1080:0:1:0:8:800::") << 6; + QTest::newRow("ip6_17") << QByteArray("1080:0:0:0:8:800:0:0") << (bool)true << QByteArray("1080::8:800:0:0") << 6; + QTest::newRow("ip6_18") << QByteArray("0:1:1:1:8:800:0:0") << (bool)true << QByteArray("0:1:1:1:8:800::") << 6; + QTest::newRow("ip6_19") << QByteArray("0:1:1:1:8:800:0:1") << (bool)true << QByteArray("0:1:1:1:8:800:0:1") << 6; - QTest::newRow("error_00") << QString("foobarcom") << (bool)false << QString() << 0; - QTest::newRow("error_01") << QString("foo.bar.com") << (bool)false << QString() << 0; - QTest::newRow("error_02") << QString("") << (bool)false << QString() << 0; - QTest::newRow("error_03") << QString() << (bool)false << QString() << 0; - QTest::newRow("error_04") << QString(" \t\r") << (bool)false << QString() << 0; + QTest::newRow("error_00") << QByteArray("foobarcom") << (bool)false << QByteArray() << 0; + QTest::newRow("error_01") << QByteArray("foo.bar.com") << (bool)false << QByteArray() << 0; + QTest::newRow("error_02") << QByteArray("") << (bool)false << QByteArray() << 0; + QTest::newRow("error_03") << QByteArray() << (bool)false << QByteArray() << 0; + QTest::newRow("error_04") << QByteArray(" \t\r") << (bool)false << QByteArray() << 0; - QTest::newRow("error_ip4_00") << QString("256.9.9.9") << (bool)false << QString() << 0; - QTest::newRow("error_ip4_01") << QString("-1.9.9.9") << (bool)false << QString() << 0; - QTest::newRow("error_ip4_02") << QString("123.0.0") << (bool)false << QString() << 0; - QTest::newRow("error_ip4_03") << QString("123.0.0.0.0") << (bool)false << QString() << 0; - QTest::newRow("error_ip4_04") << QString("255.2 3.2.1") << (bool)false << QString() << 0; + QTest::newRow("error_ip4_00") << QByteArray("256.9.9.9") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip4_01") << QByteArray("-1.9.9.9") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip4_02") << QByteArray("123.0.0") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip4_03") << QByteArray("123.0.0.0.0") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip4_04") << QByteArray("255.2 3.2.1") << (bool)false << QByteArray() << 0; - QTest::newRow("error_ip6_00") << QString(":") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_01") << QString(":::") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_02") << QString("::AAAA:") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_03") << QString(":AAAA::") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_04") << QString("FFFF:::129.144.52.38") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_05") << QString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:1234") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_06") << QString("129.144.52.38::") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_07") << QString("::129.144.52.38:129.144.52.38") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_08") << QString(":::129.144.52.38") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_09") << QString("1FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_10") << QString("::FFFFFFFF") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_11") << QString("::EFGH") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_12") << QString("ABCD:ABCD:ABCD") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_13") << QString("::ABCD:ABCD::") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_14") << QString("1::2::3") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_15") << QString("1:2:::") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_16") << QString(":::1:2") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_17") << QString("1:::2") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_18") << QString("FEDC::7654:3210:FEDC:BA98::3210") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_19") << QString("ABCD:ABCD:ABCD:1.2.3.4") << (bool)false << QString() << 0; - QTest::newRow("error_ip6_20") << QString("ABCD::ABCD::ABCD:1.2.3.4") << (bool)false << QString() << 0; + QTest::newRow("error_ip6_00") << QByteArray(":") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_01") << QByteArray(":::") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_02") << QByteArray("::AAAA:") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_03") << QByteArray(":AAAA::") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_04") << QByteArray("FFFF:::129.144.52.38") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_05") << QByteArray("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:1234") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_06") << QByteArray("129.144.52.38::") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_07") << QByteArray("::129.144.52.38:129.144.52.38") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_08") << QByteArray(":::129.144.52.38") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_09") << QByteArray("1FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_10") << QByteArray("::FFFFFFFF") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_11") << QByteArray("::EFGH") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_12") << QByteArray("ABCD:ABCD:ABCD") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_13") << QByteArray("::ABCD:ABCD::") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_14") << QByteArray("1::2::3") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_15") << QByteArray("1:2:::") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_16") << QByteArray(":::1:2") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_17") << QByteArray("1:::2") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_18") << QByteArray("FEDC::7654:3210:FEDC:BA98::3210") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_19") << QByteArray("ABCD:ABCD:ABCD:1.2.3.4") << (bool)false << QByteArray() << 0; + QTest::newRow("error_ip6_20") << QByteArray("ABCD::ABCD::ABCD:1.2.3.4") << (bool)false << QByteArray() << 0; } void tst_QHostAddress::setAddress_QString() { - QFETCH(QString, address); + QFETCH(QByteArray, address); QFETCH(bool, ok); + QFETCH(QByteArray, resAddr); QFETCH(int, protocol); QHostAddress hostAddr; QVERIFY(hostAddr.setAddress(address) == ok); if (ok) - QTEST(hostAddr.toString(), "resAddr"); + QTEST(hostAddr.toString(), resAddr); if ( protocol == 4 ) { QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv4Protocol || hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol ); @@ -232,44 +229,45 @@ void tst_QHostAddress::setAddress_QString() void tst_QHostAddress::specialAddresses_data() { - QTest::addColumn("text"); + QTest::addColumn("text"); QTest::addColumn("address"); QTest::addColumn("result"); - QTest::newRow("localhost_1") << QString("127.0.0.1") << (int)QHostAddress::LocalHost << true; - QTest::newRow("localhost_2") << QString("127.0.0.2") << (int)QHostAddress::LocalHost << false; - QTest::newRow("localhost_3") << QString("127.0.0.2") << (int)QHostAddress::LocalHostIPv6 << false; + QTest::newRow("localhost_1") << QByteArray("127.0.0.1") << (int)QHostAddress::LocalHost << true; + QTest::newRow("localhost_2") << QByteArray("127.0.0.2") << (int)QHostAddress::LocalHost << false; + QTest::newRow("localhost_3") << QByteArray("127.0.0.2") << (int)QHostAddress::LocalHostIPv6 << false; - QTest::newRow("localhost_ipv6_4") << QString("::1") << (int)QHostAddress::LocalHostIPv6 << true; - QTest::newRow("localhost_ipv6_5") << QString("::2") << (int)QHostAddress::LocalHostIPv6 << false; - QTest::newRow("localhost_ipv6_6") << QString("::1") << (int)QHostAddress::LocalHost << false; + QTest::newRow("localhost_ipv6_4") << QByteArray("::1") << (int)QHostAddress::LocalHostIPv6 << true; + QTest::newRow("localhost_ipv6_5") << QByteArray("::2") << (int)QHostAddress::LocalHostIPv6 << false; + QTest::newRow("localhost_ipv6_6") << QByteArray("::1") << (int)QHostAddress::LocalHost << false; - QTest::newRow("null_1") << QString("") << (int)QHostAddress::Null << true; - QTest::newRow("null_2") << QString("bjarne") << (int)QHostAddress::Null << true; + QTest::newRow("null_1") << QByteArray("") << (int)QHostAddress::Null << true; + QTest::newRow("null_2") << QByteArray("bjarne") << (int)QHostAddress::Null << true; - QTest::newRow("compare_from_null") << QString("") << (int)QHostAddress::Broadcast << false; + QTest::newRow("compare_from_null") << QByteArray("") << (int)QHostAddress::Broadcast << false; - QTest::newRow("broadcast_1") << QString("255.255.255.255") << (int)QHostAddress::Any << false; - QTest::newRow("broadcast_2") << QString("255.255.255.255") << (int)QHostAddress::Broadcast << true; + QTest::newRow("broadcast_1") << QByteArray("255.255.255.255") << (int)QHostAddress::Any << false; + QTest::newRow("broadcast_2") << QByteArray("255.255.255.255") << (int)QHostAddress::Broadcast << true; - QTest::newRow("any_ipv6") << QString("::") << (int)QHostAddress::AnyIPv6 << true; - QTest::newRow("any_ipv4") << QString("0.0.0.0") << (int)QHostAddress::Any << true; + QTest::newRow("any_ipv6") << QByteArray("::") << (int)QHostAddress::AnyIPv6 << true; + QTest::newRow("any_ipv4") << QByteArray("0.0.0.0") << (int)QHostAddress::Any << true; } void tst_QHostAddress::specialAddresses() { - QFETCH(QString, text); + QFETCH(QByteArray, text); QFETCH(int, address); QFETCH(bool, result); - QVERIFY((QHostAddress(text) == (QHostAddress::SpecialAddress)address) == result); + QHostAddress specialaddress = QHostAddress((QHostAddress::SpecialAddress)address); + QVERIFY((QHostAddress(text) == specialaddress) == result); QHostAddress setter; setter.setAddress(text); if (result) { - QVERIFY(setter == (QHostAddress::SpecialAddress) address); + QVERIFY(setter == specialaddress); } else { - QVERIFY(!((QHostAddress::SpecialAddress) address == setter)); + QVERIFY(!(specialaddress == setter)); } } @@ -308,9 +306,12 @@ void tst_QHostAddress::assignment() QCOMPARE(address, QHostAddress("::1")); QHostAddress addr("4.2.2.1"); + const QByteArray addStr = addr.toString(); sockaddr_in sockAddr; sockAddr.sin_family = AF_INET; - sockAddr.sin_addr.s_addr = htonl(addr.toIPv4Address()); + struct in_addr ia; + inet_pton(AF_INET, addStr.constData(), &ia); + sockAddr.sin_addr = ia; address.setAddress((sockaddr *)&sockAddr); QCOMPARE(address, addr); } @@ -318,17 +319,17 @@ void tst_QHostAddress::assignment() void tst_QHostAddress::scopeId() { QHostAddress address("fe80::2e0:4cff:fefb:662a%eth0"); - QCOMPARE(address.scopeId(), QString("eth0")); - QCOMPARE(address.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%eth0")); + QCOMPARE(address.scopeId(), QByteArray("eth0")); + QCOMPARE(address.toString().toLower(), QByteArray("fe80::2e0:4cff:fefb:662a%eth0")); QHostAddress address2("fe80::2e0:4cff:fefb:662a"); - QCOMPARE(address2.scopeId(), QString()); - address2.setScopeId(QString("en0")); - QCOMPARE(address2.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%en0")); + QCOMPARE(address2.scopeId(), QByteArray()); + address2.setScopeId(QByteArray("en0")); + QCOMPARE(address2.toString().toLower(), QByteArray("fe80::2e0:4cff:fefb:662a%en0")); address2 = address; - QCOMPARE(address2.scopeId(), QString("eth0")); - QCOMPARE(address2.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%eth0")); + QCOMPARE(address2.scopeId(), QByteArray("eth0")); + QCOMPARE(address2.toString().toLower(), QByteArray("fe80::2e0:4cff:fefb:662a%eth0")); } void tst_QHostAddress::hashKey() @@ -341,17 +342,16 @@ void tst_QHostAddress::streaming_data() { QTest::addColumn("address"); QTest::newRow("1") << QHostAddress(); - QTest::newRow("2") << QHostAddress(0xDEADBEEF); - QTest::newRow("3") << QHostAddress("127.128.129.130"); - QTest::newRow("4") << QHostAddress("1080:0000:0000:0000:0008:0800:200C:417A"); - QTest::newRow("5") << QHostAddress("fe80::2e0:4cff:fefb:662a%eth0"); - QTest::newRow("6") << QHostAddress(QHostAddress::Null); - QTest::newRow("7") << QHostAddress(QHostAddress::LocalHost); - QTest::newRow("8") << QHostAddress(QHostAddress::LocalHostIPv6); - QTest::newRow("9") << QHostAddress(QHostAddress::Broadcast); - QTest::newRow("10") << QHostAddress(QHostAddress::Any); - QTest::newRow("11") << QHostAddress(QHostAddress::AnyIPv6); - QTest::newRow("12") << QHostAddress("foo.bar.com"); + QTest::newRow("2") << QHostAddress("127.128.129.130"); + QTest::newRow("3") << QHostAddress("1080:0000:0000:0000:0008:0800:200C:417A"); + QTest::newRow("4") << QHostAddress("fe80::2e0:4cff:fefb:662a%eth0"); + QTest::newRow("5") << QHostAddress(QHostAddress::Null); + QTest::newRow("6") << QHostAddress(QHostAddress::LocalHost); + QTest::newRow("7") << QHostAddress(QHostAddress::LocalHostIPv6); + QTest::newRow("8") << QHostAddress(QHostAddress::Broadcast); + QTest::newRow("9") << QHostAddress(QHostAddress::Any); + QTest::newRow("10") << QHostAddress(QHostAddress::AnyIPv6); + QTest::newRow("11") << QHostAddress("foo.bar.com"); } void tst_QHostAddress::streaming() @@ -368,225 +368,6 @@ void tst_QHostAddress::streaming() QCOMPARE(address, address2); } -void tst_QHostAddress::parseSubnet_data() -{ - QTest::addColumn("subnet"); - QTest::addColumn("prefix"); - QTest::addColumn("prefixLength"); - - // invalid/error values - QTest::newRow("empty") << QString() << QHostAddress() << -1; - QTest::newRow("invalid_01") << "foobar" << QHostAddress() << -1; - QTest::newRow("invalid_02") << " " << QHostAddress() << -1; - QTest::newRow("invalid_03") << "1.2.3.a" << QHostAddress() << -1; - QTest::newRow("invalid_04") << "1.2.3.4.5" << QHostAddress() << -1; - QTest::newRow("invalid_05") << "1.2.3.4:80" << QHostAddress() << -1; - QTest::newRow("invalid_06") << "1.2.3.4/33" << QHostAddress() << -1; - QTest::newRow("invalid_07") << "1.2.3.4/-1" << QHostAddress() << -1; - QTest::newRow("invalid_08") << "1.2.3.4/256.0.0.0" << QHostAddress() << -1; - QTest::newRow("invalid_09") << "1.2.3.4/255.253.0.0" << QHostAddress() << -1; - QTest::newRow("invalid_10") << "1.2.3.4/255.0.0.255" << QHostAddress() << -1; - QTest::newRow("invalid_11") << "1.2.3.4." << QHostAddress() << -1; - QTest::newRow("invalid_20") << "ffff::/-1" << QHostAddress() << -1; - QTest::newRow("invalid_21") << "ffff::/129" << QHostAddress() << -1; - QTest::newRow("invalid_22") << "ffff::/255.255.0.0" << QHostAddress() << -1; - QTest::newRow("invalid_23") << "ffff::/ff00::" << QHostAddress() << -1; - - // correct IPv4 with netmask - QTest::newRow("netmask_0") << "0.0.0.0/0.0.0.0" << QHostAddress(QHostAddress::Any) << 0; - QTest::newRow("netmask_1") << "0.0.0.0/255.128.0.0" << QHostAddress(QHostAddress::Any) << 9; - QTest::newRow("netmask_2") << "0.0.0.0/255.192.0.0" << QHostAddress(QHostAddress::Any) << 10; - QTest::newRow("netmask_3") << "0.0.0.0/255.224.0.0" << QHostAddress(QHostAddress::Any) << 11; - QTest::newRow("netmask_4") << "0.0.0.0/255.240.0.0" << QHostAddress(QHostAddress::Any) << 12; - QTest::newRow("netmask_5") << "0.0.0.0/255.248.0.0" << QHostAddress(QHostAddress::Any) << 13; - QTest::newRow("netmask_6") << "0.0.0.0/255.252.0.0" << QHostAddress(QHostAddress::Any) << 14; - QTest::newRow("netmask_7") << "0.0.0.0/255.254.0.0" << QHostAddress(QHostAddress::Any) << 15; - QTest::newRow("netmask_8") << "0.0.0.0/255.255.0.0" << QHostAddress(QHostAddress::Any) << 16; - QTest::newRow("netmask_16") << "0.0.0.0/255.255.0.0" << QHostAddress(QHostAddress::Any) << 16; - QTest::newRow("netmask_24") << "0.0.0.0/255.255.255.0" << QHostAddress(QHostAddress::Any) << 24; - QTest::newRow("netmask_31") << "0.0.0.0/255.255.255.254" << QHostAddress(QHostAddress::Any) << 31; - QTest::newRow("netmask_32") << "0.0.0.0/255.255.255.255" << QHostAddress(QHostAddress::Any) << 32; - - // correct IPv4 with prefix - QTest::newRow("prefix_0") << "0.0.0.0/0" << QHostAddress(QHostAddress::Any) << 0; - QTest::newRow("prefix_1") << "0.0.0.0/1" << QHostAddress(QHostAddress::Any) << 1; - QTest::newRow("prefix_9") << "0.0.0.0/9" << QHostAddress(QHostAddress::Any) << 9; - QTest::newRow("prefix_31") << "0.0.0.0/31" << QHostAddress(QHostAddress::Any) << 31; - QTest::newRow("prefix_32") << "0.0.0.0/32" << QHostAddress(QHostAddress::Any) << 32; - - // correct IPv4 without prefix or netmask - QTest::newRow("classA") << "10" << QHostAddress("10.0.0.0") << 8; - QTest::newRow("classA+dot") << "10." << QHostAddress("10.0.0.0") << 8; - QTest::newRow("classB") << "172.16" << QHostAddress("172.16.0.0") << 16; - QTest::newRow("classB+dot") << "172.16." << QHostAddress("172.16.0.0") << 16; - QTest::newRow("classC") << "192.168.0" << QHostAddress("192.168.0.0") << 24; - QTest::newRow("classC+dot") << "192.168.0" << QHostAddress("192.168.0.0") << 24; - QTest::newRow("full-ipv4") << "192.168.0.1" << QHostAddress("192.168.0.1") << 32; - - // correct IPv6 with prefix - QTest::newRow("ipv6_01") << "::/0" << QHostAddress(QHostAddress::AnyIPv6) << 0; - QTest::newRow("ipv6_03") << "::/3" << QHostAddress(QHostAddress::AnyIPv6) << 3; - QTest::newRow("ipv6_16") << "::/16" << QHostAddress(QHostAddress::AnyIPv6) << 16; - QTest::newRow("ipv6_48") << "::/48" << QHostAddress(QHostAddress::AnyIPv6) << 48; - QTest::newRow("ipv6_127") << "::/127" << QHostAddress(QHostAddress::AnyIPv6) << 127; - QTest::newRow("ipv6_128") << "::/128" << QHostAddress(QHostAddress::AnyIPv6) << 128; - - // tail bit clearing: - QTest::newRow("clear_01") << "255.255.255.255/31" << QHostAddress("255.255.255.254") << 31; - QTest::newRow("clear_08") << "255.255.255.255/24" << QHostAddress("255.255.255.0") << 24; - QTest::newRow("clear_09") << "255.255.255.255/23" << QHostAddress("255.255.254.0") << 23; - QTest::newRow("clear_10") << "255.255.255.255/22" << QHostAddress("255.255.252.0") << 22; - QTest::newRow("clear_11") << "255.255.255.255/21" << QHostAddress("255.255.248.0") << 21; - QTest::newRow("clear_12") << "255.255.255.255/20" << QHostAddress("255.255.240.0") << 20; - QTest::newRow("clear_13") << "255.255.255.255/19" << QHostAddress("255.255.224.0") << 19; - QTest::newRow("clear_14") << "255.255.255.255/18" << QHostAddress("255.255.192.0") << 18; - QTest::newRow("clear_15") << "255.255.255.255/17" << QHostAddress("255.255.128.0") << 17; - QTest::newRow("clear_16") << "255.255.255.255/16" << QHostAddress("255.255.0.0") << 16; - QTest::newRow("clear_24") << "255.255.255.255/8" << QHostAddress("255.0.0.0") << 8; - QTest::newRow("clear_31") << "255.255.255.255/1" << QHostAddress("128.0.0.0") << 1; - QTest::newRow("clear_32") << "255.255.255.255/0" << QHostAddress("0.0.0.0") << 0; - - // same for IPv6: - QTest::newRow("ipv6_clear_01") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/127" - << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe") - << 127; - QTest::newRow("ipv6_clear_07") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/121" - << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff80") - << 121; - QTest::newRow("ipv6_clear_08") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/120" - << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00") - << 120; - QTest::newRow("ipv6_clear_16") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/112" - << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0") - << 112; - QTest::newRow("ipv6_clear_80") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/48" - << QHostAddress("ffff:ffff:ffff::") - << 48; - QTest::newRow("ipv6_clear_81") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/47" - << QHostAddress("ffff:ffff:fffe::") - << 47; - QTest::newRow("ipv6_clear_82") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/46" - << QHostAddress("ffff:ffff:fffc::") - << 46; - QTest::newRow("ipv6_clear_83") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/45" - << QHostAddress("ffff:ffff:fff8::") - << 45; - QTest::newRow("ipv6_clear_84") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/44" - << QHostAddress("ffff:ffff:fff0::") - << 44; - QTest::newRow("ipv6_clear_85") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/43" - << QHostAddress("ffff:ffff:ffe0::") - << 43; - QTest::newRow("ipv6_clear_86") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/42" - << QHostAddress("ffff:ffff:ffc0::") - << 42; - QTest::newRow("ipv6_clear_87") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/41" - << QHostAddress("ffff:ffff:ff80::") - << 41; - QTest::newRow("ipv6_clear_88") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/40" - << QHostAddress("ffff:ffff:ff00::") - << 40; - QTest::newRow("ipv6_clear_125") << "3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/3" - << QHostAddress("2000::") - << 3; - QTest::newRow("ipv6_clear_127") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/1" - << QHostAddress("8000::") - << 1; - QTest::newRow("ipv6_clear_128") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0" - << QHostAddress(QHostAddress::AnyIPv6) - << 0; -} - -void tst_QHostAddress::parseSubnet() -{ - QFETCH(QString, subnet); - QFETCH(QHostAddress, prefix); - QFETCH(int, prefixLength); - - QPair result = QHostAddress::parseSubnet(subnet); - QCOMPARE(result.first, prefix); - QCOMPARE(result.second, prefixLength); -} - -void tst_QHostAddress::isInSubnet_data() -{ - QTest::addColumn("address"); - QTest::addColumn("prefix"); - QTest::addColumn("prefixLength"); - QTest::addColumn("result"); - - // invalid QHostAddresses are never in any subnets - QTest::newRow("invalid_01") << QHostAddress() << QHostAddress() << 32 << false; - QTest::newRow("invalid_02") << QHostAddress() << QHostAddress(QHostAddress::Any) << 32 << false; - QTest::newRow("invalid_03") << QHostAddress() << QHostAddress(QHostAddress::Any) << 8 << false; - QTest::newRow("invalid_04") << QHostAddress() << QHostAddress(QHostAddress::Any) << 0 << false; - QTest::newRow("invalid_05") << QHostAddress() << QHostAddress("255.255.255.0") << 24 << false; - QTest::newRow("invalid_06") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 0 << false; - QTest::newRow("invalid_07") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 32 << false; - QTest::newRow("invalid_08") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 128<< false; - - // and no host address can be in a subnet whose prefix is invalid - QTest::newRow("invalid_20") << QHostAddress(QHostAddress::Any) << QHostAddress() << 16 << false; - QTest::newRow("invalid_21") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress() << 16 << false; - QTest::newRow("invalid_22") << QHostAddress(QHostAddress::LocalHost) << QHostAddress() << 16 << false; - QTest::newRow("invalid_23") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress() << 16 << false; - - // negative netmasks don't make sense: - QTest::newRow("invalid_30") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::Any) << -1 << false; - QTest::newRow("invalid_31") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv6) << -1 << false; - - // we don't support IPv4 belonging in an IPv6 netmask and vice-versa - QTest::newRow("v4-in-v6") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::AnyIPv6) << 0 << false; - QTest::newRow("v6-in-v4") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::Any) << 0 << false; - QTest::newRow("v4-in-v6mapped") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:255.0.0.0") << 113 << false; - QTest::newRow("v4-in-v6mapped2") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("::ffff:255.0.0.0") << 113 << false; - - // IPv4 correct ones - QTest::newRow("netmask_0") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::Any) << 0 << true; - QTest::newRow("netmask_0bis") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("255.255.0.0") << 0 << true; - QTest::newRow("netmask_0ter") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("1.2.3.4") << 0 << true; - QTest::newRow("netmask_1") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::Any) << 1 << true; - QTest::newRow("~netmask_1") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("128.0.0.0") << 1 << false; - QTest::newRow("netmask_1bis") << QHostAddress("224.0.0.1") << QHostAddress("128.0.0.0") << 1 << true; - QTest::newRow("~netmask_1bis") << QHostAddress("224.0.0.1") << QHostAddress("0.0.0.0") << 1 << false; - QTest::newRow("netmask_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("127.0.0.0") << 8 << true; - QTest::newRow("~netmask_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("126.0.0.0") << 8 << false; - QTest::newRow("netmask_15") << QHostAddress("10.0.1.255") << QHostAddress("10.0.0.0") << 15 << true; - QTest::newRow("netmask_16") << QHostAddress("172.16.0.1") << QHostAddress("172.16.0.0") << 16 << true; - - // the address is always in the subnet containing its address, regardless of length: - QTest::newRow("same_01") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 1 << true; - QTest::newRow("same_07") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 7 << true; - QTest::newRow("same_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 8 << true; - QTest::newRow("same_24") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 23 << true; - QTest::newRow("same_31") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 31 << true; - QTest::newRow("same_32") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 32 << true; - - // IPv6 correct ones: - QTest::newRow("ipv6_netmask_0") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv6) << 0 << true; - QTest::newRow("ipv6_netmask_0bis") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::LocalHostIPv6) << 0 << true; - QTest::newRow("ipv6_netmask_0ter") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress("ffff::") << 0 << true; - QTest::newRow("ipv6_netmask_1") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv6) << 1 << true; - QTest::newRow("ipv6_netmask_1bis") << QHostAddress("fec0::1") << QHostAddress("8000::") << 1 << true; - QTest::newRow("~ipv6_netmask_1") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress("8000::") << 1 << false; - QTest::newRow("~ipv6_netmask_1bis") << QHostAddress("fec0::1") << QHostAddress("::") << 1 << false; - QTest::newRow("ipv6_netmask_47") << QHostAddress("2:3:5::1") << QHostAddress("2:3:4::") << 47 << true; - QTest::newRow("ipv6_netmask_48") << QHostAddress("2:3:4::1") << QHostAddress("2:3:4::") << 48 << true; - QTest::newRow("~ipv6_netmask_48") << QHostAddress("2:3:5::1") << QHostAddress("2:3:4::") << 48 << false; - QTest::newRow("ipv6_netmask_127") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::") << 127 << true; - QTest::newRow("ipv6_netmask_128") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::1") << 128 << true; - QTest::newRow("~ipv6_netmask_128") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::0") << 128 << false; -} - -void tst_QHostAddress::isInSubnet() -{ - QFETCH(QHostAddress, address); - QFETCH(QHostAddress, prefix); - QFETCH(int, prefixLength); - - QTEST(address.isInSubnet(prefix, prefixLength), "result"); -} - QTEST_MAIN(tst_QHostAddress) #include "moc_tst_qhostaddress.cpp" diff --git a/tests/auto/qhostinfo/tst_qhostinfo.cpp b/tests/auto/qhostinfo/tst_qhostinfo.cpp index 77076bb3a..95f45a7f0 100644 --- a/tests/auto/qhostinfo/tst_qhostinfo.cpp +++ b/tests/auto/qhostinfo/tst_qhostinfo.cpp @@ -194,22 +194,22 @@ void tst_QHostInfo::lookupIPv6() void tst_QHostInfo::reverseLookup_data() { - QTest::addColumn("address"); + QTest::addColumn("address"); QTest::addColumn("hostNames"); QTest::addColumn("err"); - QTest::newRow("qt-project.org") << QString("87.238.53.172") << QStringList(QString("tufsla.qtproject.c.bitbit.net")) << 0; + QTest::newRow("qt-project.org") << QByteArray("87.238.53.172") << QStringList(QString("tufsla.qtproject.c.bitbit.net")) << 0; - QTest::newRow("gitorious.org") << QString("87.238.52.168") << QStringList(QString("gitorious.org")) << 0; + QTest::newRow("gitorious.org") << QByteArray("87.238.52.168") << QStringList(QString("gitorious.org")) << 0; if (!ipv6Available) - QTest::newRow("bogus-name") << QString("1.2..4") << QStringList() << 1; + QTest::newRow("bogus-name") << QByteArray("1.2..4") << QStringList() << 1; else - QTest::newRow("bogus-name") << QString("1::2::::4") << QStringList() << 1; + QTest::newRow("bogus-name") << QByteArray("1::2::::4") << QStringList() << 1; } void tst_QHostInfo::reverseLookup() { - QFETCH(QString, address); + QFETCH(QByteArray, address); QFETCH(QStringList, hostNames); QFETCH(int, err); @@ -219,7 +219,7 @@ void tst_QHostInfo::reverseLookup() QVERIFY(hostNames.contains(info.hostName())); QCOMPARE(info.addresses().first(), QHostAddress(address)); } else { - QCOMPARE(info.hostName(), address); + QCOMPARE(info.hostName().toLatin1(), address); QCOMPARE(info.error(), QHostInfo::HostNotFound); }