address.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // ip/address.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_HPP
  11. #define BOOST_ASIO_IP_ADDRESS_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/system/error_code.hpp>
  18. #include <boost/asio/ip/address_v4.hpp>
  19. #include <boost/asio/ip/address_v6.hpp>
  20. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  21. # include <iosfwd>
  22. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace ip {
  27. /// Implements version-independent IP addresses.
  28. /**
  29. * The boost::asio::ip::address class provides the ability to use either IP
  30. * version 4 or version 6 addresses.
  31. *
  32. * @par Thread Safety
  33. * @e Distinct @e objects: Safe.@n
  34. * @e Shared @e objects: Unsafe.
  35. */
  36. class address
  37. {
  38. public:
  39. /// Default constructor.
  40. BOOST_ASIO_DECL address();
  41. /// Construct an address from an IPv4 address.
  42. BOOST_ASIO_DECL address(const boost::asio::ip::address_v4& ipv4_address);
  43. /// Construct an address from an IPv6 address.
  44. BOOST_ASIO_DECL address(const boost::asio::ip::address_v6& ipv6_address);
  45. /// Copy constructor.
  46. BOOST_ASIO_DECL address(const address& other);
  47. #if defined(BOOST_ASIO_HAS_MOVE)
  48. /// Move constructor.
  49. BOOST_ASIO_DECL address(address&& other);
  50. #endif // defined(BOOST_ASIO_HAS_MOVE)
  51. /// Assign from another address.
  52. BOOST_ASIO_DECL address& operator=(const address& other);
  53. #if defined(BOOST_ASIO_HAS_MOVE)
  54. /// Move-assign from another address.
  55. BOOST_ASIO_DECL address& operator=(address&& other);
  56. #endif // defined(BOOST_ASIO_HAS_MOVE)
  57. /// Assign from an IPv4 address.
  58. BOOST_ASIO_DECL address& operator=(
  59. const boost::asio::ip::address_v4& ipv4_address);
  60. /// Assign from an IPv6 address.
  61. BOOST_ASIO_DECL address& operator=(
  62. const boost::asio::ip::address_v6& ipv6_address);
  63. /// Get whether the address is an IP version 4 address.
  64. bool is_v4() const
  65. {
  66. return type_ == ipv4;
  67. }
  68. /// Get whether the address is an IP version 6 address.
  69. bool is_v6() const
  70. {
  71. return type_ == ipv6;
  72. }
  73. /// Get the address as an IP version 4 address.
  74. BOOST_ASIO_DECL boost::asio::ip::address_v4 to_v4() const;
  75. /// Get the address as an IP version 6 address.
  76. BOOST_ASIO_DECL boost::asio::ip::address_v6 to_v6() const;
  77. /// Get the address as a string in dotted decimal format.
  78. BOOST_ASIO_DECL std::string to_string() const;
  79. /// Get the address as a string in dotted decimal format.
  80. BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
  81. /// Create an address from an IPv4 address string in dotted decimal form,
  82. /// or from an IPv6 address in hexadecimal notation.
  83. BOOST_ASIO_DECL static address from_string(const char* str);
  84. /// Create an address from an IPv4 address string in dotted decimal form,
  85. /// or from an IPv6 address in hexadecimal notation.
  86. BOOST_ASIO_DECL static address from_string(
  87. const char* str, boost::system::error_code& ec);
  88. /// Create an address from an IPv4 address string in dotted decimal form,
  89. /// or from an IPv6 address in hexadecimal notation.
  90. BOOST_ASIO_DECL static address from_string(const std::string& str);
  91. /// Create an address from an IPv4 address string in dotted decimal form,
  92. /// or from an IPv6 address in hexadecimal notation.
  93. BOOST_ASIO_DECL static address from_string(
  94. const std::string& str, boost::system::error_code& ec);
  95. /// Determine whether the address is a loopback address.
  96. BOOST_ASIO_DECL bool is_loopback() const;
  97. /// Determine whether the address is unspecified.
  98. BOOST_ASIO_DECL bool is_unspecified() const;
  99. /// Determine whether the address is a multicast address.
  100. BOOST_ASIO_DECL bool is_multicast() const;
  101. /// Compare two addresses for equality.
  102. BOOST_ASIO_DECL friend bool operator==(const address& a1, const address& a2);
  103. /// Compare two addresses for inequality.
  104. friend bool operator!=(const address& a1, const address& a2)
  105. {
  106. return !(a1 == a2);
  107. }
  108. /// Compare addresses for ordering.
  109. BOOST_ASIO_DECL friend bool operator<(const address& a1, const address& a2);
  110. /// Compare addresses for ordering.
  111. friend bool operator>(const address& a1, const address& a2)
  112. {
  113. return a2 < a1;
  114. }
  115. /// Compare addresses for ordering.
  116. friend bool operator<=(const address& a1, const address& a2)
  117. {
  118. return !(a2 < a1);
  119. }
  120. /// Compare addresses for ordering.
  121. friend bool operator>=(const address& a1, const address& a2)
  122. {
  123. return !(a1 < a2);
  124. }
  125. private:
  126. // The type of the address.
  127. enum { ipv4, ipv6 } type_;
  128. // The underlying IPv4 address.
  129. boost::asio::ip::address_v4 ipv4_address_;
  130. // The underlying IPv6 address.
  131. boost::asio::ip::address_v6 ipv6_address_;
  132. };
  133. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  134. /// Output an address as a string.
  135. /**
  136. * Used to output a human-readable string for a specified address.
  137. *
  138. * @param os The output stream to which the string will be written.
  139. *
  140. * @param addr The address to be written.
  141. *
  142. * @return The output stream.
  143. *
  144. * @relates boost::asio::ip::address
  145. */
  146. template <typename Elem, typename Traits>
  147. std::basic_ostream<Elem, Traits>& operator<<(
  148. std::basic_ostream<Elem, Traits>& os, const address& addr);
  149. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  150. } // namespace ip
  151. } // namespace asio
  152. } // namespace boost
  153. #include <boost/asio/detail/pop_options.hpp>
  154. #include <boost/asio/ip/impl/address.hpp>
  155. #if defined(BOOST_ASIO_HEADER_ONLY)
  156. # include <boost/asio/ip/impl/address.ipp>
  157. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  158. #endif // BOOST_ASIO_IP_ADDRESS_HPP