File indexing completed on 2025-01-18 09:28:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP
0013 #define BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP
0014
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif
0018
0019 #include <boost/asio/detail/config.hpp>
0020 #include <climits>
0021 #include <cstdio>
0022 #include <cstdlib>
0023 #include <stdexcept>
0024 #include <boost/asio/error.hpp>
0025 #include <boost/asio/detail/throw_error.hpp>
0026 #include <boost/asio/detail/throw_exception.hpp>
0027 #include <boost/asio/ip/network_v4.hpp>
0028
0029 #include <boost/asio/detail/push_options.hpp>
0030
0031 namespace boost {
0032 namespace asio {
0033 namespace ip {
0034
0035 network_v4::network_v4(const address_v4& addr, unsigned short prefix_len)
0036 : address_(addr),
0037 prefix_length_(prefix_len)
0038 {
0039 if (prefix_len > 32)
0040 {
0041 std::out_of_range ex("prefix length too large");
0042 boost::asio::detail::throw_exception(ex);
0043 }
0044 }
0045
0046 network_v4::network_v4(const address_v4& addr, const address_v4& mask)
0047 : address_(addr),
0048 prefix_length_(0)
0049 {
0050 address_v4::bytes_type mask_bytes = mask.to_bytes();
0051 bool finished = false;
0052 for (std::size_t i = 0; i < mask_bytes.size(); ++i)
0053 {
0054 if (finished)
0055 {
0056 if (mask_bytes[i])
0057 {
0058 std::invalid_argument ex("non-contiguous netmask");
0059 boost::asio::detail::throw_exception(ex);
0060 }
0061 continue;
0062 }
0063 else
0064 {
0065 switch (mask_bytes[i])
0066 {
0067 case 255:
0068 prefix_length_ += 8;
0069 break;
0070 case 254:
0071 prefix_length_ += 1;
0072 case 252:
0073 prefix_length_ += 1;
0074 case 248:
0075 prefix_length_ += 1;
0076 case 240:
0077 prefix_length_ += 1;
0078 case 224:
0079 prefix_length_ += 1;
0080 case 192:
0081 prefix_length_ += 1;
0082 case 128:
0083 prefix_length_ += 1;
0084 case 0:
0085 finished = true;
0086 break;
0087 default:
0088 std::out_of_range ex("non-contiguous netmask");
0089 boost::asio::detail::throw_exception(ex);
0090 }
0091 }
0092 }
0093 }
0094
0095 address_v4 network_v4::netmask() const noexcept
0096 {
0097 uint32_t nmbits = 0xffffffff;
0098 if (prefix_length_ == 0)
0099 nmbits = 0;
0100 else
0101 nmbits = nmbits << (32 - prefix_length_);
0102 return address_v4(nmbits);
0103 }
0104
0105 address_v4_range network_v4::hosts() const noexcept
0106 {
0107 return is_host()
0108 ? address_v4_range(address_, address_v4(address_.to_uint() + 1))
0109 : address_v4_range(address_v4(network().to_uint() + 1), broadcast());
0110 }
0111
0112 bool network_v4::is_subnet_of(const network_v4& other) const
0113 {
0114 if (other.prefix_length_ >= prefix_length_)
0115 return false;
0116 const network_v4 me(address_, other.prefix_length_);
0117 return other.canonical() == me.canonical();
0118 }
0119
0120 std::string network_v4::to_string() const
0121 {
0122 boost::system::error_code ec;
0123 std::string addr = to_string(ec);
0124 boost::asio::detail::throw_error(ec);
0125 return addr;
0126 }
0127
0128 std::string network_v4::to_string(boost::system::error_code& ec) const
0129 {
0130 using namespace std;
0131 ec = boost::system::error_code();
0132 char prefix_len[16];
0133 #if defined(BOOST_ASIO_HAS_SNPRINTF)
0134 snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
0135 #elif defined(BOOST_ASIO_HAS_SECURE_RTL)
0136 sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
0137 #else
0138 sprintf(prefix_len, "/%u", prefix_length_);
0139 #endif
0140 return address_.to_string() + prefix_len;
0141 }
0142
0143 network_v4 make_network_v4(const char* str)
0144 {
0145 return make_network_v4(std::string(str));
0146 }
0147
0148 network_v4 make_network_v4(const char* str, boost::system::error_code& ec)
0149 {
0150 return make_network_v4(std::string(str), ec);
0151 }
0152
0153 network_v4 make_network_v4(const std::string& str)
0154 {
0155 boost::system::error_code ec;
0156 network_v4 net = make_network_v4(str, ec);
0157 boost::asio::detail::throw_error(ec);
0158 return net;
0159 }
0160
0161 network_v4 make_network_v4(const std::string& str,
0162 boost::system::error_code& ec)
0163 {
0164 std::string::size_type pos = str.find_first_of("/");
0165
0166 if (pos == std::string::npos)
0167 {
0168 ec = boost::asio::error::invalid_argument;
0169 return network_v4();
0170 }
0171
0172 if (pos == str.size() - 1)
0173 {
0174 ec = boost::asio::error::invalid_argument;
0175 return network_v4();
0176 }
0177
0178 std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
0179 if (end != std::string::npos)
0180 {
0181 ec = boost::asio::error::invalid_argument;
0182 return network_v4();
0183 }
0184
0185 const address_v4 addr = make_address_v4(str.substr(0, pos), ec);
0186 if (ec)
0187 return network_v4();
0188
0189 const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
0190 if (prefix_len < 0 || prefix_len > 32)
0191 {
0192 ec = boost::asio::error::invalid_argument;
0193 return network_v4();
0194 }
0195
0196 return network_v4(addr, static_cast<unsigned short>(prefix_len));
0197 }
0198
0199 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
0200
0201 network_v4 make_network_v4(string_view str)
0202 {
0203 return make_network_v4(static_cast<std::string>(str));
0204 }
0205
0206 network_v4 make_network_v4(string_view str,
0207 boost::system::error_code& ec)
0208 {
0209 return make_network_v4(static_cast<std::string>(str), ec);
0210 }
0211
0212 #endif
0213
0214 }
0215 }
0216 }
0217
0218 #include <boost/asio/detail/pop_options.hpp>
0219
0220 #endif