Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
The QHostAddress class provides an IP address. More...
#include <qhostaddress.h>
Public Types | |
enum | SpecialAddress { Null , Broadcast , LocalHost , LocalHostIPv6 , Any , AnyIPv6 , AnyIPv4 } |
\value Null The null address object. More... | |
enum | ConversionModeFlag { ConvertV4MappedToIPv4 = 1 , ConvertV4CompatToIPv4 = 2 , ConvertUnspecifiedAddress = 4 , ConvertLocalHost = 8 , TolerantConversion = 0xff , StrictConversion = 0 } |
enum | NetworkLayerProtocol { IPv4Protocol , IPv6Protocol , AnyIPProtocol , UnknownNetworkLayerProtocol = -1 } |
Public Member Functions | |
QHostAddress () | |
Constructs a null host address object, i.e. | |
QHostAddress (quint32 ip4Addr) | |
Constructs a host address object with the IPv4 address ip4Addr. | |
QHostAddress (const quint8 *ip6Addr) | |
QHostAddress (const Q_IPV6ADDR &ip6Addr) | |
Constructs a host address object with the IPv6 address ip6Addr. | |
QHostAddress (const sockaddr *address) | |
Constructs an IPv4 or IPv6 address using the address specified by the native structure sockaddr. | |
QHostAddress (const QString &address) | |
Constructs an IPv4 or IPv6 address based on the string address (e.g., "127.0.0.1"). | |
QHostAddress (const QHostAddress ©) | |
Constructs a copy of the given address. | |
QHostAddress (QHostAddress &&other) noexcept=default | |
QHostAddress (SpecialAddress address) | |
Constructs a QHostAddress object for address. | |
~QHostAddress () | |
Destroys the host address object. | |
QHostAddress & | operator= (QHostAddress &&other) noexcept |
QHostAddress & | operator= (const QHostAddress &other) |
Assigns another host address to this object, and returns a reference to this object. | |
QHostAddress & | operator= (SpecialAddress address) |
void | swap (QHostAddress &other) noexcept |
void | setAddress (quint32 ip4Addr) |
Set the IPv4 address specified by ip4Addr. | |
void | setAddress (const quint8 *ip6Addr) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
void | setAddress (const Q_IPV6ADDR &ip6Addr) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Set the IPv6 address specified by ip6Addr. | |
void | setAddress (const sockaddr *address) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the IPv4 or IPv6 address specified by the native structure sockaddr. | |
bool | setAddress (const QString &address) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the IPv4 or IPv6 address specified by the string representation specified by address (e.g. | |
void | setAddress (SpecialAddress address) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
NetworkLayerProtocol | protocol () const |
Returns the network layer protocol of the host address. | |
quint32 | toIPv4Address (bool *ok=nullptr) const |
Returns the IPv4 address as a number. | |
Q_IPV6ADDR | toIPv6Address () const |
Returns the IPv6 address as a Q_IPV6ADDR structure. | |
QString | toString () const |
Returns the address as a string. | |
QString | scopeId () const |
void | setScopeId (const QString &id) |
bool | isEqual (const QHostAddress &address, ConversionMode mode=TolerantConversion) const |
bool | operator== (const QHostAddress &address) const |
Returns true if this host address is the same as the other address given; otherwise returns false . | |
bool | operator== (SpecialAddress address) const |
Returns true if this host address is the same as the other address given; otherwise returns false . | |
bool | operator!= (const QHostAddress &address) const |
bool | operator!= (SpecialAddress address) const |
Returns true if this host address is not the same as the other address given; otherwise returns false . | |
bool | isNull () const |
Returns true if this host address is not valid for any host or interface. | |
void | clear () |
Sets the host address to null and sets the protocol to QAbstractSocket::UnknownNetworkLayerProtocol. | |
bool | isInSubnet (const QHostAddress &subnet, int netmask) const |
bool | isInSubnet (const std::pair< QHostAddress, int > &subnet) const |
bool | isLoopback () const |
bool | isGlobal () const |
bool | isLinkLocal () const |
bool | isSiteLocal () const |
bool | isUniqueLocalUnicast () const |
bool | isMulticast () const |
bool | isBroadcast () const |
bool | isPrivateUse () const |
Static Public Member Functions | |
static std::pair< QHostAddress, int > | parseSubnet (const QString &subnet) |
Protected Attributes | |
QExplicitlySharedDataPointer< QHostAddressPrivate > | d |
Friends | |
class | QHostAddressPrivate |
Q_NETWORK_EXPORT size_t | qHash (const QHostAddress &key, size_t seed=0) |
bool | operator== (QHostAddress::SpecialAddress lhs, const QHostAddress &rhs) |
Returns true if special address lhs is the same as host address rhs; otherwise returns false . | |
bool | operator!= (QHostAddress::SpecialAddress lhs, const QHostAddress &rhs) |
Related Symbols | |
(Note that these are not member symbols.) | |
QDataStream & | operator<< (QDataStream &out, const QHostAddress &address) |
Writes host address address to the stream out and returns a reference to the stream. | |
QDataStream & | operator>> (QDataStream &in, QHostAddress &address) |
Reads a host address into address from the stream in and returns a reference to the stream. | |
The QHostAddress class provides an IP address.
\inmodule QtNetwork
This class holds an IPv4 or IPv6 address in a platform- and protocol-independent manner.
QHostAddress is normally used with the QTcpSocket, QTcpServer, and QUdpSocket to connect to a host or to set up a server.
A host address is set with setAddress(), and retrieved with toIPv4Address(), toIPv6Address(), or toString(). You can check the type with protocol().
The class also supports common predefined addresses: \l Null, \l LocalHost, \l LocalHostIPv6, \l Broadcast, and \l Any.
Definition at line 39 of file qhostaddress.h.
\value StrictConversion Don't convert IPv6 addresses to IPv4 when comparing two QHostAddress objects of different protocols, so they will always be considered different. \value ConvertV4MappedToIPv4 Convert IPv4-mapped IPv6 addresses (RFC 4291 sect. 2.5.5.2) when comparing. Therefore QHostAddress("::ffff:192.168.1.1") will compare equal to QHostAddress("192.168.1.1"). \value ConvertV4CompatToIPv4 Convert IPv4-compatible IPv6 addresses (RFC 4291 sect. 2.5.5.1) when comparing. Therefore QHostAddress("::192.168.1.1") will compare equal to QHostAddress("192.168.1.1"). \value ConvertLocalHost Convert the IPv6 loopback addresses to its IPv4 equivalent when comparing. Therefore e.g. QHostAddress("::1") will compare equal to QHostAddress("127.0.0.1"). \value ConvertUnspecifiedAddress All unspecified addresses will compare equal, namely AnyIPv4, AnyIPv6 and Any. \value TolerantConversion Sets all three preceding flags.
Enumerator | |
---|---|
ConvertV4MappedToIPv4 | |
ConvertV4CompatToIPv4 | |
ConvertUnspecifiedAddress | |
ConvertLocalHost | |
TolerantConversion | |
StrictConversion |
Definition at line 52 of file qhostaddress.h.
Enumerator | |
---|---|
IPv4Protocol | |
IPv6Protocol | |
AnyIPProtocol | |
UnknownNetworkLayerProtocol |
Definition at line 70 of file qhostaddress.h.
\value Null The null address object.
Equivalent to QHostAddress(). See also QHostAddress::isNull(). \value LocalHost The IPv4 localhost address. Equivalent to QHostAddress("127.0.0.1"). \value LocalHostIPv6 The IPv6 localhost address. Equivalent to QHostAddress("::1"). \value Broadcast The IPv4 broadcast address. Equivalent to QHostAddress("255.255.255.255"). \value AnyIPv4 The IPv4 any-address. Equivalent to QHostAddress("0.0.0.0"). A socket bound with this address will listen only on IPv4 interfaces. \value AnyIPv6 The IPv6 any-address. Equivalent to QHostAddress("::"). A socket bound with this address will listen only on IPv6 interfaces. \value Any The dual stack any-address. A socket bound with this address will listen on both IPv4 and IPv6 interfaces.
Enumerator | |
---|---|
Null | |
Broadcast | |
LocalHost | |
LocalHostIPv6 | |
Any | |
AnyIPv6 | |
AnyIPv4 |
Definition at line 43 of file qhostaddress.h.
QHostAddress::QHostAddress | ( | ) |
Constructs a null host address object, i.e.
an address which is not valid for any host or interface.
Definition at line 381 of file qhostaddress.cpp.
|
explicit |
Constructs a host address object with the IPv4 address ip4Addr.
Definition at line 389 of file qhostaddress.cpp.
|
explicit |
ip6Addr must be a 16-byte array in network byte order (big endian).
Definition at line 402 of file qhostaddress.cpp.
|
explicit |
Constructs a host address object with the IPv6 address ip6Addr.
Definition at line 411 of file qhostaddress.cpp.
|
explicit |
Constructs an IPv4 or IPv6 address using the address specified by the native structure sockaddr.
|
explicit |
Constructs an IPv4 or IPv6 address based on the string address (e.g., "127.0.0.1").
Definition at line 423 of file qhostaddress.cpp.
QHostAddress::QHostAddress | ( | const QHostAddress & | copy | ) |
Constructs a copy of the given address.
Definition at line 449 of file qhostaddress.cpp.
|
defaultnoexcept |
Move-constructs a new QHostAddress from other.
QHostAddress::QHostAddress | ( | SpecialAddress | address | ) |
Constructs a QHostAddress object for address.
Definition at line 469 of file qhostaddress.cpp.
QHostAddress::~QHostAddress | ( | ) |
Destroys the host address object.
Definition at line 478 of file qhostaddress.cpp.
void QHostAddress::clear | ( | ) |
Sets the host address to null and sets the protocol to QAbstractSocket::UnknownNetworkLayerProtocol.
Definition at line 532 of file qhostaddress.cpp.
bool QHostAddress::isBroadcast | ( | ) | const |
Returns true
if the address is the IPv4 broadcast address, false
otherwise. The IPv4 broadcast address is 255.255.255.255.
Note that this function does not return true for an IPv4 network's local broadcast address. For that, please use \l QNetworkInterface to obtain the broadcast addresses of the local machine.
Definition at line 1226 of file qhostaddress.cpp.
bool QHostAddress::isEqual | ( | const QHostAddress & | other, |
ConversionMode | mode = TolerantConversion ) const |
Returns true
if this host address is the same as the other address given; otherwise returns false
.
The parameter mode controls which conversions are performed between addresses of differing protocols. If no mode is given, TolerantConversion
is performed by default.
Definition at line 809 of file qhostaddress.cpp.
bool QHostAddress::isGlobal | ( | ) | const |
Returns true
if the address is an IPv4 or IPv6 global address, false
otherwise. A global address is an address that is not reserved for special purposes (like loopback or multicast) or future purposes.
Note that IPv6 unique local unicast addresses are considered global addresses (see isUniqueLocalUnicast()), as are IPv4 addresses reserved for local networks by \l {RFC 1918}.
Also note that IPv6 site-local addresses are deprecated and should be considered as global in new applications. This function returns true for site-local addresses too.
Definition at line 1134 of file qhostaddress.cpp.
bool QHostAddress::isInSubnet | ( | const QHostAddress & | subnet, |
int | netmask ) const |
Returns true
if this IP is in the subnet described by the network prefix subnet and netmask netmask.
An IP is considered to belong to a subnet if it is contained between the lowest and the highest address in that subnet. In the case of IP version 4, the lowest address is the network address, while the highest address is the broadcast address.
The subnet argument does not have to be the actual network address (the lowest address in the subnet). It can be any valid IP belonging to that subnet. In particular, if it is equal to the IP address held by this object, this function will always return true (provided the netmask is a valid value).
Definition at line 927 of file qhostaddress.cpp.
bool QHostAddress::isInSubnet | ( | const std::pair< QHostAddress, int > & | subnet | ) | const |
Returns true
if this IP is in the subnet described by subnet. The QHostAddress member of subnet contains the network prefix and the int (second) member contains the netmask (prefix length).
Definition at line 975 of file qhostaddress.cpp.
bool QHostAddress::isLinkLocal | ( | ) | const |
Returns true
if the address is an IPv4 or IPv6 link-local address, false
otherwise.
An IPv4 link-local address is an address in the network 169.254.0.0/16. An IPv6 link-local address is one in the network fe80::/10. See the \l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml}{IANA IPv6 Address Space} registry for more information.
Definition at line 1152 of file qhostaddress.cpp.
bool QHostAddress::isLoopback | ( | ) | const |
returns true
if the address is the IPv6 loopback address, or any of the IPv4 loopback addresses.
Definition at line 1112 of file qhostaddress.cpp.
bool QHostAddress::isMulticast | ( | ) | const |
Returns true
if the address is an IPv4 or IPv6 multicast address, false
otherwise.
Definition at line 1209 of file qhostaddress.cpp.
bool QHostAddress::isNull | ( | ) | const |
Returns true
if this host address is not valid for any host or interface.
The default constructor creates a null address.
Definition at line 903 of file qhostaddress.cpp.
bool QHostAddress::isPrivateUse | ( | ) | const |
Returns true
if the address is an IPv6 unique local unicast address or IPv4 address reserved for local networks by \l {RFC 1918}, false
otherwise.
Definition at line 1239 of file qhostaddress.cpp.
bool QHostAddress::isSiteLocal | ( | ) | const |
Returns true
if the address is an IPv6 site-local address, false
otherwise.
An IPv6 site-local address is one in the network fec0::/10. See the \l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml}{IANA IPv6 Address Space} registry for more information.
IPv6 site-local addresses are deprecated and should not be depended upon in new applications. New applications should not depend on this function and should consider site-local addresses the same as global (which is why isGlobal() also returns true). Site-local addresses were replaced by Unique Local Addresses (ULA).
Definition at line 1175 of file qhostaddress.cpp.
bool QHostAddress::isUniqueLocalUnicast | ( | ) | const |
Returns true
if the address is an IPv6 unique local unicast address, false
otherwise.
An IPv6 unique local unicast address is one in the network fc00::/7. See the \l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml} {IANA IPv6 Address Space} registry for more information.
Note that Unique local unicast addresses count as global addresses too. RFC 4193 says that, in practice, "applications may treat these addresses like global scoped addresses." Only routers need care about the distinction.
Definition at line 1196 of file qhostaddress.cpp.
|
inline |
Returns true
if this host address is not the same as the other address given; otherwise returns false
.
Definition at line 116 of file qhostaddress.h.
|
inline |
Returns true
if this host address is not the same as the other address given; otherwise returns false
.
Definition at line 118 of file qhostaddress.h.
QHostAddress & QHostAddress::operator= | ( | const QHostAddress & | other | ) |
Assigns another host address to this object, and returns a reference to this object.
Definition at line 486 of file qhostaddress.cpp.
|
inlinenoexcept |
Definition at line 90 of file qhostaddress.h.
QHostAddress & QHostAddress::operator= | ( | SpecialAddress | address | ) |
Definition at line 499 of file qhostaddress.cpp.
bool QHostAddress::operator== | ( | const QHostAddress & | other | ) | const |
Returns true
if this host address is the same as the other address given; otherwise returns false
.
This operator just calls isEqual(other, StrictConversion).
Definition at line 792 of file qhostaddress.cpp.
bool QHostAddress::operator== | ( | SpecialAddress | address | ) | const |
Returns true
if this host address is the same as the other address given; otherwise returns false
.
Definition at line 862 of file qhostaddress.cpp.
|
static |
Parses the IP and subnet information contained in subnet and returns the network prefix for that network and its prefix length.
The IP address and the netmask must be separated by a slash (/).
This function supports arguments in the form: \list
For IP version 4, this function accepts as well missing trailing components (i.e., less than 4 octets, like "192.168.1"), followed or not by a dot. If the netmask is also missing in that case, it is set to the number of octets actually passed (in the example above, it would be 24, for 3 octets).
Definition at line 1005 of file qhostaddress.cpp.
QHostAddress::NetworkLayerProtocol QHostAddress::protocol | ( | ) | const |
Returns the network layer protocol of the host address.
Definition at line 677 of file qhostaddress.cpp.
QString QHostAddress::scopeId | ( | ) | const |
Returns the scope ID of an IPv6 address. For IPv4 addresses, or if the address does not contain a scope ID, an empty QString is returned.
The IPv6 scope ID specifies the scope of reachability for non-global IPv6 addresses, limiting the area in which the address can be used. All IPv6 addresses are associated with such a reachability scope. The scope ID is used to disambiguate addresses that are not guaranteed to be globally unique.
IPv6 specifies the following four levels of reachability:
\list
\endlist
When using a link-local or site-local address for IPv6 connections, you must specify the scope ID. The scope ID for a link-local address is usually the same as the interface name (e.g., "eth0", "en1") or number (e.g., "1", "2").
Definition at line 762 of file qhostaddress.cpp.
void QHostAddress::setAddress | ( | const Q_IPV6ADDR & | ip6Addr | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Set the IPv6 address specified by ip6Addr.
Definition at line 567 of file qhostaddress.cpp.
bool QHostAddress::setAddress | ( | const QString & | address | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the IPv4 or IPv6 address specified by the string representation specified by address (e.g.
"127.0.0.1"). Returns true
and sets the address if the address was successfully parsed; otherwise returns false
.
Definition at line 581 of file qhostaddress.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Set the IPv6 address specified by ip6Addr.
ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).
Definition at line 556 of file qhostaddress.cpp.
void QHostAddress::setAddress | ( | const sockaddr * | sockaddr | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the IPv4 or IPv6 address specified by the native structure sockaddr.
Returns true
and sets the address if the address was successfully parsed; otherwise returns false
.
Set the IPv4 address specified by ip4Addr.
Definition at line 541 of file qhostaddress.cpp.
void QHostAddress::setAddress | ( | SpecialAddress | address | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Sets the special address specified by address.
Definition at line 611 of file qhostaddress.cpp.
Sets the IPv6 scope ID of the address to id. If the address protocol is not IPv6, this function does nothing. The scope ID may be set as an interface name (such as "eth0" or "en1") or as an integer representing the interface index. If id is an interface name, QtNetwork will convert to an interface index using QNetworkInterface::interfaceIndexFromName() before calling the operating system networking functions.
Definition at line 779 of file qhostaddress.cpp.
|
inlinenoexcept |
Definition at line 95 of file qhostaddress.h.
Returns the IPv4 address as a number.
For example, if the address is 127.0.0.1, the returned value is 2130706433 (i.e. 0x7f000001).
This value is valid if the protocol() is \l{QAbstractSocket::}{IPv4Protocol}, or if the protocol is \l{QAbstractSocket::}{IPv6Protocol}, and the IPv6 address is an IPv4 mapped address (RFC4291). In those cases, ok will be set to true. Otherwise, it will be set to false.
Definition at line 663 of file qhostaddress.cpp.
Q_IPV6ADDR QHostAddress::toIPv6Address | ( | ) | const |
Returns the IPv6 address as a Q_IPV6ADDR structure.
The structure consists of 16 unsigned characters.
This value is valid if the protocol() is \l{QAbstractSocket::}{IPv6Protocol}. If the protocol is \l{QAbstractSocket::}{IPv4Protocol}, then the address is returned as an IPv4 mapped IPv6 address. (RFC4291)
Definition at line 696 of file qhostaddress.cpp.
QString QHostAddress::toString | ( | ) | const |
Returns the address as a string.
For example, if the address is the IPv4 address 127.0.0.1, the returned string is "127.0.0.1". For IPv6 the string format will follow the RFC5952 recommendation. For QHostAddress::Any, its IPv4 address will be returned ("0.0.0.0")
Definition at line 711 of file qhostaddress.cpp.
|
friend |
Returns false
if special address lhs is the same as host address rhs; otherwise returns true
.
Definition at line 141 of file qhostaddress.h.
|
related |
Writes host address address to the stream out and returns a reference to the stream.
Definition at line 1296 of file qhostaddress.cpp.
|
friend |
Returns true
if special address lhs is the same as host address rhs; otherwise returns false
.
Definition at line 139 of file qhostaddress.h.
|
related |
Reads a host address into address from the stream in and returns a reference to the stream.
Definition at line 1327 of file qhostaddress.cpp.
|
friend |
Returns a hash of the host address key, using seed to seed the calculation.
Definition at line 1263 of file qhostaddress.cpp.
|
friend |
Definition at line 145 of file qhostaddress.h.
|
protected |
Definition at line 146 of file qhostaddress.h.