Qt 4.8
qnetworkinterface_unix.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 #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 
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  int scope = ((sockaddr_in6 *)sa)->sin6_scope_id;
87  if (scope) {
88 #ifndef QT_NO_IPV6IFNAME
89  char scopeid[IFNAMSIZ];
90  if (::if_indextoname(scope, scopeid)) {
91  address.setScopeId(QLatin1String(scopeid));
92  } else
93 #endif
94  address.setScopeId(QString::number(scope));
95  }
96  }
97 #endif
98  return address;
99 
100 }
101 
102 static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags)
103 {
104  QNetworkInterface::InterfaceFlags flags = 0;
105  flags |= (rawFlags & IFF_UP) ? QNetworkInterface::IsUp : QNetworkInterface::InterfaceFlag(0);
106  flags |= (rawFlags & IFF_RUNNING) ? QNetworkInterface::IsRunning : QNetworkInterface::InterfaceFlag(0);
107  flags |= (rawFlags & IFF_BROADCAST) ? QNetworkInterface::CanBroadcast : QNetworkInterface::InterfaceFlag(0);
108  flags |= (rawFlags & IFF_LOOPBACK) ? QNetworkInterface::IsLoopBack : QNetworkInterface::InterfaceFlag(0);
109 #ifdef IFF_POINTOPOINT //cygwin doesn't define IFF_POINTOPOINT
110  flags |= (rawFlags & IFF_POINTOPOINT) ? QNetworkInterface::IsPointToPoint : QNetworkInterface::InterfaceFlag(0);
111 #endif
112 
113 #ifdef IFF_MULTICAST
114  flags |= (rawFlags & IFF_MULTICAST) ? QNetworkInterface::CanMulticast : QNetworkInterface::InterfaceFlag(0);
115 #endif
116  return flags;
117 }
118 
119 #ifdef QT_NO_GETIFADDRS
120 // getifaddrs not available
121 
122 static const int STORAGEBUFFER_GROWTH = 256;
123 
124 static QSet<QByteArray> interfaceNames(int socket)
125 {
126  QSet<QByteArray> result;
127 #ifdef QT_NO_IPV6IFNAME
128  QByteArray storageBuffer;
129  struct ifconf interfaceList;
130 
131  forever {
132  // grow the storage buffer
133  storageBuffer.resize(storageBuffer.size() + STORAGEBUFFER_GROWTH);
134  interfaceList.ifc_buf = storageBuffer.data();
135  interfaceList.ifc_len = storageBuffer.size();
136 
137  // get the interface list
138  if (qt_safe_ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) {
139  if (int(interfaceList.ifc_len + sizeof(ifreq) + 64) < storageBuffer.size()) {
140  // if the buffer was big enough, break
141  storageBuffer.resize(interfaceList.ifc_len);
142  break;
143  }
144  } else {
145  // internal error
146  return result;
147  }
148  if (storageBuffer.size() > 100000) {
149  // out of space
150  return result;
151  }
152  }
153 
154  int interfaceCount = interfaceList.ifc_len / sizeof(ifreq);
155  for (int i = 0; i < interfaceCount; ++i) {
156  QByteArray name = QByteArray(interfaceList.ifc_req[i].ifr_name);
157  if (!name.isEmpty())
158  result << name;
159  }
160 
161  return result;
162 #else
163  Q_UNUSED(socket);
164 
165  // use if_nameindex
166  struct if_nameindex *interfaceList = ::if_nameindex();
167  for (struct if_nameindex *ptr = interfaceList; ptr && ptr->if_name; ++ptr)
168  result << ptr->if_name;
169 
170  if_freenameindex(interfaceList);
171  return result;
172 #endif
173 }
174 
175 static QNetworkInterfacePrivate *findInterface(int socket, QList<QNetworkInterfacePrivate *> &interfaces,
176  struct ifreq &req)
177 {
178  QNetworkInterfacePrivate *iface = 0;
179  int ifindex = 0;
180 
181 #ifndef QT_NO_IPV6IFNAME
182  // Get the interface index
183  ifindex = if_nametoindex(req.ifr_name);
184 
185  // find the interface data
187  for ( ; if_it != interfaces.end(); ++if_it)
188  if ((*if_it)->index == ifindex) {
189  // existing interface
190  iface = *if_it;
191  break;
192  }
193 #else
194  // Search by name
196  for ( ; if_it != interfaces.end(); ++if_it)
197  if ((*if_it)->name == QLatin1String(req.ifr_name)) {
198  // existing interface
199  iface = *if_it;
200  break;
201  }
202 #endif
203 
204  if (!iface) {
205  // new interface, create data:
206  iface = new QNetworkInterfacePrivate;
207  iface->index = ifindex;
208  interfaces << iface;
209 
210 #ifdef SIOCGIFNAME
211  // Get the canonical name
212  QByteArray oldName = req.ifr_name;
213  if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) {
214  iface->name = QString::fromLatin1(req.ifr_name);
215 
216  // reset the name:
217  memcpy(req.ifr_name, oldName, qMin<int>(oldName.length() + 1, sizeof(req.ifr_name) - 1));
218  } else
219 #endif
220  {
221  // use this name anyways
222  iface->name = QString::fromLatin1(req.ifr_name);
223  }
224 
225  // Get interface flags
226  if (qt_safe_ioctl(socket, SIOCGIFFLAGS, &req) >= 0) {
227  iface->flags = convertFlags(req.ifr_flags);
228  }
229 
230 #ifdef SIOCGIFHWADDR
231  // Get the HW address
232  if (qt_safe_ioctl(socket, SIOCGIFHWADDR, &req) >= 0) {
233  uchar *addr = (uchar *)req.ifr_addr.sa_data;
234  iface->hardwareAddress = iface->makeHwAddress(6, addr);
235  }
236 #endif
237  }
238 
239  return iface;
240 }
241 
243 {
245 
246  int socket;
247  if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
248  return interfaces; // error
249 
250  QSet<QByteArray> names = interfaceNames(socket);
252  for ( ; it != names.constEnd(); ++it) {
253  ifreq req;
254  memset(&req, 0, sizeof(ifreq));
255  memcpy(req.ifr_name, *it, qMin<int>(it->length() + 1, sizeof(req.ifr_name) - 1));
256 
257  QNetworkInterfacePrivate *iface = findInterface(socket, interfaces, req);
258 
259  // Get the interface broadcast address
260  QNetworkAddressEntry entry;
261  if (iface->flags & QNetworkInterface::CanBroadcast) {
262  if (qt_safe_ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) {
263  sockaddr *sa = &req.ifr_addr;
264  if (sa->sa_family == AF_INET)
266  }
267  }
268 
269  // Get the address of the interface
270  if (qt_safe_ioctl(socket, SIOCGIFADDR, &req) >= 0) {
271  sockaddr *sa = &req.ifr_addr;
272  entry.setIp(addressFromSockaddr(sa));
273  }
274 
275  // Get the interface netmask
276  if (qt_safe_ioctl(socket, SIOCGIFNETMASK, &req) >= 0) {
277  sockaddr *sa = &req.ifr_addr;
278  entry.setNetmask(addressFromSockaddr(sa));
279  }
280 
281  iface->addressEntries << entry;
282  }
283 
284  ::close(socket);
285  return interfaces;
286 }
287 
288 #else
289 // use getifaddrs
290 
291 // platform-specific defs:
292 # ifdef Q_OS_LINUX
294 # include <features.h>
296 # endif
297 
298 # if defined(Q_OS_LINUX) && __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1
299 # include <netpacket/packet.h>
300 
302 {
304 
305  for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {
306  if ( !ptr->ifa_addr )
307  continue;
308 
309  // Get the interface index
310  int ifindex = if_nametoindex(ptr->ifa_name);
311 
312  // on Linux we use AF_PACKET and sockaddr_ll to obtain hHwAddress
314  for ( ; if_it != interfaces.end(); ++if_it)
315  if ((*if_it)->index == ifindex) {
316  // this one has been added already
317  if ( ptr->ifa_addr->sa_family == AF_PACKET
318  && (*if_it)->hardwareAddress.isEmpty()) {
319  sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;
320  (*if_it)->hardwareAddress = (*if_it)->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);
321  }
322  break;
323  }
324  if ( if_it != interfaces.end() )
325  continue;
326 
328  interfaces << iface;
329  iface->index = ifindex;
330  iface->name = QString::fromLatin1(ptr->ifa_name);
331  iface->flags = convertFlags(ptr->ifa_flags);
332 
333  if ( ptr->ifa_addr->sa_family == AF_PACKET ) {
334  sockaddr_ll *sll = (sockaddr_ll *)ptr->ifa_addr;
335  iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (uchar*)sll->sll_addr);
336  }
337  }
338 
339  return interfaces;
340 }
341 
342 # elif defined(Q_OS_BSD4)
344 # include <net/if_dl.h>
346 
348 {
350 
351  // on NetBSD we use AF_LINK and sockaddr_dl
352  // scan the list for that family
353  for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next)
354  if (ptr->ifa_addr && ptr->ifa_addr->sa_family == AF_LINK) {
356  interfaces << iface;
357 
358  sockaddr_dl *sdl = (sockaddr_dl *)ptr->ifa_addr;
359  iface->index = sdl->sdl_index;
360  iface->name = QString::fromLatin1(ptr->ifa_name);
361  iface->flags = convertFlags(ptr->ifa_flags);
362  iface->hardwareAddress = iface->makeHwAddress(sdl->sdl_alen, (uchar*)LLADDR(sdl));
363  }
364 
365  return interfaces;
366 }
367 
368 # else // Generic version
369 
371 {
373 
374  // make sure there's one entry for each interface
375  for (ifaddrs *ptr = rawList; ptr; ptr = ptr->ifa_next) {
376  // Get the interface index
377  int ifindex = if_nametoindex(ptr->ifa_name);
378 
380  for ( ; if_it != interfaces.end(); ++if_it)
381  if ((*if_it)->index == ifindex)
382  // this one has been added already
383  break;
384 
385  if (if_it == interfaces.end()) {
386  // none found, create
388  interfaces << iface;
389 
390  iface->index = ifindex;
391  iface->name = QString::fromLatin1(ptr->ifa_name);
392  iface->flags = convertFlags(ptr->ifa_flags);
393  }
394  }
395 
396  return interfaces;
397 }
398 
399 # endif
400 
401 
403 {
405 
406  int socket;
407  if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
408  return interfaces; // error
409 
410  ifaddrs *interfaceListing;
411  if (getifaddrs(&interfaceListing) == -1) {
412  // error
413  ::close(socket);
414  return interfaces;
415  }
416 
417  interfaces = createInterfaces(interfaceListing);
418  for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) {
419  // Get the interface index
420  int ifindex = if_nametoindex(ptr->ifa_name);
421  QNetworkInterfacePrivate *iface = 0;
423  for ( ; if_it != interfaces.end(); ++if_it)
424  if ((*if_it)->index == ifindex) {
425  // found this interface already
426  iface = *if_it;
427  break;
428  }
429  if (!iface) {
430  // skip all non-IP interfaces
431  continue;
432  }
433 
434  QNetworkAddressEntry entry;
435  entry.setIp(addressFromSockaddr(ptr->ifa_addr));
436  if (entry.ip().isNull())
437  // could not parse the address
438  continue;
439 
440  entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask));
442  entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr));
443 
444  iface->addressEntries << entry;
445  }
446 
447  freeifaddrs(interfaceListing);
448  ::close(socket);
449  return interfaces;
450 }
451 #endif
452 
454 {
455  return interfaceListing();
456 }
457 
459 
460 #endif // QT_NO_NETWORKINTERFACE
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
void setScopeId(const QString &id)
Sets the IPv6 scope ID of the address to id.
bool isNull() const
Returns true if this host address is null (INADDR_ANY or in6addr_any).
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QHostAddress ip() const
This function returns one IPv4 or IPv6 address found, that was found in a network interface...
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
#define it(className, varName)
const_iterator constEnd() const
Definition: qset.h:171
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static int qt_safe_ioctl(int sockfd, int request, T arg)
Definition: qnet_unix_p.h:167
InterfaceFlag
Specifies the flags associated with this network interface.
static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags)
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
QList< QNetworkInterfacePrivate * > scan()
const char * name
unsigned int uint
Definition: qglobal.h:996
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
const T * ptr(const T &t)
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
Definition: qlist.h:181
static QList< QNetworkInterfacePrivate * > interfaceListing()
int length() const
Same as size().
Definition: qbytearray.h:356
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
static int qt_safe_socket(int domain, int type, int protocol, int flags=0)
Definition: qnet_unix_p.h:83
void setIp(const QHostAddress &newIp)
Sets the IP address the QNetworkAddressEntry object contains to newIp.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QList< QNetworkInterfacePrivate * > createInterfaces(ifaddrs *rawList)
QList< QNetworkAddressEntry > addressEntries
void resize(int size)
Sets the size of the byte array to size bytes.
const_iterator constBegin() const
Definition: qset.h:168
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
void setNetmask(const QHostAddress &newNetmask)
Sets the netmask that this QNetworkAddressEntry object contains to newNetmask.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QNetworkInterface::InterfaceFlags flags
The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address.
static QHostAddress addressFromSockaddr(sockaddr *sa)
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
#define AF_INET6
void setBroadcast(const QHostAddress &newBroadcast)
Sets the broadcast IP address of this QNetworkAddressEntry object to newBroadcast.
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452