address.hpp 8.6 KB

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