28#ifndef WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
29#define WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
55typedef lib::function<lib::shared_ptr<lib::asio::ssl::context>(
connection_hdl)>
63class connection :
public lib::enable_shared_from_this<connection> {
68 typedef lib::shared_ptr<type>
ptr;
71 typedef lib::asio::ssl::stream<lib::asio::ip::tcp::socket>
socket_type;
77 typedef lib::shared_ptr<lib::asio::io_service::strand>
strand_ptr;
88 return shared_from_this();
104 return m_socket->lowest_layer();
112 return m_socket->next_layer();
132 m_socket_init_handler = h;
145 m_tls_init_handler = h;
161 lib::asio::error_code aec;
162 lib::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(aec);
166 s <<
"Error getting remote endpoint: " << aec
167 <<
" (" << aec.message() <<
")";
170 ec = lib::error_code();
188 if (!m_tls_init_handler) {
191 m_context = m_tls_init_handler(m_hdl);
196 m_socket = lib::make_shared<socket_type>(*service, *m_context);
198 m_io_service = service;
200 m_is_server = is_server;
202 return lib::error_code();
234#if OPENSSL_VERSION_NUMBER >= 0x90812f
239 long res = SSL_set_tlsext_host_name(
240 get_socket().native_handle(), m_uri->get_host().c_str());
247 if (m_socket_init_handler) {
251 callback(lib::error_code());
267 m_socket->async_handshake(
268 get_handshake_type(),
269 m_strand->wrap(lib::bind(
272 lib::placeholders::_1
276 m_socket->async_handshake(
277 get_handshake_type(),
281 lib::placeholders::_1
302 m_ec = lib::error_code();
322 lib::asio::error_code ec;
329 m_socket->async_shutdown(m_strand->wrap(callback));
331 m_socket->async_shutdown(callback);
354 template <
typename ErrorCodeType>
356 if (ec.category() == lib::asio::error::get_ssl_category()) {
381 socket_type::handshake_type get_handshake_type() {
383 return lib::asio::ssl::stream_base::server;
385 return lib::asio::ssl::stream_base::client;
396 lib::error_code m_ec;
438 m_socket_init_handler = h;
451 m_tls_init_handler = h;
464 scon->set_socket_init_handler(m_socket_init_handler);
465 scon->set_tls_init_handler(m_tls_init_handler);
466 return lib::error_code();
TLS enabled Asio connection socket component.
lib::asio::ssl::stream< lib::asio::ip::tcp::socket > socket_type
Type of the ASIO socket being used.
bool is_secure() const
Check whether or not this connection is secure.
lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
Perform one time initializations.
void pre_init(init_handler callback)
Pre-initialize security policy.
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
void set_handle(connection_hdl hdl)
Sets the connection handle.
void async_shutdown(socket::shutdown_handler callback)
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
connection type
Type of this connection socket component.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
lib::error_code translate_ec(ErrorCodeType ec)
Translate any security policy specific information about an error code.
socket_type::lowest_layer_type & get_raw_socket()
Retrieve a pointer to the underlying socket.
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
lib::shared_ptr< lib::asio::io_service::strand > strand_ptr
Type of a pointer to the ASIO io_service strand being used.
void handle_init(init_handler callback, lib::asio::error_code const &ec)
lib::error_code translate_ec(lib::error_code ec)
void set_uri(uri_ptr u)
Set hostname hook.
lib::error_code get_ec() const
void post_init(init_handler callback)
Post-initialize security policy.
ptr get_shared()
Get a shared pointer to this component.
socket_type::next_layer_type & get_next_layer()
Retrieve a pointer to the layer below the ssl stream.
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the ASIO socket being used.
lib::shared_ptr< lib::asio::ssl::context > context_ptr
Type of a shared pointer to the ASIO TLS context being used.
lib::asio::error_code cancel_socket()
Cancel all async operations on this socket.
TLS enabled Asio endpoint socket component.
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
bool is_secure() const
Checks whether the endpoint creates secure connections.
socket_con_type::ptr socket_con_ptr
endpoint type
The type of this endpoint socket component.
connection socket_con_type
The type of the corresponding connection socket component.
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
lib::error_code make_error_code(error::value e)
Create an error code with the given value and the asio transport category.
@ pass_through
there was an error in the underlying transport library
@ missing_tls_init_handler
Required tls_init handler not present.
@ tls_failed_sni_hostname
Failed to set TLS SNI hostname.
@ tls_handshake_failed
TLS Handshake Failed.
@ tls_handshake_timeout
TLS Handshake Timeout.
lib::function< void(lib::asio::error_code const &)> shutdown_handler
lib::error_code make_error_code(error::value e)
lib::function< void(connection_hdl, lib::asio::ssl::stream< lib::asio::ip::tcp::socket > &)> socket_init_handler
The signature of the socket_init_handler for this socket policy.
lib::function< lib::shared_ptr< lib::asio::ssl::context >(connection_hdl)> tls_init_handler
The signature of the tls_init_handler for this socket policy.
@ pass_through
underlying transport pass through
@ tls_error
Other TLS error.
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
Namespace for the WebSocket++ project.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.