• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // ip/network_v6.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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 
27 namespace boost {
28 namespace asio {
29 namespace 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  */
40 class network_v6
41 {
42 public:
43   /// Default constructor.
network_v6()44   network_v6() BOOST_ASIO_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.
network_v6(const network_v6 & other)55   network_v6(const network_v6& other) BOOST_ASIO_NOEXCEPT
56     : address_(other.address_),
57       prefix_length_(other.prefix_length_)
58   {
59   }
60 
61 #if defined(BOOST_ASIO_HAS_MOVE)
62   /// Move constructor.
network_v6(network_v6 && other)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_)
66   {
67   }
68 #endif // defined(BOOST_ASIO_HAS_MOVE)
69 
70   /// Assign from another network.
operator =(const network_v6 & other)71   network_v6& operator=(const network_v6& other) BOOST_ASIO_NOEXCEPT
72   {
73     address_ = other.address_;
74     prefix_length_ = other.prefix_length_;
75     return *this;
76   }
77 
78 #if defined(BOOST_ASIO_HAS_MOVE)
79   /// Move-assign from another network.
operator =(network_v6 && other)80   network_v6& operator=(network_v6&& other) BOOST_ASIO_NOEXCEPT
81   {
82     address_ = BOOST_ASIO_MOVE_CAST(address_v6)(other.address_);
83     prefix_length_ = other.prefix_length_;
84     return *this;
85   }
86 #endif // defined(BOOST_ASIO_HAS_MOVE)
87 
88   /// Obtain the address object specified when the network object was created.
address() const89   address_v6 address() const BOOST_ASIO_NOEXCEPT
90   {
91     return address_;
92   }
93 
94   /// Obtain the prefix length that was specified when the network object was
95   /// created.
prefix_length() const96   unsigned short prefix_length() const BOOST_ASIO_NOEXCEPT
97   {
98     return prefix_length_;
99   }
100 
101   /// Obtain an address object that represents the network address.
102   BOOST_ASIO_DECL address_v6 network() const BOOST_ASIO_NOEXCEPT;
103 
104   /// Obtain an address range corresponding to the hosts in the network.
105   BOOST_ASIO_DECL address_v6_range hosts() const BOOST_ASIO_NOEXCEPT;
106 
107   /// Obtain the true network address, omitting any host bits.
canonical() const108   network_v6 canonical() const BOOST_ASIO_NOEXCEPT
109   {
110     return network_v6(network(), prefix_length());
111   }
112 
113   /// Test if network is a valid host address.
is_host() const114   bool is_host() const BOOST_ASIO_NOEXCEPT
115   {
116     return prefix_length_ == 128;
117   }
118 
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;
121 
122   /// Get the network as an address in dotted decimal format.
123   BOOST_ASIO_DECL std::string to_string() const;
124 
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;
127 
128   /// Compare two networks for equality.
operator ==(const network_v6 & a,const network_v6 & b)129   friend bool operator==(const network_v6& a, const network_v6& b)
130   {
131     return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
132   }
133 
134   /// Compare two networks for inequality.
operator !=(const network_v6 & a,const network_v6 & b)135   friend bool operator!=(const network_v6& a, const network_v6& b)
136   {
137     return !(a == b);
138   }
139 
140 private:
141   address_v6 address_;
142   unsigned short prefix_length_;
143 };
144 
145 /// Create an IPv6 network from an address and prefix length.
146 /**
147  * @relates address_v6
148  */
make_network_v6(const address_v6 & addr,unsigned short prefix_len)149 inline network_v6 make_network_v6(
150     const address_v6& addr, unsigned short prefix_len)
151 {
152   return network_v6(addr, prefix_len);
153 }
154 
155 /// Create an IPv6 network from a string containing IP address and prefix
156 /// length.
157 /**
158  * @relates network_v6
159  */
160 BOOST_ASIO_DECL network_v6 make_network_v6(const char* str);
161 
162 /// Create an IPv6 network from a string containing IP address and prefix
163 /// length.
164 /**
165  * @relates network_v6
166  */
167 BOOST_ASIO_DECL network_v6 make_network_v6(
168     const char* str, boost::system::error_code& ec);
169 
170 /// Create an IPv6 network from a string containing IP address and prefix
171 /// length.
172 /**
173  * @relates network_v6
174  */
175 BOOST_ASIO_DECL network_v6 make_network_v6(const std::string& str);
176 
177 /// Create an IPv6 network from a string containing IP address and prefix
178 /// length.
179 /**
180  * @relates network_v6
181  */
182 BOOST_ASIO_DECL network_v6 make_network_v6(
183     const std::string& str, boost::system::error_code& ec);
184 
185 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
186   || defined(GENERATING_DOCUMENTATION)
187 
188 /// Create an IPv6 network from a string containing IP address and prefix
189 /// length.
190 /**
191  * @relates network_v6
192  */
193 BOOST_ASIO_DECL network_v6 make_network_v6(string_view str);
194 
195 /// Create an IPv6 network from a string containing IP address and prefix
196 /// length.
197 /**
198  * @relates network_v6
199  */
200 BOOST_ASIO_DECL network_v6 make_network_v6(
201     string_view str, boost::system::error_code& ec);
202 
203 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
204        //  || defined(GENERATING_DOCUMENTATION)
205 
206 #if !defined(BOOST_ASIO_NO_IOSTREAM)
207 
208 /// Output a network as a string.
209 /**
210  * Used to output a human-readable string for a specified network.
211  *
212  * @param os The output stream to which the string will be written.
213  *
214  * @param net The network to be written.
215  *
216  * @return The output stream.
217  *
218  * @relates boost::asio::ip::address_v6
219  */
220 template <typename Elem, typename Traits>
221 std::basic_ostream<Elem, Traits>& operator<<(
222     std::basic_ostream<Elem, Traits>& os, const network_v6& net);
223 
224 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
225 
226 } // namespace ip
227 } // namespace asio
228 } // namespace boost
229 
230 #include <boost/asio/detail/pop_options.hpp>
231 
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)
236 
237 #endif // BOOST_ASIO_IP_NETWORK_V6_HPP
238