address_v4.hpp 6.4 KB

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