Qt 4.8
Public Types | Public Functions | Static Public Functions | Properties | Friends | List of all members
QNetworkInterface Class Reference

The QNetworkInterface class provides a listing of the host's IP addresses and network interfaces. More...

#include <qnetworkinterface.h>

Public Types

enum  InterfaceFlag {
  IsUp = 0x1, IsRunning = 0x2, CanBroadcast = 0x4, IsLoopBack = 0x8,
  IsPointToPoint = 0x10, CanMulticast = 0x20
}
 Specifies the flags associated with this network interface. More...
 

Public Functions

QList< QNetworkAddressEntryaddressEntries () const
 Returns the list of IP addresses that this interface possesses along with their associated netmasks and broadcast addresses. More...
 
InterfaceFlags flags () const
 Returns the flags associated with this network interface. More...
 
QString hardwareAddress () const
 Returns the low-level hardware address for this interface. More...
 
QString humanReadableName () const
 Returns the human-readable name of this network interface on Windows, such as "Local Area Connection", if the name could be determined. More...
 
int index () const
 Returns the interface system index, if known. More...
 
bool isValid () const
 Returns true if this QNetworkInterface object contains valid information about a network interface. More...
 
QString name () const
 Returns the name of this network interface. More...
 
QNetworkInterfaceoperator= (const QNetworkInterface &other)
 Copies the contents of the QNetworkInterface object contained in other into this one. More...
 
 QNetworkInterface ()
 Constructs an empty network interface object. More...
 
 QNetworkInterface (const QNetworkInterface &other)
 Creates a copy of the QNetworkInterface object contained in other. More...
 
 ~QNetworkInterface ()
 Frees the resources associated with the QNetworkInterface object. More...
 

Static Public Functions

static QList< QHostAddressallAddresses ()
 This convenience function returns all IP addresses found on the host machine. More...
 
static QList< QNetworkInterfaceallInterfaces ()
 Returns a listing of all the network interfaces found on the host machine. More...
 
static QNetworkInterface interfaceFromIndex (int index)
 Returns a QNetworkInterface object for the interface whose internal ID is index. More...
 
static QNetworkInterface interfaceFromName (const QString &name)
 Returns a QNetworkInterface object for the interface named name. More...
 

Properties

QSharedDataPointer< QNetworkInterfacePrivated
 

Friends

class QNetworkInterfacePrivate
 

Detailed Description

The QNetworkInterface class provides a listing of the host's IP addresses and network interfaces.

Since
4.2
Note
This class or function is reentrant.

QNetworkInterface represents one network interface attached to the host where the program is being run. Each network interface may contain zero or more IP addresses, each of which is optionally associated with a netmask and/or a broadcast address. The list of such trios can be obtained with addressEntries(). Alternatively, when the netmask or the broadcast addresses aren't necessary, use the allAddresses() convenience function to obtain just the IP addresses.

QNetworkInterface also reports the interface's hardware address with hardwareAddress().

Not all operating systems support reporting all features. Only the IPv4 addresses are guaranteed to be listed by this class in all platforms. In particular, IPv6 address listing is only supported on Windows XP and more recent versions, Linux, MacOS X and the BSDs.

See also
QNetworkAddressEntry

Definition at line 87 of file qnetworkinterface.h.

Enumerations

◆ InterfaceFlag

Specifies the flags associated with this network interface.

The possible values are:

  • IsUp the network interface is active
  • IsRunning the network interface has resources allocated
  • CanBroadcast the network interface works in broadcast mode
  • IsLoopBack the network interface is a loopback interface: that is, it's a virtual interface whose destination is the host computer itself
  • IsPointToPoint the network interface is a point-to-point interface: that is, there is one, single other address that can be directly reached by it.
  • CanMulticast the network interface supports multicasting

Note that one network interface cannot be both broadcast-based and point-to-point.

Enumerator
IsUp 
IsRunning 
CanBroadcast 
IsLoopBack 
IsPointToPoint 
CanMulticast 

