diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2014-12-24 15:43:08 -0200 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2015-03-06 07:53:59 +0000 |
commit | 775d04f97ec1723c01c12f8faab05236686b9b05 (patch) | |
tree | b1481664428dd47abe58b0c9763d137d70a9f6b9 /src/network/kernel | |
parent | ab8d36d6f36119dc9ef211acc391d8c2bfb5ee83 (diff) | |
download | qtbase-775d04f97ec1723c01c12f8faab05236686b9b05.tar.gz |
Add a QHostAddress::toIPv4Address overload taking a bool *ok
This allows one to check whether the conversion is successful without
checking for the return result, as the value of 0 represents the valid
IPv4 address 0.0.0.0.
Change-Id: I637fe55583f2255c85b0d955e5886b61494e0c7c
Reviewed-by: Richard J. Moore <rich@kde.org>
Diffstat (limited to 'src/network/kernel')
-rw-r--r-- | src/network/kernel/qhostaddress.cpp | 24 | ||||
-rw-r--r-- | src/network/kernel/qhostaddress.h | 3 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index 8d32711de3..9a993392e9 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -634,7 +634,31 @@ void QHostAddress::setAddress(const struct sockaddr *sockaddr) */ quint32 QHostAddress::toIPv4Address() const { + return toIPv4Address(Q_NULLPTR); +} + +/*! + 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, \a ok will be set to true. Otherwise, it will be set to false. + + \sa toString() +*/ +quint32 QHostAddress::toIPv4Address(bool *ok) const +{ QT_ENSURE_PARSED(this); + quint32 dummy; + if (ok) + *ok = d->protocol == QAbstractSocket::IPv4Protocol || d->protocol == QAbstractSocket::AnyIPProtocol + || (d->protocol == QAbstractSocket::IPv6Protocol && convertToIpv4(dummy, d->a6)); return d->a; } diff --git a/src/network/kernel/qhostaddress.h b/src/network/kernel/qhostaddress.h index 93d64ff54a..de3a79278e 100644 --- a/src/network/kernel/qhostaddress.h +++ b/src/network/kernel/qhostaddress.h @@ -93,7 +93,8 @@ public: bool setAddress(const QString &address); QAbstractSocket::NetworkLayerProtocol protocol() const; - quint32 toIPv4Address() const; + quint32 toIPv4Address() const; // ### Qt6: merge with next overload + quint32 toIPv4Address(bool *ok) const; Q_IPV6ADDR toIPv6Address() const; QString toString() const; |