1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QNETWORKINTERFACE_H
41 #define QNETWORKINTERFACE_H
42 
43 #include <QtNetwork/qtnetworkglobal.h>
44 #include <QtCore/qshareddata.h>
45 #include <QtCore/qscopedpointer.h>
46 #include <QtNetwork/qhostaddress.h>
47 
48 #ifndef QT_NO_NETWORKINTERFACE
49 
50 QT_BEGIN_NAMESPACE
51 
52 class QDeadlineTimer;
53 template<typename T> class QList;
54 
55 class QNetworkAddressEntryPrivate;
56 class Q_NETWORK_EXPORT QNetworkAddressEntry
57 {
58 public:
59     enum DnsEligibilityStatus : qint8 {
60         DnsEligibilityUnknown = -1,
61         DnsIneligible = 0,
62         DnsEligible = 1
63     };
64 
65     QNetworkAddressEntry();
66     QNetworkAddressEntry(const QNetworkAddressEntry &other);
67     QNetworkAddressEntry &operator=(QNetworkAddressEntry &&other) noexcept { swap(other); return *this; }
68     QNetworkAddressEntry &operator=(const QNetworkAddressEntry &other);
69     ~QNetworkAddressEntry();
70 
swap(QNetworkAddressEntry & other)71     void swap(QNetworkAddressEntry &other) noexcept { qSwap(d, other.d); }
72 
73     bool operator==(const QNetworkAddressEntry &other) const;
74     inline bool operator!=(const QNetworkAddressEntry &other) const
75     { return !(*this == other); }
76 
77     DnsEligibilityStatus dnsEligibility() const;
78     void setDnsEligibility(DnsEligibilityStatus status);
79 
80     QHostAddress ip() const;
81     void setIp(const QHostAddress &newIp);
82 
83     QHostAddress netmask() const;
84     void setNetmask(const QHostAddress &newNetmask);
85     int prefixLength() const;
86     void setPrefixLength(int length);
87 
88     QHostAddress broadcast() const;
89     void setBroadcast(const QHostAddress &newBroadcast);
90 
91     bool isLifetimeKnown() const;
92     QDeadlineTimer preferredLifetime() const;
93     QDeadlineTimer validityLifetime() const;
94     void setAddressLifetime(QDeadlineTimer preferred, QDeadlineTimer validity);
95     void clearAddressLifetime();
96     bool isPermanent() const;
isTemporary()97     bool isTemporary() const { return !isPermanent(); }
98 
99 private:
100     QScopedPointer<QNetworkAddressEntryPrivate> d;
101 };
102 
Q_DECLARE_SHARED(QNetworkAddressEntry)103 Q_DECLARE_SHARED(QNetworkAddressEntry)
104 
105 class QNetworkInterfacePrivate;
106 class Q_NETWORK_EXPORT QNetworkInterface
107 {
108     Q_GADGET
109 public:
110     enum InterfaceFlag {
111         IsUp = 0x1,
112         IsRunning = 0x2,
113         CanBroadcast = 0x4,
114         IsLoopBack = 0x8,
115         IsPointToPoint = 0x10,
116         CanMulticast = 0x20
117     };
118     Q_DECLARE_FLAGS(InterfaceFlags, InterfaceFlag)
119     Q_FLAG(InterfaceFlags)
120 
121     enum InterfaceType {
122         Loopback = 1,
123         Virtual,
124         Ethernet,
125         Slip,
126         CanBus,
127         Ppp,
128         Fddi,
129         Wifi,
130         Ieee80211 = Wifi,   // alias
131         Phonet,
132         Ieee802154,
133         SixLoWPAN,  // 6LoWPAN, but we can't start with a digit
134         Ieee80216,
135         Ieee1394,
136 
137         Unknown = 0
138     };
139     Q_ENUM(InterfaceType)
140 
141     QNetworkInterface();
142     QNetworkInterface(const QNetworkInterface &other);
143     QNetworkInterface &operator=(QNetworkInterface &&other) noexcept { swap(other); return *this; }
144     QNetworkInterface &operator=(const QNetworkInterface &other);
145     ~QNetworkInterface();
146 
147     void swap(QNetworkInterface &other) noexcept { qSwap(d, other.d); }
148 
149     bool isValid() const;
150 
151     int index() const;
152     int maximumTransmissionUnit() const;
153     QString name() const;
154     QString humanReadableName() const;
155     InterfaceFlags flags() const;
156     InterfaceType type() const;
157     QString hardwareAddress() const;
158     QList<QNetworkAddressEntry> addressEntries() const;
159 
160     static int interfaceIndexFromName(const QString &name);
161     static QNetworkInterface interfaceFromName(const QString &name);
162     static QNetworkInterface interfaceFromIndex(int index);
163     static QString interfaceNameFromIndex(int index);
164     static QList<QNetworkInterface> allInterfaces();
165     static QList<QHostAddress> allAddresses();
166 
167 private:
168     friend class QNetworkInterfacePrivate;
169     QSharedDataPointer<QNetworkInterfacePrivate> d;
170 };
171 
172 Q_DECLARE_SHARED(QNetworkInterface)
173 
174 Q_DECLARE_OPERATORS_FOR_FLAGS(QNetworkInterface::InterfaceFlags)
175 
176 #ifndef QT_NO_DEBUG_STREAM
177 Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface);
178 #endif
179 
180 QT_END_NAMESPACE
181 
182 Q_DECLARE_METATYPE(QNetworkAddressEntry)
183 Q_DECLARE_METATYPE(QNetworkInterface)
184 
185 #endif // QT_NO_NETWORKINTERFACE
186 
187 #endif
188