| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QNETWORKINTERFACE_H |
| 5 | #define QNETWORKINTERFACE_H |
| 6 | |
| 7 | #include <QtNetwork/qtnetworkglobal.h> |
| 8 | #include <QtCore/qshareddata.h> |
| 9 | #include <QtCore/qscopedpointer.h> |
| 10 | #include <QtNetwork/qhostaddress.h> |
| 11 | |
| 12 | #include <memory> |
| 13 | |
| 14 | #ifndef QT_NO_NETWORKINTERFACE |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | class QDeadlineTimer; |
| 19 | |
| 20 | class QNetworkAddressEntryPrivate; |
| 21 | class Q_NETWORK_EXPORT QNetworkAddressEntry |
| 22 | { |
| 23 | public: |
| 24 | enum DnsEligibilityStatus : qint8 { |
| 25 | DnsEligibilityUnknown = -1, |
| 26 | DnsIneligible = 0, |
| 27 | DnsEligible = 1 |
| 28 | }; |
| 29 | |
| 30 | QNetworkAddressEntry(); |
| 31 | QNetworkAddressEntry(const QNetworkAddressEntry &other); |
| 32 | QNetworkAddressEntry &operator=(QNetworkAddressEntry &&other) noexcept { swap(other); return *this; } |
| 33 | QNetworkAddressEntry &operator=(const QNetworkAddressEntry &other); |
| 34 | ~QNetworkAddressEntry(); |
| 35 | |
| 36 | void swap(QNetworkAddressEntry &other) noexcept { d.swap(u&: other.d); } |
| 37 | |
| 38 | bool operator==(const QNetworkAddressEntry &other) const; |
| 39 | inline bool operator!=(const QNetworkAddressEntry &other) const |
| 40 | { return !(*this == other); } |
| 41 | |
| 42 | DnsEligibilityStatus dnsEligibility() const; |
| 43 | void setDnsEligibility(DnsEligibilityStatus status); |
| 44 | |
| 45 | QHostAddress ip() const; |
| 46 | void setIp(const QHostAddress &newIp); |
| 47 | |
| 48 | QHostAddress netmask() const; |
| 49 | void setNetmask(const QHostAddress &newNetmask); |
| 50 | int prefixLength() const; |
| 51 | void setPrefixLength(int length); |
| 52 | |
| 53 | QHostAddress broadcast() const; |
| 54 | void setBroadcast(const QHostAddress &newBroadcast); |
| 55 | |
| 56 | bool isLifetimeKnown() const; |
| 57 | QDeadlineTimer preferredLifetime() const; |
| 58 | QDeadlineTimer validityLifetime() const; |
| 59 | void setAddressLifetime(QDeadlineTimer preferred, QDeadlineTimer validity); |
| 60 | void clearAddressLifetime(); |
| 61 | bool isPermanent() const; |
| 62 | bool isTemporary() const { return !isPermanent(); } |
| 63 | |
| 64 | private: |
| 65 | // ### Qt 7: make implicitly shared |
| 66 | std::unique_ptr<QNetworkAddressEntryPrivate> d; |
| 67 | }; |
| 68 | |
| 69 | Q_DECLARE_SHARED(QNetworkAddressEntry) |
| 70 | |
| 71 | class QNetworkInterfacePrivate; |
| 72 | class Q_NETWORK_EXPORT QNetworkInterface |
| 73 | { |
| 74 | Q_GADGET |
| 75 | public: |
| 76 | enum InterfaceFlag { |
| 77 | IsUp = 0x1, |
| 78 | IsRunning = 0x2, |
| 79 | CanBroadcast = 0x4, |
| 80 | IsLoopBack = 0x8, |
| 81 | IsPointToPoint = 0x10, |
| 82 | CanMulticast = 0x20 |
| 83 | }; |
| 84 | Q_DECLARE_FLAGS(InterfaceFlags, InterfaceFlag) |
| 85 | Q_FLAG(InterfaceFlags) |
| 86 | |
| 87 | enum InterfaceType { |
| 88 | Loopback = 1, |
| 89 | Virtual, |
| 90 | Ethernet, |
| 91 | Slip, |
| 92 | CanBus, |
| 93 | Ppp, |
| 94 | Fddi, |
| 95 | Wifi, |
| 96 | Ieee80211 = Wifi, // alias |
| 97 | Phonet, |
| 98 | Ieee802154, |
| 99 | SixLoWPAN, // 6LoWPAN, but we can't start with a digit |
| 100 | Ieee80216, |
| 101 | Ieee1394, |
| 102 | |
| 103 | Unknown = 0 |
| 104 | }; |
| 105 | Q_ENUM(InterfaceType) |
| 106 | |
| 107 | QNetworkInterface(); |
| 108 | QNetworkInterface(const QNetworkInterface &other); |
| 109 | QNetworkInterface &operator=(QNetworkInterface &&other) noexcept { swap(other); return *this; } |
| 110 | QNetworkInterface &operator=(const QNetworkInterface &other); |
| 111 | ~QNetworkInterface(); |
| 112 | |
| 113 | void swap(QNetworkInterface &other) noexcept { d.swap(other&: other.d); } |
| 114 | |
| 115 | bool isValid() const; |
| 116 | |
| 117 | int index() const; |
| 118 | int maximumTransmissionUnit() const; |
| 119 | QString name() const; |
| 120 | QString humanReadableName() const; |
| 121 | InterfaceFlags flags() const; |
| 122 | InterfaceType type() const; |
| 123 | QString hardwareAddress() const; |
| 124 | QList<QNetworkAddressEntry> addressEntries() const; |
| 125 | |
| 126 | static int interfaceIndexFromName(const QString &name); |
| 127 | static QNetworkInterface interfaceFromName(const QString &name); |
| 128 | static QNetworkInterface interfaceFromIndex(int index); |
| 129 | static QString interfaceNameFromIndex(int index); |
| 130 | static QList<QNetworkInterface> allInterfaces(); |
| 131 | static QList<QHostAddress> allAddresses(); |
| 132 | |
| 133 | private: |
| 134 | friend class QNetworkInterfacePrivate; |
| 135 | QSharedDataPointer<QNetworkInterfacePrivate> d; |
| 136 | }; |
| 137 | |
| 138 | Q_DECLARE_SHARED(QNetworkInterface) |
| 139 | |
| 140 | Q_DECLARE_OPERATORS_FOR_FLAGS(QNetworkInterface::InterfaceFlags) |
| 141 | |
| 142 | #ifndef QT_NO_DEBUG_STREAM |
| 143 | Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry); |
| 144 | Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface); |
| 145 | #endif |
| 146 | |
| 147 | QT_END_NAMESPACE |
| 148 | |
| 149 | QT_DECL_METATYPE_EXTERN(QNetworkAddressEntry, Q_NETWORK_EXPORT) |
| 150 | QT_DECL_METATYPE_EXTERN(QNetworkInterface, Q_NETWORK_EXPORT) |
| 151 | |
| 152 | #endif // QT_NO_NETWORKINTERFACE |
| 153 | |
| 154 | #endif |
| 155 | |