28 #ifndef WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
29 #define WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
31 #include <websocketpp/transport/asio/security/base.hpp>
33 #include <websocketpp/common/connection_hdl.hpp>
34 #include <websocketpp/common/functional.hpp>
35 #include <websocketpp/common/memory.hpp>
37 #include <boost/asio.hpp>
38 #include <boost/asio/ssl.hpp>
39 #include <boost/system/error_code.hpp>
49 namespace tls_socket {
52 typedef lib::function<void(
connection_hdl,boost::asio::ssl::stream<
55 typedef lib::function<lib::shared_ptr<boost::asio::ssl::context>(
connection_hdl)>
63 class connection :
public lib::enable_shared_from_this<connection> {
68 typedef lib::shared_ptr<type>
ptr;
71 typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket>
socket_type;
77 typedef lib::shared_ptr<boost::asio::io_service::strand>
strand_ptr;
79 typedef lib::shared_ptr<boost::asio::ssl::context>
context_ptr;
81 typedef boost::system::error_code boost_error;
90 return shared_from_this();
106 return m_socket->lowest_layer();
114 return m_socket->next_layer();
134 m_socket_init_handler = h;
147 m_tls_init_handler = h;
163 boost::system::error_code bec;
164 boost::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(bec);
168 s <<
"Error getting remote endpoint: " << bec
169 <<
" (" << bec.message() <<
")";
172 ec = lib::error_code();
187 lib::error_code
init_asio (io_service_ptr service, strand_ptr strand,
190 if (!m_tls_init_handler) {
193 m_context = m_tls_init_handler(m_hdl);
198 m_socket = lib::make_shared<socket_type>(
199 _WEBSOCKETPP_REF(*service),lib::ref(*m_context));
201 m_io_service = service;
203 m_is_server = is_server;
205 return lib::error_code();
218 if (m_socket_init_handler) {
222 callback(lib::error_code());
238 m_socket->async_handshake(
239 get_handshake_type(),
240 m_strand->wrap(lib::bind(
243 lib::placeholders::_1
247 m_socket->async_handshake(
248 get_handshake_type(),
252 lib::placeholders::_1
269 void handle_init(
init_handler callback,boost::system::error_code
const & ec)
274 m_ec = lib::error_code();
280 lib::error_code get_ec()
const {
289 void async_shutdown(socket_shutdown_handler callback) {
290 m_socket->async_shutdown(callback);
309 if (ec.category() == boost::asio::error::get_ssl_category()) {
310 if (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ) {
324 socket_type::handshake_type get_handshake_type() {
326 return boost::asio::ssl::stream_base::server;
328 return boost::asio::ssl::stream_base::client;
332 io_service_ptr m_io_service;
334 context_ptr m_context;
338 lib::error_code m_ec;
341 socket_init_handler m_socket_init_handler;
380 m_socket_init_handler = h;
393 m_tls_init_handler = h;
405 lib::error_code
init(socket_con_ptr scon) {
406 scon->set_socket_init_handler(m_socket_init_handler);
407 scon->set_tls_init_handler(m_tls_init_handler);
408 return lib::error_code();
412 socket_init_handler m_socket_init_handler;
421 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
lib::shared_ptr< boost::asio::ssl::context > context_ptr
Type of a shared pointer to the ASIO TLS context being used.
TLS enabled Boost ASIO connection socket component.
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
lib::error_code make_error_code(error::value e)
Create an error code with the given value and the asio transport category.
bool is_secure() const
Checks whether the endpoint creates secure connections.
void pre_init(init_handler callback)
Pre-initialize security policy.
bool is_secure() const
Check whether or not this connection is secure.
connection type
Type of this connection socket component.
void set_handle(connection_hdl hdl)
Sets the connection handle.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
Required tls_init handler not present.
lib::shared_ptr< boost::asio::io_service::strand > strand_ptr
Type of a pointer to the ASIO io_service strand being used.
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
underlying transport pass through
socket_type::next_layer_type & get_next_layer()
Retrieve a pointer to the layer below the ssl stream.
connection socket_con_type
The type of the corresponding connection socket component.
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
lib::error_code translate_ec(boost::system::error_code ec)
Translate any security policy specific information about an error code.
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the ASIO socket being used.
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
ptr get_shared()
Get a shared pointer to this component.
there was an error in the underlying transport library
lib::function< lib::shared_ptr< boost::asio::ssl::context >connection_hdl)> tls_init_handler
The signature of the tls_init_handler for this socket policy.
Namespace for the WebSocket++ project.
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
Perform one time initializations.
socket_type::lowest_layer_type & get_raw_socket()
Retrieve a pointer to the underlying socket.
endpoint type
The type of this endpoint socket component.
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > socket_type
Type of the ASIO socket being used.
void post_init(init_handler callback)
Post-initialize security policy.
TLS enabled Boost ASIO endpoint socket component.
lib::function< void(connection_hdl, boost::asio::ssl::stream< boost::asio::ip::tcp::socket > &)> socket_init_handler
The signature of the socket_init_handler for this socket policy.
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
void cancel_socket()
Cancel all async operations on this socket.
socket_con_type::ptr socket_con_ptr