address.hpp 7.6 KB

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