MySQL 9.3.0
Source Code Documentation
tls_stream.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_STREAM_H_
27#define ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_STREAM_H_
28
29#include <errno.h>
30#include <memory>
31#include <utility>
32
36
40
41namespace net {
42namespace tls {
43
45
46template <typename LowerLayer>
47class TlsStream : private TlsBase<LowerLayer> {
48 public:
51 using endpoint_type = typename LowerLayer::endpoint_type;
53
54 public:
55 // Import constructor
56 using Parent::TlsBase;
57
58 void set_parent(const char *) {}
59
60 auto get_executor() { return lower_layer().get_executor(); }
61 auto cancel() { return lower_layer().cancel(); }
62
63 auto shutdown(typename LowerLayer::shutdown_type sd) {
64 return lower_layer().shutdown(sd);
65 }
66
69 }
70
71 const typename Parent::LowerLayerType &lower_layer() const {
73 }
74
75 bool is_open() const { return lower_layer().is_open(); }
76
77 auto connect(const endpoint_type &endpoint) {
78 // The call might initialize SSL handshake.
79 // Current implementation is sufficient.
80 return lower_layer().connect(endpoint);
81 }
82
83 template <class CompletionToken>
84 auto async_connect(const endpoint_type &endpoint, CompletionToken &&token) {
85 // The call might initialize SSL handshake.
86 // Current implementation is sufficient.
87 lower_layer().async_connect(endpoint, std::forward<CompletionToken>(token));
88 }
89
90 template <class CompletionToken>
91 auto async_handshake(HandshakeType type, CompletionToken &&token) {
92 if (type == kServer) {
93 assert(false && "Server handshake is not supported.");
94 return;
95 }
96
98 CompletionToken, Parent>
99 io_token(*this, {}, token);
100
101 io_token.do_it();
102 }
103
104 template <class MutableBufferSequence, class CompletionToken>
105 auto async_receive(const MutableBufferSequence &buffers,
106 CompletionToken &&token) {
107 SslIoCompletionToken<SslReadOperation, MutableBufferSequence,
108 CompletionToken, Parent>
109 io_token(*this, buffers, token);
110
111 io_token.do_it();
112 }
113
114 template <class ConstBufferSequence, class CompletionToken>
115 auto async_send(const ConstBufferSequence &buffers,
116 CompletionToken &&user_token) {
118 "");
119
120 SslIoCompletionToken<SslWriteOperation, ConstBufferSequence,
121 CompletionToken, Parent>
122 io_token(*this, buffers, user_token);
123
124 io_token.do_it();
125 }
126
127 template <typename ConstBufferSequence>
128 Io_result_type write_some(const ConstBufferSequence &buffers) {
130 "");
131
133 SyncAction sync_action;
134 auto handle_write_done = [&result](std::error_code ec, size_t s) {
135 if (ec)
137 else
138 result = s;
139 };
140 SslIoCompletionToken<SslWriteOperation, ConstBufferSequence,
141 decltype(handle_write_done), Parent, SyncAction &>
142 it(*this, buffers, std::move(handle_write_done), sync_action);
143
144 SyncAction::Handler_result handle_result{it.do_it()};
145
146 while (handle_result) {
147 switch (handle_result.value()) {
148 case Operation::Result::want_read:
149 handle_result = sync_action.handle_read_result(&it);
150 break;
151
152 case Operation::Result::want_write:
153 handle_result = sync_action.handle_write_result(&it);
154 break;
155
156 default:
158 }
159 }
160
161 return result;
162 }
163
164 template <typename MutableBufferSequence>
165 Io_result_type read_some(const MutableBufferSequence &buffers) {
167 size_t total{0};
168 SyncAction sync_action;
169 auto handle_read_done = [&result, &total](std::error_code ec, size_t s) {
170 total += s;
171 if (ec)
173 else
174 result = total;
175 };
176 SslIoCompletionToken<SslReadOperation, MutableBufferSequence,
177 decltype(handle_read_done), Parent, SyncAction &>
178 it(*this, buffers, std::move(handle_read_done), sync_action);
179
180 SyncAction::Handler_result handle_result{it.do_it()};
181
182 while (handle_result) {
183 switch (handle_result.value()) {
184 case Operation::Result::want_read:
185 handle_result = sync_action.handle_read_result(&it);
186 break;
187
188 case Operation::Result::want_write:
189 handle_result = sync_action.handle_write_result(&it);
190 break;
191
192 default:
194 }
195 }
196
197 return result;
198 }
199
200 template <typename SettableSocketOption>
202 const SettableSocketOption &option) {
203 return lower_layer().set_option(option);
204 }
205
206 auto close() { return lower_layer().close(); }
207 auto release() { return lower_layer().release(); }
208 auto native_handle() { return lower_layer().native_handle(); }
209};
210
211} // namespace tls
212} // namespace net
213
214#endif // ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_STREAM_H_
Definition: buffer.h:113
Definition: ssl_operation.h:165
Definition: ssl_io_completion.h:129
Definition: ssl_operation.h:99
Definition: ssl_operation.h:132
Definition: ssl_io_completion.h:85
Handler_result handle_write_result(Handler *handler)
Definition: ssl_io_completion.h:102
Handler_result handle_read_result(Handler *handler)
Definition: ssl_io_completion.h:113
Definition: tls_base.h:42
LowerLayer lower_layer_
Definition: tls_base.h:95
LowerLayer LowerLayerType
Definition: tls_base.h:68
TlsBase(LowerLayer &&layer, TlsContext *tls_context)
Definition: tls_base.h:56
Definition: tls_stream.h:47
auto connect(const endpoint_type &endpoint)
Definition: tls_stream.h:77
auto native_handle()
Definition: tls_stream.h:208
Parent::LowerLayerType & lower_layer()
Definition: tls_stream.h:67
auto async_receive(const MutableBufferSequence &buffers, CompletionToken &&token)
Definition: tls_stream.h:105
const Parent::LowerLayerType & lower_layer() const
Definition: tls_stream.h:71
auto release()
Definition: tls_stream.h:207
auto async_handshake(HandshakeType type, CompletionToken &&token)
Definition: tls_stream.h:91
typename LowerLayer::endpoint_type endpoint_type
Definition: tls_stream.h:51
auto cancel()
Definition: tls_stream.h:61
auto get_executor()
Definition: tls_stream.h:60
auto async_connect(const endpoint_type &endpoint, CompletionToken &&token)
Definition: tls_stream.h:84
auto shutdown(typename LowerLayer::shutdown_type sd)
Definition: tls_stream.h:63
auto close()
Definition: tls_stream.h:206
auto async_send(const ConstBufferSequence &buffers, CompletionToken &&user_token)
Definition: tls_stream.h:115
Io_result_type read_some(const MutableBufferSequence &buffers)
Definition: tls_stream.h:165
stdx::expected< void, std::error_code > set_option(const SettableSocketOption &option)
Definition: tls_stream.h:201
bool is_open() const
Definition: tls_stream.h:75
void set_parent(const char *)
Definition: tls_stream.h:58
Io_result_type write_some(const ConstBufferSequence &buffers)
Definition: tls_stream.h:128
Type total(const Shards< COUNT > &shards) noexcept
Get the total value of all shards.
Definition: ut0counter.h:333
HandshakeType
Definition: tls_stream.h:44
@ kServer
Definition: tls_stream.h:44
@ kClient
Definition: tls_stream.h:44
Definition: buffer.h:45
constexpr unexpect_t unexpect
Definition: expected.h:109
unexpected(E) -> unexpected< E >
Definition: tls_keylog_dumper.h:35
struct result result
Definition: result.h:34
required string type
Definition: replication_group_member_actions.proto:34
Definition: buffer.h:259
Definition: result.h:30