address_v4.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // ip/address_v4.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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/socket_types.hpp>
  19. #include <boost/asio/detail/winsock_init.hpp>
  20. #include <boost/system/error_code.hpp>
  21. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  22. # include <iosfwd>
  23. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace ip {
  28. /// Implements IP version 4 style addresses.
  29. /**
  30. * The boost::asio::ip::address_v4 class provides the ability to use and
  31. * manipulate IP version 4 addresses.
  32. *
  33. * @par Thread Safety
  34. * @e Distinct @e objects: Safe.@n
  35. * @e Shared @e objects: Unsafe.
  36. */
  37. class address_v4
  38. {
  39. public:
  40. /// The type used to represent an address as an array of bytes.
  41. /**
  42. * @note This type is defined in terms of the C++0x template @c std::array
  43. * when it is available. Otherwise, it uses @c boost:array.
  44. */
  45. #if defined(GENERATING_DOCUMENTATION)
  46. typedef array<unsigned char, 4> bytes_type;
  47. #else
  48. typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
  49. #endif
  50. /// Default constructor.
  51. address_v4()
  52. {
  53. addr_.s_addr = 0;
  54. }
  55. /// Construct an address from raw bytes.
  56. BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
  57. /// Construct an address from a unsigned long in host byte order.
  58. BOOST_ASIO_DECL explicit address_v4(unsigned long addr);
  59. /// Copy constructor.
  60. address_v4(const address_v4& other)
  61. : addr_(other.addr_)
  62. {
  63. }
  64. #if defined(BOOST_ASIO_HAS_MOVE)
  65. /// Move constructor.
  66. address_v4(address_v4&& other)
  67. : addr_(other.addr_)
  68. {
  69. }
  70. #endif // defined(BOOST_ASIO_HAS_MOVE)
  71. /// Assign from another address.
  72. address_v4& operator=(const address_v4& other)
  73. {
  74. addr_ = other.addr_;
  75. return *this;
  76. }
  77. #if defined(BOOST_ASIO_HAS_MOVE)
  78. /// Move-assign from another address.
  79. address_v4& operator=(address_v4&& other)
  80. {
  81. addr_ = other.addr_;
  82. return *this;
  83. }
  84. #endif // defined(BOOST_ASIO_HAS_MOVE)
  85. /// Get the address in bytes, in network byte order.
  86. BOOST_ASIO_DECL bytes_type to_bytes() const;
  87. /// Get the address as an unsigned long in host byte order
  88. BOOST_ASIO_DECL unsigned long to_ulong() const;
  89. /// Get the address as a string in dotted decimal format.
  90. BOOST_ASIO_DECL std::string to_string() const;
  91. /// Get the address as a string in dotted decimal format.
  92. BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
  93. /// Create an address from an IP address string in dotted decimal form.
  94. BOOST_ASIO_DECL static address_v4 from_string(const char* str);
  95. /// Create an address from an IP address string in dotted decimal form.
  96. BOOST_ASIO_DECL static address_v4 from_string(
  97. const char* str, boost::system::error_code& ec);
  98. /// Create an address from an IP address string in dotted decimal form.
  99. BOOST_ASIO_DECL static address_v4 from_string(const std::string& str);
  100. /// Create an address from an IP address string in dotted decimal form.
  101. BOOST_ASIO_DECL static address_v4 from_string(
  102. const std::string& str, boost::system::error_code& ec);
  103. /// Determine whether the address is a loopback address.
  104. BOOST_ASIO_DECL bool is_loopback() const;
  105. /// Determine whether the address is unspecified.
  106. BOOST_ASIO_DECL bool is_unspecified() const;
  107. /// Determine whether the address is a class A address.
  108. BOOST_ASIO_DECL bool is_class_a() const;
  109. /// Determine whether the address is a class B address.
  110. BOOST_ASIO_DECL bool is_class_b() const;
  111. /// Determine whether the address is a class C address.
  112. BOOST_ASIO_DECL bool is_class_c() const;
  113. /// Determine whether the address is a multicast address.
  114. BOOST_ASIO_DECL bool is_multicast() const;
  115. /// Compare two addresses for equality.
  116. friend bool operator==(const address_v4& a1, const address_v4& a2)
  117. {
  118. return a1.addr_.s_addr == a2.addr_.s_addr;
  119. }
  120. /// Compare two addresses for inequality.
  121. friend bool operator!=(const address_v4& a1, const address_v4& a2)
  122. {
  123. return a1.addr_.s_addr != a2.addr_.s_addr;
  124. }
  125. /// Compare addresses for ordering.
  126. friend bool operator<(const address_v4& a1, const address_v4& a2)
  127. {
  128. return a1.to_ulong() < a2.to_ulong();
  129. }
  130. /// Compare addresses for ordering.
  131. friend bool operator>(const address_v4& a1, const address_v4& a2)
  132. {
  133. return a1.to_ulong() > a2.to_ulong();
  134. }
  135. /// Compare addresses for ordering.
  136. friend bool operator<=(const address_v4& a1, const address_v4& a2)
  137. {
  138. return a1.to_ulong() <= a2.to_ulong();
  139. }
  140. /// Compare addresses for ordering.
  141. friend bool operator>=(const address_v4& a1, const address_v4& a2)
  142. {
  143. return a1.to_ulong() >= a2.to_ulong();
  144. }
  145. /// Obtain an address object that represents any address.
  146. static address_v4 any()
  147. {
  148. return address_v4();
  149. }
  150. /// Obtain an address object that represents the loopback address.
  151. static address_v4 loopback()
  152. {
  153. return address_v4(0x7F000001);
  154. }
  155. /// Obtain an address object that represents the broadcast address.
  156. static address_v4 broadcast()
  157. {
  158. return address_v4(0xFFFFFFFF);
  159. }
  160. /// Obtain an address object that represents the broadcast address that
  161. /// corresponds to the specified address and netmask.
  162. BOOST_ASIO_DECL static address_v4 broadcast(
  163. const address_v4& addr, const address_v4& mask);
  164. /// Obtain the netmask that corresponds to the address, based on its address
  165. /// class.
  166. BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
  167. private:
  168. // The underlying IPv4 address.
  169. boost::asio::detail::in4_addr_type addr_;
  170. };
  171. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  172. /// Output an address as a string.
  173. /**
  174. * Used to output a human-readable string for a specified address.
  175. *
  176. * @param os The output stream to which the string will be written.
  177. *
  178. * @param addr The address to be written.
  179. *
  180. * @return The output stream.
  181. *
  182. * @relates boost::asio::ip::address_v4
  183. */
  184. template <typename Elem, typename Traits>
  185. std::basic_ostream<Elem, Traits>& operator<<(
  186. std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
  187. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  188. } // namespace ip
  189. } // namespace asio
  190. } // namespace boost
  191. #include <boost/asio/detail/pop_options.hpp>
  192. #include <boost/asio/ip/impl/address_v4.hpp>
  193. #if defined(BOOST_ASIO_HEADER_ONLY)
  194. # include <boost/asio/ip/impl/address_v4.ipp>
  195. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  196. #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP