socket_option.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //
  2. // detail/socket_option.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #ifndef BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP
  11. #define BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <cstring>
  18. #include <stdexcept>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/socket_types.hpp>
  21. #include <boost/asio/detail/throw_exception.hpp>
  22. #include <boost/asio/ip/address.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace ip {
  27. namespace detail {
  28. namespace socket_option {
  29. // Helper template for implementing multicast enable loopback options.
  30. template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
  31. class multicast_enable_loopback
  32. {
  33. public:
  34. #if defined(__sun) || defined(__osf__)
  35. typedef unsigned char ipv4_value_type;
  36. typedef unsigned char ipv6_value_type;
  37. #elif defined(_AIX) || defined(__hpux) || defined(__QNXNTO__)
  38. typedef unsigned char ipv4_value_type;
  39. typedef unsigned int ipv6_value_type;
  40. #else
  41. typedef int ipv4_value_type;
  42. typedef int ipv6_value_type;
  43. #endif
  44. // Default constructor.
  45. multicast_enable_loopback()
  46. : ipv4_value_(0),
  47. ipv6_value_(0)
  48. {
  49. }
  50. // Construct with a specific option value.
  51. explicit multicast_enable_loopback(bool v)
  52. : ipv4_value_(v ? 1 : 0),
  53. ipv6_value_(v ? 1 : 0)
  54. {
  55. }
  56. // Set the value of the boolean.
  57. multicast_enable_loopback& operator=(bool v)
  58. {
  59. ipv4_value_ = v ? 1 : 0;
  60. ipv6_value_ = v ? 1 : 0;
  61. return *this;
  62. }
  63. // Get the current value of the boolean.
  64. bool value() const
  65. {
  66. return !!ipv4_value_;
  67. }
  68. // Convert to bool.
  69. operator bool() const
  70. {
  71. return !!ipv4_value_;
  72. }
  73. // Test for false.
  74. bool operator!() const
  75. {
  76. return !ipv4_value_;
  77. }
  78. // Get the level of the socket option.
  79. template <typename Protocol>
  80. int level(const Protocol& protocol) const
  81. {
  82. if (protocol.family() == PF_INET6)
  83. return IPv6_Level;
  84. return IPv4_Level;
  85. }
  86. // Get the name of the socket option.
  87. template <typename Protocol>
  88. int name(const Protocol& protocol) const
  89. {
  90. if (protocol.family() == PF_INET6)
  91. return IPv6_Name;
  92. return IPv4_Name;
  93. }
  94. // Get the address of the boolean data.
  95. template <typename Protocol>
  96. void* data(const Protocol& protocol)
  97. {
  98. if (protocol.family() == PF_INET6)
  99. return &ipv6_value_;
  100. return &ipv4_value_;
  101. }
  102. // Get the address of the boolean data.
  103. template <typename Protocol>
  104. const void* data(const Protocol& protocol) const
  105. {
  106. if (protocol.family() == PF_INET6)
  107. return &ipv6_value_;
  108. return &ipv4_value_;
  109. }
  110. // Get the size of the boolean data.
  111. template <typename Protocol>
  112. std::size_t size(const Protocol& protocol) const
  113. {
  114. if (protocol.family() == PF_INET6)
  115. return sizeof(ipv6_value_);
  116. return sizeof(ipv4_value_);
  117. }
  118. // Set the size of the boolean data.
  119. template <typename Protocol>
  120. void resize(const Protocol& protocol, std::size_t s)
  121. {
  122. if (protocol.family() == PF_INET6)
  123. {
  124. if (s != sizeof(ipv6_value_))
  125. {
  126. std::length_error ex("multicast_enable_loopback socket option resize");
  127. boost::asio::detail::throw_exception(ex);
  128. }
  129. ipv4_value_ = ipv6_value_ ? 1 : 0;
  130. }
  131. else
  132. {
  133. if (s != sizeof(ipv4_value_))
  134. {
  135. std::length_error ex("multicast_enable_loopback socket option resize");
  136. boost::asio::detail::throw_exception(ex);
  137. }
  138. ipv6_value_ = ipv4_value_ ? 1 : 0;
  139. }
  140. }
  141. private:
  142. ipv4_value_type ipv4_value_;
  143. ipv6_value_type ipv6_value_;
  144. };
  145. // Helper template for implementing unicast hops options.
  146. template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
  147. class unicast_hops
  148. {
  149. public:
  150. // Default constructor.
  151. unicast_hops()
  152. : value_(0)
  153. {
  154. }
  155. // Construct with a specific option value.
  156. explicit unicast_hops(int v)
  157. : value_(v)
  158. {
  159. }
  160. // Set the value of the option.
  161. unicast_hops& operator=(int v)
  162. {
  163. value_ = v;
  164. return *this;
  165. }
  166. // Get the current value of the option.
  167. int value() const
  168. {
  169. return value_;
  170. }
  171. // Get the level of the socket option.
  172. template <typename Protocol>
  173. int level(const Protocol& protocol) const
  174. {
  175. if (protocol.family() == PF_INET6)
  176. return IPv6_Level;
  177. return IPv4_Level;
  178. }
  179. // Get the name of the socket option.
  180. template <typename Protocol>
  181. int name(const Protocol& protocol) const
  182. {
  183. if (protocol.family() == PF_INET6)
  184. return IPv6_Name;
  185. return IPv4_Name;
  186. }
  187. // Get the address of the data.
  188. template <typename Protocol>
  189. int* data(const Protocol&)
  190. {
  191. return &value_;
  192. }
  193. // Get the address of the data.
  194. template <typename Protocol>
  195. const int* data(const Protocol&) const
  196. {
  197. return &value_;
  198. }
  199. // Get the size of the data.
  200. template <typename Protocol>
  201. std::size_t size(const Protocol&) const
  202. {
  203. return sizeof(value_);
  204. }
  205. // Set the size of the data.
  206. template <typename Protocol>
  207. void resize(const Protocol&, std::size_t s)
  208. {
  209. if (s != sizeof(value_))
  210. {
  211. std::length_error ex("unicast hops socket option resize");
  212. boost::asio::detail::throw_exception(ex);
  213. }
  214. #if defined(__hpux)
  215. if (value_ < 0)
  216. value_ = value_ & 0xFF;
  217. #endif
  218. }
  219. private:
  220. int value_;
  221. };
  222. // Helper template for implementing multicast hops options.
  223. template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
  224. class multicast_hops
  225. {
  226. public:
  227. #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  228. typedef int ipv4_value_type;
  229. #else
  230. typedef unsigned char ipv4_value_type;
  231. #endif
  232. typedef int ipv6_value_type;
  233. // Default constructor.
  234. multicast_hops()
  235. : ipv4_value_(0),
  236. ipv6_value_(0)
  237. {
  238. }
  239. // Construct with a specific option value.
  240. explicit multicast_hops(int v)
  241. {
  242. if (v < 0 || v > 255)
  243. {
  244. std::out_of_range ex("multicast hops value out of range");
  245. boost::asio::detail::throw_exception(ex);
  246. }
  247. ipv4_value_ = (ipv4_value_type)v;
  248. ipv6_value_ = v;
  249. }
  250. // Set the value of the option.
  251. multicast_hops& operator=(int v)
  252. {
  253. if (v < 0 || v > 255)
  254. {
  255. std::out_of_range ex("multicast hops value out of range");
  256. boost::asio::detail::throw_exception(ex);
  257. }
  258. ipv4_value_ = (ipv4_value_type)v;
  259. ipv6_value_ = v;
  260. return *this;
  261. }
  262. // Get the current value of the option.
  263. int value() const
  264. {
  265. return ipv6_value_;
  266. }
  267. // Get the level of the socket option.
  268. template <typename Protocol>
  269. int level(const Protocol& protocol) const
  270. {
  271. if (protocol.family() == PF_INET6)
  272. return IPv6_Level;
  273. return IPv4_Level;
  274. }
  275. // Get the name of the socket option.
  276. template <typename Protocol>
  277. int name(const Protocol& protocol) const
  278. {
  279. if (protocol.family() == PF_INET6)
  280. return IPv6_Name;
  281. return IPv4_Name;
  282. }
  283. // Get the address of the data.
  284. template <typename Protocol>
  285. void* data(const Protocol& protocol)
  286. {
  287. if (protocol.family() == PF_INET6)
  288. return &ipv6_value_;
  289. return &ipv4_value_;
  290. }
  291. // Get the address of the data.
  292. template <typename Protocol>
  293. const void* data(const Protocol& protocol) const
  294. {
  295. if (protocol.family() == PF_INET6)
  296. return &ipv6_value_;
  297. return &ipv4_value_;
  298. }
  299. // Get the size of the data.
  300. template <typename Protocol>
  301. std::size_t size(const Protocol& protocol) const
  302. {
  303. if (protocol.family() == PF_INET6)
  304. return sizeof(ipv6_value_);
  305. return sizeof(ipv4_value_);
  306. }
  307. // Set the size of the data.
  308. template <typename Protocol>
  309. void resize(const Protocol& protocol, std::size_t s)
  310. {
  311. if (protocol.family() == PF_INET6)
  312. {
  313. if (s != sizeof(ipv6_value_))
  314. {
  315. std::length_error ex("multicast hops socket option resize");
  316. boost::asio::detail::throw_exception(ex);
  317. }
  318. if (ipv6_value_ < 0)
  319. ipv4_value_ = 0;
  320. else if (ipv6_value_ > 255)
  321. ipv4_value_ = 255;
  322. else
  323. ipv4_value_ = (ipv4_value_type)ipv6_value_;
  324. }
  325. else
  326. {
  327. if (s != sizeof(ipv4_value_))
  328. {
  329. std::length_error ex("multicast hops socket option resize");
  330. boost::asio::detail::throw_exception(ex);
  331. }
  332. ipv6_value_ = ipv4_value_;
  333. }
  334. }
  335. private:
  336. ipv4_value_type ipv4_value_;
  337. ipv6_value_type ipv6_value_;
  338. };
  339. // Helper template for implementing ip_mreq-based options.
  340. template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
  341. class multicast_request
  342. {
  343. public:
  344. // Default constructor.
  345. multicast_request()
  346. : ipv4_value_(), // Zero-initialisation gives the "any" address.
  347. ipv6_value_() // Zero-initialisation gives the "any" address.
  348. {
  349. }
  350. // Construct with multicast address only.
  351. explicit multicast_request(const address& multicast_address)
  352. : ipv4_value_(), // Zero-initialisation gives the "any" address.
  353. ipv6_value_() // Zero-initialisation gives the "any" address.
  354. {
  355. if (multicast_address.is_v6())
  356. {
  357. using namespace std; // For memcpy.
  358. address_v6 ipv6_address = multicast_address.to_v6();
  359. address_v6::bytes_type bytes = ipv6_address.to_bytes();
  360. memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
  361. ipv6_value_.ipv6mr_interface = ipv6_address.scope_id();
  362. }
  363. else
  364. {
  365. ipv4_value_.imr_multiaddr.s_addr =
  366. boost::asio::detail::socket_ops::host_to_network_long(
  367. multicast_address.to_v4().to_uint());
  368. ipv4_value_.imr_interface.s_addr =
  369. boost::asio::detail::socket_ops::host_to_network_long(
  370. address_v4::any().to_uint());
  371. }
  372. }
  373. // Construct with multicast address and IPv4 address specifying an interface.
  374. explicit multicast_request(const address_v4& multicast_address,
  375. const address_v4& network_interface = address_v4::any())
  376. : ipv6_value_() // Zero-initialisation gives the "any" address.
  377. {
  378. ipv4_value_.imr_multiaddr.s_addr =
  379. boost::asio::detail::socket_ops::host_to_network_long(
  380. multicast_address.to_uint());
  381. ipv4_value_.imr_interface.s_addr =
  382. boost::asio::detail::socket_ops::host_to_network_long(
  383. network_interface.to_uint());
  384. }
  385. // Construct with multicast address and IPv6 network interface index.
  386. explicit multicast_request(
  387. const address_v6& multicast_address,
  388. unsigned long network_interface = 0)
  389. : ipv4_value_() // Zero-initialisation gives the "any" address.
  390. {
  391. using namespace std; // For memcpy.
  392. address_v6::bytes_type bytes = multicast_address.to_bytes();
  393. memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
  394. if (network_interface)
  395. ipv6_value_.ipv6mr_interface = network_interface;
  396. else
  397. ipv6_value_.ipv6mr_interface = multicast_address.scope_id();
  398. }
  399. // Get the level of the socket option.
  400. template <typename Protocol>
  401. int level(const Protocol& protocol) const
  402. {
  403. if (protocol.family() == PF_INET6)
  404. return IPv6_Level;
  405. return IPv4_Level;
  406. }
  407. // Get the name of the socket option.
  408. template <typename Protocol>
  409. int name(const Protocol& protocol) const
  410. {
  411. if (protocol.family() == PF_INET6)
  412. return IPv6_Name;
  413. return IPv4_Name;
  414. }
  415. // Get the address of the option data.
  416. template <typename Protocol>
  417. const void* data(const Protocol& protocol) const
  418. {
  419. if (protocol.family() == PF_INET6)
  420. return &ipv6_value_;
  421. return &ipv4_value_;
  422. }
  423. // Get the size of the option data.
  424. template <typename Protocol>
  425. std::size_t size(const Protocol& protocol) const
  426. {
  427. if (protocol.family() == PF_INET6)
  428. return sizeof(ipv6_value_);
  429. return sizeof(ipv4_value_);
  430. }
  431. private:
  432. boost::asio::detail::in4_mreq_type ipv4_value_;
  433. boost::asio::detail::in6_mreq_type ipv6_value_;
  434. };
  435. // Helper template for implementing options that specify a network interface.
  436. template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
  437. class network_interface
  438. {
  439. public:
  440. // Default constructor.
  441. network_interface()
  442. {
  443. ipv4_value_.s_addr =
  444. boost::asio::detail::socket_ops::host_to_network_long(
  445. address_v4::any().to_uint());
  446. ipv6_value_ = 0;
  447. }
  448. // Construct with IPv4 interface.
  449. explicit network_interface(const address_v4& ipv4_interface)
  450. {
  451. ipv4_value_.s_addr =
  452. boost::asio::detail::socket_ops::host_to_network_long(
  453. ipv4_interface.to_uint());
  454. ipv6_value_ = 0;
  455. }
  456. // Construct with IPv6 interface.
  457. explicit network_interface(unsigned int ipv6_interface)
  458. {
  459. ipv4_value_.s_addr =
  460. boost::asio::detail::socket_ops::host_to_network_long(
  461. address_v4::any().to_uint());
  462. ipv6_value_ = ipv6_interface;
  463. }
  464. // Get the level of the socket option.
  465. template <typename Protocol>
  466. int level(const Protocol& protocol) const
  467. {
  468. if (protocol.family() == PF_INET6)
  469. return IPv6_Level;
  470. return IPv4_Level;
  471. }
  472. // Get the name of the socket option.
  473. template <typename Protocol>
  474. int name(const Protocol& protocol) const
  475. {
  476. if (protocol.family() == PF_INET6)
  477. return IPv6_Name;
  478. return IPv4_Name;
  479. }
  480. // Get the address of the option data.
  481. template <typename Protocol>
  482. const void* data(const Protocol& protocol) const
  483. {
  484. if (protocol.family() == PF_INET6)
  485. return &ipv6_value_;
  486. return &ipv4_value_;
  487. }
  488. // Get the size of the option data.
  489. template <typename Protocol>
  490. std::size_t size(const Protocol& protocol) const
  491. {
  492. if (protocol.family() == PF_INET6)
  493. return sizeof(ipv6_value_);
  494. return sizeof(ipv4_value_);
  495. }
  496. private:
  497. boost::asio::detail::in4_addr_type ipv4_value_;
  498. unsigned int ipv6_value_;
  499. };
  500. } // namespace socket_option
  501. } // namespace detail
  502. } // namespace ip
  503. } // namespace asio
  504. } // namespace boost
  505. #include <boost/asio/detail/pop_options.hpp>
  506. #endif // BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP