2 // detail/socket_option.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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 BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP
12 #define BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
22 #include <boost/asio/detail/socket_ops.hpp>
23 #include <boost/asio/detail/socket_types.hpp>
24 #include <boost/asio/detail/throw_exception.hpp>
25 #include <boost/asio/ip/address.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 namespace socket_option {
35 // Helper template for implementing multicast enable loopback options.
36 template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
37 class multicast_enable_loopback
40 #if defined(__sun) || defined(__osf__)
41 typedef unsigned char ipv4_value_type;
42 typedef unsigned char ipv6_value_type;
43 #elif defined(_AIX) || defined(__hpux) || defined(__QNXNTO__)
44 typedef unsigned char ipv4_value_type;
45 typedef unsigned int ipv6_value_type;
47 typedef int ipv4_value_type;
48 typedef int ipv6_value_type;
51 // Default constructor.
52 multicast_enable_loopback()
58 // Construct with a specific option value.
59 explicit multicast_enable_loopback(bool v)
60 : ipv4_value_(v ? 1 : 0),
61 ipv6_value_(v ? 1 : 0)
65 // Set the value of the boolean.
66 multicast_enable_loopback& operator=(bool v)
68 ipv4_value_ = v ? 1 : 0;
69 ipv6_value_ = v ? 1 : 0;
73 // Get the current value of the boolean.
86 bool operator!() const
91 // Get the level of the socket option.
92 template <typename Protocol>
93 int level(const Protocol& protocol) const
95 if (protocol.family() == PF_INET6)
100 // Get the name of the socket option.
101 template <typename Protocol>
102 int name(const Protocol& protocol) const
104 if (protocol.family() == PF_INET6)
109 // Get the address of the boolean data.
110 template <typename Protocol>
111 void* data(const Protocol& protocol)
113 if (protocol.family() == PF_INET6)
118 // Get the address of the boolean data.
119 template <typename Protocol>
120 const void* data(const Protocol& protocol) const
122 if (protocol.family() == PF_INET6)
127 // Get the size of the boolean data.
128 template <typename Protocol>
129 std::size_t size(const Protocol& protocol) const
131 if (protocol.family() == PF_INET6)
132 return sizeof(ipv6_value_);
133 return sizeof(ipv4_value_);
136 // Set the size of the boolean data.
137 template <typename Protocol>
138 void resize(const Protocol& protocol, std::size_t s)
140 if (protocol.family() == PF_INET6)
142 if (s != sizeof(ipv6_value_))
144 std::length_error ex("multicast_enable_loopback socket option resize");
145 boost::asio::detail::throw_exception(ex);
147 ipv4_value_ = ipv6_value_ ? 1 : 0;
151 if (s != sizeof(ipv4_value_))
153 std::length_error ex("multicast_enable_loopback socket option resize");
154 boost::asio::detail::throw_exception(ex);
156 ipv6_value_ = ipv4_value_ ? 1 : 0;
161 ipv4_value_type ipv4_value_;
162 ipv6_value_type ipv6_value_;
165 // Helper template for implementing unicast hops options.
166 template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
170 // Default constructor.
176 // Construct with a specific option value.
177 explicit unicast_hops(int v)
182 // Set the value of the option.
183 unicast_hops& operator=(int v)
189 // Get the current value of the option.
195 // Get the level of the socket option.
196 template <typename Protocol>
197 int level(const Protocol& protocol) const
199 if (protocol.family() == PF_INET6)
204 // Get the name of the socket option.
205 template <typename Protocol>
206 int name(const Protocol& protocol) const
208 if (protocol.family() == PF_INET6)
213 // Get the address of the data.
214 template <typename Protocol>
215 int* data(const Protocol&)
220 // Get the address of the data.
221 template <typename Protocol>
222 const int* data(const Protocol&) const
227 // Get the size of the data.
228 template <typename Protocol>
229 std::size_t size(const Protocol&) const
231 return sizeof(value_);
234 // Set the size of the data.
235 template <typename Protocol>
236 void resize(const Protocol&, std::size_t s)
238 if (s != sizeof(value_))
240 std::length_error ex("unicast hops socket option resize");
241 boost::asio::detail::throw_exception(ex);
245 value_ = value_ & 0xFF;
253 // Helper template for implementing multicast hops options.
254 template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
258 #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
259 typedef int ipv4_value_type;
261 typedef unsigned char ipv4_value_type;
263 typedef int ipv6_value_type;
265 // Default constructor.
272 // Construct with a specific option value.
273 explicit multicast_hops(int v)
275 if (v < 0 || v > 255)
277 std::out_of_range ex("multicast hops value out of range");
278 boost::asio::detail::throw_exception(ex);
280 ipv4_value_ = (ipv4_value_type)v;
284 // Set the value of the option.
285 multicast_hops& operator=(int v)
287 if (v < 0 || v > 255)
289 std::out_of_range ex("multicast hops value out of range");
290 boost::asio::detail::throw_exception(ex);
292 ipv4_value_ = (ipv4_value_type)v;
297 // Get the current value of the option.
303 // Get the level of the socket option.
304 template <typename Protocol>
305 int level(const Protocol& protocol) const
307 if (protocol.family() == PF_INET6)
312 // Get the name of the socket option.
313 template <typename Protocol>
314 int name(const Protocol& protocol) const
316 if (protocol.family() == PF_INET6)
321 // Get the address of the data.
322 template <typename Protocol>
323 void* data(const Protocol& protocol)
325 if (protocol.family() == PF_INET6)
330 // Get the address of the data.
331 template <typename Protocol>
332 const void* data(const Protocol& protocol) const
334 if (protocol.family() == PF_INET6)
339 // Get the size of the data.
340 template <typename Protocol>
341 std::size_t size(const Protocol& protocol) const
343 if (protocol.family() == PF_INET6)
344 return sizeof(ipv6_value_);
345 return sizeof(ipv4_value_);
348 // Set the size of the data.
349 template <typename Protocol>
350 void resize(const Protocol& protocol, std::size_t s)
352 if (protocol.family() == PF_INET6)
354 if (s != sizeof(ipv6_value_))
356 std::length_error ex("multicast hops socket option resize");
357 boost::asio::detail::throw_exception(ex);
361 else if (ipv6_value_ > 255)
364 ipv4_value_ = (ipv4_value_type)ipv6_value_;
368 if (s != sizeof(ipv4_value_))
370 std::length_error ex("multicast hops socket option resize");
371 boost::asio::detail::throw_exception(ex);
373 ipv6_value_ = ipv4_value_;
378 ipv4_value_type ipv4_value_;
379 ipv6_value_type ipv6_value_;
382 // Helper template for implementing ip_mreq-based options.
383 template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
384 class multicast_request
387 // Default constructor.
389 : ipv4_value_(), // Zero-initialisation gives the "any" address.
390 ipv6_value_() // Zero-initialisation gives the "any" address.
394 // Construct with multicast address only.
395 explicit multicast_request(const boost::asio::ip::address& multicast_address)
396 : ipv4_value_(), // Zero-initialisation gives the "any" address.
397 ipv6_value_() // Zero-initialisation gives the "any" address.
399 if (multicast_address.is_v6())
401 using namespace std; // For memcpy.
402 boost::asio::ip::address_v6 ipv6_address = multicast_address.to_v6();
403 boost::asio::ip::address_v6::bytes_type bytes = ipv6_address.to_bytes();
404 memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
405 ipv6_value_.ipv6mr_interface = ipv6_address.scope_id();
409 ipv4_value_.imr_multiaddr.s_addr =
410 boost::asio::detail::socket_ops::host_to_network_long(
411 multicast_address.to_v4().to_ulong());
412 ipv4_value_.imr_interface.s_addr =
413 boost::asio::detail::socket_ops::host_to_network_long(
414 boost::asio::ip::address_v4::any().to_ulong());
418 // Construct with multicast address and IPv4 address specifying an interface.
419 explicit multicast_request(
420 const boost::asio::ip::address_v4& multicast_address,
421 const boost::asio::ip::address_v4& network_interface
422 = boost::asio::ip::address_v4::any())
423 : ipv6_value_() // Zero-initialisation gives the "any" address.
425 ipv4_value_.imr_multiaddr.s_addr =
426 boost::asio::detail::socket_ops::host_to_network_long(
427 multicast_address.to_ulong());
428 ipv4_value_.imr_interface.s_addr =
429 boost::asio::detail::socket_ops::host_to_network_long(
430 network_interface.to_ulong());
433 // Construct with multicast address and IPv6 network interface index.
434 explicit multicast_request(
435 const boost::asio::ip::address_v6& multicast_address,
436 unsigned long network_interface = 0)
437 : ipv4_value_() // Zero-initialisation gives the "any" address.
439 using namespace std; // For memcpy.
440 boost::asio::ip::address_v6::bytes_type bytes =
441 multicast_address.to_bytes();
442 memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
443 if (network_interface)
444 ipv6_value_.ipv6mr_interface = network_interface;
446 ipv6_value_.ipv6mr_interface = multicast_address.scope_id();
449 // Get the level of the socket option.
450 template <typename Protocol>
451 int level(const Protocol& protocol) const
453 if (protocol.family() == PF_INET6)
458 // Get the name of the socket option.
459 template <typename Protocol>
460 int name(const Protocol& protocol) const
462 if (protocol.family() == PF_INET6)
467 // Get the address of the option data.
468 template <typename Protocol>
469 const void* data(const Protocol& protocol) const
471 if (protocol.family() == PF_INET6)
476 // Get the size of the option data.
477 template <typename Protocol>
478 std::size_t size(const Protocol& protocol) const
480 if (protocol.family() == PF_INET6)
481 return sizeof(ipv6_value_);
482 return sizeof(ipv4_value_);
486 boost::asio::detail::in4_mreq_type ipv4_value_;
487 boost::asio::detail::in6_mreq_type ipv6_value_;
490 // Helper template for implementing options that specify a network interface.
491 template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
492 class network_interface
495 // Default constructor.
499 boost::asio::detail::socket_ops::host_to_network_long(
500 boost::asio::ip::address_v4::any().to_ulong());
504 // Construct with IPv4 interface.
505 explicit network_interface(const boost::asio::ip::address_v4& ipv4_interface)
508 boost::asio::detail::socket_ops::host_to_network_long(
509 ipv4_interface.to_ulong());
513 // Construct with IPv6 interface.
514 explicit network_interface(unsigned int ipv6_interface)
517 boost::asio::detail::socket_ops::host_to_network_long(
518 boost::asio::ip::address_v4::any().to_ulong());
519 ipv6_value_ = ipv6_interface;
522 // Get the level of the socket option.
523 template <typename Protocol>
524 int level(const Protocol& protocol) const
526 if (protocol.family() == PF_INET6)
531 // Get the name of the socket option.
532 template <typename Protocol>
533 int name(const Protocol& protocol) const
535 if (protocol.family() == PF_INET6)
540 // Get the address of the option data.
541 template <typename Protocol>
542 const void* data(const Protocol& protocol) const
544 if (protocol.family() == PF_INET6)
549 // Get the size of the option data.
550 template <typename Protocol>
551 std::size_t size(const Protocol& protocol) const
553 if (protocol.family() == PF_INET6)
554 return sizeof(ipv6_value_);
555 return sizeof(ipv4_value_);
559 boost::asio::detail::in4_addr_type ipv4_value_;
560 unsigned int ipv6_value_;
563 } // namespace socket_option
564 } // namespace detail
569 #include <boost/asio/detail/pop_options.hpp>
571 #endif // BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP