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)
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)
12 #ifndef BOOST_ASIO_IP_NETWORK_V6_HPP
13 #define BOOST_ASIO_IP_NETWORK_V6_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #include <boost/asio/detail/string_view.hpp>
22 #include <boost/system/error_code.hpp>
23 #include <boost/asio/ip/address_v6_range.hpp>
25 #include <boost/asio/detail/push_options.hpp>
31 /// Represents an IPv6 network.
33 * The boost::asio::ip::network_v6 class provides the ability to use and
34 * manipulate IP version 6 networks.
37 * @e Distinct @e objects: Safe.@n
38 * @e Shared @e objects: Unsafe.
43 /// Default constructor.
44 network_v6() BOOST_ASIO_NOEXCEPT
50 /// Construct a network based on the specified address and prefix length.
51 BOOST_ASIO_DECL network_v6(const address_v6& addr,
52 unsigned short prefix_len);
55 network_v6(const network_v6& other) BOOST_ASIO_NOEXCEPT
56 : address_(other.address_),
57 prefix_length_(other.prefix_length_)
61 #if defined(BOOST_ASIO_HAS_MOVE)
63 network_v6(network_v6&& other) BOOST_ASIO_NOEXCEPT
64 : address_(BOOST_ASIO_MOVE_CAST(address_v6)(other.address_)),
65 prefix_length_(other.prefix_length_)
68 #endif // defined(BOOST_ASIO_HAS_MOVE)
70 /// Assign from another network.
71 network_v6& operator=(const network_v6& other) BOOST_ASIO_NOEXCEPT
73 address_ = other.address_;
74 prefix_length_ = other.prefix_length_;
78 #if defined(BOOST_ASIO_HAS_MOVE)
79 /// Move-assign from another network.
80 network_v6& operator=(network_v6&& other) BOOST_ASIO_NOEXCEPT
82 address_ = BOOST_ASIO_MOVE_CAST(address_v6)(other.address_);
83 prefix_length_ = other.prefix_length_;
86 #endif // defined(BOOST_ASIO_HAS_MOVE)
88 /// Obtain the address object specified when the network object was created.
89 address_v6 address() const BOOST_ASIO_NOEXCEPT
94 /// Obtain the prefix length that was specified when the network object was
96 unsigned short prefix_length() const BOOST_ASIO_NOEXCEPT
98 return prefix_length_;
101 /// Obtain an address object that represents the network address.
102 BOOST_ASIO_DECL address_v6 network() const BOOST_ASIO_NOEXCEPT;
104 /// Obtain an address range corresponding to the hosts in the network.
105 BOOST_ASIO_DECL address_v6_range hosts() const BOOST_ASIO_NOEXCEPT;
107 /// Obtain the true network address, omitting any host bits.
108 network_v6 canonical() const BOOST_ASIO_NOEXCEPT
110 return network_v6(network(), prefix_length());
113 /// Test if network is a valid host address.
114 bool is_host() const BOOST_ASIO_NOEXCEPT
116 return prefix_length_ == 128;
119 /// Test if a network is a real subnet of another network.
120 BOOST_ASIO_DECL bool is_subnet_of(const network_v6& other) const;
122 /// Get the network as an address in dotted decimal format.
123 BOOST_ASIO_DECL std::string to_string() const;
125 /// Get the network as an address in dotted decimal format.
126 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
128 /// Compare two networks for equality.
129 friend bool operator==(const network_v6& a, const network_v6& b)
131 return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
134 /// Compare two networks for inequality.
135 friend bool operator!=(const network_v6& a, const network_v6& b)
142 unsigned short prefix_length_;
145 /// Create an IPv6 network from an address and prefix length.
147 * @relates address_v6
149 inline network_v6 make_network_v6(
150 const address_v6& addr, unsigned short prefix_len)
152 return network_v6(addr, prefix_len);
155 /// Create an IPv6 network from a string containing IP address and prefix
158 * @relates network_v6
160 BOOST_ASIO_DECL network_v6 make_network_v6(const char* str);
162 /// Create an IPv6 network from a string containing IP address and prefix
165 * @relates network_v6
167 BOOST_ASIO_DECL network_v6 make_network_v6(
168 const char* str, boost::system::error_code& ec);
170 /// Create an IPv6 network from a string containing IP address and prefix
173 * @relates network_v6
175 BOOST_ASIO_DECL network_v6 make_network_v6(const std::string& str);
177 /// Create an IPv6 network from a string containing IP address and prefix
180 * @relates network_v6
182 BOOST_ASIO_DECL network_v6 make_network_v6(
183 const std::string& str, boost::system::error_code& ec);
185 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
186 || defined(GENERATING_DOCUMENTATION)
188 /// Create an IPv6 network from a string containing IP address and prefix
191 * @relates network_v6
193 BOOST_ASIO_DECL network_v6 make_network_v6(string_view str);
195 /// Create an IPv6 network from a string containing IP address and prefix
198 * @relates network_v6
200 BOOST_ASIO_DECL network_v6 make_network_v6(
201 string_view str, boost::system::error_code& ec);
203 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
204 // || defined(GENERATING_DOCUMENTATION)
206 #if !defined(BOOST_ASIO_NO_IOSTREAM)
208 /// Output a network as a string.
210 * Used to output a human-readable string for a specified network.
212 * @param os The output stream to which the string will be written.
214 * @param net The network to be written.
216 * @return The output stream.
218 * @relates boost::asio::ip::address_v6
220 template <typename Elem, typename Traits>
221 std::basic_ostream<Elem, Traits>& operator<<(
222 std::basic_ostream<Elem, Traits>& os, const network_v6& net);
224 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
230 #include <boost/asio/detail/pop_options.hpp>
232 #include <boost/asio/ip/impl/network_v6.hpp>
233 #if defined(BOOST_ASIO_HEADER_ONLY)
234 # include <boost/asio/ip/impl/network_v6.ipp>
235 #endif // defined(BOOST_ASIO_HEADER_ONLY)
237 #endif // BOOST_ASIO_IP_NETWORK_V6_HPP