address_v4.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // ip/address_v4.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_V4_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <functional>
  17. #include <string>
  18. #include <boost/asio/detail/array.hpp>
  19. #include <boost/asio/detail/cstdint.hpp>
  20. #include <boost/asio/detail/socket_types.hpp>
  21. #include <boost/asio/detail/string_view.hpp>
  22. #include <boost/asio/detail/winsock_init.hpp>
  23. #include <boost/system/error_code.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 IP version 4 style addresses.
  32. /**
  33. * The boost::asio::ip::address_v4 class provides the ability to use and
  34. * manipulate IP version 4 addresses.
  35. *
  36. * @par Thread Safety
  37. * @e Distinct @e objects: Safe.@n
  38. * @e Shared @e objects: Unsafe.
  39. */
  40. class address_v4
  41. {
  42. public:
  43. /// The type used to represent an address as an unsigned integer.
  44. typedef uint_least32_t uint_type;
  45. /// The type used to represent an address as an array of bytes.
  46. /**
  47. * @note This type is defined in terms of the C++0x template @c std::array
  48. * when it is available. Otherwise, it uses @c boost:array.
  49. */
  50. #if defined(GENERATING_DOCUMENTATION)
  51. typedef array<unsigned char, 4> bytes_type;
  52. #else
  53. typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
  54. #endif
  55. /// Default constructor.
  56. /**
  57. * Initialises the @c address_v4 object such that:
  58. * @li <tt>to_bytes()</tt> yields <tt>{0, 0, 0, 0}</tt>; and
  59. * @li <tt>to_uint() == 0</tt>.
  60. */
  61. address_v4() noexcept
  62. {
  63. addr_.s_addr = 0;
  64. }
  65. /// Construct an address from raw bytes.
  66. /**
  67. * Initialises the @c address_v4 object such that <tt>to_bytes() ==
  68. * bytes</tt>.
  69. *
  70. * @throws out_of_range Thrown if any element in @c bytes is not in the range
  71. * <tt>0 - 0xFF</tt>. Note that no range checking is required for platforms
  72. * where <tt>std::numeric_limits<unsigned char>::max()</tt> is <tt>0xFF</tt>.
  73. */
  74. BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
  75. /// Construct an address from an unsigned integer in host byte order.
  76. /**
  77. * Initialises the @c address_v4 object such that <tt>to_uint() == addr</tt>.
  78. */
  79. BOOST_ASIO_DECL explicit address_v4(uint_type addr);
  80. /// Copy constructor.
  81. address_v4(const address_v4& other) noexcept
  82. : addr_(other.addr_)
  83. {
  84. }
  85. /// Move constructor.
  86. address_v4(address_v4&& other) noexcept
  87. : addr_(other.addr_)
  88. {
  89. }
  90. /// Assign from another address.
  91. address_v4& operator=(const address_v4& other) noexcept
  92. {
  93. addr_ = other.addr_;
  94. return *this;
  95. }
  96. /// Move-assign from another address.
  97. address_v4& operator=(address_v4&& other) noexcept
  98. {
  99. addr_ = other.addr_;
  100. return *this;
  101. }
  102. /// Get the address in bytes, in network byte order.
  103. BOOST_ASIO_DECL bytes_type to_bytes() const noexcept;
  104. /// Get the address as an unsigned integer in host byte order.
  105. BOOST_ASIO_DECL uint_type to_uint() const noexcept;
  106. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  107. /// (Deprecated: Use to_uint().) Get the address as an unsigned long in host
  108. /// byte order.
  109. BOOST_ASIO_DECL unsigned long to_ulong() const;
  110. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  111. /// Get the address as a string in dotted decimal format.
  112. BOOST_ASIO_DECL std::string to_string() const;
  113. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  114. /// (Deprecated: Use other overload.) Get the address as a string in dotted
  115. /// decimal format.
  116. BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
  117. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  118. /// string in dotted decimal form.
  119. static address_v4 from_string(const char* str);
  120. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  121. /// string in dotted decimal form.
  122. static address_v4 from_string(
  123. const char* str, boost::system::error_code& ec);
  124. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  125. /// string in dotted decimal form.
  126. static address_v4 from_string(const std::string& str);
  127. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  128. /// string in dotted decimal form.
  129. static address_v4 from_string(
  130. const std::string& str, boost::system::error_code& ec);
  131. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  132. /// Determine whether the address is a loopback address.
  133. /**
  134. * This function tests whether the address is in the address block
  135. * <tt>127.0.0.0/8</tt>, which corresponds to the address range
  136. * <tt>127.0.0.0 - 127.255.255.255</tt>.
  137. *
  138. * @returns <tt>(to_uint() & 0xFF000000) == 0x7F000000</tt>.
  139. */
  140. BOOST_ASIO_DECL bool is_loopback() const noexcept;
  141. /// Determine whether the address is unspecified.
  142. /**
  143. * This function tests whether the address is the unspecified address
  144. * <tt>0.0.0.0</tt>.
  145. *
  146. * @returns <tt>to_uint() == 0</tt>.
  147. */
  148. BOOST_ASIO_DECL bool is_unspecified() const noexcept;
  149. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  150. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  151. /// class A address.
  152. BOOST_ASIO_DECL bool is_class_a() const;
  153. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  154. /// class B address.
  155. BOOST_ASIO_DECL bool is_class_b() const;
  156. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  157. /// class C address.
  158. BOOST_ASIO_DECL bool is_class_c() const;
  159. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  160. /// Determine whether the address is a multicast address.
  161. /**
  162. * This function tests whether the address is in the multicast address block
  163. * <tt>224.0.0.0/4</tt>, which corresponds to the address range
  164. * <tt>224.0.0.0 - 239.255.255.255</tt>.
  165. *
  166. * @returns <tt>(to_uint() & 0xF0000000) == 0xE0000000</tt>.
  167. */
  168. BOOST_ASIO_DECL bool is_multicast() const noexcept;
  169. /// Compare two addresses for equality.
  170. friend bool operator==(const address_v4& a1,
  171. const address_v4& a2) noexcept
  172. {
  173. return a1.addr_.s_addr == a2.addr_.s_addr;
  174. }
  175. /// Compare two addresses for inequality.
  176. friend bool operator!=(const address_v4& a1,
  177. const address_v4& a2) noexcept
  178. {
  179. return a1.addr_.s_addr != a2.addr_.s_addr;
  180. }
  181. /// Compare addresses for ordering.
  182. /**
  183. * Compares two addresses in host byte order.
  184. *
  185. * @returns <tt>a1.to_uint() < a2.to_uint()</tt>.
  186. */
  187. friend bool operator<(const address_v4& a1,
  188. const address_v4& a2) noexcept
  189. {
  190. return a1.to_uint() < a2.to_uint();
  191. }
  192. /// Compare addresses for ordering.
  193. /**
  194. * Compares two addresses in host byte order.
  195. *
  196. * @returns <tt>a1.to_uint() > a2.to_uint()</tt>.
  197. */
  198. friend bool operator>(const address_v4& a1,
  199. const address_v4& a2) noexcept
  200. {
  201. return a1.to_uint() > a2.to_uint();
  202. }
  203. /// Compare addresses for ordering.
  204. /**
  205. * Compares two addresses in host byte order.
  206. *
  207. * @returns <tt>a1.to_uint() <= a2.to_uint()</tt>.
  208. */
  209. friend bool operator<=(const address_v4& a1,
  210. const address_v4& a2) noexcept
  211. {
  212. return a1.to_uint() <= a2.to_uint();
  213. }
  214. /// Compare addresses for ordering.
  215. /**
  216. * Compares two addresses in host byte order.
  217. *
  218. * @returns <tt>a1.to_uint() >= a2.to_uint()</tt>.
  219. */
  220. friend bool operator>=(const address_v4& a1,
  221. const address_v4& a2) noexcept
  222. {
  223. return a1.to_uint() >= a2.to_uint();
  224. }
  225. /// Obtain an address object that represents any address.
  226. /**
  227. * This functions returns an address that represents the "any" address
  228. * <tt>0.0.0.0</tt>.
  229. *
  230. * @returns A default-constructed @c address_v4 object.
  231. */
  232. static address_v4 any() noexcept
  233. {
  234. return address_v4();
  235. }
  236. /// Obtain an address object that represents the loopback address.
  237. /**
  238. * This function returns an address that represents the well-known loopback
  239. * address <tt>127.0.0.1</tt>.
  240. *
  241. * @returns <tt>address_v4(0x7F000001)</tt>.
  242. */
  243. static address_v4 loopback() noexcept
  244. {
  245. return address_v4(0x7F000001);
  246. }
  247. /// Obtain an address object that represents the broadcast address.
  248. /**
  249. * This function returns an address that represents the broadcast address
  250. * <tt>255.255.255.255</tt>.
  251. *
  252. * @returns <tt>address_v4(0xFFFFFFFF)</tt>.
  253. */
  254. static address_v4 broadcast() noexcept
  255. {
  256. return address_v4(0xFFFFFFFF);
  257. }
  258. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  259. /// (Deprecated: Use network_v4 class.) Obtain an address object that
  260. /// represents the broadcast address that corresponds to the specified
  261. /// address and netmask.
  262. BOOST_ASIO_DECL static address_v4 broadcast(
  263. const address_v4& addr, const address_v4& mask);
  264. /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
  265. /// to the address, based on its address class.
  266. BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
  267. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  268. private:
  269. // The underlying IPv4 address.
  270. boost::asio::detail::in4_addr_type addr_;
  271. };
  272. /// Create an IPv4 address from raw bytes in network order.
  273. /**
  274. * @relates address_v4
  275. */
  276. inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
  277. {
  278. return address_v4(bytes);
  279. }
  280. /// Create an IPv4 address from an unsigned integer in host byte order.
  281. /**
  282. * @relates address_v4
  283. */
  284. inline address_v4 make_address_v4(address_v4::uint_type addr)
  285. {
  286. return address_v4(addr);
  287. }
  288. /// Create an IPv4 address from an IP address string in dotted decimal form.
  289. /**
  290. * @relates address_v4
  291. */
  292. BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
  293. /// Create an IPv4 address from an IP address string in dotted decimal form.
  294. /**
  295. * @relates address_v4
  296. */
  297. BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
  298. boost::system::error_code& ec) noexcept;
  299. /// Create an IPv4 address from an IP address string in dotted decimal form.
  300. /**
  301. * @relates address_v4
  302. */
  303. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
  304. /// Create an IPv4 address from an IP address string in dotted decimal form.
  305. /**
  306. * @relates address_v4
  307. */
  308. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
  309. boost::system::error_code& ec) noexcept;
  310. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  311. || defined(GENERATING_DOCUMENTATION)
  312. /// Create an IPv4 address from an IP address string in dotted decimal form.
  313. /**
  314. * @relates address_v4
  315. */
  316. BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
  317. /// Create an IPv4 address from an IP address string in dotted decimal form.
  318. /**
  319. * @relates address_v4
  320. */
  321. BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
  322. boost::system::error_code& ec) noexcept;
  323. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  324. // || defined(GENERATING_DOCUMENTATION)
  325. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  326. /// Output an address as a string.
  327. /**
  328. * Used to output a human-readable string for a specified address.
  329. *
  330. * @param os The output stream to which the string will be written.
  331. *
  332. * @param addr The address to be written.
  333. *
  334. * @return The output stream.
  335. *
  336. * @relates boost::asio::ip::address_v4
  337. */
  338. template <typename Elem, typename Traits>
  339. std::basic_ostream<Elem, Traits>& operator<<(
  340. std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
  341. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  342. } // namespace ip
  343. } // namespace asio
  344. } // namespace boost
  345. namespace std {
  346. template <>
  347. struct hash<boost::asio::ip::address_v4>
  348. {
  349. std::size_t operator()(const boost::asio::ip::address_v4& addr)
  350. const noexcept
  351. {
  352. return std::hash<unsigned int>()(addr.to_uint());
  353. }
  354. };
  355. } // namespace std
  356. #include <boost/asio/detail/pop_options.hpp>
  357. #include <boost/asio/ip/impl/address_v4.hpp>
  358. #if defined(BOOST_ASIO_HEADER_ONLY)
  359. # include <boost/asio/ip/impl/address_v4.ipp>
  360. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  361. #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP