address_v4.ipp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // ip/impl/address_v4.ipp
  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_IMPL_ADDRESS_V4_IPP
  11. #define BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
  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 <climits>
  17. #include <limits>
  18. #include <stdexcept>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/detail/socket_ops.hpp>
  21. #include <boost/asio/detail/throw_error.hpp>
  22. #include <boost/asio/detail/throw_exception.hpp>
  23. #include <boost/asio/ip/address_v4.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace ip {
  28. address_v4::address_v4(const address_v4::bytes_type& bytes)
  29. {
  30. #if UCHAR_MAX > 0xFF
  31. if (bytes[0] > 0xFF || bytes[1] > 0xFF
  32. || bytes[2] > 0xFF || bytes[3] > 0xFF)
  33. {
  34. std::out_of_range ex("address_v4 from bytes_type");
  35. boost::asio::detail::throw_exception(ex);
  36. }
  37. #endif // UCHAR_MAX > 0xFF
  38. using namespace std; // For memcpy.
  39. memcpy(&addr_.s_addr, bytes.data(), 4);
  40. }
  41. address_v4::address_v4(address_v4::uint_type addr)
  42. {
  43. if ((std::numeric_limits<uint_type>::max)() > 0xFFFFFFFF)
  44. {
  45. std::out_of_range ex("address_v4 from unsigned integer");
  46. boost::asio::detail::throw_exception(ex);
  47. }
  48. addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(
  49. static_cast<boost::asio::detail::u_long_type>(addr));
  50. }
  51. address_v4::bytes_type address_v4::to_bytes() const BOOST_ASIO_NOEXCEPT
  52. {
  53. using namespace std; // For memcpy.
  54. bytes_type bytes;
  55. #if defined(BOOST_ASIO_HAS_STD_ARRAY)
  56. memcpy(bytes.data(), &addr_.s_addr, 4);
  57. #else // defined(BOOST_ASIO_HAS_STD_ARRAY)
  58. memcpy(bytes.elems, &addr_.s_addr, 4);
  59. #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
  60. return bytes;
  61. }
  62. address_v4::uint_type address_v4::to_uint() const BOOST_ASIO_NOEXCEPT
  63. {
  64. return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  65. }
  66. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  67. unsigned long address_v4::to_ulong() const
  68. {
  69. return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  70. }
  71. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  72. std::string address_v4::to_string() const
  73. {
  74. boost::system::error_code ec;
  75. char addr_str[boost::asio::detail::max_addr_v4_str_len];
  76. const char* addr =
  77. boost::asio::detail::socket_ops::inet_ntop(
  78. BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  79. boost::asio::detail::max_addr_v4_str_len, 0, ec);
  80. if (addr == 0)
  81. boost::asio::detail::throw_error(ec);
  82. return addr;
  83. }
  84. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  85. std::string address_v4::to_string(boost::system::error_code& ec) const
  86. {
  87. char addr_str[boost::asio::detail::max_addr_v4_str_len];
  88. const char* addr =
  89. boost::asio::detail::socket_ops::inet_ntop(
  90. BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  91. boost::asio::detail::max_addr_v4_str_len, 0, ec);
  92. if (addr == 0)
  93. return std::string();
  94. return addr;
  95. }
  96. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  97. bool address_v4::is_loopback() const BOOST_ASIO_NOEXCEPT
  98. {
  99. return (to_uint() & 0xFF000000) == 0x7F000000;
  100. }
  101. bool address_v4::is_unspecified() const BOOST_ASIO_NOEXCEPT
  102. {
  103. return to_uint() == 0;
  104. }
  105. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  106. bool address_v4::is_class_a() const
  107. {
  108. return (to_uint() & 0x80000000) == 0;
  109. }
  110. bool address_v4::is_class_b() const
  111. {
  112. return (to_uint() & 0xC0000000) == 0x80000000;
  113. }
  114. bool address_v4::is_class_c() const
  115. {
  116. return (to_uint() & 0xE0000000) == 0xC0000000;
  117. }
  118. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  119. bool address_v4::is_multicast() const BOOST_ASIO_NOEXCEPT
  120. {
  121. return (to_uint() & 0xF0000000) == 0xE0000000;
  122. }
  123. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  124. address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
  125. {
  126. return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF));
  127. }
  128. address_v4 address_v4::netmask(const address_v4& addr)
  129. {
  130. if (addr.is_class_a())
  131. return address_v4(0xFF000000);
  132. if (addr.is_class_b())
  133. return address_v4(0xFFFF0000);
  134. if (addr.is_class_c())
  135. return address_v4(0xFFFFFF00);
  136. return address_v4(0xFFFFFFFF);
  137. }
  138. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  139. address_v4 make_address_v4(const char* str)
  140. {
  141. boost::system::error_code ec;
  142. address_v4 addr = make_address_v4(str, ec);
  143. boost::asio::detail::throw_error(ec);
  144. return addr;
  145. }
  146. address_v4 make_address_v4(const char* str,
  147. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
  148. {
  149. address_v4::bytes_type bytes;
  150. if (boost::asio::detail::socket_ops::inet_pton(
  151. BOOST_ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0)
  152. return address_v4();
  153. return address_v4(bytes);
  154. }
  155. address_v4 make_address_v4(const std::string& str)
  156. {
  157. return make_address_v4(str.c_str());
  158. }
  159. address_v4 make_address_v4(const std::string& str,
  160. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
  161. {
  162. return make_address_v4(str.c_str(), ec);
  163. }
  164. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  165. address_v4 make_address_v4(string_view str)
  166. {
  167. return make_address_v4(static_cast<std::string>(str));
  168. }
  169. address_v4 make_address_v4(string_view str,
  170. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
  171. {
  172. return make_address_v4(static_cast<std::string>(str), ec);
  173. }
  174. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  175. } // namespace ip
  176. } // namespace asio
  177. } // namespace boost
  178. #include <boost/asio/detail/pop_options.hpp>
  179. #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP