Qt 4.8
Public Types | Public Functions | Static Public Functions | Protected Variables | Related Functions | List of all members
QHostAddress Class Reference

The QHostAddress class provides an IP address. More...

#include <qhostaddress.h>

Inheritance diagram for QHostAddress:
QNetmaskAddress

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...
 
QHostAddressoperator= (const QHostAddress &other)
 Assigns another host address to this object, and returns a reference to this object. More...
 
QHostAddressoperator= (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 &copy)
 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< QHostAddressPrivated
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &out, const QHostAddress &address)
 Writes host address address to the stream out and returns a reference to the stream. More...
 
QDataStreamoperator>> (QDataStream &in, QHostAddress &address)
 Reads a host address into address from the stream in and returns a reference to the stream. More...
 

Detailed Description

The QHostAddress class provides an IP address.

Attention
Module: 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().

Note
Please note that QHostAddress does not do DNS lookups. QHostInfo is needed for that.

The class also supports common predefined addresses: Null , LocalHost , LocalHostIPv6 , Broadcast , and Any .

See also
QHostInfo, QTcpSocket, QTcpServer, QUdpSocket

Definition at line 70 of file qhostaddress.h.

Enumerations

◆ SpecialAddress

  • Null The null address object. Equivalent to QHostAddress().
  • LocalHost The IPv4 localhost address. Equivalent to QHostAddress("127.0.0.1").
  • LocalHostIPv6 The IPv6 localhost address. Equivalent to QHostAddress("::1").
  • Broadcast The IPv4 broadcast address. Equivalent to QHostAddress("255.255.255.255").
  • Any The IPv4 any-address. Equivalent to QHostAddress("0.0.0.0").
  • AnyIPv6 The IPv6 any-address. Equivalent to QHostAddress("::").
Enumerator
Null 
Broadcast 
LocalHost 
LocalHostIPv6 
Any 
AnyIPv6 

Definition at line 73 of file qhostaddress.h.

Constructors and Destructors

◆ QHostAddress() [1/8]

QHostAddress::QHostAddress ( )

Constructs a host address object with the IP address 0.0.0.0.

See also
clear()

Definition at line 460 of file qhostaddress.cpp.

Referenced by parseSubnet(), and QHostAddress().

461  : d(new QHostAddressPrivate)
462 {
463 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134

◆ QHostAddress() [2/8]

QHostAddress::QHostAddress ( quint32  ip4Addr)
explicit

Constructs a host address object with the IPv4 address ip4Addr.

Definition at line 468 of file qhostaddress.cpp.

469  : d(new QHostAddressPrivate)
470 {
471  setAddress(ip4Addr);
472 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.

◆ QHostAddress() [3/8]

QHostAddress::QHostAddress ( quint8 ip6Addr)
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.

481  : d(new QHostAddressPrivate)
482 {
483  setAddress(ip6Addr);
484 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.

◆ QHostAddress() [4/8]

QHostAddress::QHostAddress ( const Q_IPV6ADDR ip6Addr)
explicit

Constructs a host address object with the IPv6 address ip6Addr.

Definition at line 489 of file qhostaddress.cpp.

490  : d(new QHostAddressPrivate)
491 {
492  setAddress(ip6Addr);
493 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.

◆ QHostAddress() [5/8]

QHostAddress::QHostAddress ( const sockaddr *  sockaddr)
explicit

Constructs an IPv4 or IPv6 address using the address specified by the native structure sockaddr.

See also
setAddress()

◆ QHostAddress() [6/8]

QHostAddress::QHostAddress ( const QString address)
explicit

Constructs an IPv4 or IPv6 address based on the string address (e.g., "127.0.0.1").

See also
setAddress()

Definition at line 501 of file qhostaddress.cpp.

502  : d(new QHostAddressPrivate)
503 {
504  d->ipString = address;
505  d->isParsed = false;
506 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134

◆ QHostAddress() [7/8]

QHostAddress::QHostAddress ( const QHostAddress copy)

Constructs a copy of the given address.

Definition at line 530 of file qhostaddress.cpp.

531  : d(new QHostAddressPrivate(*address.d.data()))
532 {
533 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134

◆ QHostAddress() [8/8]

QHostAddress::QHostAddress ( SpecialAddress  address)

Constructs a QHostAddress object for address.

Definition at line 538 of file qhostaddress.cpp.

539  : d(new QHostAddressPrivate)
540 {
541  switch (address) {
542  case Null:
543  break;
544  case Broadcast:
545  setAddress(QLatin1String("255.255.255.255"));
546  break;
547  case LocalHost:
548  setAddress(QLatin1String("127.0.0.1"));
549  break;
550  case LocalHostIPv6:
551  setAddress(QLatin1String("::1"));
552  break;
553  case Any:
554  setAddress(QLatin1String("0.0.0.0"));
555  break;
556  case AnyIPv6:
557  setAddress(QLatin1String("::"));
558  break;
559  }
560 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.

◆ ~QHostAddress()

QHostAddress::~QHostAddress ( )

Destroys the host address object.

Definition at line 565 of file qhostaddress.cpp.

566 {
567 }

Functions

◆ clear()

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().

616 {
617  d->clear();
618 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134

◆ isInSubnet() [1/2]

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.

Since
4.5

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

See also
parseSubnet()

Definition at line 968 of file qhostaddress.cpp.

Referenced by isBypassed(), isHostExcluded(), Maemo::ProxyConfPrivate::isHostExcluded(), and isInSubnet().

969 {
970  QT_ENSURE_PARSED(this);
971  if (subnet.protocol() != d->protocol || netmask < 0)
972  return false;
973 
974  union {
975  quint32 ip;
976  quint8 data[4];
977  } ip4, net4;
978  const quint8 *ip;
979  const quint8 *net;
981  if (netmask > 32)
982  netmask = 32;
983  ip4.ip = qToBigEndian(d->a);
984  net4.ip = qToBigEndian(subnet.d->a);
985  ip = ip4.data;
986  net = net4.data;
987  } else if (d->protocol == QAbstractSocket::IPv6Protocol) {
988  if (netmask > 128)
989  netmask = 128;
990  ip = d->a6.c;
991  net = subnet.d->a6.c;
992  } else {
993  return false;
994  }
995 
996  if (netmask >= 8 && memcmp(ip, net, netmask / 8) != 0)
997  return false;
998  if ((netmask & 7) == 0)
999  return true;
1000 
1001  // compare the last octet now
1002  quint8 bytemask = 256 - (1 << (8 - (netmask & 7)));
1003  quint8 ipbyte = ip[netmask / 8];
1004  quint8 netbyte = net[netmask / 8];
1005  return (ipbyte & bytemask) == (netbyte & bytemask);
1006 }
quint8 c[16]
Definition: qhostaddress.h:65
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QAbstractSocket::NetworkLayerProtocol protocol
unsigned char quint8
Definition: qglobal.h:934
T qToBigEndian(T source)
Definition: qendian.h:337
static const char * data(const QByteArray &arr)
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
#define QT_ENSURE_PARSED(a)
unsigned int quint32
Definition: qglobal.h:938

◆ isInSubnet() [2/2]

bool QHostAddress::isInSubnet ( const QPair< QHostAddress, int > &  subnet) const

Returns true if this IP is in the subnet described by subnet.

Since
4.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

1021 {
1022  return isInSubnet(subnet.first, subnet.second);
1023 }
T1 first
Definition: qpair.h:65
T2 second
Definition: qpair.h:66
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...

◆ isNull()

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<<().

905 {
906  QT_ENSURE_PARSED(this);
908 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QAbstractSocket::NetworkLayerProtocol protocol
#define QT_ENSURE_PARSED(a)

◆ operator!=() [1/2]

bool QHostAddress::operator!= ( const QHostAddress other) const
inline

Returns true if this host address is not the same as the other address given; otherwise returns false.

Since
4.2

Definition at line 112 of file qhostaddress.h.

113  { return !operator==(address); }
bool operator==(const QHostAddress &address) const
Returns true if this host address is the same as the other address given; otherwise returns false...

◆ operator!=() [2/2]

bool QHostAddress::operator!= ( SpecialAddress  address) const
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.

115  { return !operator==(address); }
bool operator==(const QHostAddress &address) const
Returns true if this host address is the same as the other address given; otherwise returns false...

◆ operator=() [1/2]

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().

574 {
575  *d.data() = *address.d.data();
576  return *this;
577 }
T * data() const
Returns the value of the pointer referenced by this object.
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134

◆ operator=() [2/2]

QHostAddress & QHostAddress::operator= ( const QString address)

Assigns the host address address to this object, and returns a reference to this object.

See also
setAddress()

Definition at line 585 of file qhostaddress.cpp.

586 {
587  setAddress(address);
588  return *this;
589 }
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.

◆ operator==() [1/2]

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.

867 {
868  QT_ENSURE_PARSED(this);
869  QT_ENSURE_PARSED(&other);
870 
872  return other.d->protocol == QAbstractSocket::IPv4Protocol && d->a == other.d->a;
874  return other.d->protocol == QAbstractSocket::IPv6Protocol
875  && memcmp(&d->a6, &other.d->a6, sizeof(Q_IPV6ADDR)) == 0;
876  }
877  return d->protocol == other.d->protocol;
878 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QAbstractSocket::NetworkLayerProtocol protocol
#define QT_ENSURE_PARSED(a)

◆ operator==() [2/2]

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.

885 {
886  QT_ENSURE_PARSED(this);
887  QHostAddress otherAddress(other);
888  QT_ENSURE_PARSED(&otherAddress);
889 
891  return otherAddress.d->protocol == QAbstractSocket::IPv4Protocol && d->a == otherAddress.d->a;
893  return otherAddress.d->protocol == QAbstractSocket::IPv6Protocol
894  && memcmp(&d->a6, &otherAddress.d->a6, sizeof(Q_IPV6ADDR)) == 0;
895  }
896  return int(other) == int(Null);
897 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QAbstractSocket::NetworkLayerProtocol protocol
#define QT_ENSURE_PARSED(a)
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70

◆ parseSubnet()

QPair< QHostAddress, int > QHostAddress::parseSubnet ( const QString subnet)
static

Parses the IP and subnet information contained in subnet and returns the network prefix for that network and its prefix length.

Since
4.5

The IP address and the netmask must be separated by a slash (/).

This function supports arguments in the form:

  • 123.123.123.123/n where n is any value between 0 and 32
  • 123.123.123.123/255.255.255.255
  • <ipv6-address>/n where n is any value between 0 and 128

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

See also
isInSubnet()

Definition at line 1053 of file qhostaddress.cpp.

Referenced by isBypassed(), isHostExcluded(), and Maemo::ProxyConfPrivate::isHostExcluded().

1054 {
1055  // We support subnets in the form:
1056  // ddd.ddd.ddd.ddd/nn
1057  // ddd.ddd.ddd/nn
1058  // ddd.ddd/nn
1059  // ddd/nn
1060  // ddd.ddd.ddd.
1061  // ddd.ddd.ddd
1062  // ddd.ddd.
1063  // ddd.ddd
1064  // ddd.
1065  // ddd
1066  // <ipv6-address>/nn
1067  //
1068  // where nn can be an IPv4-style netmask for the IPv4 forms
1069 
1070  const QPair<QHostAddress, int> invalid = qMakePair(QHostAddress(), -1);
1071  if (subnet.isEmpty())
1072  return invalid;
1073 
1074  int slash = subnet.indexOf(QLatin1Char('/'));
1075  QString netStr = subnet;
1076  if (slash != -1)
1077  netStr.truncate(slash);
1078 
1079  int netmask = -1;
1080  bool isIpv6 = netStr.contains(QLatin1Char(':'));
1081 
1082  if (slash != -1) {
1083  // is the netmask given in IP-form or in bit-count form?
1084  if (!isIpv6 && subnet.indexOf(QLatin1Char('.'), slash + 1) != -1) {
1085  // IP-style, convert it to bit-count form
1086  QNetmaskAddress parser;
1087  if (!parser.setAddress(subnet.mid(slash + 1)))
1088  return invalid;
1089  netmask = parser.prefixLength();
1090  } else {
1091  bool ok;
1092  netmask = subnet.mid(slash + 1).toUInt(&ok);
1093  if (!ok)
1094  return invalid; // failed to parse the subnet
1095  }
1096  }
1097 
1098  if (isIpv6) {
1099  // looks like it's an IPv6 address
1100  if (netmask > 128)
1101  return invalid; // invalid netmask
1102  if (netmask < 0)
1103  netmask = 128;
1104 
1105  QHostAddress net;
1106  if (!net.setAddress(netStr))
1107  return invalid; // failed to parse the IP
1108 
1109  clearBits(net.d->a6.c, netmask, 128);
1110  return qMakePair(net, netmask);
1111  }
1112 
1113  if (netmask > 32)
1114  return invalid; // invalid netmask
1115 
1116  // parse the address manually
1117  QStringList parts = netStr.split(QLatin1Char('.'));
1118  if (parts.isEmpty() || parts.count() > 4)
1119  return invalid; // invalid IPv4 address
1120 
1121  if (parts.last().isEmpty())
1122  parts.removeLast();
1123 
1124  quint32 addr = 0;
1125  for (int i = 0; i < parts.count(); ++i) {
1126  bool ok;
1127  uint byteValue = parts.at(i).toUInt(&ok);
1128  if (!ok || byteValue > 255)
1129  return invalid; // invalid IPv4 address
1130 
1131  addr <<= 8;
1132  addr += byteValue;
1133  }
1134  addr <<= 8 * (4 - parts.count());
1135  if (netmask == -1) {
1136  netmask = 8 * parts.count();
1137  } else if (netmask == 0) {
1138  // special case here
1139  // x86's instructions "shr" and "shl" do not operate when
1140  // their argument is 32, so the code below doesn't work as expected
1141  addr = 0;
1142  } else if (netmask != 32) {
1143  // clear remaining bits
1144  quint32 mask = quint32(0xffffffff) >> (32 - netmask) << (32 - netmask);
1145  addr &= mask;
1146  }
1147 
1148  return qMakePair(QHostAddress(addr), netmask);
1149 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
quint8 c[16]
Definition: qhostaddress.h:65
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
void removeLast()
Removes the last item in the list.
Definition: qlist.h:287
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
unsigned int uint
Definition: qglobal.h:996
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QHostAddress()
Constructs a host address object with the IP address 0.0.0.0.
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
unsigned int quint32
Definition: qglobal.h:938
uint toUInt(bool *ok=0, int base=10) const
Returns the string converted to an unsigned int using base base, which is 10 by default and must be b...
Definition: qstring.cpp:6120
static void clearBits(quint8 *where, int start, int end)
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
int prefixLength() const
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool setAddress(const QString &address)

◆ protocol()

QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol ( ) const

◆ scopeId()

QString QHostAddress::scopeId ( ) const

Returns the scope ID of an IPv6 address.

Since
4.1

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").

See also
setScopeId()

Definition at line 840 of file qhostaddress.cpp.

Referenced by QNativeSocketEnginePrivate::nativeBind(), QNativeSocketEnginePrivate::nativeConnect(), QNativeSocketEnginePrivate::nativeSendDatagram(), operator<<(), and qt_socket_setPortAndAddress().

841 {
842  QT_ENSURE_PARSED(this);
844 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QAbstractSocket::NetworkLayerProtocol protocol
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define QT_ENSURE_PARSED(a)

◆ setAddress() [1/5]

void QHostAddress::setAddress ( quint32  ip4Addr)

◆ setAddress() [2/5]

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.

640 {
641  d->setAddress(ip6Addr);
642 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
void setAddress(quint32 a_=0)

◆ setAddress() [3/5]

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.

653 {
654  d->setAddress(ip6Addr);
655 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
void setAddress(quint32 a_=0)

◆ setAddress() [4/5]

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.

◆ setAddress() [5/5]

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.

669 {
670  d->ipString = address;
671  return d->parse();
672 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134

◆ setScopeId()

void QHostAddress::setScopeId ( const QString id)

Sets the IPv6 scope ID of the address to id.

Since
4.1

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().

856 {
857  QT_ENSURE_PARSED(this);
859  d->scopeId = id;
860 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QAbstractSocket::NetworkLayerProtocol protocol
#define QT_ENSURE_PARSED(a)

◆ toIPv4Address()

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.

See also
toString()

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().

705 {
706  QT_ENSURE_PARSED(this);
707  return d->a;
708 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
#define QT_ENSURE_PARSED(a)

◆ toIPv6Address()

Q_IPV6ADDR QHostAddress::toIPv6Address ( ) const

Returns the IPv6 address as a Q_IPV6ADDR structure.

The structure consists of 16 unsigned characters.

Q_IPV6ADDR addr = hostAddr.toIPv6Address();
// addr contains 16 unsigned characters
for (int i = 0; i < 16; ++i) {
// process addr[i]
}

This value is only valid if the protocol() is QAbstractSocket::IPv6Protocol.

See also
toString()

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().

731 {
732  QT_ENSURE_PARSED(this);
733  return d->a6;
734 }
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
#define QT_ENSURE_PARSED(a)

◆ toString()

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.

See also
toIPv4Address()

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().

746 {
747  QT_ENSURE_PARSED(this);
749  quint32 i = toIPv4Address();
750  QString s;
751  s.sprintf("%d.%d.%d.%d", (i>>24) & 0xff, (i>>16) & 0xff,
752  (i >> 8) & 0xff, i & 0xff);
753  return s;
754  }
755 
757  quint16 ugle[8];
758  for (int i = 0; i < 8; i++) {
759  ugle[i] = (quint16(d->a6[2*i]) << 8) | quint16(d->a6[2*i+1]);
760  }
761  QString s;
762  QString temp;
763  bool zeroDetected = false;
764  bool zeroShortened = false;
765  for (int i = 0; i < 8; i++) {
766  if ((ugle[i] != 0) || zeroShortened) {
767  temp.sprintf("%X", ugle[i]);
768  s.append(temp);
769  if (zeroDetected)
770  zeroShortened = true;
771  } else {
772  if (!zeroDetected) {
773  if (i<7 && (ugle[i+1] == 0)) {
774  s.append(QLatin1Char(':'));
775  zeroDetected = true;
776  } else {
777  temp.sprintf("%X", ugle[i]);
778  s.append(temp);
779  if (i<7)
780  s.append(QLatin1Char(':'));
781  }
782  }
783  }
784  if (i<7 && ((ugle[i] != 0) || zeroShortened || (i==0 && zeroDetected)))
785  s.append(QLatin1Char(':'));
786  }
787 
788  if (!d->scopeId.isEmpty())
789  s.append(QLatin1Char('%') + d->scopeId);
790  return s;
791  }
792 
793  return QString();
794 }
QString & sprintf(const char *format,...)
Safely builds a formatted string from the format string cformat and an arbitrary list of arguments...
Definition: qstring.cpp:5567
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
QAbstractSocket::NetworkLayerProtocol protocol
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
unsigned short quint16
Definition: qglobal.h:936
QString & append(QChar c)
Definition: qstring.cpp:1777
#define QT_ENSURE_PARSED(a)
unsigned int quint32
Definition: qglobal.h:938
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

Friends and Related Functions

◆ operator<<()

QDataStream & operator<< ( QDataStream out,
const QHostAddress address 
)
related

Writes host address address to the stream out and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 1176 of file qhostaddress.cpp.

1177 {
1178  qint8 prot;
1179  prot = qint8(address.protocol());
1180  out << prot;
1181  switch (address.protocol()) {
1183  break;
1185  out << address.toIPv4Address();
1186  break;
1188  {
1189  Q_IPV6ADDR ipv6 = address.toIPv6Address();
1190  for (int i = 0; i < 16; ++i)
1191  out << ipv6[i];
1192  out << address.scopeId();
1193  }
1194  break;
1195  }
1196  return out;
1197 }
QString scopeId() const
Returns the scope ID of an IPv6 address.
Q_IPV6ADDR toIPv6Address() const
Returns the IPv6 address as a Q_IPV6ADDR structure.
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
signed char qint8
Definition: qglobal.h:933
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.

◆ operator>>()

QDataStream & operator>> ( QDataStream in,
QHostAddress address 
)
related

Reads a host address into address from the stream in and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 1209 of file qhostaddress.cpp.

1210 {
1211  qint8 prot;
1212  in >> prot;
1213  switch (QAbstractSocket::NetworkLayerProtocol(prot)) {
1215  address.clear();
1216  break;
1218  {
1219  quint32 ipv4;
1220  in >> ipv4;
1221  address.setAddress(ipv4);
1222  }
1223  break;
1225  {
1226  Q_IPV6ADDR ipv6;
1227  for (int i = 0; i < 16; ++i)
1228  in >> ipv6[i];
1229  address.setAddress(ipv6);
1230 
1231  QString scope;
1232  in >> scope;
1233  address.setScopeId(scope);
1234  }
1235  break;
1236  default:
1237  address.clear();
1239  }
1240  return in;
1241 }
void setScopeId(const QString &id)
Sets the IPv6 scope ID of the address to id.
void setStatus(Status status)
Sets the status of the data stream to the status given.
NetworkLayerProtocol
This enum describes the network layer protocol values used in Qt.
The QString class provides a Unicode character string.
Definition: qstring.h:83
void clear()
Sets the host address to 0.0.0.0.
signed char qint8
Definition: qglobal.h:933
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
unsigned int quint32
Definition: qglobal.h:938

Properties

◆ d

QScopedPointer<QHostAddressPrivate> QHostAddress::d
protected

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