| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/kernel/qhostaddress.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
| 5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 6 | ** | - | ||||||||||||
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
| 8 | ** | - | ||||||||||||
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
| 10 | ** Commercial License Usage | - | ||||||||||||
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
| 17 | ** | - | ||||||||||||
| 18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
| 22 | ** packaging of this file. Please review the following information to | - | ||||||||||||
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** GNU General Public License Usage | - | ||||||||||||
| 27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
| 29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 36 | ** | - | ||||||||||||
| 37 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 38 | ** | - | ||||||||||||
| 39 | ****************************************************************************/ | - | ||||||||||||
| 40 | - | |||||||||||||
| 41 | #include "qhostaddress.h" | - | ||||||||||||
| 42 | #include "qhostaddress_p.h" | - | ||||||||||||
| 43 | #include "private/qipaddress_p.h" | - | ||||||||||||
| 44 | #include "qdebug.h" | - | ||||||||||||
| 45 | #if defined(Q_OS_WIN) | - | ||||||||||||
| 46 | # include <winsock2.h> | - | ||||||||||||
| 47 | #else | - | ||||||||||||
| 48 | # include <netinet/in.h> | - | ||||||||||||
| 49 | #endif | - | ||||||||||||
| 50 | #include "qplatformdefs.h" | - | ||||||||||||
| 51 | #include "qstringlist.h" | - | ||||||||||||
| 52 | #include "qendian.h" | - | ||||||||||||
| 53 | #ifndef QT_NO_DATASTREAM | - | ||||||||||||
| 54 | #include <qdatastream.h> | - | ||||||||||||
| 55 | #endif | - | ||||||||||||
| 56 | #ifdef __SSE2__ | - | ||||||||||||
| 57 | # include <private/qsimd_p.h> | - | ||||||||||||
| 58 | #endif | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | #ifdef QT_LINUXBASE | - | ||||||||||||
| 61 | # include <arpa/inet.h> | - | ||||||||||||
| 62 | #endif | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 65 | - | |||||||||||||
| 66 | #define QT_ENSURE_PARSED(a) \ | - | ||||||||||||
| 67 | do { \ | - | ||||||||||||
| 68 | if (!(a)->d->isParsed) \ | - | ||||||||||||
| 69 | (a)->d->parse(); \ | - | ||||||||||||
| 70 | } while (0) | - | ||||||||||||
| 71 | - | |||||||||||||
| 72 | #ifdef Q_OS_WIN | - | ||||||||||||
| 73 | // sockaddr_in6 size changed between old and new SDK | - | ||||||||||||
| 74 | // Only the new version is the correct one, so always | - | ||||||||||||
| 75 | // use this structure. | - | ||||||||||||
| 76 | #if defined(Q_OS_WINCE) || defined(Q_OS_WINRT) | - | ||||||||||||
| 77 | # if !defined(u_char) | - | ||||||||||||
| 78 | # define u_char unsigned char | - | ||||||||||||
| 79 | # endif | - | ||||||||||||
| 80 | # if !defined(u_short) | - | ||||||||||||
| 81 | # define u_short unsigned short | - | ||||||||||||
| 82 | # endif | - | ||||||||||||
| 83 | # if !defined(u_long) | - | ||||||||||||
| 84 | # define u_long unsigned long | - | ||||||||||||
| 85 | # endif | - | ||||||||||||
| 86 | #endif | - | ||||||||||||
| 87 | struct qt_in6_addr { | - | ||||||||||||
| 88 | u_char qt_s6_addr[16]; | - | ||||||||||||
| 89 | }; | - | ||||||||||||
| 90 | typedef struct { | - | ||||||||||||
| 91 | short sin6_family; /* AF_INET6 */ | - | ||||||||||||
| 92 | u_short sin6_port; /* Transport level port number */ | - | ||||||||||||
| 93 | u_long sin6_flowinfo; /* IPv6 flow information */ | - | ||||||||||||
| 94 | struct qt_in6_addr sin6_addr; /* IPv6 address */ | - | ||||||||||||
| 95 | u_long sin6_scope_id; /* set of interfaces for a scope */ | - | ||||||||||||
| 96 | } qt_sockaddr_in6; | - | ||||||||||||
| 97 | #else | - | ||||||||||||
| 98 | #define qt_sockaddr_in6 sockaddr_in6 | - | ||||||||||||
| 99 | #define qt_s6_addr s6_addr | - | ||||||||||||
| 100 | #endif | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | - | |||||||||||||
| 103 | class QHostAddressPrivate | - | ||||||||||||
| 104 | { | - | ||||||||||||
| 105 | public: | - | ||||||||||||
| 106 | QHostAddressPrivate(); | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | void setAddress(quint32 a_ = 0); | - | ||||||||||||
| 109 | void setAddress(const quint8 *a_); | - | ||||||||||||
| 110 | void setAddress(const Q_IPV6ADDR &a_); | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | bool parse(); | - | ||||||||||||
| 113 | void clear(); | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | QString ipString; | - | ||||||||||||
| 116 | QString scopeId; | - | ||||||||||||
| quint32 a; | ||||||||||||||
| 117 | - | |||||||||||||
| 118 | union { | - | ||||||||||||
| 119 | Q_IPV6ADDR a6; // IPv6 address | - | ||||||||||||
| 120 | struct { quint64 c[2]; } a6_64; | - | ||||||||||||
| 121 | struct { quint32 c[4]; } a6_32; | - | ||||||||||||
| 122 | }; | - | ||||||||||||
| 123 | QAbstractSocket::NetworkLayerProtocolquint32 a; // IPv4 address | - | ||||||||||||
| 124 | qint8 protocol; | - | ||||||||||||
| 125 | bool isParsed; | - | ||||||||||||
| 126 | - | |||||||||||||
| 127 | friend class QHostAddress; | - | ||||||||||||
| 128 | }; | - | ||||||||||||
| 129 | - | |||||||||||||
| 130 | QHostAddressPrivate::QHostAddressPrivate() | - | ||||||||||||
| 131 | : a(0), protocol(QAbstractSocket::UnknownNetworkLayerProtocol), isParsed(true) | - | ||||||||||||
| 132 | { | - | ||||||||||||
| 133 | memset(&a6, 0, sizeof(a6)); | - | ||||||||||||
| 134 | } | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | void QHostAddressPrivate::setAddress(quint32 a_) | - | ||||||||||||
| 137 | { | - | ||||||||||||
| 138 | a = a_; | - | ||||||||||||
| 139 | protocol = QAbstractSocket::IPv4Protocol; | - | ||||||||||||
| 140 | isParsed = true; | - | ||||||||||||
| 141 | - | |||||||||||||
| 142 | //create mapped address, except for a_ == 0 (any) | - | ||||||||||||
| 143 | a6_64.c[0] = 0; | - | ||||||||||||
| 144 | if (a) { | - | ||||||||||||
| 145 | a6_32.c[2] = qToBigEndian(0xffff); | - | ||||||||||||
| 146 | a6_32.c[3] = qToBigEndian(a); | - | ||||||||||||
| 147 | } else { | - | ||||||||||||
| 148 | a6_64.c[1] = 0; | - | ||||||||||||
| 149 | } | - | ||||||||||||
| 150 | } | - | ||||||||||||
| 151 | - | |||||||||||||
| 152 | /// parses v4-mapped addresses or the AnyIPv6 address and stores in \a a; | - | ||||||||||||
| 153 | /// returns true if the address was one of those | - | ||||||||||||
| 154 | static bool convertToIpv4(quint32& a, const Q_IPV6ADDR &a6) | - | ||||||||||||
| 155 | { | - | ||||||||||||
| 156 | const uchar *ptr = a6.c; | - | ||||||||||||
| 157 | if (qFromUnaligned<quint64>(ptr) != 0) | - | ||||||||||||
| 158 | return false; | - | ||||||||||||
| 159 | if (qFromBigEndian<quint32>(ptr + 8) == 0) { | - | ||||||||||||
| 160 | // is it AnyIPv6? | - | ||||||||||||
| 161 | a = 0; | - | ||||||||||||
| 162 | return qFromBigEndian<quint32>(ptr + 12) == 0; | - | ||||||||||||
| 163 | } | - | ||||||||||||
| 164 | if (qFromBigEndian<quint32>(ptr + 8) != 0xFFFF) | - | ||||||||||||
| 165 | return false; | - | ||||||||||||
| 166 | a = qFromBigEndian<quint32>(ptr + 12); | - | ||||||||||||
| 167 | return true; | - | ||||||||||||
| 168 | } | - | ||||||||||||
| 169 | - | |||||||||||||
| 170 | void QHostAddressPrivate::setAddress(const quint8 *a_) | - | ||||||||||||
| 171 | { | - | ||||||||||||
| 172 | protocol = QAbstractSocket::IPv6Protocol; | - | ||||||||||||
| 173 | isParsed = true; | - | ||||||||||||
| 174 | memcpy(a6.c, a_, sizeof(a6)); | - | ||||||||||||
| 175 | a = 0; | - | ||||||||||||
| 176 | convertToIpv4(a, a6); | - | ||||||||||||
| 177 | } | - | ||||||||||||
| 178 | - | |||||||||||||
| 179 | void QHostAddressPrivate::setAddress(const Q_IPV6ADDR &a_) | - | ||||||||||||
| 180 | { | - | ||||||||||||
| 181 | setAddress(a_.c); | - | ||||||||||||
| 182 | } | - | ||||||||||||
| 183 | - | |||||||||||||
| 184 | static bool parseIp6(const QString &address, QIPAddressUtils::IPv6Address &addr, QString *scopeId) | - | ||||||||||||
| 185 | { | - | ||||||||||||
| 186 | QString tmp = address; | - | ||||||||||||
| 187 | int scopeIdPos = tmp.lastIndexOf(QLatin1Char('%')); | - | ||||||||||||
| 188 | if (scopeIdPos != -1) { | - | ||||||||||||
| 189 | *scopeId = tmp.mid(scopeIdPos + 1); | - | ||||||||||||
| 190 | tmp.chop(tmp.size() - scopeIdPos); | - | ||||||||||||
| 191 | } else { | - | ||||||||||||
| 192 | scopeId->clear(); | - | ||||||||||||
| 193 | } | - | ||||||||||||
| 194 | return QIPAddressUtils::parseIp6(addr, tmp.constBegin(), tmp.constEnd()) == 0; | - | ||||||||||||
| 195 | } | - | ||||||||||||
| 196 | - | |||||||||||||
| 197 | Q_NEVER_INLINE bool QHostAddressPrivate::parse() | - | ||||||||||||
| 198 | { | - | ||||||||||||
| 199 | isParsed = true; | - | ||||||||||||
| 200 | protocol = QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
| 201 | QString a = ipString.simplified(); | - | ||||||||||||
| 202 | if (a.isEmpty()) | - | ||||||||||||
| 203 | return false; | - | ||||||||||||
| 204 | - | |||||||||||||
| 205 | // All IPv6 addresses contain a ':', and may contain a '.'. | - | ||||||||||||
| 206 | if (a.contains(QLatin1Char(':'))) { | - | ||||||||||||
| 207 | quint8 maybeIp6[16]; | - | ||||||||||||
| 208 | if (parseIp6(a, maybeIp6, &scopeId)) { | - | ||||||||||||
| 209 | setAddress(maybeIp6); | - | ||||||||||||
| 210 | return true; | - | ||||||||||||
| 211 | } | - | ||||||||||||
| 212 | } | - | ||||||||||||
| 213 | - | |||||||||||||
| 214 | quint32 maybeIp4 = 0; | - | ||||||||||||
| 215 | if (QIPAddressUtils::parseIp4(maybeIp4, a.constBegin(), a.constEnd())) { | - | ||||||||||||
| 216 | setAddress(maybeIp4); | - | ||||||||||||
| 217 | return true; | - | ||||||||||||
| 218 | } | - | ||||||||||||
| 219 | - | |||||||||||||
| 220 | return false; | - | ||||||||||||
| 221 | } | - | ||||||||||||
| 222 | - | |||||||||||||
| 223 | void QHostAddressPrivate::clear() | - | ||||||||||||
| 224 | { | - | ||||||||||||
| 225 | a = 0; | - | ||||||||||||
| 226 | protocol = QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
| 227 | isParsed = true; | - | ||||||||||||
| 228 | memset(&a6, 0, sizeof(a6)); | - | ||||||||||||
| 229 | } | - | ||||||||||||
| 230 | - | |||||||||||||
| 231 | - | |||||||||||||
| 232 | bool QNetmaskAddress::setAddress(const QString &address) | - | ||||||||||||
| 233 | { | - | ||||||||||||
| 234 | length = -1; | - | ||||||||||||
| 235 | QHostAddress other; | - | ||||||||||||
| 236 | return other.setAddress(address) && setAddress(other); | - | ||||||||||||
| 237 | } | - | ||||||||||||
| 238 | - | |||||||||||||
| 239 | bool QNetmaskAddress::setAddress(const QHostAddress &address) | - | ||||||||||||
| 240 | { | - | ||||||||||||
| 241 | static const quint8 zeroes[16] = { 0 }; | - | ||||||||||||
| 242 | union { | - | ||||||||||||
| 243 | quint32 v4; | - | ||||||||||||
| 244 | quint8 v6[16]; | - | ||||||||||||
| 245 | } ip; | - | ||||||||||||
| 246 | - | |||||||||||||
| 247 | int netmask = 0; | - | ||||||||||||
| 248 | quint8 *ptr = ip.v6; | - | ||||||||||||
| 249 | quint8 *end; | - | ||||||||||||
| 250 | length = -1; | - | ||||||||||||
| 251 | - | |||||||||||||
| 252 | QHostAddress::operator=(address); | - | ||||||||||||
| 253 | - | |||||||||||||
| 254 | if (d->protocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
| 255 | ip.v4 = qToBigEndian(d->a); | - | ||||||||||||
| 256 | end = ptr + 4; | - | ||||||||||||
| 257 | } else if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
| 258 | memcpy(ip.v6, d->a6.c, 16); | - | ||||||||||||
| 259 | end = ptr + 16; | - | ||||||||||||
| 260 | } else { | - | ||||||||||||
| 261 | d->clear(); | - | ||||||||||||
| 262 | return false; | - | ||||||||||||
| 263 | } | - | ||||||||||||
| 264 | - | |||||||||||||
| 265 | while (ptr < end) { | - | ||||||||||||
| 266 | switch (*ptr) { | - | ||||||||||||
| 267 | case 255: | - | ||||||||||||
| 268 | netmask += 8; | - | ||||||||||||
| 269 | ++ptr; | - | ||||||||||||
| 270 | continue; | - | ||||||||||||
| 271 | - | |||||||||||||
| 272 | default: | - | ||||||||||||
| 273 | d->clear(); | - | ||||||||||||
| 274 | return false; // invalid IP-style netmask | - | ||||||||||||
| 275 | - | |||||||||||||
| 276 | // the rest always falls through | - | ||||||||||||
| 277 | case 254: | - | ||||||||||||
| 278 | ++netmask; | - | ||||||||||||
| 279 | case 252: | - | ||||||||||||
| 280 | ++netmask; | - | ||||||||||||
| 281 | case 248: | - | ||||||||||||
| 282 | ++netmask; | - | ||||||||||||
| 283 | case 240: | - | ||||||||||||
| 284 | ++netmask; | - | ||||||||||||
| 285 | case 224: | - | ||||||||||||
| 286 | ++netmask; | - | ||||||||||||
| 287 | case 192: | - | ||||||||||||
| 288 | ++netmask; | - | ||||||||||||
| 289 | case 128: | - | ||||||||||||
| 290 | ++netmask; | - | ||||||||||||
| 291 | case 0: | - | ||||||||||||
| 292 | break; | - | ||||||||||||
| 293 | } | - | ||||||||||||
| 294 | break; | - | ||||||||||||
| 295 | } | - | ||||||||||||
| 296 | - | |||||||||||||
| 297 | // confirm that the rest is only zeroes | - | ||||||||||||
| 298 | if (ptr < end && memcmp(ptr + 1, zeroes, end - ptr - 1) != 0) { | - | ||||||||||||
| 299 | d->clear(); | - | ||||||||||||
| 300 | return false; | - | ||||||||||||
| 301 | } | - | ||||||||||||
| 302 | - | |||||||||||||
| 303 | length = netmask; | - | ||||||||||||
| 304 | return true; | - | ||||||||||||
| 305 | } | - | ||||||||||||
| 306 | - | |||||||||||||
| 307 | static void clearBits(quint8 *where, int start, int end) | - | ||||||||||||
| 308 | { | - | ||||||||||||
| 309 | Q_ASSERT(end == 32 || end == 128); | - | ||||||||||||
| 310 | if (start == end) | - | ||||||||||||
| 311 | return; | - | ||||||||||||
| 312 | - | |||||||||||||
| 313 | // for the byte where 'start' is, clear the lower bits only | - | ||||||||||||
| 314 | quint8 bytemask = 256 - (1 << (8 - (start & 7))); | - | ||||||||||||
| 315 | where[start / 8] &= bytemask; | - | ||||||||||||
| 316 | - | |||||||||||||
| 317 | // for the tail part, clear everything | - | ||||||||||||
| 318 | memset(where + (start + 7) / 8, 0, end / 8 - (start + 7) / 8); | - | ||||||||||||
| 319 | } | - | ||||||||||||
| 320 | - | |||||||||||||
| 321 | int QNetmaskAddress::prefixLength() const | - | ||||||||||||
| 322 | { | - | ||||||||||||
| 323 | return length; | - | ||||||||||||
| 324 | } | - | ||||||||||||
| 325 | - | |||||||||||||
| 326 | void QNetmaskAddress::setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int newLength) | - | ||||||||||||
| 327 | { | - | ||||||||||||
| 328 | length = newLength; | - | ||||||||||||
| 329 | if (length < 0 || length > (proto == QAbstractSocket::IPv4Protocol ? 32 : | - | ||||||||||||
| 330 | proto == QAbstractSocket::IPv6Protocol ? 128 : -1)) { | - | ||||||||||||
| 331 | // invalid information, reject | - | ||||||||||||
| 332 | d->protocol = QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
| 333 | length = -1; | - | ||||||||||||
| 334 | return; | - | ||||||||||||
| 335 | } | - | ||||||||||||
| 336 | - | |||||||||||||
| 337 | d->protocol = proto; | - | ||||||||||||
| 338 | if (d->protocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
| 339 | if (length == 0) { | - | ||||||||||||
| 340 | d->a = 0; | - | ||||||||||||
| 341 | } else if (length == 32) { | - | ||||||||||||
| 342 | d->a = quint32(0xffffffff); | - | ||||||||||||
| 343 | } else { | - | ||||||||||||
| 344 | d->a = quint32(0xffffffff) >> (32 - length) << (32 - length); | - | ||||||||||||
| 345 | } | - | ||||||||||||
| 346 | } else { | - | ||||||||||||
| 347 | memset(d->a6.c, 0xFF, sizeof(d->a6)); | - | ||||||||||||
| 348 | clearBits(d->a6.c, length, 128); | - | ||||||||||||
| 349 | } | - | ||||||||||||
| 350 | } | - | ||||||||||||
| 351 | - | |||||||||||||
| 352 | /*! | - | ||||||||||||
| 353 | \class QHostAddress | - | ||||||||||||
| 354 | \brief The QHostAddress class provides an IP address. | - | ||||||||||||
| 355 | \ingroup network | - | ||||||||||||
| 356 | \inmodule QtNetwork | - | ||||||||||||
| 357 | - | |||||||||||||
| 358 | This class holds an IPv4 or IPv6 address in a platform- and | - | ||||||||||||
| 359 | protocol-independent manner. | - | ||||||||||||
| 360 | - | |||||||||||||
| 361 | QHostAddress is normally used with the QTcpSocket, QTcpServer, | - | ||||||||||||
| 362 | and QUdpSocket to connect to a host or to set up a server. | - | ||||||||||||
| 363 | - | |||||||||||||
| 364 | A host address is set with setAddress(), and retrieved with | - | ||||||||||||
| 365 | toIPv4Address(), toIPv6Address(), or toString(). You can check the | - | ||||||||||||
| 366 | type with protocol(). | - | ||||||||||||
| 367 | - | |||||||||||||
| 368 | \note Please note that QHostAddress does not do DNS lookups. | - | ||||||||||||
| 369 | QHostInfo is needed for that. | - | ||||||||||||
| 370 | - | |||||||||||||
| 371 | The class also supports common predefined addresses: \l Null, \l | - | ||||||||||||
| 372 | LocalHost, \l LocalHostIPv6, \l Broadcast, and \l Any. | - | ||||||||||||
| 373 | - | |||||||||||||
| 374 | \sa QHostInfo, QTcpSocket, QTcpServer, QUdpSocket | - | ||||||||||||
| 375 | */ | - | ||||||||||||
| 376 | - | |||||||||||||
| 377 | /*! \enum QHostAddress::SpecialAddress | - | ||||||||||||
| 378 | - | |||||||||||||
| 379 | \value Null The null address object. Equivalent to QHostAddress(). | - | ||||||||||||
| 380 | \value LocalHost The IPv4 localhost address. Equivalent to QHostAddress("127.0.0.1"). | - | ||||||||||||
| 381 | \value LocalHostIPv6 The IPv6 localhost address. Equivalent to QHostAddress("::1"). | - | ||||||||||||
| 382 | \value Broadcast The IPv4 broadcast address. Equivalent to QHostAddress("255.255.255.255"). | - | ||||||||||||
| 383 | \value AnyIPv4 The IPv4 any-address. Equivalent to QHostAddress("0.0.0.0"). A socket bound with this address will listen only on IPv4 interaces. | - | ||||||||||||
| 384 | \value AnyIPv6 The IPv6 any-address. Equivalent to QHostAddress("::"). A socket bound with this address will listen only on IPv6 interaces. | - | ||||||||||||
| 385 | \value Any The dual stack any-address. A socket bound with this address will listen on both IPv4 and IPv6 interfaces. | - | ||||||||||||
| 386 | */ | - | ||||||||||||
| 387 | - | |||||||||||||
| 388 | /*! Constructs a null host address object, i.e. an address which is not valid for any host or interface. | - | ||||||||||||
| 389 | - | |||||||||||||
| 390 | \sa clear() | - | ||||||||||||
| 391 | */ | - | ||||||||||||
| 392 | QHostAddress::QHostAddress() | - | ||||||||||||
| 393 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 394 | { | - | ||||||||||||
| 395 | } | - | ||||||||||||
| 396 | - | |||||||||||||
| 397 | /*! | - | ||||||||||||
| 398 | Constructs a host address object with the IPv4 address \a ip4Addr. | - | ||||||||||||
| 399 | */ | - | ||||||||||||
| 400 | QHostAddress::QHostAddress(quint32 ip4Addr) | - | ||||||||||||
| 401 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 402 | { | - | ||||||||||||
| 403 | setAddress(ip4Addr); | - | ||||||||||||
| 404 | } | - | ||||||||||||
| 405 | - | |||||||||||||
| 406 | /*! | - | ||||||||||||
| 407 | Constructs a host address object with the IPv6 address \a ip6Addr. | - | ||||||||||||
| 408 | - | |||||||||||||
| 409 | \a ip6Addr must be a 16-byte array in network byte order (big | - | ||||||||||||
| 410 | endian). | - | ||||||||||||
| 411 | */ | - | ||||||||||||
| 412 | QHostAddress::QHostAddress(quint8 *ip6Addr) | - | ||||||||||||
| 413 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 414 | { | - | ||||||||||||
| 415 | setAddress(ip6Addr); | - | ||||||||||||
| 416 | } | - | ||||||||||||
| 417 | - | |||||||||||||
| 418 | /*! | - | ||||||||||||
| 419 | \since 5.5 | - | ||||||||||||
| 420 | Constructs a host address object with the IPv6 address \a ip6Addr. | - | ||||||||||||
| 421 | - | |||||||||||||
| 422 | \a ip6Addr must be a 16-byte array in network byte order (big | - | ||||||||||||
| 423 | endian). | - | ||||||||||||
| 424 | */ | - | ||||||||||||
| 425 | QHostAddress::QHostAddress(const quint8 *ip6Addr) | - | ||||||||||||
| 426 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 427 | { | - | ||||||||||||
| 428 | setAddress(ip6Addr); | - | ||||||||||||
| 429 | } | - | ||||||||||||
| 430 | - | |||||||||||||
| 431 | /*! | - | ||||||||||||
| 432 | Constructs a host address object with the IPv6 address \a ip6Addr. | - | ||||||||||||
| 433 | */ | - | ||||||||||||
| 434 | QHostAddress::QHostAddress(const Q_IPV6ADDR &ip6Addr) | - | ||||||||||||
| 435 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 436 | { | - | ||||||||||||
| 437 | setAddress(ip6Addr); | - | ||||||||||||
| 438 | } | - | ||||||||||||
| 439 | - | |||||||||||||
| 440 | /*! | - | ||||||||||||
| 441 | Constructs an IPv4 or IPv6 address based on the string \a address | - | ||||||||||||
| 442 | (e.g., "127.0.0.1"). | - | ||||||||||||
| 443 | - | |||||||||||||
| 444 | \sa setAddress() | - | ||||||||||||
| 445 | */ | - | ||||||||||||
| 446 | QHostAddress::QHostAddress(const QString &address) | - | ||||||||||||
| 447 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 448 | { | - | ||||||||||||
| 449 | d->ipString = address; | - | ||||||||||||
| 450 | d->isParsed = false; | - | ||||||||||||
| 451 | } | - | ||||||||||||
| 452 | - | |||||||||||||
| 453 | /*! | - | ||||||||||||
| 454 | \fn QHostAddress::QHostAddress(const sockaddr *sockaddr) | - | ||||||||||||
| 455 | - | |||||||||||||
| 456 | Constructs an IPv4 or IPv6 address using the address specified by | - | ||||||||||||
| 457 | the native structure \a sockaddr. | - | ||||||||||||
| 458 | - | |||||||||||||
| 459 | \sa setAddress() | - | ||||||||||||
| 460 | */ | - | ||||||||||||
| 461 | QHostAddress::QHostAddress(const struct sockaddr *sockaddr) | - | ||||||||||||
| 462 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 463 | { | - | ||||||||||||
| 464 | #ifndef Q_OS_WINRT | - | ||||||||||||
| 465 | if (sockaddr->sa_family == AF_INET) | - | ||||||||||||
| 466 | setAddress(htonl(((const sockaddr_in *)sockaddr)->sin_addr.s_addr)); | - | ||||||||||||
| 467 | else if (sockaddr->sa_family == AF_INET6) | - | ||||||||||||
| 468 | setAddress(((const qt_sockaddr_in6 *)sockaddr)->sin6_addr.qt_s6_addr); | - | ||||||||||||
| 469 | #else | - | ||||||||||||
| 470 | Q_UNUSED(sockaddr) | - | ||||||||||||
| 471 | #endif | - | ||||||||||||
| 472 | } | - | ||||||||||||
| 473 | - | |||||||||||||
| 474 | /*! | - | ||||||||||||
| 475 | Constructs a copy of the given \a address. | - | ||||||||||||
| 476 | */ | - | ||||||||||||
| 477 | QHostAddress::QHostAddress(const QHostAddress &address) | - | ||||||||||||
| 478 | : d(new QHostAddressPrivate(*address.d.data())) | - | ||||||||||||
| 479 | { | - | ||||||||||||
| 480 | } | - | ||||||||||||
| 481 | - | |||||||||||||
| 482 | /*! | - | ||||||||||||
| 483 | Constructs a QHostAddress object for \a address. | - | ||||||||||||
| 484 | */ | - | ||||||||||||
| 485 | QHostAddress::QHostAddress(SpecialAddress address) | - | ||||||||||||
| 486 | : d(new QHostAddressPrivate) | - | ||||||||||||
| 487 | { | - | ||||||||||||
| 488 | Q_IPV6ADDR ip6; | - | ||||||||||||
| 489 | memset(&ip6, 0, sizeof ip6); | - | ||||||||||||
| 490 | quint32 ip4 = INADDR_ANY; | - | ||||||||||||
| 491 | - | |||||||||||||
| 492 | switch (address) { | - | ||||||||||||
| 493 | case Null: | - | ||||||||||||
| 494 | return; | - | ||||||||||||
| 495 | - | |||||||||||||
| 496 | case Broadcast: | - | ||||||||||||
| 497 | ip4 = INADDR_BROADCAST; | - | ||||||||||||
| 498 | break; | - | ||||||||||||
| 499 | case LocalHost: | - | ||||||||||||
| 500 | ip4 = INADDR_LOOPBACK; | - | ||||||||||||
| 501 | break; | - | ||||||||||||
| 502 | case AnyIPv4: | - | ||||||||||||
| 503 | break; | - | ||||||||||||
| 504 | - | |||||||||||||
| 505 | case LocalHostIPv6: | - | ||||||||||||
| 506 | ip6[15] = 1; | - | ||||||||||||
| 507 | // fall through | - | ||||||||||||
| 508 | case AnyIPv6: | - | ||||||||||||
| 509 | d->setAddress(ip6); | - | ||||||||||||
| 510 | return; | - | ||||||||||||
| 511 | - | |||||||||||||
| 512 | case Any: | - | ||||||||||||
| 513 | d->protocol = QAbstractSocket::AnyIPProtocol; | - | ||||||||||||
| 514 | return; | - | ||||||||||||
| 515 | } | - | ||||||||||||
| 516 | - | |||||||||||||
| 517 | // common IPv4 part | - | ||||||||||||
| 518 | d->setAddress(ip4); | - | ||||||||||||
| 519 | } | - | ||||||||||||
| 520 | - | |||||||||||||
| 521 | /*! | - | ||||||||||||
| 522 | Destroys the host address object. | - | ||||||||||||
| 523 | */ | - | ||||||||||||
| 524 | QHostAddress::~QHostAddress() | - | ||||||||||||
| 525 | { | - | ||||||||||||
| 526 | } | - | ||||||||||||
| 527 | - | |||||||||||||
| 528 | /*! | - | ||||||||||||
| 529 | Assigns another host \a address to this object, and returns a reference | - | ||||||||||||
| 530 | to this object. | - | ||||||||||||
| 531 | */ | - | ||||||||||||
| 532 | QHostAddress &QHostAddress::operator=(const QHostAddress &address) | - | ||||||||||||
| 533 | { | - | ||||||||||||
| 534 | *d.data() = *address.d.data(); | - | ||||||||||||
| 535 | return *this; | - | ||||||||||||
| 536 | } | - | ||||||||||||
| 537 | - | |||||||||||||
| 538 | /*! | - | ||||||||||||
| 539 | Assigns the host address \a address to this object, and returns a | - | ||||||||||||
| 540 | reference to this object. | - | ||||||||||||
| 541 | - | |||||||||||||
| 542 | \sa setAddress() | - | ||||||||||||
| 543 | */ | - | ||||||||||||
| 544 | QHostAddress &QHostAddress::operator=(const QString &address) | - | ||||||||||||
| 545 | { | - | ||||||||||||
| 546 | setAddress(address); | - | ||||||||||||
| 547 | return *this; | - | ||||||||||||
| 548 | } | - | ||||||||||||
| 549 | - | |||||||||||||
| 550 | /*! | - | ||||||||||||
| 551 | \fn bool QHostAddress::operator!=(const QHostAddress &other) const | - | ||||||||||||
| 552 | \since 4.2 | - | ||||||||||||
| 553 | - | |||||||||||||
| 554 | Returns \c true if this host address is not the same as the \a other | - | ||||||||||||
| 555 | address given; otherwise returns \c false. | - | ||||||||||||
| 556 | */ | - | ||||||||||||
| 557 | - | |||||||||||||
| 558 | /*! | - | ||||||||||||
| 559 | \fn bool QHostAddress::operator!=(SpecialAddress other) const | - | ||||||||||||
| 560 | - | |||||||||||||
| 561 | Returns \c true if this host address is not the same as the \a other | - | ||||||||||||
| 562 | address given; otherwise returns \c false. | - | ||||||||||||
| 563 | */ | - | ||||||||||||
| 564 | - | |||||||||||||
| 565 | /*! | - | ||||||||||||
| 566 | Sets the host address to 0.0.0.0. | - | ||||||||||||
| 567 | */ | - | ||||||||||||
| 568 | void QHostAddress::clear() | - | ||||||||||||
| 569 | { | - | ||||||||||||
| 570 | d->clear(); | - | ||||||||||||
| 571 | } | - | ||||||||||||
| 572 | - | |||||||||||||
| 573 | /*! | - | ||||||||||||
| 574 | Set the IPv4 address specified by \a ip4Addr. | - | ||||||||||||
| 575 | */ | - | ||||||||||||
| 576 | void QHostAddress::setAddress(quint32 ip4Addr) | - | ||||||||||||
| 577 | { | - | ||||||||||||
| 578 | d->setAddress(ip4Addr); | - | ||||||||||||
| 579 | } | - | ||||||||||||
| 580 | - | |||||||||||||
| 581 | /*! | - | ||||||||||||
| 582 | \overload | - | ||||||||||||
| 583 | - | |||||||||||||
| 584 | Set the IPv6 address specified by \a ip6Addr. | - | ||||||||||||
| 585 | - | |||||||||||||
| 586 | \a ip6Addr must be an array of 16 bytes in network byte order | - | ||||||||||||
| 587 | (high-order byte first). | - | ||||||||||||
| 588 | */ | - | ||||||||||||
| 589 | void QHostAddress::setAddress(quint8 *ip6Addr) | - | ||||||||||||
| 590 | { | - | ||||||||||||
| 591 | d->setAddress(ip6Addr); | - | ||||||||||||
| 592 | } | - | ||||||||||||
| 593 | - | |||||||||||||
| 594 | /*! | - | ||||||||||||
| 595 | \overload | - | ||||||||||||
| 596 | \since 5.5 | - | ||||||||||||
| 597 | - | |||||||||||||
| 598 | Set the IPv6 address specified by \a ip6Addr. | - | ||||||||||||
| 599 | - | |||||||||||||
| 600 | \a ip6Addr must be an array of 16 bytes in network byte order | - | ||||||||||||
| 601 | (high-order byte first). | - | ||||||||||||
| 602 | */ | - | ||||||||||||
| 603 | void QHostAddress::setAddress(const quint8 *ip6Addr) | - | ||||||||||||
| 604 | { | - | ||||||||||||
| 605 | d->setAddress(ip6Addr); | - | ||||||||||||
| 606 | } | - | ||||||||||||
| 607 | - | |||||||||||||
| 608 | /*! | - | ||||||||||||
| 609 | \overload | - | ||||||||||||
| 610 | - | |||||||||||||
| 611 | Set the IPv6 address specified by \a ip6Addr. | - | ||||||||||||
| 612 | */ | - | ||||||||||||
| 613 | void QHostAddress::setAddress(const Q_IPV6ADDR &ip6Addr) | - | ||||||||||||
| 614 | { | - | ||||||||||||
| 615 | d->setAddress(ip6Addr); | - | ||||||||||||
| 616 | } | - | ||||||||||||
| 617 | - | |||||||||||||
| 618 | /*! | - | ||||||||||||
| 619 | \overload | - | ||||||||||||
| 620 | - | |||||||||||||
| 621 | Sets the IPv4 or IPv6 address specified by the string | - | ||||||||||||
| 622 | representation specified by \a address (e.g. "127.0.0.1"). | - | ||||||||||||
| 623 | Returns \c true and sets the address if the address was successfully | - | ||||||||||||
| 624 | parsed; otherwise returns \c false. | - | ||||||||||||
| 625 | */ | - | ||||||||||||
| 626 | bool QHostAddress::setAddress(const QString &address) | - | ||||||||||||
| 627 | { | - | ||||||||||||
| 628 | d->ipString = address; | - | ||||||||||||
| 629 | return d->parse(); | - | ||||||||||||
| 630 | } | - | ||||||||||||
| 631 | - | |||||||||||||
| 632 | /*! | - | ||||||||||||
| 633 | \fn void QHostAddress::setAddress(const sockaddr *sockaddr) | - | ||||||||||||
| 634 | \overload | - | ||||||||||||
| 635 | - | |||||||||||||
| 636 | Sets the IPv4 or IPv6 address specified by the native structure \a | - | ||||||||||||
| 637 | sockaddr. Returns \c true and sets the address if the address was | - | ||||||||||||
| 638 | successfully parsed; otherwise returns \c false. | - | ||||||||||||
| 639 | */ | - | ||||||||||||
| 640 | void QHostAddress::setAddress(const struct sockaddr *sockaddr) | - | ||||||||||||
| 641 | { | - | ||||||||||||
| 642 | #ifndef Q_OS_WINRT | - | ||||||||||||
| 643 | clear(); | - | ||||||||||||
| 644 | if (sockaddr->sa_family == AF_INET) | - | ||||||||||||
| 645 | setAddress(htonl(((const sockaddr_in *)sockaddr)->sin_addr.s_addr)); | - | ||||||||||||
| 646 | else if (sockaddr->sa_family == AF_INET6) | - | ||||||||||||
| 647 | setAddress(((const qt_sockaddr_in6 *)sockaddr)->sin6_addr.qt_s6_addr); | - | ||||||||||||
| 648 | #else | - | ||||||||||||
| 649 | Q_UNUSED(sockaddr) | - | ||||||||||||
| 650 | #endif | - | ||||||||||||
| 651 | } | - | ||||||||||||
| 652 | - | |||||||||||||
| 653 | /*! | - | ||||||||||||
| 654 | Returns the IPv4 address as a number. | - | ||||||||||||
| 655 | - | |||||||||||||
| 656 | For example, if the address is 127.0.0.1, the returned value is | - | ||||||||||||
| 657 | 2130706433 (i.e. 0x7f000001). | - | ||||||||||||
| 658 | - | |||||||||||||
| 659 | This value is valid if the protocol() is | - | ||||||||||||
| 660 | \l{QAbstractSocket::}{IPv4Protocol}, | - | ||||||||||||
| 661 | or if the protocol is | - | ||||||||||||
| 662 | \l{QAbstractSocket::}{IPv6Protocol}, | - | ||||||||||||
| 663 | and the IPv6 address is an IPv4 mapped address. (RFC4291) | - | ||||||||||||
| 664 | - | |||||||||||||
| 665 | \sa toString() | - | ||||||||||||
| 666 | */ | - | ||||||||||||
| 667 | quint32 QHostAddress::toIPv4Address() const | - | ||||||||||||
| 668 | { | - | ||||||||||||
| 669 | return toIPv4Address(Q_NULLPTR); | - | ||||||||||||
| 670 | } | - | ||||||||||||
| 671 | - | |||||||||||||
| 672 | /*! | - | ||||||||||||
| 673 | Returns the IPv4 address as a number. | - | ||||||||||||
| 674 | - | |||||||||||||
| 675 | For example, if the address is 127.0.0.1, the returned value is | - | ||||||||||||
| 676 | 2130706433 (i.e. 0x7f000001). | - | ||||||||||||
| 677 | - | |||||||||||||
| 678 | This value is valid if the protocol() is | - | ||||||||||||
| 679 | \l{QAbstractSocket::}{IPv4Protocol}, | - | ||||||||||||
| 680 | or if the protocol is | - | ||||||||||||
| 681 | \l{QAbstractSocket::}{IPv6Protocol}, | - | ||||||||||||
| 682 | and the IPv6 address is an IPv4 mapped address. (RFC4291). In those | - | ||||||||||||
| 683 | cases, \a ok will be set to true. Otherwise, it will be set to false. | - | ||||||||||||
| 684 | - | |||||||||||||
| 685 | \sa toString() | - | ||||||||||||
| 686 | */ | - | ||||||||||||
| 687 | quint32 QHostAddress::toIPv4Address(bool *ok) const | - | ||||||||||||
| 688 | { | - | ||||||||||||
| 689 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 690 | quint32 dummy; | - | ||||||||||||
| 691 | if (ok) | - | ||||||||||||
| 692 | *ok = d->protocol == QAbstractSocket::IPv4Protocol || d->protocol == QAbstractSocket::AnyIPProtocol | - | ||||||||||||
| 693 | || (d->protocol == QAbstractSocket::IPv6Protocol && convertToIpv4(dummy, d->a6)); | - | ||||||||||||
| 694 | return d->a; | - | ||||||||||||
| 695 | } | - | ||||||||||||
| 696 | - | |||||||||||||
| 697 | /*! | - | ||||||||||||
| 698 | Returns the network layer protocol of the host address. | - | ||||||||||||
| 699 | */ | - | ||||||||||||
| 700 | QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol() const | - | ||||||||||||
| 701 | { | - | ||||||||||||
| 702 | QT_ENSURE_PARSED(this); executed 286 times by 12 tests: (this)->d->parse();Executed by:
| 286-40148 | ||||||||||||
| 703 | return QAbstractSocket::NetworkLayerProtocol(d->protocol;); executed 40434 times by 39 tests: return QAbstractSocket::NetworkLayerProtocol(d->protocol);Executed by:
| 40434 | ||||||||||||
| 704 | } | - | ||||||||||||
| 705 | - | |||||||||||||
| 706 | /*! | - | ||||||||||||
| 707 | Returns the IPv6 address as a Q_IPV6ADDR structure. The structure | - | ||||||||||||
| 708 | consists of 16 unsigned characters. | - | ||||||||||||
| 709 | - | |||||||||||||
| 710 | \snippet code/src_network_kernel_qhostaddress.cpp 0 | - | ||||||||||||
| 711 | - | |||||||||||||
| 712 | This value is valid if the protocol() is | - | ||||||||||||
| 713 | \l{QAbstractSocket::}{IPv6Protocol}. | - | ||||||||||||
| 714 | If the protocol is | - | ||||||||||||
| 715 | \l{QAbstractSocket::}{IPv4Protocol}, | - | ||||||||||||
| 716 | then the address is returned an an IPv4 mapped IPv6 address. (RFC4291) | - | ||||||||||||
| 717 | - | |||||||||||||
| 718 | \sa toString() | - | ||||||||||||
| 719 | */ | - | ||||||||||||
| 720 | Q_IPV6ADDR QHostAddress::toIPv6Address() const | - | ||||||||||||
| 721 | { | - | ||||||||||||
| 722 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 723 | return d->a6; | - | ||||||||||||
| 724 | } | - | ||||||||||||
| 725 | - | |||||||||||||
| 726 | /*! | - | ||||||||||||
| 727 | Returns the address as a string. | - | ||||||||||||
| 728 | - | |||||||||||||
| 729 | For example, if the address is the IPv4 address 127.0.0.1, the | - | ||||||||||||
| 730 | returned string is "127.0.0.1". For IPv6 the string format will | - | ||||||||||||
| 731 | follow the RFC5952 recommendation. | - | ||||||||||||
| 732 | For QHostAddress::Any, its IPv4 address will be returned ("0.0.0.0") | - | ||||||||||||
| 733 | - | |||||||||||||
| 734 | \sa toIPv4Address() | - | ||||||||||||
| 735 | */ | - | ||||||||||||
| 736 | QString QHostAddress::toString() const | - | ||||||||||||
| 737 | { | - | ||||||||||||
| 738 | QT_ENSURE_PARSED(this); executed 3 times by 2 tests: (this)->d->parse();Executed by:
| 3-2970 | ||||||||||||
| 739 | QString s; | - | ||||||||||||
| 740 | if (d->protocol == QAbstractSocket::IPv4Protocol
| 487-2486 | ||||||||||||
| 741 | || d->protocol == QAbstractSocket::AnyIPProtocol) {
| 23-464 | ||||||||||||
| 742 | quint32 i = toIPv4Address(); | - | ||||||||||||
| 743 | QString s;QIPAddressUtils::toString(s, i); | - | ||||||||||||
| 744 | return s;} else if (d->protocol == QAbstractSocket::IPv6Protocol) { executed 2509 times by 25 tests: end of blockExecuted by:
| 181-2509 | ||||||||||||
| 745 | QString s;QIPAddressUtils::toString(s, d->a6.c); | - | ||||||||||||
| 746 | if (!d->scopeId.isEmpty())
| 33-250 | ||||||||||||
| 747 | s.append(QLatin1Char('%') + d->scopeId); executed 33 times by 5 tests: s.append(QLatin1Char('%') + d->scopeId);Executed by:
| 33 | ||||||||||||
| 748 | } executed 283 times by 10 tests: end of blockExecuted by:
| 283 | ||||||||||||
| 749 | return s; executed 2973 times by 25 tests: return s;Executed by:
| 2973 | ||||||||||||
| } | ||||||||||||||
| return QString();} | ||||||||||||||
| 751 | - | |||||||||||||
| 752 | /*! | - | ||||||||||||
| 753 | \since 4.1 | - | ||||||||||||
| 754 | - | |||||||||||||
| 755 | Returns the scope ID of an IPv6 address. For IPv4 addresses, or if the | - | ||||||||||||
| 756 | address does not contain a scope ID, an empty QString is returned. | - | ||||||||||||
| 757 | - | |||||||||||||
| 758 | The IPv6 scope ID specifies the scope of \e reachability for non-global | - | ||||||||||||
| 759 | IPv6 addresses, limiting the area in which the address can be used. All | - | ||||||||||||
| 760 | IPv6 addresses are associated with such a reachability scope. The scope ID | - | ||||||||||||
| 761 | is used to disambiguate addresses that are not guaranteed to be globally | - | ||||||||||||
| 762 | unique. | - | ||||||||||||
| 763 | - | |||||||||||||
| 764 | IPv6 specifies the following four levels of reachability: | - | ||||||||||||
| 765 | - | |||||||||||||
| 766 | \list | - | ||||||||||||
| 767 | - | |||||||||||||
| 768 | \li Node-local: Addresses that are only used for communicating with | - | ||||||||||||
| 769 | services on the same interface (e.g., the loopback interface "::1"). | - | ||||||||||||
| 770 | - | |||||||||||||
| 771 | \li Link-local: Addresses that are local to the network interface | - | ||||||||||||
| 772 | (\e{link}). There is always one link-local address for each IPv6 interface | - | ||||||||||||
| 773 | on your host. Link-local addresses ("fe80...") are generated from the MAC | - | ||||||||||||
| 774 | address of the local network adaptor, and are not guaranteed to be unique. | - | ||||||||||||
| 775 | - | |||||||||||||
| 776 | \li Global: For globally routable addresses, such as public servers on the | - | ||||||||||||
| 777 | Internet. | - | ||||||||||||
| 778 | - | |||||||||||||
| 779 | \endlist | - | ||||||||||||
| 780 | - | |||||||||||||
| 781 | When using a link-local or site-local address for IPv6 connections, you | - | ||||||||||||
| 782 | must specify the scope ID. The scope ID for a link-local address is | - | ||||||||||||
| 783 | usually the same as the interface name (e.g., "eth0", "en1") or number | - | ||||||||||||
| 784 | (e.g., "1", "2"). | - | ||||||||||||
| 785 | - | |||||||||||||
| 786 | \sa setScopeId(), QNetworkInterface, QNetworkInterface::interfaceFromName | - | ||||||||||||
| 787 | */ | - | ||||||||||||
| 788 | QString QHostAddress::scopeId() const | - | ||||||||||||
| 789 | { | - | ||||||||||||
| 790 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 791 | return (d->protocol == QAbstractSocket::IPv6Protocol) ? d->scopeId : QString(); | - | ||||||||||||
| 792 | } | - | ||||||||||||
| 793 | - | |||||||||||||
| 794 | /*! | - | ||||||||||||
| 795 | \since 4.1 | - | ||||||||||||
| 796 | - | |||||||||||||
| 797 | Sets the IPv6 scope ID of the address to \a id. If the address protocol is | - | ||||||||||||
| 798 | not IPv6, this function does nothing. The scope ID may be set as an | - | ||||||||||||
| 799 | interface name (such as "eth0" or "en1") or as an integer representing the | - | ||||||||||||
| 800 | interface index. If \a id is an interface name, QtNetwork will convert to | - | ||||||||||||
| 801 | an interface index using QNetworkInterface::interfaceIndexFromName() before | - | ||||||||||||
| 802 | calling the operating system networking functions. | - | ||||||||||||
| 803 | - | |||||||||||||
| 804 | \sa scopeId(), QNetworkInterface, QNetworkInterface::interfaceFromName | - | ||||||||||||
| 805 | */ | - | ||||||||||||
| 806 | void QHostAddress::setScopeId(const QString &id) | - | ||||||||||||
| 807 | { | - | ||||||||||||
| 808 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 809 | if (d->protocol == QAbstractSocket::IPv6Protocol) | - | ||||||||||||
| 810 | d->scopeId = id; | - | ||||||||||||
| 811 | } | - | ||||||||||||
| 812 | - | |||||||||||||
| 813 | /*! | - | ||||||||||||
| 814 | Returns \c true if this host address is the same as the \a other address | - | ||||||||||||
| 815 | given; otherwise returns \c false. | - | ||||||||||||
| 816 | */ | - | ||||||||||||
| 817 | bool QHostAddress::operator==(const QHostAddress &other) const | - | ||||||||||||
| 818 | { | - | ||||||||||||
| 819 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 820 | QT_ENSURE_PARSED(&other); | - | ||||||||||||
| 821 | - | |||||||||||||
| 822 | if (d->protocol == QAbstractSocket::IPv4Protocol) | - | ||||||||||||
| 823 | return other.d->protocol == QAbstractSocket::IPv4Protocol && d->a == other.d->a; | - | ||||||||||||
| 824 | if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
| 825 | return other.d->protocol == QAbstractSocket::IPv6Protocol | - | ||||||||||||
| 826 | && memcmp(&d->a6, &other.d->a6, sizeof(Q_IPV6ADDR)) == 0; | - | ||||||||||||
| 827 | } | - | ||||||||||||
| 828 | return d->protocol == other.d->protocol; | - | ||||||||||||
| 829 | } | - | ||||||||||||
| 830 | - | |||||||||||||
| 831 | /*! | - | ||||||||||||
| 832 | Returns \c true if this host address is the same as the \a other | - | ||||||||||||
| 833 | address given; otherwise returns \c false. | - | ||||||||||||
| 834 | */ | - | ||||||||||||
| 835 | bool QHostAddress::operator ==(SpecialAddress other) const | - | ||||||||||||
| 836 | { | - | ||||||||||||
| 837 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 838 | quint32 ip4 = INADDR_ANY; | - | ||||||||||||
| 839 | switch (other) { | - | ||||||||||||
| 840 | case Null: | - | ||||||||||||
| 841 | return d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
| 842 | - | |||||||||||||
| 843 | case Broadcast: | - | ||||||||||||
| 844 | ip4 = INADDR_BROADCAST; | - | ||||||||||||
| 845 | break; | - | ||||||||||||
| 846 | - | |||||||||||||
| 847 | case LocalHost: | - | ||||||||||||
| 848 | ip4 = INADDR_LOOPBACK; | - | ||||||||||||
| 849 | break; | - | ||||||||||||
| 850 | - | |||||||||||||
| 851 | case Any: | - | ||||||||||||
| 852 | return d->protocol == QAbstractSocket::AnyIPProtocol; | - | ||||||||||||
| 853 | - | |||||||||||||
| 854 | case AnyIPv4: | - | ||||||||||||
| 855 | break; | - | ||||||||||||
| 856 | - | |||||||||||||
| 857 | case LocalHostIPv6: | - | ||||||||||||
| 858 | case AnyIPv6: | - | ||||||||||||
| 859 | if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
| 860 | quint64 second = quint8(other == LocalHostIPv6); // 1 for localhost, 0 for any | - | ||||||||||||
| 861 | return d->a6_64.c[0] == 0 && d->a6_64.c[1] == qToBigEndian(second); | - | ||||||||||||
| 862 | } | - | ||||||||||||
| 863 | return false; | - | ||||||||||||
| 864 | } | - | ||||||||||||
| 865 | - | |||||||||||||
| 866 | // common IPv4 part | - | ||||||||||||
| 867 | return d->protocol == QAbstractSocket::IPv4Protocol && d->a == ip4; | - | ||||||||||||
| 868 | } | - | ||||||||||||
| 869 | - | |||||||||||||
| 870 | /*! | - | ||||||||||||
| 871 | Returns \c true if this host address is null (INADDR_ANY or in6addr_any). | - | ||||||||||||
| 872 | The default constructor creates a null address, and that address is | - | ||||||||||||
| 873 | not valid for any host or interface. | - | ||||||||||||
| 874 | */ | - | ||||||||||||
| 875 | bool QHostAddress::isNull() const | - | ||||||||||||
| 876 | { | - | ||||||||||||
| 877 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 878 | return d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
| 879 | } | - | ||||||||||||
| 880 | - | |||||||||||||
| 881 | /*! | - | ||||||||||||
| 882 | \since 4.5 | - | ||||||||||||
| 883 | - | |||||||||||||
| 884 | Returns \c true if this IP is in the subnet described by the network | - | ||||||||||||
| 885 | prefix \a subnet and netmask \a netmask. | - | ||||||||||||
| 886 | - | |||||||||||||
| 887 | An IP is considered to belong to a subnet if it is contained | - | ||||||||||||
| 888 | between the lowest and the highest address in that subnet. In the | - | ||||||||||||
| 889 | case of IP version 4, the lowest address is the network address, | - | ||||||||||||
| 890 | while the highest address is the broadcast address. | - | ||||||||||||
| 891 | - | |||||||||||||
| 892 | The \a subnet argument does not have to be the actual network | - | ||||||||||||
| 893 | address (the lowest address in the subnet). It can be any valid IP | - | ||||||||||||
| 894 | belonging to that subnet. In particular, if it is equal to the IP | - | ||||||||||||
| 895 | address held by this object, this function will always return true | - | ||||||||||||
| 896 | (provided the netmask is a valid value). | - | ||||||||||||
| 897 | - | |||||||||||||
| 898 | \sa parseSubnet() | - | ||||||||||||
| 899 | */ | - | ||||||||||||
| 900 | bool QHostAddress::isInSubnet(const QHostAddress &subnet, int netmask) const | - | ||||||||||||
| 901 | { | - | ||||||||||||
| 902 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 903 | if (subnet.protocol() != d->protocol || netmask < 0) | - | ||||||||||||
| 904 | return false; | - | ||||||||||||
| 905 | - | |||||||||||||
| 906 | union { | - | ||||||||||||
| 907 | quint32 ip; | - | ||||||||||||
| 908 | quint8 data[4]; | - | ||||||||||||
| 909 | } ip4, net4; | - | ||||||||||||
| 910 | const quint8 *ip; | - | ||||||||||||
| 911 | const quint8 *net; | - | ||||||||||||
| 912 | if (d->protocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
| 913 | if (netmask > 32) | - | ||||||||||||
| 914 | netmask = 32; | - | ||||||||||||
| 915 | ip4.ip = qToBigEndian(d->a); | - | ||||||||||||
| 916 | net4.ip = qToBigEndian(subnet.d->a); | - | ||||||||||||
| 917 | ip = ip4.data; | - | ||||||||||||
| 918 | net = net4.data; | - | ||||||||||||
| 919 | } else if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
| 920 | if (netmask > 128) | - | ||||||||||||
| 921 | netmask = 128; | - | ||||||||||||
| 922 | ip = d->a6.c; | - | ||||||||||||
| 923 | net = subnet.d->a6.c; | - | ||||||||||||
| 924 | } else { | - | ||||||||||||
| 925 | return false; | - | ||||||||||||
| 926 | } | - | ||||||||||||
| 927 | - | |||||||||||||
| 928 | if (netmask >= 8 && memcmp(ip, net, netmask / 8) != 0) | - | ||||||||||||
| 929 | return false; | - | ||||||||||||
| 930 | if ((netmask & 7) == 0) | - | ||||||||||||
| 931 | return true; | - | ||||||||||||
| 932 | - | |||||||||||||
| 933 | // compare the last octet now | - | ||||||||||||
| 934 | quint8 bytemask = 256 - (1 << (8 - (netmask & 7))); | - | ||||||||||||
| 935 | quint8 ipbyte = ip[netmask / 8]; | - | ||||||||||||
| 936 | quint8 netbyte = net[netmask / 8]; | - | ||||||||||||
| 937 | return (ipbyte & bytemask) == (netbyte & bytemask); | - | ||||||||||||
| 938 | } | - | ||||||||||||
| 939 | - | |||||||||||||
| 940 | /*! | - | ||||||||||||
| 941 | \since 4.5 | - | ||||||||||||
| 942 | \overload | - | ||||||||||||
| 943 | - | |||||||||||||
| 944 | Returns \c true if this IP is in the subnet described by \a | - | ||||||||||||
| 945 | subnet. The QHostAddress member of \a subnet contains the network | - | ||||||||||||
| 946 | prefix and the int (second) member contains the netmask (prefix | - | ||||||||||||
| 947 | length). | - | ||||||||||||
| 948 | */ | - | ||||||||||||
| 949 | bool QHostAddress::isInSubnet(const QPair<QHostAddress, int> &subnet) const | - | ||||||||||||
| 950 | { | - | ||||||||||||
| 951 | return isInSubnet(subnet.first, subnet.second); | - | ||||||||||||
| 952 | } | - | ||||||||||||
| 953 | - | |||||||||||||
| 954 | - | |||||||||||||
| 955 | /*! | - | ||||||||||||
| 956 | \since 4.5 | - | ||||||||||||
| 957 | - | |||||||||||||
| 958 | Parses the IP and subnet information contained in \a subnet and | - | ||||||||||||
| 959 | returns the network prefix for that network and its prefix length. | - | ||||||||||||
| 960 | - | |||||||||||||
| 961 | The IP address and the netmask must be separated by a slash | - | ||||||||||||
| 962 | (/). | - | ||||||||||||
| 963 | - | |||||||||||||
| 964 | This function supports arguments in the form: | - | ||||||||||||
| 965 | \list | - | ||||||||||||
| 966 | \li 123.123.123.123/n where n is any value between 0 and 32 | - | ||||||||||||
| 967 | \li 123.123.123.123/255.255.255.255 | - | ||||||||||||
| 968 | \li <ipv6-address>/n where n is any value between 0 and 128 | - | ||||||||||||
| 969 | \endlist | - | ||||||||||||
| 970 | - | |||||||||||||
| 971 | For IP version 4, this function accepts as well missing trailing | - | ||||||||||||
| 972 | components (i.e., less than 4 octets, like "192.168.1"), followed | - | ||||||||||||
| 973 | or not by a dot. If the netmask is also missing in that case, it | - | ||||||||||||
| 974 | is set to the number of octets actually passed (in the example | - | ||||||||||||
| 975 | above, it would be 24, for 3 octets). | - | ||||||||||||
| 976 | - | |||||||||||||
| 977 | \sa isInSubnet() | - | ||||||||||||
| 978 | */ | - | ||||||||||||
| 979 | QPair<QHostAddress, int> QHostAddress::parseSubnet(const QString &subnet) | - | ||||||||||||
| 980 | { | - | ||||||||||||
| 981 | // We support subnets in the form: | - | ||||||||||||
| 982 | // ddd.ddd.ddd.ddd/nn | - | ||||||||||||
| 983 | // ddd.ddd.ddd/nn | - | ||||||||||||
| 984 | // ddd.ddd/nn | - | ||||||||||||
| 985 | // ddd/nn | - | ||||||||||||
| 986 | // ddd.ddd.ddd. | - | ||||||||||||
| 987 | // ddd.ddd.ddd | - | ||||||||||||
| 988 | // ddd.ddd. | - | ||||||||||||
| 989 | // ddd.ddd | - | ||||||||||||
| 990 | // ddd. | - | ||||||||||||
| 991 | // ddd | - | ||||||||||||
| 992 | // <ipv6-address>/nn | - | ||||||||||||
| 993 | // | - | ||||||||||||
| 994 | // where nn can be an IPv4-style netmask for the IPv4 forms | - | ||||||||||||
| 995 | - | |||||||||||||
| 996 | const QPair<QHostAddress, int> invalid = qMakePair(QHostAddress(), -1); | - | ||||||||||||
| 997 | if (subnet.isEmpty())
| 1-85 | ||||||||||||
| 998 | return invalid; executed 1 time by 1 test: return invalid;Executed by:
| 1 | ||||||||||||
| 999 | - | |||||||||||||
| 1000 | int slash = subnet.indexOf(QLatin1Char('/')); | - | ||||||||||||
| 1001 | QString netStr = subnet; | - | ||||||||||||
| 1002 | if (slash != -1)
| 13-72 | ||||||||||||
| 1003 | netStr.truncate(slash); executed 72 times by 2 tests: netStr.truncate(slash);Executed by:
| 72 | ||||||||||||
| 1004 | - | |||||||||||||
| 1005 | int netmask = -1; | - | ||||||||||||
| 1006 | bool isIpv6 = netStr.contains(QLatin1Char(':')); | - | ||||||||||||
| 1007 | - | |||||||||||||
| 1008 | if (slash != -1) {
| 13-72 | ||||||||||||
| 1009 | // is the netmask given in IP-form or in bit-count form? | - | ||||||||||||
| 1010 | if (!isIpv6 && subnet.indexOf(QLatin1Char('.'), slash + 1) != -1) {
| 16-40 | ||||||||||||
| 1011 | // IP-style, convert it to bit-count form | - | ||||||||||||
| 1012 | QNetmaskAddress parser; | - | ||||||||||||
| 1013 | if (!parser.setAddress(subnet.mid(slash + 1)))
| 3-13 | ||||||||||||
| 1014 | return invalid; executed 3 times by 1 test: return invalid;Executed by:
| 3 | ||||||||||||
| 1015 | netmask = parser.prefixLength(); | - | ||||||||||||
| 1016 | } else { executed 13 times by 1 test: end of blockExecuted by:
| 13 | ||||||||||||
| 1017 | bool ok; | - | ||||||||||||
| 1018 | netmask = subnet.midmidRef(slash + 1).toUInt(&ok); | - | ||||||||||||
| 1019 | if (!ok)
| 4-52 | ||||||||||||
| 1020 | return invalid; // failed to parse the subnet executed 4 times by 1 test: return invalid;Executed by:
| 4 | ||||||||||||
| 1021 | } executed 52 times by 2 tests: end of blockExecuted by:
| 52 | ||||||||||||
| 1022 | } | - | ||||||||||||
| 1023 | - | |||||||||||||
| 1024 | if (isIpv6) {
| 30-48 | ||||||||||||
| 1025 | // looks like it's an IPv6 address | - | ||||||||||||
| 1026 | if (netmask > 128)
| 1-29 | ||||||||||||
| 1027 | return invalid; // invalid netmask executed 1 time by 1 test: return invalid;Executed by:
| 1 | ||||||||||||
| 1028 | if (netmask < 0)
| 1-28 | ||||||||||||
| 1029 | netmask = 128; executed 1 time by 1 test: netmask = 128;Executed by:
| 1 | ||||||||||||
| 1030 | - | |||||||||||||
| 1031 | QHostAddress net; | - | ||||||||||||
| 1032 | if (!net.setAddress(netStr))
| 1-28 | ||||||||||||
| 1033 | return invalid; // failed to parse the IP executed 1 time by 1 test: return invalid;Executed by:
| 1 | ||||||||||||
| 1034 | - | |||||||||||||
| 1035 | clearBits(net.d->a6.c, netmask, 128); | - | ||||||||||||
| 1036 | return qMakePair(net, netmask); executed 28 times by 2 tests: return qMakePair(net, netmask);Executed by:
| 28 | ||||||||||||
| 1037 | } | - | ||||||||||||
| 1038 | - | |||||||||||||
| 1039 | if (netmask > 32)
| 1-47 | ||||||||||||
| 1040 | return invalid; // invalid netmask executed 1 time by 1 test: return invalid;Executed by:
| 1 | ||||||||||||
| 1041 | - | |||||||||||||
| 1042 | // parse the address manually | - | ||||||||||||
| 1043 | QStringListauto parts = netStr.splitsplitRef(QLatin1Char('.')); | - | ||||||||||||
| 1044 | if (parts.isEmpty() || parts.count() > 4)
| 0-47 | ||||||||||||
| 1045 | return invalid; // invalid IPv4 address executed 2 times by 1 test: return invalid;Executed by:
| 2 | ||||||||||||
| 1046 | - | |||||||||||||
| 1047 | if (parts.lastconstLast().isEmpty())
| 2-43 | ||||||||||||
| 1048 | parts.removeLast(); executed 2 times by 1 test: parts.removeLast();Executed by:
| 2 | ||||||||||||
| 1049 | - | |||||||||||||
| 1050 | quint32 addr = 0; | - | ||||||||||||
| 1051 | for (int i = 0; i < parts.count(); ++i) {
| 42-154 | ||||||||||||
| 1052 | bool ok; | - | ||||||||||||
| 1053 | uint byteValue = parts.at(i).toUInt(&ok); | - | ||||||||||||
| 1054 | if (!ok || byteValue > 255)
| 0-151 | ||||||||||||
| 1055 | return invalid; // invalid IPv4 address executed 3 times by 1 test: return invalid;Executed by:
| 3 | ||||||||||||
| 1056 | - | |||||||||||||
| 1057 | addr <<= 8; | - | ||||||||||||
| 1058 | addr += byteValue; | - | ||||||||||||
| 1059 | } executed 151 times by 2 tests: end of blockExecuted by:
| 151 | ||||||||||||
| 1060 | addr <<= 8 * (4 - parts.count()); | - | ||||||||||||
| 1061 | if (netmask == -1) {
| 7-35 | ||||||||||||
| 1062 | netmask = 8 * parts.count(); | - | ||||||||||||
| 1063 | } else if (netmask == 0) { executed 7 times by 1 test: end of blockExecuted by:
| 3-32 | ||||||||||||
| 1064 | // special case here | - | ||||||||||||
| 1065 | // x86's instructions "shr" and "shl" do not operate when | - | ||||||||||||
| 1066 | // their argument is 32, so the code below doesn't work as expected | - | ||||||||||||
| 1067 | addr = 0; | - | ||||||||||||
| 1068 | } else if (netmask != 32) { executed 3 times by 1 test: end of blockExecuted by:
| 2-30 | ||||||||||||
| 1069 | // clear remaining bits | - | ||||||||||||
| 1070 | quint32 mask = quint32(0xffffffff) >> (32 - netmask) << (32 - netmask); | - | ||||||||||||
| 1071 | addr &= mask; | - | ||||||||||||
| 1072 | } executed 30 times by 2 tests: end of blockExecuted by:
| 30 | ||||||||||||
| 1073 | - | |||||||||||||
| 1074 | return qMakePair(QHostAddress(addr), netmask); executed 42 times by 2 tests: return qMakePair(QHostAddress(addr), netmask);Executed by:
| 42 | ||||||||||||
| 1075 | } | - | ||||||||||||
| 1076 | - | |||||||||||||
| 1077 | /*! | - | ||||||||||||
| 1078 | \since 5.0 | - | ||||||||||||
| 1079 | - | |||||||||||||
| 1080 | returns \c true if the address is the IPv6 loopback address, or any | - | ||||||||||||
| 1081 | of the IPv4 loopback addresses. | - | ||||||||||||
| 1082 | */ | - | ||||||||||||
| 1083 | bool QHostAddress::isLoopback() const | - | ||||||||||||
| 1084 | { | - | ||||||||||||
| 1085 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 1086 | if ((d->a & 0xFF000000) == 0x7F000000) | - | ||||||||||||
| 1087 | return true; // v4 range (including IPv6 wrapped IPv4 addresses) | - | ||||||||||||
| 1088 | if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
| 1089 | #ifdef __SSE2__ | - | ||||||||||||
| 1090 | const __m128i loopback = _mm_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); | - | ||||||||||||
| 1091 | __m128i ipv6 = _mm_loadu_si128((const __m128i *)d->a6.c); | - | ||||||||||||
| 1092 | __m128i cmp = _mm_cmpeq_epi8(ipv6, loopback); | - | ||||||||||||
| 1093 | return _mm_movemask_epi8(cmp) == 0xffff; | - | ||||||||||||
| 1094 | #else | - | ||||||||||||
| 1095 | if (d->a6_64.c[0] != 0 || qFromBigEndian(d->a6_64.c[1]) != 1) | - | ||||||||||||
| 1096 | return false; | - | ||||||||||||
| 1097 | #endif | - | ||||||||||||
| 1098 | return true; dead code: return true; | - | ||||||||||||
| 1099 | } | - | ||||||||||||
| 1100 | return false; | - | ||||||||||||
| 1101 | } | - | ||||||||||||
| 1102 | - | |||||||||||||
| 1103 | /*! | - | ||||||||||||
| 1104 | \since 5.6 | - | ||||||||||||
| 1105 | - | |||||||||||||
| 1106 | Returns \c true if the address is an IPv4 or IPv6 multicast address, \c | - | ||||||||||||
| 1107 | false otherwise. | - | ||||||||||||
| 1108 | */ | - | ||||||||||||
| 1109 | bool QHostAddress::isMulticast() const | - | ||||||||||||
| 1110 | { | - | ||||||||||||
| 1111 | QT_ENSURE_PARSED(this); | - | ||||||||||||
| 1112 | if ((d->a & 0xF0000000) == 0xE0000000) | - | ||||||||||||
| 1113 | return true; // 224.0.0.0-239.255.255.255 (including v4-mapped IPv6 addresses) | - | ||||||||||||
| 1114 | if (d->protocol == QAbstractSocket::IPv6Protocol) | - | ||||||||||||
| 1115 | return d->a6.c[0] == 0xff; | - | ||||||||||||
| 1116 | return false; | - | ||||||||||||
| 1117 | } | - | ||||||||||||
| 1118 | - | |||||||||||||
| 1119 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||
| 1120 | QDebug operator<<(QDebug d, const QHostAddress &address) | - | ||||||||||||
| 1121 | { | - | ||||||||||||
| 1122 | QDebugStateSaver saver(d); | - | ||||||||||||
| 1123 | d.resetFormat().nospace(); | - | ||||||||||||
| 1124 | if (address == QHostAddress::Any) | - | ||||||||||||
| 1125 | d << "QHostAddress(QHostAddress::Any)"; | - | ||||||||||||
| 1126 | else | - | ||||||||||||
| 1127 | d << "QHostAddress(" << address.toString() << ')'; | - | ||||||||||||
| 1128 | return d; | - | ||||||||||||
| 1129 | } | - | ||||||||||||
| 1130 | #endif | - | ||||||||||||
| 1131 | - | |||||||||||||
| 1132 | uint qHash(const QHostAddress &key, uint seed) | - | ||||||||||||
| 1133 | { | - | ||||||||||||
| 1134 | // both lines might throw | - | ||||||||||||
| 1135 | QT_ENSURE_PARSED(&key); | - | ||||||||||||
| 1136 | return qHashBits(key.d->a6.c, 16, seed); | - | ||||||||||||
| 1137 | } | - | ||||||||||||
| 1138 | - | |||||||||||||
| 1139 | #ifndef QT_NO_DATASTREAM | - | ||||||||||||
| 1140 | - | |||||||||||||
| 1141 | /*! \relates QHostAddress | - | ||||||||||||
| 1142 | - | |||||||||||||
| 1143 | Writes host address \a address to the stream \a out and returns a reference | - | ||||||||||||
| 1144 | to the stream. | - | ||||||||||||
| 1145 | - | |||||||||||||
| 1146 | \sa {Serializing Qt Data Types} | - | ||||||||||||
| 1147 | */ | - | ||||||||||||
| 1148 | QDataStream &operator<<(QDataStream &out, const QHostAddress &address) | - | ||||||||||||
| 1149 | { | - | ||||||||||||
| 1150 | qint8 prot; | - | ||||||||||||
| 1151 | prot = qint8(address.protocol()); | - | ||||||||||||
| 1152 | out << prot; | - | ||||||||||||
| 1153 | switch (address.protocol()) { | - | ||||||||||||
| 1154 | case QAbstractSocket::UnknownNetworkLayerProtocol: | - | ||||||||||||
| 1155 | case QAbstractSocket::AnyIPProtocol: | - | ||||||||||||
| 1156 | break; | - | ||||||||||||
| 1157 | case QAbstractSocket::IPv4Protocol: | - | ||||||||||||
| 1158 | out << address.toIPv4Address(); | - | ||||||||||||
| 1159 | break; | - | ||||||||||||
| 1160 | case QAbstractSocket::IPv6Protocol: | - | ||||||||||||
| 1161 | { | - | ||||||||||||
| 1162 | Q_IPV6ADDR ipv6 = address.toIPv6Address(); | - | ||||||||||||
| 1163 | for (int i = 0; i < 16; ++i) | - | ||||||||||||
| 1164 | out << ipv6[i]; | - | ||||||||||||
| 1165 | out << address.scopeId(); | - | ||||||||||||
| 1166 | } | - | ||||||||||||
| 1167 | break; | - | ||||||||||||
| 1168 | } | - | ||||||||||||
| 1169 | return out; | - | ||||||||||||
| 1170 | } | - | ||||||||||||
| 1171 | - | |||||||||||||
| 1172 | /*! \relates QHostAddress | - | ||||||||||||
| 1173 | - | |||||||||||||
| 1174 | Reads a host address into \a address from the stream \a in and returns a | - | ||||||||||||
| 1175 | reference to the stream. | - | ||||||||||||
| 1176 | - | |||||||||||||
| 1177 | \sa {Serializing Qt Data Types} | - | ||||||||||||
| 1178 | */ | - | ||||||||||||
| 1179 | QDataStream &operator>>(QDataStream &in, QHostAddress &address) | - | ||||||||||||
| 1180 | { | - | ||||||||||||
| 1181 | qint8 prot; | - | ||||||||||||
| 1182 | in >> prot; | - | ||||||||||||
| 1183 | switch (QAbstractSocket::NetworkLayerProtocol(prot)) { | - | ||||||||||||
| 1184 | case QAbstractSocket::UnknownNetworkLayerProtocol: | - | ||||||||||||
| 1185 | address.clear(); | - | ||||||||||||
| 1186 | break; | - | ||||||||||||
| 1187 | case QAbstractSocket::IPv4Protocol: | - | ||||||||||||
| 1188 | { | - | ||||||||||||
| 1189 | quint32 ipv4; | - | ||||||||||||
| 1190 | in >> ipv4; | - | ||||||||||||
| 1191 | address.setAddress(ipv4); | - | ||||||||||||
| 1192 | } | - | ||||||||||||
| 1193 | break; | - | ||||||||||||
| 1194 | case QAbstractSocket::IPv6Protocol: | - | ||||||||||||
| 1195 | { | - | ||||||||||||
| 1196 | Q_IPV6ADDR ipv6; | - | ||||||||||||
| 1197 | for (int i = 0; i < 16; ++i) | - | ||||||||||||
| 1198 | in >> ipv6[i]; | - | ||||||||||||
| 1199 | address.setAddress(ipv6); | - | ||||||||||||
| 1200 | - | |||||||||||||
| 1201 | QString scope; | - | ||||||||||||
| 1202 | in >> scope; | - | ||||||||||||
| 1203 | address.setScopeId(scope); | - | ||||||||||||
| 1204 | } | - | ||||||||||||
| 1205 | break; | - | ||||||||||||
| 1206 | case QAbstractSocket::AnyIPProtocol: | - | ||||||||||||
| 1207 | address = QHostAddress::Any; | - | ||||||||||||
| 1208 | break; | - | ||||||||||||
| 1209 | default: | - | ||||||||||||
| 1210 | address.clear(); | - | ||||||||||||
| 1211 | in.setStatus(QDataStream::ReadCorruptData); | - | ||||||||||||
| 1212 | } | - | ||||||||||||
| 1213 | return in; | - | ||||||||||||
| 1214 | } | - | ||||||||||||
| 1215 | - | |||||||||||||
| 1216 | #endif //QT_NO_DATASTREAM | - | ||||||||||||
| 1217 | - | |||||||||||||
| 1218 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |