Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QNetworkInterface Class Reference

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

#include <qnetworkinterface.h>

+ Collaboration diagram for QNetworkInterface:

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...
 
enum  InterfaceType {
  Loopback = 1 , Virtual , Ethernet , Slip ,
  CanBus , Ppp , Fddi , Wifi ,
  Ieee80211 = Wifi , Phonet , Ieee802154 , SixLoWPAN ,
  Ieee80216 , Ieee1394 , Unknown = 0
}
 Specifies the type of hardware (PHY layer, OSI level 1) this interface is, if it could be determined. More...
 

Public Member Functions

 QNetworkInterface ()
 Constructs an empty network interface object.
 
 QNetworkInterface (const QNetworkInterface &other)
 Creates a copy of the QNetworkInterface object contained in other.
 
QNetworkInterfaceoperator= (QNetworkInterface &&other) noexcept
 
QNetworkInterfaceoperator= (const QNetworkInterface &other)
 Copies the contents of the QNetworkInterface object contained in other into this one.
 
 ~QNetworkInterface ()
 Frees the resources associated with the QNetworkInterface object.
 
void swap (QNetworkInterface &other) noexcept
 
bool isValid () const
 Returns true if this QNetworkInterface object contains valid information about a network interface.
 
int index () const
 
int maximumTransmissionUnit () const
 
QString name () const
 Returns the name of this network interface.
 
QString humanReadableName () const
 
InterfaceFlags flags () const
 Returns the flags associated with this network interface.
 
InterfaceType type () const
 
QString hardwareAddress () const
 Returns the low-level hardware address for this interface.
 
QList< QNetworkAddressEntryaddressEntries () const
 Returns the list of IP addresses that this interface possesses along with their associated netmasks and broadcast addresses.
 

Static Public Member Functions

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

Friends

class QNetworkInterfacePrivate
 

Related Symbols

(Note that these are not member symbols.)

QDebug operator<< (QDebug debug, const QNetworkInterface &networkInterface)
 Writes the QNetworkInterface networkInterface to the stream and returns a reference to the debug stream.
 

Detailed Description

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

Since
4.2 \reentrant

\inmodule QtNetwork

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 or other information aren't necessary, use the allAddresses() convenience function to obtain just the IP addresses of the active interfaces.

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, Linux, \macos and the BSDs.

See also
QNetworkAddressEntry

Definition at line 72 of file qnetworkinterface.h.

Member Enumeration Documentation

◆ InterfaceFlag

Specifies the flags associated with this network interface.

The possible values are:

\value IsUp the network interface is "up" - enabled by administrative action \value IsRunning the network interface is operational: configured "up" and (typically) physically connected to a network \value CanBroadcast the network interface works in broadcast mode \value IsLoopBack the network interface is a loopback interface: that is, it's a virtual interface whose destination is the host computer itself \value 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. \value 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 76 of file qnetworkinterface.h.

◆ InterfaceType

Specifies the type of hardware (PHY layer, OSI level 1) this interface is, if it could be determined.

Interface types that are not among those listed below will generally be listed as Unknown, though future versions of Qt may add new enumeration values.

The possible values are:

\value Unknown The interface type could not be determined or is not one of the other listed types. \value Loopback The virtual loopback interface, which is assigned the loopback IP addresses (127.0.0.1, ::1). \value Virtual A type of interface determined to be virtual, but not any of the other possible types. For example, tunnel interfaces are (currently) detected as virtual ones. \value Ethernet IEEE 802.3 Ethernet interfaces, though on many systems other types of IEEE 802 interfaces may also be detected as Ethernet (especially Wi-Fi). \value Wifi IEEE 802.11 Wi-Fi interfaces. Note that on some systems, QNetworkInterface may be unable to distinguish regular Ethernet from Wi-Fi and will not return this enum value. \value Ieee80211 An alias for WiFi. \value CanBus ISO 11898 Controller Area Network bus interfaces, usually found on automotive systems. \value Fddi ANSI X3T12 Fiber Distributed Data Interface, a local area network over optical fibers. \value Ppp Point-to-Point Protocol interfaces, establishing a direct connection between two nodes over a lower transport layer (often serial over radio or physical line). \value Slip Serial Line Internet Protocol interfaces. \value Phonet Interfaces using the Linux Phonet socket family, for communication with cellular modems. See the \l {https://www.kernel.org/doc/Documentation/networking/phonet.txt}{Linux kernel documentation} for more information. \value Ieee802154 IEEE 802.15.4 Personal Area Network interfaces, other than 6LoWPAN (see below). \value SixLoWPAN 6LoWPAN (IPv6 over Low-power Wireless Personal Area Networks) interfaces, which operate on IEEE 802.15.4 PHY, but have specific header compression schemes for IPv6 and UDP. This type of interface is often used for mesh networking. \value Ieee80216 IEEE 802.16 Wireless Metropolitan Area Network, also known under the commercial name "WiMAX". \value Ieee1394 IEEE 1394 interfaces (a.k.a. "FireWire").

Enumerator
Loopback 
Virtual 
Ethernet 
Slip 
CanBus 
Ppp 
Fddi 
Wifi 
Ieee80211 
Phonet 
Ieee802154 
SixLoWPAN 
Ieee80216 
Ieee1394 
Unknown 

Definition at line 87 of file qnetworkinterface.h.

Constructor & Destructor Documentation

◆ QNetworkInterface() [1/2]

QNetworkInterface::QNetworkInterface ( )

Constructs an empty network interface object.

Definition at line 601 of file qnetworkinterface.cpp.

◆ QNetworkInterface() [2/2]

QNetworkInterface::QNetworkInterface ( const QNetworkInterface & other)

Creates a copy of the QNetworkInterface object contained in other.

Definition at line 617 of file qnetworkinterface.cpp.

◆ ~QNetworkInterface()

QNetworkInterface::~QNetworkInterface ( )

Frees the resources associated with the QNetworkInterface object.

Definition at line 609 of file qnetworkinterface.cpp.

Member Function Documentation

◆ 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 or other information is not necessary, you can call the allAddresses() function to obtain just the IP addresses of the active interfaces.

Definition at line 761 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::addressEntries.

◆ 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() that are in the QNetworkInterface::IsUp state to obtain lists of QNetworkAddressEntry objects then calling QNetworkAddressEntry::ip() on each of these.

Definition at line 867 of file qnetworkinterface.cpp.

References IsUp, and manager.

◆ allInterfaces()

QList< QNetworkInterface > QNetworkInterface::allInterfaces ( )
static

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

In case of failure it returns a list with zero elements.

Definition at line 846 of file qnetworkinterface.cpp.

References item, and manager.

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

+ Here is the caller graph for this function:

◆ flags()

QNetworkInterface::InterfaceFlags QNetworkInterface::flags ( ) const

Returns the flags associated with this network interface.

Definition at line 719 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::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.

See also
type()

Definition at line 748 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::hardwareAddress.

◆ humanReadableName()

QString QNetworkInterface::humanReadableName ( ) const
Since
4.5

Returns the human-readable name of this network interface on Windows, such as "Local Area Connection", if the name could be determined. 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 711 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::friendlyName, QString::isEmpty(), and name().

+ Here is the call graph for this function:

◆ index()

int QNetworkInterface::index ( ) const
Since
4.5 Returns the interface system index, if known. 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 658 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::index.

◆ 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 816 of file qnetworkinterface.cpp.

References manager.

Referenced by QNativeSocketEnginePrivate::nativeMulticastInterface().

+ Here is the caller graph for this function:

◆ 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.

The string name may be either an actual interface name (such as "eth0" or "en1") or an interface index in string form ("1", "2", etc.).

See also
name(), isValid()

Definition at line 799 of file qnetworkinterface.cpp.

References manager.

◆ interfaceIndexFromName()

int QNetworkInterface::interfaceIndexFromName ( const QString & name)
static
Since
5.7

Returns the index of the interface whose name is name or 0 if there is no interface with that name. This function should produce the same result as the following code, but will probably execute faster.

See also
interfaceFromName(), interfaceNameFromIndex(), QNetworkDatagram::interfaceIndex()

Definition at line 777 of file qnetworkinterface.cpp.

References QNetworkInterfaceManager::interfaceIndexFromName(), and ok.

Referenced by QT_BEGIN_NAMESPACE::setSockaddr().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interfaceNameFromIndex()

QString QNetworkInterface::interfaceNameFromIndex ( int index)
static
Since
5.7

Returns the name of the interface whose index is index or an empty string if there is no interface with that index. This function should produce the same result as the following code, but will probably execute faster.

See also
interfaceFromIndex(), interfaceIndexFromName(), QNetworkDatagram::interfaceIndex()

Definition at line 835 of file qnetworkinterface.cpp.

References QNetworkInterfaceManager::interfaceNameFromIndex().

Referenced by qt_socket_getPortAndAddress().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isValid()

bool QNetworkInterface::isValid ( ) const

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

Definition at line 644 of file qnetworkinterface.cpp.

References QString::isEmpty(), and name().

+ Here is the call graph for this function:

◆ maximumTransmissionUnit()

int QNetworkInterface::maximumTransmissionUnit ( ) const
Since
5.11

Returns the maximum transmission unit on this interface, if known, or 0 otherwise.

The maximum transmission unit is the largest packet that may be sent on this interface without incurring link-level fragmentation. Applications may use this value to calculate the size of the payload that will fit an unfragmented UDP datagram. Remember to subtract the sizes of headers used in your communication over the interface, e.g. TCP (20 bytes) or UDP (12), IPv4 (20) or IPv6 (40, absent some form of header compression), when computing how big a payload you can transmit. Also note that the MTU along the full path (the Path MTU) to the destination may be smaller than the interface's MTU.

See also
QUdpSocket

Definition at line 681 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::mtu.

◆ 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 692 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::name.

Referenced by humanReadableName(), and isValid().

+ Here is the caller graph for this function:

◆ operator=() [1/2]

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

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

Definition at line 626 of file qnetworkinterface.cpp.

References other().

+ Here is the call graph for this function:

◆ operator=() [2/2]

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

Definition at line 109 of file qnetworkinterface.h.

References other(), and swap().

+ Here is the call graph for this function:

◆ swap()

void QNetworkInterface::swap ( QNetworkInterface & other)
inlinenoexcept
Since
5.0

Swaps this network interface instance with other. This function is very fast and never fails.

Definition at line 113 of file qnetworkinterface.h.

References d, and other().

+ Here is the call graph for this function:

◆ type()

QNetworkInterface::InterfaceType QNetworkInterface::type ( ) const
Since
5.11

Returns the type of this interface, if it could be determined. If it could not be determined, this function returns QNetworkInterface::Unknown.

See also
hardwareAddress()

Definition at line 732 of file qnetworkinterface.cpp.

References QNetworkInterfacePrivate::type, and Unknown.

Friends And Related Symbol Documentation

◆ operator<<()

QDebug operator<< ( QDebug debug,
const QNetworkInterface & networkInterface )
related

Writes the QNetworkInterface networkInterface to the stream and returns a reference to the debug stream.

Definition at line 927 of file qnetworkinterface.cpp.

References debug, and flagsDebug().

+ Here is the call graph for this function:

◆ QNetworkInterfacePrivate

friend class QNetworkInterfacePrivate
friend

Definition at line 134 of file qnetworkinterface.h.


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