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