You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015, Peter Thorson. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the WebSocket++ Project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #ifndef WEBSOCKETPP_TRANSPORT_ASIO_SOCKET_BASE_HPP
  28. #define WEBSOCKETPP_TRANSPORT_ASIO_SOCKET_BASE_HPP
  29. #include <websocketpp/common/asio.hpp>
  30. #include <websocketpp/common/memory.hpp>
  31. #include <websocketpp/common/functional.hpp>
  32. #include <websocketpp/common/system_error.hpp>
  33. #include <websocketpp/common/cpp11.hpp>
  34. #include <websocketpp/common/connection_hdl.hpp>
  35. #include <string>
  36. // Interface that sockets/security policies must implement
  37. /*
  38. * Endpoint Interface
  39. *
  40. * bool is_secure() const;
  41. * @return Whether or not the endpoint creates secure connections
  42. *
  43. * lib::error_code init(socket_con_ptr scon);
  44. * Called by the transport after a new connection is created to initialize
  45. * the socket component of the connection.
  46. * @param scon Pointer to the socket component of the connection
  47. * @return Error code (empty on success)
  48. */
  49. // Connection
  50. // TODO
  51. // set_hostname(std::string hostname)
  52. // pre_init(init_handler);
  53. // post_init(init_handler);
  54. namespace websocketpp {
  55. namespace transport {
  56. namespace asio {
  57. namespace socket {
  58. typedef lib::function<void(lib::asio::error_code const &)> shutdown_handler;
  59. /**
  60. * The transport::asio::socket::* classes are a set of security/socket related
  61. * policies and support code for the ASIO transport types.
  62. */
  63. /// Errors related to asio transport sockets
  64. namespace error {
  65. enum value {
  66. /// Catch-all error for security policy errors that don't fit in other
  67. /// categories
  68. security = 1,
  69. /// Catch-all error for socket component errors that don't fit in other
  70. /// categories
  71. socket,
  72. /// A function was called in a state that it was illegal to do so.
  73. invalid_state,
  74. /// The application was prompted to provide a TLS context and it was
  75. /// empty or otherwise invalid
  76. invalid_tls_context,
  77. /// TLS Handshake Timeout
  78. tls_handshake_timeout,
  79. /// pass_through from underlying library
  80. pass_through,
  81. /// Required tls_init handler not present
  82. missing_tls_init_handler,
  83. /// TLS Handshake Failed
  84. tls_handshake_failed,
  85. /// Failed to set TLS SNI hostname
  86. tls_failed_sni_hostname
  87. };
  88. } // namespace error
  89. /// Error category related to asio transport socket policies
  90. class socket_category : public lib::error_category {
  91. public:
  92. char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
  93. return "websocketpp.transport.asio.socket";
  94. }
  95. std::string message(int value) const {
  96. switch(value) {
  97. case error::security:
  98. return "Security policy error";
  99. case error::socket:
  100. return "Socket component error";
  101. case error::invalid_state:
  102. return "Invalid state";
  103. case error::invalid_tls_context:
  104. return "Invalid or empty TLS context supplied";
  105. case error::tls_handshake_timeout:
  106. return "TLS handshake timed out";
  107. case error::pass_through:
  108. return "Pass through from socket policy";
  109. case error::missing_tls_init_handler:
  110. return "Required tls_init handler not present.";
  111. case error::tls_handshake_failed:
  112. return "TLS handshake failed";
  113. case error::tls_failed_sni_hostname:
  114. return "Failed to set TLS SNI hostname";
  115. default:
  116. return "Unknown";
  117. }
  118. }
  119. };
  120. inline lib::error_category const & get_socket_category() {
  121. static socket_category instance;
  122. return instance;
  123. }
  124. inline lib::error_code make_error_code(error::value e) {
  125. return lib::error_code(static_cast<int>(e), get_socket_category());
  126. }
  127. /// Type of asio transport socket policy initialization handlers
  128. typedef lib::function<void(const lib::error_code&)> init_handler;
  129. } // namespace socket
  130. } // namespace asio
  131. } // namespace transport
  132. } // namespace websocketpp
  133. #endif // WEBSOCKETPP_TRANSPORT_ASIO_SOCKET_BASE_HPP