address_v4.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // ip/address_v4.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_V4_HPP
  11. #define ASIO_IP_ADDRESS_V4_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/array.hpp"
  18. #include "asio/detail/cstdint.hpp"
  19. #include "asio/detail/socket_types.hpp"
  20. #include "asio/detail/string_view.hpp"
  21. #include "asio/detail/winsock_init.hpp"
  22. #include "asio/error_code.hpp"
  23. #if defined(ASIO_HAS_STD_HASH)
  24. # include <functional>
  25. #endif // defined(ASIO_HAS_STD_HASH)
  26. #if !defined(ASIO_NO_IOSTREAM)
  27. # include <iosfwd>
  28. #endif // !defined(ASIO_NO_IOSTREAM)
  29. #include "asio/detail/push_options.hpp"
  30. namespace asio {
  31. namespace ip {
  32. /// Implements IP version 4 style addresses.
  33. /**
  34. * The asio::ip::address_v4 class provides the ability to use and
  35. * manipulate IP version 4 addresses.
  36. *
  37. * @par Thread Safety
  38. * @e Distinct @e objects: Safe.@n
  39. * @e Shared @e objects: Unsafe.
  40. */
  41. class address_v4
  42. {
  43. public:
  44. /// The type used to represent an address as an unsigned integer.
  45. typedef uint_least32_t uint_type;
  46. /// The type used to represent an address as an array of bytes.
  47. /**
  48. * @note This type is defined in terms of the C++0x template @c std::array
  49. * when it is available. Otherwise, it uses @c boost:array.
  50. */
  51. #if defined(GENERATING_DOCUMENTATION)
  52. typedef array<unsigned char, 4> bytes_type;
  53. #else
  54. typedef asio::detail::array<unsigned char, 4> bytes_type;
  55. #endif
  56. /// Default constructor.
  57. address_v4() ASIO_NOEXCEPT
  58. {
  59. addr_.s_addr = 0;
  60. }
  61. /// Construct an address from raw bytes.
  62. ASIO_DECL explicit address_v4(const bytes_type& bytes);
  63. /// Construct an address from an unsigned integer in host byte order.
  64. ASIO_DECL explicit address_v4(uint_type addr);
  65. /// Copy constructor.
  66. address_v4(const address_v4& other) ASIO_NOEXCEPT
  67. : addr_(other.addr_)
  68. {
  69. }
  70. #if defined(ASIO_HAS_MOVE)
  71. /// Move constructor.
  72. address_v4(address_v4&& other) ASIO_NOEXCEPT
  73. : addr_(other.addr_)
  74. {
  75. }
  76. #endif // defined(ASIO_HAS_MOVE)
  77. /// Assign from another address.
  78. address_v4& operator=(const address_v4& other) ASIO_NOEXCEPT
  79. {
  80. addr_ = other.addr_;
  81. return *this;
  82. }
  83. #if defined(ASIO_HAS_MOVE)
  84. /// Move-assign from another address.
  85. address_v4& operator=(address_v4&& other) ASIO_NOEXCEPT
  86. {
  87. addr_ = other.addr_;
  88. return *this;
  89. }
  90. #endif // defined(ASIO_HAS_MOVE)
  91. /// Get the address in bytes, in network byte order.
  92. ASIO_DECL bytes_type to_bytes() const ASIO_NOEXCEPT;
  93. /// Get the address as an unsigned integer in host byte order
  94. ASIO_DECL uint_type to_uint() const ASIO_NOEXCEPT;
  95. #if !defined(ASIO_NO_DEPRECATED)
  96. /// Get the address as an unsigned long in host byte order
  97. ASIO_DECL unsigned long to_ulong() const;
  98. #endif // !defined(ASIO_NO_DEPRECATED)
  99. /// Get the address as a string in dotted decimal format.
  100. ASIO_DECL std::string to_string() const;
  101. #if !defined(ASIO_NO_DEPRECATED)
  102. /// (Deprecated: Use other overload.) Get the address as a string in dotted
  103. /// decimal format.
  104. ASIO_DECL std::string to_string(asio::error_code& ec) const;
  105. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  106. /// string in dotted decimal form.
  107. static address_v4 from_string(const char* str);
  108. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  109. /// string in dotted decimal form.
  110. static address_v4 from_string(
  111. const char* str, asio::error_code& ec);
  112. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  113. /// string in dotted decimal form.
  114. static address_v4 from_string(const std::string& str);
  115. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  116. /// string in dotted decimal form.
  117. static address_v4 from_string(
  118. const std::string& str, asio::error_code& ec);
  119. #endif // !defined(ASIO_NO_DEPRECATED)
  120. /// Determine whether the address is a loopback address.
  121. ASIO_DECL bool is_loopback() const ASIO_NOEXCEPT;
  122. /// Determine whether the address is unspecified.
  123. ASIO_DECL bool is_unspecified() const ASIO_NOEXCEPT;
  124. #if !defined(ASIO_NO_DEPRECATED)
  125. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  126. /// class A address.
  127. ASIO_DECL bool is_class_a() const;
  128. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  129. /// class B address.
  130. ASIO_DECL bool is_class_b() const;
  131. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  132. /// class C address.
  133. ASIO_DECL bool is_class_c() const;
  134. #endif // !defined(ASIO_NO_DEPRECATED)
  135. /// Determine whether the address is a multicast address.
  136. ASIO_DECL bool is_multicast() const ASIO_NOEXCEPT;
  137. /// Compare two addresses for equality.
  138. friend bool operator==(const address_v4& a1,
  139. const address_v4& a2) ASIO_NOEXCEPT
  140. {
  141. return a1.addr_.s_addr == a2.addr_.s_addr;
  142. }
  143. /// Compare two addresses for inequality.
  144. friend bool operator!=(const address_v4& a1,
  145. const address_v4& a2) ASIO_NOEXCEPT
  146. {
  147. return a1.addr_.s_addr != a2.addr_.s_addr;
  148. }
  149. /// Compare addresses for ordering.
  150. friend bool operator<(const address_v4& a1,
  151. const address_v4& a2) ASIO_NOEXCEPT
  152. {
  153. return a1.to_uint() < a2.to_uint();
  154. }
  155. /// Compare addresses for ordering.
  156. friend bool operator>(const address_v4& a1,
  157. const address_v4& a2) ASIO_NOEXCEPT
  158. {
  159. return a1.to_uint() > a2.to_uint();
  160. }
  161. /// Compare addresses for ordering.
  162. friend bool operator<=(const address_v4& a1,
  163. const address_v4& a2) ASIO_NOEXCEPT
  164. {
  165. return a1.to_uint() <= a2.to_uint();
  166. }
  167. /// Compare addresses for ordering.
  168. friend bool operator>=(const address_v4& a1,
  169. const address_v4& a2) ASIO_NOEXCEPT
  170. {
  171. return a1.to_uint() >= a2.to_uint();
  172. }
  173. /// Obtain an address object that represents any address.
  174. static address_v4 any() ASIO_NOEXCEPT
  175. {
  176. return address_v4();
  177. }
  178. /// Obtain an address object that represents the loopback address.
  179. static address_v4 loopback() ASIO_NOEXCEPT
  180. {
  181. return address_v4(0x7F000001);
  182. }
  183. /// Obtain an address object that represents the broadcast address.
  184. static address_v4 broadcast() ASIO_NOEXCEPT
  185. {
  186. return address_v4(0xFFFFFFFF);
  187. }
  188. #if !defined(ASIO_NO_DEPRECATED)
  189. /// (Deprecated: Use network_v4 class.) Obtain an address object that
  190. /// represents the broadcast address that corresponds to the specified
  191. /// address and netmask.
  192. ASIO_DECL static address_v4 broadcast(
  193. const address_v4& addr, const address_v4& mask);
  194. /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
  195. /// to the address, based on its address class.
  196. ASIO_DECL static address_v4 netmask(const address_v4& addr);
  197. #endif // !defined(ASIO_NO_DEPRECATED)
  198. private:
  199. // The underlying IPv4 address.
  200. asio::detail::in4_addr_type addr_;
  201. };
  202. /// Create an IPv4 address from raw bytes in network order.
  203. /**
  204. * @relates address_v4
  205. */
  206. inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
  207. {
  208. return address_v4(bytes);
  209. }
  210. /// Create an IPv4 address from an unsigned integer in host byte order.
  211. /**
  212. * @relates address_v4
  213. */
  214. inline address_v4 make_address_v4(address_v4::uint_type addr)
  215. {
  216. return address_v4(addr);
  217. }
  218. /// Create an IPv4 address from an IP address string in dotted decimal form.
  219. /**
  220. * @relates address_v4
  221. */
  222. ASIO_DECL address_v4 make_address_v4(const char* str);
  223. /// Create an IPv4 address from an IP address string in dotted decimal form.
  224. /**
  225. * @relates address_v4
  226. */
  227. ASIO_DECL address_v4 make_address_v4(const char* str,
  228. asio::error_code& ec) ASIO_NOEXCEPT;
  229. /// Create an IPv4 address from an IP address string in dotted decimal form.
  230. /**
  231. * @relates address_v4
  232. */
  233. ASIO_DECL address_v4 make_address_v4(const std::string& str);
  234. /// Create an IPv4 address from an IP address string in dotted decimal form.
  235. /**
  236. * @relates address_v4
  237. */
  238. ASIO_DECL address_v4 make_address_v4(const std::string& str,
  239. asio::error_code& ec) ASIO_NOEXCEPT;
  240. #if defined(ASIO_HAS_STRING_VIEW) \
  241. || defined(GENERATING_DOCUMENTATION)
  242. /// Create an IPv4 address from an IP address string in dotted decimal form.
  243. /**
  244. * @relates address_v4
  245. */
  246. ASIO_DECL address_v4 make_address_v4(string_view str);
  247. /// Create an IPv4 address from an IP address string in dotted decimal form.
  248. /**
  249. * @relates address_v4
  250. */
  251. ASIO_DECL address_v4 make_address_v4(string_view str,
  252. asio::error_code& ec) ASIO_NOEXCEPT;
  253. #endif // defined(ASIO_HAS_STRING_VIEW)
  254. // || defined(GENERATING_DOCUMENTATION)
  255. #if !defined(ASIO_NO_IOSTREAM)
  256. /// Output an address as a string.
  257. /**
  258. * Used to output a human-readable string for a specified address.
  259. *
  260. * @param os The output stream to which the string will be written.
  261. *
  262. * @param addr The address to be written.
  263. *
  264. * @return The output stream.
  265. *
  266. * @relates asio::ip::address_v4
  267. */
  268. template <typename Elem, typename Traits>
  269. std::basic_ostream<Elem, Traits>& operator<<(
  270. std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
  271. #endif // !defined(ASIO_NO_IOSTREAM)
  272. } // namespace ip
  273. } // namespace asio
  274. #if defined(ASIO_HAS_STD_HASH)
  275. namespace std {
  276. template <>
  277. struct hash<asio::ip::address_v4>
  278. {
  279. std::size_t operator()(const asio::ip::address_v4& addr)
  280. const ASIO_NOEXCEPT
  281. {
  282. return std::hash<unsigned int>()(addr.to_uint());
  283. }
  284. };
  285. } // namespace std
  286. #endif // defined(ASIO_HAS_STD_HASH)
  287. #include "asio/detail/pop_options.hpp"
  288. #include "asio/ip/impl/address_v4.hpp"
  289. #if defined(ASIO_HEADER_ONLY)
  290. # include "asio/ip/impl/address_v4.ipp"
  291. #endif // defined(ASIO_HEADER_ONLY)
  292. #endif // ASIO_IP_ADDRESS_V4_HPP