Chris@16: // Chris@16: // detail/socket_option.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP Chris@16: #define BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace ip { Chris@16: namespace detail { Chris@16: namespace socket_option { Chris@16: Chris@16: // Helper template for implementing multicast enable loopback options. Chris@16: template Chris@16: class multicast_enable_loopback Chris@16: { Chris@16: public: Chris@16: #if defined(__sun) || defined(__osf__) Chris@16: typedef unsigned char ipv4_value_type; Chris@16: typedef unsigned char ipv6_value_type; Chris@16: #elif defined(_AIX) || defined(__hpux) || defined(__QNXNTO__) Chris@16: typedef unsigned char ipv4_value_type; Chris@16: typedef unsigned int ipv6_value_type; Chris@16: #else Chris@16: typedef int ipv4_value_type; Chris@16: typedef int ipv6_value_type; Chris@16: #endif Chris@16: Chris@16: // Default constructor. Chris@16: multicast_enable_loopback() Chris@16: : ipv4_value_(0), Chris@16: ipv6_value_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: // Construct with a specific option value. Chris@16: explicit multicast_enable_loopback(bool v) Chris@16: : ipv4_value_(v ? 1 : 0), Chris@16: ipv6_value_(v ? 1 : 0) Chris@16: { Chris@16: } Chris@16: Chris@16: // Set the value of the boolean. Chris@16: multicast_enable_loopback& operator=(bool v) Chris@16: { Chris@16: ipv4_value_ = v ? 1 : 0; Chris@16: ipv6_value_ = v ? 1 : 0; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Get the current value of the boolean. Chris@16: bool value() const Chris@16: { Chris@16: return !!ipv4_value_; Chris@16: } Chris@16: Chris@16: // Convert to bool. Chris@16: operator bool() const Chris@16: { Chris@16: return !!ipv4_value_; Chris@16: } Chris@16: Chris@16: // Test for false. Chris@16: bool operator!() const Chris@16: { Chris@16: return !ipv4_value_; Chris@16: } Chris@16: Chris@16: // Get the level of the socket option. Chris@16: template Chris@16: int level(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Level; Chris@16: return IPv4_Level; Chris@16: } Chris@16: Chris@16: // Get the name of the socket option. Chris@16: template Chris@16: int name(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Name; Chris@16: return IPv4_Name; Chris@16: } Chris@16: Chris@16: // Get the address of the boolean data. Chris@16: template Chris@16: void* data(const Protocol& protocol) Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return &ipv6_value_; Chris@16: return &ipv4_value_; Chris@16: } Chris@16: Chris@16: // Get the address of the boolean data. Chris@16: template Chris@16: const void* data(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return &ipv6_value_; Chris@16: return &ipv4_value_; Chris@16: } Chris@16: Chris@16: // Get the size of the boolean data. Chris@16: template Chris@16: std::size_t size(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return sizeof(ipv6_value_); Chris@16: return sizeof(ipv4_value_); Chris@16: } Chris@16: Chris@16: // Set the size of the boolean data. Chris@16: template Chris@16: void resize(const Protocol& protocol, std::size_t s) Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: { Chris@16: if (s != sizeof(ipv6_value_)) Chris@16: { Chris@16: std::length_error ex("multicast_enable_loopback socket option resize"); Chris@16: boost::asio::detail::throw_exception(ex); Chris@16: } Chris@16: ipv4_value_ = ipv6_value_ ? 1 : 0; Chris@16: } Chris@16: else Chris@16: { Chris@16: if (s != sizeof(ipv4_value_)) Chris@16: { Chris@16: std::length_error ex("multicast_enable_loopback socket option resize"); Chris@16: boost::asio::detail::throw_exception(ex); Chris@16: } Chris@16: ipv6_value_ = ipv4_value_ ? 1 : 0; Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: ipv4_value_type ipv4_value_; Chris@16: ipv6_value_type ipv6_value_; Chris@16: }; Chris@16: Chris@16: // Helper template for implementing unicast hops options. Chris@16: template Chris@16: class unicast_hops Chris@16: { Chris@16: public: Chris@16: // Default constructor. Chris@16: unicast_hops() Chris@16: : value_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: // Construct with a specific option value. Chris@16: explicit unicast_hops(int v) Chris@16: : value_(v) Chris@16: { Chris@16: } Chris@16: Chris@16: // Set the value of the option. Chris@16: unicast_hops& operator=(int v) Chris@16: { Chris@16: value_ = v; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Get the current value of the option. Chris@16: int value() const Chris@16: { Chris@16: return value_; Chris@16: } Chris@16: Chris@16: // Get the level of the socket option. Chris@16: template Chris@16: int level(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Level; Chris@16: return IPv4_Level; Chris@16: } Chris@16: Chris@16: // Get the name of the socket option. Chris@16: template Chris@16: int name(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Name; Chris@16: return IPv4_Name; Chris@16: } Chris@16: Chris@16: // Get the address of the data. Chris@16: template Chris@16: int* data(const Protocol&) Chris@16: { Chris@16: return &value_; Chris@16: } Chris@16: Chris@16: // Get the address of the data. Chris@16: template Chris@16: const int* data(const Protocol&) const Chris@16: { Chris@16: return &value_; Chris@16: } Chris@16: Chris@16: // Get the size of the data. Chris@16: template Chris@16: std::size_t size(const Protocol&) const Chris@16: { Chris@16: return sizeof(value_); Chris@16: } Chris@16: Chris@16: // Set the size of the data. Chris@16: template Chris@16: void resize(const Protocol&, std::size_t s) Chris@16: { Chris@16: if (s != sizeof(value_)) Chris@16: { Chris@16: std::length_error ex("unicast hops socket option resize"); Chris@16: boost::asio::detail::throw_exception(ex); Chris@16: } Chris@16: #if defined(__hpux) Chris@16: if (value_ < 0) Chris@16: value_ = value_ & 0xFF; Chris@16: #endif Chris@16: } Chris@16: Chris@16: private: Chris@16: int value_; Chris@16: }; Chris@16: Chris@16: // Helper template for implementing multicast hops options. Chris@16: template Chris@16: class multicast_hops Chris@16: { Chris@16: public: Chris@16: #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE) Chris@16: typedef int ipv4_value_type; Chris@16: #else Chris@16: typedef unsigned char ipv4_value_type; Chris@16: #endif Chris@16: typedef int ipv6_value_type; Chris@16: Chris@16: // Default constructor. Chris@16: multicast_hops() Chris@16: : ipv4_value_(0), Chris@16: ipv6_value_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: // Construct with a specific option value. Chris@16: explicit multicast_hops(int v) Chris@16: { Chris@16: if (v < 0 || v > 255) Chris@16: { Chris@16: std::out_of_range ex("multicast hops value out of range"); Chris@16: boost::asio::detail::throw_exception(ex); Chris@16: } Chris@16: ipv4_value_ = (ipv4_value_type)v; Chris@16: ipv6_value_ = v; Chris@16: } Chris@16: Chris@16: // Set the value of the option. Chris@16: multicast_hops& operator=(int v) Chris@16: { Chris@16: if (v < 0 || v > 255) Chris@16: { Chris@16: std::out_of_range ex("multicast hops value out of range"); Chris@16: boost::asio::detail::throw_exception(ex); Chris@16: } Chris@16: ipv4_value_ = (ipv4_value_type)v; Chris@16: ipv6_value_ = v; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Get the current value of the option. Chris@16: int value() const Chris@16: { Chris@16: return ipv6_value_; Chris@16: } Chris@16: Chris@16: // Get the level of the socket option. Chris@16: template Chris@16: int level(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Level; Chris@16: return IPv4_Level; Chris@16: } Chris@16: Chris@16: // Get the name of the socket option. Chris@16: template Chris@16: int name(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Name; Chris@16: return IPv4_Name; Chris@16: } Chris@16: Chris@16: // Get the address of the data. Chris@16: template Chris@16: void* data(const Protocol& protocol) Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return &ipv6_value_; Chris@16: return &ipv4_value_; Chris@16: } Chris@16: Chris@16: // Get the address of the data. Chris@16: template Chris@16: const void* data(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return &ipv6_value_; Chris@16: return &ipv4_value_; Chris@16: } Chris@16: Chris@16: // Get the size of the data. Chris@16: template Chris@16: std::size_t size(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return sizeof(ipv6_value_); Chris@16: return sizeof(ipv4_value_); Chris@16: } Chris@16: Chris@16: // Set the size of the data. Chris@16: template Chris@16: void resize(const Protocol& protocol, std::size_t s) Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: { Chris@16: if (s != sizeof(ipv6_value_)) Chris@16: { Chris@16: std::length_error ex("multicast hops socket option resize"); Chris@16: boost::asio::detail::throw_exception(ex); Chris@16: } Chris@16: if (ipv6_value_ < 0) Chris@16: ipv4_value_ = 0; Chris@16: else if (ipv6_value_ > 255) Chris@16: ipv4_value_ = 255; Chris@16: else Chris@16: ipv4_value_ = (ipv4_value_type)ipv6_value_; Chris@16: } Chris@16: else Chris@16: { Chris@16: if (s != sizeof(ipv4_value_)) Chris@16: { Chris@16: std::length_error ex("multicast hops socket option resize"); Chris@16: boost::asio::detail::throw_exception(ex); Chris@16: } Chris@16: ipv6_value_ = ipv4_value_; Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: ipv4_value_type ipv4_value_; Chris@16: ipv6_value_type ipv6_value_; Chris@16: }; Chris@16: Chris@16: // Helper template for implementing ip_mreq-based options. Chris@16: template Chris@16: class multicast_request Chris@16: { Chris@16: public: Chris@16: // Default constructor. Chris@16: multicast_request() Chris@16: : ipv4_value_(), // Zero-initialisation gives the "any" address. Chris@16: ipv6_value_() // Zero-initialisation gives the "any" address. Chris@16: { Chris@16: } Chris@16: Chris@16: // Construct with multicast address only. Chris@16: explicit multicast_request(const boost::asio::ip::address& multicast_address) Chris@16: : ipv4_value_(), // Zero-initialisation gives the "any" address. Chris@16: ipv6_value_() // Zero-initialisation gives the "any" address. Chris@16: { Chris@16: if (multicast_address.is_v6()) Chris@16: { Chris@16: using namespace std; // For memcpy. Chris@16: boost::asio::ip::address_v6 ipv6_address = multicast_address.to_v6(); Chris@16: boost::asio::ip::address_v6::bytes_type bytes = ipv6_address.to_bytes(); Chris@16: memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16); Chris@101: ipv6_value_.ipv6mr_interface = ipv6_address.scope_id(); Chris@16: } Chris@16: else Chris@16: { Chris@16: ipv4_value_.imr_multiaddr.s_addr = Chris@16: boost::asio::detail::socket_ops::host_to_network_long( Chris@16: multicast_address.to_v4().to_ulong()); Chris@16: ipv4_value_.imr_interface.s_addr = Chris@16: boost::asio::detail::socket_ops::host_to_network_long( Chris@16: boost::asio::ip::address_v4::any().to_ulong()); Chris@16: } Chris@16: } Chris@16: Chris@16: // Construct with multicast address and IPv4 address specifying an interface. Chris@16: explicit multicast_request( Chris@16: const boost::asio::ip::address_v4& multicast_address, Chris@16: const boost::asio::ip::address_v4& network_interface Chris@16: = boost::asio::ip::address_v4::any()) Chris@16: : ipv6_value_() // Zero-initialisation gives the "any" address. Chris@16: { Chris@16: ipv4_value_.imr_multiaddr.s_addr = Chris@16: boost::asio::detail::socket_ops::host_to_network_long( Chris@16: multicast_address.to_ulong()); Chris@16: ipv4_value_.imr_interface.s_addr = Chris@16: boost::asio::detail::socket_ops::host_to_network_long( Chris@16: network_interface.to_ulong()); Chris@16: } Chris@16: Chris@16: // Construct with multicast address and IPv6 network interface index. Chris@16: explicit multicast_request( Chris@16: const boost::asio::ip::address_v6& multicast_address, Chris@16: unsigned long network_interface = 0) Chris@16: : ipv4_value_() // Zero-initialisation gives the "any" address. Chris@16: { Chris@16: using namespace std; // For memcpy. Chris@16: boost::asio::ip::address_v6::bytes_type bytes = Chris@16: multicast_address.to_bytes(); Chris@16: memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16); Chris@101: if (network_interface) Chris@101: ipv6_value_.ipv6mr_interface = network_interface; Chris@101: else Chris@101: ipv6_value_.ipv6mr_interface = multicast_address.scope_id(); Chris@16: } Chris@16: Chris@16: // Get the level of the socket option. Chris@16: template Chris@16: int level(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Level; Chris@16: return IPv4_Level; Chris@16: } Chris@16: Chris@16: // Get the name of the socket option. Chris@16: template Chris@16: int name(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Name; Chris@16: return IPv4_Name; Chris@16: } Chris@16: Chris@16: // Get the address of the option data. Chris@16: template Chris@16: const void* data(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return &ipv6_value_; Chris@16: return &ipv4_value_; Chris@16: } Chris@16: Chris@16: // Get the size of the option data. Chris@16: template Chris@16: std::size_t size(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return sizeof(ipv6_value_); Chris@16: return sizeof(ipv4_value_); Chris@16: } Chris@16: Chris@16: private: Chris@16: boost::asio::detail::in4_mreq_type ipv4_value_; Chris@16: boost::asio::detail::in6_mreq_type ipv6_value_; Chris@16: }; Chris@16: Chris@16: // Helper template for implementing options that specify a network interface. Chris@16: template Chris@16: class network_interface Chris@16: { Chris@16: public: Chris@16: // Default constructor. Chris@16: network_interface() Chris@16: { Chris@16: ipv4_value_.s_addr = Chris@16: boost::asio::detail::socket_ops::host_to_network_long( Chris@16: boost::asio::ip::address_v4::any().to_ulong()); Chris@16: ipv6_value_ = 0; Chris@16: } Chris@16: Chris@16: // Construct with IPv4 interface. Chris@16: explicit network_interface(const boost::asio::ip::address_v4& ipv4_interface) Chris@16: { Chris@16: ipv4_value_.s_addr = Chris@16: boost::asio::detail::socket_ops::host_to_network_long( Chris@16: ipv4_interface.to_ulong()); Chris@16: ipv6_value_ = 0; Chris@16: } Chris@16: Chris@16: // Construct with IPv6 interface. Chris@16: explicit network_interface(unsigned int ipv6_interface) Chris@16: { Chris@16: ipv4_value_.s_addr = Chris@16: boost::asio::detail::socket_ops::host_to_network_long( Chris@16: boost::asio::ip::address_v4::any().to_ulong()); Chris@16: ipv6_value_ = ipv6_interface; Chris@16: } Chris@16: Chris@16: // Get the level of the socket option. Chris@16: template Chris@16: int level(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Level; Chris@16: return IPv4_Level; Chris@16: } Chris@16: Chris@16: // Get the name of the socket option. Chris@16: template Chris@16: int name(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return IPv6_Name; Chris@16: return IPv4_Name; Chris@16: } Chris@16: Chris@16: // Get the address of the option data. Chris@16: template Chris@16: const void* data(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return &ipv6_value_; Chris@16: return &ipv4_value_; Chris@16: } Chris@16: Chris@16: // Get the size of the option data. Chris@16: template Chris@16: std::size_t size(const Protocol& protocol) const Chris@16: { Chris@16: if (protocol.family() == PF_INET6) Chris@16: return sizeof(ipv6_value_); Chris@16: return sizeof(ipv4_value_); Chris@16: } Chris@16: Chris@16: private: Chris@16: boost::asio::detail::in4_addr_type ipv4_value_; Chris@16: unsigned int ipv6_value_; Chris@16: }; Chris@16: Chris@16: } // namespace socket_option Chris@16: } // namespace detail Chris@16: } // namespace ip Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP