address_v4.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // ip/address_v4.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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() BOOST_ASIO_NOEXCEPT
  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) BOOST_ASIO_NOEXCEPT
  65. : addr_(other.addr_)
  66. {
  67. }
  68. #if defined(BOOST_ASIO_HAS_MOVE)
  69. /// Move constructor.
  70. address_v4(address_v4&& other) BOOST_ASIO_NOEXCEPT
  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) BOOST_ASIO_NOEXCEPT
  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) BOOST_ASIO_NOEXCEPT
  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 BOOST_ASIO_NOEXCEPT;
  91. /// Get the address as an unsigned integer in host byte order
  92. BOOST_ASIO_DECL uint_type to_uint() const BOOST_ASIO_NOEXCEPT;
  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 BOOST_ASIO_NOEXCEPT;
  120. /// Determine whether the address is unspecified.
  121. BOOST_ASIO_DECL bool is_unspecified() const BOOST_ASIO_NOEXCEPT;
  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 BOOST_ASIO_NOEXCEPT;
  135. /// Compare two addresses for equality.
  136. friend bool operator==(const address_v4& a1,
  137. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  138. {
  139. return a1.addr_.s_addr == a2.addr_.s_addr;
  140. }
  141. /// Compare two addresses for inequality.
  142. friend bool operator!=(const address_v4& a1,
  143. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  144. {
  145. return a1.addr_.s_addr != a2.addr_.s_addr;
  146. }
  147. /// Compare addresses for ordering.
  148. friend bool operator<(const address_v4& a1,
  149. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  150. {
  151. return a1.to_uint() < a2.to_uint();
  152. }
  153. /// Compare addresses for ordering.
  154. friend bool operator>(const address_v4& a1,
  155. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  156. {
  157. return a1.to_uint() > a2.to_uint();
  158. }
  159. /// Compare addresses for ordering.
  160. friend bool operator<=(const address_v4& a1,
  161. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  162. {
  163. return a1.to_uint() <= a2.to_uint();
  164. }
  165. /// Compare addresses for ordering.
  166. friend bool operator>=(const address_v4& a1,
  167. const address_v4& a2) BOOST_ASIO_NOEXCEPT
  168. {
  169. return a1.to_uint() >= a2.to_uint();
  170. }
  171. /// Obtain an address object that represents any address.
  172. static address_v4 any() BOOST_ASIO_NOEXCEPT
  173. {
  174. return address_v4();
  175. }
  176. /// Obtain an address object that represents the loopback address.
  177. static address_v4 loopback() BOOST_ASIO_NOEXCEPT
  178. {
  179. return address_v4(0x7F000001);
  180. }
  181. /// Obtain an address object that represents the broadcast address.
  182. static address_v4 broadcast() BOOST_ASIO_NOEXCEPT
  183. {
  184. return address_v4(0xFFFFFFFF);
  185. }
  186. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  187. /// (Deprecated: Use network_v4 class.) Obtain an address object that
  188. /// represents the broadcast address that corresponds to the specified
  189. /// address and netmask.
  190. BOOST_ASIO_DECL static address_v4 broadcast(
  191. const address_v4& addr, const address_v4& mask);
  192. /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
  193. /// to the address, based on its address class.
  194. BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
  195. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  196. private:
  197. // The underlying IPv4 address.
  198. boost::asio::detail::in4_addr_type addr_;
  199. };
  200. /// Create an IPv4 address from raw bytes in network order.
  201. /**
  202. * @relates address_v4
  203. */
  204. inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
  205. {
  206. return address_v4(bytes);
  207. }
  208. /// Create an IPv4 address from an unsigned integer in host byte order.
  209. /**
  210. * @relates address_v4
  211. */
  212. inline address_v4 make_address_v4(address_v4::uint_type addr)
  213. {
  214. return address_v4(addr);
  215. }
  216. /// Create an IPv4 address from an IP address string in dotted decimal form.
  217. /**
  218. * @relates address_v4
  219. */
  220. BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
  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 char* str,
  226. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  227. /// Create an IPv4 address from an IP address string in dotted decimal form.
  228. /**
  229. * @relates address_v4
  230. */
  231. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
  232. /// Create an IPv4 address from an IP address string in dotted decimal form.
  233. /**
  234. * @relates address_v4
  235. */
  236. BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
  237. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  238. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  239. || defined(GENERATING_DOCUMENTATION)
  240. /// Create an IPv4 address from an IP address string in dotted decimal form.
  241. /**
  242. * @relates address_v4
  243. */
  244. BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
  245. /// Create an IPv4 address from an IP address string in dotted decimal form.
  246. /**
  247. * @relates address_v4
  248. */
  249. BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
  250. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  251. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  252. // || defined(GENERATING_DOCUMENTATION)
  253. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  254. /// Output an address as a string.
  255. /**
  256. * Used to output a human-readable string for a specified address.
  257. *
  258. * @param os The output stream to which the string will be written.
  259. *
  260. * @param addr The address to be written.
  261. *
  262. * @return The output stream.
  263. *
  264. * @relates boost::asio::ip::address_v4
  265. */
  266. template <typename Elem, typename Traits>
  267. std::basic_ostream<Elem, Traits>& operator<<(
  268. std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
  269. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  270. } // namespace ip
  271. } // namespace asio
  272. } // namespace boost
  273. #include <boost/asio/detail/pop_options.hpp>
  274. #include <boost/asio/ip/impl/address_v4.hpp>
  275. #if defined(BOOST_ASIO_HEADER_ONLY)
  276. # include <boost/asio/ip/impl/address_v4.ipp>
  277. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  278. #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP