address.hpp 8.0 KB

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