Qt 4.8
Macros | Functions
qnetworkinterface_unix.cpp File Reference
#include "qset.h"
#include "qnetworkinterface.h"
#include "qnetworkinterface_p.h"
#include "qalgorithms.h"
#include "private/qnet_unix_p.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <qplatformdefs.h>
#include <features.h>

Go to the source code of this file.

Macros

#define IP_MULTICAST
 

Functions

static QHostAddress addressFromSockaddr (sockaddr *sa)
 
static QNetworkInterface::InterfaceFlags convertFlags (uint rawFlags)
 
static QList< QNetworkInterfacePrivate * > createInterfaces (ifaddrs *rawList)
 
static QList< QNetworkInterfacePrivate * > interfaceListing ()
 

Macro Definition Documentation

◆ IP_MULTICAST

#define IP_MULTICAST

Definition at line 50 of file qnetworkinterface_unix.cpp.

Function Documentation

◆ addressFromSockaddr()

static QHostAddress addressFromSockaddr ( sockaddr *  sa)
static

Definition at line 75 of file qnetworkinterface_unix.cpp.

Referenced by convertFlags(), and interfaceListing().

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 }
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.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
#define AF_INET6

◆ convertFlags()

static QNetworkInterface::InterfaceFlags convertFlags ( uint  rawFlags)
static

Definition at line 102 of file qnetworkinterface_unix.cpp.

Referenced by createInterfaces().

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 }
InterfaceFlag
Specifies the flags associated with this network interface.

◆ createInterfaces()

static QList<QNetworkInterfacePrivate *> createInterfaces ( ifaddrs *  rawList)
static

Definition at line 370 of file qnetworkinterface_unix.cpp.

Referenced by convertFlags(), and interfaceListing().

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 }
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags)
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
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 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
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ interfaceListing()

static QList<QNetworkInterfacePrivate *> interfaceListing ( )
static

Definition at line 402 of file qnetworkinterface_unix.cpp.

Referenced by convertFlags(), and QNetworkInterfaceManager::scan().

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 }
bool isNull() const
Returns true if this host address is null (INADDR_ANY or in6addr_any).
QHostAddress ip() const
This function returns one IPv4 or IPv6 address found, that was found in a network interface...
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
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()
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 QList< QNetworkInterfacePrivate * > createInterfaces(ifaddrs *rawList)
QList< QNetworkAddressEntry > addressEntries
void setNetmask(const QHostAddress &newNetmask)
Sets the netmask that this QNetworkAddressEntry object contains to newNetmask.
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
void setBroadcast(const QHostAddress &newBroadcast)
Sets the broadcast IP address of this QNetworkAddressEntry object to newBroadcast.