Definition at line 90 of file qnetworkinterface.h.

Constructors and Destructors

◆ QNetworkInterface() [1/2]

QNetworkInterface::QNetworkInterface ( )

Constructs an empty network interface object.

Definition at line 404 of file qnetworkinterface.cpp.

405  : d(0)
406 {
407 }
QSharedDataPointer< QNetworkInterfacePrivate > d

◆ QNetworkInterface() [2/2]

QNetworkInterface::QNetworkInterface ( const QNetworkInterface other)

Creates a copy of the QNetworkInterface object contained in other.

Definition at line 420 of file qnetworkinterface.cpp.

421  : d(other.d)
422 {
423 }
QSharedDataPointer< QNetworkInterfacePrivate > d

◆ ~QNetworkInterface()

QNetworkInterface::~QNetworkInterface ( )

Frees the resources associated with the QNetworkInterface object.

Definition at line 412 of file qnetworkinterface.cpp.

413 {
414 }

Functions

◆ addressEntries()

QList< QNetworkAddressEntry > QNetworkInterface::addressEntries ( ) const

Returns the list of IP addresses that this interface possesses along with their associated netmasks and broadcast addresses.

If the netmask or broadcast address information is not necessary, you can call the allAddresses() function to obtain just the IP addresses.

Definition at line 524 of file qnetworkinterface.cpp.

Referenced by isBypassed(), multicastMembershipHelper(), QNativeSocketEnginePrivate::nativeMulticastInterface(), QNativeSocketEnginePrivate::nativeSetMulticastInterface(), and operator<<().

525 {
527 }
QList< QNetworkAddressEntry > addressEntries
QSharedDataPointer< QNetworkInterfacePrivate > d

◆ allAddresses()

QList< QHostAddress > QNetworkInterface::allAddresses ( )
static

This convenience function returns all IP addresses found on the host machine.

It is equivalent to calling addressEntries() on all the objects returned by allInterfaces() to obtain lists of QHostAddress objects then calling QHostAddress::ip() on each of these.

Definition at line 583 of file qnetworkinterface.cpp.

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 }
QHostAddress ip() const
This function returns one IPv4 or IPv6 address found, that was found in a network interface...
QList< QNetworkAddressEntry > addressEntries
The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ allInterfaces()

QList< QNetworkInterface > QNetworkInterface::allInterfaces ( )
static

Returns a listing of all the network interfaces found on the host machine.

Definition at line 564 of file qnetworkinterface.cpp.

Referenced by QGenericEngine::doRequestUpdate(), isBypassed(), multicastMembershipHelper(), and QNativeSocketEnginePrivate::nativeMulticastInterface().

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 }
QSharedDataPointer< QNetworkInterfacePrivate > d
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ flags()

QNetworkInterface::InterfaceFlags QNetworkInterface::flags ( ) const

Returns the flags associated with this network interface.

Definition at line 497 of file qnetworkinterface.cpp.

Referenced by multicastMembershipHelper(), and operator<<().

498 {
499  return d ? d->flags : InterfaceFlags(0);
500 }
QSharedDataPointer< QNetworkInterfacePrivate > d
QNetworkInterface::InterfaceFlags flags

◆ hardwareAddress()

QString QNetworkInterface::hardwareAddress ( ) const

Returns the low-level hardware address for this interface.

On Ethernet interfaces, this will be a MAC address in string representation, separated by colons.

Other interface types may have other types of hardware addresses. Implementations should not depend on this function returning a valid MAC address.

Definition at line 511 of file qnetworkinterface.cpp.

Referenced by operator<<().

