]> git.proxmox.com Git - ceph.git/blame - 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
CommitLineData
7c673cae
FG
1//
2// ip/impl/address_v4.ipp
3// ~~~~~~~~~~~~~~~~~~~~~~
4//
92f5a8d4 5// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7c673cae
FG
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>
b32b8144 20#include <limits>
7c673cae
FG
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
30namespace boost {
31namespace asio {
32namespace ip {
33
34address_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
b32b8144 49address_v4::address_v4(address_v4::uint_type addr)
7c673cae 50{
b32b8144 51 if ((std::numeric_limits<uint_type>::max)() > 0xFFFFFFFF)
7c673cae 52 {
b32b8144 53 std::out_of_range ex("address_v4 from unsigned integer");
7c673cae
FG
54 boost::asio::detail::throw_exception(ex);
55 }
7c673cae
FG
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
92f5a8d4 61address_v4::bytes_type address_v4::to_bytes() const BOOST_ASIO_NOEXCEPT
7c673cae
FG
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
92f5a8d4 73address_v4::uint_type address_v4::to_uint() const BOOST_ASIO_NOEXCEPT
b32b8144
FG
74{
75 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
76}
77
78#if !defined(BOOST_ASIO_NO_DEPRECATED)
7c673cae
FG
79unsigned long address_v4::to_ulong() const
80{
81 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
82}
b32b8144 83#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
7c673cae
FG
84
85std::string address_v4::to_string() const
86{
87 boost::system::error_code ec;
b32b8144
FG
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);
7c673cae
FG
95 return addr;
96}
97
b32b8144 98#if !defined(BOOST_ASIO_NO_DEPRECATED)
7c673cae
FG
99std::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}
b32b8144 110#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
7c673cae 111
92f5a8d4 112bool address_v4::is_loopback() const BOOST_ASIO_NOEXCEPT
7c673cae 113{
b32b8144 114 return (to_uint() & 0xFF000000) == 0x7F000000;
7c673cae
FG
115}
116
92f5a8d4 117bool address_v4::is_unspecified() const BOOST_ASIO_NOEXCEPT
7c673cae 118{
b32b8144 119 return to_uint() == 0;
7c673cae
FG
120}
121
b32b8144 122#if !defined(BOOST_ASIO_NO_DEPRECATED)
7c673cae
FG
123bool address_v4::is_class_a() const
124{
b32b8144 125 return (to_uint() & 0x80000000) == 0;
7c673cae
FG
126}
127
128bool address_v4::is_class_b() const
129{
b32b8144 130 return (to_uint() & 0xC0000000) == 0x80000000;
7c673cae
FG
131}
132
133bool address_v4::is_class_c() const
134{
b32b8144 135 return (to_uint() & 0xE0000000) == 0xC0000000;
7c673cae 136}
b32b8144 137#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
7c673cae 138
92f5a8d4 139bool address_v4::is_multicast() const BOOST_ASIO_NOEXCEPT
7c673cae 140{
b32b8144 141 return (to_uint() & 0xF0000000) == 0xE0000000;
7c673cae
FG
142}
143
b32b8144 144#if !defined(BOOST_ASIO_NO_DEPRECATED)
7c673cae
FG
145address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
146{
b32b8144 147 return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF));
7c673cae
FG
148}
149
150address_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}
b32b8144
FG
160#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
161
162address_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
92f5a8d4
TL
170address_v4 make_address_v4(const char* str,
171 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
b32b8144
FG
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
180address_v4 make_address_v4(const std::string& str)
181{
182 return make_address_v4(str.c_str());
183}
184
92f5a8d4
TL
185address_v4 make_address_v4(const std::string& str,
186 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
b32b8144
FG
187{
188 return make_address_v4(str.c_str(), ec);
189}
190
11fdf7f2 191#if defined(BOOST_ASIO_HAS_STRING_VIEW)
b32b8144
FG
192
193address_v4 make_address_v4(string_view str)
194{
195 return make_address_v4(static_cast<std::string>(str));
196}
197
198address_v4 make_address_v4(string_view str,
92f5a8d4 199 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT
b32b8144
FG
200{
201 return make_address_v4(static_cast<std::string>(str), ec);
202}
203
11fdf7f2 204#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
7c673cae
FG
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