Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / asio / ip / address_v6.hpp
1 //
2 // ip/address_v6.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 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_ADDRESS_V6_HPP
12 #define BOOST_ASIO_IP_ADDRESS_V6_HPP
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 <string>
20 #include <boost/asio/detail/array.hpp>
21 #include <boost/asio/detail/socket_types.hpp>
22 #include <boost/asio/detail/winsock_init.hpp>
23 #include <boost/system/error_code.hpp>
24 #include <boost/asio/ip/address_v4.hpp>
25
26 #if !defined(BOOST_ASIO_NO_IOSTREAM)
27 # include <iosfwd>
28 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
29
30 #include <boost/asio/detail/push_options.hpp>
31
32 namespace boost {
33 namespace asio {
34 namespace ip {
35
36 /// Implements IP version 6 style addresses.
37 /**
38  * The boost::asio::ip::address_v6 class provides the ability to use and
39  * manipulate IP version 6 addresses.
40  *
41  * @par Thread Safety
42  * @e Distinct @e objects: Safe.@n
43  * @e Shared @e objects: Unsafe.
44  */
45 class address_v6
46 {
47 public:
48   /// The type used to represent an address as an array of bytes.
49   /**
50    * @note This type is defined in terms of the C++0x template @c std::array
51    * when it is available. Otherwise, it uses @c boost:array.
52    */
53 #if defined(GENERATING_DOCUMENTATION)
54   typedef array<unsigned char, 16> bytes_type;
55 #else
56   typedef boost::asio::detail::array<unsigned char, 16> bytes_type;
57 #endif
58
59   /// Default constructor.
60   BOOST_ASIO_DECL address_v6();
61
62   /// Construct an address from raw bytes and scope ID.
63   BOOST_ASIO_DECL explicit address_v6(const bytes_type& bytes,
64       unsigned long scope_id = 0);
65
66   /// Copy constructor.
67   BOOST_ASIO_DECL address_v6(const address_v6& other);
68
69 #if defined(BOOST_ASIO_HAS_MOVE)
70   /// Move constructor.
71   BOOST_ASIO_DECL address_v6(address_v6&& other);
72 #endif // defined(BOOST_ASIO_HAS_MOVE)
73
74   /// Assign from another address.
75   BOOST_ASIO_DECL address_v6& operator=(const address_v6& other);
76
77 #if defined(BOOST_ASIO_HAS_MOVE)
78   /// Move-assign from another address.
79   BOOST_ASIO_DECL address_v6& operator=(address_v6&& other);
80 #endif // defined(BOOST_ASIO_HAS_MOVE)
81
82   /// The scope ID of the address.
83   /**
84    * Returns the scope ID associated with the IPv6 address.
85    */
86   unsigned long scope_id() const
87   {
88     return scope_id_;
89   }
90
91   /// The scope ID of the address.
92   /**
93    * Modifies the scope ID associated with the IPv6 address.
94    */
95   void scope_id(unsigned long id)
96   {
97     scope_id_ = id;
98   }
99
100   /// Get the address in bytes, in network byte order.
101   BOOST_ASIO_DECL bytes_type to_bytes() const;
102
103   /// Get the address as a string.
104   BOOST_ASIO_DECL std::string to_string() const;
105
106   /// Get the address as a string.
107   BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
108
109   /// Create an address from an IP address string.
110   BOOST_ASIO_DECL static address_v6 from_string(const char* str);
111
112   /// Create an address from an IP address string.
113   BOOST_ASIO_DECL static address_v6 from_string(
114       const char* str, boost::system::error_code& ec);
115
116   /// Create an address from an IP address string.
117   BOOST_ASIO_DECL static address_v6 from_string(const std::string& str);
118
119   /// Create an address from an IP address string.
120   BOOST_ASIO_DECL static address_v6 from_string(
121       const std::string& str, boost::system::error_code& ec);
122
123   /// Converts an IPv4-mapped or IPv4-compatible address to an IPv4 address.
124   BOOST_ASIO_DECL address_v4 to_v4() const;
125
126   /// Determine whether the address is a loopback address.
127   BOOST_ASIO_DECL bool is_loopback() const;
128
129   /// Determine whether the address is unspecified.
130   BOOST_ASIO_DECL bool is_unspecified() const;
131
132   /// Determine whether the address is link local.
133   BOOST_ASIO_DECL bool is_link_local() const;
134
135   /// Determine whether the address is site local.
136   BOOST_ASIO_DECL bool is_site_local() const;
137
138   /// Determine whether the address is a mapped IPv4 address.
139   BOOST_ASIO_DECL bool is_v4_mapped() const;
140
141   /// Determine whether the address is an IPv4-compatible address.
142   BOOST_ASIO_DECL bool is_v4_compatible() const;
143
144   /// Determine whether the address is a multicast address.
145   BOOST_ASIO_DECL bool is_multicast() const;
146
147   /// Determine whether the address is a global multicast address.
148   BOOST_ASIO_DECL bool is_multicast_global() const;
149
150   /// Determine whether the address is a link-local multicast address.
151   BOOST_ASIO_DECL bool is_multicast_link_local() const;
152
153   /// Determine whether the address is a node-local multicast address.
154   BOOST_ASIO_DECL bool is_multicast_node_local() const;
155
156   /// Determine whether the address is a org-local multicast address.
157   BOOST_ASIO_DECL bool is_multicast_org_local() const;
158
159   /// Determine whether the address is a site-local multicast address.
160   BOOST_ASIO_DECL bool is_multicast_site_local() const;
161
162   /// Compare two addresses for equality.
163   BOOST_ASIO_DECL friend bool operator==(
164       const address_v6& a1, const address_v6& a2);
165
166   /// Compare two addresses for inequality.
167   friend bool operator!=(const address_v6& a1, const address_v6& a2)
168   {
169     return !(a1 == a2);
170   }
171
172   /// Compare addresses for ordering.
173   BOOST_ASIO_DECL friend bool operator<(
174       const address_v6& a1, const address_v6& a2);
175
176   /// Compare addresses for ordering.
177   friend bool operator>(const address_v6& a1, const address_v6& a2)
178   {
179     return a2 < a1;
180   }
181
182   /// Compare addresses for ordering.
183   friend bool operator<=(const address_v6& a1, const address_v6& a2)
184   {
185     return !(a2 < a1);
186   }
187
188   /// Compare addresses for ordering.
189   friend bool operator>=(const address_v6& a1, const address_v6& a2)
190   {
191     return !(a1 < a2);
192   }
193
194   /// Obtain an address object that represents any address.
195   static address_v6 any()
196   {
197     return address_v6();
198   }
199
200   /// Obtain an address object that represents the loopback address.
201   BOOST_ASIO_DECL static address_v6 loopback();
202
203   /// Create an IPv4-mapped IPv6 address.
204   BOOST_ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
205
206   /// Create an IPv4-compatible IPv6 address.
207   BOOST_ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
208
209 private:
210   // The underlying IPv6 address.
211   boost::asio::detail::in6_addr_type addr_;
212
213   // The scope ID associated with the address.
214   unsigned long scope_id_;
215 };
216
217 #if !defined(BOOST_ASIO_NO_IOSTREAM)
218
219 /// Output an address as a string.
220 /**
221  * Used to output a human-readable string for a specified address.
222  *
223  * @param os The output stream to which the string will be written.
224  *
225  * @param addr The address to be written.
226  *
227  * @return The output stream.
228  *
229  * @relates boost::asio::ip::address_v6
230  */
231 template <typename Elem, typename Traits>
232 std::basic_ostream<Elem, Traits>& operator<<(
233     std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
234
235 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
236
237 } // namespace ip
238 } // namespace asio
239 } // namespace boost
240
241 #include <boost/asio/detail/pop_options.hpp>
242
243 #include <boost/asio/ip/impl/address_v6.hpp>
244 #if defined(BOOST_ASIO_HEADER_ONLY)
245 # include <boost/asio/ip/impl/address_v6.ipp>
246 #endif // defined(BOOST_ASIO_HEADER_ONLY)
247
248 #endif // BOOST_ASIO_IP_ADDRESS_V6_HPP