address.hpp 7.6 KB

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