WebSocket++
0.8.3-dev
C++ websocket client/server library
|
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/uri.hpp>
35 #include <websocketpp/common/asio_ssl.hpp>
36 #include <websocketpp/common/asio.hpp>
37 #include <websocketpp/common/connection_hdl.hpp>
38 #include <websocketpp/common/functional.hpp>
39 #include <websocketpp/common/memory.hpp>
81 explicit connection() {
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);
165 ec = error::make_error_code(error::pass_through);
166 s <<
"Error getting remote endpoint: " << aec
167 <<
" (" << aec.message() <<
")";
170 ec = lib::error_code();
188 if (!m_tls_init_handler) {
189 return socket::make_error_code(socket::error::missing_tls_init_handler);
191 m_context = m_tls_init_handler(m_hdl);
194 return socket::make_error_code(socket::error::invalid_tls_context);
196 m_socket.reset(
new socket_type(*service, *m_context));
198 if (m_socket_init_handler) {
199 m_socket_init_handler(m_hdl, get_socket());
202 m_io_service = service;
204 m_is_server = is_server;
206 return lib::error_code();
238 #if OPENSSL_VERSION_NUMBER >= 0x90812f
243 long res = SSL_set_tlsext_host_name(
244 get_socket().native_handle(), m_uri->get_host().c_str());
246 callback(socket::make_error_code(socket::error::tls_failed_sni_hostname));
251 callback(lib::error_code());
263 m_ec = socket::make_error_code(socket::error::tls_handshake_timeout);
267 m_socket->async_handshake(
268 get_handshake_type(),
269 m_strand->wrap(lib::bind(
270 &type::handle_init, get_shared(),
272 lib::placeholders::_1
276 m_socket->async_handshake(
277 get_handshake_type(),
279 &type::handle_init, get_shared(),
281 lib::placeholders::_1
298 void handle_init(init_handler callback,lib::asio::error_code
const & ec) {
300 m_ec = socket::make_error_code(socket::error::tls_handshake_failed);
302 m_ec = lib::error_code();
308 lib::error_code get_ec()
const {
322 lib::asio::error_code ec;
323 get_raw_socket().cancel(ec);
327 void async_shutdown(socket::shutdown_handler callback) {
329 m_socket->async_shutdown(m_strand->wrap(callback));
331 m_socket->async_shutdown(callback);
355 template <
typename ErrorCodeType>
358 if (ec.category() == lib::asio::error::get_ssl_category()) {
361 return make_error_code(transport::error::tls_error);
365 return make_error_code(transport::error::pass_through);
376 socket_type::handshake_type get_handshake_type() {
378 return lib::asio::ssl::stream_base::server;
380 return lib::asio::ssl::stream_base::client;
391 lib::error_code m_ec;
393 connection_hdl m_hdl;
414 explicit endpoint() {}
433 m_socket_init_handler = h;
446 m_tls_init_handler = h;
459 scon->set_socket_init_handler(m_socket_init_handler);
460 scon->set_tls_init_handler(m_tls_init_handler);
461 return lib::error_code();
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::asio::error_code cancel_socket()
Cancel all async operations on this socket.
TLS enabled Asio endpoint socket component.
Transport policy that uses asio.
ptr get_shared()
Get a shared pointer to this component.
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.
void set_handle(connection_hdl hdl)
Sets the connection handle.
connection socket_con_type
The type of the corresponding connection socket component.
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
socket_type::next_layer_type & get_next_layer()
Retrieve a pointer to the layer below the ssl stream.
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.
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
Perform one time initializations.
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
endpoint type
The type of this endpoint socket component.
void set_uri(uri_ptr u)
Set hostname hook.
void pre_init(init_handler callback)
Pre-initialize security policy.
void post_init(init_handler callback)
Post-initialize security policy.
connection type
Type of this connection socket component.
lib::shared_ptr< lib::asio::io_service::strand > strand_ptr
Type of a pointer to the ASIO io_service strand being used.
static lib::error_code translate_ec(ErrorCodeType 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.
socket_con_type::ptr socket_con_ptr
bool is_secure() const
Checks whether the endpoint creates secure connections.
static lib::error_code translate_ec(lib::error_code ec)
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
socket_type::lowest_layer_type & get_raw_socket()
Retrieve a pointer to the underlying socket.
lib::shared_ptr< lib::asio::ssl::context > context_ptr
Type of a shared pointer to the ASIO TLS context being used.