Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/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 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qset.h" | - |
43 | #include "qnetworkinterface.h" | - |
44 | #include "qnetworkinterface_p.h" | - |
45 | #include "qalgorithms.h" | - |
46 | #include "private/qnet_unix_p.h" | - |
47 | | - |
48 | #ifndef QT_NO_NETWORKINTERFACE | - |
49 | | - |
50 | #define IP_MULTICAST // make AIX happy and define IFF_MULTICAST | - |
51 | | - |
52 | #include <sys/types.h> | - |
53 | #include <sys/socket.h> | - |
54 | | - |
55 | #ifdef Q_OS_SOLARIS | - |
56 | # include <sys/sockio.h> | - |
57 | #endif | - |
58 | #include <net/if.h> | - |
59 | | - |
60 | #if defined(QT_LINUXBASE) | - |
61 | # define QT_NO_GETIFADDRS | - |
62 | #endif | - |
63 | | - |
64 | #ifdef Q_OS_LINUX_ANDROID | - |
65 | // android lacks if_nameindex | - |
66 | # define QT_NO_IPV6IFNAME | - |
67 | #endif | - |
68 | | - |
69 | #ifndef QT_NO_GETIFADDRS | - |
70 | # include <ifaddrs.h> | - |
71 | #endif | - |
72 | | - |
73 | #ifdef QT_LINUXBASE | - |
74 | # include <arpa/inet.h> | - |
75 | # ifndef SIOCGIFBRDADDR | - |
76 | # define SIOCGIFBRDADDR 0x8919 | - |
77 | # endif | - |
78 | #endif // QT_LINUXBASE | - |
79 | | - |
80 | #include <qplatformdefs.h> | - |
81 | | - |
82 | QT_BEGIN_NAMESPACE | - |
83 | | - |
84 | static QHostAddress addressFromSockaddr(sockaddr *sa) | - |
85 | { | - |
86 | QHostAddress address; executed (the execution status of this line is deduced): QHostAddress address; | - |
87 | if (!sa) evaluated: !sa yes Evaluation Count:489 | yes Evaluation Count:3912 |
| 489-3912 |
88 | return address; executed: return address; Execution Count:489 | 489 |
89 | | - |
90 | if (sa->sa_family == AF_INET) evaluated: sa->sa_family == 2 yes Evaluation Count:1793 | yes Evaluation Count:2119 |
| 1793-2119 |
91 | address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr)); executed: address.setAddress((__extension__ ({ register unsigned int __v, __x = (((sockaddr_in *)sa)->sin_addr.s_addr); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))); Execution Count:1793 never executed: __v = ((((__x) & 0xff000000) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) << 24)); executed: __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); Execution Count:1793 partially evaluated: __builtin_constant_p (__x) no Evaluation Count:0 | yes Evaluation Count:1793 |
| 0-1793 |
92 | else if (sa->sa_family == AF_INET6) { evaluated: sa->sa_family == 10 yes Evaluation Count:1304 | yes Evaluation Count:815 |
| 815-1304 |
93 | address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr); executed (the execution status of this line is deduced): address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.__in6_u.__u6_addr8); | - |
94 | int scope = ((sockaddr_in6 *)sa)->sin6_scope_id; executed (the execution status of this line is deduced): int scope = ((sockaddr_in6 *)sa)->sin6_scope_id; | - |
95 | if (scope) { evaluated: scope yes Evaluation Count:489 | yes Evaluation Count:815 |
| 489-815 |
96 | #ifndef QT_NO_IPV6IFNAME | - |
97 | char scopeid[IFNAMSIZ]; executed (the execution status of this line is deduced): char scopeid[16]; | - |
98 | if (::if_indextoname(scope, scopeid)) { partially evaluated: ::if_indextoname(scope, scopeid) yes Evaluation Count:489 | no Evaluation Count:0 |
| 0-489 |
99 | address.setScopeId(QLatin1String(scopeid)); executed (the execution status of this line is deduced): address.setScopeId(QLatin1String(scopeid)); | - |
100 | } else executed: } Execution Count:489 | 489 |
101 | #endif | - |
102 | address.setScopeId(QString::number(scope)); never executed: address.setScopeId(QString::number(scope)); | 0 |
103 | } | - |
104 | } executed: } Execution Count:1304 | 1304 |
105 | return address; executed: return address; Execution Count:3912 | 3912 |
106 | | - |
107 | } | - |
108 | | - |
109 | static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags) | - |
110 | { | - |
111 | QNetworkInterface::InterfaceFlags flags = 0; executed (the execution status of this line is deduced): QNetworkInterface::InterfaceFlags flags = 0; | - |
112 | flags |= (rawFlags & IFF_UP) ? QNetworkInterface::IsUp : QNetworkInterface::InterfaceFlag(0); evaluated: (rawFlags & IFF_UP) yes Evaluation Count:652 | yes Evaluation Count:163 |
| 163-652 |
113 | flags |= (rawFlags & IFF_RUNNING) ? QNetworkInterface::IsRunning : QNetworkInterface::InterfaceFlag(0); evaluated: (rawFlags & IFF_RUNNING) yes Evaluation Count:652 | yes Evaluation Count:163 |
| 163-652 |
114 | flags |= (rawFlags & IFF_BROADCAST) ? QNetworkInterface::CanBroadcast : QNetworkInterface::InterfaceFlag(0); evaluated: (rawFlags & IFF_BROADCAST) yes Evaluation Count:652 | yes Evaluation Count:163 |
| 163-652 |
115 | flags |= (rawFlags & IFF_LOOPBACK) ? QNetworkInterface::IsLoopBack : QNetworkInterface::InterfaceFlag(0); evaluated: (rawFlags & IFF_LOOPBACK) yes Evaluation Count:163 | yes Evaluation Count:652 |
| 163-652 |
116 | #ifdef IFF_POINTOPOINT //cygwin doesn't define IFF_POINTOPOINT | - |
117 | flags |= (rawFlags & IFF_POINTOPOINT) ? QNetworkInterface::IsPointToPoint : QNetworkInterface::InterfaceFlag(0); partially evaluated: (rawFlags & IFF_POINTOPOINT) no Evaluation Count:0 | yes Evaluation Count:815 |
| 0-815 |
118 | #endif | - |
119 | | - |
120 | #ifdef IFF_MULTICAST | - |
121 | flags |= (rawFlags & IFF_MULTICAST) ? QNetworkInterface::CanMulticast : QNetworkInterface::InterfaceFlag(0); evaluated: (rawFlags & IFF_MULTICAST) yes Evaluation Count:652 | yes Evaluation Count:163 |
| 163-652 |
122 | #endif | - |
123 | return flags; executed: return flags; Execution Count:815 | 815 |
124 | } | - |
125 | | - |
126 | #ifdef QT_NO_GETIFADDRS | - |
127 | // getifaddrs not available | - |
128 | | - |
129 | static const int STORAGEBUFFER_GROWTH = 256; | - |
130 | | - |
131 | static QSet<QByteArray> interfaceNames(int socket) | - |
132 | { | - |
133 | QSet<QByteArray> result; | - |
134 | #ifdef QT_NO_IPV6IFNAME | - |
135 | QByteArray storageBuffer; | - |
136 | struct ifconf interfaceList; | - |
137 | | - |
138 | forever { | - |
139 | // grow the storage buffer | - |
140 | storageBuffer.resize(storageBuffer.size() + STORAGEBUFFER_GROWTH); | - |
141 | interfaceList.ifc_buf = storageBuffer.data(); | - |
142 | interfaceList.ifc_len = storageBuffer.size(); | - |
143 | | - |
144 | // get the interface list | - |
145 | if (qt_safe_ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) { | - |
146 | if (int(interfaceList.ifc_len + sizeof(ifreq) + 64) < storageBuffer.size()) { | - |
147 | // if the buffer was big enough, break | - |
148 | storageBuffer.resize(interfaceList.ifc_len); | - |
149 | break; | - |
150 | } | - |
151 | } else { | - |
152 | // internal error | - |
153 | return result; | - |
154 | } | - |
155 | if (storageBuffer.size() > 100000) { | - |
156 | // out of space | - |
157 | return result; | - |
158 | } | - |
159 | } | - |
160 | | - |
161 | int interfaceCount = interfaceList.ifc_len / sizeof(ifreq); | - |
162 | for (int i = 0; i < interfaceCount; ++i) { | - |
163 | QByteArray name = QByteArray(interfaceList.ifc_req[i].ifr_name); | - |
164 | if (!name.isEmpty()) | - |
165 | result << name; | - |
166 | } | - |
167 | | - |
168 | return result; | - |
169 | #else | - |
170 | Q_UNUSED(socket); | - |
171 | | - |
172 | // use if_nameindex | - |
173 | struct if_nameindex *interfaceList = ::if_nameindex(); | - |
174 | for (struct if_nameindex *ptr = interfaceList; ptr && ptr->if_name; ++ptr) | - |
175 | result << ptr->if_name; | - |
176 | | - |
177 | if_freenameindex(interfaceList); | - |
178 | return result; | - |
179 | #endif | - |
180 | } | - |
181 | | - |
182 | static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfacePrivate *> &interfaces, | - |
183 | struct ifreq &req) | - |
184 | { | - |
185 | QNetworkInterfacePrivate *iface = 0; | - |
186 | int ifindex = 0; | - |
187 | | - |
188 | #ifndef QT_NO_IPV6IFNAME | - |
189 | // Get the interface index | - |
190 | ifindex = if_nametoindex(req.ifr_name); | - |
191 | | - |
192 | // find the interface data | - |
193 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - |
194 | for ( ; if_it != interfaces.end(); ++if_it) | - |
195 | if ((*if_it)->index == ifindex) { | - |
196 | // existing interface | - |
197 | iface = *if_it; | - |
198 | break; | - |
199 | } | - |
200 | #else | - |
201 | // Search by name | - |
202 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - |
203 | for ( ; if_it != interfaces.end(); ++if_it) | - |
204 | if ((*if_it)->name == QLatin1String(req.ifr_name)) { | - |
205 | // existing interface | - |
206 | iface = *if_it; | - |
207 | break; | - |
208 | } | - |
209 | #endif | - |
210 | | - |
211 | if (!iface) { | - |
212 | // new interface, create data: | - |
213 | iface = new QNetworkInterfacePrivate; | - |
214 | iface->index = ifindex; | - |
215 | interfaces << iface; | - |
216 | | - |
217 | #ifdef SIOCGIFNAME | - |
218 | // Get the canonical name | - |
219 | QByteArray oldName = req.ifr_name; | - |
220 | if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) { | - |
221 | iface->name = QString::fromLatin1(req.ifr_name); | - |
222 | | - |
223 | // reset the name: | - |
224 | memcpy(req.ifr_name, oldName, qMin<int>(oldName.length() + 1, sizeof(req.ifr_name) - 1)); | - |
225 | } else | - |
226 | #endif | - |
227 | { | - |
228 | // use this name anyways | - |
229 | iface->name = QString::fromLatin1(req.ifr_name); | - |
230 | } | - |
231 | | - |
232 | // Get interface flags | - |
233 | if (qt_safe_ioctl(socket, SIOCGIFFLAGS, &req) >= 0) { | - |
234 | iface->flags = convertFlags(req.ifr_flags); | - |
235 | } | - |
236 | | - |
237 | #ifdef SIOCGIFHWADDR | - |
238 | // Get the HW address | - |
239 | if (qt_safe_ioctl(socket, SIOCGIFHWADDR, &req) >= 0) { | - |
240 | uchar *addr = (uchar *)req.ifr_addr.sa_data; | - |
241 | iface->hardwareAddress = iface->makeHwAddress(6, addr); | - |
242 | } | - |
243 | #endif | - |
244 | } | - |
245 | | - |
246 | return iface; | - |
247 | } | - |
248 | | - |
249 | static QList<QNetworkInterfacePrivate *> interfaceListing() | - |
250 | { | - |
251 | QList<QNetworkInterfacePrivate *> interfaces; | - |
252 | | - |
253 | int socket; | - |
254 | if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) | - |
255 | return interfaces; // error | - |
256 | | - |
257 | QSet<QByteArray> names = interfaceNames(socket); | - |
258 | QSet<QByteArray>::ConstIterator it = names.constBegin(); | - |
259 | for ( ; it != names.constEnd(); ++it) { | - |
260 | ifreq req; | - |
261 | memset(&req, 0, sizeof(ifreq)); | - |
262 | memcpy(req.ifr_name, *it, qMin<int>(it->length() + 1, sizeof(req.ifr_name) - 1)); | - |
263 | | - |
264 | QNetworkInterfacePrivate *iface = findInterface(socket, interfaces, req); | - |
265 | | - |
266 | // Get the interface broadcast address | - |
267 | QNetworkAddressEntry entry; | - |
268 | if (iface->flags & QNetworkInterface::CanBroadcast) { | - |
269 | if (qt_safe_ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) { | - |
270 | sockaddr *sa = &req.ifr_addr; | - |
271 | if (sa->sa_family == AF_INET) | - |
272 | entry.setBroadcast(addressFromSockaddr(sa)); | - |
273 | } | - |
274 | } | - |
275 | | - |
276 | // Get the interface netmask | - |
277 | if (qt_safe_ioctl(socket, SIOCGIFNETMASK, &req) >= 0) { | - |
278 | sockaddr *sa = &req.ifr_addr; | - |
279 | entry.setNetmask(addressFromSockaddr(sa)); | - |
280 | } | - |
281 | | - |
282 | // Get the address of the interface | - |
283 | if (qt_safe_ioctl(socket, SIOCGIFADDR, &req) >= 0) { | - |
284 | sockaddr *sa = &req.ifr_addr; | - |
285 | entry.setIp(addressFromSockaddr(sa)); | - |
286 | } | - |
287 | | - |
288 | iface->addressEntries << entry; | - |
289 | } | - |
290 | | - |
291 | ::close(socket); | - |
292 | return interfaces; | - |
293 | } | - |
294 | | - |
295 | #else | - |
296 | // use getifaddrs | - |
297 | | - |
298 | // platform-specific defs: | - |
299 | # ifdef Q_OS_LINUX | - |
300 | QT_BEGIN_INCLUDE_NAMESPACE | - |
301 | # include <features.h> | - |
302 | QT_END_INCLUDE_NAMESPACE | - |
303 | # endif | - |
304 | | - |
305 | # if defined(Q_OS_LINUX) && __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1 && !defined(QT_LINUXBASE) | - |
306 | # include <netpacket/packet.h> | - |
307 | | - |
308 | static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList) | - |
309 | { | - |
310 | QList<QNetworkInterfacePrivate *> interfaces; executed (the execution status of this line is deduced): QList<QNetworkInterfacePrivate *> interfaces; | - |
311 | | - |
312 | for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) { evaluated: ptr yes Evaluation Count:2119 | yes Evaluation Count:163 |
| 163-2119 |
313 | if ( !ptr->ifa_addr ) partially evaluated: !ptr->ifa_addr no Evaluation Count:0 | yes Evaluation Count:2119 |
| 0-2119 |
314 | continue; never executed: continue; | 0 |
315 | | - |
316 | // Get the interface index | - |
317 | int ifindex = if_nametoindex(ptr->ifa_name); executed (the execution status of this line is deduced): int ifindex = if_nametoindex(ptr->ifa_name); | - |
318 | | - |
319 | // on Linux we use AF_PACKET and sockaddr_ll to obtain hHwAddress | - |
320 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); executed (the execution status of this line is deduced): QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - |
321 | for ( ; if_it != interfaces.end(); ++if_it) evaluated: if_it != interfaces.end() yes Evaluation Count:5542 | yes Evaluation Count:815 |
| 815-5542 |
322 | if ((*if_it)->index == ifindex) { evaluated: (*if_it)->index == ifindex yes Evaluation Count:1304 | yes Evaluation Count:4238 |
| 1304-4238 |
323 | // this one has been added already | - |
324 | if ( ptr->ifa_addr->sa_family == AF_PACKET partially evaluated: ptr->ifa_addr->sa_family == 17 no Evaluation Count:0 | yes Evaluation Count:1304 |
| 0-1304 |
325 | && (*if_it)->hardwareAddress.isEmpty()) { never evaluated: (*if_it)->hardwareAddress.isEmpty() | 0 |
326 | sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr; never executed (the execution status of this line is deduced): sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr; | - |
327 | (*if_it)->hardwareAddress = (*if_it)->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr); never executed (the execution status of this line is deduced): (*if_it)->hardwareAddress = (*if_it)->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr); | - |
328 | } | 0 |
329 | break; executed: break; Execution Count:1304 | 1304 |
330 | } | - |
331 | if ( if_it != interfaces.end() ) evaluated: if_it != interfaces.end() yes Evaluation Count:1304 | yes Evaluation Count:815 |
| 815-1304 |
332 | continue; executed: continue; Execution Count:1304 | 1304 |
333 | | - |
334 | QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; executed (the execution status of this line is deduced): QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; | - |
335 | interfaces << iface; executed (the execution status of this line is deduced): interfaces << iface; | - |
336 | iface->index = ifindex; executed (the execution status of this line is deduced): iface->index = ifindex; | - |
337 | iface->name = QString::fromLatin1(ptr->ifa_name); executed (the execution status of this line is deduced): iface->name = QString::fromLatin1(ptr->ifa_name); | - |
338 | iface->flags = convertFlags(ptr->ifa_flags); executed (the execution status of this line is deduced): iface->flags = convertFlags(ptr->ifa_flags); | - |
339 | | - |
340 | if ( ptr->ifa_addr->sa_family == AF_PACKET ) { partially evaluated: ptr->ifa_addr->sa_family == 17 yes Evaluation Count:815 | no Evaluation Count:0 |
| 0-815 |
341 | sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr; executed (the execution status of this line is deduced): sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr; | - |
342 | iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr); executed (the execution status of this line is deduced): iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr); | - |
343 | } executed: } Execution Count:815 | 815 |
344 | } executed: } Execution Count:815 | 815 |
345 | | - |
346 | return interfaces; executed: return interfaces; Execution Count:163 | 163 |
347 | } | - |
348 | | - |
349 | # elif defined(Q_OS_BSD4) | - |
350 | QT_BEGIN_INCLUDE_NAMESPACE | - |
351 | # include <net/if_dl.h> | - |
352 | QT_END_INCLUDE_NAMESPACE | - |
353 | | - |
354 | static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList) | - |
355 | { | - |
356 | QList<QNetworkInterfacePrivate *> interfaces; | - |
357 | | - |
358 | // on NetBSD we use AF_LINK and sockaddr_dl | - |
359 | // scan the list for that family | - |
360 | for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) | - |
361 | if (ptr->ifa_addr && ptr->ifa_addr->sa_family == AF_LINK) { | - |
362 | QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; | - |
363 | interfaces << iface; | - |
364 | | - |
365 | sockaddr_dl *sdl = (sockaddr_dl *)ptr->ifa_addr; | - |
366 | iface->index = sdl->sdl_index; | - |
367 | iface->name = QString::fromLatin1(ptr->ifa_name); | - |
368 | iface->flags = convertFlags(ptr->ifa_flags); | - |
369 | iface->hardwareAddress = iface->makeHwAddress(sdl->sdl_alen, (uchar*)LLADDR(sdl)); | - |
370 | } | - |
371 | | - |
372 | return interfaces; | - |
373 | } | - |
374 | | - |
375 | # else // Generic version | - |
376 | | - |
377 | static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList) | - |
378 | { | - |
379 | QList<QNetworkInterfacePrivate *> interfaces; | - |
380 | | - |
381 | // make sure there's one entry for each interface | - |
382 | for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) { | - |
383 | // Get the interface index | - |
384 | int ifindex = if_nametoindex(ptr->ifa_name); | - |
385 | | - |
386 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - |
387 | for ( ; if_it != interfaces.end(); ++if_it) | - |
388 | if ((*if_it)->index == ifindex) | - |
389 | // this one has been added already | - |
390 | break; | - |
391 | | - |
392 | if (if_it == interfaces.end()) { | - |
393 | // none found, create | - |
394 | QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; | - |
395 | interfaces << iface; | - |
396 | | - |
397 | iface->index = ifindex; | - |
398 | iface->name = QString::fromLatin1(ptr->ifa_name); | - |
399 | iface->flags = convertFlags(ptr->ifa_flags); | - |
400 | } | - |
401 | } | - |
402 | | - |
403 | return interfaces; | - |
404 | } | - |
405 | | - |
406 | # endif | - |
407 | | - |
408 | | - |
409 | static QList<QNetworkInterfacePrivate *> interfaceListing() | - |
410 | { | - |
411 | QList<QNetworkInterfacePrivate *> interfaces; executed (the execution status of this line is deduced): QList<QNetworkInterfacePrivate *> interfaces; | - |
412 | | - |
413 | int socket; executed (the execution status of this line is deduced): int socket; | - |
414 | if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) partially evaluated: (socket = qt_safe_socket(2, SOCK_STREAM, IPPROTO_IP)) == -1 no Evaluation Count:0 | yes Evaluation Count:163 |
| 0-163 |
415 | return interfaces; // error never executed: return interfaces; | 0 |
416 | | - |
417 | ifaddrs *interfaceListing; executed (the execution status of this line is deduced): ifaddrs *interfaceListing; | - |
418 | if (getifaddrs(&interfaceListing) == -1) { partially evaluated: getifaddrs(&interfaceListing) == -1 no Evaluation Count:0 | yes Evaluation Count:163 |
| 0-163 |
419 | // error | - |
420 | ::close(socket); never executed (the execution status of this line is deduced): ::close(socket); | - |
421 | return interfaces; never executed: return interfaces; | 0 |
422 | } | - |
423 | | - |
424 | interfaces = createInterfaces(interfaceListing); executed (the execution status of this line is deduced): interfaces = createInterfaces(interfaceListing); | - |
425 | for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) { evaluated: ptr yes Evaluation Count:2119 | yes Evaluation Count:163 |
| 163-2119 |
426 | // Get the interface index | - |
427 | int ifindex = if_nametoindex(ptr->ifa_name); executed (the execution status of this line is deduced): int ifindex = if_nametoindex(ptr->ifa_name); | - |
428 | QNetworkInterfacePrivate *iface = 0; executed (the execution status of this line is deduced): QNetworkInterfacePrivate *iface = 0; | - |
429 | QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); executed (the execution status of this line is deduced): QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); | - |
430 | for ( ; if_it != interfaces.end(); ++if_it) partially evaluated: if_it != interfaces.end() yes Evaluation Count:6357 | no Evaluation Count:0 |
| 0-6357 |
431 | if ((*if_it)->index == ifindex) { evaluated: (*if_it)->index == ifindex yes Evaluation Count:2119 | yes Evaluation Count:4238 |
| 2119-4238 |
432 | // found this interface already | - |
433 | iface = *if_it; executed (the execution status of this line is deduced): iface = *if_it; | - |
434 | break; executed: break; Execution Count:2119 | 2119 |
435 | } | - |
436 | if (!iface) { partially evaluated: !iface no Evaluation Count:0 | yes Evaluation Count:2119 |
| 0-2119 |
437 | // skip all non-IP interfaces | - |
438 | continue; never executed: continue; | 0 |
439 | } | - |
440 | | - |
441 | QNetworkAddressEntry entry; executed (the execution status of this line is deduced): QNetworkAddressEntry entry; | - |
442 | entry.setIp(addressFromSockaddr(ptr->ifa_addr)); executed (the execution status of this line is deduced): entry.setIp(addressFromSockaddr(ptr->ifa_addr)); | - |
443 | if (entry.ip().isNull()) evaluated: entry.ip().isNull() yes Evaluation Count:815 | yes Evaluation Count:1304 |
| 815-1304 |
444 | // could not parse the address | - |
445 | continue; executed: continue; Execution Count:815 | 815 |
446 | | - |
447 | entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask)); executed (the execution status of this line is deduced): entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask)); | - |
448 | if (iface->flags & QNetworkInterface::CanBroadcast) evaluated: iface->flags & QNetworkInterface::CanBroadcast yes Evaluation Count:978 | yes Evaluation Count:326 |
| 326-978 |
449 | entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr)); executed: entry.setBroadcast(addressFromSockaddr(ptr->ifa_ifu.ifu_broadaddr)); Execution Count:978 | 978 |
450 | | - |
451 | iface->addressEntries << entry; executed (the execution status of this line is deduced): iface->addressEntries << entry; | - |
452 | } executed: } Execution Count:1304 | 1304 |
453 | | - |
454 | freeifaddrs(interfaceListing); executed (the execution status of this line is deduced): freeifaddrs(interfaceListing); | - |
455 | ::close(socket); executed (the execution status of this line is deduced): ::close(socket); | - |
456 | return interfaces; executed: return interfaces; Execution Count:163 | 163 |
457 | } | - |
458 | #endif | - |
459 | | - |
460 | QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan() | - |
461 | { | - |
462 | return interfaceListing(); executed: return interfaceListing(); Execution Count:163 | 163 |
463 | } | - |
464 | | - |
465 | QT_END_NAMESPACE | - |
466 | | - |
467 | #endif // QT_NO_NETWORKINTERFACE | - |
468 | | - |
| | |