2 // ip/impl/network_v6.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_V6_IPP
13 #define BOOST_ASIO_IP_IMPL_NETWORK_V6_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_v6.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 network_v6::network_v6(const address_v6& 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 BOOST_ASIO_DECL address_v6 network_v6::network() const BOOST_ASIO_NOEXCEPT
48 address_v6::bytes_type bytes(address_.to_bytes());
49 for (std::size_t i = 0; i < 16; ++i)
51 if (prefix_length_ <= i * 8)
53 else if (prefix_length_ < (i + 1) * 8)
54 bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
56 return address_v6(bytes, address_.scope_id());
59 address_v6_range network_v6::hosts() const BOOST_ASIO_NOEXCEPT
61 address_v6::bytes_type begin_bytes(address_.to_bytes());
62 address_v6::bytes_type end_bytes(address_.to_bytes());
63 for (std::size_t i = 0; i < 16; ++i)
65 if (prefix_length_ <= i * 8)
70 else if (prefix_length_ < (i + 1) * 8)
72 begin_bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
73 end_bytes[i] |= 0xFF >> (prefix_length_ % 8);
76 return address_v6_range(
77 address_v6_iterator(address_v6(begin_bytes, address_.scope_id())),
78 ++address_v6_iterator(address_v6(end_bytes, address_.scope_id())));
81 bool network_v6::is_subnet_of(const network_v6& other) const
83 if (other.prefix_length_ >= prefix_length_)
84 return false; // Only real subsets are allowed.
85 const network_v6 me(address_, other.prefix_length_);
86 return other.canonical() == me.canonical();
89 std::string network_v6::to_string() const
91 boost::system::error_code ec;
92 std::string addr = to_string(ec);
93 boost::asio::detail::throw_error(ec);
97 std::string network_v6::to_string(boost::system::error_code& ec) const
99 ec = boost::system::error_code();
101 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
102 sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
103 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
104 sprintf(prefix_len, "/%u", prefix_length_);
105 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
106 return address_.to_string() + prefix_len;
109 network_v6 make_network_v6(const char* str)
111 return make_network_v6(std::string(str));
114 network_v6 make_network_v6(const char* str, boost::system::error_code& ec)
116 return make_network_v6(std::string(str), ec);
119 network_v6 make_network_v6(const std::string& str)
121 boost::system::error_code ec;
122 network_v6 net = make_network_v6(str, ec);
123 boost::asio::detail::throw_error(ec);
127 network_v6 make_network_v6(const std::string& str,
128 boost::system::error_code& ec)
130 std::string::size_type pos = str.find_first_of("/");
132 if (pos == std::string::npos)
134 ec = boost::asio::error::invalid_argument;
138 if (pos == str.size() - 1)
140 ec = boost::asio::error::invalid_argument;
144 std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
145 if (end != std::string::npos)
147 ec = boost::asio::error::invalid_argument;
151 const address_v6 addr = make_address_v6(str.substr(0, pos), ec);
155 const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
156 if (prefix_len < 0 || prefix_len > 128)
158 ec = boost::asio::error::invalid_argument;
162 return network_v6(addr, static_cast<unsigned short>(prefix_len));
165 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
167 network_v6 make_network_v6(string_view str)
169 return make_network_v6(static_cast<std::string>(str));
172 network_v6 make_network_v6(string_view str,
173 boost::system::error_code& ec)
175 return make_network_v6(static_cast<std::string>(str), ec);
178 #endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
184 #include <boost/asio/detail/pop_options.hpp>
186 #endif // BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP