11#include "private/qtools_p.h"
13#ifndef QT_NO_NETWORKINTERFACE
17QT_IMPL_METATYPE_EXTERN(QNetworkAddressEntry)
18QT_IMPL_METATYPE_EXTERN(QNetworkInterface)
20static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list)
30 for (QNetworkInterfacePrivate *iface : list) {
31 for (QNetworkAddressEntry &address : iface->addressEntries) {
32 if (address.ip().protocol() != QAbstractSocket::IPv4Protocol)
35 if (!address.netmask().isNull() && address.broadcast().isNull()) {
36 QHostAddress bcast = address.ip();
37 bcast = QHostAddress(bcast.toIPv4Address() | ~address.netmask().toIPv4Address());
38 address.setBroadcast(bcast);
48QNetworkInterfaceManager::QNetworkInterfaceManager()
58 const auto interfaceList = allInterfaces();
61 uint index = name.toUInt(&ok);
63 for (
const auto &iface : interfaceList) {
64 if (ok && iface->index ==
int(index))
66 else if (iface->name == name)
75 const auto interfaceList = allInterfaces();
76 for (
const auto &iface : interfaceList) {
77 if (iface->index == index)
86 const QList<QNetworkInterfacePrivate *> list = postProcess(scan());
87 QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;
88 result.reserve(list.size());
90 for (QNetworkInterfacePrivate *ptr : list) {
91 if ((ptr->flags & QNetworkInterface::IsUp) == 0) {
93 for (
auto &addr : ptr->addressEntries)
94 addr.setDnsEligibility(QNetworkAddressEntry::DnsIneligible);
97 result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);
105 const int outLen = qMax(len * 2 + (len - 1) * 1, 0);
106 QString result(outLen, Qt::Uninitialized);
107 QChar *out = result.data();
108 for (
int i = 0; i < len; ++i) {
111 *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] / 16));
112 *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] % 16));
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
163
164
165QNetworkAddressEntry::QNetworkAddressEntry()
166 : d(
new QNetworkAddressEntryPrivate)
171
172
173
174QNetworkAddressEntry::QNetworkAddressEntry(
const QNetworkAddressEntry &other)
175 : d(
new QNetworkAddressEntryPrivate(*other.d.get()))
180
181
182QNetworkAddressEntry &QNetworkAddressEntry::operator=(
const QNetworkAddressEntry &other)
184 *d.get() = *other.d.get();
189
190
191
192
195
196
197QNetworkAddressEntry::~QNetworkAddressEntry()
202
203
204
205bool QNetworkAddressEntry::operator==(
const QNetworkAddressEntry &other)
const
207 if (d == other.d)
return true;
208 if (!d || !other.d)
return false;
209 return d->address == other.d->address &&
210 d->netmask == other.d->netmask &&
211 d->broadcast == other.d->broadcast;
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230QNetworkAddressEntry::DnsEligibilityStatus QNetworkAddressEntry::dnsEligibility()
const
232 return d->dnsEligibility;
236
237
238
239
240
241
242void QNetworkAddressEntry::setDnsEligibility(DnsEligibilityStatus status)
244 d->dnsEligibility = status;
248
249
250
251
252
255
256
257
258QHostAddress QNetworkAddressEntry::ip()
const
264
265
266
267void QNetworkAddressEntry::setIp(
const QHostAddress &newIp)
273
274
275
276
277
278
279
280
281
282
283
284
285QHostAddress QNetworkAddressEntry::netmask()
const
287 return d->netmask.address(d->address.protocol());
291
292
293
294
295
296
297void QNetworkAddressEntry::setNetmask(
const QHostAddress &newNetmask)
299 if (newNetmask.protocol() != ip().protocol()) {
300 d->netmask = QNetmask();
304 d->netmask.setAddress(newNetmask);
308
309
310
311
312
313
314
315
316
317
318
319
320int QNetworkAddressEntry::prefixLength()
const
322 return d->netmask.prefixLength();
326
327
328
329
330
331
332
333
334
335
336
337void QNetworkAddressEntry::setPrefixLength(
int length)
339 d->netmask.setPrefixLength(d->address.protocol(), length);
343
344
345
346
347
348
349
350
351
352
353
354
355QHostAddress QNetworkAddressEntry::broadcast()
const
361
362
363
364void QNetworkAddressEntry::setBroadcast(
const QHostAddress &newBroadcast)
366 d->broadcast = newBroadcast;
370
371
372
373
374
375
376
377
378bool QNetworkAddressEntry::isLifetimeKnown()
const
380 return d->lifetimeKnown;
384
385
386
387
388
389
390
391
392
393
394
395
396
397QDeadlineTimer QNetworkAddressEntry::preferredLifetime()
const
399 return d->preferredLifetime;
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417QDeadlineTimer QNetworkAddressEntry::validityLifetime()
const
419 return d->validityLifetime;
423
424
425
426
427
428
429
430
431
432void QNetworkAddressEntry::setAddressLifetime(QDeadlineTimer preferred, QDeadlineTimer validity)
434 d->preferredLifetime = preferred;
435 d->validityLifetime = validity;
436 d->lifetimeKnown =
true;
440
441
442
443
444
445
446
447void QNetworkAddressEntry::clearAddressLifetime()
449 d->preferredLifetime = QDeadlineTimer::Forever;
450 d->validityLifetime = QDeadlineTimer::Forever;
451 d->lifetimeKnown =
false;
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470bool QNetworkAddressEntry::isPermanent()
const
472 return d->validityLifetime.isForever();
476
477
478
479
480
481
482
483
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
595
596
597QNetworkInterface::QNetworkInterface()
603
604
605QNetworkInterface::~QNetworkInterface()
610
611
612
613QNetworkInterface::QNetworkInterface(
const QNetworkInterface &other)
619
620
621
622QNetworkInterface &QNetworkInterface::operator=(
const QNetworkInterface &other)
629
630
631
632
635
636
637
638bool QNetworkInterface::isValid()
const
640 return !name().isEmpty();
644
645
646
647
648
649
650
651
652int QNetworkInterface::index()
const
654 return d ? d->index : 0;
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675int QNetworkInterface::maximumTransmissionUnit()
const
677 return d ? d->mtu : 0;
681
682
683
684
685
686QString QNetworkInterface::name()
const
688 return d ? d->name : QString();
692
693
694
695
696
697
698
699
700
701
702
703
704
705QString QNetworkInterface::humanReadableName()
const
707 return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString();
711
712
713QNetworkInterface::InterfaceFlags QNetworkInterface::flags()
const
715 return d ? d->flags : InterfaceFlags{};
719
720
721
722
723
724
725
726QNetworkInterface::InterfaceType QNetworkInterface::type()
const
728 return d ? d->type : Unknown;
732
733
734
735
736
737
738
739
740
741
742QString QNetworkInterface::hardwareAddress()
const
744 return d ? d->hardwareAddress : QString();
748
749
750
751
752
753
754
755QList<QNetworkAddressEntry> QNetworkInterface::addressEntries()
const
757 return d ? d->addressEntries : QList<QNetworkAddressEntry>();
761
762
763
764
765
766
767
768
769
770
771int QNetworkInterface::interfaceIndexFromName(
const QString &name)
777 uint id = name.toUInt(&ok);
779 id = QNetworkInterfaceManager::interfaceIndexFromName(name);
784
785
786
787
788
789
790
791
792
793QNetworkInterface QNetworkInterface::interfaceFromName(
const QString &name)
795 QNetworkInterface result;
796 result.d = manager()->interfaceFromName(name);
801
802
803
804
805
806
807
808
809
810QNetworkInterface QNetworkInterface::interfaceFromIndex(
int index)
812 QNetworkInterface result;
813 result.d = manager()->interfaceFromIndex(index);
818
819
820
821
822
823
824
825
826
827
828
829QString QNetworkInterface::interfaceNameFromIndex(
int index)
833 return QNetworkInterfaceManager::interfaceNameFromIndex(index);
837
838
839
840QList<QNetworkInterface> QNetworkInterface::allInterfaces()
842 const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
843 QList<QNetworkInterface> result;
844 result.reserve(privs.size());
845 for (
const auto &p : privs) {
846 QNetworkInterface item;
855
856
857
858
859
860
861QList<QHostAddress> QNetworkInterface::allAddresses()
863 const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
864 QList<QHostAddress> result;
865 for (
const auto &p : privs) {
867 if ((p->flags & QNetworkInterface::IsUp) == 0)
870 for (
const QNetworkAddressEntry &entry : std::as_const(p->addressEntries))
871 result += entry.ip();
877#ifndef QT_NO_DEBUG_STREAM
880 if (flags & QNetworkInterface::IsUp)
882 if (flags & QNetworkInterface::IsRunning)
883 debug <<
"IsRunning ";
884 if (flags & QNetworkInterface::CanBroadcast)
885 debug <<
"CanBroadcast ";
886 if (flags & QNetworkInterface::IsLoopBack)
887 debug <<
"IsLoopBack ";
888 if (flags & QNetworkInterface::IsPointToPoint)
889 debug <<
"IsPointToPoint ";
890 if (flags & QNetworkInterface::CanMulticast)
891 debug <<
"CanMulticast ";
896
897
898
899
900
901
902
905 QDebugStateSaver saver(debug);
906 debug.resetFormat().nospace();
907 debug <<
"address = " << entry.ip();
908 if (!entry.netmask().isNull())
909 debug <<
", netmask = " << entry.netmask();
910 if (!entry.broadcast().isNull())
911 debug <<
", broadcast = " << entry.broadcast();
916
917
918
919
920
923 QDebugStateSaver saver(debug);
924 debug.resetFormat().nospace();
925 debug <<
"QNetworkInterface(name = " << networkInterface.name()
926 <<
", hardware address = " << networkInterface.hardwareAddress()
928 flagsDebug(debug, networkInterface.flags());
929 debug <<
", entries = " << networkInterface.addressEntries()
937#include "moc_qnetworkinterface.cpp"
~QNetworkInterfaceManager()
QSharedDataPointer< QNetworkInterfacePrivate > interfaceFromName(const QString &name)
QList< QSharedDataPointer< QNetworkInterfacePrivate > > allInterfaces()
QSharedDataPointer< QNetworkInterfacePrivate > interfaceFromIndex(int index)
Combined button and popup list for selecting options.
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
static QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags)