File indexing completed on 2025-01-18 09:28:59
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 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0075 unsigned long address_v4::to_ulong() const
0076 {
0077 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
0078 }
0079 #endif
0080
0081 std::string address_v4::to_string() const
0082 {
0083 boost::system::error_code ec;
0084 char addr_str[boost::asio::detail::max_addr_v4_str_len];
0085 const char* addr =
0086 boost::asio::detail::socket_ops::inet_ntop(
0087 BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
0088 boost::asio::detail::max_addr_v4_str_len, 0, ec);
0089 if (addr == 0)
0090 boost::asio::detail::throw_error(ec);
0091 return addr;
0092 }
0093
0094 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0095 std::string address_v4::to_string(boost::system::error_code& ec) const
0096 {
0097 char addr_str[boost::asio::detail::max_addr_v4_str_len];
0098 const char* addr =
0099 boost::asio::detail::socket_ops::inet_ntop(
0100 BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
0101 boost::asio::detail::max_addr_v4_str_len, 0, ec);
0102 if (addr == 0)
0103 return std::string();
0104 return addr;
0105 }
0106 #endif
0107
0108 bool address_v4::is_loopback() const noexcept
0109 {
0110 return (to_uint() & 0xFF000000) == 0x7F000000;
0111 }
0112
0113 bool address_v4::is_unspecified() const noexcept
0114 {
0115 return to_uint() == 0;
0116 }
0117
0118 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0119 bool address_v4::is_class_a() const
0120 {
0121 return (to_uint() & 0x80000000) == 0;
0122 }
0123
0124 bool address_v4::is_class_b() const
0125 {
0126 return (to_uint() & 0xC0000000) == 0x80000000;
0127 }
0128
0129 bool address_v4::is_class_c() const
0130 {
0131 return (to_uint() & 0xE0000000) == 0xC0000000;
0132 }
0133 #endif
0134
0135 bool address_v4::is_multicast() const noexcept
0136 {
0137 return (to_uint() & 0xF0000000) == 0xE0000000;
0138 }
0139
0140 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0141 address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
0142 {
0143 return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF));
0144 }
0145
0146 address_v4 address_v4::netmask(const address_v4& addr)
0147 {
0148 if (addr.is_class_a())
0149 return address_v4(0xFF000000);
0150 if (addr.is_class_b())
0151 return address_v4(0xFFFF0000);
0152 if (addr.is_class_c())
0153 return address_v4(0xFFFFFF00);
0154 return address_v4(0xFFFFFFFF);
0155 }
0156 #endif
0157
0158 address_v4 make_address_v4(const char* str)
0159 {
0160 boost::system::error_code ec;
0161 address_v4 addr = make_address_v4(str, ec);
0162 boost::asio::detail::throw_error(ec);
0163 return addr;
0164 }
0165
0166 address_v4 make_address_v4(const char* str,
0167 boost::system::error_code& ec) noexcept
0168 {
0169 address_v4::bytes_type bytes;
0170 if (boost::asio::detail::socket_ops::inet_pton(
0171 BOOST_ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0)
0172 return address_v4();
0173 return address_v4(bytes);
0174 }
0175
0176 address_v4 make_address_v4(const std::string& str)
0177 {
0178 return make_address_v4(str.c_str());
0179 }
0180
0181 address_v4 make_address_v4(const std::string& str,
0182 boost::system::error_code& ec) noexcept
0183 {
0184 return make_address_v4(str.c_str(), ec);
0185 }
0186
0187 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
0188
0189 address_v4 make_address_v4(string_view str)
0190 {
0191 return make_address_v4(static_cast<std::string>(str));
0192 }
0193
0194 address_v4 make_address_v4(string_view str,
0195 boost::system::error_code& ec) noexcept
0196 {
0197 return make_address_v4(static_cast<std::string>(str), ec);
0198 }
0199
0200 #endif
0201
0202 }
0203 }
0204 }
0205
0206 #include <boost/asio/detail/pop_options.hpp>
0207
0208 #endif