PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/boost/asio/ip/detail/socket_option.hpp

https://gitlab.com/NucleusStudios/SkyrimOnline
C++ Header | 568 lines | 445 code | 62 blank | 61 comment | 68 complexity | a7178b11825691c5dcd26d9fb6c17b3b MD5 | raw file
  1. //
  2. // detail/socket_option.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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 boost::asio::ip::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. boost::asio::ip::address_v6 ipv6_address = multicast_address.to_v6();
  359. boost::asio::ip::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 = 0;
  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_ulong());
  368. ipv4_value_.imr_interface.s_addr =
  369. boost::asio::detail::socket_ops::host_to_network_long(
  370. boost::asio::ip::address_v4::any().to_ulong());
  371. }
  372. }
  373. // Construct with multicast address and IPv4 address specifying an interface.
  374. explicit multicast_request(
  375. const boost::asio::ip::address_v4& multicast_address,
  376. const boost::asio::ip::address_v4& network_interface
  377. = boost::asio::ip::address_v4::any())
  378. : ipv6_value_() // Zero-initialisation gives the "any" address.
  379. {
  380. ipv4_value_.imr_multiaddr.s_addr =
  381. boost::asio::detail::socket_ops::host_to_network_long(
  382. multicast_address.to_ulong());
  383. ipv4_value_.imr_interface.s_addr =
  384. boost::asio::detail::socket_ops::host_to_network_long(
  385. network_interface.to_ulong());
  386. }
  387. // Construct with multicast address and IPv6 network interface index.
  388. explicit multicast_request(
  389. const boost::asio::ip::address_v6& multicast_address,
  390. unsigned long network_interface = 0)
  391. : ipv4_value_() // Zero-initialisation gives the "any" address.
  392. {
  393. using namespace std; // For memcpy.
  394. boost::asio::ip::address_v6::bytes_type bytes =
  395. multicast_address.to_bytes();
  396. memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
  397. ipv6_value_.ipv6mr_interface = network_interface;
  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. boost::asio::ip::address_v4::any().to_ulong());
  446. ipv6_value_ = 0;
  447. }
  448. // Construct with IPv4 interface.
  449. explicit network_interface(const boost::asio::ip::address_v4& ipv4_interface)
  450. {
  451. ipv4_value_.s_addr =
  452. boost::asio::detail::socket_ops::host_to_network_long(
  453. ipv4_interface.to_ulong());
  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. boost::asio::ip::address_v4::any().to_ulong());
  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