Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/kernel/qhostaddress.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | - | |||||||||||||
6 | - | |||||||||||||
7 | - | |||||||||||||
8 | - | |||||||||||||
9 | - | |||||||||||||
10 | - | |||||||||||||
11 | - | |||||||||||||
12 | - | |||||||||||||
13 | - | |||||||||||||
14 | - | |||||||||||||
15 | class QHostAddressPrivate | - | ||||||||||||
16 | { | - | ||||||||||||
17 | public: | - | ||||||||||||
18 | QHostAddressPrivate(); | - | ||||||||||||
19 | - | |||||||||||||
20 | void setAddress(quint32 a_ = 0); | - | ||||||||||||
21 | void setAddress(const quint8 *a_); | - | ||||||||||||
22 | void setAddress(const Q_IPV6ADDR &a_); | - | ||||||||||||
23 | - | |||||||||||||
24 | bool parse(); | - | ||||||||||||
25 | void clear(); | - | ||||||||||||
26 | - | |||||||||||||
27 | QString ipString; | - | ||||||||||||
28 | QString scopeId; | - | ||||||||||||
29 | - | |||||||||||||
30 | quint32 a;union { | - | ||||||||||||
31 | Q_IPV6ADDR a6; | - | ||||||||||||
32 | struct { quint64 c[2]; } a6_64; | - | ||||||||||||
33 | struct { quint32 c[4]; } a6_32; | - | ||||||||||||
34 | }; | - | ||||||||||||
35 | QAbstractSocket::NetworkLayerProtocolquint32 a; | - | ||||||||||||
36 | qint8 protocol; | - | ||||||||||||
37 | bool isParsed; | - | ||||||||||||
38 | - | |||||||||||||
39 | friend class QHostAddress; | - | ||||||||||||
40 | }; | - | ||||||||||||
41 | - | |||||||||||||
42 | QHostAddressPrivate::QHostAddressPrivate() | - | ||||||||||||
43 | : a(0), protocol(QAbstractSocket::UnknownNetworkLayerProtocol), isParsed(true) | - | ||||||||||||
44 | { | - | ||||||||||||
45 | memset(&a6, 0, sizeof(a6)); | - | ||||||||||||
46 | } | - | ||||||||||||
47 | - | |||||||||||||
48 | void QHostAddressPrivate::setAddress(quint32 a_) | - | ||||||||||||
49 | { | - | ||||||||||||
50 | a = a_; | - | ||||||||||||
51 | protocol = QAbstractSocket::IPv4Protocol; | - | ||||||||||||
52 | isParsed = true; | - | ||||||||||||
53 | - | |||||||||||||
54 | - | |||||||||||||
55 | a6_64.c[0] = 0; | - | ||||||||||||
56 | if (a) { | - | ||||||||||||
57 | a6_32.c[2] = qToBigEndian(0xffff); | - | ||||||||||||
58 | a6_32.c[3] = qToBigEndian(a); | - | ||||||||||||
59 | } else { | - | ||||||||||||
60 | a6_64.c[1] = 0; | - | ||||||||||||
61 | } | - | ||||||||||||
62 | } | - | ||||||||||||
63 | - | |||||||||||||
64 | - | |||||||||||||
65 | - | |||||||||||||
66 | static bool convertToIpv4(quint32& a, const Q_IPV6ADDR &a6) | - | ||||||||||||
67 | { | - | ||||||||||||
68 | const uchar *ptr = a6.c; | - | ||||||||||||
69 | if (qFromUnaligned<quint64>(ptr) != 0) | - | ||||||||||||
70 | return false; | - | ||||||||||||
71 | if (qFromBigEndian<quint32>(ptr + 8) == 0) { | - | ||||||||||||
72 | - | |||||||||||||
73 | a = 0; | - | ||||||||||||
74 | return qFromBigEndian<quint32>(ptr + 12) == 0; | - | ||||||||||||
75 | } | - | ||||||||||||
76 | if (qFromBigEndian<quint32>(ptr + 8) != 0xFFFF) | - | ||||||||||||
77 | return false; | - | ||||||||||||
78 | a = qFromBigEndian<quint32>(ptr + 12); | - | ||||||||||||
79 | return true; | - | ||||||||||||
80 | } | - | ||||||||||||
81 | - | |||||||||||||
82 | void QHostAddressPrivate::setAddress(const quint8 *a_) | - | ||||||||||||
83 | { | - | ||||||||||||
84 | protocol = QAbstractSocket::IPv6Protocol; | - | ||||||||||||
85 | isParsed = true; | - | ||||||||||||
86 | memcpy(a6.c, a_, sizeof(a6)); | - | ||||||||||||
87 | a = 0; | - | ||||||||||||
88 | convertToIpv4(a, a6); | - | ||||||||||||
89 | } | - | ||||||||||||
90 | - | |||||||||||||
91 | void QHostAddressPrivate::setAddress(const Q_IPV6ADDR &a_) | - | ||||||||||||
92 | { | - | ||||||||||||
93 | setAddress(a_.c); | - | ||||||||||||
94 | } | - | ||||||||||||
95 | - | |||||||||||||
96 | static bool parseIp6(const QString &address, QIPAddressUtils::IPv6Address &addr, QString *scopeId) | - | ||||||||||||
97 | { | - | ||||||||||||
98 | QString tmp = address; | - | ||||||||||||
99 | int scopeIdPos = tmp.lastIndexOf(QLatin1Char('%')); | - | ||||||||||||
100 | if (scopeIdPos != -1) { | - | ||||||||||||
101 | *scopeId = tmp.mid(scopeIdPos + 1); | - | ||||||||||||
102 | tmp.chop(tmp.size() - scopeIdPos); | - | ||||||||||||
103 | } else { | - | ||||||||||||
104 | scopeId->clear(); | - | ||||||||||||
105 | } | - | ||||||||||||
106 | return QIPAddressUtils::parseIp6(addr, tmp.constBegin(), tmp.constEnd()) == 0; | - | ||||||||||||
107 | } | - | ||||||||||||
108 | - | |||||||||||||
109 | __attribute__((noinline)) bool QHostAddressPrivate::parse() | - | ||||||||||||
110 | { | - | ||||||||||||
111 | isParsed = true; | - | ||||||||||||
112 | protocol = QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
113 | QString a = ipString.simplified(); | - | ||||||||||||
114 | if (a.isEmpty()) | - | ||||||||||||
115 | return false; | - | ||||||||||||
116 | - | |||||||||||||
117 | - | |||||||||||||
118 | if (a.contains(QLatin1Char(':'))) { | - | ||||||||||||
119 | quint8 maybeIp6[16]; | - | ||||||||||||
120 | if (parseIp6(a, maybeIp6, &scopeId)) { | - | ||||||||||||
121 | setAddress(maybeIp6); | - | ||||||||||||
122 | return true; | - | ||||||||||||
123 | } | - | ||||||||||||
124 | } | - | ||||||||||||
125 | - | |||||||||||||
126 | quint32 maybeIp4 = 0; | - | ||||||||||||
127 | if (QIPAddressUtils::parseIp4(maybeIp4, a.constBegin(), a.constEnd())) { | - | ||||||||||||
128 | setAddress(maybeIp4); | - | ||||||||||||
129 | return true; | - | ||||||||||||
130 | } | - | ||||||||||||
131 | - | |||||||||||||
132 | return false; | - | ||||||||||||
133 | } | - | ||||||||||||
134 | - | |||||||||||||
135 | void QHostAddressPrivate::clear() | - | ||||||||||||
136 | { | - | ||||||||||||
137 | a = 0; | - | ||||||||||||
138 | protocol = QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
139 | isParsed = true; | - | ||||||||||||
140 | memset(&a6, 0, sizeof(a6)); | - | ||||||||||||
141 | } | - | ||||||||||||
142 | - | |||||||||||||
143 | - | |||||||||||||
144 | bool QNetmaskAddress::setAddress(const QString &address) | - | ||||||||||||
145 | { | - | ||||||||||||
146 | length = -1; | - | ||||||||||||
147 | QHostAddress other; | - | ||||||||||||
148 | return other.setAddress(address) && setAddress(other); | - | ||||||||||||
149 | } | - | ||||||||||||
150 | - | |||||||||||||
151 | bool QNetmaskAddress::setAddress(const QHostAddress &address) | - | ||||||||||||
152 | { | - | ||||||||||||
153 | static const quint8 zeroes[16] = { 0 }; | - | ||||||||||||
154 | union { | - | ||||||||||||
155 | quint32 v4; | - | ||||||||||||
156 | quint8 v6[16]; | - | ||||||||||||
157 | } ip; | - | ||||||||||||
158 | - | |||||||||||||
159 | int netmask = 0; | - | ||||||||||||
160 | quint8 *ptr = ip.v6; | - | ||||||||||||
161 | quint8 *end; | - | ||||||||||||
162 | length = -1; | - | ||||||||||||
163 | - | |||||||||||||
164 | QHostAddress::operator=(address); | - | ||||||||||||
165 | - | |||||||||||||
166 | if (d->protocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
167 | ip.v4 = qToBigEndian(d->a); | - | ||||||||||||
168 | end = ptr + 4; | - | ||||||||||||
169 | } else if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
170 | memcpy(ip.v6, d->a6.c, 16); | - | ||||||||||||
171 | end = ptr + 16; | - | ||||||||||||
172 | } else { | - | ||||||||||||
173 | d->clear(); | - | ||||||||||||
174 | return false; | - | ||||||||||||
175 | } | - | ||||||||||||
176 | - | |||||||||||||
177 | while (ptr < end) { | - | ||||||||||||
178 | switch (*ptr) { | - | ||||||||||||
179 | case 255: | - | ||||||||||||
180 | netmask += 8; | - | ||||||||||||
181 | ++ptr; | - | ||||||||||||
182 | continue; | - | ||||||||||||
183 | - | |||||||||||||
184 | default: | - | ||||||||||||
185 | d->clear(); | - | ||||||||||||
186 | return false; | - | ||||||||||||
187 | - | |||||||||||||
188 | - | |||||||||||||
189 | case 254: | - | ||||||||||||
190 | ++netmask; | - | ||||||||||||
191 | case 252: | - | ||||||||||||
192 | ++netmask; | - | ||||||||||||
193 | case 248: | - | ||||||||||||
194 | ++netmask; | - | ||||||||||||
195 | case 240: | - | ||||||||||||
196 | ++netmask; | - | ||||||||||||
197 | case 224: | - | ||||||||||||
198 | ++netmask; | - | ||||||||||||
199 | case 192: | - | ||||||||||||
200 | ++netmask; | - | ||||||||||||
201 | case 128: | - | ||||||||||||
202 | ++netmask; | - | ||||||||||||
203 | case 0: | - | ||||||||||||
204 | break; | - | ||||||||||||
205 | } | - | ||||||||||||
206 | break; | - | ||||||||||||
207 | } | - | ||||||||||||
208 | - | |||||||||||||
209 | - | |||||||||||||
210 | if (ptr < end && memcmp(ptr + 1, zeroes, end - ptr - 1) != 0) { | - | ||||||||||||
211 | d->clear(); | - | ||||||||||||
212 | return false; | - | ||||||||||||
213 | } | - | ||||||||||||
214 | - | |||||||||||||
215 | length = netmask; | - | ||||||||||||
216 | return true; | - | ||||||||||||
217 | } | - | ||||||||||||
218 | - | |||||||||||||
219 | static void clearBits(quint8 *where, int start, int end) | - | ||||||||||||
220 | { | - | ||||||||||||
221 | ((!(end == 32 || end == 128)) ? qt_assert("end == 32 || end == 128",__FILE__,303309) : qt_noop()); | - | ||||||||||||
222 | if (start == end) | - | ||||||||||||
223 | return; | - | ||||||||||||
224 | - | |||||||||||||
225 | - | |||||||||||||
226 | quint8 bytemask = 256 - (1 << (8 - (start & 7))); | - | ||||||||||||
227 | where[start / 8] &= bytemask; | - | ||||||||||||
228 | - | |||||||||||||
229 | - | |||||||||||||
230 | memset(where + (start + 7) / 8, 0, end / 8 - (start + 7) / 8); | - | ||||||||||||
231 | } | - | ||||||||||||
232 | - | |||||||||||||
233 | int QNetmaskAddress::prefixLength() const | - | ||||||||||||
234 | { | - | ||||||||||||
235 | return length; | - | ||||||||||||
236 | } | - | ||||||||||||
237 | - | |||||||||||||
238 | void QNetmaskAddress::setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int newLength) | - | ||||||||||||
239 | { | - | ||||||||||||
240 | length = newLength; | - | ||||||||||||
241 | if (length < 0 || length > (proto == QAbstractSocket::IPv4Protocol ? 32 : | - | ||||||||||||
242 | proto == QAbstractSocket::IPv6Protocol ? 128 : -1)) { | - | ||||||||||||
243 | - | |||||||||||||
244 | d->protocol = QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
245 | length = -1; | - | ||||||||||||
246 | return; | - | ||||||||||||
247 | } | - | ||||||||||||
248 | - | |||||||||||||
249 | d->protocol = proto; | - | ||||||||||||
250 | if (d->protocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
251 | if (length == 0) { | - | ||||||||||||
252 | d->a = 0; | - | ||||||||||||
253 | } else if (length == 32) { | - | ||||||||||||
254 | d->a = quint32(0xffffffff); | - | ||||||||||||
255 | } else { | - | ||||||||||||
256 | d->a = quint32(0xffffffff) >> (32 - length) << (32 - length); | - | ||||||||||||
257 | } | - | ||||||||||||
258 | } else { | - | ||||||||||||
259 | memset(d->a6.c, 0xFF, sizeof(d->a6)); | - | ||||||||||||
260 | clearBits(d->a6.c, length, 128); | - | ||||||||||||
261 | } | - | ||||||||||||
262 | } | - | ||||||||||||
263 | QHostAddress::QHostAddress() | - | ||||||||||||
264 | : d(new QHostAddressPrivate) | - | ||||||||||||
265 | { | - | ||||||||||||
266 | } | - | ||||||||||||
267 | - | |||||||||||||
268 | - | |||||||||||||
269 | - | |||||||||||||
270 | - | |||||||||||||
271 | QHostAddress::QHostAddress(quint32 ip4Addr) | - | ||||||||||||
272 | : d(new QHostAddressPrivate) | - | ||||||||||||
273 | { | - | ||||||||||||
274 | setAddress(ip4Addr); | - | ||||||||||||
275 | } | - | ||||||||||||
276 | - | |||||||||||||
277 | - | |||||||||||||
278 | - | |||||||||||||
279 | - | |||||||||||||
280 | - | |||||||||||||
281 | - | |||||||||||||
282 | - | |||||||||||||
283 | QHostAddress::QHostAddress(quint8 *ip6Addr) | - | ||||||||||||
284 | : d(new QHostAddressPrivate) | - | ||||||||||||
285 | { | - | ||||||||||||
286 | setAddress(ip6Addr); | - | ||||||||||||
287 | } | - | ||||||||||||
288 | QHostAddress::QHostAddress(const quint8 *ip6Addr) | - | ||||||||||||
289 | : d(new QHostAddressPrivate) | - | ||||||||||||
290 | { | - | ||||||||||||
291 | setAddress(ip6Addr); | - | ||||||||||||
292 | } | - | ||||||||||||
293 | - | |||||||||||||
294 | - | |||||||||||||
295 | - | |||||||||||||
296 | - | |||||||||||||
297 | QHostAddress::QHostAddress(const Q_IPV6ADDR &ip6Addr) | - | ||||||||||||
298 | : d(new QHostAddressPrivate) | - | ||||||||||||
299 | { | - | ||||||||||||
300 | setAddress(ip6Addr); | - | ||||||||||||
301 | } | - | ||||||||||||
302 | - | |||||||||||||
303 | - | |||||||||||||
304 | - | |||||||||||||
305 | - | |||||||||||||
306 | - | |||||||||||||
307 | - | |||||||||||||
308 | - | |||||||||||||
309 | QHostAddress::QHostAddress(const QString &address) | - | ||||||||||||
310 | : d(new QHostAddressPrivate) | - | ||||||||||||
311 | { | - | ||||||||||||
312 | d->ipString = address; | - | ||||||||||||
313 | d->isParsed = false; | - | ||||||||||||
314 | } | - | ||||||||||||
315 | QHostAddress::QHostAddress(const struct sockaddr *sockaddr) | - | ||||||||||||
316 | : d(new QHostAddressPrivate) | - | ||||||||||||
317 | { | - | ||||||||||||
318 | - | |||||||||||||
319 | if (sockaddr->sa_family == 2) | - | ||||||||||||
320 | setAddress(htonl(((const sockaddr_in *)sockaddr)->sin_addr.s_addr)); | - | ||||||||||||
321 | else if (sockaddr->sa_family == 10) | - | ||||||||||||
322 | setAddress(((const sockaddr_in6 *)sockaddr)->sin6_addr.__in6_u.__u6_addr8); | - | ||||||||||||
323 | - | |||||||||||||
324 | - | |||||||||||||
325 | - | |||||||||||||
326 | } | - | ||||||||||||
327 | - | |||||||||||||
328 | - | |||||||||||||
329 | - | |||||||||||||
330 | - | |||||||||||||
331 | QHostAddress::QHostAddress(const QHostAddress &address) | - | ||||||||||||
332 | : d(new QHostAddressPrivate(*address.d.data())) | - | ||||||||||||
333 | { | - | ||||||||||||
334 | } | - | ||||||||||||
335 | - | |||||||||||||
336 | - | |||||||||||||
337 | - | |||||||||||||
338 | - | |||||||||||||
339 | QHostAddress::QHostAddress(SpecialAddress address) | - | ||||||||||||
340 | : d(new QHostAddressPrivate) | - | ||||||||||||
341 | { | - | ||||||||||||
342 | Q_IPV6ADDR ip6; | - | ||||||||||||
343 | memset(&ip6, 0, sizeof ip6); | - | ||||||||||||
344 | quint32 ip4 = ((in_addr_t) 0x00000000); | - | ||||||||||||
345 | - | |||||||||||||
346 | switch (address) { | - | ||||||||||||
347 | case Null: | - | ||||||||||||
348 | return; | - | ||||||||||||
349 | - | |||||||||||||
350 | case Broadcast: | - | ||||||||||||
351 | ip4 = ((in_addr_t) 0xffffffff); | - | ||||||||||||
352 | break; | - | ||||||||||||
353 | case LocalHost: | - | ||||||||||||
354 | ip4 = ((in_addr_t) 0x7f000001); | - | ||||||||||||
355 | break; | - | ||||||||||||
356 | case AnyIPv4: | - | ||||||||||||
357 | break; | - | ||||||||||||
358 | - | |||||||||||||
359 | case LocalHostIPv6: | - | ||||||||||||
360 | ip6[15] = 1; | - | ||||||||||||
361 | - | |||||||||||||
362 | case AnyIPv6: | - | ||||||||||||
363 | d->setAddress(ip6); | - | ||||||||||||
364 | return; | - | ||||||||||||
365 | - | |||||||||||||
366 | case Any: | - | ||||||||||||
367 | d->protocol = QAbstractSocket::AnyIPProtocol; | - | ||||||||||||
368 | return; | - | ||||||||||||
369 | } | - | ||||||||||||
370 | - | |||||||||||||
371 | - | |||||||||||||
372 | d->setAddress(ip4); | - | ||||||||||||
373 | } | - | ||||||||||||
374 | - | |||||||||||||
375 | - | |||||||||||||
376 | - | |||||||||||||
377 | - | |||||||||||||
378 | QHostAddress::~QHostAddress() | - | ||||||||||||
379 | { | - | ||||||||||||
380 | } | - | ||||||||||||
381 | - | |||||||||||||
382 | - | |||||||||||||
383 | - | |||||||||||||
384 | - | |||||||||||||
385 | - | |||||||||||||
386 | QHostAddress &QHostAddress::operator=(const QHostAddress &address) | - | ||||||||||||
387 | { | - | ||||||||||||
388 | *d.data() = *address.d.data(); | - | ||||||||||||
389 | return *this; | - | ||||||||||||
390 | } | - | ||||||||||||
391 | - | |||||||||||||
392 | - | |||||||||||||
393 | - | |||||||||||||
394 | - | |||||||||||||
395 | - | |||||||||||||
396 | - | |||||||||||||
397 | - | |||||||||||||
398 | QHostAddress &QHostAddress::operator=(const QString &address) | - | ||||||||||||
399 | { | - | ||||||||||||
400 | setAddress(address); | - | ||||||||||||
401 | return *this; | - | ||||||||||||
402 | } | - | ||||||||||||
403 | void QHostAddress::clear() | - | ||||||||||||
404 | { | - | ||||||||||||
405 | d->clear(); | - | ||||||||||||
406 | } | - | ||||||||||||
407 | - | |||||||||||||
408 | - | |||||||||||||
409 | - | |||||||||||||
410 | - | |||||||||||||
411 | void QHostAddress::setAddress(quint32 ip4Addr) | - | ||||||||||||
412 | { | - | ||||||||||||
413 | d->setAddress(ip4Addr); | - | ||||||||||||
414 | } | - | ||||||||||||
415 | void QHostAddress::setAddress(quint8 *ip6Addr) | - | ||||||||||||
416 | { | - | ||||||||||||
417 | d->setAddress(ip6Addr); | - | ||||||||||||
418 | } | - | ||||||||||||
419 | void QHostAddress::setAddress(const quint8 *ip6Addr) | - | ||||||||||||
420 | { | - | ||||||||||||
421 | d->setAddress(ip6Addr); | - | ||||||||||||
422 | } | - | ||||||||||||
423 | - | |||||||||||||
424 | - | |||||||||||||
425 | - | |||||||||||||
426 | - | |||||||||||||
427 | - | |||||||||||||
428 | - | |||||||||||||
429 | void QHostAddress::setAddress(const Q_IPV6ADDR &ip6Addr) | - | ||||||||||||
430 | { | - | ||||||||||||
431 | d->setAddress(ip6Addr); | - | ||||||||||||
432 | } | - | ||||||||||||
433 | bool QHostAddress::setAddress(const QString &address) | - | ||||||||||||
434 | { | - | ||||||||||||
435 | d->ipString = address; | - | ||||||||||||
436 | return d->parse(); | - | ||||||||||||
437 | } | - | ||||||||||||
438 | void QHostAddress::setAddress(const struct sockaddr *sockaddr) | - | ||||||||||||
439 | { | - | ||||||||||||
440 | - | |||||||||||||
441 | clear(); | - | ||||||||||||
442 | if (sockaddr->sa_family == 2) | - | ||||||||||||
443 | setAddress(htonl(((const sockaddr_in *)sockaddr)->sin_addr.s_addr)); | - | ||||||||||||
444 | else if (sockaddr->sa_family == 10) | - | ||||||||||||
445 | setAddress(((const sockaddr_in6 *)sockaddr)->sin6_addr.__in6_u.__u6_addr8); | - | ||||||||||||
446 | - | |||||||||||||
447 | - | |||||||||||||
448 | - | |||||||||||||
449 | } | - | ||||||||||||
450 | quint32 QHostAddress::toIPv4Address() const | - | ||||||||||||
451 | { | - | ||||||||||||
452 | return toIPv4Address(nullptr); | - | ||||||||||||
453 | } | - | ||||||||||||
454 | quint32 QHostAddress::toIPv4Address(bool *ok) const | - | ||||||||||||
455 | { | - | ||||||||||||
456 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
457 | quint32 dummy; | - | ||||||||||||
458 | if (ok) | - | ||||||||||||
459 | *ok = d->protocol == QAbstractSocket::IPv4Protocol || d->protocol == QAbstractSocket::AnyIPProtocol | - | ||||||||||||
460 | || (d->protocol == QAbstractSocket::IPv6Protocol && convertToIpv4(dummy, d->a6)); | - | ||||||||||||
461 | return d->a; | - | ||||||||||||
462 | } | - | ||||||||||||
463 | - | |||||||||||||
464 | - | |||||||||||||
465 | - | |||||||||||||
466 | - | |||||||||||||
467 | QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol() const | - | ||||||||||||
468 | { | - | ||||||||||||
469 | do { if (!(this)->d->isParsed
executed 286 times by 12 tests: this)->d->parse();(this)->d->parse(); Executed by:
executed 286 times by 12 tests: } while (0);(this)->d->parse(); Executed by:
| 286-40148 | ||||||||||||
470 | return executed 40434 times by 39 tests: QAbstractSocket::NetworkLayerProtocol(d->protocol;);return QAbstractSocket::NetworkLayerProtocol(d->protocol); Executed by:
executed 40434 times by 39 tests: return QAbstractSocket::NetworkLayerProtocol(d->protocol); Executed by:
| 40434 | ||||||||||||
471 | } | - | ||||||||||||
472 | Q_IPV6ADDR QHostAddress::toIPv6Address() const | - | ||||||||||||
473 | { | - | ||||||||||||
474 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
475 | return d->a6; | - | ||||||||||||
476 | } | - | ||||||||||||
477 | QString QHostAddress::toString() const | - | ||||||||||||
478 | { | - | ||||||||||||
479 | do { if (!(this)->d->isParsed
executed 3 times by 2 tests: this)->d->parse();(this)->d->parse(); Executed by:
executed 3 times by 2 tests: } while (0);(this)->d->parse(); Executed by:
| 3-2970 | ||||||||||||
480 | QString s; | - | ||||||||||||
481 | if (d->protocol == QAbstractSocket::IPv4Protocol
| 487-2486 | ||||||||||||
482 | || d->protocol == QAbstractSocket::AnyIPProtocol
| 23-464 | ||||||||||||
483 | quint32 i = toIPv4Address(); | - | ||||||||||||
484 | QString s;QIPAddressUtils::toString(s, i); | - | ||||||||||||
485 | return s;} executed 2509 times by 25 tests: else if (d->protocol == QAbstractSocket::IPv6Protocolend of block Executed by:
| 181-2509 | ||||||||||||
486 | QString s;QIPAddressUtils::toString(s, d->a6.c); | - | ||||||||||||
487 | if (!d->scopeId.isEmpty()
| 33-250 | ||||||||||||
488 | s.append(QLatin1Char('%') + d->scopeId); executed 33 times by 5 tests: s.append(QLatin1Char('%') + d->scopeId); Executed by:
| 33 | ||||||||||||
489 | } executed 283 times by 10 tests: end of block Executed by:
| 283 | ||||||||||||
490 | return executed 2973 times by 25 tests: s;return s; Executed by:
executed 2973 times by 25 tests: return s; Executed by:
| 2973 | ||||||||||||
} | ||||||||||||||
return QString();} | ||||||||||||||
492 | QString QHostAddress::scopeId() const | - | ||||||||||||
493 | { | - | ||||||||||||
494 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
495 | return (d->protocol == QAbstractSocket::IPv6Protocol) ? d->scopeId : QString(); | - | ||||||||||||
496 | } | - | ||||||||||||
497 | void QHostAddress::setScopeId(const QString &id) | - | ||||||||||||
498 | { | - | ||||||||||||
499 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
500 | if (d->protocol == QAbstractSocket::IPv6Protocol) | - | ||||||||||||
501 | d->scopeId = id; | - | ||||||||||||
502 | } | - | ||||||||||||
503 | - | |||||||||||||
504 | - | |||||||||||||
505 | - | |||||||||||||
506 | - | |||||||||||||
507 | - | |||||||||||||
508 | bool QHostAddress::operator==(const QHostAddress &other) const | - | ||||||||||||
509 | { | - | ||||||||||||
510 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
511 | do { if (!(&other)->d->isParsed) (&other)->d->parse(); } while (0); | - | ||||||||||||
512 | - | |||||||||||||
513 | if (d->protocol == QAbstractSocket::IPv4Protocol) | - | ||||||||||||
514 | return other.d->protocol == QAbstractSocket::IPv4Protocol && d->a == other.d->a; | - | ||||||||||||
515 | if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
516 | return other.d->protocol == QAbstractSocket::IPv6Protocol | - | ||||||||||||
517 | && memcmp(&d->a6, &other.d->a6, sizeof(Q_IPV6ADDR)) == 0; | - | ||||||||||||
518 | } | - | ||||||||||||
519 | return d->protocol == other.d->protocol; | - | ||||||||||||
520 | } | - | ||||||||||||
521 | - | |||||||||||||
522 | - | |||||||||||||
523 | - | |||||||||||||
524 | - | |||||||||||||
525 | - | |||||||||||||
526 | bool QHostAddress::operator ==(SpecialAddress other) const | - | ||||||||||||
527 | { | - | ||||||||||||
528 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
529 | quint32 ip4 = ((in_addr_t) 0x00000000); | - | ||||||||||||
530 | switch (other) { | - | ||||||||||||
531 | case Null: | - | ||||||||||||
532 | return d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
533 | - | |||||||||||||
534 | case Broadcast: | - | ||||||||||||
535 | ip4 = ((in_addr_t) 0xffffffff); | - | ||||||||||||
536 | break; | - | ||||||||||||
537 | - | |||||||||||||
538 | case LocalHost: | - | ||||||||||||
539 | ip4 = ((in_addr_t) 0x7f000001); | - | ||||||||||||
540 | break; | - | ||||||||||||
541 | - | |||||||||||||
542 | case Any: | - | ||||||||||||
543 | return d->protocol == QAbstractSocket::AnyIPProtocol; | - | ||||||||||||
544 | - | |||||||||||||
545 | case AnyIPv4: | - | ||||||||||||
546 | break; | - | ||||||||||||
547 | - | |||||||||||||
548 | case LocalHostIPv6: | - | ||||||||||||
549 | case AnyIPv6: | - | ||||||||||||
550 | if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
551 | quint64 second = quint8(other == LocalHostIPv6); | - | ||||||||||||
552 | return d->a6_64.c[0] == 0 && d->a6_64.c[1] == qToBigEndian(second); | - | ||||||||||||
553 | } | - | ||||||||||||
554 | return false; | - | ||||||||||||
555 | } | - | ||||||||||||
556 | - | |||||||||||||
557 | - | |||||||||||||
558 | return d->protocol == QAbstractSocket::IPv4Protocol && d->a == ip4; | - | ||||||||||||
559 | } | - | ||||||||||||
560 | - | |||||||||||||
561 | - | |||||||||||||
562 | - | |||||||||||||
563 | - | |||||||||||||
564 | - | |||||||||||||
565 | - | |||||||||||||
566 | bool QHostAddress::isNull() const | - | ||||||||||||
567 | { | - | ||||||||||||
568 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
569 | return d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
570 | } | - | ||||||||||||
571 | bool QHostAddress::isInSubnet(const QHostAddress &subnet, int netmask) const | - | ||||||||||||
572 | { | - | ||||||||||||
573 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
574 | if (subnet.protocol() != d->protocol || netmask < 0) | - | ||||||||||||
575 | return false; | - | ||||||||||||
576 | - | |||||||||||||
577 | union { | - | ||||||||||||
578 | quint32 ip; | - | ||||||||||||
579 | quint8 data[4]; | - | ||||||||||||
580 | } ip4, net4; | - | ||||||||||||
581 | const quint8 *ip; | - | ||||||||||||
582 | const quint8 *net; | - | ||||||||||||
583 | if (d->protocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
584 | if (netmask > 32) | - | ||||||||||||
585 | netmask = 32; | - | ||||||||||||
586 | ip4.ip = qToBigEndian(d->a); | - | ||||||||||||
587 | net4.ip = qToBigEndian(subnet.d->a); | - | ||||||||||||
588 | ip = ip4.data; | - | ||||||||||||
589 | net = net4.data; | - | ||||||||||||
590 | } else if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
591 | if (netmask > 128) | - | ||||||||||||
592 | netmask = 128; | - | ||||||||||||
593 | ip = d->a6.c; | - | ||||||||||||
594 | net = subnet.d->a6.c; | - | ||||||||||||
595 | } else { | - | ||||||||||||
596 | return false; | - | ||||||||||||
597 | } | - | ||||||||||||
598 | - | |||||||||||||
599 | if (netmask >= 8 && memcmp(ip, net, netmask / 8) != 0) | - | ||||||||||||
600 | return false; | - | ||||||||||||
601 | if ((netmask & 7) == 0) | - | ||||||||||||
602 | return true; | - | ||||||||||||
603 | - | |||||||||||||
604 | - | |||||||||||||
605 | quint8 bytemask = 256 - (1 << (8 - (netmask & 7))); | - | ||||||||||||
606 | quint8 ipbyte = ip[netmask / 8]; | - | ||||||||||||
607 | quint8 netbyte = net[netmask / 8]; | - | ||||||||||||
608 | return (ipbyte & bytemask) == (netbyte & bytemask); | - | ||||||||||||
609 | } | - | ||||||||||||
610 | bool QHostAddress::isInSubnet(const QPair<QHostAddress, int> &subnet) const | - | ||||||||||||
611 | { | - | ||||||||||||
612 | return isInSubnet(subnet.first, subnet.second); | - | ||||||||||||
613 | } | - | ||||||||||||
614 | QPair<QHostAddress, int> QHostAddress::parseSubnet(const QString &subnet) | - | ||||||||||||
615 | { | - | ||||||||||||
616 | const QPair<QHostAddress, int> invalid = qMakePair(QHostAddress(), -1); | - | ||||||||||||
617 | if (subnet.isEmpty()
| 1-85 | ||||||||||||
618 | return executed 1 time by 1 test: invalid;return invalid; Executed by:
executed 1 time by 1 test: return invalid; Executed by:
| 1 | ||||||||||||
619 | - | |||||||||||||
620 | int slash = subnet.indexOf(QLatin1Char('/')); | - | ||||||||||||
621 | QString netStr = subnet; | - | ||||||||||||
622 | if (slash != -1
| 13-72 | ||||||||||||
623 | netStr.truncate(slash); executed 72 times by 2 tests: netStr.truncate(slash); Executed by:
| 72 | ||||||||||||
624 | - | |||||||||||||
625 | int netmask = -1; | - | ||||||||||||
626 | bool isIpv6 = netStr.contains(QLatin1Char(':')); | - | ||||||||||||
627 | - | |||||||||||||
628 | if (slash != -1
| 13-72 | ||||||||||||
629 | - | |||||||||||||
630 | if (!isIpv6
| 16-40 | ||||||||||||
631 | - | |||||||||||||
632 | QNetmaskAddress parser; | - | ||||||||||||
633 | if (!parser.setAddress(subnet.mid(slash + 1))
| 3-13 | ||||||||||||
634 | return executed 3 times by 1 test: invalid;return invalid; Executed by:
executed 3 times by 1 test: return invalid; Executed by:
| 3 | ||||||||||||
635 | netmask = parser.prefixLength(); | - | ||||||||||||
636 | } executed 13 times by 1 test: else {end of block Executed by:
| 13 | ||||||||||||
637 | bool ok; | - | ||||||||||||
638 | netmask = subnet.midmidRef(slash + 1).toUInt(&ok); | - | ||||||||||||
639 | if (!ok
| 4-52 | ||||||||||||
640 | return executed 4 times by 1 test: invalid;return invalid; Executed by:
executed 4 times by 1 test: return invalid; Executed by:
| 4 | ||||||||||||
641 | } executed 52 times by 2 tests: end of block Executed by:
| 52 | ||||||||||||
642 | } | - | ||||||||||||
643 | - | |||||||||||||
644 | if (isIpv6
| 30-48 | ||||||||||||
645 | - | |||||||||||||
646 | if (netmask > 128
| 1-29 | ||||||||||||
647 | return executed 1 time by 1 test: invalid;return invalid; Executed by:
executed 1 time by 1 test: return invalid; Executed by:
| 1 | ||||||||||||
648 | if (netmask < 0
| 1-28 | ||||||||||||
649 | netmask = 128; executed 1 time by 1 test: netmask = 128; Executed by:
| 1 | ||||||||||||
650 | - | |||||||||||||
651 | QHostAddress net; | - | ||||||||||||
652 | if (!net.setAddress(netStr)
| 1-28 | ||||||||||||
653 | return executed 1 time by 1 test: invalid;return invalid; Executed by:
executed 1 time by 1 test: return invalid; Executed by:
| 1 | ||||||||||||
654 | - | |||||||||||||
655 | clearBits(net.d->a6.c, netmask, 128); | - | ||||||||||||
656 | return executed 28 times by 2 tests: qMakePair(net, netmask);return qMakePair(net, netmask); Executed by:
executed 28 times by 2 tests: return qMakePair(net, netmask); Executed by:
| 28 | ||||||||||||
657 | } | - | ||||||||||||
658 | - | |||||||||||||
659 | if (netmask > 32
| 1-47 | ||||||||||||
660 | return executed 1 time by 1 test: invalid;return invalid; Executed by:
executed 1 time by 1 test: return invalid; Executed by:
| 1 | ||||||||||||
661 | - | |||||||||||||
662 | - | |||||||||||||
663 | QStringListauto parts = netStr.splitsplitRef(QLatin1Char('.')); | - | ||||||||||||
664 | if (parts.isEmpty()
| 0-47 | ||||||||||||
665 | return executed 2 times by 1 test: invalid;return invalid; Executed by:
executed 2 times by 1 test: return invalid; Executed by:
| 2 | ||||||||||||
666 | - | |||||||||||||
667 | if (parts.lastconstLast().isEmpty()
| 2-43 | ||||||||||||
668 | parts.removeLast(); executed 2 times by 1 test: parts.removeLast(); Executed by:
| 2 | ||||||||||||
669 | - | |||||||||||||
670 | quint32 addr = 0; | - | ||||||||||||
671 | for (int i = 0; i < parts.count()
| 42-154 | ||||||||||||
672 | bool ok; | - | ||||||||||||
673 | uint byteValue = parts.at(i).toUInt(&ok); | - | ||||||||||||
674 | if (!ok
| 0-151 | ||||||||||||
675 | return executed 3 times by 1 test: invalid;return invalid; Executed by:
executed 3 times by 1 test: return invalid; Executed by:
| 3 | ||||||||||||
676 | - | |||||||||||||
677 | addr <<= 8; | - | ||||||||||||
678 | addr += byteValue; | - | ||||||||||||
679 | } executed 151 times by 2 tests: end of block Executed by:
| 151 | ||||||||||||
680 | addr <<= 8 * (4 - parts.count()); | - | ||||||||||||
681 | if (netmask == -1
| 7-35 | ||||||||||||
682 | netmask = 8 * parts.count(); | - | ||||||||||||
683 | } executed 7 times by 1 test: else if (netmask == 0end of block Executed by:
| 3-32 | ||||||||||||
684 | - | |||||||||||||
685 | - | |||||||||||||
686 | - | |||||||||||||
687 | addr = 0; | - | ||||||||||||
688 | } executed 3 times by 1 test: else if (netmask != 32end of block Executed by:
| 2-30 | ||||||||||||
689 | - | |||||||||||||
690 | quint32 mask = quint32(0xffffffff) >> (32 - netmask) << (32 - netmask); | - | ||||||||||||
691 | addr &= mask; | - | ||||||||||||
692 | } executed 30 times by 2 tests: end of block Executed by:
| 30 | ||||||||||||
693 | - | |||||||||||||
694 | return executed 42 times by 2 tests: qMakePair(QHostAddress(addr), netmask);return qMakePair(QHostAddress(addr), netmask); Executed by:
executed 42 times by 2 tests: return qMakePair(QHostAddress(addr), netmask); Executed by:
| 42 | ||||||||||||
695 | } | - | ||||||||||||
696 | - | |||||||||||||
697 | - | |||||||||||||
698 | - | |||||||||||||
699 | - | |||||||||||||
700 | - | |||||||||||||
701 | - | |||||||||||||
702 | - | |||||||||||||
703 | bool QHostAddress::isLoopback() const | - | ||||||||||||
704 | { | - | ||||||||||||
705 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
706 | if ((d->a & 0xFF000000) == 0x7F000000) | - | ||||||||||||
707 | return true; | - | ||||||||||||
708 | if (d->protocol == QAbstractSocket::IPv6Protocol) { | - | ||||||||||||
709 | - | |||||||||||||
710 | const __m128i loopback = _mm_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); | - | ||||||||||||
711 | __m128i ipv6 = _mm_loadu_si128((const __m128i *)d->a6.c); | - | ||||||||||||
712 | __m128i cmp = _mm_cmpeq_epi8(ipv6, loopback); | - | ||||||||||||
713 | return _mm_movemask_epi8(cmp) == 0xffff; | - | ||||||||||||
714 | - | |||||||||||||
715 | - | |||||||||||||
716 | - | |||||||||||||
717 | - | |||||||||||||
718 | return dead code: true;return true; dead code: return true; | - | ||||||||||||
719 | } | - | ||||||||||||
720 | return false; | - | ||||||||||||
721 | } | - | ||||||||||||
722 | - | |||||||||||||
723 | - | |||||||||||||
724 | - | |||||||||||||
725 | - | |||||||||||||
726 | - | |||||||||||||
727 | - | |||||||||||||
728 | - | |||||||||||||
729 | bool QHostAddress::isMulticast() const | - | ||||||||||||
730 | { | - | ||||||||||||
731 | do { if (!(this)->d->isParsed) (this)->d->parse(); } while (0); | - | ||||||||||||
732 | if ((d->a & 0xF0000000) == 0xE0000000) | - | ||||||||||||
733 | return true; | - | ||||||||||||
734 | if (d->protocol == QAbstractSocket::IPv6Protocol) | - | ||||||||||||
735 | return d->a6.c[0] == 0xff; | - | ||||||||||||
736 | return false; | - | ||||||||||||
737 | } | - | ||||||||||||
738 | - | |||||||||||||
739 | - | |||||||||||||
740 | QDebug operator<<(QDebug d, const QHostAddress &address) | - | ||||||||||||
741 | { | - | ||||||||||||
742 | QDebugStateSaver saver(d); | - | ||||||||||||
743 | d.resetFormat().nospace(); | - | ||||||||||||
744 | if (address == QHostAddress::Any) | - | ||||||||||||
745 | d << "QHostAddress(QHostAddress::Any)"; | - | ||||||||||||
746 | else | - | ||||||||||||
747 | d << "QHostAddress(" << address.toString() << ')'; | - | ||||||||||||
748 | return d; | - | ||||||||||||
749 | } | - | ||||||||||||
750 | - | |||||||||||||
751 | - | |||||||||||||
752 | uint qHash(const QHostAddress &key, uint seed) | - | ||||||||||||
753 | { | - | ||||||||||||
754 | - | |||||||||||||
755 | do { if (!(&key)->d->isParsed) (&key)->d->parse(); } while (0); | - | ||||||||||||
756 | return qHashBits(key.d->a6.c, 16, seed); | - | ||||||||||||
757 | } | - | ||||||||||||
758 | QDataStream &operator<<(QDataStream &out, const QHostAddress &address) | - | ||||||||||||
759 | { | - | ||||||||||||
760 | qint8 prot; | - | ||||||||||||
761 | prot = qint8(address.protocol()); | - | ||||||||||||
762 | out << prot; | - | ||||||||||||
763 | switch (address.protocol()) { | - | ||||||||||||
764 | case QAbstractSocket::UnknownNetworkLayerProtocol: | - | ||||||||||||
765 | case QAbstractSocket::AnyIPProtocol: | - | ||||||||||||
766 | break; | - | ||||||||||||
767 | case QAbstractSocket::IPv4Protocol: | - | ||||||||||||
768 | out << address.toIPv4Address(); | - | ||||||||||||
769 | break; | - | ||||||||||||
770 | case QAbstractSocket::IPv6Protocol: | - | ||||||||||||
771 | { | - | ||||||||||||
772 | Q_IPV6ADDR ipv6 = address.toIPv6Address(); | - | ||||||||||||
773 | for (int i = 0; i < 16; ++i) | - | ||||||||||||
774 | out << ipv6[i]; | - | ||||||||||||
775 | out << address.scopeId(); | - | ||||||||||||
776 | } | - | ||||||||||||
777 | break; | - | ||||||||||||
778 | } | - | ||||||||||||
779 | return out; | - | ||||||||||||
780 | } | - | ||||||||||||
781 | QDataStream &operator>>(QDataStream &in, QHostAddress &address) | - | ||||||||||||
782 | { | - | ||||||||||||
783 | qint8 prot; | - | ||||||||||||
784 | in >> prot; | - | ||||||||||||
785 | switch (QAbstractSocket::NetworkLayerProtocol(prot)) { | - | ||||||||||||
786 | case QAbstractSocket::UnknownNetworkLayerProtocol: | - | ||||||||||||
787 | address.clear(); | - | ||||||||||||
788 | break; | - | ||||||||||||
789 | case QAbstractSocket::IPv4Protocol: | - | ||||||||||||
790 | { | - | ||||||||||||
791 | quint32 ipv4; | - | ||||||||||||
792 | in >> ipv4; | - | ||||||||||||
793 | address.setAddress(ipv4); | - | ||||||||||||
794 | } | - | ||||||||||||
795 | break; | - | ||||||||||||
796 | case QAbstractSocket::IPv6Protocol: | - | ||||||||||||
797 | { | - | ||||||||||||
798 | Q_IPV6ADDR ipv6; | - | ||||||||||||
799 | for (int i = 0; i < 16; ++i) | - | ||||||||||||
800 | in >> ipv6[i]; | - | ||||||||||||
801 | address.setAddress(ipv6); | - | ||||||||||||
802 | - | |||||||||||||
803 | QString scope; | - | ||||||||||||
804 | in >> scope; | - | ||||||||||||
805 | address.setScopeId(scope); | - | ||||||||||||
806 | } | - | ||||||||||||
807 | break; | - | ||||||||||||
808 | case QAbstractSocket::AnyIPProtocol: | - | ||||||||||||
809 | address = QHostAddress::Any; | - | ||||||||||||
810 | break; | - | ||||||||||||
811 | default: | - | ||||||||||||
812 | address.clear(); | - | ||||||||||||
813 | in.setStatus(QDataStream::ReadCorruptData); | - | ||||||||||||
814 | } | - | ||||||||||||
815 | return in; | - | ||||||||||||
816 | } | - | ||||||||||||
817 | - | |||||||||||||
818 | - | |||||||||||||
819 | - | |||||||||||||
820 | - | |||||||||||||
Switch to Source code | Preprocessed file |