1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtNetwork module of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "qnetworkinterface.h"
43
#include "qnetworkinterface_p.h"
48
#ifndef QT_NO_NETWORKINTERFACE
52
static QList<QNetworkInterfacePrivate *> postProcess(QList<QNetworkInterfacePrivate *> list)
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
58
// This is an IPv4-only thing -- IPv6 has no concept of broadcasts
60
// broadcast = IP | ~netmask
62
QList<QNetworkInterfacePrivate *>::Iterator it = list.begin();
63
const QList<QNetworkInterfacePrivate *>::Iterator end = list.end();
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)
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);
82
Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager)
84
QNetworkInterfaceManager::QNetworkInterfaceManager()
88
QNetworkInterfaceManager::~QNetworkInterfaceManager()
92
QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name)
94
QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces();
95
QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
96
for ( ; it != interfaceList.constEnd(); ++it)
97
if ((*it)->name == name)
103
QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromIndex(int index)
105
QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces();
106
QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
107
for ( ; it != interfaceList.constEnd(); ++it)
108
if ((*it)->index == index)
114
QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces()
116
QList<QNetworkInterfacePrivate *> list = postProcess(scan());
117
QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;
119
foreach (QNetworkInterfacePrivate *ptr, list)
120
result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);
125
QString QNetworkInterfacePrivate::makeHwAddress(int len, uchar *data)
128
for (int i = 0; i < len; ++i) {
130
result += QLatin1Char(':');
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]));
136
sprintf(buf, "%02hX", ushort(data[i]));
138
result += QLatin1String(buf);
144
\class QNetworkAddressEntry
145
\brief The QNetworkAddressEntry class stores one IP address
146
supported by a network interface, along with its associated
147
netmask and broadcast address.
155
Each network interface can contain zero or more IP addresses, which
156
in turn can be associated with a netmask and/or a broadcast
157
address (depending on support from the operating system).
159
This class represents one such group.
163
Constructs an empty QNetworkAddressEntry object.
165
QNetworkAddressEntry::QNetworkAddressEntry()
166
: d(new QNetworkAddressEntryPrivate)
171
Constructs a QNetworkAddressEntry object that is a copy of the
174
QNetworkAddressEntry::QNetworkAddressEntry(const QNetworkAddressEntry &other)
175
: d(new QNetworkAddressEntryPrivate(*other.d.data()))
180
Makes a copy of the QNetworkAddressEntry object \a other.
182
QNetworkAddressEntry &QNetworkAddressEntry::operator=(const QNetworkAddressEntry &other)
184
*d.data() = *other.d.data();
189
\fn void QNetworkAddressEntry::swap(QNetworkAddressEntry &other)
192
Swaps this network address entry instance with \a other. This
193
function is very fast and never fails.
197
Destroys this QNetworkAddressEntry object.
199
QNetworkAddressEntry::~QNetworkAddressEntry()
204
Returns true if this network address entry is the same as \a
207
bool QNetworkAddressEntry::operator==(const QNetworkAddressEntry &other) const
209
if (d == other.d) return true;
210
if (!d || !other.d) return false;
211
return d->address == other.d->address &&
212
d->netmask == other.d->netmask &&
213
d->broadcast == other.d->broadcast;
217
\fn bool QNetworkAddressEntry::operator!=(const QNetworkAddressEntry &other) const
219
Returns true if this network address entry is different from \a
224
This function returns one IPv4 or IPv6 address found, that was
225
found in a network interface.
227
QHostAddress QNetworkAddressEntry::ip() const
233
Sets the IP address the QNetworkAddressEntry object contains to \a
236
void QNetworkAddressEntry::setIp(const QHostAddress &newIp)
242
Returns the netmask associated with the IP address. The
243
netmask is expressed in the form of an IP address, such as
246
For IPv6 addresses, the prefix length is converted to an address
247
where the number of bits set to 1 is equal to the prefix
248
length. For a prefix length of 64 bits (the most common value),
249
the netmask will be expressed as a QHostAddress holding the
250
address FFFF:FFFF:FFFF:FFFF::
254
QHostAddress QNetworkAddressEntry::netmask() const
260
Sets the netmask that this QNetworkAddressEntry object contains to
261
\a newNetmask. Setting the netmask also sets the prefix length to
262
match the new netmask.
264
\sa setPrefixLength()
266
void QNetworkAddressEntry::setNetmask(const QHostAddress &newNetmask)
268
if (newNetmask.protocol() != ip().protocol()) {
269
d->netmask = QNetmaskAddress();
273
d->netmask.setAddress(newNetmask);
278
Returns the prefix length of this IP address. The prefix length
279
matches the number of bits set to 1 in the netmask (see
280
netmask()). For IPv4 addresses, the value is between 0 and 32. For
281
IPv6 addresses, it's contained between 0 and 128 and is the
282
preferred form of representing addresses.
284
This function returns -1 if the prefix length could not be
285
determined (i.e., netmask() returns a null QHostAddress()).
289
int QNetworkAddressEntry::prefixLength() const
291
return d->netmask.prefixLength();
296
Sets the prefix length of this IP address to \a length. The value
297
of \a length must be valid for this type of IP address: between 0
298
and 32 for IPv4 addresses, between 0 and 128 for IPv6
299
addresses. Setting to any invalid value is equivalent to setting
300
to -1, which means "no prefix length".
302
Setting the prefix length also sets the netmask (see netmask()).
306
void QNetworkAddressEntry::setPrefixLength(int length)
308
d->netmask.setPrefixLength(d->address.protocol(), length);
312
Returns the broadcast address associated with the IPv4
313
address and netmask. It can usually be derived from those two by
314
setting to 1 the bits of the IP address where the netmask contains
315
a 0. (In other words, by bitwise-OR'ing the IP address with the
316
inverse of the netmask)
318
This member is always empty for IPv6 addresses, since the concept
319
of broadcast has been abandoned in that system in favor of
320
multicast. In particular, the group of hosts corresponding to all
321
the nodes in the local network can be reached by the "all-nodes"
322
special multicast group (address FF02::1).
324
QHostAddress QNetworkAddressEntry::broadcast() const
330
Sets the broadcast IP address of this QNetworkAddressEntry object
333
void QNetworkAddressEntry::setBroadcast(const QHostAddress &newBroadcast)
335
d->broadcast = newBroadcast;
339
\class QNetworkInterface
340
\brief The QNetworkInterface class provides a listing of the host's IP
341
addresses and network interfaces.
349
QNetworkInterface represents one network interface attached to the
350
host where the program is being run. Each network interface may
351
contain zero or more IP addresses, each of which is optionally
352
associated with a netmask and/or a broadcast address. The list of
353
such trios can be obtained with addressEntries(). Alternatively,
354
when the netmask or the broadcast addresses aren't necessary, use
355
the allAddresses() convenience function to obtain just the IP
358
QNetworkInterface also reports the interface's hardware address with
361
Not all operating systems support reporting all features. Only the
362
IPv4 addresses are guaranteed to be listed by this class in all
363
platforms. In particular, IPv6 address listing is only supported
364
on Windows XP and more recent versions, Linux, MacOS X and the
367
\sa QNetworkAddressEntry
371
\enum QNetworkInterface::InterfaceFlag
372
Specifies the flags associated with this network interface. The
375
\value IsUp the network interface is active
376
\value IsRunning the network interface has resources
378
\value CanBroadcast the network interface works in
380
\value IsLoopBack the network interface is a loopback
381
interface: that is, it's a virtual
382
interface whose destination is the
384
\value IsPointToPoint the network interface is a
385
point-to-point interface: that is,
386
there is one, single other address
387
that can be directly reached by it.
388
\value CanMulticast the network interface supports
391
Note that one network interface cannot be both broadcast-based and
396
Constructs an empty network interface object.
398
QNetworkInterface::QNetworkInterface()
404
Frees the resources associated with the QNetworkInterface object.
406
QNetworkInterface::~QNetworkInterface()
411
Creates a copy of the QNetworkInterface object contained in \a
414
QNetworkInterface::QNetworkInterface(const QNetworkInterface &other)
420
Copies the contents of the QNetworkInterface object contained in \a
423
QNetworkInterface &QNetworkInterface::operator=(const QNetworkInterface &other)
430
\fn void QNetworkInterface::swap(QNetworkInterface &other)
433
Swaps this network interface instance with \a other. This function
434
is very fast and never fails.
438
Returns true if this QNetworkInterface object contains valid
439
information about a network interface.
441
bool QNetworkInterface::isValid() const
443
return !name().isEmpty();
448
Returns the interface system index, if known. This is an integer
449
assigned by the operating system to identify this interface and it
450
generally doesn't change. It matches the scope ID field in IPv6
453
If the index isn't known, this function returns 0.
455
int QNetworkInterface::index() const
457
return d ? d->index : 0;
461
Returns the name of this network interface. On Unix systems, this
462
is a string containing the type of the interface and optionally a
463
sequence number, such as "eth0", "lo" or "pcn0". On Windows, it's
464
an internal ID that cannot be changed by the user.
466
QString QNetworkInterface::name() const
468
return d ? d->name : QString();
474
Returns the human-readable name of this network interface on
475
Windows, such as "Local Area Connection", if the name could be
476
determined. If it couldn't, this function returns the same as
477
name(). The human-readable name is a name that the user can modify
478
in the Windows Control Panel, so it may change during the
479
execution of the program.
481
On Unix, this function currently always returns the same as
482
name(), since Unix systems don't store a configuration for
483
human-readable names.
485
QString QNetworkInterface::humanReadableName() const
487
return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString();
491
Returns the flags associated with this network interface.
493
QNetworkInterface::InterfaceFlags QNetworkInterface::flags() const
495
return d ? d->flags : InterfaceFlags(0);
499
Returns the low-level hardware address for this interface. On
500
Ethernet interfaces, this will be a MAC address in string
501
representation, separated by colons.
503
Other interface types may have other types of hardware
504
addresses. Implementations should not depend on this function
505
returning a valid MAC address.
507
QString QNetworkInterface::hardwareAddress() const
509
return d ? d->hardwareAddress : QString();
513
Returns the list of IP addresses that this interface possesses
514
along with their associated netmasks and broadcast addresses.
516
If the netmask or broadcast address information is not necessary,
517
you can call the allAddresses() function to obtain just the IP
520
QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const
522
return d ? d->addressEntries : QList<QNetworkAddressEntry>();
526
Returns a QNetworkInterface object for the interface named \a
527
name. If no such interface exists, this function returns an
528
invalid QNetworkInterface object.
530
\sa name(), isValid()
532
QNetworkInterface QNetworkInterface::interfaceFromName(const QString &name)
534
QNetworkInterface result;
535
result.d = manager()->interfaceFromName(name);
540
Returns a QNetworkInterface object for the interface whose internal
541
ID is \a index. Network interfaces have a unique identifier called
542
the "interface index" to distinguish it from other interfaces on
543
the system. Often, this value is assigned progressively and
544
interfaces being removed and then added again get a different
547
This index is also found in the IPv6 address' scope ID field.
549
QNetworkInterface QNetworkInterface::interfaceFromIndex(int index)
551
QNetworkInterface result;
552
result.d = manager()->interfaceFromIndex(index);
557
Returns a listing of all the network interfaces found on the host
560
QList<QNetworkInterface> QNetworkInterface::allInterfaces()
562
QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
563
QList<QNetworkInterface> result;
564
foreach (const QSharedDataPointer<QNetworkInterfacePrivate> &p, privs) {
565
QNetworkInterface item;
574
This convenience function returns all IP addresses found on the
575
host machine. It is equivalent to calling addressEntries() on all the
576
objects returned by allInterfaces() to obtain lists of QHostAddress
577
objects then calling QHostAddress::ip() on each of these.
579
QList<QHostAddress> QNetworkInterface::allAddresses()
581
QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
582
QList<QHostAddress> result;
583
foreach (const QSharedDataPointer<QNetworkInterfacePrivate> &p, privs) {
584
foreach (const QNetworkAddressEntry &entry, p->addressEntries)
585
result += entry.ip();
591
#ifndef QT_NO_DEBUG_STREAM
592
static inline QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags)
594
if (flags & QNetworkInterface::IsUp)
595
debug.nospace() << "IsUp ";
596
if (flags & QNetworkInterface::IsRunning)
597
debug.nospace() << "IsRunning ";
598
if (flags & QNetworkInterface::CanBroadcast)
599
debug.nospace() << "CanBroadcast ";
600
if (flags & QNetworkInterface::IsLoopBack)
601
debug.nospace() << "IsLoopBack ";
602
if (flags & QNetworkInterface::IsPointToPoint)
603
debug.nospace() << "IsPointToPoint ";
604
if (flags & QNetworkInterface::CanMulticast)
605
debug.nospace() << "CanMulticast ";
606
return debug.nospace();
609
static inline QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry)
611
debug.nospace() << "(address = " << entry.ip();
612
if (!entry.netmask().isNull())
613
debug.nospace() << ", netmask = " << entry.netmask();
614
if (!entry.broadcast().isNull())
615
debug.nospace() << ", broadcast = " << entry.broadcast();
616
debug.nospace() << ')';
617
return debug.space();
620
QDebug operator<<(QDebug debug, const QNetworkInterface &networkInterface)
622
debug.nospace() << "QNetworkInterface(name = " << networkInterface.name()
623
<< ", hardware address = " << networkInterface.hardwareAddress()
625
flagsDebug(debug, networkInterface.flags());
626
#if defined(Q_CC_RVCT)
627
// RVCT gets confused with << networkInterface.addressEntries(), reason unknown.
628
debug.nospace() << ")\n";
630
debug.nospace() << ", entries = " << networkInterface.addressEntries()
633
return debug.space();
639
#endif // QT_NO_NETWORKINTERFACE