Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qnetworkinterface.cpp
Go to the documentation of this file.
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"
7
8#include "qdebug.h"
9#include "qendian.h"
10#include "private/qtools_p.h"
11
12#ifndef QT_NO_NETWORKINTERFACE
13
15
18
19static_assert(QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
21
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
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
49
53
57
58QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name)
59{
60 const auto interfaceList = allInterfaces();
61
62 bool ok;
63 uint index = name.toUInt(&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
75QSharedDataPointer<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
86QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces()
87{
88 const QList<QNetworkInterfacePrivate *> list = postProcess(scan());
89 QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;
90 result.reserve(list.size());
91
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)
97 }
98
99 result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);
100 }
101
102 return result;
103}
104
106{
107 const int outLen = qMax(len * 2 + (len - 1) * 1, 0);
109 QChar *out = result.data();
110 for (int i = 0; i < len; ++i) {
111 if (i)
112 *out++ = u':';
115 }
116 return result;
117}
118
171
180
185{
186 *d.get() = *other.d.get();
187 return *this;
188}
189
204
210{
211 if (d == other.d) return true;
212 if (!d || !other.d) return false;
213 return d->address == other.d->address &&
214 d->netmask == other.d->netmask &&
215 d->broadcast == other.d->broadcast;
216}
217
238
247{
248 d->dnsEligibility = status;
249}
250
263{
264 return d->address;
265}
266
272{
273 d->address = newIp;
274}
275
290{
291 return d->netmask.address(d->address.protocol());
292}
293
302{
303 if (newNetmask.protocol() != ip().protocol()) {
304 d->netmask = QNetmask();
305 return;
306 }
307
308 d->netmask.setAddress(newNetmask);
309}
310
325{
326 return d->netmask.prefixLength();
327}
328
342{
343 d->netmask.setPrefixLength(d->address.protocol(), length);
344}
345
360{
361 return d->broadcast;
362}
363
369{
370 d->broadcast = newBroadcast;
371}
372
383{
384 return d->lifetimeKnown;
385}
386
402{
403 return d->preferredLifetime;
404}
405
422{
423 return d->validityLifetime;
424}
425
437{
438 d->preferredLifetime = preferred;
439 d->validityLifetime = validity;
440 d->lifetimeKnown = true;
441}
442
452{
453 d->preferredLifetime = QDeadlineTimer::Forever;
454 d->validityLifetime = QDeadlineTimer::Forever;
455 d->lifetimeKnown = false;
456}
457
475{
476 return d->validityLifetime.isForever();
477}
478
605
612
621
627{
628 d = other.d;
629 return *this;
630}
631
645{
646 return !name().isEmpty();
647}
648
659{
660 return d ? d->index : 0;
661}
662
682{
683 return d ? d->mtu : 0;
684}
685
693{
694 return d ? d->name : QString();
695}
696
712{
713 return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString();
714}
715
719QNetworkInterface::InterfaceFlags QNetworkInterface::flags() const
720{
721 return d ? d->flags : InterfaceFlags{};
722}
723
736
749{
750 return d ? d->hardwareAddress : QString();
751}
752
761QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const
762{
763 return d ? d->addressEntries : QList<QNetworkAddressEntry>();
764}
765
778{
779 if (name.isEmpty())
780 return 0;
781
782 bool ok;
783 uint id = name.toUInt(&ok);
784 if (!ok)
786 return int(id);
787}
788
800{
802 result.d = manager()->interfaceFromName(name);
803 return result;
804}
805
822
841
846QList<QNetworkInterface> QNetworkInterface::allInterfaces()
847{
848 const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
849 QList<QNetworkInterface> result;
850 result.reserve(privs.size());
851 for (const auto &p : privs) {
853 item.d = p;
854 result << item;
855 }
856
857 return result;
858}
859
868{
869 const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
870 QList<QHostAddress> result;
871 for (const auto &p : privs) {
872 // skip addresses if the interface isn't up
873 if ((p->flags & QNetworkInterface::IsUp) == 0)
874 continue;
875
876 for (const QNetworkAddressEntry &entry : std::as_const(p->addressEntries))
877 result += entry.ip();
878 }
879
880 return result;
881}
882
883#ifndef QT_NO_DEBUG_STREAM
884static inline QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags)
885{
887 debug << "IsUp ";
889 debug << "IsRunning ";
891 debug << "CanBroadcast ";
893 debug << "IsLoopBack ";
895 debug << "IsPointToPoint ";
897 debug << "CanMulticast ";
898 return debug;
899}
900
910{
911 QDebugStateSaver saver(debug);
912 debug.resetFormat().nospace();
913 debug << "address = " << entry.ip();
914 if (!entry.netmask().isNull())
915 debug << ", netmask = " << entry.netmask();
916 if (!entry.broadcast().isNull())
917 debug << ", broadcast = " << entry.broadcast();
918 return debug;
919}
920
928{
929 QDebugStateSaver saver(debug);
930 debug.resetFormat().nospace();
931 debug << "QNetworkInterface(name = " << networkInterface.name()
932 << ", hardware address = " << networkInterface.hardwareAddress()
933 << ", flags = ";
934 flagsDebug(debug, networkInterface.flags());
935 debug << ", entries = " << networkInterface.addressEntries()
936 << ")\n";
937 return debug;
938}
939#endif
940
942
943#include "moc_qnetworkinterface.cpp"
944
945#endif // QT_NO_NETWORKINTERFACE
static constexpr auto IPv4Protocol
\inmodule QtCore
\inmodule QtCore
static constexpr ForeverConstant Forever
\inmodule QtCore
\inmodule QtCore
The QHostAddress class provides an IP address.
Definition qlist.h:75
qsizetype size() const noexcept
Definition qlist.h:397
The QNetworkAddressEntry class stores one IP address supported by a network interface,...
void setAddressLifetime(QDeadlineTimer preferred, QDeadlineTimer validity)
QDeadlineTimer validityLifetime() const
void setPrefixLength(int length)
QHostAddress broadcast() const
Returns the broadcast address associated with the IPv4 address and netmask.
QHostAddress netmask() const
Returns the netmask associated with the IP address.
QDeadlineTimer preferredLifetime() const
QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry)
~QNetworkAddressEntry()
Destroys this QNetworkAddressEntry object.
void setNetmask(const QHostAddress &newNetmask)
Sets the netmask that this QNetworkAddressEntry object contains to newNetmask.
bool operator==(const QNetworkAddressEntry &other) const
Returns true if this network address entry is the same as other.
QHostAddress ip() const
This function returns one IPv4 or IPv6 address found, that was found in a network interface.
void setIp(const QHostAddress &newIp)
Sets the IP address the QNetworkAddressEntry object contains to newIp.
void setBroadcast(const QHostAddress &newBroadcast)
Sets the broadcast IP address of this QNetworkAddressEntry object to newBroadcast.
void setDnsEligibility(DnsEligibilityStatus status)
DnsEligibilityStatus dnsEligibility() const
QNetworkAddressEntry()
Constructs an empty QNetworkAddressEntry object.
QNetworkAddressEntry & operator=(QNetworkAddressEntry &&other) noexcept
QSharedDataPointer< QNetworkInterfacePrivate > interfaceFromName(const QString &name)
static uint interfaceIndexFromName(const QString &name)
QList< QSharedDataPointer< QNetworkInterfacePrivate > > allInterfaces()
QSharedDataPointer< QNetworkInterfacePrivate > empty
static QString interfaceNameFromIndex(uint index)
QSharedDataPointer< QNetworkInterfacePrivate > interfaceFromIndex(int index)
static QString makeHwAddress(int len, uchar *data)
QNetworkInterface::InterfaceFlags flags
QNetworkInterface::InterfaceType type
QList< QNetworkAddressEntry > addressEntries
The QNetworkInterface class provides a listing of the host's IP addresses and network interfaces.
~QNetworkInterface()
Frees the resources associated with the QNetworkInterface object.
static QNetworkInterface interfaceFromIndex(int index)
Returns a QNetworkInterface object for the interface whose internal ID is index.
QNetworkInterface()
Constructs an empty network interface object.
QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface)
Writes the QNetworkInterface networkInterface to the stream and returns a reference to the debug stre...
QNetworkInterface & operator=(QNetworkInterface &&other) noexcept
static QList< QHostAddress > allAddresses()
This convenience function returns all IP addresses found on the host machine.
int maximumTransmissionUnit() const
QString name() const
Returns the name of this network interface.
static int interfaceIndexFromName(const QString &name)
InterfaceFlags flags() const
Returns the flags associated with this network interface.
InterfaceType type() const
InterfaceType
Specifies the type of hardware (PHY layer, OSI level 1) this interface is, if it could be determined.
static QString interfaceNameFromIndex(int index)
QString hardwareAddress() const
Returns the low-level hardware address for this interface.
static QNetworkInterface interfaceFromName(const QString &name)
Returns a QNetworkInterface object for the interface named name.
QList< QNetworkAddressEntry > addressEntries() const
Returns the list of IP addresses that this interface possesses along with their associated netmasks a...
static QList< QNetworkInterface > allInterfaces()
Returns a listing of all the network interfaces found on the host machine.
bool isValid() const
Returns true if this QNetworkInterface object contains valid information about a network interface.
QString humanReadableName() const
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
Combined button and popup list for selecting options.
constexpr char toHexUpper(char32_t value) noexcept
Definition qtools_p.h:27
constexpr Initialization Uninitialized
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char * interface
static QDBusError::ErrorType get(const char *name)
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
static ControlElement< T > * ptr(QWidget *widget)
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
static QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags)
static QList< QNetworkInterfacePrivate * > postProcess(QList< QNetworkInterfacePrivate * > list)
GLuint index
[2]
GLenum GLuint GLenum GLsizei length
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLbitfield flags
GLuint name
GLuint entry
GLenum const void * addr
GLuint GLuint64EXT address
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
GLenum GLsizei len
#define QT_VERSION_CHECK(major, minor, patch)
#define QT_VERSION
unsigned char uchar
Definition qtypes.h:32
unsigned int uint
Definition qtypes.h:34
QList< int > list
[14]
QTextStream out(stdout)
[7]
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item
QNetworkAccessManager manager
\inmodule QtCore \reentrant
Definition qchar.h:18