address_v4.ipp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // ip/impl/address_v4.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 noexcept
  52. {
  53. using namespace std; // For memcpy.
  54. bytes_type bytes;
  55. memcpy(bytes.data(), &addr_.s_addr, 4);
  56. return bytes;
  57. }
  58. address_v4::uint_type address_v4::to_uint() const noexcept
  59. {
  60. return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  61. }
  62. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  63. unsigned long address_v4::to_ulong() const
  64. {
  65. return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  66. }
  67. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  68. std::string address_v4::to_string() const
  69. {
  70. boost::system::error_code ec;
  71. char addr_str[boost::asio::detail::max_addr_v4_str_len];
  72. const char* addr =
  73. boost::asio::detail::socket_ops::inet_ntop(
  74. BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  75. boost::asio::detail::max_addr_v4_str_len, 0, ec);
  76. if (addr == 0)
  77. boost::asio::detail::throw_error(ec);
  78. return addr;
  79. }
  80. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  81. std::string address_v4::to_string(boost::system::error_code& ec) const
  82. {
  83. char addr_str[boost::asio::detail::max_addr_v4_str_len];
  84. const char* addr =
  85. boost::asio::detail::socket_ops::inet_ntop(
  86. BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  87. boost::asio::detail::max_addr_v4_str_len, 0, ec);
  88. if (addr == 0)
  89. return std::string();
  90. return addr;
  91. }
  92. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  93. bool address_v4::is_loopback() const noexcept
  94. {
  95. return (to_uint() & 0xFF000000) == 0x7F000000;
  96. }
  97. bool address_v4::is_unspecified() const noexcept
  98. {
  99. return to_uint() == 0;
  100. }
  101. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  102. bool address_v4::is_class_a() const
  103. {
  104. return (to_uint() & 0x80000000) == 0;
  105. }
  106. bool address_v4::is_class_b() const
  107. {
  108. return (to_uint() & 0xC0000000) == 0x80000000;
  109. }
  110. bool address_v4::is_class_c() const
  111. {
  112. return (to_uint() & 0xE0000000) == 0xC0000000;
  113. }
  114. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  115. bool address_v4::is_multicast() const noexcept
  116. {
  117. return (to_uint() & 0xF0000000) == 0xE0000000;
  118. }
  119. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  120. address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
  121. {
  122. return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF));
  123. }
  124. address_v4 address_v4::netmask(const address_v4& addr)
  125. {
  126. if (addr.is_class_a())
  127. return address_v4(0xFF000000);
  128. if (addr.is_class_b())
  129. return address_v4(0xFFFF0000);
  130. if (addr.is_class_c())
  131. return address_v4(0xFFFFFF00);
  132. return address_v4(0xFFFFFFFF);
  133. }
  134. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  135. address_v4 make_address_v4(const char* str)
  136. {
  137. boost::system::error_code ec;
  138. address_v4 addr = make_address_v4(str, ec);
  139. boost::asio::detail::throw_error(ec);
  140. return addr;
  141. }
  142. address_v4 make_address_v4(const char* str,
  143. boost::system::error_code& ec) noexcept
  144. {
  145. address_v4::bytes_type bytes;
  146. if (boost::asio::detail::socket_ops::inet_pton(
  147. BOOST_ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0)
  148. return address_v4();
  149. return address_v4(bytes);
  150. }
  151. address_v4 make_address_v4(const std::string& str)
  152. {
  153. return make_address_v4(str.c_str());
  154. }
  155. address_v4 make_address_v4(const std::string& str,
  156. boost::system::error_code& ec) noexcept
  157. {
  158. return make_address_v4(str.c_str(), ec);
  159. }
  160. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  161. address_v4 make_address_v4(string_view str)
  162. {
  163. return make_address_v4(static_cast<std::string>(str));
  164. }
  165. address_v4 make_address_v4(string_view str,
  166. boost::system::error_code& ec) noexcept
  167. {
  168. return make_address_v4(static_cast<std::string>(str), ec);
  169. }
  170. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  171. } // namespace ip
  172. } // namespace asio
  173. } // namespace boost
  174. #include <boost/asio/detail/pop_options.hpp>
  175. #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP