1 | // |
2 | // ip/impl/address_v4.ipp |
3 | // ~~~~~~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2024 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(dest: &addr_.s_addr, src: bytes.data(), n: 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(e: ex); |
55 | } |
56 | |
57 | addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long( |
58 | value: static_cast<boost::asio::detail::u_long_type>(addr)); |
59 | } |
60 | |
61 | address_v4::bytes_type address_v4::to_bytes() const noexcept |
62 | { |
63 | using namespace std; // For memcpy. |
64 | bytes_type bytes; |
65 | memcpy(dest: bytes.data(), src: &addr_.s_addr, n: 4); |
66 | return bytes; |
67 | } |
68 | |
69 | address_v4::uint_type address_v4::to_uint() const noexcept |
70 | { |
71 | return boost::asio::detail::socket_ops::network_to_host_long(value: addr_.s_addr); |
72 | } |
73 | |
74 | #if !defined(BOOST_ASIO_NO_DEPRECATED) |
75 | unsigned long address_v4::to_ulong() const |
76 | { |
77 | return boost::asio::detail::socket_ops::network_to_host_long(value: addr_.s_addr); |
78 | } |
79 | #endif // !defined(BOOST_ASIO_NO_DEPRECATED) |
80 | |
81 | std::string address_v4::to_string() const |
82 | { |
83 | boost::system::error_code ec; |
84 | char addr_str[boost::asio::detail::max_addr_v4_str_len]; |
85 | const char* addr = |
86 | boost::asio::detail::socket_ops::inet_ntop( |
87 | BOOST_ASIO_OS_DEF(AF_INET), src: &addr_, dest: addr_str, |
88 | length: boost::asio::detail::max_addr_v4_str_len, scope_id: 0, ec); |
89 | if (addr == 0) |
90 | boost::asio::detail::throw_error(err: ec); |
91 | return addr; |
92 | } |
93 | |
94 | #if !defined(BOOST_ASIO_NO_DEPRECATED) |
95 | std::string address_v4::to_string(boost::system::error_code& ec) const |
96 | { |
97 | char addr_str[boost::asio::detail::max_addr_v4_str_len]; |
98 | const char* addr = |
99 | boost::asio::detail::socket_ops::inet_ntop( |
100 | BOOST_ASIO_OS_DEF(AF_INET), src: &addr_, dest: addr_str, |
101 | length: boost::asio::detail::max_addr_v4_str_len, scope_id: 0, ec); |
102 | if (addr == 0) |
103 | return std::string(); |
104 | return addr; |
105 | } |
106 | #endif // !defined(BOOST_ASIO_NO_DEPRECATED) |
107 | |
108 | bool address_v4::is_loopback() const noexcept |
109 | { |
110 | return (to_uint() & 0xFF000000) == 0x7F000000; |
111 | } |
112 | |
113 | bool address_v4::is_unspecified() const noexcept |
114 | { |
115 | return to_uint() == 0; |
116 | } |
117 | |
118 | #if !defined(BOOST_ASIO_NO_DEPRECATED) |
119 | bool address_v4::is_class_a() const |
120 | { |
121 | return (to_uint() & 0x80000000) == 0; |
122 | } |
123 | |
124 | bool address_v4::is_class_b() const |
125 | { |
126 | return (to_uint() & 0xC0000000) == 0x80000000; |
127 | } |
128 | |
129 | bool address_v4::is_class_c() const |
130 | { |
131 | return (to_uint() & 0xE0000000) == 0xC0000000; |
132 | } |
133 | #endif // !defined(BOOST_ASIO_NO_DEPRECATED) |
134 | |
135 | bool address_v4::is_multicast() const noexcept |
136 | { |
137 | return (to_uint() & 0xF0000000) == 0xE0000000; |
138 | } |
139 | |
140 | #if !defined(BOOST_ASIO_NO_DEPRECATED) |
141 | address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask) |
142 | { |
143 | return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF)); |
144 | } |
145 | |
146 | address_v4 address_v4::netmask(const address_v4& addr) |
147 | { |
148 | if (addr.is_class_a()) |
149 | return address_v4(0xFF000000); |
150 | if (addr.is_class_b()) |
151 | return address_v4(0xFFFF0000); |
152 | if (addr.is_class_c()) |
153 | return address_v4(0xFFFFFF00); |
154 | return address_v4(0xFFFFFFFF); |
155 | } |
156 | #endif // !defined(BOOST_ASIO_NO_DEPRECATED) |
157 | |
158 | address_v4 make_address_v4(const char* str) |
159 | { |
160 | boost::system::error_code ec; |
161 | address_v4 addr = make_address_v4(str, ec); |
162 | boost::asio::detail::throw_error(err: ec); |
163 | return addr; |
164 | } |
165 | |
166 | address_v4 make_address_v4(const char* str, |
167 | boost::system::error_code& ec) noexcept |
168 | { |
169 | address_v4::bytes_type bytes; |
170 | if (boost::asio::detail::socket_ops::inet_pton( |
171 | BOOST_ASIO_OS_DEF(AF_INET), src: str, dest: &bytes, scope_id: 0, ec) <= 0) |
172 | return address_v4(); |
173 | return address_v4(bytes); |
174 | } |
175 | |
176 | address_v4 make_address_v4(const std::string& str) |
177 | { |
178 | return make_address_v4(str: str.c_str()); |
179 | } |
180 | |
181 | address_v4 make_address_v4(const std::string& str, |
182 | boost::system::error_code& ec) noexcept |
183 | { |
184 | return make_address_v4(str: str.c_str(), ec); |
185 | } |
186 | |
187 | #if defined(BOOST_ASIO_HAS_STRING_VIEW) |
188 | |
189 | address_v4 make_address_v4(string_view str) |
190 | { |
191 | return make_address_v4(str: static_cast<std::string>(str)); |
192 | } |
193 | |
194 | address_v4 make_address_v4(string_view str, |
195 | boost::system::error_code& ec) noexcept |
196 | { |
197 | return make_address_v4(str: static_cast<std::string>(str), ec); |
198 | } |
199 | |
200 | #endif // defined(BOOST_ASIO_HAS_STRING_VIEW) |
201 | |
202 | } // namespace ip |
203 | } // namespace asio |
204 | } // namespace boost |
205 | |
206 | #include <boost/asio/detail/pop_options.hpp> |
207 | |
208 | #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP |
209 | |