address_v4.hpp 9.4 KB

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