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