Qt 4.8
qnetworkinterface.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 "qnetworkinterface.h"
43 #include "qnetworkinterface_p.h"
44 
45 #include "qdebug.h"
46 #include "qendian.h"
47 
48 #ifndef QT_NO_NETWORKINTERFACE
49 
51 
53 {
54  // Some platforms report a netmask but don't report a broadcast address
55  // Go through all available addresses and calculate the broadcast address
56  // from the IP and the netmask
57  //
58  // This is an IPv4-only thing -- IPv6 has no concept of broadcasts
59  // The math is:
60  // broadcast = IP | ~netmask
61 
64  for ( ; it != end; ++it) {
65  QList<QNetworkAddressEntry>::Iterator addr_it = (*it)->addressEntries.begin();
66  const QList<QNetworkAddressEntry>::Iterator addr_end = (*it)->addressEntries.end();
67  for ( ; addr_it != addr_end; ++addr_it) {
68  if (addr_it->ip().protocol() != QAbstractSocket::IPv4Protocol)
69  continue;
70 
71  if (!addr_it->netmask().isNull() && addr_it->broadcast().isNull()) {
72  QHostAddress bcast = addr_it->ip();
73  bcast = QHostAddress(bcast.toIPv4Address() | ~addr_it->netmask().toIPv4Address());
74  addr_it->setBroadcast(bcast);
75  }
76  }
77  }
78 
79  return list;
80 }
81 
83 
85 {
86 }
87 
89 {
90 }
91 
93 {
95  QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
96  for ( ; it != interfaceList.constEnd(); ++it)
97  if ((*it)->name == name)
98  return *it;
99 
100  return empty;
101 }
102 
104 {
106  QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
107  for ( ; it != interfaceList.constEnd(); ++it)
108  if ((*it)->index == index)
109  return *it;
110 
111  return empty;
112 }
113 
115 {
118 
119  foreach (QNetworkInterfacePrivate *ptr, list)
120  result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);
121 
122  return result;
123 }
124 
126 {
127  QString result;
128  for (int i = 0; i < len; ++i) {
129  if (i)
130  result += QLatin1Char(':');
131 
132  char buf[3];
133 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && defined(_MSC_VER) && _MSC_VER >= 1400
134  sprintf_s(buf, 3, "%02hX", ushort(data[i]));
135 #else
136  sprintf(buf, "%02hX", ushort(data[i]));
137 #endif
138  result += QLatin1String(buf);
139  }
140  return result;
141 }
142 
168 {
169 }
170 
176  : d(new QNetworkAddressEntryPrivate(*other.d.data()))
177 {
178 }
179 
184 {
185  *d.data() = *other.d.data();
186  return *this;
187 }
188 
193 {
194 }
195 
201 {
202  if (d == other.d) return true;
203  if (!d || !other.d) return false;
204  return d->address == other.d->address &&
205  d->netmask == other.d->netmask &&
206  d->broadcast == other.d->broadcast;
207 }
208 
224 {
225  return d->address;
226 }
227 
233 {
234  d->address = newIp;
235 }
236 
251 {
252  return d->netmask;
253 }
254 
263 {
264  if (newNetmask.protocol() != ip().protocol()) {
266  return;
267  }
268 
269  d->netmask.setAddress(newNetmask);
270 }
271 
289 {
290  return d->netmask.prefixLength();
291 }
292 
309 {
310  d->netmask.setPrefixLength(d->address.protocol(), length);
311 }
312 
327 {
328  return d->broadcast;
329 }
330 
336 {
337  d->broadcast = newBroadcast;
338 }
339 
405  : d(0)
406 {
407 }
408 
413 {
414 }
415 
421  : d(other.d)
422 {
423 }
424 
430 {
431  d = other.d;
432  return *this;
433 }
434 
440 {
441  return !name().isEmpty();
442 }
443 
457 {
458  return d ? d->index : 0;
459 }
460 
468 {
469  return d ? d->name : QString();
470 }
471 
490 {
491  return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString();
492 }
493 
497 QNetworkInterface::InterfaceFlags QNetworkInterface::flags() const
498 {
499  return d ? d->flags : InterfaceFlags(0);
500 }
501 
512 {
513  return d ? d->hardwareAddress : QString();
514 }
515 
525 {
527 }
528 
537 {
538  QNetworkInterface result;
539  result.d = manager()->interfaceFromName(name);
540  return result;
541 }
542 
554 {
555  QNetworkInterface result;
556  result.d = manager()->interfaceFromIndex(index);
557  return result;
558 }
559 
565 {
566  QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
568  foreach (const QSharedDataPointer<QNetworkInterfacePrivate> &p, privs) {
569  QNetworkInterface item;
570  item.d = p;
571  result << item;
572  }
573 
574  return result;
575 }
576 
584 {
585  QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
586  QList<QHostAddress> result;
587  foreach (const QSharedDataPointer<QNetworkInterfacePrivate> &p, privs) {
588  foreach (const QNetworkAddressEntry &entry, p->addressEntries)
589  result += entry.ip();
590  }
591 
592  return result;
593 }
594 
595 #ifndef QT_NO_DEBUG_STREAM
596 static inline QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags)
597 {
598  if (flags & QNetworkInterface::IsUp)
599  debug.nospace() << "IsUp ";
600  if (flags & QNetworkInterface::IsRunning)
601  debug.nospace() << "IsRunning ";
603  debug.nospace() << "CanBroadcast ";
604  if (flags & QNetworkInterface::IsLoopBack)
605  debug.nospace() << "IsLoopBack ";
607  debug.nospace() << "IsPointToPoint ";
609  debug.nospace() << "CanMulticast ";
610  return debug.nospace();
611 }
612 
613 static inline QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry)
614 {
615  debug.nospace() << "(address = " << entry.ip();
616  if (!entry.netmask().isNull())
617  debug.nospace() << ", netmask = " << entry.netmask();
618  if (!entry.broadcast().isNull())
619  debug.nospace() << ", broadcast = " << entry.broadcast();
620  debug.nospace() << ')';
621  return debug.space();
622 }
623 
624 QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface)
625 {
626  debug.nospace() << "QNetworkInterface(name = " << networkInterface.name()
627  << ", hardware address = " << networkInterface.hardwareAddress()
628  << ", flags = ";
629  flagsDebug(debug, networkInterface.flags());
630 #if defined(Q_CC_RVCT)
631  // RVCT gets confused with << networkInterface.addressEntries(), reason unknown.
632  debug.nospace() << ")\n";
633 #else
634  debug.nospace() << ", entries = " << networkInterface.addressEntries()
635  << ")\n";
636 #endif
637  return debug.space();
638 }
639 #endif
640 
642 
643 #endif // QT_NO_NETWORKINTERFACE
static QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry)
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
QSharedDataPointer< QNetworkInterfacePrivate > interfaceFromName(const QString &name)
static QNetworkInterface interfaceFromName(const QString &name)
Returns a QNetworkInterface object for the interface named name.
QNetworkInterface & operator=(const QNetworkInterface &other)
Copies the contents of the QNetworkInterface object contained in other into this one.
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...
QString name() const
Returns the name of this network interface.
T * data() const
Returns the value of the pointer referenced by this object.
#define it(className, varName)
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
QNetworkAddressEntry & operator=(const QNetworkAddressEntry &other)
Makes a copy of the QNetworkAddressEntry object other.
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
void setPrefixLength(int length)
Sets the prefix length of this IP address to length.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QHostAddress broadcast() const
Returns the broadcast address associated with the IPv4 address and netmask.
~QNetworkAddressEntry()
Destroys this QNetworkAddressEntry object.
QScopedPointer< QNetworkAddressEntryPrivate > d
QNetworkAddressEntry()
Constructs an empty QNetworkAddressEntry object.
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QNetworkInterface()
Constructs an empty network interface object.
static QString makeHwAddress(int len, uchar *data)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
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
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
bool isValid() const
Returns true if this QNetworkInterface object contains valid information about a network interface...
QSharedDataPointer< QNetworkInterfacePrivate > empty
static const char * data(const QByteArray &arr)
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 QNetworkInterface interfaceFromIndex(int index)
Returns a QNetworkInterface object for the interface whose internal ID is index.
static QList< QNetworkInterfacePrivate * > postProcess(QList< QNetworkInterfacePrivate *> list)
~QNetworkInterface()
Frees the resources associated with the QNetworkInterface object.
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
void setIp(const QHostAddress &newIp)
Sets the IP address the QNetworkAddressEntry object contains to newIp.
unsigned short ushort
Definition: qglobal.h:995
bool operator==(const QNetworkAddressEntry &other) const
Returns true if this network address entry is the same as other.
static QList< QNetworkInterface > allInterfaces()
Returns a listing of all the network interfaces found on the host machine.
QList< QNetworkAddressEntry > addressEntries
QString humanReadableName() const
Returns the human-readable name of this network interface on Windows, such as "Local Area Connection"...
int index() const
Returns the interface system index, if known.
static QList< QHostAddress > allAddresses()
This convenience function returns all IP addresses found on the host machine.
quint16 index
InterfaceFlags flags() const
Returns the flags associated with this network interface.
QSharedDataPointer< QNetworkInterfacePrivate > d
void setNetmask(const QHostAddress &newNetmask)
Sets the netmask that this QNetworkAddressEntry object contains to newNetmask.
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...
QSharedDataPointer< QNetworkInterfacePrivate > interfaceFromIndex(int index)
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
QList< QSharedDataPointer< QNetworkInterfacePrivate > > allInterfaces()
void setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int len)
static const KeyPair *const end
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
QHostAddress netmask() const
Returns the netmask associated with the IP address.
QList< QNetworkAddressEntry > addressEntries() const
Returns the list of IP addresses that this interface possesses along with their associated netmasks a...
int prefixLength() const
static QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags)
QNetworkInterface::InterfaceFlags flags
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address.
QString hardwareAddress() const
Returns the low-level hardware address for this interface.
bool setAddress(const QString &address)
int prefixLength() const
Returns the prefix length of this IP address.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272
void setBroadcast(const QHostAddress &newBroadcast)
Sets the broadcast IP address of this QNetworkAddressEntry object to newBroadcast.