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;
77 typedef lib::shared_ptr<lib::asio::io_context::strand>
strand_ptr;
81 explicit connection() {
88 return shared_from_this();
93
94
101
102
104 return m_socket->lowest_layer();
109
110
112 return m_socket->next_layer();
117
118
125
126
127
128
129
130
132 m_socket_init_handler = h;
137
138
139
140
141
142
143
145 m_tls_init_handler = h;
150
151
152
153
154
155
156
157
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();
178
179
180
181
182
183
184
188 if (!m_tls_init_handler) {
191 m_context = m_tls_init_handler(m_hdl);
196 m_socket.reset(
new socket_type(*context, *m_context));
198 m_io_context = context;
200 m_is_server = is_server;
202 return lib::error_code();
207
208
209
210
211
212
213
214
215
216
223
224
225
226
227
228
229
233#if OPENSSL_VERSION_NUMBER
>= 0x90812f
238 std::string
const & host = m_uri->get_host();
239 lib::asio::error_code ec_addr;
242 lib::asio::ip::address addr = lib::asio::ip::make_address(host, ec_addr);
250 long res = SSL_set_tlsext_host_name(
258 if (m_socket_init_handler) {
259 m_socket_init_handler(m_hdl, get_socket());
262 callback(lib::error_code());
267
268
269
270
271
272
278 m_socket->async_handshake(
279 get_handshake_type(),
280 lib::asio::bind_executor(*m_strand, lib::bind(
281 &type::handle_init, get_shared(),
283 lib::placeholders::_1
287 m_socket->async_handshake(
288 get_handshake_type(),
290 &type::handle_init, get_shared(),
292 lib::placeholders::_1
300
301
302
303
304
309 void handle_init(init_handler callback,lib::asio::error_code
const & ec) {
313 m_ec = lib::error_code();
319 lib::error_code get_ec()
const {
325
326
327
328
329
330
331
333 lib::asio::error_code ec;
334 get_raw_socket().cancel(ec);
338 void async_shutdown(socket::shutdown_handler callback) {
340 m_socket->async_shutdown(lib::asio::bind_executor(*m_strand, callback));
342 m_socket->async_shutdown(callback);
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 template <
typename ErrorCodeType>
369 if (ec.category() == lib::asio::error::get_ssl_category()) {
372 return make_error_code(transport::error::tls_error);
376 return make_error_code(transport::error::pass_through);
387 socket_type::handshake_type get_handshake_type() {
389 return lib::asio::ssl::stream_base::server;
391 return lib::asio::ssl::stream_base::client;
402 lib::error_code m_ec;
411
412
413
425 explicit endpoint() {}
429
430
437
438
439
440
441
442
444 m_socket_init_handler = h;
449
450
451
452
453
454
455
457 m_tls_init_handler = h;
462
463
464
465
466
467
468
470 scon->set_socket_init_handler(m_socket_init_handler);
471 scon->set_tls_init_handler(m_tls_init_handler);
472 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.
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.