]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/ip/impl/address_v4.ipp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / asio / ip / impl / address_v4.ipp
1 //
2 // ip/impl/address_v4.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
12 #define BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <climits>
20 #include <limits>
21 #include <stdexcept>
22 #include <boost/asio/error.hpp>
23 #include <boost/asio/detail/socket_ops.hpp>
24 #include <boost/asio/detail/throw_error.hpp>
25 #include <boost/asio/detail/throw_exception.hpp>
26 #include <boost/asio/ip/address_v4.hpp>
27
28 #include <boost/asio/detail/push_options.hpp>
29
30 namespace boost {
31 namespace asio {
32 namespace ip {
33
34 address_v4::address_v4(const address_v4::bytes_type& bytes)
35 {
36 #if UCHAR_MAX > 0xFF
37 if (bytes[0] > 0xFF || bytes[1] > 0xFF
38 || bytes[2] > 0xFF || bytes[3] > 0xFF)
39 {
40 std::out_of_range ex("address_v4 from bytes_type");
41 boost::asio::detail::throw_exception(ex);
42 }
43 #endif // UCHAR_MAX > 0xFF
44
45 using namespace std; // For memcpy.
46 memcpy(&addr_.s_addr, bytes.data(), 4);
47 }
48
49 address_v4::address_v4(address_v4::uint_type addr)
50 {
51 if ((std::numeric_limits<uint_type>::max)() > 0xFFFFFFFF)
52 {
53 std::out_of_range ex("address_v4 from unsigned integer");
54 boost::asio::detail::throw_exception(ex);
55 }
56
57 addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(
58 static_cast<boost::asio::detail::u_long_type>(addr));
59 }
60
61 address_v4::bytes_type address_v4::to_bytes() const BOOST_ASIO_NOEXCEPT
62 {
63 using namespace std; // For memcpy.
64 bytes_type bytes;
65 #if defined(BOOST_ASIO_HAS_STD_ARRAY)
66 memcpy(bytes.data(), &addr_.s_addr, 4);
67 #else // defined(BOOST_ASIO_HAS_STD_ARRAY)
68 memcpy(bytes.elems, &addr_.s_addr, 4);
69 #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
70 return bytes;
71 }
72
73 address_v4::uint_type address_v4::to_uint() const BOOST_ASIO_NOEXCEPT
74 {
75 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
76 }
77
78 #if !defined(BOOST_ASIO_NO_DEPRECATED)
79 unsigned long address_v4::to_ulong() const
80 {
81 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
82 }
83 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
84
85 std::string address_v4::to_string() const
86 {
87 boost::system::error_code ec;
88 char addr_str[boost::asio::detail::max_addr_v4_str_len];
89 const char* addr =
90 boost::asio::detail::socket_ops::inet_ntop(
91 BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
92 boost::asio::detail::max_addr_v4_str_len, 0, ec);
93 if (addr == 0)
94 boost::asio::detail::throw_error(ec);
95 return addr;
96 }
97
98 #if !defined(BOOST_ASIO_NO_DEPRECATED)
99 std::string address_v4::to_string(boost::system::error_code& ec) const
100 {
101 char addr_str[boost::asio::detail::max_addr_v4_str_len];
102 const char* addr =
103 boost::asio::detail::socket_ops::inet_ntop(
104 BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
105 boost::asio::detail::max_addr_v4_str_len, 0, ec);
106 if (addr == 0)
107 return std::string();
108 return addr;
109 }
110 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
111
112 bool address_v4::is_loopback() const BOOST_ASIO_NOEXCEPT
113 {
114 return (to_uint() & 0xFF000000) == 0x7F000000;
115 }
116
117 bool address_v4::is_unspecified() const BOOST_ASIO_NOEXCEPT
118 {
119 return to_uint() == 0;
120 }
121
122 #if !defined(BOOST_ASIO_NO_DEPRECATED)
123 bool address_v4::is_class_a() const
124 {
125 return (to_uint() & 0x80000000) == 0;
126 }
127
128 bool address_v4::is_class_b() const
129 {
130 return (to_uint() & 0xC0000000) == 0x80000000;
131 }
132
133 bool address_v4::is_class_c() const
134 {
135 return (to_uint() & 0xE0000000) == 0xC0000000;
136 }
137 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
138
139 bool address_v4::is_multicast() const BOOST_ASIO_NOEXCEPT
140 {
141 return (to_uint() & 0xF0000000) == 0xE0000000;
142 }
143
144 #if !defined(BOOST_ASIO_NO_DEPRECATED)
145 address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
146 {
147 return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF));
148 }
149
150 address_v4 address_v4::netmask(const address_v4& addr)
151 {
152 if (addr.is_class_a())
153 return address_v4(0xFF000000);
154 if (addr.is_class_b())
155 return address_v4(0xFFFF0000);
156 if (addr.is_class_c())
157 return address_v4(0xFFFFFF00);
158 return address_v4(0xFFFFFFFF);
159 }
160 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
161
162 address_v4 make_address_v4(const char* str)
163 {
164 boost::system::error_code ec;
165 address_v4 addr = make_address_v4(str, ec);
166 boost::asio::detail::throw_error(ec);
167 return addr;
168 }
169
170 address_v4 make_address_v4(const char* str,
171 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
172 {
173 address_v4::bytes_type bytes;
174 if (boost::asio::detail::socket_ops::inet_pton(
175 BOOST_ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0)
176 return address_v4();
177 return address_v4(bytes);
178 }
179
180 address_v4 make_address_v4(const std::string& str)
181 {
182 return make_address_v4(str.c_str());
183 }
184
185 address_v4 make_address_v4(const std::string& str,
186 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
187 {
188 return make_address_v4(str.c_str(), ec);
189 }
190
191 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
192
193 address_v4 make_address_v4(string_view str)
194 {
195 return make_address_v4(static_cast<std::string>(str));
196 }
197
198 address_v4 make_address_v4(string_view str,
199 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
200 {
201 return make_address_v4(static_cast<std::string>(str), ec);
202 }
203
204 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
205
206 } // namespace ip
207 } // namespace asio
208 } // namespace boost
209
210 #include <boost/asio/detail/pop_options.hpp>
211
212 #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP