address_v4.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // ip/address_v4.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. address_v4() BOOST_ASIO_NOEXCEPT
  59. {
  60. addr_.s_addr = 0;
  61. }
  62. /// Construct an address from raw bytes.
  63. BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
  64. /// Construct an address from an unsigned integer in host byte order.
  65. BOOST_ASIO_DECL explicit address_v4(uint_type addr);
  66. /// Copy constructor.
  67. address_v4(const address_v4& other) BOOST_ASIO_NOEXCEPT
  68. : addr_(other.addr_)
  69. {
  70. }
  71. #if defined(BOOST_ASIO_HAS_MOVE)
  72. /// Move constructor.
  73. address_v4(address_v4&& other) BOOST_ASIO_NOEXCEPT
  74. : addr_(other.addr_)
  75. {
  76. }
  77. #endif // defined(BOOST_ASIO_HAS_MOVE)
  78. /// Assign from another address.
  79. address_v4& operator=(const address_v4& other) BOOST_ASIO_NOEXCEPT
  80. {
  81. addr_ = other.addr_;
  82. return *this;
  83. }
  84. #if defined(BOOST_ASIO_HAS_MOVE)
  85. /// Move-assign from another address.
  86. address_v4& operator=(address_v4&& other) BOOST_ASIO_NOEXCEPT
  87. {
  88. addr_ = other.addr_;
  89. return *this;
  90. }
  91. #endif // defined(BOOST_ASIO_HAS_MOVE)
  92. /// Get the address in bytes, in network byte order.
  93. BOOST_ASIO_DECL bytes_type to_bytes() const BOOST_ASIO_NOEXCEPT;
  94. /// Get the address as an unsigned integer in host byte order
  95. BOOST_ASIO_DECL uint_type to_uint() const BOOST_ASIO_NOEXCEPT;
  96. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  97. /// Get the address as an unsigned long in host byte order
  98. BOOST_ASIO_DECL unsigned long to_ulong() const;
  99. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  100. /// Get the address as a string in dotted decimal format.
  101. BOOST_ASIO_DECL std::string to_string() const;
  102. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  103. /// (Deprecated: Use other overload.) Get the address as a string in dotted
  104. /// decimal format.
  105. BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
  106. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  107. /// string in dotted decimal form.
  108. static address_v4 from_string(const char* str);
  109. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  110. /// string in dotted decimal form.
  111. static address_v4 from_string(
  112. const char* str, boost::system::error_code& ec);
  113. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  114. /// string in dotted decimal form.
  115. static address_v4 from_string(const std::string& str);
  116. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  117. /// string in dotted decimal form.
  118. static address_v4 from_string(
  119. const std::string& str, boost::system::error_code& ec);
  120. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  121. /// Determine whether the address is a loopback address.
  122. BOOST_ASIO_DECL bool is_loopback() const BOOST_ASIO_NOEXCEPT;
  123. /// Determine whether the address is unspecified.
  124. BOOST_ASIO_DECL bool is_unspecified() const BOOST_ASIO_NOEXCEPT;
  125. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  126. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  127. /// class A address.
  128. BOOST_ASIO_DECL bool is_class_a() const;
  129. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  130. /// class B address.
  131. BOOST_ASIO_DECL bool is_class_b() const;
  132. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  133. /// class C address.
  134. BOOST_ASIO_DECL bool is_class_c() const;
  135. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  136. /// Determine whether the address is a multicast address.
  137. BOOST_ASIO_DECL bool is_multicast() const BOOST_ASIO_NOEXCEPT;
  138. /// Compare two addresses for equality.
  139. friend bool operator==(const address_v4& a1,
  140. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  141. {
  142. return a1.addr_.s_addr == a2.addr_.s_addr;
  143. }
  144. /// Compare two addresses for inequality.
  145. friend bool operator!=(const address_v4& a1,
  146. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  147. {
  148. return a1.addr_.s_addr != a2.addr_.s_addr;
  149. }
  150. /// Compare addresses for ordering.
  151. friend bool operator<(const address_v4& a1,
  152. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  153. {
  154. return a1.to_uint() < a2.to_uint();
  155. }
  156. /// Compare addresses for ordering.
  157. friend bool operator>(const address_v4& a1,
  158. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  159. {
  160. return a1.to_uint() > a2.to_uint();
  161. }
  162. /// Compare addresses for ordering.
  163. friend bool operator<=(const address_v4& a1,
  164. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  165. {
  166. return a1.to_uint() <= a2.to_uint();
  167. }
  168. /// Compare addresses for ordering.
  169. friend bool operator>=(const address_v4& a1,
  170. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  171. {
  172. return a1.to_uint() >= a2.to_uint();
  173. }
  174. /// Obtain an address object that represents any address.
  175. static address_v4 any() BOOST_ASIO_NOEXCEPT
  176. {
  177. return address_v4();
  178. }
  179. /// Obtain an address object that represents the loopback address.
  180. static address_v4 loopback() BOOST_ASIO_NOEXCEPT
  181. {
  182. return address_v4(0x7F000001);
  183. }
  184. /// Obtain an address object that represents the broadcast address.
  185. static address_v4 broadcast() BOOST_ASIO_NOEXCEPT
  186. {
  187. return address_v4(0xFFFFFFFF);
  188. }
  189. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  190. /// (Deprecated: Use network_v4 class.) Obtain an address object that
  191. /// represents the broadcast address that corresponds to the specified
  192. /// address and netmask.
  193. BOOST_ASIO_DECL static address_v4 broadcast(
  194. const address_v4& addr, const address_v4& mask);
  195. /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
  196. /// to the address, based on its address class.
  197. BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
  198. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  199. private:
  200. // The underlying IPv4 address.
  201. boost::asio::detail::in4_addr_type addr_;
  202. };
  203. /// Create an IPv4 address from raw bytes in network order.
  204. /**
  205. * @relates address_v4
  206. */
  207. inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
  208. {
  209. return address_v4(bytes);
  210. }
  211. /// Create an IPv4 address from an unsigned integer in host byte order.
  212. /**
  213. * @relates address_v4
  214. */
  215. inline address_v4 make_address_v4(address_v4::uint_type addr)
  216. {
  217. return address_v4(addr);
  218. }
  219. /// Create an IPv4 address from an IP address string in dotted decimal form.
  220. /**
  221. * @relates address_v4
  222. */
  223. BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
  224. /// Create an IPv4 address from an IP address string in dotted decimal form.
  225. /**
  226. * @relates address_v4
  227. */
  228. BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
  229. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  230. /// Create an IPv4 address from an IP address string in dotted decimal form.
  231. /**
  232. * @relates address_v4
  233. */
  234. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
  235. /// Create an IPv4 address from an IP address string in dotted decimal form.
  236. /**
  237. * @relates address_v4
  238. */
  239. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
  240. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  241. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  242. || defined(GENERATING_DOCUMENTATION)
  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(string_view 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(string_view str,
  253. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  254. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  255. // || defined(GENERATING_DOCUMENTATION)
  256. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  257. /// Output an address as a string.
  258. /**
  259. * Used to output a human-readable string for a specified address.
  260. *
  261. * @param os The output stream to which the string will be written.
  262. *
  263. * @param addr The address to be written.
  264. *
  265. * @return The output stream.
  266. *
  267. * @relates boost::asio::ip::address_v4
  268. */
  269. template <typename Elem, typename Traits>
  270. std::basic_ostream<Elem, Traits>& operator<<(
  271. std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
  272. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  273. } // namespace ip
  274. } // namespace asio
  275. } // namespace boost
  276. #if defined(BOOST_ASIO_HAS_STD_HASH)
  277. namespace std {
  278. template <>
  279. struct hash<boost::asio::ip::address_v4>
  280. {
  281. std::size_t operator()(const boost::asio::ip::address_v4& addr)
  282. const BOOST_ASIO_NOEXCEPT
  283. {
  284. return std::hash<unsigned int>()(addr.to_uint());
  285. }
  286. };
  287. } // namespace std
  288. #endif // defined(BOOST_ASIO_HAS_STD_HASH)
  289. #include <boost/asio/detail/pop_options.hpp>
  290. #include <boost/asio/ip/impl/address_v4.hpp>
  291. #if defined(BOOST_ASIO_HEADER_ONLY)
  292. # include <boost/asio/ip/impl/address_v4.ipp>
  293. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  294. #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP