2 // ip/impl/network_v4.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP
13 #define BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
24 #include <boost/asio/error.hpp>
25 #include <boost/asio/detail/throw_error.hpp>
26 #include <boost/asio/detail/throw_exception.hpp>
27 #include <boost/asio/ip/network_v4.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 network_v4::network_v4(const address_v4& addr, unsigned short prefix_len)
37 prefix_length_(prefix_len)
41 std::out_of_range ex("prefix length too large");
42 boost::asio::detail::throw_exception(ex);
46 network_v4::network_v4(const address_v4& addr, const address_v4& mask)
50 address_v4::bytes_type mask_bytes = mask.to_bytes();
51 bool finished = false;
52 for (std::size_t i = 0; i < mask_bytes.size(); ++i)
58 std::invalid_argument ex("non-contiguous netmask");
59 boost::asio::detail::throw_exception(ex);
65 switch (mask_bytes[i])
70 case 254: // prefix_length_ += 7
72 case 252: // prefix_length_ += 6
74 case 248: // prefix_length_ += 5
76 case 240: // prefix_length_ += 4
78 case 224: // prefix_length_ += 3
80 case 192: // prefix_length_ += 2
82 case 128: // prefix_length_ += 1
88 std::out_of_range ex("non-contiguous netmask");
89 boost::asio::detail::throw_exception(ex);
95 address_v4 network_v4::netmask() const BOOST_ASIO_NOEXCEPT
97 uint32_t nmbits = 0xffffffff;
98 if (prefix_length_ == 0)
101 nmbits = nmbits << (32 - prefix_length_);
102 return address_v4(nmbits);
105 address_v4_range network_v4::hosts() const BOOST_ASIO_NOEXCEPT
108 ? address_v4_range(address_, address_v4(address_.to_uint() + 1))
109 : address_v4_range(address_v4(network().to_uint() + 1), broadcast());
112 bool network_v4::is_subnet_of(const network_v4& other) const
114 if (other.prefix_length_ >= prefix_length_)
115 return false; // Only real subsets are allowed.
116 const network_v4 me(address_, other.prefix_length_);
117 return other.canonical() == me.canonical();
120 std::string network_v4::to_string() const
122 boost::system::error_code ec;
123 std::string addr = to_string(ec);
124 boost::asio::detail::throw_error(ec);
128 std::string network_v4::to_string(boost::system::error_code& ec) const
130 using namespace std; // For sprintf.
131 ec = boost::system::error_code();
133 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
134 sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
135 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
136 sprintf(prefix_len, "/%u", prefix_length_);
137 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
138 return address_.to_string() + prefix_len;
141 network_v4 make_network_v4(const char* str)
143 return make_network_v4(std::string(str));
146 network_v4 make_network_v4(const char* str, boost::system::error_code& ec)
148 return make_network_v4(std::string(str), ec);
151 network_v4 make_network_v4(const std::string& str)
153 boost::system::error_code ec;
154 network_v4 net = make_network_v4(str, ec);
155 boost::asio::detail::throw_error(ec);
159 network_v4 make_network_v4(const std::string& str,
160 boost::system::error_code& ec)
162 std::string::size_type pos = str.find_first_of("/");
164 if (pos == std::string::npos)
166 ec = boost::asio::error::invalid_argument;
170 if (pos == str.size() - 1)
172 ec = boost::asio::error::invalid_argument;
176 std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
177 if (end != std::string::npos)
179 ec = boost::asio::error::invalid_argument;
183 const address_v4 addr = make_address_v4(str.substr(0, pos), ec);
187 const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
188 if (prefix_len < 0 || prefix_len > 32)
190 ec = boost::asio::error::invalid_argument;
194 return network_v4(addr, static_cast<unsigned short>(prefix_len));
197 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
199 network_v4 make_network_v4(string_view str)
201 return make_network_v4(static_cast<std::string>(str));
204 network_v4 make_network_v4(string_view str,
205 boost::system::error_code& ec)
207 return make_network_v4(static_cast<std::string>(str), ec);
210 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
216 #include <boost/asio/detail/pop_options.hpp>
218 #endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP