2 // detail/socket_option.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 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 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 address_v6 ipv6_address = multicast_address.to_v6();
403 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_uint());
412 ipv4_value_.imr_interface.s_addr =
413 boost::asio::detail::socket_ops::host_to_network_long(
414 address_v4::any().to_uint());
418 // Construct with multicast address and IPv4 address specifying an interface.
419 explicit multicast_request(const address_v4& multicast_address,
420 const address_v4& network_interface = address_v4::any())
421 : ipv6_value_() // Zero-initialisation gives the "any" address.
423 ipv4_value_.imr_multiaddr.s_addr =
424 boost::asio::detail::socket_ops::host_to_network_long(
425 multicast_address.to_uint());
426 ipv4_value_.imr_interface.s_addr =
427 boost::asio::detail::socket_ops::host_to_network_long(
428 network_interface.to_uint());
431 // Construct with multicast address and IPv6 network interface index.
432 explicit multicast_request(
433 const address_v6& multicast_address,
434 unsigned long network_interface = 0)
435 : ipv4_value_() // Zero-initialisation gives the "any" address.
437 using namespace std; // For memcpy.
438 address_v6::bytes_type bytes = multicast_address.to_bytes();
439 memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
440 if (network_interface)
441 ipv6_value_.ipv6mr_interface = network_interface;
443 ipv6_value_.ipv6mr_interface = multicast_address.scope_id();
446 // Get the level of the socket option.
447 template <typename Protocol>
448 int level(const Protocol& protocol) const
450 if (protocol.family() == PF_INET6)
455 // Get the name of the socket option.
456 template <typename Protocol>
457 int name(const Protocol& protocol) const
459 if (protocol.family() == PF_INET6)
464 // Get the address of the option data.
465 template <typename Protocol>
466 const void* data(const Protocol& protocol) const
468 if (protocol.family() == PF_INET6)
473 // Get the size of the option data.
474 template <typename Protocol>
475 std::size_t size(const Protocol& protocol) const
477 if (protocol.family() == PF_INET6)
478 return sizeof(ipv6_value_);
479 return sizeof(ipv4_value_);
483 boost::asio::detail::in4_mreq_type ipv4_value_;
484 boost::asio::detail::in6_mreq_type ipv6_value_;
487 // Helper template for implementing options that specify a network interface.
488 template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
489 class network_interface
492 // Default constructor.
496 boost::asio::detail::socket_ops::host_to_network_long(
497 address_v4::any().to_uint());
501 // Construct with IPv4 interface.
502 explicit network_interface(const address_v4& ipv4_interface)
505 boost::asio::detail::socket_ops::host_to_network_long(
506 ipv4_interface.to_uint());
510 // Construct with IPv6 interface.
511 explicit network_interface(unsigned int ipv6_interface)
514 boost::asio::detail::socket_ops::host_to_network_long(
515 address_v4::any().to_uint());
516 ipv6_value_ = ipv6_interface;
519 // Get the level of the socket option.
520 template <typename Protocol>
521 int level(const Protocol& protocol) const
523 if (protocol.family() == PF_INET6)
528 // Get the name of the socket option.
529 template <typename Protocol>
530 int name(const Protocol& protocol) const
532 if (protocol.family() == PF_INET6)
537 // Get the address of the option data.
538 template <typename Protocol>
539 const void* data(const Protocol& protocol) const
541 if (protocol.family() == PF_INET6)
546 // Get the size of the option data.
547 template <typename Protocol>
548 std::size_t size(const Protocol& protocol) const
550 if (protocol.family() == PF_INET6)
551 return sizeof(ipv6_value_);
552 return sizeof(ipv4_value_);
556 boost::asio::detail::in4_addr_type ipv4_value_;
557 unsigned int ipv6_value_;
560 } // namespace socket_option
561 } // namespace detail
566 #include <boost/asio/detail/pop_options.hpp>
568 #endif // BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP