address_v4.ipp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // ip/impl/address_v4.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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. std::string address_v4::to_string() const
  63. {
  64. boost::system::error_code ec;
  65. char addr_str[boost::asio::detail::max_addr_v4_str_len];
  66. const char* addr =
  67. boost::asio::detail::socket_ops::inet_ntop(
  68. BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  69. boost::asio::detail::max_addr_v4_str_len, 0, ec);
  70. if (addr == 0)
  71. boost::asio::detail::throw_error(ec);
  72. return addr;
  73. }
  74. bool address_v4::is_loopback() const noexcept
  75. {
  76. return (to_uint() & 0xFF000000) == 0x7F000000;
  77. }
  78. bool address_v4::is_unspecified() const noexcept
  79. {
  80. return to_uint() == 0;
  81. }
  82. bool address_v4::is_multicast() const noexcept
  83. {
  84. return (to_uint() & 0xF0000000) == 0xE0000000;
  85. }
  86. address_v4 make_address_v4(const char* str)
  87. {
  88. boost::system::error_code ec;
  89. address_v4 addr = make_address_v4(str, ec);
  90. boost::asio::detail::throw_error(ec);
  91. return addr;
  92. }
  93. address_v4 make_address_v4(const char* str,
  94. boost::system::error_code& ec) noexcept
  95. {
  96. address_v4::bytes_type bytes;
  97. if (boost::asio::detail::socket_ops::inet_pton(
  98. BOOST_ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0)
  99. return address_v4();
  100. return address_v4(bytes);
  101. }
  102. address_v4 make_address_v4(const std::string& str)
  103. {
  104. return make_address_v4(str.c_str());
  105. }
  106. address_v4 make_address_v4(const std::string& str,
  107. boost::system::error_code& ec) noexcept
  108. {
  109. return make_address_v4(str.c_str(), ec);
  110. }
  111. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  112. address_v4 make_address_v4(string_view str)
  113. {
  114. return make_address_v4(static_cast<std::string>(str));
  115. }
  116. address_v4 make_address_v4(string_view str,
  117. boost::system::error_code& ec) noexcept
  118. {
  119. return make_address_v4(static_cast<std::string>(str), ec);
  120. }
  121. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  122. } // namespace ip
  123. } // namespace asio
  124. } // namespace boost
  125. #include <boost/asio/detail/pop_options.hpp>
  126. #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP