1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include "qnetworkinterface.h"
44 #include "qnetworkinterface_p.h"
45 #include "qalgorithms.h"
46 #include "private/qnet_unix_p.h"
48 #ifndef QT_NO_NETWORKINTERFACE
50 #define IP_MULTICAST // make AIX happy and define IFF_MULTICAST
52 #include <sys/types.h>
53 #include <sys/socket.h>
56 # include <sys/sockio.h>
60 #if defined(QT_LINUXBASE)
61 # define QT_NO_GETIFADDRS
64 #ifndef QT_NO_GETIFADDRS
69 # include <arpa/inet.h>
70 # ifndef SIOCGIFBRDADDR
71 # define SIOCGIFBRDADDR 0x8919
73 #endif // QT_LINUXBASE
75 #include <qplatformdefs.h>
79 static QHostAddress addressFromSockaddr(sockaddr *sa)
85 if (sa->sa_family == AF_INET)
86 address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr));
87 else if (sa->sa_family == AF_INET6)
88 address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr);
94 static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags)
96 QNetworkInterface::InterfaceFlags flags = 0;
97 flags |= (rawFlags & IFF_UP) ? QNetworkInterface::IsUp : QNetworkInterface::InterfaceFlag(0);
98 flags |= (rawFlags & IFF_RUNNING) ? QNetworkInterface::IsRunning : QNetworkInterface::InterfaceFlag(0);
99 flags |= (rawFlags & IFF_BROADCAST) ? QNetworkInterface::CanBroadcast : QNetworkInterface::InterfaceFlag(0);
100 flags |= (rawFlags & IFF_LOOPBACK) ? QNetworkInterface::IsLoopBack : QNetworkInterface::InterfaceFlag(0);
101 #ifdef IFF_POINTOPOINT //cygwin doesn't define IFF_POINTOPOINT
102 flags |= (rawFlags & IFF_POINTOPOINT) ? QNetworkInterface::IsPointToPoint : QNetworkInterface::InterfaceFlag(0);
106 flags |= (rawFlags & IFF_MULTICAST) ? QNetworkInterface::CanMulticast : QNetworkInterface::InterfaceFlag(0);
111 #ifdef QT_NO_GETIFADDRS
112 // getifaddrs not available
114 static const int STORAGEBUFFER_GROWTH = 256;
116 static QSet<QByteArray> interfaceNames(int socket)
118 QSet<QByteArray> result;
119 #ifdef QT_NO_IPV6IFNAME
120 QByteArray storageBuffer;
121 struct ifconf interfaceList;
124 // grow the storage buffer
125 storageBuffer.resize(storageBuffer.size() + STORAGEBUFFER_GROWTH);
126 interfaceList.ifc_buf = storageBuffer.data();
127 interfaceList.ifc_len = storageBuffer.size();
129 // get the interface list
130 if (qt_safe_ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) {
131 if (int(interfaceList.ifc_len + sizeof(ifreq) + 64) < storageBuffer.size()) {
132 // if the buffer was big enough, break
133 storageBuffer.resize(interfaceList.ifc_len);
140 if (storageBuffer.size() > 100000) {
146 int interfaceCount = interfaceList.ifc_len / sizeof(ifreq);
147 for (int i = 0; i < interfaceCount; ++i) {
148 QByteArray name = QByteArray(interfaceList.ifc_req[i].ifr_name);
158 struct if_nameindex *interfaceList = ::if_nameindex();
159 for (struct if_nameindex *ptr = interfaceList; ptr && ptr->if_name; ++ptr)
160 result << ptr->if_name;
162 if_freenameindex(interfaceList);
167 static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfacePrivate *> &interfaces,
170 QNetworkInterfacePrivate *iface = 0;
173 #ifndef QT_NO_IPV6IFNAME
174 // Get the interface index
175 ifindex = if_nametoindex(req.ifr_name);
177 // find the interface data
178 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
179 for ( ; if_it != interfaces.end(); ++if_it)
180 if ((*if_it)->index == ifindex) {
181 // existing interface
187 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
188 for ( ; if_it != interfaces.end(); ++if_it)
189 if ((*if_it)->name == QLatin1String(req.ifr_name)) {
190 // existing interface
197 // new interface, create data:
198 iface = new QNetworkInterfacePrivate;
199 iface->index = ifindex;
203 // Get the canonical name
204 QByteArray oldName = req.ifr_name;
205 if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) {
206 iface->name = QString::fromLatin1(req.ifr_name);
209 memcpy(req.ifr_name, oldName, qMin<int>(oldName.length() + 1, sizeof(req.ifr_name) - 1));
213 // use this name anyways
214 iface->name = QString::fromLatin1(req.ifr_name);
217 // Get interface flags
218 if (qt_safe_ioctl(socket, SIOCGIFFLAGS, &req) >= 0) {
219 iface->flags = convertFlags(req.ifr_flags);
223 // Get the HW address
224 if (qt_safe_ioctl(socket, SIOCGIFHWADDR, &req) >= 0) {
225 uchar *addr = (uchar *)req.ifr_addr.sa_data;
226 iface->hardwareAddress = iface->makeHwAddress(6, addr);
234 static QList<QNetworkInterfacePrivate *> interfaceListing()
236 QList<QNetworkInterfacePrivate *> interfaces;
239 if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
240 return interfaces; // error
242 QSet<QByteArray> names = interfaceNames(socket);
243 QSet<QByteArray>::ConstIterator it = names.constBegin();
244 for ( ; it != names.constEnd(); ++it) {
246 memset(&req, 0, sizeof(ifreq));
247 memcpy(req.ifr_name, *it, qMin<int>(it->length() + 1, sizeof(req.ifr_name) - 1));
249 QNetworkInterfacePrivate *iface = findInterface(socket, interfaces, req);
251 // Get the interface broadcast address
252 QNetworkAddressEntry entry;
253 if (iface->flags & QNetworkInterface::CanBroadcast) {
254 if (qt_safe_ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) {
255 sockaddr *sa = &req.ifr_addr;
256 if (sa->sa_family == AF_INET)
257 entry.setBroadcast(addressFromSockaddr(sa));
261 // Get the interface netmask
262 if (qt_safe_ioctl(socket, SIOCGIFNETMASK, &req) >= 0) {
263 sockaddr *sa = &req.ifr_addr;
264 entry.setNetmask(addressFromSockaddr(sa));
267 // Get the address of the interface
268 if (qt_safe_ioctl(socket, SIOCGIFADDR, &req) >= 0) {
269 sockaddr *sa = &req.ifr_addr;
270 entry.setIp(addressFromSockaddr(sa));
273 iface->addressEntries << entry;
283 // platform-specific defs:
285 QT_BEGIN_INCLUDE_NAMESPACE
286 # include <features.h>
287 QT_END_INCLUDE_NAMESPACE
290 # if defined(Q_OS_LINUX) && __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1 && !defined(QT_LINUXBASE)
291 # include <netpacket/packet.h>
293 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
295 QList<QNetworkInterfacePrivate *> interfaces;
297 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {
298 if ( !ptr->ifa_addr )
301 // Get the interface index
302 int ifindex = if_nametoindex(ptr->ifa_name);
304 // on Linux we use AF_PACKET and sockaddr_ll to obtain hHwAddress
305 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
306 for ( ; if_it != interfaces.end(); ++if_it)
307 if ((*if_it)->index == ifindex) {
308 // this one has been added already
309 if ( ptr->ifa_addr->sa_family == AF_PACKET
310 && (*if_it)->hardwareAddress.isEmpty()) {
311 sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;
312 (*if_it)->hardwareAddress = (*if_it)->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);
316 if ( if_it != interfaces.end() )
319 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
321 iface->index = ifindex;
322 iface->name = QString::fromLatin1(ptr->ifa_name);
323 iface->flags = convertFlags(ptr->ifa_flags);
325 if ( ptr->ifa_addr->sa_family == AF_PACKET ) {
326 sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;
327 iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);
334 # elif defined(Q_OS_BSD4)
335 QT_BEGIN_INCLUDE_NAMESPACE
336 # include <net/if_dl.h>
337 QT_END_INCLUDE_NAMESPACE
339 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
341 QList<QNetworkInterfacePrivate *> interfaces;
343 // on NetBSD we use AF_LINK and sockaddr_dl
344 // scan the list for that family
345 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next)
346 if (ptr->ifa_addr && ptr->ifa_addr->sa_family == AF_LINK) {
347 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
350 sockaddr_dl *sdl = (sockaddr_dl *)ptr->ifa_addr;
351 iface->index = sdl->sdl_index;
352 iface->name = QString::fromLatin1(ptr->ifa_name);
353 iface->flags = convertFlags(ptr->ifa_flags);
354 iface->hardwareAddress = iface->makeHwAddress(sdl->sdl_alen, (uchar*)LLADDR(sdl));
360 # else // Generic version
362 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
364 QList<QNetworkInterfacePrivate *> interfaces;
366 // make sure there's one entry for each interface
367 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {
368 // Get the interface index
369 int ifindex = if_nametoindex(ptr->ifa_name);
371 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
372 for ( ; if_it != interfaces.end(); ++if_it)
373 if ((*if_it)->index == ifindex)
374 // this one has been added already
377 if (if_it == interfaces.end()) {
378 // none found, create
379 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
382 iface->index = ifindex;
383 iface->name = QString::fromLatin1(ptr->ifa_name);
384 iface->flags = convertFlags(ptr->ifa_flags);
394 static QList<QNetworkInterfacePrivate *> interfaceListing()
396 QList<QNetworkInterfacePrivate *> interfaces;
399 if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
400 return interfaces; // error
402 ifaddrs *interfaceListing;
403 if (getifaddrs(&interfaceListing) == -1) {
409 interfaces = createInterfaces(interfaceListing);
410 for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) {
411 // Get the interface index
412 int ifindex = if_nametoindex(ptr->ifa_name);
413 QNetworkInterfacePrivate *iface = 0;
414 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
415 for ( ; if_it != interfaces.end(); ++if_it)
416 if ((*if_it)->index == ifindex) {
417 // found this interface already
422 // skip all non-IP interfaces
426 QNetworkAddressEntry entry;
427 entry.setIp(addressFromSockaddr(ptr->ifa_addr));
428 if (entry.ip().isNull())
429 // could not parse the address
432 entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask));
433 if (iface->flags & QNetworkInterface::CanBroadcast)
434 entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr));
436 iface->addressEntries << entry;
439 freeifaddrs(interfaceListing);
445 QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan()
447 return interfaceListing();
452 #endif // QT_NO_NETWORKINTERFACE