2 // ip/impl/address_v4.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
12 #define BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
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>
28 #include <boost/asio/detail/push_options.hpp>
34 address_v4::address_v4(const address_v4::bytes_type& bytes)
37 if (bytes[0] > 0xFF || bytes[1] > 0xFF
38 || bytes[2] > 0xFF || bytes[3] > 0xFF)
40 std::out_of_range ex("address_v4 from bytes_type");
41 boost::asio::detail::throw_exception(ex);
43 #endif // UCHAR_MAX > 0xFF
45 using namespace std; // For memcpy.
46 memcpy(&addr_.s_addr, bytes.data(), 4);
49 address_v4::address_v4(address_v4::uint_type addr)
51 if ((std::numeric_limits<uint_type>::max)() > 0xFFFFFFFF)
53 std::out_of_range ex("address_v4 from unsigned integer");
54 boost::asio::detail::throw_exception(ex);
57 addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(
58 static_cast<boost::asio::detail::u_long_type>(addr));
61 address_v4::bytes_type address_v4::to_bytes() const
63 using namespace std; // For memcpy.
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)
73 address_v4::uint_type address_v4::to_uint() const
75 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
78 #if !defined(BOOST_ASIO_NO_DEPRECATED)
79 unsigned long address_v4::to_ulong() const
81 return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
83 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
85 std::string address_v4::to_string() const
87 boost::system::error_code ec;
88 char addr_str[boost::asio::detail::max_addr_v4_str_len];
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);
94 boost::asio::detail::throw_error(ec);
98 #if !defined(BOOST_ASIO_NO_DEPRECATED)
99 std::string address_v4::to_string(boost::system::error_code& ec) const
101 char addr_str[boost::asio::detail::max_addr_v4_str_len];
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);
107 return std::string();
110 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
112 bool address_v4::is_loopback() const
114 return (to_uint() & 0xFF000000) == 0x7F000000;
117 bool address_v4::is_unspecified() const
119 return to_uint() == 0;
122 #if !defined(BOOST_ASIO_NO_DEPRECATED)
123 bool address_v4::is_class_a() const
125 return (to_uint() & 0x80000000) == 0;
128 bool address_v4::is_class_b() const
130 return (to_uint() & 0xC0000000) == 0x80000000;
133 bool address_v4::is_class_c() const
135 return (to_uint() & 0xE0000000) == 0xC0000000;
137 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
139 bool address_v4::is_multicast() const
141 return (to_uint() & 0xF0000000) == 0xE0000000;
144 #if !defined(BOOST_ASIO_NO_DEPRECATED)
145 address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
147 return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF));
150 address_v4 address_v4::netmask(const address_v4& addr)
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);
160 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
162 address_v4 make_address_v4(const char* str)
164 boost::system::error_code ec;
165 address_v4 addr = make_address_v4(str, ec);
166 boost::asio::detail::throw_error(ec);
170 address_v4 make_address_v4(
171 const char* str, boost::system::error_code& ec)
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)
177 return address_v4(bytes);
180 address_v4 make_address_v4(const std::string& str)
182 return make_address_v4(str.c_str());
185 address_v4 make_address_v4(
186 const std::string& str, boost::system::error_code& ec)
188 return make_address_v4(str.c_str(), ec);
191 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
193 address_v4 make_address_v4(string_view str)
195 return make_address_v4(static_cast<std::string>(str));
198 address_v4 make_address_v4(string_view str,
199 boost::system::error_code& ec)
201 return make_address_v4(static_cast<std::string>(str), ec);
204 #endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
210 #include <boost/asio/detail/pop_options.hpp>
212 #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP