raw_protocol.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // generic/raw_protocol.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_GENERIC_RAW_PROTOCOL_HPP
  11. #define BOOST_ASIO_GENERIC_RAW_PROTOCOL_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 <typeinfo>
  17. #include <boost/asio/basic_raw_socket.hpp>
  18. #include <boost/asio/detail/socket_types.hpp>
  19. #include <boost/asio/detail/throw_exception.hpp>
  20. #include <boost/asio/generic/basic_endpoint.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace generic {
  25. /// Encapsulates the flags needed for a generic raw socket.
  26. /**
  27. * The boost::asio::generic::raw_protocol class contains flags necessary for
  28. * raw sockets of any address family and protocol.
  29. *
  30. * @par Examples
  31. * Constructing using a native address family and socket protocol:
  32. * @code raw_protocol p(AF_INET, IPPROTO_ICMP); @endcode
  33. * Constructing from a specific protocol type:
  34. * @code raw_protocol p(boost::asio::ip::icmp::v4()); @endcode
  35. *
  36. * @par Thread Safety
  37. * @e Distinct @e objects: Safe.@n
  38. * @e Shared @e objects: Safe.
  39. *
  40. * @par Concepts:
  41. * Protocol.
  42. */
  43. class raw_protocol
  44. {
  45. public:
  46. /// Construct a protocol object for a specific address family and protocol.
  47. raw_protocol(int address_family, int socket_protocol)
  48. : family_(address_family),
  49. protocol_(socket_protocol)
  50. {
  51. }
  52. /// Construct a generic protocol object from a specific protocol.
  53. /**
  54. * @throws @c bad_cast Thrown if the source protocol is not raw-oriented.
  55. */
  56. template <typename Protocol>
  57. raw_protocol(const Protocol& source_protocol)
  58. : family_(source_protocol.family()),
  59. protocol_(source_protocol.protocol())
  60. {
  61. if (source_protocol.type() != type())
  62. {
  63. std::bad_cast ex;
  64. boost::asio::detail::throw_exception(ex);
  65. }
  66. }
  67. /// Obtain an identifier for the type of the protocol.
  68. int type() const BOOST_ASIO_NOEXCEPT
  69. {
  70. return BOOST_ASIO_OS_DEF(SOCK_RAW);
  71. }
  72. /// Obtain an identifier for the protocol.
  73. int protocol() const BOOST_ASIO_NOEXCEPT
  74. {
  75. return protocol_;
  76. }
  77. /// Obtain an identifier for the protocol family.
  78. int family() const BOOST_ASIO_NOEXCEPT
  79. {
  80. return family_;
  81. }
  82. /// Compare two protocols for equality.
  83. friend bool operator==(const raw_protocol& p1, const raw_protocol& p2)
  84. {
  85. return p1.family_ == p2.family_ && p1.protocol_ == p2.protocol_;
  86. }
  87. /// Compare two protocols for inequality.
  88. friend bool operator!=(const raw_protocol& p1, const raw_protocol& p2)
  89. {
  90. return !(p1 == p2);
  91. }
  92. /// The type of an endpoint.
  93. typedef basic_endpoint<raw_protocol> endpoint;
  94. /// The generic socket type.
  95. typedef basic_raw_socket<raw_protocol> socket;
  96. private:
  97. int family_;
  98. int protocol_;
  99. };
  100. } // namespace generic
  101. } // namespace asio
  102. } // namespace boost
  103. #include <boost/asio/detail/pop_options.hpp>
  104. #endif // BOOST_ASIO_GENERIC_RAW_PROTOCOL_HPP