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
515
516
517
518
519
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
593
594
595
596
597
600
601
602QNetworkInterface::QNetworkInterface()
608
609
610QNetworkInterface::~QNetworkInterface()
615
616
617
618QNetworkInterface::QNetworkInterface(
const QNetworkInterface &other)
624
625
626
627QNetworkInterface &QNetworkInterface::operator=(
const QNetworkInterface &other)
634
635
636
637
640
641
642
643bool QNetworkInterface::isValid()
const
645 return !name().isEmpty();
649
650
651
652
653
654
655
656
657int QNetworkInterface::index()
const
659 return d ? d->index : 0;
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680int QNetworkInterface::maximumTransmissionUnit()
const
682 return d ? d->mtu : 0;
686
687
688
689
690
691QString QNetworkInterface::name()
const
693 return d ? d->name : QString();
697
698
699
700
701
702
703
704
705
706
707
708
709
710QString QNetworkInterface::humanReadableName()
const
712 return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString();
716
717
718QNetworkInterface::InterfaceFlags QNetworkInterface::flags()
const
720 return d ? d->flags : InterfaceFlags{};
724
725
726
727
728
729
730
731QNetworkInterface::InterfaceType QNetworkInterface::type()
const
733 return d ? d->type : Unknown;
737
738
739
740
741
742
743
744
745
746
747QString QNetworkInterface::hardwareAddress()
const
749 return d ? d->hardwareAddress : QString();
753
754
755
756
757
758
759
760QList<QNetworkAddressEntry> QNetworkInterface::addressEntries()
const
762 return d ? d->addressEntries : QList<QNetworkAddressEntry>();
766
767
768
769
770
771
772
773
774
775
776int QNetworkInterface::interfaceIndexFromName(
const QString &name)
782 uint id = name.toUInt(&ok);
784 id = QNetworkInterfaceManager::interfaceIndexFromName(name);
789
790
791
792
793
794
795
796
797
798QNetworkInterface QNetworkInterface::interfaceFromName(
const QString &name)
800 QNetworkInterface result;
801 result.d = manager()->interfaceFromName(name);
806
807
808
809
810
811
812
813
814
815QNetworkInterface QNetworkInterface::interfaceFromIndex(
int index)
817 QNetworkInterface result;
818 result.d = manager()->interfaceFromIndex(index);
823
824
825
826
827
828
829
830
831
832
833
834QString QNetworkInterface::interfaceNameFromIndex(
int index)
838 return QNetworkInterfaceManager::interfaceNameFromIndex(index);
842
843
844
845QList<QNetworkInterface> QNetworkInterface::allInterfaces()
847 const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
848 QList<QNetworkInterface> result;
849 result.reserve(privs.size());
850 for (
const auto &p : privs) {
851 QNetworkInterface item;
860
861
862
863
864
865
866QList<QHostAddress> QNetworkInterface::allAddresses()
868 const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
869 QList<QHostAddress> result;
870 for (
const auto &p : privs) {
872 if ((p->flags & QNetworkInterface::IsUp) == 0)
875 for (
const QNetworkAddressEntry &entry : std::as_const(p->addressEntries))
876 result += entry.ip();
882#ifndef QT_NO_DEBUG_STREAM
885 if (flags & QNetworkInterface::IsUp)
887 if (flags & QNetworkInterface::IsRunning)
888 debug <<
"IsRunning ";
889 if (flags & QNetworkInterface::CanBroadcast)
890 debug <<
"CanBroadcast ";
891 if (flags & QNetworkInterface::IsLoopBack)
892 debug <<
"IsLoopBack ";
893 if (flags & QNetworkInterface::IsPointToPoint)
894 debug <<
"IsPointToPoint ";
895 if (flags & QNetworkInterface::CanMulticast)
896 debug <<
"CanMulticast ";
901
902
903
904
905
906
907
910 QDebugStateSaver saver(debug);
911 debug.resetFormat().nospace();
912 debug <<
"address = " << entry.ip();
913 if (!entry.netmask().isNull())
914 debug <<
", netmask = " << entry.netmask();
915 if (!entry.broadcast().isNull())
916 debug <<
", broadcast = " << entry.broadcast();
921
922
923
924
925
928 QDebugStateSaver saver(debug);
929 debug.resetFormat().nospace();
930 debug <<
"QNetworkInterface(name = " << networkInterface.name()
931 <<
", hardware address = " << networkInterface.hardwareAddress()
933 flagsDebug(debug, networkInterface.flags());
934 debug <<
", entries = " << networkInterface.addressEntries()
942#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)