address_v4.hpp 13 KB

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