qhostaddress.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Copyright (C) 2016 Intel Corporation.
  5. ** Contact: https://www.qt.io/licensing/
  6. **
  7. ** This file is part of the QtNetwork module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** Commercial License Usage
  11. ** Licensees holding valid commercial Qt licenses may use this file in
  12. ** accordance with the commercial license agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and The Qt Company. For licensing terms
  15. ** and conditions see https://www.qt.io/terms-conditions. For further
  16. ** information use the contact form at https://www.qt.io/contact-us.
  17. **
  18. ** GNU Lesser General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU Lesser
  20. ** General Public License version 3 as published by the Free Software
  21. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU Lesser General Public License version 3 requirements
  24. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  25. **
  26. ** GNU General Public License Usage
  27. ** Alternatively, this file may be used under the terms of the GNU
  28. ** General Public License version 2.0 or (at your option) the GNU General
  29. ** Public license version 3 or any later version approved by the KDE Free
  30. ** Qt Foundation. The licenses are as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  32. ** included in the packaging of this file. Please review the following
  33. ** information to ensure the GNU General Public License requirements will
  34. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  35. ** https://www.gnu.org/licenses/gpl-3.0.html.
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. #ifndef QHOSTADDRESS_H
  41. #define QHOSTADDRESS_H
  42. #include <QtCore/qpair.h>
  43. #include <QtCore/qstring.h>
  44. #include <QtCore/qscopedpointer.h>
  45. #include <QtNetwork/qabstractsocket.h>
  46. struct sockaddr;
  47. QT_BEGIN_NAMESPACE
  48. class QHostAddressPrivate;
  49. class Q_NETWORK_EXPORT QIPv6Address
  50. {
  51. public:
  52. inline quint8 &operator [](int index) { return c[index]; }
  53. inline quint8 operator [](int index) const { return c[index]; }
  54. quint8 c[16];
  55. };
  56. typedef QIPv6Address Q_IPV6ADDR;
  57. class QHostAddress;
  58. // qHash is a friend, but we can't use default arguments for friends (§8.3.6.4)
  59. Q_NETWORK_EXPORT uint qHash(const QHostAddress &key, uint seed = 0);
  60. class Q_NETWORK_EXPORT QHostAddress
  61. {
  62. public:
  63. enum SpecialAddress {
  64. Null,
  65. Broadcast,
  66. LocalHost,
  67. LocalHostIPv6,
  68. Any,
  69. AnyIPv6,
  70. AnyIPv4
  71. };
  72. QHostAddress();
  73. explicit QHostAddress(quint32 ip4Addr);
  74. explicit QHostAddress(quint8 *ip6Addr); // ### Qt 6: remove me
  75. explicit QHostAddress(const quint8 *ip6Addr);
  76. explicit QHostAddress(const Q_IPV6ADDR &ip6Addr);
  77. explicit QHostAddress(const sockaddr *address);
  78. explicit QHostAddress(const QString &address);
  79. QHostAddress(const QHostAddress &copy);
  80. QHostAddress(SpecialAddress address);
  81. ~QHostAddress();
  82. #ifdef Q_COMPILER_RVALUE_REFS
  83. QHostAddress &operator=(QHostAddress &&other) Q_DECL_NOTHROW
  84. { swap(other); return *this; }
  85. #endif
  86. QHostAddress &operator=(const QHostAddress &other);
  87. QHostAddress &operator=(const QString &address);
  88. void swap(QHostAddress &other) Q_DECL_NOTHROW { d.swap(other.d); }
  89. void setAddress(quint32 ip4Addr);
  90. void setAddress(quint8 *ip6Addr); // ### Qt 6: remove me
  91. void setAddress(const quint8 *ip6Addr);
  92. void setAddress(const Q_IPV6ADDR &ip6Addr);
  93. void setAddress(const sockaddr *address);
  94. bool setAddress(const QString &address);
  95. QAbstractSocket::NetworkLayerProtocol protocol() const;
  96. quint32 toIPv4Address() const; // ### Qt6: merge with next overload
  97. quint32 toIPv4Address(bool *ok) const;
  98. Q_IPV6ADDR toIPv6Address() const;
  99. QString toString() const;
  100. QString scopeId() const;
  101. void setScopeId(const QString &id);
  102. bool operator ==(const QHostAddress &address) const;
  103. bool operator ==(SpecialAddress address) const;
  104. inline bool operator !=(const QHostAddress &address) const
  105. { return !operator==(address); }
  106. inline bool operator !=(SpecialAddress address) const
  107. { return !operator==(address); }
  108. bool isNull() const;
  109. void clear();
  110. bool isInSubnet(const QHostAddress &subnet, int netmask) const;
  111. bool isInSubnet(const QPair<QHostAddress, int> &subnet) const;
  112. bool isLoopback() const;
  113. bool isMulticast() const;
  114. static QPair<QHostAddress, int> parseSubnet(const QString &subnet);
  115. friend Q_NETWORK_EXPORT uint qHash(const QHostAddress &key, uint seed);
  116. protected:
  117. QScopedPointer<QHostAddressPrivate> d;
  118. };
  119. Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QHostAddress)
  120. inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddress &address2)
  121. { return address2 == address1; }
  122. #ifndef QT_NO_DEBUG_STREAM
  123. Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &);
  124. #endif
  125. #ifndef QT_NO_DATASTREAM
  126. Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &);
  127. Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &);
  128. #endif
  129. QT_END_NAMESPACE
  130. #endif // QHOSTADDRESS_H