QHostAddress review

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-30 04:11:15 +02:00
parent 10ee130424
commit bec40b8120
2 changed files with 75 additions and 81 deletions

View file

@ -95,18 +95,6 @@ QHostAddress::QHostAddress()
{
}
/*!
Constructs an IPv4 or IPv6 address based on the string \a address
(e.g., "127.0.0.1").
\sa setAddress()
*/
QHostAddress::QHostAddress(const QByteArray &address)
: d(new QHostAddressPrivate())
{
setAddress(address);
}
/*!
Constructs an IPv4 or IPv6 address using the address specified by
the native structure \a sockaddr.
@ -119,6 +107,18 @@ QHostAddress::QHostAddress(const struct sockaddr *sockaddr)
setAddress(sockaddr);
}
/*!
Constructs an IPv4 or IPv6 address based on the string \a address
(e.g., "127.0.0.1").
\sa setAddress()
*/
QHostAddress::QHostAddress(const QByteArray &address)
: d(new QHostAddressPrivate())
{
setAddress(address);
}
/*!
Constructs a copy of the given \a address.
*/
@ -195,12 +195,14 @@ QHostAddress &QHostAddress::operator=(const QByteArray &address)
}
/*!
\fn bool QHostAddress::operator!=(const QHostAddress &other) const
\since 4.2
Returns true if this host address is not the same as the \a other
address given; otherwise returns false.
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.
*/
bool QHostAddress::isNull() const
{
return (d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol);
}
/*!
Sets the host address to 0.0.0.0.
@ -212,49 +214,6 @@ void QHostAddress::clear()
d->scopeId.clear();
}
/*!
\overload
Sets the IPv4 or IPv6 address specified by the string
representation specified by \a address (e.g. "127.0.0.1").
Returns true and sets the address if the address was successfully
parsed; otherwise returns false.
*/
bool QHostAddress::setAddress(const QByteArray &address)
{
// compat
QByteArray scope;
QByteArray fixedaddress(address.trimmed());
if (fixedaddress.contains(':')) {
const int indexofpercent = fixedaddress.indexOf(addrscopeseparator);
if (indexofpercent >= 0) {
scope = fixedaddress.mid(indexofpercent + 1, fixedaddress.size() - indexofpercent - 1);
fixedaddress = fixedaddress.mid(0, indexofpercent);
}
}
int result = 0;
#ifndef QT_NO_IPV6
struct in6_addr inaddripv6;
result = inet_pton(AF_INET6, fixedaddress.constData(), &inaddripv6);
if (result == 1) {
d->protocol = QAbstractSocket::IPv6Protocol;
d->ipString = fixedaddress;
d->scopeId = scope;
return true;
}
#endif
struct in_addr inaddripv4;
result = inet_pton(AF_INET, fixedaddress.constData(), &inaddripv4);
if (result == 1) {
d->protocol = QAbstractSocket::IPv4Protocol;
d->ipString = fixedaddress;
d->scopeId.clear();
return true;
}
clear();
return false;
}
/*!
Sets the IPv4 or IPv6 address specified by the native structure \a
sockaddr. Returns true and sets the address if the address was
@ -303,6 +262,49 @@ bool QHostAddress::setAddress(const struct sockaddr *sockaddr)
return false;
}
/*!
\overload
Sets the IPv4 or IPv6 address specified by the string
representation specified by \a address (e.g. "127.0.0.1").
Returns true and sets the address if the address was successfully
parsed; otherwise returns false.
*/
bool QHostAddress::setAddress(const QByteArray &address)
{
// compat
QByteArray scope;
QByteArray fixedaddress(address.trimmed());
if (fixedaddress.contains(':')) {
const int indexofpercent = fixedaddress.indexOf(addrscopeseparator);
if (indexofpercent >= 0) {
scope = fixedaddress.mid(indexofpercent + 1, fixedaddress.size() - indexofpercent - 1);
fixedaddress = fixedaddress.mid(0, indexofpercent);
}
}
int result = 0;
#ifndef QT_NO_IPV6
struct in6_addr inaddripv6;
result = inet_pton(AF_INET6, fixedaddress.constData(), &inaddripv6);
if (result == 1) {
d->protocol = QAbstractSocket::IPv6Protocol;
d->ipString = fixedaddress;
d->scopeId = scope;
return true;
}
#endif
struct in_addr inaddripv4;
result = inet_pton(AF_INET, fixedaddress.constData(), &inaddripv4);
if (result == 1) {
d->protocol = QAbstractSocket::IPv4Protocol;
d->ipString = fixedaddress;
d->scopeId.clear();
return true;
}
clear();
return false;
}
/*!
Returns the network layer protocol of the host address.
*/
@ -404,14 +406,12 @@ bool QHostAddress::operator==(const QHostAddress &other) 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.
\fn bool QHostAddress::operator!=(const QHostAddress &other) const
\since 4.2
Returns true if this host address is not the same as the \a other
address given; otherwise returns false.
*/
bool QHostAddress::isNull() const
{
return (d->protocol == QAbstractSocket::UnknownNetworkLayerProtocol);
}
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QHostAddress &address)
@ -460,23 +460,18 @@ QDataStream &operator<<(QDataStream &out, const QHostAddress &address)
*/
QDataStream &operator>>(QDataStream &in, QHostAddress &address)
{
qint8 prot;
qint8 prot = QAbstractSocket::UnknownNetworkLayerProtocol;
in >> prot;
switch (QAbstractSocket::NetworkLayerProtocol(prot)) {
case QAbstractSocket::UnknownNetworkLayerProtocol: {
address.clear();
break;
}
case QAbstractSocket::IPv4Protocol: {
QByteArray ipv4;
in >> ipv4;
address.setAddress(ipv4);
break;
}
case QAbstractSocket::IPv4Protocol:
case QAbstractSocket::IPv6Protocol: {
QByteArray ipv6;
in >> ipv6;
address.setAddress(ipv6);
QByteArray ip;
in >> ip;
address.setAddress(ip);
break;
}
default: {
@ -489,5 +484,3 @@ QDataStream &operator>>(QDataStream &in, QHostAddress &address)
#endif //QT_NO_DATASTREAM
QT_END_NAMESPACE

View file

@ -61,6 +61,9 @@ public:
QHostAddress &operator=(const QHostAddress &other);
QHostAddress &operator=(const QByteArray &address);
bool isNull() const;
void clear();
bool setAddress(const sockaddr *sockaddr);
bool setAddress(const QByteArray &address);
@ -74,8 +77,6 @@ public:
bool operator ==(const QHostAddress &address) const;
inline bool operator !=(const QHostAddress &address) const
{ return !operator==(address); }
bool isNull() const;
void clear();
private:
QScopedPointer<QHostAddressPrivate> d;