socket_option.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. //
  2. // socket_option.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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 ASIO_IP_DETAIL_SOCKET_OPTION_HPP
  11. #define 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 "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <cstring>
  19. #include <boost/config.hpp>
  20. #include <boost/throw_exception.hpp>
  21. #include "asio/detail/pop_options.hpp"
  22. #include "asio/ip/address.hpp"
  23. #include "asio/detail/socket_ops.hpp"
  24. #include "asio/detail/socket_types.hpp"
  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::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::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::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_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::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::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::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::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. {
  347. ipv4_value_.imr_multiaddr.s_addr =
  348. asio::detail::socket_ops::host_to_network_long(
  349. asio::ip::address_v4::any().to_ulong());
  350. ipv4_value_.imr_interface.s_addr =
  351. asio::detail::socket_ops::host_to_network_long(
  352. asio::ip::address_v4::any().to_ulong());
  353. asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
  354. ipv6_value_.ipv6mr_multiaddr = tmp_addr;
  355. ipv6_value_.ipv6mr_interface = 0;
  356. }
  357. // Construct with multicast address only.
  358. explicit multicast_request(const asio::ip::address& multicast_address)
  359. {
  360. if (multicast_address.is_v6())
  361. {
  362. ipv4_value_.imr_multiaddr.s_addr =
  363. asio::detail::socket_ops::host_to_network_long(
  364. asio::ip::address_v4::any().to_ulong());
  365. ipv4_value_.imr_interface.s_addr =
  366. asio::detail::socket_ops::host_to_network_long(
  367. asio::ip::address_v4::any().to_ulong());
  368. using namespace std; // For memcpy.
  369. asio::ip::address_v6 ipv6_address = multicast_address.to_v6();
  370. asio::ip::address_v6::bytes_type bytes = ipv6_address.to_bytes();
  371. memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.elems, 16);
  372. ipv6_value_.ipv6mr_interface = 0;
  373. }
  374. else
  375. {
  376. ipv4_value_.imr_multiaddr.s_addr =
  377. asio::detail::socket_ops::host_to_network_long(
  378. multicast_address.to_v4().to_ulong());
  379. ipv4_value_.imr_interface.s_addr =
  380. asio::detail::socket_ops::host_to_network_long(
  381. asio::ip::address_v4::any().to_ulong());
  382. asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
  383. ipv6_value_.ipv6mr_multiaddr = tmp_addr;
  384. ipv6_value_.ipv6mr_interface = 0;
  385. }
  386. }
  387. // Construct with multicast address and IPv4 address specifying an interface.
  388. explicit multicast_request(
  389. const asio::ip::address_v4& multicast_address,
  390. const asio::ip::address_v4& network_interface
  391. = asio::ip::address_v4::any())
  392. {
  393. ipv4_value_.imr_multiaddr.s_addr =
  394. asio::detail::socket_ops::host_to_network_long(
  395. multicast_address.to_ulong());
  396. ipv4_value_.imr_interface.s_addr =
  397. asio::detail::socket_ops::host_to_network_long(
  398. network_interface.to_ulong());
  399. asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
  400. ipv6_value_.ipv6mr_multiaddr = tmp_addr;
  401. ipv6_value_.ipv6mr_interface = 0;
  402. }
  403. // Construct with multicast address and IPv6 network interface index.
  404. explicit multicast_request(
  405. const asio::ip::address_v6& multicast_address,
  406. unsigned long network_interface = 0)
  407. {
  408. ipv4_value_.imr_multiaddr.s_addr =
  409. asio::detail::socket_ops::host_to_network_long(
  410. asio::ip::address_v4::any().to_ulong());
  411. ipv4_value_.imr_interface.s_addr =
  412. asio::detail::socket_ops::host_to_network_long(
  413. asio::ip::address_v4::any().to_ulong());
  414. using namespace std; // For memcpy.
  415. asio::ip::address_v6::bytes_type bytes =
  416. multicast_address.to_bytes();
  417. memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.elems, 16);
  418. ipv6_value_.ipv6mr_interface = network_interface;
  419. }
  420. // Get the level of the socket option.
  421. template <typename Protocol>
  422. int level(const Protocol& protocol) const
  423. {
  424. if (protocol.family() == PF_INET6)
  425. return IPv6_Level;
  426. return IPv4_Level;
  427. }
  428. // Get the name of the socket option.
  429. template <typename Protocol>
  430. int name(const Protocol& protocol) const
  431. {
  432. if (protocol.family() == PF_INET6)
  433. return IPv6_Name;
  434. return IPv4_Name;
  435. }
  436. // Get the address of the option data.
  437. template <typename Protocol>
  438. const void* data(const Protocol& protocol) const
  439. {
  440. if (protocol.family() == PF_INET6)
  441. return &ipv6_value_;
  442. return &ipv4_value_;
  443. }
  444. // Get the size of the option data.
  445. template <typename Protocol>
  446. std::size_t size(const Protocol& protocol) const
  447. {
  448. if (protocol.family() == PF_INET6)
  449. return sizeof(ipv6_value_);
  450. return sizeof(ipv4_value_);
  451. }
  452. private:
  453. asio::detail::in4_mreq_type ipv4_value_;
  454. asio::detail::in6_mreq_type ipv6_value_;
  455. };
  456. // Helper template for implementing options that specify a network interface.
  457. template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
  458. class network_interface
  459. {
  460. public:
  461. // Default constructor.
  462. network_interface()
  463. {
  464. ipv4_value_.s_addr =
  465. asio::detail::socket_ops::host_to_network_long(
  466. asio::ip::address_v4::any().to_ulong());
  467. ipv6_value_ = 0;
  468. }
  469. // Construct with IPv4 interface.
  470. explicit network_interface(const asio::ip::address_v4& ipv4_interface)
  471. {
  472. ipv4_value_.s_addr =
  473. asio::detail::socket_ops::host_to_network_long(
  474. ipv4_interface.to_ulong());
  475. ipv6_value_ = 0;
  476. }
  477. // Construct with IPv6 interface.
  478. explicit network_interface(unsigned int ipv6_interface)
  479. {
  480. ipv4_value_.s_addr =
  481. asio::detail::socket_ops::host_to_network_long(
  482. asio::ip::address_v4::any().to_ulong());
  483. ipv6_value_ = ipv6_interface;
  484. }
  485. // Get the level of the socket option.
  486. template <typename Protocol>
  487. int level(const Protocol& protocol) const
  488. {
  489. if (protocol.family() == PF_INET6)
  490. return IPv6_Level;
  491. return IPv4_Level;
  492. }
  493. // Get the name of the socket option.
  494. template <typename Protocol>
  495. int name(const Protocol& protocol) const
  496. {
  497. if (protocol.family() == PF_INET6)
  498. return IPv6_Name;
  499. return IPv4_Name;
  500. }
  501. // Get the address of the option data.
  502. template <typename Protocol>
  503. const void* data(const Protocol& protocol) const
  504. {
  505. if (protocol.family() == PF_INET6)
  506. return &ipv6_value_;
  507. return &ipv4_value_;
  508. }
  509. // Get the size of the option data.
  510. template <typename Protocol>
  511. std::size_t size(const Protocol& protocol) const
  512. {
  513. if (protocol.family() == PF_INET6)
  514. return sizeof(ipv6_value_);
  515. return sizeof(ipv4_value_);
  516. }
  517. private:
  518. asio::detail::in4_addr_type ipv4_value_;
  519. unsigned int ipv6_value_;
  520. };
  521. } // namespace socket_option
  522. } // namespace detail
  523. } // namespace ip
  524. } // namespace asio
  525. #include "asio/detail/pop_options.hpp"
  526. #endif // ASIO_IP_DETAIL_SOCKET_OPTION_HPP