From 2d2312a1d7dea5055ba96738c93b82fc83071e11 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Fri, 28 Feb 2014 09:00:45 -0600 Subject: [PATCH] add socket/security policies to translate asio -> websocketpp error codes The reason these were added to the socket policies rather than higher up is that in some cases translating error codes requires information from headers and libraries that are not present except when certain socket policies are being used. For example, SSL/TLS related errors can only be translated using openssl, which is only pulled in with the TLS socket policy. --- websocketpp/transport/asio/security/none.hpp | 17 +++++++++++ websocketpp/transport/asio/security/tls.hpp | 31 ++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/websocketpp/transport/asio/security/none.hpp b/websocketpp/transport/asio/security/none.hpp index 1c9486fa8d..96bf868589 100644 --- a/websocketpp/transport/asio/security/none.hpp +++ b/websocketpp/transport/asio/security/none.hpp @@ -229,6 +229,23 @@ protected: lib::error_code get_ec() const { return lib::error_code(); } + + /// Translate any security policy specific information about an error code + /** + * Translate_ec takes a boost error code and attempts to convert its value + * to an appropriate websocketpp error code. The plain socket policy does + * not presently provide any additional information so all errors will be + * reported as the generic transport pass_through error. + * + * @since 0.4.0-beta1 + * + * @param ec The error code to translate_ec + * @return The translated error code + */ + lib::error_code translate_ec(boost::system::error_code ec) { + // We don't know any more information about this error so pass through + return make_error_code(transport::error::pass_through); + } private: enum state { UNINITIALIZED = 0, diff --git a/websocketpp/transport/asio/security/tls.hpp b/websocketpp/transport/asio/security/tls.hpp index 2a743112bb..4f383e5a27 100644 --- a/websocketpp/transport/asio/security/tls.hpp +++ b/websocketpp/transport/asio/security/tls.hpp @@ -284,6 +284,37 @@ protected: void async_shutdown(socket_shutdown_handler callback) { m_socket->async_shutdown(callback); } + + /// Translate any security policy specific information about an error code + /** + * Translate_ec takes a boost error code and attempts to convert its value + * to an appropriate websocketpp error code. Any error that is determined to + * be related to TLS but does not have a more specific websocketpp error + * code is returned under the catch all error "tls_error". + * + * Non-TLS related errors are returned as the transport generic pass_through + * error. + * + * @since 0.4.0-beta1 + * + * @param ec The error code to translate_ec + * @return The translated error code + */ + lib::error_code translate_ec(boost::system::error_code ec) { + if (ec.category() == boost::asio::error::get_ssl_category()) { + if (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ) { + return make_error_code(transport::error::tls_short_read); + } else { + // We know it is a TLS related error, but otherwise don't know + // more. Pass through as TLS generic. + return make_error_code(transport::error::tls_error); + } + } else { + // We don't know any more information about this error so pass + // through + return make_error_code(transport::error::pass_through); + } + } private: socket_type::handshake_type get_handshake_type() { if (m_is_server) {