]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/ip/impl/network_v4.ipp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / asio / ip / impl / network_v4.ipp
1 //
2 // ip/impl/network_v4.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
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)
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_V4_IPP
13 #define BOOST_ASIO_IP_IMPL_NETWORK_V4_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_v4.hpp>
28
29 #include <boost/asio/detail/push_options.hpp>
30
31 namespace boost {
32 namespace asio {
33 namespace ip {
34
35 network_v4::network_v4(const address_v4& addr, unsigned short prefix_len)
36 : address_(addr),
37 prefix_length_(prefix_len)
38 {
39 if (prefix_len > 32)
40 {
41 std::out_of_range ex("prefix length too large");
42 boost::asio::detail::throw_exception(ex);
43 }
44 }
45
46 network_v4::network_v4(const address_v4& addr, const address_v4& mask)
47 : address_(addr),
48 prefix_length_(0)
49 {
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)
53 {
54 if (finished)
55 {
56 if (mask_bytes[i])
57 {
58 std::invalid_argument ex("non-contiguous netmask");
59 boost::asio::detail::throw_exception(ex);
60 }
61 continue;
62 }
63 else
64 {
65 switch (mask_bytes[i])
66 {
67 case 255:
68 prefix_length_ += 8;
69 break;
70 case 254: // prefix_length_ += 7
71 prefix_length_ += 1;
72 case 252: // prefix_length_ += 6
73 prefix_length_ += 1;
74 case 248: // prefix_length_ += 5
75 prefix_length_ += 1;
76 case 240: // prefix_length_ += 4
77 prefix_length_ += 1;
78 case 224: // prefix_length_ += 3
79 prefix_length_ += 1;
80 case 192: // prefix_length_ += 2
81 prefix_length_ += 1;
82 case 128: // prefix_length_ += 1
83 prefix_length_ += 1;
84 case 0: // nbits += 0
85 finished = true;
86 break;
87 default:
88 std::out_of_range ex("non-contiguous netmask");
89 boost::asio::detail::throw_exception(ex);
90 }
91 }
92 }
93 }
94
95 address_v4 network_v4::netmask() const BOOST_ASIO_NOEXCEPT
96 {
97 uint32_t nmbits = 0xffffffff;
98 if (prefix_length_ == 0)
99 nmbits = 0;
100 else
101 nmbits = nmbits << (32 - prefix_length_);
102 return address_v4(nmbits);
103 }
104
105 address_v4_range network_v4::hosts() const BOOST_ASIO_NOEXCEPT
106 {
107 return is_host()
108 ? address_v4_range(address_, address_v4(address_.to_uint() + 1))
109 : address_v4_range(address_v4(network().to_uint() + 1), broadcast());
110 }
111
112 bool network_v4::is_subnet_of(const network_v4& other) const
113 {
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();
118 }
119
120 std::string network_v4::to_string() const
121 {
122 boost::system::error_code ec;
123 std::string addr = to_string(ec);
124 boost::asio::detail::throw_error(ec);
125 return addr;
126 }
127
128 std::string network_v4::to_string(boost::system::error_code& ec) const
129 {
130 using namespace std; // For sprintf.
131 ec = boost::system::error_code();
132 char prefix_len[16];
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;
139 }
140
141 network_v4 make_network_v4(const char* str)
142 {
143 return make_network_v4(std::string(str));
144 }
145
146 network_v4 make_network_v4(const char* str, boost::system::error_code& ec)
147 {
148 return make_network_v4(std::string(str), ec);
149 }
150
151 network_v4 make_network_v4(const std::string& str)
152 {
153 boost::system::error_code ec;
154 network_v4 net = make_network_v4(str, ec);
155 boost::asio::detail::throw_error(ec);
156 return net;
157 }
158
159 network_v4 make_network_v4(const std::string& str,
160 boost::system::error_code& ec)
161 {
162 std::string::size_type pos = str.find_first_of("/");
163
164 if (pos == std::string::npos)
165 {
166 ec = boost::asio::error::invalid_argument;
167 return network_v4();
168 }
169
170 if (pos == str.size() - 1)
171 {
172 ec = boost::asio::error::invalid_argument;
173 return network_v4();
174 }
175
176 std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
177 if (end != std::string::npos)
178 {
179 ec = boost::asio::error::invalid_argument;
180 return network_v4();
181 }
182
183 const address_v4 addr = make_address_v4(str.substr(0, pos), ec);
184 if (ec)
185 return network_v4();
186
187 const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
188 if (prefix_len < 0 || prefix_len > 32)
189 {
190 ec = boost::asio::error::invalid_argument;
191 return network_v4();
192 }
193
194 return network_v4(addr, static_cast<unsigned short>(prefix_len));
195 }
196
197 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
198
199 network_v4 make_network_v4(string_view str)
200 {
201 return make_network_v4(static_cast<std::string>(str));
202 }
203
204 network_v4 make_network_v4(string_view str,
205 boost::system::error_code& ec)
206 {
207 return make_network_v4(static_cast<std::string>(str), ec);
208 }
209
210 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
211
212 } // namespace ip
213 } // namespace asio
214 } // namespace boost
215
216 #include <boost/asio/detail/pop_options.hpp>
217
218 #endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP