]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/ip/impl/network_v6.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / ip / impl / network_v6.ipp
1 //
2 // ip/impl/network_v6.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
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)
7 //
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)
10 //
11
12 #ifndef BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP
13 #define BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP
14
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
19 #include <boost/asio/detail/config.hpp>
20 #include <climits>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <stdexcept>
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>
28
29 #include <boost/asio/detail/push_options.hpp>
30
31 namespace boost {
32 namespace asio {
33 namespace ip {
34
35 network_v6::network_v6(const address_v6& addr, unsigned short prefix_len)
36 : address_(addr),
37 prefix_length_(prefix_len)
38 {
39 if (prefix_len > 128)
40 {
41 std::out_of_range ex("prefix length too large");
42 boost::asio::detail::throw_exception(ex);
43 }
44 }
45
46 BOOST_ASIO_DECL address_v6 network_v6::network() const BOOST_ASIO_NOEXCEPT
47 {
48 address_v6::bytes_type bytes(address_.to_bytes());
49 for (std::size_t i = 0; i < 16; ++i)
50 {
51 if (prefix_length_ <= i * 8)
52 bytes[i] = 0;
53 else if (prefix_length_ < (i + 1) * 8)
54 bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
55 }
56 return address_v6(bytes, address_.scope_id());
57 }
58
59 address_v6_range network_v6::hosts() const BOOST_ASIO_NOEXCEPT
60 {
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)
64 {
65 if (prefix_length_ <= i * 8)
66 {
67 begin_bytes[i] = 0;
68 end_bytes[i] = 0xFF;
69 }
70 else if (prefix_length_ < (i + 1) * 8)
71 {
72 begin_bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
73 end_bytes[i] |= 0xFF >> (prefix_length_ % 8);
74 }
75 }
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())));
79 }
80
81 bool network_v6::is_subnet_of(const network_v6& other) const
82 {
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();
87 }
88
89 std::string network_v6::to_string() const
90 {
91 boost::system::error_code ec;
92 std::string addr = to_string(ec);
93 boost::asio::detail::throw_error(ec);
94 return addr;
95 }
96
97 std::string network_v6::to_string(boost::system::error_code& ec) const
98 {
99 ec = boost::system::error_code();
100 char prefix_len[16];
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;
107 }
108
109 network_v6 make_network_v6(const char* str)
110 {
111 return make_network_v6(std::string(str));
112 }
113
114 network_v6 make_network_v6(const char* str, boost::system::error_code& ec)
115 {
116 return make_network_v6(std::string(str), ec);
117 }
118
119 network_v6 make_network_v6(const std::string& str)
120 {
121 boost::system::error_code ec;
122 network_v6 net = make_network_v6(str, ec);
123 boost::asio::detail::throw_error(ec);
124 return net;
125 }
126
127 network_v6 make_network_v6(const std::string& str,
128 boost::system::error_code& ec)
129 {
130 std::string::size_type pos = str.find_first_of("/");
131
132 if (pos == std::string::npos)
133 {
134 ec = boost::asio::error::invalid_argument;
135 return network_v6();
136 }
137
138 if (pos == str.size() - 1)
139 {
140 ec = boost::asio::error::invalid_argument;
141 return network_v6();
142 }
143
144 std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
145 if (end != std::string::npos)
146 {
147 ec = boost::asio::error::invalid_argument;
148 return network_v6();
149 }
150
151 const address_v6 addr = make_address_v6(str.substr(0, pos), ec);
152 if (ec)
153 return network_v6();
154
155 const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
156 if (prefix_len < 0 || prefix_len > 128)
157 {
158 ec = boost::asio::error::invalid_argument;
159 return network_v6();
160 }
161
162 return network_v6(addr, static_cast<unsigned short>(prefix_len));
163 }
164
165 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
166
167 network_v6 make_network_v6(string_view str)
168 {
169 return make_network_v6(static_cast<std::string>(str));
170 }
171
172 network_v6 make_network_v6(string_view str,
173 boost::system::error_code& ec)
174 {
175 return make_network_v6(static_cast<std::string>(str), ec);
176 }
177
178 #endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
179
180 } // namespace ip
181 } // namespace asio
182 } // namespace boost
183
184 #include <boost/asio/detail/pop_options.hpp>
185
186 #endif // BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP