2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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>
55typedef lib::function<lib::shared_ptr<lib::asio::ssl::context>(
connection_hdl)>
60
61
62
71 typedef lib::asio::ssl::stream<lib::asio::ip::tcp::socket>
socket_type;
78
79
82 typedef lib::shared_ptr<lib::asio::io_context::strand>
strand_ptr;
86 explicit connection() {
93 return shared_from_this();
98
99
106
107
109 return m_socket->lowest_layer();
114
115
117 return m_socket->next_layer();
122
123
130
131
132
133
134
135
137 m_socket_init_handler = h;
142
143
144
145
146
147
148
150 m_tls_init_handler = h;
155
156
157
158
159
160
161
162
166 lib::asio::error_code aec;
167 lib::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(aec);
170 ec =
error::make_error_code(error::pass_through);
171 s <<
"Error getting remote endpoint: " << aec
172 <<
" (" << aec.message() <<
")";
175 ec = lib::error_code();
183
184
185
186
187
188
189
193 if (!m_tls_init_handler) {
196 m_context = m_tls_init_handler(m_hdl);
201 m_socket.reset(
new socket_type(*context, *m_context));
203 m_io_context = context;
205 m_is_server = is_server;
207 return lib::error_code();
212
213
214
215
216
217
218
219
220
221
228
229
230
231
232
233
234
238#if OPENSSL_VERSION_NUMBER
>= 0x90812f
243 std::string
const & host = m_uri->get_host();
244 lib::asio::error_code ec_addr;
247 lib::asio::ip::address addr = lib::asio::ip::make_address(host, ec_addr);
255 long res = SSL_set_tlsext_host_name(
263 if (m_socket_init_handler) {
264 m_socket_init_handler(m_hdl, get_socket());
267 callback(lib::error_code());
272
273
274
275
276
277
283 m_socket->async_handshake(
284 get_handshake_type(),
285 lib::asio::bind_executor(*m_strand, lib::bind(
286 &type::handle_init, get_shared(),
288 lib::placeholders::_1
292 m_socket->async_handshake(
293 get_handshake_type(),
295 &type::handle_init, get_shared(),
297 lib::placeholders::_1
305
306
307
308
309
314 void handle_init(init_handler callback,lib::asio::error_code
const & ec) {
318 m_ec = lib::error_code();
324 lib::error_code get_ec()
const {
330
331
332
333
334
335
336
338 lib::asio::error_code ec;
339 get_raw_socket().cancel(ec);
343 void async_shutdown(socket::shutdown_handler callback) {
345 m_socket->async_shutdown(lib::asio::bind_executor(*m_strand, callback));
347 m_socket->async_shutdown(callback);
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371 template <
typename ErrorCodeType>
374 if (ec.category() == lib::asio::error::get_ssl_category()) {
377 return make_error_code(transport::error::tls_error);
381 return make_error_code(transport::error::pass_through);
392 socket_type::handshake_type get_handshake_type() {
394 return lib::asio::ssl::stream_base::server;
396 return lib::asio::ssl::stream_base::client;
407 lib::error_code m_ec;
416
417
418
430 explicit endpoint() {}
434
435
442
443
444
445
446
447
449 m_socket_init_handler = h;
454
455
456
457
458
459
460
462 m_tls_init_handler = h;
467
468
469
470
471
472
473
475 scon->set_socket_init_handler(m_socket_init_handler);
476 scon->set_tls_init_handler(m_tls_init_handler);
477 return lib::error_code();
TLS enabled Asio connection socket component.
bool is_secure() const
Check whether or not this connection is secure.
connection type
Type of this connection socket component.
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.
lib::shared_ptr< lib::asio::ssl::context > context_ptr
Type of a shared pointer to the ASIO TLS context being used.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
io_context_ptr io_service_ptr
Type of a pointer to the ASIO io_context being used (deprecated).
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the ASIO socket being used.
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
static lib::error_code translate_ec(lib::error_code ec)
lib::error_code init_asio(io_context_ptr context, 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.
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.
void set_uri(uri_ptr u)
Set hostname hook.
lib::asio::ssl::stream< lib::asio::ip::tcp::socket > socket_type
Type of the ASIO socket being used.
static lib::error_code translate_ec(ErrorCodeType ec)
Translate any security policy specific information about an error code.
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::asio::io_context * io_context_ptr
Type of a pointer to the ASIO io_context being used.
lib::shared_ptr< lib::asio::io_context::strand > strand_ptr
Type of a pointer to the ASIO io_context strand 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.
connection socket_con_type
The type of the corresponding connection socket component.
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.
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Library level error codes.
Errors related to asio transport sockets.
@ 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(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.
Transport policy that uses asio.
Transport policies provide network connectivity and timers.
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.