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