512 {
513  return d ? d->hardwareAddress : QString();
514 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
QSharedDataPointer< QNetworkInterfacePrivate > d

◆ humanReadableName()

QString QNetworkInterface::humanReadableName ( ) const

Returns the human-readable name of this network interface on Windows, such as "Local Area Connection", if the name could be determined.

Since
4.5

If it couldn't, this function returns the same as name(). The human-readable name is a name that the user can modify in the Windows Control Panel, so it may change during the execution of the program.

On Unix, this function currently always returns the same as name(), since Unix systems don't store a configuration for human-readable names.

Definition at line 489 of file qnetworkinterface.cpp.

490 {
491  return d ? !d->friendlyName.isEmpty() ? d->friendlyName : name() : QString();
492 }
QString name() const
Returns the name of this network interface.
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QSharedDataPointer< QNetworkInterfacePrivate > d

◆ index()

int QNetworkInterface::index ( ) const

Returns the interface system index, if known.

Since
4.5 This is an integer assigned by the operating system to identify this interface and it generally doesn't change. It matches the scope ID field in IPv6 addresses.

If the index isn't known, this function returns 0.

Definition at line 456 of file qnetworkinterface.cpp.

Referenced by multicastMembershipHelper(), and QNativeSocketEnginePrivate::nativeSetMulticastInterface().

457 {
458  return d ? d->index : 0;
459 }
QSharedDataPointer< QNetworkInterfacePrivate > d

◆ interfaceFromIndex()

QNetworkInterface QNetworkInterface::interfaceFromIndex ( int  index)
static

Returns a QNetworkInterface object for the interface whose internal ID is index.

Network interfaces have a unique identifier called the "interface index" to distinguish it from other interfaces on the system. Often, this value is assigned progressively and interfaces being removed and then added again get a different value every time.

This index is also found in the IPv6 address' scope ID field.

Definition at line 553 of file qnetworkinterface.cpp.

Referenced by multicastMembershipHelper(), and QNativeSocketEnginePrivate::nativeMulticastInterface().

554 {
555  QNetworkInterface result;
556  result.d = manager()->interfaceFromIndex(index);
557  return result;
558 }
int index() const
Returns the interface system index, if known.
QSharedDataPointer< QNetworkInterfacePrivate > d
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...

◆ interfaceFromName()

QNetworkInterface QNetworkInterface::interfaceFromName ( const QString name)
static

Returns a QNetworkInterface object for the interface named name.

If no such interface exists, this function returns an invalid QNetworkInterface object.

See also
name(), isValid()

Definition at line 536 of file qnetworkinterface.cpp.

Referenced by QNetworkSessionPrivateImpl::currentInterface(), and QNetworkSessionManagerPrivate::forceSessionClose().

537 {
538  QNetworkInterface result;
539  result.d = manager()->interfaceFromName(name);
540  return result;
541 }
QSharedDataPointer< QNetworkInterfacePrivate > d
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...

◆ isValid()

bool QNetworkInterface::isValid ( ) const

Returns true if this QNetworkInterface object contains valid information about a network interface.

Definition at line 439 of file qnetworkinterface.cpp.

Referenced by multicastMembershipHelper(), and QNativeSocketEnginePrivate::nativeSetMulticastInterface().

440 {
441  return !name().isEmpty();
442 }
QString name() const
Returns the name of this network interface.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704

◆ name()

QString QNetworkInterface::name ( ) const

Returns the name of this network interface.

On Unix systems, this is a string containing the type of the interface and optionally a sequence number, such as "eth0", "lo" or "pcn0". On Windows, it's an internal ID that cannot be changed by the user.

Definition at line 467 of file qnetworkinterface.cpp.

Referenced by humanReadableName(), isValid(), and operator<<().

468 {
469  return d ? d->name : QString();
470 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
QSharedDataPointer< QNetworkInterfacePrivate > d

◆ operator=()

QNetworkInterface & QNetworkInterface::operator= ( const QNetworkInterface other)

Copies the contents of the QNetworkInterface object contained in other into this one.

Definition at line 429 of file qnetworkinterface.cpp.

430 {
431  d = other.d;
432  return *this;
433 }
QSharedDataPointer< QNetworkInterfacePrivate > d

Friends and Related Functions

◆ QNetworkInterfacePrivate

Definition at line 120 of file qnetworkinterface.h.

Properties

◆ d

QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterface::d
private

The documentation for this class was generated from the following files: