File indexing completed on 2025-12-16 09:43:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
0012 #define BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019 #include <climits>
0020 #include <limits>
0021 #include <stdexcept>
0022 #include <boost/asio/error.hpp>
0023 #include <boost/asio/detail/socket_ops.hpp>
0024 #include <boost/asio/detail/throw_error.hpp>
0025 #include <boost/asio/detail/throw_exception.hpp>
0026 #include <boost/asio/ip/address_v4.hpp>
0027
0028 #include <boost/asio/detail/push_options.hpp>
0029
0030 namespace boost {
0031 namespace asio {
0032 namespace ip {
0033
0034 address_v4::address_v4(const address_v4::bytes_type& bytes)
0035 {
0036 #if UCHAR_MAX > 0xFF
0037 if (bytes[0] > 0xFF || bytes[1] > 0xFF
0038 || bytes[2] > 0xFF || bytes[3] > 0xFF)
0039 {
0040 std::out_of_range ex("address_v4 from bytes_type");
0041 boost::asio::detail::throw_exception(ex);
0042 }
0043 #endif
0044
0045 using namespace std;
0046 memcpy(&addr_.s_addr, bytes.data(), 4);
0047 }
0048
0049 address_v4::address_v4(address_v4::uint_type addr)
0050 {
0051 if ((std::numeric_limits<uint_type>::max)() > 0xFFFFFFFF)
0052 {
0053 std::out_of_range ex("address_v4 from unsigned integer");
0054 boost::asio::detail::throw_exception(ex);
0055 }
0056
0057 addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(
0058 static_cast<boost::asio::detail::u_long_type>(addr));
0059 }
0060
0061 address_v4::bytes_type address_v4::to_bytes() const noexcept
0062 {
0063 using namespace std;
0064 bytes_type bytes;
0065 memcpy(bytes.data(), &addr_.s_addr, 4);
0066 return bytes;
0067 }
0068
0069 address_v4::uint_type address_v4::to_uint() const noexcept
0070 {
0071 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
0072 }
0073
0074 std::string address_v4::to_string() const
0075 {
0076 boost::system::error_code ec;
0077 char addr_str[boost::asio::detail::max_addr_v4_str_len];
0078 const char* addr =
0079 boost::asio::detail::socket_ops::inet_ntop(
0080 BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
0081 boost::asio::detail::max_addr_v4_str_len, 0, ec);
0082 if (addr == 0)
0083 boost::asio::detail::throw_error(ec);
0084 return addr;
0085 }
0086
0087 bool address_v4::is_loopback() const noexcept
0088 {
0089 return (to_uint() & 0xFF000000) == 0x7F000000;
0090 }
0091
0092 bool address_v4::is_unspecified() const noexcept
0093 {
0094 return to_uint() == 0;
0095 }
0096
0097 bool address_v4::is_multicast() const noexcept
0098 {
0099 return (to_uint() & 0xF0000000) == 0xE0000000;
0100 }
0101
0102 address_v4 make_address_v4(const char* str)
0103 {
0104 boost::system::error_code ec;
0105 address_v4 addr = make_address_v4(str, ec);
0106 boost::asio::detail::throw_error(ec);
0107 return addr;
0108 }
0109
0110 address_v4 make_address_v4(const char* str,
0111 boost::system::error_code& ec) noexcept
0112 {
0113 address_v4::bytes_type bytes;
0114 if (boost::asio::detail::socket_ops::inet_pton(
0115 BOOST_ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0)
0116 return address_v4();
0117 return address_v4(bytes);
0118 }
0119
0120 address_v4 make_address_v4(const std::string& str)
0121 {
0122 return make_address_v4(str.c_str());
0123 }
0124
0125 address_v4 make_address_v4(const std::string& str,
0126 boost::system::error_code& ec) noexcept
0127 {
0128 return make_address_v4(str.c_str(), ec);
0129 }
0130
0131 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
0132
0133 address_v4 make_address_v4(string_view str)
0134 {
0135 return make_address_v4(static_cast<std::string>(str));
0136 }
0137
0138 address_v4 make_address_v4(string_view str,
0139 boost::system::error_code& ec) noexcept
0140 {
0141 return make_address_v4(static_cast<std::string>(str), ec);
0142 }
0143
0144 #endif
0145
0146 }
0147 }
0148 }
0149
0150 #include <boost/asio/detail/pop_options.hpp>
0151
0152 #endif