address_v4.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // ip/address_v4.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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. /// Get the address as a string in dotted decimal format.
  107. BOOST_ASIO_DECL std::string to_string() const;
  108. /// Determine whether the address is a loopback address.
  109. /**
  110. * This function tests whether the address is in the address block
  111. * <tt>127.0.0.0/8</tt>, which corresponds to the address range
  112. * <tt>127.0.0.0 - 127.255.255.255</tt>.
  113. *
  114. * @returns <tt>(to_uint() & 0xFF000000) == 0x7F000000</tt>.
  115. */
  116. BOOST_ASIO_DECL bool is_loopback() const noexcept;
  117. /// Determine whether the address is unspecified.
  118. /**
  119. * This function tests whether the address is the unspecified address
  120. * <tt>0.0.0.0</tt>.
  121. *
  122. * @returns <tt>to_uint() == 0</tt>.
  123. */
  124. BOOST_ASIO_DECL bool is_unspecified() const noexcept;
  125. /// Determine whether the address is a multicast address.
  126. /**
  127. * This function tests whether the address is in the multicast address block
  128. * <tt>224.0.0.0/4</tt>, which corresponds to the address range
  129. * <tt>224.0.0.0 - 239.255.255.255</tt>.
  130. *
  131. * @returns <tt>(to_uint() & 0xF0000000) == 0xE0000000</tt>.
  132. */
  133. BOOST_ASIO_DECL bool is_multicast() const noexcept;
  134. /// Compare two addresses for equality.
  135. friend bool operator==(const address_v4& a1,
  136. const address_v4& a2) noexcept
  137. {
  138. return a1.addr_.s_addr == a2.addr_.s_addr;
  139. }
  140. /// Compare two addresses for inequality.
  141. friend bool operator!=(const address_v4& a1,
  142. const address_v4& a2) noexcept
  143. {
  144. return a1.addr_.s_addr != a2.addr_.s_addr;
  145. }
  146. /// Compare addresses for ordering.
  147. /**
  148. * Compares two addresses in host byte order.
  149. *
  150. * @returns <tt>a1.to_uint() < a2.to_uint()</tt>.
  151. */
  152. friend bool operator<(const address_v4& a1,
  153. const address_v4& a2) noexcept
  154. {
  155. return a1.to_uint() < a2.to_uint();
  156. }
  157. /// Compare addresses for ordering.
  158. /**
  159. * Compares two addresses in host byte order.
  160. *
  161. * @returns <tt>a1.to_uint() > a2.to_uint()</tt>.
  162. */
  163. friend bool operator>(const address_v4& a1,
  164. const address_v4& a2) noexcept
  165. {
  166. return a1.to_uint() > a2.to_uint();
  167. }
  168. /// Compare addresses for ordering.
  169. /**
  170. * Compares two addresses in host byte order.
  171. *
  172. * @returns <tt>a1.to_uint() <= a2.to_uint()</tt>.
  173. */
  174. friend bool operator<=(const address_v4& a1,
  175. const address_v4& a2) noexcept
  176. {
  177. return a1.to_uint() <= a2.to_uint();
  178. }
  179. /// Compare addresses for ordering.
  180. /**
  181. * Compares two addresses in host byte order.
  182. *
  183. * @returns <tt>a1.to_uint() >= a2.to_uint()</tt>.
  184. */
  185. friend bool operator>=(const address_v4& a1,
  186. const address_v4& a2) noexcept
  187. {
  188. return a1.to_uint() >= a2.to_uint();
  189. }
  190. /// Obtain an address object that represents any address.
  191. /**
  192. * This functions returns an address that represents the "any" address
  193. * <tt>0.0.0.0</tt>.
  194. *
  195. * @returns A default-constructed @c address_v4 object.
  196. */
  197. static address_v4 any() noexcept
  198. {
  199. return address_v4();
  200. }
  201. /// Obtain an address object that represents the loopback address.
  202. /**
  203. * This function returns an address that represents the well-known loopback
  204. * address <tt>127.0.0.1</tt>.
  205. *
  206. * @returns <tt>address_v4(0x7F000001)</tt>.
  207. */
  208. static address_v4 loopback() noexcept
  209. {
  210. return address_v4(0x7F000001);
  211. }
  212. /// Obtain an address object that represents the broadcast address.
  213. /**
  214. * This function returns an address that represents the broadcast address
  215. * <tt>255.255.255.255</tt>.
  216. *
  217. * @returns <tt>address_v4(0xFFFFFFFF)</tt>.
  218. */
  219. static address_v4 broadcast() noexcept
  220. {
  221. return address_v4(0xFFFFFFFF);
  222. }
  223. private:
  224. // The underlying IPv4 address.
  225. boost::asio::detail::in4_addr_type addr_;
  226. };
  227. /// Create an IPv4 address from raw bytes in network order.
  228. /**
  229. * @relates address_v4
  230. */
  231. inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
  232. {
  233. return address_v4(bytes);
  234. }
  235. /// Create an IPv4 address from an unsigned integer in host byte order.
  236. /**
  237. * @relates address_v4
  238. */
  239. inline address_v4 make_address_v4(address_v4::uint_type addr)
  240. {
  241. return address_v4(addr);
  242. }
  243. /// Create an IPv4 address from an IP address string in dotted decimal form.
  244. /**
  245. * @relates address_v4
  246. */
  247. BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
  248. /// Create an IPv4 address from an IP address string in dotted decimal form.
  249. /**
  250. * @relates address_v4
  251. */
  252. BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
  253. boost::system::error_code& ec) noexcept;
  254. /// Create an IPv4 address from an IP address string in dotted decimal form.
  255. /**
  256. * @relates address_v4
  257. */
  258. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
  259. /// Create an IPv4 address from an IP address string in dotted decimal form.
  260. /**
  261. * @relates address_v4
  262. */
  263. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
  264. boost::system::error_code& ec) noexcept;
  265. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  266. || defined(GENERATING_DOCUMENTATION)
  267. /// Create an IPv4 address from an IP address string in dotted decimal form.
  268. /**
  269. * @relates address_v4
  270. */
  271. BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
  272. /// Create an IPv4 address from an IP address string in dotted decimal form.
  273. /**
  274. * @relates address_v4
  275. */
  276. BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
  277. boost::system::error_code& ec) noexcept;
  278. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  279. // || defined(GENERATING_DOCUMENTATION)
  280. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  281. /// Output an address as a string.
  282. /**
  283. * Used to output a human-readable string for a specified address.
  284. *
  285. * @param os The output stream to which the string will be written.
  286. *
  287. * @param addr The address to be written.
  288. *
  289. * @return The output stream.
  290. *
  291. * @relates boost::asio::ip::address_v4
  292. */
  293. template <typename Elem, typename Traits>
  294. std::basic_ostream<Elem, Traits>& operator<<(
  295. std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
  296. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  297. } // namespace ip
  298. } // namespace asio
  299. } // namespace boost
  300. namespace std {
  301. template <>
  302. struct hash<boost::asio::ip::address_v4>
  303. {
  304. std::size_t operator()(const boost::asio::ip::address_v4& addr)
  305. const noexcept
  306. {
  307. return std::hash<unsigned int>()(addr.to_uint());
  308. }
  309. };
  310. } // namespace std
  311. #include <boost/asio/detail/pop_options.hpp>
  312. #include <boost/asio/ip/impl/address_v4.hpp>
  313. #if defined(BOOST_ASIO_HEADER_ONLY)
  314. # include <boost/asio/ip/impl/address_v4.ipp>
  315. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  316. #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP