MySQL 9.1.0
Source Code Documentation
tls_stream.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2024, 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
65 }
66
67 const typename Parent::LowerLayerType &lower_layer() const {
69 }
70
71 bool is_open() const { return lower_layer().is_open(); }
72
73 auto connect(const endpoint_type &endpoint) {
74 // The call might initialize SSL handshake.
75 // Current implementation is sufficient.
76 return lower_layer().connect(endpoint);
77 }
78
79 template <class CompletionToken>
80 auto async_connect(const endpoint_type &endpoint, CompletionToken &&token) {
81 // The call might initialize SSL handshake.
82 // Current implementation is sufficient.
83 lower_layer().async_connect(endpoint, std::forward<CompletionToken>(token));
84 }
85
86 template <class CompletionToken>
87 auto async_handshake(HandshakeType type, CompletionToken &&token) {
88 if (type == kServer) {
89 assert(false && "Server handshake is not supported.");
90 return;
91 }
92
94 CompletionToken, Parent>
95 io_token(*this, {}, token);
96
97 io_token.do_it();
98 }
99
100 template <class MutableBufferSequence, class CompletionToken>
101 auto async_receive(const MutableBufferSequence &buffers,
102 CompletionToken &&token) {
103 SslIoCompletionToken<SslReadOperation, MutableBufferSequence,
104 CompletionToken, Parent>
105 io_token(*this, buffers, token);
106
107 io_token.do_it();
108 }
109
110 template <class ConstBufferSequence, class CompletionToken>
111 auto async_send(const ConstBufferSequence &buffers,
112 CompletionToken &&user_token) {
114 "");
115
116 SslIoCompletionToken<SslWriteOperation, ConstBufferSequence,
117 CompletionToken, Parent>
118 io_token(*this, buffers, user_token);
119
120 io_token.do_it();
121 }
122
123 template <typename ConstBufferSequence>
124 Io_result_type write_some(const ConstBufferSequence &buffers) {
126 "");
127
129 SyncAction sync_action;
130 auto handle_write_done = [&result](std::error_code ec, size_t s) {
131 if (ec)
133 else
134 result = s;
135 };
136 SslIoCompletionToken<SslWriteOperation, ConstBufferSequence,
137 decltype(handle_write_done), Parent, SyncAction &>
138 it(*this, buffers, std::move(handle_write_done), sync_action);
139
140 SyncAction::Handler_result handle_result{it.do_it()};
141
142 while (handle_result) {
143 switch (handle_result.value()) {
144 case Operation::Result::want_read:
145 handle_result = sync_action.handle_read_result(&it);
146 break;
147
148 case Operation::Result::want_write:
149 handle_result = sync_action.handle_write_result(&it);
150 break;
151
152 default:
154 }
155 }
156
157 return result;
158 }
159
160 template <typename MutableBufferSequence>
161 Io_result_type read_some(const MutableBufferSequence &buffers) {
163 size_t total{0};
164 SyncAction sync_action;
165 auto handle_read_done = [&result, &total](std::error_code ec, size_t s) {
166 total += s;
167 if (ec)
169 else
170 result = total;
171 };
172 SslIoCompletionToken<SslReadOperation, MutableBufferSequence,
173 decltype(handle_read_done), Parent, SyncAction &>
174 it(*this, buffers, std::move(handle_read_done), sync_action);
175
176 SyncAction::Handler_result handle_result{it.do_it()};
177
178 while (handle_result) {
179 switch (handle_result.value()) {
180 case Operation::Result::want_read:
181 handle_result = sync_action.handle_read_result(&it);
182 break;
183
184 case Operation::Result::want_write:
185 handle_result = sync_action.handle_write_result(&it);
186 break;
187
188 default:
190 }
191 }
192
193 return result;
194 }
195
196 template <typename SettableSocketOption>
198 const SettableSocketOption &option) {
199 return lower_layer().set_option(option);
200 }
201
202 auto close() { return lower_layer().close(); }
203 auto release() { return lower_layer().release(); }
204 auto native_handle() { return lower_layer().native_handle(); }
205};
206
207} // namespace tls
208} // namespace net
209
210#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:73
auto native_handle()
Definition: tls_stream.h:204
Parent::LowerLayerType & lower_layer()
Definition: tls_stream.h:63
auto async_receive(const MutableBufferSequence &buffers, CompletionToken &&token)
Definition: tls_stream.h:101
const Parent::LowerLayerType & lower_layer() const
Definition: tls_stream.h:67
auto release()
Definition: tls_stream.h:203
auto async_handshake(HandshakeType type, CompletionToken &&token)
Definition: tls_stream.h:87
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:80
auto close()
Definition: tls_stream.h:202
auto async_send(const ConstBufferSequence &buffers, CompletionToken &&user_token)
Definition: tls_stream.h:111
Io_result_type read_some(const MutableBufferSequence &buffers)
Definition: tls_stream.h:161
stdx::expected< void, std::error_code > set_option(const SettableSocketOption &option)
Definition: tls_stream.h:197
bool is_open() const
Definition: tls_stream.h:71
void set_parent(const char *)
Definition: tls_stream.h:58
Io_result_type write_some(const ConstBufferSequence &buffers)
Definition: tls_stream.h:124
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:32
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