5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11
#ifndef ASIO_IP_ADDRESS_V6_HPP
12
#define ASIO_IP_ADDRESS_V6_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
25
#include <boost/array.hpp>
26
#include <boost/throw_exception.hpp>
27
#include "asio/detail/pop_options.hpp"
29
#include "asio/error.hpp"
30
#include "asio/detail/socket_ops.hpp"
31
#include "asio/detail/socket_types.hpp"
32
#include "asio/detail/throw_error.hpp"
33
#include "asio/ip/address_v4.hpp"
38
/// Implements IP version 6 style addresses.
40
* The asio::ip::address_v6 class provides the ability to use and
41
* manipulate IP version 6 addresses.
44
* @e Distinct @e objects: Safe.@n
45
* @e Shared @e objects: Unsafe.
50
/// The type used to represent an address as an array of bytes.
51
typedef boost::array<unsigned char, 16> bytes_type;
53
/// Default constructor.
57
asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
61
/// Construct an address from raw bytes and scope ID.
62
explicit address_v6(const bytes_type& bytes, unsigned long scope_id = 0)
66
for (std::size_t i = 0; i < bytes.size(); ++i)
70
std::out_of_range ex("address_v6 from bytes_type");
71
boost::throw_exception(ex);
74
#endif // UCHAR_MAX > 0xFF
76
using namespace std; // For memcpy.
77
memcpy(addr_.s6_addr, bytes.elems, 16);
81
address_v6(const address_v6& other)
83
scope_id_(other.scope_id_)
87
/// Assign from another address.
88
address_v6& operator=(const address_v6& other)
91
scope_id_ = other.scope_id_;
95
/// The scope ID of the address.
97
* Returns the scope ID associated with the IPv6 address.
99
unsigned long scope_id() const
104
/// The scope ID of the address.
106
* Modifies the scope ID associated with the IPv6 address.
108
void scope_id(unsigned long id)
113
/// Get the address in bytes.
114
bytes_type to_bytes() const
116
using namespace std; // For memcpy.
118
memcpy(bytes.elems, addr_.s6_addr, 16);
122
/// Get the address as a string.
123
std::string to_string() const
126
std::string addr = to_string(ec);
127
asio::detail::throw_error(ec);
131
/// Get the address as a string.
132
std::string to_string(asio::error_code& ec) const
134
char addr_str[asio::detail::max_addr_v6_str_len];
136
asio::detail::socket_ops::inet_ntop(AF_INET6, &addr_, addr_str,
137
asio::detail::max_addr_v6_str_len, scope_id_, ec);
139
return std::string();
143
/// Create an address from an IP address string.
144
static address_v6 from_string(const char* str)
147
address_v6 addr = from_string(str, ec);
148
asio::detail::throw_error(ec);
152
/// Create an address from an IP address string.
153
static address_v6 from_string(const char* str, asio::error_code& ec)
156
if (asio::detail::socket_ops::inet_pton(
157
AF_INET6, str, &tmp.addr_, &tmp.scope_id_, ec) <= 0)
162
/// Create an address from an IP address string.
163
static address_v6 from_string(const std::string& str)
165
return from_string(str.c_str());
168
/// Create an address from an IP address string.
169
static address_v6 from_string(const std::string& str,
170
asio::error_code& ec)
172
return from_string(str.c_str(), ec);
175
/// Converts an IPv4-mapped or IPv4-compatible address to an IPv4 address.
176
address_v4 to_v4() const
178
if (!is_v4_mapped() && !is_v4_compatible())
181
boost::throw_exception(ex);
184
address_v4::bytes_type v4_bytes = { { addr_.s6_addr[12],
185
addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } };
186
return address_v4(v4_bytes);
189
/// Determine whether the address is a loopback address.
190
bool is_loopback() const
192
#if defined(__BORLANDC__)
193
return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
194
&& (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
195
&& (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
196
&& (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
197
&& (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
198
&& (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
199
&& (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
200
&& (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 1));
202
using namespace asio::detail;
203
return IN6_IS_ADDR_LOOPBACK(&addr_) != 0;
207
/// Determine whether the address is unspecified.
208
bool is_unspecified() const
210
#if defined(__BORLANDC__)
211
return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
212
&& (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
213
&& (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
214
&& (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
215
&& (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
216
&& (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
217
&& (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
218
&& (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 0));
220
using namespace asio::detail;
221
return IN6_IS_ADDR_UNSPECIFIED(&addr_) != 0;
225
/// Determine whether the address is link local.
226
bool is_link_local() const
228
using namespace asio::detail;
229
return IN6_IS_ADDR_LINKLOCAL(&addr_) != 0;
232
/// Determine whether the address is site local.
233
bool is_site_local() const
235
using namespace asio::detail;
236
return IN6_IS_ADDR_SITELOCAL(&addr_) != 0;
239
/// Determine whether the address is a mapped IPv4 address.
240
bool is_v4_mapped() const
242
using namespace asio::detail;
243
return IN6_IS_ADDR_V4MAPPED(&addr_) != 0;
246
/// Determine whether the address is an IPv4-compatible address.
247
bool is_v4_compatible() const
249
using namespace asio::detail;
250
return IN6_IS_ADDR_V4COMPAT(&addr_) != 0;
253
/// Determine whether the address is a multicast address.
254
bool is_multicast() const
256
using namespace asio::detail;
257
return IN6_IS_ADDR_MULTICAST(&addr_) != 0;
260
/// Determine whether the address is a global multicast address.
261
bool is_multicast_global() const
263
using namespace asio::detail;
264
return IN6_IS_ADDR_MC_GLOBAL(&addr_) != 0;
267
/// Determine whether the address is a link-local multicast address.
268
bool is_multicast_link_local() const
270
using namespace asio::detail;
271
return IN6_IS_ADDR_MC_LINKLOCAL(&addr_) != 0;
274
/// Determine whether the address is a node-local multicast address.
275
bool is_multicast_node_local() const
277
using namespace asio::detail;
278
return IN6_IS_ADDR_MC_NODELOCAL(&addr_) != 0;
281
/// Determine whether the address is a org-local multicast address.
282
bool is_multicast_org_local() const
284
using namespace asio::detail;
285
return IN6_IS_ADDR_MC_ORGLOCAL(&addr_) != 0;
288
/// Determine whether the address is a site-local multicast address.
289
bool is_multicast_site_local() const
291
using namespace asio::detail;
292
return IN6_IS_ADDR_MC_SITELOCAL(&addr_) != 0;
295
/// Compare two addresses for equality.
296
friend bool operator==(const address_v6& a1, const address_v6& a2)
298
using namespace std; // For memcmp.
299
return memcmp(&a1.addr_, &a2.addr_,
300
sizeof(asio::detail::in6_addr_type)) == 0
301
&& a1.scope_id_ == a2.scope_id_;
304
/// Compare two addresses for inequality.
305
friend bool operator!=(const address_v6& a1, const address_v6& a2)
307
using namespace std; // For memcmp.
308
return memcmp(&a1.addr_, &a2.addr_,
309
sizeof(asio::detail::in6_addr_type)) != 0
310
|| a1.scope_id_ != a2.scope_id_;
313
/// Compare addresses for ordering.
314
friend bool operator<(const address_v6& a1, const address_v6& a2)
316
using namespace std; // For memcmp.
317
int memcmp_result = memcmp(&a1.addr_, &a2.addr_,
318
sizeof(asio::detail::in6_addr_type));
319
if (memcmp_result < 0)
321
if (memcmp_result > 0)
323
return a1.scope_id_ < a2.scope_id_;
326
/// Compare addresses for ordering.
327
friend bool operator>(const address_v6& a1, const address_v6& a2)
332
/// Compare addresses for ordering.
333
friend bool operator<=(const address_v6& a1, const address_v6& a2)
338
/// Compare addresses for ordering.
339
friend bool operator>=(const address_v6& a1, const address_v6& a2)
344
/// Obtain an address object that represents any address.
345
static address_v6 any()
350
/// Obtain an address object that represents the loopback address.
351
static address_v6 loopback()
354
asio::detail::in6_addr_type tmp_addr = IN6ADDR_LOOPBACK_INIT;
355
tmp.addr_ = tmp_addr;
359
/// Create an IPv4-mapped IPv6 address.
360
static address_v6 v4_mapped(const address_v4& addr)
362
address_v4::bytes_type v4_bytes = addr.to_bytes();
363
bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
364
v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
365
return address_v6(v6_bytes);
368
/// Create an IPv4-compatible IPv6 address.
369
static address_v6 v4_compatible(const address_v4& addr)
371
address_v4::bytes_type v4_bytes = addr.to_bytes();
372
bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
373
v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
374
return address_v6(v6_bytes);
378
// The underlying IPv6 address.
379
asio::detail::in6_addr_type addr_;
381
// The scope ID associated with the address.
382
unsigned long scope_id_;
385
/// Output an address as a string.
387
* Used to output a human-readable string for a specified address.
389
* @param os The output stream to which the string will be written.
391
* @param addr The address to be written.
393
* @return The output stream.
395
* @relates asio::ip::address_v6
397
template <typename Elem, typename Traits>
398
std::basic_ostream<Elem, Traits>& operator<<(
399
std::basic_ostream<Elem, Traits>& os, const address_v6& addr)
402
std::string s = addr.to_string(ec);
405
if (os.exceptions() & std::ios::failbit)
406
asio::detail::throw_error(ec);
408
os.setstate(std::ios_base::failbit);
411
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
419
#include "asio/detail/pop_options.hpp"
421
#endif // ASIO_IP_ADDRESS_V6_HPP