MySQL 8.4.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
44template <typename LowerLayer>
45class TlsStream : private TlsBase<LowerLayer> {
46 public:
49 using endpoint_type = typename LowerLayer::endpoint_type;
51
52 public:
53 // Import constructor
54 using Parent::TlsBase;
55
56 void set_parent(const char *) {}
57
58 auto get_executor() { return lower_layer().get_executor(); }
59 auto cancel() { return lower_layer().cancel(); }
60
63 }
64
65 const typename Parent::LowerLayerType &lower_layer() const {
67 }
68
69 bool is_open() const { return lower_layer().is_open(); }
70
71 auto connect(const endpoint_type &endpoint) {
72 // The call might initialize SSL handshake.
73 // Current implementation is sufficient.
74 lower_layer().connect(endpoint);
75 }
76
77 template <class CompletionToken>
78 auto async_connect(const endpoint_type &endpoint, CompletionToken &&token) {
79 // The call might initialize SSL handshake.
80 // Current implementation is sufficient.
81 lower_layer().async_connect(endpoint, std::forward<CompletionToken>(token));
82 }
83
84 template <class MutableBufferSequence, class CompletionToken>
85 auto async_receive(const MutableBufferSequence &buffers,
86 CompletionToken &&token) {
87 SslIoCompletionToken<SslReadOperation, MutableBufferSequence,
88 CompletionToken, Parent>
89 io_token(*this, buffers, token);
90
91 io_token.do_it();
92 }
93
94 template <class ConstBufferSequence, class CompletionToken>
95 auto async_send(const ConstBufferSequence &buffers,
96 CompletionToken &&user_token) {
98 "");
99
100 SslIoCompletionToken<SslWriteOperation, ConstBufferSequence,
101 CompletionToken, Parent>
102 io_token(*this, buffers, user_token);
103
104 io_token.do_it();
105 }
106
107 template <typename ConstBufferSequence>
108 Io_result_type write_some(const ConstBufferSequence &buffers) {
110 "");
111
113 SyncAction sync_action;
114 auto handle_write_done = [&result](std::error_code ec, size_t s) {
115 if (ec)
117 else
118 result = s;
119 };
120 SslIoCompletionToken<SslWriteOperation, ConstBufferSequence,
121 decltype(handle_write_done), Parent, SyncAction &>
122 it(*this, buffers, std::move(handle_write_done), sync_action);
123
124 SyncAction::Handler_result handle_result{it.do_it()};
125
126 while (handle_result) {
127 switch (handle_result.value()) {
128 case Operation::Result::want_read:
129 handle_result = sync_action.handle_read_result(&it);
130 break;
131
132 case Operation::Result::want_write:
133 handle_result = sync_action.handle_write_result(&it);
134 break;
135
136 default:
138 }
139 }
140
141 return result;
142 }
143
144 template <typename MutableBufferSequence>
145 Io_result_type read_some(const MutableBufferSequence &buffers) {
147 size_t total{0};
148 SyncAction sync_action;
149 auto handle_read_done = [&result, &total](std::error_code ec, size_t s) {
150 total += s;
151 if (ec)
153 else
154 result = total;
155 };
156 SslIoCompletionToken<SslReadOperation, MutableBufferSequence,
157 decltype(handle_read_done), Parent, SyncAction &>
158 it(*this, buffers, std::move(handle_read_done), sync_action);
159
160 SyncAction::Handler_result handle_result{it.do_it()};
161
162 while (handle_result) {
163 switch (handle_result.value()) {
164 case Operation::Result::want_read:
165 handle_result = sync_action.handle_read_result(&it);
166 break;
167
168 case Operation::Result::want_write:
169 handle_result = sync_action.handle_write_result(&it);
170 break;
171
172 default:
174 }
175 }
176
177 return result;
178 }
179
180 template <typename SettableSocketOption>
182 const SettableSocketOption &option) {
183 return lower_layer().set_option(option);
184 }
185
186 auto close() { return lower_layer().close(); }
187 auto release() { return lower_layer().release(); }
188 auto native_handle() { return lower_layer().native_handle(); }
189};
190
191} // namespace tls
192} // namespace net
193
194#endif // ROUTER_SRC_OPENSSL_INCLUDE_TLS_TLS_STREAM_H_
Definition: ssl_io_completion.h:129
Definition: ssl_operation.h:98
Definition: ssl_operation.h:131
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:45
auto connect(const endpoint_type &endpoint)
Definition: tls_stream.h:71
auto native_handle()
Definition: tls_stream.h:188
Parent::LowerLayerType & lower_layer()
Definition: tls_stream.h:61
auto async_receive(const MutableBufferSequence &buffers, CompletionToken &&token)
Definition: tls_stream.h:85
const Parent::LowerLayerType & lower_layer() const
Definition: tls_stream.h:65
auto release()
Definition: tls_stream.h:187
typename LowerLayer::endpoint_type endpoint_type
Definition: tls_stream.h:49
auto cancel()
Definition: tls_stream.h:59
auto get_executor()
Definition: tls_stream.h:58
auto async_connect(const endpoint_type &endpoint, CompletionToken &&token)
Definition: tls_stream.h:78
auto close()
Definition: tls_stream.h:186
auto async_send(const ConstBufferSequence &buffers, CompletionToken &&user_token)
Definition: tls_stream.h:95
Io_result_type read_some(const MutableBufferSequence &buffers)
Definition: tls_stream.h:145
stdx::expected< void, std::error_code > set_option(const SettableSocketOption &option)
Definition: tls_stream.h:181
bool is_open() const
Definition: tls_stream.h:69
void set_parent(const char *)
Definition: tls_stream.h:56
Io_result_type write_some(const ConstBufferSequence &buffers)
Definition: tls_stream.h:108
Type total(const Shards< COUNT > &shards) noexcept
Get the total value of all shards.
Definition: ut0counter.h:333
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
Definition: buffer.h:259
Definition: result.h:30