1//
2// ip/impl/address_v6.ipp
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 BOOST_ASIO_IP_IMPL_ADDRESS_V6_IPP
12#define BOOST_ASIO_IP_IMPL_ADDRESS_V6_IPP
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 <cstring>
20#include <stdexcept>
21#include <typeinfo>
22#include <boost/asio/detail/socket_ops.hpp>
23#include <boost/asio/detail/throw_error.hpp>
24#include <boost/asio/detail/throw_exception.hpp>
25#include <boost/asio/error.hpp>
26#include <boost/asio/ip/address_v6.hpp>
27
28#include <boost/asio/detail/push_options.hpp>
29
30namespace boost {
31namespace asio {
32namespace ip {
33
34address_v6::address_v6()
35 : addr_(),
36 scope_id_(0)
37{
38}
39
40address_v6::address_v6(const address_v6::bytes_type& bytes,
41 unsigned long scope)
42 : scope_id_(scope)
43{
44#if UCHAR_MAX > 0xFF
45 for (std::size_t i = 0; i < bytes.size(); ++i)
46 {
47 if (bytes[i] > 0xFF)
48 {
49 std::out_of_range ex("address_v6 from bytes_type");
50 boost::asio::detail::throw_exception(ex);
51 }
52 }
53#endif // UCHAR_MAX > 0xFF
54
55 using namespace std; // For memcpy.
56 memcpy(addr_.s6_addr, bytes.data(), 16);
57}
58
59address_v6::address_v6(const address_v6& other)
60 : addr_(other.addr_),
61 scope_id_(other.scope_id_)
62{
63}
64
65#if defined(BOOST_ASIO_HAS_MOVE)
66address_v6::address_v6(address_v6&& other)
67 : addr_(other.addr_),
68 scope_id_(other.scope_id_)
69{
70}
71#endif // defined(BOOST_ASIO_HAS_MOVE)
72
73address_v6& address_v6::operator=(const address_v6& other)
74{
75 addr_ = other.addr_;
76 scope_id_ = other.scope_id_;
77 return *this;
78}
79
80#if defined(BOOST_ASIO_HAS_MOVE)
81address_v6& address_v6::operator=(address_v6&& other)
82{
83 addr_ = other.addr_;
84 scope_id_ = other.scope_id_;
85 return *this;
86}
87#endif // defined(BOOST_ASIO_HAS_MOVE)
88
89address_v6::bytes_type address_v6::to_bytes() const
90{
91 using namespace std; // For memcpy.
92 bytes_type bytes;
93#if defined(BOOST_ASIO_HAS_STD_ARRAY)
94 memcpy(bytes.data(), addr_.s6_addr, 16);
95#else // defined(BOOST_ASIO_HAS_STD_ARRAY)
96 memcpy(bytes.elems, addr_.s6_addr, 16);
97#endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
98 return bytes;
99}
100
101std::string address_v6::to_string() const
102{
103 boost::system::error_code ec;
104 std::string addr = to_string(ec);
105 boost::asio::detail::throw_error(ec);
106 return addr;
107}
108
109std::string address_v6::to_string(boost::system::error_code& ec) const
110{
111 char addr_str[boost::asio::detail::max_addr_v6_str_len];
112 const char* addr =
113 boost::asio::detail::socket_ops::inet_ntop(
114 BOOST_ASIO_OS_DEF(AF_INET6), &addr_, addr_str,
115 boost::asio::detail::max_addr_v6_str_len, scope_id_, ec);
116 if (addr == 0)
117 return std::string();
118 return addr;
119}
120
121address_v6 address_v6::from_string(const char* str)
122{
123 boost::system::error_code ec;
124 address_v6 addr = from_string(str, ec);
125 boost::asio::detail::throw_error(ec);
126 return addr;
127}
128
129address_v6 address_v6::from_string(
130 const char* str, boost::system::error_code& ec)
131{
132 address_v6 tmp;
133 if (boost::asio::detail::socket_ops::inet_pton(
134 BOOST_ASIO_OS_DEF(AF_INET6), str, &tmp.addr_, &tmp.scope_id_, ec) <= 0)
135 return address_v6();
136 return tmp;
137}
138
139address_v6 address_v6::from_string(const std::string& str)
140{
141 return from_string(str.c_str());
142}
143
144address_v6 address_v6::from_string(
145 const std::string& str, boost::system::error_code& ec)
146{
147 return from_string(str.c_str(), ec);
148}
149
150address_v4 address_v6::to_v4() const
151{
152 if (!is_v4_mapped() && !is_v4_compatible())
153 {
154 std::bad_cast ex;
155 boost::asio::detail::throw_exception(ex);
156 }
157
158 address_v4::bytes_type v4_bytes = { { addr_.s6_addr[12],
159 addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } };
160 return address_v4(v4_bytes);
161}
162
163bool address_v6::is_loopback() const
164{
165 return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
166 && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
167 && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
168 && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
169 && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
170 && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
171 && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
172 && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 1));
173}
174
175bool address_v6::is_unspecified() const
176{
177 return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
178 && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
179 && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
180 && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
181 && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
182 && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
183 && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
184 && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 0));
185}
186
187bool address_v6::is_link_local() const
188{
189 return ((addr_.s6_addr[0] == 0xfe) && ((addr_.s6_addr[1] & 0xc0) == 0x80));
190}
191
192bool address_v6::is_site_local() const
193{
194 return ((addr_.s6_addr[0] == 0xfe) && ((addr_.s6_addr[1] & 0xc0) == 0xc0));
195}
196
197bool address_v6::is_v4_mapped() const
198{
199 return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
200 && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
201 && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
202 && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
203 && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
204 && (addr_.s6_addr[10] == 0xff) && (addr_.s6_addr[11] == 0xff));
205}
206
207bool address_v6::is_v4_compatible() const
208{
209 return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
210 && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
211 && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
212 && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
213 && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
214 && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
215 && !((addr_.s6_addr[12] == 0)
216 && (addr_.s6_addr[13] == 0)
217 && (addr_.s6_addr[14] == 0)
218 && ((addr_.s6_addr[15] == 0) || (addr_.s6_addr[15] == 1))));
219}
220
221bool address_v6::is_multicast() const
222{
223 return (addr_.s6_addr[0] == 0xff);
224}
225
226bool address_v6::is_multicast_global() const
227{
228 return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x0e));
229}
230
231bool address_v6::is_multicast_link_local() const
232{
233 return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x02));
234}
235
236bool address_v6::is_multicast_node_local() const
237{
238 return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x01));
239}
240
241bool address_v6::is_multicast_org_local() const
242{
243 return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x08));
244}
245
246bool address_v6::is_multicast_site_local() const
247{
248 return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x05));
249}
250
251bool operator==(const address_v6& a1, const address_v6& a2)
252{
253 using namespace std; // For memcmp.
254 return memcmp(&a1.addr_, &a2.addr_,
255 sizeof(boost::asio::detail::in6_addr_type)) == 0
256 && a1.scope_id_ == a2.scope_id_;
257}
258
259bool operator<(const address_v6& a1, const address_v6& a2)
260{
261 using namespace std; // For memcmp.
262 int memcmp_result = memcmp(&a1.addr_, &a2.addr_,
263 sizeof(boost::asio::detail::in6_addr_type));
264 if (memcmp_result < 0)
265 return true;
266 if (memcmp_result > 0)
267 return false;
268 return a1.scope_id_ < a2.scope_id_;
269}
270
271address_v6 address_v6::loopback()
272{
273 address_v6 tmp;
274 tmp.addr_.s6_addr[15] = 1;
275 return tmp;
276}
277
278address_v6 address_v6::v4_mapped(const address_v4& addr)
279{
280 address_v4::bytes_type v4_bytes = addr.to_bytes();
281 bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
282 v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
283 return address_v6(v6_bytes);
284}
285
286address_v6 address_v6::v4_compatible(const address_v4& addr)
287{
288 address_v4::bytes_type v4_bytes = addr.to_bytes();
289 bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290 v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
291 return address_v6(v6_bytes);
292}
293
294} // namespace ip
295} // namespace asio
296} // namespace boost
297
298#include <boost/asio/detail/pop_options.hpp>
299
300#endif // BOOST_ASIO_IP_IMPL_ADDRESS_V6_IPP
301