TrinityCore
Loading...
Searching...
No Matches
SslStream.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_SSL_STREAM_H
19#define TRINITYCORE_SSL_STREAM_H
20
22#include <boost/asio/ip/tcp.hpp>
23#include <boost/asio/ssl/stream.hpp>
24#include <boost/system/error_code.hpp>
25
26namespace Trinity::Net
27{
28template <typename SocketImpl>
30{
31 explicit SslHandshakeConnectionInitializer(SocketImpl* socket) : _socket(socket) { }
32
33 void Start() override
34 {
35 _socket->underlying_stream().async_handshake(boost::asio::ssl::stream_base::server,
36 [socketRef = _socket->weak_from_this(), self = this->shared_from_this()](boost::system::error_code const& error)
37 {
38 std::shared_ptr<SocketImpl> socket = static_pointer_cast<SocketImpl>(socketRef.lock());
39 if (!socket)
40 return;
41
42 if (error)
43 {
44 TC_LOG_ERROR("session", "{} SSL Handshake failed {}", socket->GetClientInfo(), error.message());
45 socket->CloseSocket();
46 return;
47 }
48
49 if (self->next)
50 self->next->Start();
51 });
52 }
53
54private:
55 SocketImpl* _socket;
56};
57
58template<class WrappedStream = IoContextTcpSocket>
60{
61public:
62 explicit SslStream(IoContextTcpSocket&& socket, boost::asio::ssl::context& sslContext) : _sslSocket(std::move(socket), sslContext)
63 {
64 _sslSocket.set_verify_mode(boost::asio::ssl::verify_none);
65 }
66
67 explicit SslStream(boost::asio::io_context& context, boost::asio::ssl::context& sslContext) : _sslSocket(context, sslContext)
68 {
69 _sslSocket.set_verify_mode(boost::asio::ssl::verify_none);
70 }
71
72 // adapting tcp::socket api
73 void close(boost::system::error_code& error)
74 {
75 _sslSocket.next_layer().close(error);
76 }
77
78 void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
79 {
80 _sslSocket.shutdown(shutdownError);
81 _sslSocket.next_layer().shutdown(what, shutdownError);
82 }
83
84 template<typename MutableBufferSequence, typename ReadHandlerType>
85 void async_read_some(MutableBufferSequence const& buffers, ReadHandlerType&& handler)
86 {
87 _sslSocket.async_read_some(buffers, std::forward<ReadHandlerType>(handler));
88 }
89
90 template<typename ConstBufferSequence, typename WriteHandlerType>
91 void async_write_some(ConstBufferSequence const& buffers, WriteHandlerType&& handler)
92 {
93 _sslSocket.async_write_some(buffers, std::forward<WriteHandlerType>(handler));
94 }
95
96 template<typename ConstBufferSequence>
97 std::size_t write_some(ConstBufferSequence const& buffers, boost::system::error_code& error)
98 {
99 return _sslSocket.write_some(buffers, error);
100 }
101
102 template<typename WaitHandlerType>
103 void async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType&& handler)
104 {
105 _sslSocket.next_layer().async_wait(type, std::forward<WaitHandlerType>(handler));
106 }
107
108 template<typename SettableSocketOption>
109 void set_option(SettableSocketOption const& option, boost::system::error_code& error)
110 {
111 _sslSocket.next_layer().set_option(option, error);
112 }
113
114 IoContextTcpSocket::endpoint_type remote_endpoint() const
115 {
116 return _sslSocket.next_layer().remote_endpoint();
117 }
118
119 // ssl api
120 template<typename HandshakeHandlerType>
121 void async_handshake(boost::asio::ssl::stream_base::handshake_type type, HandshakeHandlerType&& handler)
122 {
123 _sslSocket.async_handshake(type, std::forward<HandshakeHandlerType>(handler));
124 }
125
126protected:
127 boost::asio::ssl::stream<WrappedStream> _sslSocket;
128};
129}
130
131#endif // TRINITYCORE_SSL_STREAM_H
if(posix_memalign(&__mallocedMemory, __align, __size)) return NULL
SslStream(IoContextTcpSocket &&socket, boost::asio::ssl::context &sslContext)
Definition SslStream.h:62
void async_read_some(MutableBufferSequence const &buffers, ReadHandlerType &&handler)
Definition SslStream.h:85
IoContextTcpSocket::endpoint_type remote_endpoint() const
Definition SslStream.h:114
void async_handshake(boost::asio::ssl::stream_base::handshake_type type, HandshakeHandlerType &&handler)
Definition SslStream.h:121
void async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType &&handler)
Definition SslStream.h:103
SslStream(boost::asio::io_context &context, boost::asio::ssl::context &sslContext)
Definition SslStream.h:67
void set_option(SettableSocketOption const &option, boost::system::error_code &error)
Definition SslStream.h:109
void close(boost::system::error_code &error)
Definition SslStream.h:73
void async_write_some(ConstBufferSequence const &buffers, WriteHandlerType &&handler)
Definition SslStream.h:91
std::size_t write_some(ConstBufferSequence const &buffers, boost::system::error_code &error)
Definition SslStream.h:97
void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code &shutdownError)
Definition SslStream.h:78
boost::asio::ssl::stream< WrappedStream > _sslSocket
Definition SslStream.h:127
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket
Definition Socket.h:41
STL namespace.
SslHandshakeConnectionInitializer(SocketImpl *socket)
Definition SslStream.h:31