1//
2// ip/network_v6.hpp
3// ~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
7//
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)
10//
11
12#ifndef BOOST_ASIO_IP_NETWORK_V6_HPP
13#define BOOST_ASIO_IP_NETWORK_V6_HPP
14
15#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16# pragma once
17#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
19#include <boost/asio/detail/config.hpp>
20#include <string>
21#include <boost/asio/detail/string_view.hpp>
22#include <boost/system/error_code.hpp>
23#include <boost/asio/ip/address_v6_range.hpp>
24
25#include <boost/asio/detail/push_options.hpp>
26
27namespace boost {
28namespace asio {
29namespace ip {
30
31/// Represents an IPv6 network.
32/**
33 * The boost::asio::ip::network_v6 class provides the ability to use and
34 * manipulate IP version 6 networks.
35 *
36 * @par Thread Safety
37 * @e Distinct @e objects: Safe.@n
38 * @e Shared @e objects: Unsafe.
39 */
40class network_v6
41{
42public:
43 /// Default constructor.
44 network_v6() noexcept
45 : address_(),
46 prefix_length_(0)
47 {
48 }
49
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);
53
54 /// Copy constructor.
55 network_v6(const network_v6& other) noexcept
56 : address_(other.address_),
57 prefix_length_(other.prefix_length_)
58 {
59 }
60
61 /// Move constructor.
62 network_v6(network_v6&& other) noexcept
63 : address_(static_cast<address_v6&&>(other.address_)),
64 prefix_length_(other.prefix_length_)
65 {
66 }
67
68 /// Assign from another network.
69 network_v6& operator=(const network_v6& other) noexcept
70 {
71 address_ = other.address_;
72 prefix_length_ = other.prefix_length_;
73 return *this;
74 }
75
76 /// Move-assign from another network.
77 network_v6& operator=(network_v6&& other) noexcept
78 {
79 address_ = static_cast<address_v6&&>(other.address_);
80 prefix_length_ = other.prefix_length_;
81 return *this;
82 }
83
84 /// Obtain the address object specified when the network object was created.
85 address_v6 address() const noexcept
86 {
87 return address_;
88 }
89
90 /// Obtain the prefix length that was specified when the network object was
91 /// created.
92 unsigned short prefix_length() const noexcept
93 {
94 return prefix_length_;
95 }
96
97 /// Obtain an address object that represents the network address.
98 BOOST_ASIO_DECL address_v6 network() const noexcept;
99
100 /// Obtain an address range corresponding to the hosts in the network.
101 BOOST_ASIO_DECL address_v6_range hosts() const noexcept;
102
103 /// Obtain the true network address, omitting any host bits.
104 network_v6 canonical() const noexcept
105 {
106 return network_v6(network(), prefix_length());
107 }
108
109 /// Test if network is a valid host address.
110 bool is_host() const noexcept
111 {
112 return prefix_length_ == 128;
113 }
114
115 /// Test if a network is a real subnet of another network.
116 BOOST_ASIO_DECL bool is_subnet_of(const network_v6& other) const;
117
118 /// Get the network as an address in dotted decimal format.
119 BOOST_ASIO_DECL std::string to_string() const;
120
121 /// Get the network as an address in dotted decimal format.
122 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
123
124 /// Compare two networks for equality.
125 friend bool operator==(const network_v6& a, const network_v6& b)
126 {
127 return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
128 }
129
130 /// Compare two networks for inequality.
131 friend bool operator!=(const network_v6& a, const network_v6& b)
132 {
133 return !(a == b);
134 }
135
136private:
137 address_v6 address_;
138 unsigned short prefix_length_;
139};
140
141/// Create an IPv6 network from an address and prefix length.
142/**
143 * @relates address_v6
144 */
145inline network_v6 make_network_v6(
146 const address_v6& addr, unsigned short prefix_len)
147{
148 return network_v6(addr, prefix_len);
149}
150
151/// Create an IPv6 network from a string containing IP address and prefix
152/// length.
153/**
154 * @relates network_v6
155 */
156BOOST_ASIO_DECL network_v6 make_network_v6(const char* str);
157
158/// Create an IPv6 network from a string containing IP address and prefix
159/// length.
160/**
161 * @relates network_v6
162 */
163BOOST_ASIO_DECL network_v6 make_network_v6(
164 const char* str, boost::system::error_code& ec);
165
166/// Create an IPv6 network from a string containing IP address and prefix
167/// length.
168/**
169 * @relates network_v6
170 */
171BOOST_ASIO_DECL network_v6 make_network_v6(const std::string& str);
172
173/// Create an IPv6 network from a string containing IP address and prefix
174/// length.
175/**
176 * @relates network_v6
177 */
178BOOST_ASIO_DECL network_v6 make_network_v6(
179 const std::string& str, boost::system::error_code& ec);
180
181#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
182 || defined(GENERATING_DOCUMENTATION)
183
184/// Create an IPv6 network from a string containing IP address and prefix
185/// length.
186/**
187 * @relates network_v6
188 */
189BOOST_ASIO_DECL network_v6 make_network_v6(string_view str);
190
191/// Create an IPv6 network from a string containing IP address and prefix
192/// length.
193/**
194 * @relates network_v6
195 */
196BOOST_ASIO_DECL network_v6 make_network_v6(
197 string_view str, boost::system::error_code& ec);
198
199#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
200 // || defined(GENERATING_DOCUMENTATION)
201
202#if !defined(BOOST_ASIO_NO_IOSTREAM)
203
204/// Output a network as a string.
205/**
206 * Used to output a human-readable string for a specified network.
207 *
208 * @param os The output stream to which the string will be written.
209 *
210 * @param net The network to be written.
211 *
212 * @return The output stream.
213 *
214 * @relates boost::asio::ip::address_v6
215 */
216template <typename Elem, typename Traits>
217std::basic_ostream<Elem, Traits>& operator<<(
218 std::basic_ostream<Elem, Traits>& os, const network_v6& net);
219
220#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
221
222} // namespace ip
223} // namespace asio
224} // namespace boost
225
226#include <boost/asio/detail/pop_options.hpp>
227
228#include <boost/asio/ip/impl/network_v6.hpp>
229#if defined(BOOST_ASIO_HEADER_ONLY)
230# include <boost/asio/ip/impl/network_v6.ipp>
231#endif // defined(BOOST_ASIO_HEADER_ONLY)
232
233#endif // BOOST_ASIO_IP_NETWORK_V6_HPP
234

source code of boost/libs/asio/include/boost/asio/ip/network_v6.hpp