1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // Copyright (C) 2017 Intel Corporation. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include "qnetworkinterface.h" |
6 | #include "qnetworkinterface_p.h" |
7 | |
8 | #include "qdebug.h" |
9 | #include "qendian.h" |
10 | #include "private/qtools_p.h" |
11 | |
12 | #ifndef QT_NO_NETWORKINTERFACE |
13 | |
14 | QT_BEGIN_NAMESPACE |
15 | |
16 | QT_IMPL_METATYPE_EXTERN(QNetworkAddressEntry) |
17 | QT_IMPL_METATYPE_EXTERN(QNetworkInterface) |
18 | |
19 | static_assert(QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
20 | && sizeof(QScopedPointer<QNetworkAddressEntryPrivate>) == sizeof(std::unique_ptr<QNetworkAddressEntryPrivate>)); |
21 | |
22 | static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list) |
23 | { |
24 | // Some platforms report a netmask but don't report a broadcast address |
25 | // Go through all available addresses and calculate the broadcast address |
26 | // from the IP and the netmask |
27 | // |
28 | // This is an IPv4-only thing -- IPv6 has no concept of broadcasts |
29 | // The math is: |
30 | // broadcast = IP | ~netmask |
31 | |
32 | for (QNetworkInterfacePrivate *interface : list) { |
33 | for (QNetworkAddressEntry &address : interface->addressEntries) { |
34 | if (address.ip().protocol() != QAbstractSocket::IPv4Protocol) |
35 | continue; |
36 | |
37 | if (!address.netmask().isNull() && address.broadcast().isNull()) { |
38 | QHostAddress bcast = address.ip(); |
39 | bcast = QHostAddress(bcast.toIPv4Address() | ~address.netmask().toIPv4Address()); |
40 | address.setBroadcast(bcast); |
41 | } |
42 | } |
43 | } |
44 | |
45 | return list; |
46 | } |
47 | |
48 | Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager) |
49 | |
50 | QNetworkInterfaceManager::QNetworkInterfaceManager() |
51 | { |
52 | } |
53 | |
54 | QNetworkInterfaceManager::~QNetworkInterfaceManager() |
55 | { |
56 | } |
57 | |
58 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name) |
59 | { |
60 | const auto interfaceList = allInterfaces(); |
61 | |
62 | bool ok; |
63 | uint index = name.toUInt(ok: &ok); |
64 | |
65 | for (const auto &interface : interfaceList) { |
66 | if (ok && interface->index == int(index)) |
67 | return interface; |
68 | else if (interface->name == name) |
69 | return interface; |
70 | } |
71 | |
72 | return empty; |
73 | } |
74 | |
75 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromIndex(int index) |
76 | { |
77 | const auto interfaceList = allInterfaces(); |
78 | for (const auto &interface : interfaceList) { |
79 | if (interface->index == index) |
80 | return interface; |
81 | } |
82 | |
83 | return empty; |
84 | } |
85 | |
86 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces() |
87 | { |
88 | const QList<QNetworkInterfacePrivate *> list = postProcess(list: scan()); |
89 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > result; |
90 | result.reserve(asize: list.size()); |
91 | |
92 | for (QNetworkInterfacePrivate *ptr : list) { |
93 | if ((ptr->flags & QNetworkInterface::IsUp) == 0) { |
94 | // if the network interface isn't UP, the addresses are ineligible for DNS |
95 | for (auto &addr : ptr->addressEntries) |
96 | addr.setDnsEligibility(QNetworkAddressEntry::DnsIneligible); |
97 | } |
98 | |
99 | result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr); |
100 | } |
101 | |
102 | return result; |
103 | } |
104 | |
105 | QString QNetworkInterfacePrivate::makeHwAddress(int len, uchar *data) |
106 | { |
107 | const int outLen = qMax(a: len * 2 + (len - 1) * 1, b: 0); |
108 | QString result(outLen, Qt::Uninitialized); |
109 | QChar *out = result.data(); |
110 | for (int i = 0; i < len; ++i) { |
111 | if (i) |
112 | *out++ = u':'; |
113 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(value: data[i] / 16)); |
114 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(value: data[i] % 16)); |
115 | } |
116 | return result; |
117 | } |
118 | |
119 | /*! |
120 | \class QNetworkAddressEntry |
121 | \brief The QNetworkAddressEntry class stores one IP address |
122 | supported by a network interface, along with its associated |
123 | netmask and broadcast address. |
124 | |
125 | \since 4.2 |
126 | \reentrant |
127 | \ingroup network |
128 | \ingroup shared |
129 | \inmodule QtNetwork |
130 | |
131 | Each network interface can contain zero or more IP addresses, which |
132 | in turn can be associated with a netmask and/or a broadcast |
133 | address (depending on support from the operating system). |
134 | |
135 | This class represents one such group. |
136 | */ |
137 | |
138 | /*! |
139 | \enum QNetworkAddressEntry::DnsEligibilityStatus |
140 | \since 5.11 |
141 | |
142 | This enum indicates whether a given host address is eligible to be |
143 | published in the Domain Name System (DNS) or other similar name resolution |
144 | mechanisms. In general, an address is suitable for publication if it is an |
145 | address this machine will be reached at for an indeterminate amount of |
146 | time, though it need not be permanent. For example, addresses obtained via |
147 | DHCP are often eligible, but cryptographically-generated temporary IPv6 |
148 | addresses are not. |
149 | |
150 | \value DnsEligibilityUnknown Qt and the operating system could not determine |
151 | whether this address should be published or not. |
152 | The application may need to apply further |
153 | heuristics if it cannot find any eligible |
154 | addresses. |
155 | \value DnsEligible This address is eligible for publication in DNS. |
156 | \value DnsIneligible This address should not be published in DNS and |
157 | should not be transmitted to other parties, |
158 | except maybe as the source address of an outgoing |
159 | packet. |
160 | |
161 | \sa dnsEligibility(), setDnsEligibility() |
162 | */ |
163 | |
164 | /*! |
165 | Constructs an empty QNetworkAddressEntry object. |
166 | */ |
167 | QNetworkAddressEntry::QNetworkAddressEntry() |
168 | : d(new QNetworkAddressEntryPrivate) |
169 | { |
170 | } |
171 | |
172 | /*! |
173 | Constructs a QNetworkAddressEntry object that is a copy of the |
174 | object \a other. |
175 | */ |
176 | QNetworkAddressEntry::QNetworkAddressEntry(const QNetworkAddressEntry &other) |
177 | : d(new QNetworkAddressEntryPrivate(*other.d.get())) |
178 | { |
179 | } |
180 | |
181 | /*! |
182 | Makes a copy of the QNetworkAddressEntry object \a other. |
183 | */ |
184 | QNetworkAddressEntry &QNetworkAddressEntry::operator=(const QNetworkAddressEntry &other) |
185 | { |
186 | *d.get() = *other.d.get(); |
187 | return *this; |
188 | } |
189 | |
190 | /*! |
191 | \fn void QNetworkAddressEntry::swap(QNetworkAddressEntry &other) |
192 | \since 5.0 |
193 | \memberswap{network address entry instance} |
194 | */ |
195 | |
196 | /*! |
197 | Destroys this QNetworkAddressEntry object. |
198 | */ |
199 | QNetworkAddressEntry::~QNetworkAddressEntry() |
200 | { |
201 | } |
202 | |
203 | /*! |
204 | Returns \c true if this network address entry is the same as \a |
205 | other. |
206 | */ |
207 | bool QNetworkAddressEntry::operator==(const QNetworkAddressEntry &other) const |
208 | { |
209 | if (d == other.d) return true; |
210 | if (!d || !other.d) return false; |
211 | return d->address == other.d->address && |
212 | d->netmask == other.d->netmask && |
213 | d->broadcast == other.d->broadcast; |
214 | } |
215 | |
216 | /*! |
217 | \since 5.11 |
218 | |
219 | Returns whether this address is eligible for publication in the Domain Name |
220 | System (DNS) or similar name resolution mechanisms. |
221 | |
222 | In general, an address is suitable for publication if it is an address this |
223 | machine will be reached at for an indeterminate amount of time, though it |
224 | need not be permanent. For example, addresses obtained via DHCP are often |
225 | eligible, but cryptographically-generated temporary IPv6 addresses are not. |
226 | |
227 | On some systems, QNetworkInterface will need to heuristically determine |
228 | which addresses are eligible. |
229 | |
230 | \sa isLifetimeKnown(), isPermanent(), setDnsEligibility() |
231 | */ |
232 | QNetworkAddressEntry::DnsEligibilityStatus QNetworkAddressEntry::dnsEligibility() const |
233 | { |
234 | return d->dnsEligibility; |
235 | } |
236 | |
237 | /*! |
238 | \since 5.11 |
239 | |
240 | Sets the DNS eligibility flag for this address to \a status. |
241 | |
242 | \sa dnsEligibility() |
243 | */ |
244 | void QNetworkAddressEntry::setDnsEligibility(DnsEligibilityStatus status) |
245 | { |
246 | d->dnsEligibility = status; |
247 | } |
248 | |
249 | /*! |
250 | \fn bool QNetworkAddressEntry::operator!=(const QNetworkAddressEntry &other) const |
251 | |
252 | Returns \c true if this network address entry is different from \a |
253 | other. |
254 | */ |
255 | |
256 | /*! |
257 | This function returns one IPv4 or IPv6 address found, that was |
258 | found in a network interface. |
259 | */ |
260 | QHostAddress QNetworkAddressEntry::ip() const |
261 | { |
262 | return d->address; |
263 | } |
264 | |
265 | /*! |
266 | Sets the IP address the QNetworkAddressEntry object contains to \a |
267 | newIp. |
268 | */ |
269 | void QNetworkAddressEntry::setIp(const QHostAddress &newIp) |
270 | { |
271 | d->address = newIp; |
272 | } |
273 | |
274 | /*! |
275 | Returns the netmask associated with the IP address. The |
276 | netmask is expressed in the form of an IP address, such as |
277 | 255.255.0.0. |
278 | |
279 | For IPv6 addresses, the prefix length is converted to an address |
280 | where the number of bits set to 1 is equal to the prefix |
281 | length. For a prefix length of 64 bits (the most common value), |
282 | the netmask will be expressed as a QHostAddress holding the |
283 | address FFFF:FFFF:FFFF:FFFF:: |
284 | |
285 | \sa prefixLength() |
286 | */ |
287 | QHostAddress QNetworkAddressEntry::netmask() const |
288 | { |
289 | return d->netmask.address(protocol: d->address.protocol()); |
290 | } |
291 | |
292 | /*! |
293 | Sets the netmask that this QNetworkAddressEntry object contains to |
294 | \a newNetmask. Setting the netmask also sets the prefix length to |
295 | match the new netmask. |
296 | |
297 | \sa setPrefixLength() |
298 | */ |
299 | void QNetworkAddressEntry::setNetmask(const QHostAddress &newNetmask) |
300 | { |
301 | if (newNetmask.protocol() != ip().protocol()) { |
302 | d->netmask = QNetmask(); |
303 | return; |
304 | } |
305 | |
306 | d->netmask.setAddress(newNetmask); |
307 | } |
308 | |
309 | /*! |
310 | \since 4.5 |
311 | Returns the prefix length of this IP address. The prefix length |
312 | matches the number of bits set to 1 in the netmask (see |
313 | netmask()). For IPv4 addresses, the value is between 0 and 32. For |
314 | IPv6 addresses, it's contained between 0 and 128 and is the |
315 | preferred form of representing addresses. |
316 | |
317 | This function returns -1 if the prefix length could not be |
318 | determined (i.e., netmask() returns a null QHostAddress()). |
319 | |
320 | \sa netmask() |
321 | */ |
322 | int QNetworkAddressEntry::prefixLength() const |
323 | { |
324 | return d->netmask.prefixLength(); |
325 | } |
326 | |
327 | /*! |
328 | \since 4.5 |
329 | Sets the prefix length of this IP address to \a length. The value |
330 | of \a length must be valid for this type of IP address: between 0 |
331 | and 32 for IPv4 addresses, between 0 and 128 for IPv6 |
332 | addresses. Setting to any invalid value is equivalent to setting |
333 | to -1, which means "no prefix length". |
334 | |
335 | Setting the prefix length also sets the netmask (see netmask()). |
336 | |
337 | \sa setNetmask() |
338 | */ |
339 | void QNetworkAddressEntry::setPrefixLength(int length) |
340 | { |
341 | d->netmask.setPrefixLength(proto: d->address.protocol(), len: length); |
342 | } |
343 | |
344 | /*! |
345 | Returns the broadcast address associated with the IPv4 |
346 | address and netmask. It can usually be derived from those two by |
347 | setting to 1 the bits of the IP address where the netmask contains |
348 | a 0. (In other words, by bitwise-OR'ing the IP address with the |
349 | inverse of the netmask) |
350 | |
351 | This member is always empty for IPv6 addresses, since the concept |
352 | of broadcast has been abandoned in that system in favor of |
353 | multicast. In particular, the group of hosts corresponding to all |
354 | the nodes in the local network can be reached by the "all-nodes" |
355 | special multicast group (address FF02::1). |
356 | */ |
357 | QHostAddress QNetworkAddressEntry::broadcast() const |
358 | { |
359 | return d->broadcast; |
360 | } |
361 | |
362 | /*! |
363 | Sets the broadcast IP address of this QNetworkAddressEntry object |
364 | to \a newBroadcast. |
365 | */ |
366 | void QNetworkAddressEntry::setBroadcast(const QHostAddress &newBroadcast) |
367 | { |
368 | d->broadcast = newBroadcast; |
369 | } |
370 | |
371 | /*! |
372 | \since 5.11 |
373 | |
374 | Returns \c true if the address lifetime is known, \c false if not. If the |
375 | lifetime is not known, both preferredLifetime() and validityLifetime() will |
376 | return QDeadlineTimer::Forever. |
377 | |
378 | \sa preferredLifetime(), validityLifetime(), setAddressLifetime(), clearAddressLifetime() |
379 | */ |
380 | bool QNetworkAddressEntry::isLifetimeKnown() const |
381 | { |
382 | return d->lifetimeKnown; |
383 | } |
384 | |
385 | /*! |
386 | \since 5.11 |
387 | |
388 | Returns the deadline when this address becomes deprecated (no longer |
389 | preferred), if known. If the address lifetime is not known (see |
390 | isLifetimeKnown()), this function always returns QDeadlineTimer::Forever. |
391 | |
392 | While an address is preferred, it may be used by the operating system as |
393 | the source address for new, outgoing packets. After it becomes deprecated, |
394 | it will remain valid for incoming packets for a while longer until finally |
395 | removed (see validityLifetime()). |
396 | |
397 | \sa validityLifetime(), isLifetimeKnown(), setAddressLifetime(), clearAddressLifetime() |
398 | */ |
399 | QDeadlineTimer QNetworkAddressEntry::preferredLifetime() const |
400 | { |
401 | return d->preferredLifetime; |
402 | } |
403 | |
404 | /*! |
405 | \since 5.11 |
406 | |
407 | Returns the deadline when this address becomes invalid and will be removed |
408 | from the networking stack, if known. If the address lifetime is not known |
409 | (see isLifetimeKnown()), this function always returns |
410 | QDeadlineTimer::Forever. |
411 | |
412 | While an address is valid, it will be accepted by the operating system as a |
413 | valid destination address for this machine. Whether it is used as a source |
414 | address for new, outgoing packets is controlled by, among other rules, the |
415 | preferred lifetime (see preferredLifetime()). |
416 | |
417 | \sa preferredLifetime(), isLifetimeKnown(), setAddressLifetime(), clearAddressLifetime() |
418 | */ |
419 | QDeadlineTimer QNetworkAddressEntry::validityLifetime() const |
420 | { |
421 | return d->validityLifetime; |
422 | } |
423 | |
424 | /*! |
425 | \since 5.11 |
426 | |
427 | Sets both the preferred and valid lifetimes for this address to the \a |
428 | preferred and \a validity deadlines, respectively. After this call, |
429 | isLifetimeKnown() will return \c true, even if both parameters are |
430 | QDeadlineTimer::Forever. |
431 | |
432 | \sa preferredLifetime(), validityLifetime(), isLifetimeKnown(), clearAddressLifetime() |
433 | */ |
434 | void QNetworkAddressEntry::setAddressLifetime(QDeadlineTimer preferred, QDeadlineTimer validity) |
435 | { |
436 | d->preferredLifetime = preferred; |
437 | d->validityLifetime = validity; |
438 | d->lifetimeKnown = true; |
439 | } |
440 | |
441 | /*! |
442 | \since 5.11 |
443 | |
444 | Resets both the preferred and valid lifetimes for this address. After this |
445 | call, isLifetimeKnown() will return \c false. |
446 | |
447 | \sa preferredLifetime(), validityLifetime(), isLifetimeKnown(), setAddressLifetime() |
448 | */ |
449 | void QNetworkAddressEntry::clearAddressLifetime() |
450 | { |
451 | d->preferredLifetime = QDeadlineTimer::Forever; |
452 | d->validityLifetime = QDeadlineTimer::Forever; |
453 | d->lifetimeKnown = false; |
454 | } |
455 | |
456 | /*! |
457 | \since 5.11 |
458 | |
459 | Returns \c true if this address is permanent on this interface, \c false if |
460 | it's temporary. A permanent address is one which has no expiration time and |
461 | is often static (manually configured). |
462 | |
463 | If this information could not be determined, this function returns \c true. |
464 | |
465 | \note Depending on the operating system and the networking configuration |
466 | tool, it is possible for a temporary address to be interpreted as |
467 | permanent, if the tool did not inform the details correctly to the |
468 | operating system. |
469 | |
470 | \sa isLifetimeKnown(), validityLifetime(), isTemporary() |
471 | */ |
472 | bool QNetworkAddressEntry::isPermanent() const |
473 | { |
474 | return d->validityLifetime.isForever(); |
475 | } |
476 | |
477 | /*! |
478 | \fn bool QNetworkAddressEntry::isTemporary() const |
479 | \since 5.11 |
480 | |
481 | Returns \c true if this address is temporary on this interface, \c false if |
482 | it's permanent. |
483 | |
484 | \sa isLifetimeKnown(), validityLifetime(), isPermanent() |
485 | */ |
486 | |
487 | /*! |
488 | \class QNetworkInterface |
489 | \brief The QNetworkInterface class provides a listing of the host's IP |
490 | addresses and network interfaces. |
491 | |
492 | \since 4.2 |
493 | \reentrant |
494 | \ingroup network |
495 | \ingroup shared |
496 | \inmodule QtNetwork |
497 | |
498 | QNetworkInterface represents one network interface attached to the |
499 | host where the program is being run. Each network interface may |
500 | contain zero or more IP addresses, each of which is optionally |
501 | associated with a netmask and/or a broadcast address. The list of |
502 | such trios can be obtained with addressEntries(). Alternatively, |
503 | when the netmask or the broadcast addresses or other information aren't |
504 | necessary, use the allAddresses() convenience function to obtain just the |
505 | IP addresses of the active interfaces. |
506 | |
507 | QNetworkInterface also reports the interface's hardware address with |
508 | hardwareAddress(). |
509 | |
510 | Not all operating systems support reporting all features. Only the |
511 | IPv4 addresses are guaranteed to be listed by this class in all |
512 | platforms. In particular, IPv6 address listing is only supported |
513 | on Windows, Linux, \macos and the BSDs. |
514 | |
515 | \sa QNetworkAddressEntry |
516 | */ |
517 | |
518 | /*! |
519 | \enum QNetworkInterface::InterfaceFlag |
520 | Specifies the flags associated with this network interface. The |
521 | possible values are: |
522 | |
523 | \value IsUp the network interface is "up" - |
524 | enabled by administrative action |
525 | \value IsRunning the network interface is operational: |
526 | configured "up" and (typically) |
527 | physically connected to a network |
528 | \value CanBroadcast the network interface works in |
529 | broadcast mode |
530 | \value IsLoopBack the network interface is a loopback |
531 | interface: that is, it's a virtual |
532 | interface whose destination is the |
533 | host computer itself |
534 | \value IsPointToPoint the network interface is a |
535 | point-to-point interface: that is, |
536 | there is one, single other address |
537 | that can be directly reached by it. |
538 | \value CanMulticast the network interface supports |
539 | multicasting |
540 | |
541 | Note that one network interface cannot be both broadcast-based and |
542 | point-to-point. |
543 | */ |
544 | |
545 | /*! |
546 | \enum QNetworkInterface::InterfaceType |
547 | |
548 | Specifies the type of hardware (PHY layer, OSI level 1) this interface is, |
549 | if it could be determined. Interface types that are not among those listed |
550 | below will generally be listed as Unknown, though future versions of Qt may |
551 | add new enumeration values. |
552 | |
553 | The possible values are: |
554 | |
555 | \value Unknown The interface type could not be determined or is not |
556 | one of the other listed types. |
557 | \value Loopback The virtual loopback interface, which is assigned |
558 | the loopback IP addresses (127.0.0.1, ::1). |
559 | \value Virtual A type of interface determined to be virtual, but |
560 | not any of the other possible types. For example, |
561 | tunnel interfaces are (currently) detected as |
562 | virtual ones. |
563 | \value Ethernet IEEE 802.3 Ethernet interfaces, though on many |
564 | systems other types of IEEE 802 interfaces may also |
565 | be detected as Ethernet (especially Wi-Fi). |
566 | \value Wifi IEEE 802.11 Wi-Fi interfaces. Note that on some |
567 | systems, QNetworkInterface may be unable to |
568 | distinguish regular Ethernet from Wi-Fi and will |
569 | not return this enum value. |
570 | \value Ieee80211 An alias for WiFi. |
571 | \value CanBus ISO 11898 Controller Area Network bus interfaces, |
572 | usually found on automotive systems. |
573 | \value Fddi ANSI X3T12 Fiber Distributed Data Interface, a local area |
574 | network over optical fibers. |
575 | \value Ppp Point-to-Point Protocol interfaces, establishing a |
576 | direct connection between two nodes over a lower |
577 | transport layer (often serial over radio or physical |
578 | line). |
579 | \value Slip Serial Line Internet Protocol interfaces. |
580 | \value Phonet Interfaces using the Linux Phonet socket family, for |
581 | communication with cellular modems. See the |
582 | \l {https://www.kernel.org/doc/Documentation/networking/phonet.txt}{Linux kernel documentation} |
583 | for more information. |
584 | \value Ieee802154 IEEE 802.15.4 Personal Area Network interfaces, other |
585 | than 6LoWPAN (see below). |
586 | \value SixLoWPAN 6LoWPAN (IPv6 over Low-power Wireless Personal Area |
587 | Networks) interfaces, which operate on IEEE 802.15.4 |
588 | PHY, but have specific header compression schemes |
589 | for IPv6 and UDP. This type of interface is often |
590 | used for mesh networking. |
591 | \value Ieee80216 IEEE 802.16 Wireless Metropolitan Area Network, also |
592 | known under the commercial name "WiMAX". |
593 | \value Ieee1394 IEEE 1394 interfaces (a.k.a. "FireWire"). |
594 | */ |
595 | |
596 | /*! |
597 | Constructs an empty network interface object. |
598 | */ |
599 | QNetworkInterface::QNetworkInterface() |
600 | : d(nullptr) |
601 | { |
602 | } |
603 | |
604 | /*! |
605 | Frees the resources associated with the QNetworkInterface object. |
606 | */ |
607 | QNetworkInterface::~QNetworkInterface() |
608 | { |
609 | } |
610 | |
611 | /*! |
612 | Creates a copy of the QNetworkInterface object contained in \a |
613 | other. |
614 | */ |
615 | QNetworkInterface::QNetworkInterface(const QNetworkInterface &other) |
616 | : d(other.d) |
617 | { |
618 | } |
619 | |
620 | /*! |
621 | Copies the contents of the QNetworkInterface object contained in \a |
622 | other into this one. |
623 | */ |
624 | QNetworkInterface &QNetworkInterface::operator=(const QNetworkInterface &other) |
625 | { |
626 | d = other.d; |
627 | return *this; |
628 | } |
629 | |
630 | /*! |
631 | \fn void QNetworkInterface::swap(QNetworkInterface &other) |
632 | \since 5.0 |
633 | \memberswap{network interface instance} |
634 | */ |
635 | |
636 | /*! |
637 | Returns \c true if this QNetworkInterface object contains valid |
638 | information about a network interface. |
639 | */ |
640 | bool QNetworkInterface::isValid() const |
641 | { |
642 | return !name().isEmpty(); |
643 | } |
644 | |
645 | /*! |
646 | \since 4.5 |
647 | Returns the interface system index, if known. This is an integer |
648 | assigned by the operating system to identify this interface and it |
649 | generally doesn't change. It matches the scope ID field in IPv6 |
650 | addresses. |
651 | |
652 | If the index isn't known, this function returns 0. |
653 | */ |
654 | int QNetworkInterface::index() const |
655 | { |
656 | return d ? d->index : 0; |
657 | } |
658 | |
659 | /*! |
660 | \since 5.11 |
661 | |
662 | Returns the maximum transmission unit on this interface, if known, or 0 |
663 | otherwise. |
664 | |
665 | The maximum transmission unit is the largest packet that may be sent on |
666 | this interface without incurring link-level fragmentation. Applications may |
667 | use this value to calculate the size of the payload that will fit an |
668 | unfragmented UDP datagram. Remember to subtract the sizes of headers used |
669 | in your communication over the interface, e.g. TCP (20 bytes) or UDP (12), |
670 | IPv4 (20) or IPv6 (40, absent some form of header compression), when |
671 | computing how big a payload you can transmit. Also note that the MTU along |
672 | the full path (the Path MTU) to the destination may be smaller than the |
673 | interface's MTU. |
674 | |
675 | \sa QUdpSocket |
676 | */ |
677 | int QNetworkInterface::maximumTransmissionUnit() const |
678 | { |
679 | return d ? d->mtu : 0; |
680 | } |
681 | |
682 | /*! |
683 | Returns the name of this network interface. On Unix systems, this |
684 | is a string containing the type of the interface and optionally a |
685 | sequence number, such as "eth0", "lo" or "pcn0". On Windows, it's |
686 | an internal ID that cannot be changed by the user. |
687 | */ |
688 | QString QNetworkInterface::name() const |
689 | { |
690 | return d ? d->name : QString(); |
691 | } |
692 | |
693 | /*! |
694 | \since 4.5 |
695 | |
696 | Returns the human-readable name of this network interface on |
697 | Windows, such as "Local Area Connection", if the name could be |
698 | determined. If it couldn't, this function returns the same as |
699 | name(). The human-readable name is a name that the user can modify |
700 | in the Windows Control Panel, so it may change during the |
701 | execution of the program. |
702 | |
703 | On Unix, this function currently always returns the same as |
704 | name(), since Unix systems don't store a configuration for |
705 | human-readable names. |
706 | */ |
707 | QString QNetworkInterface::humanReadableName() const |
708 | { |
709 | return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString(); |
710 | } |
711 | |
712 | /*! |
713 | Returns the flags associated with this network interface. |
714 | */ |
715 | QNetworkInterface::InterfaceFlags QNetworkInterface::flags() const |
716 | { |
717 | return d ? d->flags : InterfaceFlags{}; |
718 | } |
719 | |
720 | /*! |
721 | \since 5.11 |
722 | |
723 | Returns the type of this interface, if it could be determined. If it could |
724 | not be determined, this function returns QNetworkInterface::Unknown. |
725 | |
726 | \sa hardwareAddress() |
727 | */ |
728 | QNetworkInterface::InterfaceType QNetworkInterface::type() const |
729 | { |
730 | return d ? d->type : Unknown; |
731 | } |
732 | |
733 | /*! |
734 | Returns the low-level hardware address for this interface. On |
735 | Ethernet interfaces, this will be a MAC address in string |
736 | representation, separated by colons. |
737 | |
738 | Other interface types may have other types of hardware |
739 | addresses. Implementations should not depend on this function |
740 | returning a valid MAC address. |
741 | |
742 | \sa type() |
743 | */ |
744 | QString QNetworkInterface::hardwareAddress() const |
745 | { |
746 | return d ? d->hardwareAddress : QString(); |
747 | } |
748 | |
749 | /*! |
750 | Returns the list of IP addresses that this interface possesses |
751 | along with their associated netmasks and broadcast addresses. |
752 | |
753 | If the netmask or broadcast address or other information is not necessary, |
754 | you can call the allAddresses() function to obtain just the IP addresses of |
755 | the active interfaces. |
756 | */ |
757 | QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const |
758 | { |
759 | return d ? d->addressEntries : QList<QNetworkAddressEntry>(); |
760 | } |
761 | |
762 | /*! |
763 | \since 5.7 |
764 | |
765 | Returns the index of the interface whose name is \a name or 0 if there is |
766 | no interface with that name. This function should produce the same result |
767 | as the following code, but will probably execute faster. |
768 | |
769 | \snippet code/src_network_kernel_qnetworkinterface.cpp 0 |
770 | |
771 | \sa interfaceFromName(), interfaceNameFromIndex(), QNetworkDatagram::interfaceIndex() |
772 | */ |
773 | int QNetworkInterface::interfaceIndexFromName(const QString &name) |
774 | { |
775 | if (name.isEmpty()) |
776 | return 0; |
777 | |
778 | bool ok; |
779 | uint id = name.toUInt(ok: &ok); |
780 | if (!ok) |
781 | id = QNetworkInterfaceManager::interfaceIndexFromName(name); |
782 | return int(id); |
783 | } |
784 | |
785 | /*! |
786 | Returns a QNetworkInterface object for the interface named \a |
787 | name. If no such interface exists, this function returns an |
788 | invalid QNetworkInterface object. |
789 | |
790 | The string \a name may be either an actual interface name (such as "eth0" |
791 | or "en1") or an interface index in string form ("1", "2", etc.). |
792 | |
793 | \sa name(), isValid() |
794 | */ |
795 | QNetworkInterface QNetworkInterface::interfaceFromName(const QString &name) |
796 | { |
797 | QNetworkInterface result; |
798 | result.d = manager()->interfaceFromName(name); |
799 | return result; |
800 | } |
801 | |
802 | /*! |
803 | Returns a QNetworkInterface object for the interface whose internal |
804 | ID is \a index. Network interfaces have a unique identifier called |
805 | the "interface index" to distinguish it from other interfaces on |
806 | the system. Often, this value is assigned progressively and |
807 | interfaces being removed and then added again get a different |
808 | value every time. |
809 | |
810 | This index is also found in the IPv6 address' scope ID field. |
811 | */ |
812 | QNetworkInterface QNetworkInterface::interfaceFromIndex(int index) |
813 | { |
814 | QNetworkInterface result; |
815 | result.d = manager()->interfaceFromIndex(index); |
816 | return result; |
817 | } |
818 | |
819 | /*! |
820 | \since 5.7 |
821 | |
822 | Returns the name of the interface whose index is \a index or an empty |
823 | string if there is no interface with that index. This function should |
824 | produce the same result as the following code, but will probably execute |
825 | faster. |
826 | |
827 | \snippet code/src_network_kernel_qnetworkinterface.cpp 1 |
828 | |
829 | \sa interfaceFromIndex(), interfaceIndexFromName(), QNetworkDatagram::interfaceIndex() |
830 | */ |
831 | QString QNetworkInterface::interfaceNameFromIndex(int index) |
832 | { |
833 | if (!index) |
834 | return QString(); |
835 | return QNetworkInterfaceManager::interfaceNameFromIndex(index); |
836 | } |
837 | |
838 | /*! |
839 | Returns a listing of all the network interfaces found on the host |
840 | machine. In case of failure it returns a list with zero elements. |
841 | */ |
842 | QList<QNetworkInterface> QNetworkInterface::allInterfaces() |
843 | { |
844 | const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces(); |
845 | QList<QNetworkInterface> result; |
846 | result.reserve(asize: privs.size()); |
847 | for (const auto &p : privs) { |
848 | QNetworkInterface item; |
849 | item.d = p; |
850 | result << item; |
851 | } |
852 | |
853 | return result; |
854 | } |
855 | |
856 | /*! |
857 | This convenience function returns all IP addresses found on the host |
858 | machine. It is equivalent to calling addressEntries() on all the objects |
859 | returned by allInterfaces() that are in the QNetworkInterface::IsUp state |
860 | to obtain lists of QNetworkAddressEntry objects then calling |
861 | QNetworkAddressEntry::ip() on each of these. |
862 | */ |
863 | QList<QHostAddress> QNetworkInterface::allAddresses() |
864 | { |
865 | const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces(); |
866 | QList<QHostAddress> result; |
867 | for (const auto &p : privs) { |
868 | // skip addresses if the interface isn't up |
869 | if ((p->flags & QNetworkInterface::IsUp) == 0) |
870 | continue; |
871 | |
872 | for (const QNetworkAddressEntry &entry : std::as_const(t: p->addressEntries)) |
873 | result += entry.ip(); |
874 | } |
875 | |
876 | return result; |
877 | } |
878 | |
879 | #ifndef QT_NO_DEBUG_STREAM |
880 | static inline QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags) |
881 | { |
882 | if (flags & QNetworkInterface::IsUp) |
883 | debug << "IsUp "; |
884 | if (flags & QNetworkInterface::IsRunning) |
885 | debug << "IsRunning "; |
886 | if (flags & QNetworkInterface::CanBroadcast) |
887 | debug << "CanBroadcast "; |
888 | if (flags & QNetworkInterface::IsLoopBack) |
889 | debug << "IsLoopBack "; |
890 | if (flags & QNetworkInterface::IsPointToPoint) |
891 | debug << "IsPointToPoint "; |
892 | if (flags & QNetworkInterface::CanMulticast) |
893 | debug << "CanMulticast "; |
894 | return debug; |
895 | } |
896 | |
897 | /*! |
898 | \since 6.2 |
899 | |
900 | Writes the QNetworkAddressEntry \a entry to the stream and |
901 | returns a reference to the \a debug stream. |
902 | |
903 | \relates QNetworkAddressEntry |
904 | */ |
905 | QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry) |
906 | { |
907 | QDebugStateSaver saver(debug); |
908 | debug.resetFormat().nospace(); |
909 | debug << "address = "<< entry.ip(); |
910 | if (!entry.netmask().isNull()) |
911 | debug << ", netmask = "<< entry.netmask(); |
912 | if (!entry.broadcast().isNull()) |
913 | debug << ", broadcast = "<< entry.broadcast(); |
914 | return debug; |
915 | } |
916 | |
917 | /*! |
918 | Writes the QNetworkInterface \a networkInterface to the stream and |
919 | returns a reference to the \a debug stream. |
920 | |
921 | \relates QNetworkInterface |
922 | */ |
923 | QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface) |
924 | { |
925 | QDebugStateSaver saver(debug); |
926 | debug.resetFormat().nospace(); |
927 | debug << "QNetworkInterface(name = "<< networkInterface.name() |
928 | << ", hardware address = "<< networkInterface.hardwareAddress() |
929 | << ", flags = "; |
930 | flagsDebug(debug, flags: networkInterface.flags()); |
931 | debug << ", entries = "<< networkInterface.addressEntries() |
932 | << ")\n"; |
933 | return debug; |
934 | } |
935 | #endif |
936 | |
937 | QT_END_NAMESPACE |
938 | |
939 | #include "moc_qnetworkinterface.cpp" |
940 | |
941 | #endif // QT_NO_NETWORKINTERFACE |
942 |
Definitions
- postProcess
- manager
- QNetworkInterfaceManager
- ~QNetworkInterfaceManager
- interfaceFromName
- interfaceFromIndex
- allInterfaces
- makeHwAddress
- QNetworkAddressEntry
- QNetworkAddressEntry
- operator=
- ~QNetworkAddressEntry
- operator==
- dnsEligibility
- setDnsEligibility
- ip
- setIp
- netmask
- setNetmask
- prefixLength
- setPrefixLength
- broadcast
- setBroadcast
- isLifetimeKnown
- preferredLifetime
- validityLifetime
- setAddressLifetime
- clearAddressLifetime
- isPermanent
- QNetworkInterface
- ~QNetworkInterface
- QNetworkInterface
- operator=
- isValid
- index
- maximumTransmissionUnit
- name
- humanReadableName
- flags
- type
- hardwareAddress
- addressEntries
- interfaceIndexFromName
- interfaceFromName
- interfaceFromIndex
- interfaceNameFromIndex
- allInterfaces
- allAddresses
- flagsDebug
- operator<<
Learn Advanced QML with KDAB
Find out more