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) { | - | ||||||
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) { | - | ||||||
68 | if (addr_it->ip().protocol() != QAbstractSocket::IPv4Protocol) | - | ||||||
69 | continue; | - | ||||||
70 | - | |||||||
71 | if (!addr_it->netmask().isNull() && addr_it->broadcast().isNull()) { | - | ||||||
72 | QHostAddress bcast = addr_it->ip(); | - | ||||||
73 | bcast = QHostAddress(bcast.toIPv4Address() | ~addr_it->netmask().toIPv4Address()); | - | ||||||
74 | addr_it->setBroadcast(bcast); | - | ||||||
75 | } | - | ||||||
76 | } | - | ||||||
77 | } | - | ||||||
78 | - | |||||||
79 | return list; | - | ||||||
80 | } | - | ||||||
81 | - | |||||||
82 | Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager) | - | ||||||
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) { | - | ||||||
101 | if (ok && (*it)->index == int(index)) | - | ||||||
102 | return *it; | - | ||||||
103 | else if ((*it)->name == name) | - | ||||||
104 | return *it; | - | ||||||
105 | } | - | ||||||
106 | - | |||||||
107 | return empty; | - | ||||||
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) | - | ||||||
115 | if ((*it)->index == index) | - | ||||||
116 | return *it; | - | ||||||
117 | - | |||||||
118 | return empty; | - | ||||||
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 | foreachfor (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) { | - | ||||||
139 | if (i) | - | ||||||
140 | *out++ = QLatin1Char(':'); | - | ||||||
141 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] / 16)); | - | ||||||
142 | *out++ = QLatin1Char(QtMiscUtils::toHexUpper(data[i] % 16)); | - | ||||||
143 | } | - | ||||||
144 | return result; | - | ||||||
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 | } | - | ||||||
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 | } | - | ||||||
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; | - | ||||||
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; | - | ||||||
214 | if (!d || !other.d) return false; | - | ||||||
215 | return d->address == other.d->address && | - | ||||||
216 | d->netmask == other.d->netmask && | - | ||||||
217 | d->broadcast == other.d->broadcast; | - | ||||||
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; | - | ||||||
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 | } | - | ||||||
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; | - | ||||||
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()) { | - | ||||||
273 | d->netmask = QNetmaskAddress(); | - | ||||||
274 | return; | - | ||||||
275 | } | - | ||||||
276 | - | |||||||
277 | d->netmask.setAddress(newNetmask); | - | ||||||
278 | } | - | ||||||
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(); | - | ||||||
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 | } | - | ||||||
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; | - | ||||||
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 | } | - | ||||||
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 | } | - | ||||||
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 | } | - | ||||||
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; | - | ||||||
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(); | - | ||||||
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; | - | ||||||
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(); | - | ||||||
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(); | - | ||||||
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); | - | ||||||
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(); | - | ||||||
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>(); | - | ||||||
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; | - | ||||||
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; | - | ||||||
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 | foreachfor (const QSharedDataPointer<QNetworkInterfacePrivate>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 | foreachfor (const QSharedDataPointer<QNetworkInterfacePrivate>auto &p ,: privs) { | - | ||||||
637 | foreachfor (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) | - | ||||||
648 | debug << "IsUp "; | - | ||||||
649 | if (flags & QNetworkInterface::IsRunning) | - | ||||||
650 | debug << "IsRunning "; | - | ||||||
651 | if (flags & QNetworkInterface::CanBroadcast) | - | ||||||
652 | debug << "CanBroadcast "; | - | ||||||
653 | if (flags & QNetworkInterface::IsLoopBack) | - | ||||||
654 | debug << "IsLoopBack "; | - | ||||||
655 | if (flags & QNetworkInterface::IsPointToPoint) | - | ||||||
656 | debug << "IsPointToPoint "; | - | ||||||
657 | if (flags & QNetworkInterface::CanMulticast) | - | ||||||
658 | debug << "CanMulticast "; | - | ||||||
659 | return debug; | - | ||||||
660 | } | - | ||||||
661 | - | |||||||
662 | static inline QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry) | - | ||||||
663 | { | - | ||||||
664 | debug << "(address = " << entry.ip(); | - | ||||||
665 | if (!entry.netmask().isNull()) | - | ||||||
666 | debug << ", netmask = " << entry.netmask(); | - | ||||||
667 | if (!entry.broadcast().isNull()) | - | ||||||
668 | debug << ", broadcast = " << entry.broadcast(); | - | ||||||
669 | debug << ')'; | - | ||||||
670 | return debug; | - | ||||||
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; | - | ||||||
684 | } | - | ||||||
685 | #endif | - | ||||||
686 | - | |||||||
687 | QT_END_NAMESPACE | - | ||||||
688 | - | |||||||
689 | #endif // QT_NO_NETWORKINTERFACE | - | ||||||
Source code | Switch to Preprocessed file |