address.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // ip/address.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_IP_ADDRESS_HPP
  11. #define BOOST_ASIO_IP_ADDRESS_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <functional>
  17. #include <string>
  18. #include <boost/asio/detail/throw_exception.hpp>
  19. #include <boost/asio/detail/string_view.hpp>
  20. #include <boost/asio/detail/type_traits.hpp>
  21. #include <boost/system/error_code.hpp>
  22. #include <boost/asio/ip/address_v4.hpp>
  23. #include <boost/asio/ip/address_v6.hpp>
  24. #include <boost/asio/ip/bad_address_cast.hpp>
  25. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  26. # include <iosfwd>
  27. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. namespace ip {
  32. /// Implements version-independent IP addresses.
  33. /**
  34. * The boost::asio::ip::address class provides the ability to use either IP
  35. * version 4 or version 6 addresses.
  36. *
  37. * @par Thread Safety
  38. * @e Distinct @e objects: Safe.@n
  39. * @e Shared @e objects: Unsafe.
  40. */
  41. class address
  42. {
  43. public:
  44. /// Default constructor.
  45. BOOST_ASIO_DECL address() noexcept;
  46. /// Construct an address from an IPv4 address.
  47. BOOST_ASIO_DECL address(
  48. const boost::asio::ip::address_v4& ipv4_address) noexcept;
  49. /// Construct an address from an IPv6 address.
  50. BOOST_ASIO_DECL address(
  51. const boost::asio::ip::address_v6& ipv6_address) noexcept;
  52. /// Copy constructor.
  53. BOOST_ASIO_DECL address(const address& other) noexcept;
  54. /// Move constructor.
  55. BOOST_ASIO_DECL address(address&& other) noexcept;
  56. /// Assign from another address.
  57. BOOST_ASIO_DECL address& operator=(const address& other) noexcept;
  58. /// Move-assign from another address.
  59. BOOST_ASIO_DECL address& operator=(address&& other) noexcept;
  60. /// Assign from an IPv4 address.
  61. BOOST_ASIO_DECL address& operator=(
  62. const boost::asio::ip::address_v4& ipv4_address) noexcept;
  63. /// Assign from an IPv6 address.
  64. BOOST_ASIO_DECL address& operator=(
  65. const boost::asio::ip::address_v6& ipv6_address) noexcept;
  66. /// Get whether the address is an IP version 4 address.
  67. bool is_v4() const noexcept
  68. {
  69. return type_ == ipv4;
  70. }
  71. /// Get whether the address is an IP version 6 address.
  72. bool is_v6() const noexcept
  73. {
  74. return type_ == ipv6;
  75. }
  76. /// Get the address as an IP version 4 address.
  77. BOOST_ASIO_DECL boost::asio::ip::address_v4 to_v4() const;
  78. /// Get the address as an IP version 6 address.
  79. BOOST_ASIO_DECL boost::asio::ip::address_v6 to_v6() const;
  80. /// Get the address as a string.
  81. BOOST_ASIO_DECL std::string to_string() const;
  82. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  83. /// (Deprecated: Use other overload.) Get the address as a string.
  84. BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
  85. /// (Deprecated: Use make_address().) Create an address from an IPv4 address
  86. /// string in dotted decimal form, or from an IPv6 address in hexadecimal
  87. /// notation.
  88. static address from_string(const char* str);
  89. /// (Deprecated: Use make_address().) Create an address from an IPv4 address
  90. /// string in dotted decimal form, or from an IPv6 address in hexadecimal
  91. /// notation.
  92. static address from_string(const char* str, boost::system::error_code& ec);
  93. /// (Deprecated: Use make_address().) Create an address from an IPv4 address
  94. /// string in dotted decimal form, or from an IPv6 address in hexadecimal
  95. /// notation.
  96. static address from_string(const std::string& str);
  97. /// (Deprecated: Use make_address().) Create an address from an IPv4 address
  98. /// string in dotted decimal form, or from an IPv6 address in hexadecimal
  99. /// notation.
  100. static address from_string(
  101. const std::string& str, boost::system::error_code& ec);
  102. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  103. /// Determine whether the address is a loopback address.
  104. BOOST_ASIO_DECL bool is_loopback() const noexcept;
  105. /// Determine whether the address is unspecified.
  106. BOOST_ASIO_DECL bool is_unspecified() const noexcept;
  107. /// Determine whether the address is a multicast address.
  108. BOOST_ASIO_DECL bool is_multicast() const noexcept;
  109. /// Compare two addresses for equality.
  110. BOOST_ASIO_DECL friend bool operator==(const address& a1,
  111. const address& a2) noexcept;
  112. /// Compare two addresses for inequality.
  113. friend bool operator!=(const address& a1,
  114. const address& a2) noexcept
  115. {
  116. return !(a1 == a2);
  117. }
  118. /// Compare addresses for ordering.
  119. BOOST_ASIO_DECL friend bool operator<(const address& a1,
  120. const address& a2) noexcept;
  121. /// Compare addresses for ordering.
  122. friend bool operator>(const address& a1,
  123. const address& a2) noexcept
  124. {
  125. return a2 < a1;
  126. }
  127. /// Compare addresses for ordering.
  128. friend bool operator<=(const address& a1,
  129. const address& a2) noexcept
  130. {
  131. return !(a2 < a1);
  132. }
  133. /// Compare addresses for ordering.
  134. friend bool operator>=(const address& a1,
  135. const address& a2) noexcept
  136. {
  137. return !(a1 < a2);
  138. }
  139. private:
  140. // The type of the address.
  141. enum { ipv4, ipv6 } type_;
  142. // The underlying IPv4 address.
  143. boost::asio::ip::address_v4 ipv4_address_;
  144. // The underlying IPv6 address.
  145. boost::asio::ip::address_v6 ipv6_address_;
  146. };
  147. /// Create an address from an IPv4 address string in dotted decimal form,
  148. /// or from an IPv6 address in hexadecimal notation.
  149. /**
  150. * @relates address
  151. */
  152. BOOST_ASIO_DECL address make_address(const char* str);
  153. /// Create an address from an IPv4 address string in dotted decimal form,
  154. /// or from an IPv6 address in hexadecimal notation.
  155. /**
  156. * @relates address
  157. */
  158. BOOST_ASIO_DECL address make_address(const char* str,
  159. boost::system::error_code& ec) noexcept;
  160. /// Create an address from an IPv4 address string in dotted decimal form,
  161. /// or from an IPv6 address in hexadecimal notation.
  162. /**
  163. * @relates address
  164. */
  165. BOOST_ASIO_DECL address make_address(const std::string& str);
  166. /// Create an address from an IPv4 address string in dotted decimal form,
  167. /// or from an IPv6 address in hexadecimal notation.
  168. /**
  169. * @relates address
  170. */
  171. BOOST_ASIO_DECL address make_address(const std::string& str,
  172. boost::system::error_code& ec) noexcept;
  173. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  174. || defined(GENERATING_DOCUMENTATION)
  175. /// Create an address from an IPv4 address string in dotted decimal form,
  176. /// or from an IPv6 address in hexadecimal notation.
  177. /**
  178. * @relates address
  179. */
  180. BOOST_ASIO_DECL address make_address(string_view str);
  181. /// Create an address from an IPv4 address string in dotted decimal form,
  182. /// or from an IPv6 address in hexadecimal notation.
  183. /**
  184. * @relates address
  185. */
  186. BOOST_ASIO_DECL address make_address(string_view str,
  187. boost::system::error_code& ec) noexcept;
  188. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  189. // || defined(GENERATING_DOCUMENTATION)
  190. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  191. /// Output an address as a string.
  192. /**
  193. * Used to output a human-readable string for a specified address.
  194. *
  195. * @param os The output stream to which the string will be written.
  196. *
  197. * @param addr The address to be written.
  198. *
  199. * @return The output stream.
  200. *
  201. * @relates boost::asio::ip::address
  202. */
  203. template <typename Elem, typename Traits>
  204. std::basic_ostream<Elem, Traits>& operator<<(
  205. std::basic_ostream<Elem, Traits>& os, const address& addr);
  206. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  207. } // namespace ip
  208. } // namespace asio
  209. } // namespace boost
  210. namespace std {
  211. template <>
  212. struct hash<boost::asio::ip::address>
  213. {
  214. std::size_t operator()(const boost::asio::ip::address& addr)
  215. const noexcept
  216. {
  217. return addr.is_v4()
  218. ? std::hash<boost::asio::ip::address_v4>()(addr.to_v4())
  219. : std::hash<boost::asio::ip::address_v6>()(addr.to_v6());
  220. }
  221. };
  222. } // namespace std
  223. #include <boost/asio/detail/pop_options.hpp>
  224. #include <boost/asio/ip/impl/address.hpp>
  225. #if defined(BOOST_ASIO_HEADER_ONLY)
  226. # include <boost/asio/ip/impl/address.ipp>
  227. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  228. #endif // BOOST_ASIO_IP_ADDRESS_HPP