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 #ifndef QT_NO_GETIFADDRS
65 # include <arpa/inet.h>
66 # ifndef SIOCGIFBRDADDR
67 # define SIOCGIFBRDADDR 0x8919
69 #endif // QT_LINUXBASE
71 #include <qplatformdefs.h>
75 static QHostAddress addressFromSockaddr(sockaddr *sa)
81 if (sa->sa_family == AF_INET)
82 address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr));
83 else if (sa->sa_family == AF_INET6)
84 address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr);
90 static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags)
92 QNetworkInterface::InterfaceFlags flags = 0;
93 flags |= (rawFlags & IFF_UP) ? QNetworkInterface::IsUp : QNetworkInterface::InterfaceFlag(0);
94 flags |= (rawFlags & IFF_RUNNING) ? QNetworkInterface::IsRunning : QNetworkInterface::InterfaceFlag(0);
95 flags |= (rawFlags & IFF_BROADCAST) ? QNetworkInterface::CanBroadcast : QNetworkInterface::InterfaceFlag(0);
96 flags |= (rawFlags & IFF_LOOPBACK) ? QNetworkInterface::IsLoopBack : QNetworkInterface::InterfaceFlag(0);
97 #ifdef IFF_POINTOPOINT //cygwin doesn't define IFF_POINTOPOINT
98 flags |= (rawFlags & IFF_POINTOPOINT) ? QNetworkInterface::IsPointToPoint : QNetworkInterface::InterfaceFlag(0);
102 flags |= (rawFlags & IFF_MULTICAST) ? QNetworkInterface::CanMulticast : QNetworkInterface::InterfaceFlag(0);
107 #ifdef QT_NO_GETIFADDRS
108 // getifaddrs not available
110 static const int STORAGEBUFFER_GROWTH = 256;
112 static QSet<QByteArray> interfaceNames(int socket)
114 QSet<QByteArray> result;
115 #ifdef QT_NO_IPV6IFNAME
116 QByteArray storageBuffer;
117 struct ifconf interfaceList;
120 // grow the storage buffer
121 storageBuffer.resize(storageBuffer.size() + STORAGEBUFFER_GROWTH);
122 interfaceList.ifc_buf = storageBuffer.data();
123 interfaceList.ifc_len = storageBuffer.size();
125 // get the interface list
126 if (qt_safe_ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) {
127 if (int(interfaceList.ifc_len + sizeof(ifreq) + 64) < storageBuffer.size()) {
128 // if the buffer was big enough, break
129 storageBuffer.resize(interfaceList.ifc_len);
136 if (storageBuffer.size() > 100000) {
142 int interfaceCount = interfaceList.ifc_len / sizeof(ifreq);
143 for (int i = 0; i < interfaceCount; ++i) {
144 QByteArray name = QByteArray(interfaceList.ifc_req[i].ifr_name);
154 struct if_nameindex *interfaceList = ::if_nameindex();
155 for (struct if_nameindex *ptr = interfaceList; ptr && ptr->if_name; ++ptr)
156 result << ptr->if_name;
158 if_freenameindex(interfaceList);
163 static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfacePrivate *> &interfaces,
166 QNetworkInterfacePrivate *iface = 0;
169 #ifndef QT_NO_IPV6IFNAME
170 // Get the interface index
171 ifindex = if_nametoindex(req.ifr_name);
173 // find the interface data
174 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
175 for ( ; if_it != interfaces.end(); ++if_it)
176 if ((*if_it)->index == ifindex) {
177 // existing interface
183 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
184 for ( ; if_it != interfaces.end(); ++if_it)
185 if ((*if_it)->name == QLatin1String(req.ifr_name)) {
186 // existing interface
193 // new interface, create data:
194 iface = new QNetworkInterfacePrivate;
195 iface->index = ifindex;
199 // Get the canonical name
200 QByteArray oldName = req.ifr_name;
201 if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) {
202 iface->name = QString::fromLatin1(req.ifr_name);
205 memcpy(req.ifr_name, oldName, qMin<int>(oldName.length() + 1, sizeof(req.ifr_name) - 1));
209 // use this name anyways
210 iface->name = QString::fromLatin1(req.ifr_name);
213 // Get interface flags
214 if (qt_safe_ioctl(socket, SIOCGIFFLAGS, &req) >= 0) {
215 iface->flags = convertFlags(req.ifr_flags);
219 // Get the HW address
220 if (qt_safe_ioctl(socket, SIOCGIFHWADDR, &req) >= 0) {
221 uchar *addr = (uchar *)req.ifr_addr.sa_data;
222 iface->hardwareAddress = iface->makeHwAddress(6, addr);
230 static QList<QNetworkInterfacePrivate *> interfaceListing()
232 QList<QNetworkInterfacePrivate *> interfaces;
235 if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
236 return interfaces; // error
238 QSet<QByteArray> names = interfaceNames(socket);
239 QSet<QByteArray>::ConstIterator it = names.constBegin();
240 for ( ; it != names.constEnd(); ++it) {
242 memset(&req, 0, sizeof(ifreq));
243 memcpy(req.ifr_name, *it, qMin<int>(it->length() + 1, sizeof(req.ifr_name) - 1));
245 QNetworkInterfacePrivate *iface = findInterface(socket, interfaces, req);
247 // Get the interface broadcast address
248 QNetworkAddressEntry entry;
249 if (iface->flags & QNetworkInterface::CanBroadcast) {
250 if (qt_safe_ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) {
251 sockaddr *sa = &req.ifr_addr;
252 if (sa->sa_family == AF_INET)
253 entry.setBroadcast(addressFromSockaddr(sa));
257 // Get the interface netmask
258 if (qt_safe_ioctl(socket, SIOCGIFNETMASK, &req) >= 0) {
259 sockaddr *sa = &req.ifr_addr;
260 entry.setNetmask(addressFromSockaddr(sa));
263 // Get the address of the interface
264 if (qt_safe_ioctl(socket, SIOCGIFADDR, &req) >= 0) {
265 sockaddr *sa = &req.ifr_addr;
266 entry.setIp(addressFromSockaddr(sa));
269 iface->addressEntries << entry;
279 // platform-specific defs:
281 QT_BEGIN_INCLUDE_NAMESPACE
282 # include <features.h>
283 QT_END_INCLUDE_NAMESPACE
286 # if defined(Q_OS_LINUX) && __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1
287 # include <netpacket/packet.h>
289 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
291 QList<QNetworkInterfacePrivate *> interfaces;
293 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {
294 if ( !ptr->ifa_addr )
297 // Get the interface index
298 int ifindex = if_nametoindex(ptr->ifa_name);
300 // on Linux we use AF_PACKET and sockaddr_ll to obtain hHwAddress
301 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
302 for ( ; if_it != interfaces.end(); ++if_it)
303 if ((*if_it)->index == ifindex) {
304 // this one has been added already
305 if ( ptr->ifa_addr->sa_family == AF_PACKET
306 && (*if_it)->hardwareAddress.isEmpty()) {
307 sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;
308 (*if_it)->hardwareAddress = (*if_it)->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);
312 if ( if_it != interfaces.end() )
315 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
317 iface->index = ifindex;
318 iface->name = QString::fromLatin1(ptr->ifa_name);
319 iface->flags = convertFlags(ptr->ifa_flags);
321 if ( ptr->ifa_addr->sa_family == AF_PACKET ) {
322 sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;
323 iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);
330 # elif defined(Q_OS_BSD4)
331 QT_BEGIN_INCLUDE_NAMESPACE
332 # include <net/if_dl.h>
333 QT_END_INCLUDE_NAMESPACE
335 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
337 QList<QNetworkInterfacePrivate *> interfaces;
339 // on NetBSD we use AF_LINK and sockaddr_dl
340 // scan the list for that family
341 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next)
342 if (ptr->ifa_addr && ptr->ifa_addr->sa_family == AF_LINK) {
343 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
346 sockaddr_dl *sdl = (sockaddr_dl *)ptr->ifa_addr;
347 iface->index = sdl->sdl_index;
348 iface->name = QString::fromLatin1(ptr->ifa_name);
349 iface->flags = convertFlags(ptr->ifa_flags);
350 iface->hardwareAddress = iface->makeHwAddress(sdl->sdl_alen, (uchar*)LLADDR(sdl));
356 # else // Generic version
358 static QList<QNetworkInterfacePrivate *> createInterfaces(ifaddrs *rawList)
360 QList<QNetworkInterfacePrivate *> interfaces;
362 // make sure there's one entry for each interface
363 for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {
364 // Get the interface index
365 int ifindex = if_nametoindex(ptr->ifa_name);
367 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
368 for ( ; if_it != interfaces.end(); ++if_it)
369 if ((*if_it)->index == ifindex)
370 // this one has been added already
373 if (if_it == interfaces.end()) {
374 // none found, create
375 QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
378 iface->index = ifindex;
379 iface->name = QString::fromLatin1(ptr->ifa_name);
380 iface->flags = convertFlags(ptr->ifa_flags);
390 static QList<QNetworkInterfacePrivate *> interfaceListing()
392 QList<QNetworkInterfacePrivate *> interfaces;
395 if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
396 return interfaces; // error
398 ifaddrs *interfaceListing;
399 if (getifaddrs(&interfaceListing) == -1) {
405 interfaces = createInterfaces(interfaceListing);
406 for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) {
407 // Get the interface index
408 int ifindex = if_nametoindex(ptr->ifa_name);
409 QNetworkInterfacePrivate *iface = 0;
410 QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
411 for ( ; if_it != interfaces.end(); ++if_it)
412 if ((*if_it)->index == ifindex) {
413 // found this interface already
418 // skip all non-IP interfaces
422 QNetworkAddressEntry entry;
423 entry.setIp(addressFromSockaddr(ptr->ifa_addr));
424 if (entry.ip().isNull())
425 // could not parse the address
428 entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask));
429 if (iface->flags & QNetworkInterface::CanBroadcast)
430 entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr));
432 iface->addressEntries << entry;
435 freeifaddrs(interfaceListing);
441 QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan()
443 return interfaceListing();
448 #endif // QT_NO_NETWORKINTERFACE