Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/kernel/qnetworkinterface.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
6 | ** | - | ||||||||||||
7 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
8 | ** | - | ||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
17 | ** | - | ||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** GNU General Public License Usage | - | ||||||||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
36 | ** | - | ||||||||||||
37 | ** $QT_END_LICENSE$ | - | ||||||||||||
38 | ** | - | ||||||||||||
39 | ****************************************************************************/ | - | ||||||||||||
40 | - | |||||||||||||
41 | #include "qnetworkinterface.h" | - | ||||||||||||
42 | #include "qnetworkinterface_p.h" | - | ||||||||||||
43 | - | |||||||||||||
44 | #include "qdebug.h" | - | ||||||||||||
45 | #include "qendian.h" | - | ||||||||||||
46 | #include "private/qtools_p.h" | - | ||||||||||||
47 | - | |||||||||||||
48 | #ifndef QT_NO_NETWORKINTERFACE | - | ||||||||||||
49 | - | |||||||||||||
50 | QT_BEGIN_NAMESPACE | - | ||||||||||||
51 | - | |||||||||||||
52 | static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list) | - | ||||||||||||
53 | { | - | ||||||||||||
54 | // Some platforms report a netmask but don't report a broadcast address | - | ||||||||||||
55 | // Go through all available addresses and calculate the broadcast address | - | ||||||||||||
56 | // from the IP and the netmask | - | ||||||||||||
57 | // | - | ||||||||||||
58 | // This is an IPv4-only thing -- IPv6 has no concept of broadcasts | - | ||||||||||||
59 | // The math is: | - | ||||||||||||
60 | // broadcast = IP | ~netmask | - | ||||||||||||
61 | - | |||||||||||||
62 | QList<QNetworkInterfacePrivate *>::Iterator it = list.begin(); | - | ||||||||||||
63 | const QList<QNetworkInterfacePrivate *>::Iterator end = list.end(); | - | ||||||||||||
64 | for ( ; it != end; ++it) {
| 204-612 | ||||||||||||
65 | QList<QNetworkAddressEntry>::Iterator addr_it = (*it)->addressEntries.begin(); | - | ||||||||||||
66 | const QList<QNetworkAddressEntry>::Iterator addr_end = (*it)->addressEntries.end(); | - | ||||||||||||
67 | for ( ; addr_it != addr_end; ++addr_it) {
| 612-1224 | ||||||||||||
68 | if (addr_it->ip().protocol() != QAbstractSocket::IPv4Protocol)
| 612 | ||||||||||||
69 | continue; executed 612 times by 19 tests: continue; Executed by:
| 612 | ||||||||||||
70 | - | |||||||||||||
71 | if (!addr_it->netmask().isNull() && addr_it->broadcast().isNull()) {
| 0-612 | ||||||||||||
72 | QHostAddress bcast = addr_it->ip(); | - | ||||||||||||
73 | bcast = QHostAddress(bcast.toIPv4Address() | ~addr_it->netmask().toIPv4Address()); | - | ||||||||||||
74 | addr_it->setBroadcast(bcast); | - | ||||||||||||
75 | } executed 204 times by 19 tests: end of block Executed by:
| 204 | ||||||||||||
76 | } executed 612 times by 19 tests: end of block Executed by:
| 612 | ||||||||||||
77 | } executed 612 times by 19 tests: end of block Executed by:
| 612 | ||||||||||||
78 | - | |||||||||||||
79 | return list; executed 204 times by 19 tests: return list; Executed by:
| 204 | ||||||||||||
80 | } | - | ||||||||||||
81 | - | |||||||||||||
82 | Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager) executed 20 times by 20 tests: end of block Executed by:
executed 20 times by 20 tests: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 204 times by 19 tests: return &holder.value; Executed by:
| 0-204 | ||||||||||||
83 | - | |||||||||||||
84 | QNetworkInterfaceManager::QNetworkInterfaceManager() | - | ||||||||||||
85 | { | - | ||||||||||||
86 | } | - | ||||||||||||
87 | - | |||||||||||||
88 | QNetworkInterfaceManager::~QNetworkInterfaceManager() | - | ||||||||||||
89 | { | - | ||||||||||||
90 | } | - | ||||||||||||
91 | - | |||||||||||||
92 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name) | - | ||||||||||||
93 | { | - | ||||||||||||
94 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces(); | - | ||||||||||||
95 | QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin(); | - | ||||||||||||
96 | - | |||||||||||||
97 | bool ok; | - | ||||||||||||
98 | uint index = name.toUInt(&ok); | - | ||||||||||||
99 | - | |||||||||||||
100 | for ( ; it != interfaceList.constEnd(); ++it) {
| 1-16 | ||||||||||||
101 | if (ok && (*it)->index == int(index))
| 0-16 | ||||||||||||
102 | return *it; never executed: return *it; | 0 | ||||||||||||
103 | else if ((*it)->name == name)
| 6-10 | ||||||||||||
104 | return *it; executed 6 times by 2 tests: return *it; Executed by:
| 6 | ||||||||||||
105 | } executed 10 times by 2 tests: end of block Executed by:
| 10 | ||||||||||||
106 | - | |||||||||||||
107 | return empty; executed 1 time by 1 test: return empty; Executed by:
| 1 | ||||||||||||
108 | } | - | ||||||||||||
109 | - | |||||||||||||
110 | QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromIndex(int index) | - | ||||||||||||
111 | { | - | ||||||||||||
112 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces(); | - | ||||||||||||
113 | QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin(); | - | ||||||||||||
114 | for ( ; it != interfaceList.constEnd(); ++it)
| 18-66 | ||||||||||||
115 | if ((*it)->index == index)
| 6-60 | ||||||||||||
116 | return *it; executed 6 times by 2 tests: return *it; Executed by:
| 6 | ||||||||||||
117 | - | |||||||||||||
118 | return empty; executed 18 times by 16 tests: return empty; Executed by:
| 18 | ||||||||||||
119 | } | - | ||||||||||||
120 | - | |||||||||||||
121 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces() | - | ||||||||||||
122 | { | - | ||||||||||||
123 | const QList<QNetworkInterfacePrivate *> list = postProcess(scan()); | - | ||||||||||||
124 | QList<QSharedDataPointer<QNetworkInterfacePrivate> > result; | - | ||||||||||||
125 | result.reserve(list.size()); | - | ||||||||||||
126 | - | |||||||||||||
127 | for (QNetworkInterfacePrivate *ptr : list) | - | ||||||||||||
128 | result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr); executed 612 times by 19 tests: result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr); Executed by:
| 612 | ||||||||||||
129 | - | |||||||||||||
130 | return result; executed 204 times by 19 tests: return result; Executed by:
| 204 | ||||||||||||
131 | } | - | ||||||||||||
132 | - | |||||||||||||
133 | QString QNetworkInterfacePrivate::makeHwAddress(int len, uchar *data) | - | ||||||||||||
134 | { | - | ||||||||||||
135 | const int outLen = qMax(len * 2 + (len - 1) * 1, 0); | - | ||||||||||||
136 | QString result(outLen, Qt::Uninitialized); | - | ||||||||||||
137 | QChar *out = result.data(); | - | ||||||||||||
138 | for (int i = 0; i < len; ++i) {
| 612-3672 | ||||||||||||
139 | if (i)
| 612-3060 | ||||||||||||
140 | *out++ = QLatin1Char(':'); executed 3060 times by 19 tests: *out++ = QLatin1Char(':'); Executed by:
| 3060 | ||||||||||||
141 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] / 16)); | - | ||||||||||||
142 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] % 16)); | - | ||||||||||||
143 | } executed 3672 times by 19 tests: end of block Executed by:
| 3672 | ||||||||||||
144 | return result; executed 612 times by 19 tests: return result; Executed by:
| 612 | ||||||||||||
145 | } | - | ||||||||||||
146 | - | |||||||||||||
147 | /*! | - | ||||||||||||
148 | \class QNetworkAddressEntry | - | ||||||||||||
149 | \brief The QNetworkAddressEntry class stores one IP address | - | ||||||||||||
150 | supported by a network interface, along with its associated | - | ||||||||||||
151 | netmask and broadcast address. | - | ||||||||||||
152 | - | |||||||||||||
153 | \since 4.2 | - | ||||||||||||
154 | \reentrant | - | ||||||||||||
155 | \ingroup network | - | ||||||||||||
156 | \ingroup shared | - | ||||||||||||
157 | \inmodule QtNetwork | - | ||||||||||||
158 | - | |||||||||||||
159 | Each network interface can contain zero or more IP addresses, which | - | ||||||||||||
160 | in turn can be associated with a netmask and/or a broadcast | - | ||||||||||||
161 | address (depending on support from the operating system). | - | ||||||||||||
162 | - | |||||||||||||
163 | This class represents one such group. | - | ||||||||||||
164 | */ | - | ||||||||||||
165 | - | |||||||||||||
166 | /*! | - | ||||||||||||
167 | Constructs an empty QNetworkAddressEntry object. | - | ||||||||||||
168 | */ | - | ||||||||||||
169 | QNetworkAddressEntry::QNetworkAddressEntry() | - | ||||||||||||
170 | : d(new QNetworkAddressEntryPrivate) | - | ||||||||||||
171 | { | - | ||||||||||||
172 | } executed 1860 times by 20 tests: end of block Executed by:
| 1860 | ||||||||||||
173 | - | |||||||||||||
174 | /*! | - | ||||||||||||
175 | Constructs a QNetworkAddressEntry object that is a copy of the | - | ||||||||||||
176 | object \a other. | - | ||||||||||||
177 | */ | - | ||||||||||||
178 | QNetworkAddressEntry::QNetworkAddressEntry(const QNetworkAddressEntry &other) | - | ||||||||||||
179 | : d(new QNetworkAddressEntryPrivate(*other.d.data())) | - | ||||||||||||
180 | { | - | ||||||||||||
181 | } executed 1248 times by 19 tests: end of block Executed by:
| 1248 | ||||||||||||
182 | - | |||||||||||||
183 | /*! | - | ||||||||||||
184 | Makes a copy of the QNetworkAddressEntry object \a other. | - | ||||||||||||
185 | */ | - | ||||||||||||
186 | QNetworkAddressEntry &QNetworkAddressEntry::operator=(const QNetworkAddressEntry &other) | - | ||||||||||||
187 | { | - | ||||||||||||
188 | *d.data() = *other.d.data(); | - | ||||||||||||
189 | return *this; executed 1 time by 1 test: return *this; Executed by:
| 1 | ||||||||||||
190 | } | - | ||||||||||||
191 | - | |||||||||||||
192 | /*! | - | ||||||||||||
193 | \fn void QNetworkAddressEntry::swap(QNetworkAddressEntry &other) | - | ||||||||||||
194 | \since 5.0 | - | ||||||||||||
195 | - | |||||||||||||
196 | Swaps this network address entry instance with \a other. This | - | ||||||||||||
197 | function is very fast and never fails. | - | ||||||||||||
198 | */ | - | ||||||||||||
199 | - | |||||||||||||
200 | /*! | - | ||||||||||||
201 | Destroys this QNetworkAddressEntry object. | - | ||||||||||||
202 | */ | - | ||||||||||||
203 | QNetworkAddressEntry::~QNetworkAddressEntry() | - | ||||||||||||
204 | { | - | ||||||||||||
205 | } | - | ||||||||||||
206 | - | |||||||||||||
207 | /*! | - | ||||||||||||
208 | Returns \c true if this network address entry is the same as \a | - | ||||||||||||
209 | other. | - | ||||||||||||
210 | */ | - | ||||||||||||
211 | bool QNetworkAddressEntry::operator==(const QNetworkAddressEntry &other) const | - | ||||||||||||
212 | { | - | ||||||||||||
213 | if (d == other.d) return true; executed 1 time by 1 test: return true; Executed by:
| 1-4 | ||||||||||||
214 | if (!d || !other.d) return false; never executed: return false;
| 0-4 | ||||||||||||
215 | return d->address == other.d->address && executed 4 times by 1 test: return d->address == other.d->address && d->netmask == other.d->netmask && d->broadcast == other.d->broadcast; Executed by:
| 4 | ||||||||||||
216 | d->netmask == other.d->netmask && executed 4 times by 1 test: return d->address == other.d->address && d->netmask == other.d->netmask && d->broadcast == other.d->broadcast; Executed by:
| 4 | ||||||||||||
217 | d->broadcast == other.d->broadcast; executed 4 times by 1 test: return d->address == other.d->address && d->netmask == other.d->netmask && d->broadcast == other.d->broadcast; Executed by:
| 4 | ||||||||||||
218 | } | - | ||||||||||||
219 | - | |||||||||||||
220 | /*! | - | ||||||||||||
221 | \fn bool QNetworkAddressEntry::operator!=(const QNetworkAddressEntry &other) const | - | ||||||||||||
222 | - | |||||||||||||
223 | Returns \c true if this network address entry is different from \a | - | ||||||||||||
224 | other. | - | ||||||||||||
225 | */ | - | ||||||||||||
226 | - | |||||||||||||
227 | /*! | - | ||||||||||||
228 | This function returns one IPv4 or IPv6 address found, that was | - | ||||||||||||
229 | found in a network interface. | - | ||||||||||||
230 | */ | - | ||||||||||||
231 | QHostAddress QNetworkAddressEntry::ip() const | - | ||||||||||||
232 | { | - | ||||||||||||
233 | return d->address; executed 4698 times by 20 tests: return d->address; Executed by:
| 4698 | ||||||||||||
234 | } | - | ||||||||||||
235 | - | |||||||||||||
236 | /*! | - | ||||||||||||
237 | Sets the IP address the QNetworkAddressEntry object contains to \a | - | ||||||||||||
238 | newIp. | - | ||||||||||||
239 | */ | - | ||||||||||||
240 | void QNetworkAddressEntry::setIp(const QHostAddress &newIp) | - | ||||||||||||
241 | { | - | ||||||||||||
242 | d->address = newIp; | - | ||||||||||||
243 | } executed 1861 times by 20 tests: end of block Executed by:
| 1861 | ||||||||||||
244 | - | |||||||||||||
245 | /*! | - | ||||||||||||
246 | Returns the netmask associated with the IP address. The | - | ||||||||||||
247 | netmask is expressed in the form of an IP address, such as | - | ||||||||||||
248 | 255.255.0.0. | - | ||||||||||||
249 | - | |||||||||||||
250 | For IPv6 addresses, the prefix length is converted to an address | - | ||||||||||||
251 | where the number of bits set to 1 is equal to the prefix | - | ||||||||||||
252 | length. For a prefix length of 64 bits (the most common value), | - | ||||||||||||
253 | the netmask will be expressed as a QHostAddress holding the | - | ||||||||||||
254 | address FFFF:FFFF:FFFF:FFFF:: | - | ||||||||||||
255 | - | |||||||||||||
256 | \sa prefixLength() | - | ||||||||||||
257 | */ | - | ||||||||||||
258 | QHostAddress QNetworkAddressEntry::netmask() const | - | ||||||||||||
259 | { | - | ||||||||||||
260 | return d->netmask; executed 965 times by 20 tests: return d->netmask; Executed by:
| 965 | ||||||||||||
261 | } | - | ||||||||||||
262 | - | |||||||||||||
263 | /*! | - | ||||||||||||
264 | Sets the netmask that this QNetworkAddressEntry object contains to | - | ||||||||||||
265 | \a newNetmask. Setting the netmask also sets the prefix length to | - | ||||||||||||
266 | match the new netmask. | - | ||||||||||||
267 | - | |||||||||||||
268 | \sa setPrefixLength() | - | ||||||||||||
269 | */ | - | ||||||||||||
270 | void QNetworkAddressEntry::setNetmask(const QHostAddress &newNetmask) | - | ||||||||||||
271 | { | - | ||||||||||||
272 | if (newNetmask.protocol() != ip().protocol()) {
| 42-1244 | ||||||||||||
273 | d->netmask = QNetmaskAddress(); | - | ||||||||||||
274 | return; executed 42 times by 1 test: return; Executed by:
| 42 | ||||||||||||
275 | } | - | ||||||||||||
276 | - | |||||||||||||
277 | d->netmask.setAddress(newNetmask); | - | ||||||||||||
278 | } executed 1244 times by 20 tests: end of block Executed by:
| 1244 | ||||||||||||
279 | - | |||||||||||||
280 | /*! | - | ||||||||||||
281 | \since 4.5 | - | ||||||||||||
282 | Returns the prefix length of this IP address. The prefix length | - | ||||||||||||
283 | matches the number of bits set to 1 in the netmask (see | - | ||||||||||||
284 | netmask()). For IPv4 addresses, the value is between 0 and 32. For | - | ||||||||||||
285 | IPv6 addresses, it's contained between 0 and 128 and is the | - | ||||||||||||
286 | preferred form of representing addresses. | - | ||||||||||||
287 | - | |||||||||||||
288 | This function returns -1 if the prefix length could not be | - | ||||||||||||
289 | determined (i.e., netmask() returns a null QHostAddress()). | - | ||||||||||||
290 | - | |||||||||||||
291 | \sa netmask() | - | ||||||||||||
292 | */ | - | ||||||||||||
293 | int QNetworkAddressEntry::prefixLength() const | - | ||||||||||||
294 | { | - | ||||||||||||
295 | return d->netmask.prefixLength(); executed 107 times by 2 tests: return d->netmask.prefixLength(); Executed by:
| 107 | ||||||||||||
296 | } | - | ||||||||||||
297 | - | |||||||||||||
298 | /*! | - | ||||||||||||
299 | \since 4.5 | - | ||||||||||||
300 | Sets the prefix length of this IP address to \a length. The value | - | ||||||||||||
301 | of \a length must be valid for this type of IP address: between 0 | - | ||||||||||||
302 | and 32 for IPv4 addresses, between 0 and 128 for IPv6 | - | ||||||||||||
303 | addresses. Setting to any invalid value is equivalent to setting | - | ||||||||||||
304 | to -1, which means "no prefix length". | - | ||||||||||||
305 | - | |||||||||||||
306 | Setting the prefix length also sets the netmask (see netmask()). | - | ||||||||||||
307 | - | |||||||||||||
308 | \sa setNetmask() | - | ||||||||||||
309 | */ | - | ||||||||||||
310 | void QNetworkAddressEntry::setPrefixLength(int length) | - | ||||||||||||
311 | { | - | ||||||||||||
312 | d->netmask.setPrefixLength(d->address.protocol(), length); | - | ||||||||||||
313 | } executed 60 times by 1 test: end of block Executed by:
| 60 | ||||||||||||
314 | - | |||||||||||||
315 | /*! | - | ||||||||||||
316 | Returns the broadcast address associated with the IPv4 | - | ||||||||||||
317 | address and netmask. It can usually be derived from those two by | - | ||||||||||||
318 | setting to 1 the bits of the IP address where the netmask contains | - | ||||||||||||
319 | a 0. (In other words, by bitwise-OR'ing the IP address with the | - | ||||||||||||
320 | inverse of the netmask) | - | ||||||||||||
321 | - | |||||||||||||
322 | This member is always empty for IPv6 addresses, since the concept | - | ||||||||||||
323 | of broadcast has been abandoned in that system in favor of | - | ||||||||||||
324 | multicast. In particular, the group of hosts corresponding to all | - | ||||||||||||
325 | the nodes in the local network can be reached by the "all-nodes" | - | ||||||||||||
326 | special multicast group (address FF02::1). | - | ||||||||||||
327 | */ | - | ||||||||||||
328 | QHostAddress QNetworkAddressEntry::broadcast() const | - | ||||||||||||
329 | { | - | ||||||||||||
330 | return d->broadcast; executed 640 times by 20 tests: return d->broadcast; Executed by:
| 640 | ||||||||||||
331 | } | - | ||||||||||||
332 | - | |||||||||||||
333 | /*! | - | ||||||||||||
334 | Sets the broadcast IP address of this QNetworkAddressEntry object | - | ||||||||||||
335 | to \a newBroadcast. | - | ||||||||||||
336 | */ | - | ||||||||||||
337 | void QNetworkAddressEntry::setBroadcast(const QHostAddress &newBroadcast) | - | ||||||||||||
338 | { | - | ||||||||||||
339 | d->broadcast = newBroadcast; | - | ||||||||||||
340 | } executed 1023 times by 20 tests: end of block Executed by:
| 1023 | ||||||||||||
341 | - | |||||||||||||
342 | /*! | - | ||||||||||||
343 | \class QNetworkInterface | - | ||||||||||||
344 | \brief The QNetworkInterface class provides a listing of the host's IP | - | ||||||||||||
345 | addresses and network interfaces. | - | ||||||||||||
346 | - | |||||||||||||
347 | \since 4.2 | - | ||||||||||||
348 | \reentrant | - | ||||||||||||
349 | \ingroup network | - | ||||||||||||
350 | \ingroup shared | - | ||||||||||||
351 | \inmodule QtNetwork | - | ||||||||||||
352 | - | |||||||||||||
353 | QNetworkInterface represents one network interface attached to the | - | ||||||||||||
354 | host where the program is being run. Each network interface may | - | ||||||||||||
355 | contain zero or more IP addresses, each of which is optionally | - | ||||||||||||
356 | associated with a netmask and/or a broadcast address. The list of | - | ||||||||||||
357 | such trios can be obtained with addressEntries(). Alternatively, | - | ||||||||||||
358 | when the netmask or the broadcast addresses aren't necessary, use | - | ||||||||||||
359 | the allAddresses() convenience function to obtain just the IP | - | ||||||||||||
360 | addresses. | - | ||||||||||||
361 | - | |||||||||||||
362 | QNetworkInterface also reports the interface's hardware address with | - | ||||||||||||
363 | hardwareAddress(). | - | ||||||||||||
364 | - | |||||||||||||
365 | Not all operating systems support reporting all features. Only the | - | ||||||||||||
366 | IPv4 addresses are guaranteed to be listed by this class in all | - | ||||||||||||
367 | platforms. In particular, IPv6 address listing is only supported | - | ||||||||||||
368 | on Windows, Linux, \macos and the BSDs. | - | ||||||||||||
369 | - | |||||||||||||
370 | \sa QNetworkAddressEntry | - | ||||||||||||
371 | */ | - | ||||||||||||
372 | - | |||||||||||||
373 | /*! | - | ||||||||||||
374 | \enum QNetworkInterface::InterfaceFlag | - | ||||||||||||
375 | Specifies the flags associated with this network interface. The | - | ||||||||||||
376 | possible values are: | - | ||||||||||||
377 | - | |||||||||||||
378 | \value IsUp the network interface is active | - | ||||||||||||
379 | \value IsRunning the network interface has resources | - | ||||||||||||
380 | allocated | - | ||||||||||||
381 | \value CanBroadcast the network interface works in | - | ||||||||||||
382 | broadcast mode | - | ||||||||||||
383 | \value IsLoopBack the network interface is a loopback | - | ||||||||||||
384 | interface: that is, it's a virtual | - | ||||||||||||
385 | interface whose destination is the | - | ||||||||||||
386 | host computer itself | - | ||||||||||||
387 | \value IsPointToPoint the network interface is a | - | ||||||||||||
388 | point-to-point interface: that is, | - | ||||||||||||
389 | there is one, single other address | - | ||||||||||||
390 | that can be directly reached by it. | - | ||||||||||||
391 | \value CanMulticast the network interface supports | - | ||||||||||||
392 | multicasting | - | ||||||||||||
393 | - | |||||||||||||
394 | Note that one network interface cannot be both broadcast-based and | - | ||||||||||||
395 | point-to-point. | - | ||||||||||||
396 | */ | - | ||||||||||||
397 | - | |||||||||||||
398 | /*! | - | ||||||||||||
399 | Constructs an empty network interface object. | - | ||||||||||||
400 | */ | - | ||||||||||||
401 | QNetworkInterface::QNetworkInterface() | - | ||||||||||||
402 | : d(0) | - | ||||||||||||
403 | { | - | ||||||||||||
404 | } executed 558 times by 19 tests: end of block Executed by:
| 558 | ||||||||||||
405 | - | |||||||||||||
406 | /*! | - | ||||||||||||
407 | Frees the resources associated with the QNetworkInterface object. | - | ||||||||||||
408 | */ | - | ||||||||||||
409 | QNetworkInterface::~QNetworkInterface() | - | ||||||||||||
410 | { | - | ||||||||||||
411 | } | - | ||||||||||||
412 | - | |||||||||||||
413 | /*! | - | ||||||||||||
414 | Creates a copy of the QNetworkInterface object contained in \a | - | ||||||||||||
415 | other. | - | ||||||||||||
416 | */ | - | ||||||||||||
417 | QNetworkInterface::QNetworkInterface(const QNetworkInterface &other) | - | ||||||||||||
418 | : d(other.d) | - | ||||||||||||
419 | { | - | ||||||||||||
420 | } executed 999 times by 19 tests: end of block Executed by:
| 999 | ||||||||||||
421 | - | |||||||||||||
422 | /*! | - | ||||||||||||
423 | Copies the contents of the QNetworkInterface object contained in \a | - | ||||||||||||
424 | other into this one. | - | ||||||||||||
425 | */ | - | ||||||||||||
426 | QNetworkInterface &QNetworkInterface::operator=(const QNetworkInterface &other) | - | ||||||||||||
427 | { | - | ||||||||||||
428 | d = other.d; | - | ||||||||||||
429 | return *this; never executed: return *this; | 0 | ||||||||||||
430 | } | - | ||||||||||||
431 | - | |||||||||||||
432 | /*! | - | ||||||||||||
433 | \fn void QNetworkInterface::swap(QNetworkInterface &other) | - | ||||||||||||
434 | \since 5.0 | - | ||||||||||||
435 | - | |||||||||||||
436 | Swaps this network interface instance with \a other. This function | - | ||||||||||||
437 | is very fast and never fails. | - | ||||||||||||
438 | */ | - | ||||||||||||
439 | - | |||||||||||||
440 | /*! | - | ||||||||||||
441 | Returns \c true if this QNetworkInterface object contains valid | - | ||||||||||||
442 | information about a network interface. | - | ||||||||||||
443 | */ | - | ||||||||||||
444 | bool QNetworkInterface::isValid() const | - | ||||||||||||
445 | { | - | ||||||||||||
446 | return !name().isEmpty(); executed 497 times by 19 tests: return !name().isEmpty(); Executed by:
| 497 | ||||||||||||
447 | } | - | ||||||||||||
448 | - | |||||||||||||
449 | /*! | - | ||||||||||||
450 | \since 4.5 | - | ||||||||||||
451 | Returns the interface system index, if known. This is an integer | - | ||||||||||||
452 | assigned by the operating system to identify this interface and it | - | ||||||||||||
453 | generally doesn't change. It matches the scope ID field in IPv6 | - | ||||||||||||
454 | addresses. | - | ||||||||||||
455 | - | |||||||||||||
456 | If the index isn't known, this function returns 0. | - | ||||||||||||
457 | */ | - | ||||||||||||
458 | int QNetworkInterface::index() const | - | ||||||||||||
459 | { | - | ||||||||||||
460 | return d ? d->index : 0; executed 648 times by 18 tests: return d ? d->index : 0; Executed by:
| 648 | ||||||||||||
461 | } | - | ||||||||||||
462 | - | |||||||||||||
463 | /*! | - | ||||||||||||
464 | Returns the name of this network interface. On Unix systems, this | - | ||||||||||||
465 | is a string containing the type of the interface and optionally a | - | ||||||||||||
466 | sequence number, such as "eth0", "lo" or "pcn0". On Windows, it's | - | ||||||||||||
467 | an internal ID that cannot be changed by the user. | - | ||||||||||||
468 | */ | - | ||||||||||||
469 | QString QNetworkInterface::name() const | - | ||||||||||||
470 | { | - | ||||||||||||
471 | return d ? d->name : QString(); executed 1247 times by 19 tests: return d ? d->name : QString(); Executed by:
| 1247 | ||||||||||||
472 | } | - | ||||||||||||
473 | - | |||||||||||||
474 | /*! | - | ||||||||||||
475 | \since 4.5 | - | ||||||||||||
476 | - | |||||||||||||
477 | Returns the human-readable name of this network interface on | - | ||||||||||||
478 | Windows, such as "Local Area Connection", if the name could be | - | ||||||||||||
479 | determined. If it couldn't, this function returns the same as | - | ||||||||||||
480 | name(). The human-readable name is a name that the user can modify | - | ||||||||||||
481 | in the Windows Control Panel, so it may change during the | - | ||||||||||||
482 | execution of the program. | - | ||||||||||||
483 | - | |||||||||||||
484 | On Unix, this function currently always returns the same as | - | ||||||||||||
485 | name(), since Unix systems don't store a configuration for | - | ||||||||||||
486 | human-readable names. | - | ||||||||||||
487 | */ | - | ||||||||||||
488 | QString QNetworkInterface::humanReadableName() const | - | ||||||||||||
489 | { | - | ||||||||||||
490 | return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString(); executed 323 times by 18 tests: return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString(); Executed by:
| 323 | ||||||||||||
491 | } | - | ||||||||||||
492 | - | |||||||||||||
493 | /*! | - | ||||||||||||
494 | Returns the flags associated with this network interface. | - | ||||||||||||
495 | */ | - | ||||||||||||
496 | QNetworkInterface::InterfaceFlags QNetworkInterface::flags() const | - | ||||||||||||
497 | { | - | ||||||||||||
498 | return d ? d->flags : InterfaceFlags(0); executed 817 times by 18 tests: return d ? d->flags : InterfaceFlags(0); Executed by:
| 817 | ||||||||||||
499 | } | - | ||||||||||||
500 | - | |||||||||||||
501 | /*! | - | ||||||||||||
502 | Returns the low-level hardware address for this interface. On | - | ||||||||||||
503 | Ethernet interfaces, this will be a MAC address in string | - | ||||||||||||
504 | representation, separated by colons. | - | ||||||||||||
505 | - | |||||||||||||
506 | Other interface types may have other types of hardware | - | ||||||||||||
507 | addresses. Implementations should not depend on this function | - | ||||||||||||
508 | returning a valid MAC address. | - | ||||||||||||
509 | */ | - | ||||||||||||
510 | QString QNetworkInterface::hardwareAddress() const | - | ||||||||||||
511 | { | - | ||||||||||||
512 | return d ? d->hardwareAddress : QString(); executed 4 times by 1 test: return d ? d->hardwareAddress : QString(); Executed by:
| 4 | ||||||||||||
513 | } | - | ||||||||||||
514 | - | |||||||||||||
515 | /*! | - | ||||||||||||
516 | Returns the list of IP addresses that this interface possesses | - | ||||||||||||
517 | along with their associated netmasks and broadcast addresses. | - | ||||||||||||
518 | - | |||||||||||||
519 | If the netmask or broadcast address information is not necessary, | - | ||||||||||||
520 | you can call the allAddresses() function to obtain just the IP | - | ||||||||||||
521 | addresses. | - | ||||||||||||
522 | */ | - | ||||||||||||
523 | QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const | - | ||||||||||||
524 | { | - | ||||||||||||
525 | return d ? d->addressEntries : QList<QNetworkAddressEntry>(); executed 357 times by 19 tests: return d ? d->addressEntries : QList<QNetworkAddressEntry>(); Executed by:
| 357 | ||||||||||||
526 | } | - | ||||||||||||
527 | - | |||||||||||||
528 | /*! | - | ||||||||||||
529 | \since 5.7 | - | ||||||||||||
530 | - | |||||||||||||
531 | Returns the index of the interface whose name is \a name or 0 if there is | - | ||||||||||||
532 | no interface with that name. This function should produce the same result | - | ||||||||||||
533 | as the following code, but will probably execute faster. | - | ||||||||||||
534 | - | |||||||||||||
535 | \code | - | ||||||||||||
536 | QNetworkInterface::interfaceFromName(name).index() | - | ||||||||||||
537 | \endcode | - | ||||||||||||
538 | - | |||||||||||||
539 | \sa interfaceFromName(), interfaceNameFromIndex() | - | ||||||||||||
540 | */ | - | ||||||||||||
541 | int QNetworkInterface::interfaceIndexFromName(const QString &name) | - | ||||||||||||
542 | { | - | ||||||||||||
543 | if (name.isEmpty())
| 27-1556 | ||||||||||||
544 | return 0; executed 1556 times by 15 tests: return 0; Executed by:
| 1556 | ||||||||||||
545 | - | |||||||||||||
546 | bool ok; | - | ||||||||||||
547 | uint id = name.toUInt(&ok); | - | ||||||||||||
548 | if (!ok)
| 0-27 | ||||||||||||
549 | id = QNetworkInterfaceManager::interfaceIndexFromName(name); executed 27 times by 3 tests: id = QNetworkInterfaceManager::interfaceIndexFromName(name); Executed by:
| 27 | ||||||||||||
550 | return int(id); executed 27 times by 3 tests: return int(id); Executed by:
| 27 | ||||||||||||
551 | } | - | ||||||||||||
552 | - | |||||||||||||
553 | /*! | - | ||||||||||||
554 | Returns a QNetworkInterface object for the interface named \a | - | ||||||||||||
555 | name. If no such interface exists, this function returns an | - | ||||||||||||
556 | invalid QNetworkInterface object. | - | ||||||||||||
557 | - | |||||||||||||
558 | The string \a name may be either an actual interface name (such as "eth0" | - | ||||||||||||
559 | or "en1") or an interface index in string form ("1", "2", etc.). | - | ||||||||||||
560 | - | |||||||||||||
561 | \sa name(), isValid() | - | ||||||||||||
562 | */ | - | ||||||||||||
563 | QNetworkInterface QNetworkInterface::interfaceFromName(const QString &name) | - | ||||||||||||
564 | { | - | ||||||||||||
565 | QNetworkInterface result; | - | ||||||||||||
566 | result.d = manager()->interfaceFromName(name); | - | ||||||||||||
567 | return result; executed 7 times by 2 tests: return result; Executed by:
| 7 | ||||||||||||
568 | } | - | ||||||||||||
569 | - | |||||||||||||
570 | /*! | - | ||||||||||||
571 | Returns a QNetworkInterface object for the interface whose internal | - | ||||||||||||
572 | ID is \a index. Network interfaces have a unique identifier called | - | ||||||||||||
573 | the "interface index" to distinguish it from other interfaces on | - | ||||||||||||
574 | the system. Often, this value is assigned progressively and | - | ||||||||||||
575 | interfaces being removed and then added again get a different | - | ||||||||||||
576 | value every time. | - | ||||||||||||
577 | - | |||||||||||||
578 | This index is also found in the IPv6 address' scope ID field. | - | ||||||||||||
579 | */ | - | ||||||||||||
580 | QNetworkInterface QNetworkInterface::interfaceFromIndex(int index) | - | ||||||||||||
581 | { | - | ||||||||||||
582 | QNetworkInterface result; | - | ||||||||||||
583 | result.d = manager()->interfaceFromIndex(index); | - | ||||||||||||
584 | return result; executed 24 times by 16 tests: return result; Executed by:
| 24 | ||||||||||||
585 | } | - | ||||||||||||
586 | - | |||||||||||||
587 | /*! | - | ||||||||||||
588 | \since 5.7 | - | ||||||||||||
589 | - | |||||||||||||
590 | Returns the name of the interface whose index is \a index or an empty | - | ||||||||||||
591 | string if there is no interface with that index. This function should | - | ||||||||||||
592 | produce the same result as the following code, but will probably execute | - | ||||||||||||
593 | faster. | - | ||||||||||||
594 | - | |||||||||||||
595 | \code | - | ||||||||||||
596 | QNetworkInterface::interfaceFromIndex(index).name() | - | ||||||||||||
597 | \endcode | - | ||||||||||||
598 | - | |||||||||||||
599 | \sa interfaceFromIndex(), interfaceIndexFromName() | - | ||||||||||||
600 | */ | - | ||||||||||||
601 | QString QNetworkInterface::interfaceNameFromIndex(int index) | - | ||||||||||||
602 | { | - | ||||||||||||
603 | if (!index)
| 0-33 | ||||||||||||
604 | return QString(); never executed: return QString(); | 0 | ||||||||||||
605 | return QNetworkInterfaceManager::interfaceNameFromIndex(index); executed 33 times by 3 tests: return QNetworkInterfaceManager::interfaceNameFromIndex(index); Executed by:
| 33 | ||||||||||||
606 | } | - | ||||||||||||
607 | - | |||||||||||||
608 | /*! | - | ||||||||||||
609 | Returns a listing of all the network interfaces found on the host | - | ||||||||||||
610 | machine. In case of failure it returns a list with zero elements. | - | ||||||||||||
611 | */ | - | ||||||||||||
612 | QList<QNetworkInterface> QNetworkInterface::allInterfaces() | - | ||||||||||||
613 | { | - | ||||||||||||
614 | const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces(); | - | ||||||||||||
615 | QList<QNetworkInterface> result; | - | ||||||||||||
616 | result.reserve(privs.size()); | - | ||||||||||||
617 | for (const auto &p : privs) { | - | ||||||||||||
618 | QNetworkInterface item; | - | ||||||||||||
619 | item.d = p; | - | ||||||||||||
620 | result << item; | - | ||||||||||||
621 | } executed 507 times by 19 tests: end of block Executed by:
| 507 | ||||||||||||
622 | - | |||||||||||||
623 | return result; executed 169 times by 19 tests: return result; Executed by:
| 169 | ||||||||||||
624 | } | - | ||||||||||||
625 | - | |||||||||||||
626 | /*! | - | ||||||||||||
627 | This convenience function returns all IP addresses found on the | - | ||||||||||||
628 | host machine. It is equivalent to calling addressEntries() on all the | - | ||||||||||||
629 | objects returned by allInterfaces() to obtain lists of QHostAddress | - | ||||||||||||
630 | objects then calling QHostAddress::ip() on each of these. | - | ||||||||||||
631 | */ | - | ||||||||||||
632 | QList<QHostAddress> QNetworkInterface::allAddresses() | - | ||||||||||||
633 | { | - | ||||||||||||
634 | const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces(); | - | ||||||||||||
635 | QList<QHostAddress> result; | - | ||||||||||||
636 | for (const auto &p : privs) { | - | ||||||||||||
637 | for (const QNetworkAddressEntry &entry : qAsConst(p->addressEntries)) | - | ||||||||||||
638 | result += entry.ip(); executed 24 times by 2 tests: result += entry.ip(); Executed by:
| 24 | ||||||||||||
639 | } executed 12 times by 2 tests: end of block Executed by:
| 12 | ||||||||||||
640 | - | |||||||||||||
641 | return result; executed 4 times by 2 tests: return result; Executed by:
| 4 | ||||||||||||
642 | } | - | ||||||||||||
643 | - | |||||||||||||
644 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||
645 | static inline QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags) | - | ||||||||||||
646 | { | - | ||||||||||||
647 | if (flags & QNetworkInterface::IsUp)
| 0 | ||||||||||||
648 | debug << "IsUp "; never executed: debug << "IsUp "; | 0 | ||||||||||||
649 | if (flags & QNetworkInterface::IsRunning)
| 0 | ||||||||||||
650 | debug << "IsRunning "; never executed: debug << "IsRunning "; | 0 | ||||||||||||
651 | if (flags & QNetworkInterface::CanBroadcast)
| 0 | ||||||||||||
652 | debug << "CanBroadcast "; never executed: debug << "CanBroadcast "; | 0 | ||||||||||||
653 | if (flags & QNetworkInterface::IsLoopBack)
| 0 | ||||||||||||
654 | debug << "IsLoopBack "; never executed: debug << "IsLoopBack "; | 0 | ||||||||||||
655 | if (flags & QNetworkInterface::IsPointToPoint)
| 0 | ||||||||||||
656 | debug << "IsPointToPoint "; never executed: debug << "IsPointToPoint "; | 0 | ||||||||||||
657 | if (flags & QNetworkInterface::CanMulticast)
| 0 | ||||||||||||
658 | debug << "CanMulticast "; never executed: debug << "CanMulticast "; | 0 | ||||||||||||
659 | return debug; never executed: return debug; | 0 | ||||||||||||
660 | } | - | ||||||||||||
661 | - | |||||||||||||
662 | static inline QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry) | - | ||||||||||||
663 | { | - | ||||||||||||
664 | debug << "(address = " << entry.ip(); | - | ||||||||||||
665 | if (!entry.netmask().isNull())
| 0 | ||||||||||||
666 | debug << ", netmask = " << entry.netmask(); never executed: debug << ", netmask = " << entry.netmask(); | 0 | ||||||||||||
667 | if (!entry.broadcast().isNull())
| 0 | ||||||||||||
668 | debug << ", broadcast = " << entry.broadcast(); never executed: debug << ", broadcast = " << entry.broadcast(); | 0 | ||||||||||||
669 | debug << ')'; | - | ||||||||||||
670 | return debug; never executed: return debug; | 0 | ||||||||||||
671 | } | - | ||||||||||||
672 | - | |||||||||||||
673 | QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface) | - | ||||||||||||
674 | { | - | ||||||||||||
675 | QDebugStateSaver saver(debug); | - | ||||||||||||
676 | debug.resetFormat().nospace(); | - | ||||||||||||
677 | debug << "QNetworkInterface(name = " << networkInterface.name() | - | ||||||||||||
678 | << ", hardware address = " << networkInterface.hardwareAddress() | - | ||||||||||||
679 | << ", flags = "; | - | ||||||||||||
680 | flagsDebug(debug, networkInterface.flags()); | - | ||||||||||||
681 | debug << ", entries = " << networkInterface.addressEntries() | - | ||||||||||||
682 | << ")\n"; | - | ||||||||||||
683 | return debug; never executed: return debug; | 0 | ||||||||||||
684 | } | - | ||||||||||||
685 | #endif | - | ||||||||||||
686 | - | |||||||||||||
687 | QT_END_NAMESPACE | - | ||||||||||||
688 | - | |||||||||||||
689 | #endif // QT_NO_NETWORKINTERFACE | - | ||||||||||||
Source code | Switch to Preprocessed file |