base.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2014, 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/memory.hpp>
  30. #include <websocketpp/common/functional.hpp>
  31. #include <websocketpp/common/system_error.hpp>
  32. #include <websocketpp/common/cpp11.hpp>
  33. #include <websocketpp/common/connection_hdl.hpp>
  34. #include <string>
  35. // Interface that sockets/security policies must implement
  36. /*
  37. * Endpoint Interface
  38. *
  39. * bool is_secure() const;
  40. * @return Whether or not the endpoint creates secure connections
  41. *
  42. * lib::error_code init(socket_con_ptr scon);
  43. * Called by the transport after a new connection is created to initialize
  44. * the socket component of the connection.
  45. * @param scon Pointer to the socket component of the connection
  46. * @return Error code (empty on success)
  47. */
  48. // Connection
  49. // TODO
  50. // set_hostname(std::string hostname)
  51. // pre_init(init_handler);
  52. // post_init(init_handler);
  53. namespace websocketpp {
  54. namespace transport {
  55. namespace asio {
  56. namespace socket {
  57. /**
  58. * The transport::asio::socket::* classes are a set of security/socket related
  59. * policies and support code for the ASIO transport types.
  60. */
  61. /// Errors related to asio transport sockets
  62. namespace error {
  63. enum value {
  64. /// Catch-all error for security policy errors that don't fit in other
  65. /// categories
  66. security = 1,
  67. /// Catch-all error for socket component errors that don't fit in other
  68. /// categories
  69. socket,
  70. /// A function was called in a state that it was illegal to do so.
  71. invalid_state,
  72. /// The application was prompted to provide a TLS context and it was
  73. /// empty or otherwise invalid
  74. invalid_tls_context,
  75. /// TLS Handshake Timeout
  76. tls_handshake_timeout,
  77. /// pass_through from underlying library
  78. pass_through,
  79. /// Required tls_init handler not present
  80. missing_tls_init_handler,
  81. /// TLS Handshake Failed
  82. tls_handshake_failed,
  83. /// Failed to set TLS SNI hostname
  84. tls_failed_sni_hostname
  85. };
  86. } // namespace error
  87. /// Error category related to asio transport socket policies
  88. class socket_category : public lib::error_category {
  89. public:
  90. char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
  91. return "websocketpp.transport.asio.socket";
  92. }
  93. std::string message(int value) const {
  94. switch(value) {
  95. case error::security:
  96. return "Security policy error";
  97. case error::socket:
  98. return "Socket component error";
  99. case error::invalid_state:
  100. return "Invalid state";
  101. case error::invalid_tls_context:
  102. return "Invalid or empty TLS context supplied";
  103. case error::tls_handshake_timeout:
  104. return "TLS handshake timed out";
  105. case error::pass_through:
  106. return "Pass through from socket policy";
  107. case error::missing_tls_init_handler:
  108. return "Required tls_init handler not present.";
  109. case error::tls_handshake_failed:
  110. return "TLS handshake failed";
  111. case error::tls_failed_sni_hostname:
  112. return "Failed to set TLS SNI hostname";
  113. default:
  114. return "Unknown";
  115. }
  116. }
  117. };
  118. inline lib::error_category const & get_socket_category() {
  119. static socket_category instance;
  120. return instance;
  121. }
  122. inline lib::error_code make_error_code(error::value e) {
  123. return lib::error_code(static_cast<int>(e), get_socket_category());
  124. }
  125. /// Type of asio transport socket policy initialization handlers
  126. typedef lib::function<void(const lib::error_code&)> init_handler;
  127. } // namespace socket
  128. } // namespace asio
  129. } // namespace transport
  130. } // namespace websocketpp
  131. #endif // WEBSOCKETPP_TRANSPORT_ASIO_SOCKET_BASE_HPP