Qt 4.8
|
The QHostAddress class provides an IP address. More...
#include <qhostaddress.h>
Public Types | |
enum | SpecialAddress { Null, Broadcast, LocalHost, LocalHostIPv6, Any, AnyIPv6 } |
Public Functions | |
void | clear () |
Sets the host address to 0.0.0.0. More... | |
bool | 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. More... | |
bool | isInSubnet (const QPair< QHostAddress, int > &subnet) const |
Returns true if this IP is in the subnet described by subnet. More... | |
bool | isNull () const |
Returns true if this host address is null (INADDR_ANY or in6addr_any). More... | |
bool | operator!= (const QHostAddress &address) const |
Returns true if this host address is not the same as the other address given; otherwise returns false. More... | |
bool | operator!= (SpecialAddress address) const |
Returns true if this host address is not the same as the other address given; otherwise returns false. More... | |
QHostAddress & | operator= (const QHostAddress &other) |
Assigns another host address to this object, and returns a reference to this object. More... | |
QHostAddress & | operator= (const QString &address) |
Assigns the host address address to this object, and returns a reference to this object. More... | |
bool | operator== (const QHostAddress &address) const |
Returns true if this host address is the same as the other address given; otherwise returns false. More... | |
bool | operator== (SpecialAddress address) const |
Returns true if this host address is the same as the other address given; otherwise returns false. More... | |
QAbstractSocket::NetworkLayerProtocol | protocol () const |
Returns the network layer protocol of the host address. More... | |
QHostAddress () | |
Constructs a host address object with the IP address 0.0.0.0. More... | |
QHostAddress (quint32 ip4Addr) | |
Constructs a host address object with the IPv4 address ip4Addr. More... | |
QHostAddress (quint8 *ip6Addr) | |
Constructs a host address object with the IPv6 address ip6Addr. More... | |
QHostAddress (const Q_IPV6ADDR &ip6Addr) | |
Constructs a host address object with the IPv6 address ip6Addr. More... | |
QHostAddress (const sockaddr *sockaddr) | |
Constructs an IPv4 or IPv6 address using the address specified by the native structure sockaddr. More... | |
QHostAddress (const QString &address) | |
Constructs an IPv4 or IPv6 address based on the string address (e.g., "127.0.0.1"). More... | |
QHostAddress (const QHostAddress ©) | |
Constructs a copy of the given address. More... | |
QHostAddress (SpecialAddress address) | |
Constructs a QHostAddress object for address. More... | |
QString | scopeId () const |
Returns the scope ID of an IPv6 address. More... | |
void | setAddress (quint32 ip4Addr) |
Set the IPv4 address specified by ip4Addr. More... | |
void | setAddress (quint8 *ip6Addr) |
Set the IPv6 address specified by ip6Addr. More... | |
void | setAddress (const Q_IPV6ADDR &ip6Addr) |
Set the IPv6 address specified by ip6Addr. More... | |
void | 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. More... | |
bool | setAddress (const QString &address) |
Sets the IPv4 or IPv6 address specified by the string representation specified by address (e. More... | |
void | setScopeId (const QString &id) |
Sets the IPv6 scope ID of the address to id. More... | |
quint32 | toIPv4Address () const |
Returns the IPv4 address as a number. More... | |
Q_IPV6ADDR | toIPv6Address () const |
Returns the IPv6 address as a Q_IPV6ADDR structure. More... | |
QString | toString () const |
Returns the address as a string. More... | |
~QHostAddress () | |
Destroys the host address object. More... | |
Static Public Functions | |
static QPair< QHostAddress, int > | parseSubnet (const QString &subnet) |
Parses the IP and subnet information contained in subnet and returns the network prefix for that network and its prefix length. More... | |
Protected Variables | |
QScopedPointer< QHostAddressPrivate > | d |
Related Functions | |
(Note that these are not member functions.) | |
QDataStream & | operator<< (QDataStream &out, const QHostAddress &address) |
Writes host address address to the stream out and returns a reference to the stream. More... | |
QDataStream & | operator>> (QDataStream &in, QHostAddress &address) |
Reads a host address into address from the stream in and returns a reference to the stream. More... | |
The QHostAddress class provides an IP address.
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: Null , LocalHost , LocalHostIPv6 , Broadcast , and Any .
Definition at line 70 of file qhostaddress.h.
Enumerator | |
---|---|
Null | |
Broadcast | |
LocalHost | |
LocalHostIPv6 | |
Any | |
AnyIPv6 |
Definition at line 73 of file qhostaddress.h.
QHostAddress::QHostAddress | ( | ) |
Constructs a host address object with the IP address 0.0.0.0.
Definition at line 460 of file qhostaddress.cpp.
Referenced by parseSubnet(), and QHostAddress().
|
explicit |
Constructs a host address object with the IPv4 address ip4Addr.
Definition at line 468 of file qhostaddress.cpp.
|
explicit |
Constructs a host address object with the IPv6 address ip6Addr.
ip6Addr must be a 16-byte array in network byte order (big endian).
Definition at line 480 of file qhostaddress.cpp.
|
explicit |
Constructs a host address object with the IPv6 address ip6Addr.
Definition at line 489 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 501 of file qhostaddress.cpp.
QHostAddress::QHostAddress | ( | const QHostAddress & | copy | ) |
Constructs a copy of the given address.
Definition at line 530 of file qhostaddress.cpp.
QHostAddress::QHostAddress | ( | SpecialAddress | address | ) |
Constructs a QHostAddress object for address.
Definition at line 538 of file qhostaddress.cpp.
QHostAddress::~QHostAddress | ( | ) |
void QHostAddress::clear | ( | ) |
Sets the host address to 0.0.0.0.
Definition at line 615 of file qhostaddress.cpp.
Referenced by QNativeSocketEnginePrivate::fetchConnectionParameters(), operator>>(), and setAddress().
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 968 of file qhostaddress.cpp.
Referenced by isBypassed(), isHostExcluded(), Maemo::ProxyConfPrivate::isHostExcluded(), and isInSubnet().
bool QHostAddress::isInSubnet | ( | const QPair< 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 1020 of file qhostaddress.cpp.
bool QHostAddress::isNull | ( | ) | const |
Returns true if this host address is null (INADDR_ANY or in6addr_any).
The default constructor creates a null address, and that address is not valid for any host or interface.
Definition at line 904 of file qhostaddress.cpp.
Referenced by interfaceListing(), QSslSocketBackendPrivate::isMatchingHostname(), and operator<<().
|
inline |
Returns true if this host address is not the same as the other address given; otherwise returns false.
Definition at line 112 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 114 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 573 of file qhostaddress.cpp.
Referenced by QNetmaskAddress::setAddress().
QHostAddress & QHostAddress::operator= | ( | const QString & | address | ) |
Assigns the host address address to this object, and returns a reference to this object.
Definition at line 585 of file qhostaddress.cpp.
bool QHostAddress::operator== | ( | const QHostAddress & | address | ) | const |
Returns true if this host address is the same as the other address given; otherwise returns false.
Definition at line 866 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 884 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:
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 1053 of file qhostaddress.cpp.
Referenced by isBypassed(), isHostExcluded(), and Maemo::ProxyConfPrivate::isHostExcluded().
QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol | ( | ) | const |
Returns the network layer protocol of the host address.
Definition at line 713 of file qhostaddress.cpp.
Referenced by QAbstractSocketPrivate::_q_connectToNextAddress(), QNativeSocketEngine::bind(), QNativeSocketEngine::connectToHost(), QUdpSocketPrivate::doEnsureInitialized(), QHostInfoAgent::fromName(), interfaceListingWinXP(), isInSubnet(), QTcpServer::listen(), multicastMembershipHelper(), QNativeSocketEnginePrivate::nativeBind(), QNativeSocketEnginePrivate::nativeConnect(), QNativeSocketEnginePrivate::nativeSendDatagram(), QNativeSocketEnginePrivate::nativeSetMulticastInterface(), operator<<(), QHttpNetworkConnectionPrivate::prepareRequest(), qt_socket_setPortAndAddress(), qt_socks5_set_host_address_and_port(), QNetworkAddressEntry::setNetmask(), QNetworkAddressEntry::setPrefixLength(), QFtpPI::startNextCmd(), and translateWSAError().
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:
Node-local: Addresses that are only used for communicating with services on the same interface (e.g., the loopback interface "::1").
Link-local: Addresses that are local to the network interface (link). There is always one link-local address for each IPv6 interface on your host. Link-local addresses ("fe80...") are generated from the MAC address of the local network adaptor, and are not guaranteed to be unique.
Site-local: Addresses that are local to the site / private network (e.g., the company intranet). Site-local addresses ("fec0...") are usually distributed by the site router, and are not guaranteed to be unique outside of the local site.
Global: For globally routable addresses, such as public servers on the Internet.
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 840 of file qhostaddress.cpp.
Referenced by QNativeSocketEnginePrivate::nativeBind(), QNativeSocketEnginePrivate::nativeConnect(), QNativeSocketEnginePrivate::nativeSendDatagram(), operator<<(), and qt_socket_setPortAndAddress().
void QHostAddress::setAddress | ( | quint32 | ip4Addr | ) |
Set the IPv4 address specified by ip4Addr.
Definition at line 623 of file qhostaddress.cpp.
Referenced by addressFromSockaddr(), QAbstractSocket::connectToHostImplementation(), QHostInfoAgent::fromName(), QSslSocketBackendPrivate::initSslContext(), isBypassed(), isHostExcluded(), Maemo::ProxyConfPrivate::isHostExcluded(), operator=(), operator>>(), parseSubnet(), QHttpNetworkConnectionPrivate::prepareRequest(), QGlobalNetworkProxy::proxyForQuery(), QHostAddress(), qt_socket_getPortAndAddress(), qt_socks5_get_host_address_and_port(), QNetmaskAddress::setAddress(), setAddress(), and translateWSAError().
void QHostAddress::setAddress | ( | quint8 * | ip6Addr | ) |
Set the IPv6 address specified by ip6Addr.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).
Definition at line 639 of file qhostaddress.cpp.
void QHostAddress::setAddress | ( | const Q_IPV6ADDR & | ip6Addr | ) |
Set the IPv6 address specified by ip6Addr.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 652 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.
bool QHostAddress::setAddress | ( | const QString & | address | ) |
Sets the IPv4 or IPv6 address specified by the string representation specified by address (e.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
g. "127.0.0.1"). Returns true and sets the address if the address was successfully parsed; otherwise returns false.
Definition at line 668 of file qhostaddress.cpp.
void QHostAddress::setScopeId | ( | const QString & | id | ) |
Sets the IPv6 scope ID of the address to id.
If the address protocol is not IPv6, this function does nothing.
Definition at line 855 of file qhostaddress.cpp.
Referenced by addressFromSockaddr(), QHostInfoAgent::fromName(), operator>>(), and qt_socket_getPortAndAddress().
quint32 QHostAddress::toIPv4Address | ( | ) | const |
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 only valid if the Protocol() is QAbstractSocket::IPv4Protocol.
Definition at line 704 of file qhostaddress.cpp.
Referenced by QHostInfoAgent::fromName(), multicastMembershipHelper(), QNativeSocketEnginePrivate::nativeBind(), QNativeSocketEnginePrivate::nativeConnect(), QNativeSocketEnginePrivate::nativeSendDatagram(), QNativeSocketEnginePrivate::nativeSetMulticastInterface(), operator<<(), postProcess(), qt_socket_setPortAndAddress(), qt_socks5_set_host_address_and_port(), QFtpPI::startNextCmd(), toString(), and translateWSAError().
Q_IPV6ADDR QHostAddress::toIPv6Address | ( | ) | const |
Returns the IPv6 address as a Q_IPV6ADDR structure.
The structure consists of 16 unsigned characters.
This value is only valid if the protocol() is QAbstractSocket::IPv6Protocol.
Definition at line 730 of file qhostaddress.cpp.
Referenced by QHostInfoAgent::fromName(), multicastMembershipHelper(), QNativeSocketEnginePrivate::nativeBind(), QNativeSocketEnginePrivate::nativeConnect(), QNativeSocketEnginePrivate::nativeSendDatagram(), operator<<(), qt_socket_setPortAndAddress(), qt_socks5_set_host_address_and_port(), and translateWSAError().
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.
Definition at line 745 of file qhostaddress.cpp.
Referenced by QAbstractSocketPrivate::_q_connectToNextAddress(), QAbstractSocket::connectToHost(), QUdpSocketPrivate::doEnsureInitialized(), QAbstractSocketPrivate::fetchConnectionParameters(), QNativeSocketEnginePrivate::fetchConnectionParameters(), QHostInfoAgent::fromName(), QTcpServer::listen(), multicastMembershipHelper(), QNativeSocketEnginePrivate::nativeBind(), QNativeSocketEnginePrivate::nativeConnect(), QNativeSocketEnginePrivate::nativeReceiveDatagram(), QNativeSocketEnginePrivate::nativeSendDatagram(), operator<<(), QFtpPI::processReply(), qHash(), QFtpPI::startNextCmd(), translateWSAError(), and QUdpSocket::writeDatagram().
|
related |
Writes host address address to the stream out and returns a reference to the stream.
Definition at line 1176 of file qhostaddress.cpp.
|
related |
Reads a host address into address from the stream in and returns a reference to the stream.
Definition at line 1209 of file qhostaddress.cpp.
|
protected |
Definition at line 134 of file qhostaddress.h.
Referenced by clear(), isInSubnet(), isNull(), operator=(), operator==(), parseSubnet(), protocol(), QHostAddress(), scopeId(), setAddress(), setScopeId(), toIPv4Address(), toIPv6Address(), and toString().