address.hpp 8.1 KB

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